Web API

DataAPI.describe — Function
describe(io::IO, x)

For an object x, print descriptive statistics to io.

This generic function is owned by StatsBase.jl, which is the sole provider of the default definition.

source
Cropbox.serve — Function
serve(S::Type{<:System}; host="127.0.0.1", port=8000, config=(), options=(), request_defaults=(), ui=(), dashboard=:static, async=false)

Expose a system through HTTP routes. Metadata routes publish the model description from describe and the API schema; operation routes parse JSON payloads and dispatch them to simulate, visualize, evaluate and calibrate.

Every request starts from what the service was given, and what a client sends wins over it:

  • config is the base configuration. The config of request_defaults, in the JSON shape of a request, is merged over it once when the service starts, and the config of a request over the result: a value replaces the one below it whichever of the parameter's names (alias, mixin) either used.
  • The other request_defaults (stop, snap, target, ...) stand in for the fields a request leaves out, in the operations that have them. They are trusted and may hold Julia values such as a snap function. The bundled dashboard starts from their stop and snap.
  • options, the constructor options of the system (the values of its extern variables), are the service's alone: a request has no such field.

ui arranges the bundled dashboard: its title, outputs, panels, sweeps and table_inputs.

Routes:

  • GET /api/model, GET /api/dashboard, GET /api/schema, GET /api/openapi, GET /api/docs
  • POST /api/simulate, POST /api/visualize, POST /api/evaluate, POST /api/calibrate
  • GET / and GET /dashboard (bundled browser dashboard; dashboard="/path" serves it there instead and dashboard=:none disables it)

The call blocks while serving unless async=true, which returns the server object. The service has no authentication or transport encryption; bind it to loopback unless it is placed behind an appropriate gateway. Clients can only set parameters that the model declares, and a table only from the data they send, but a model may itself declare a parameter that names a file to read (the filename of the store systems).

source
Cropbox.dashboard — Function
dashboard(S::Type{<:System}; frontend=:static, kwargs...)

Start the same service as serve. frontend is passed on as its dashboard option: :static for the bundled browser client at / and /dashboard, a path to serve it at instead, or :none for the API only.

source

The rest of this page specifies contract 0.2.0 of the HTTP interface. A running service has the same specification as an OpenAPI document at GET /api/openapi, which GET /api/docs renders with Swagger UI. The Web service guide walks through a first service.

Routes

RouteResult
GET /api/modelthe model description under the configuration of the service
GET /api/dashboardthe dashboard metadata
GET /api/schemathe route table, and the schemas of the OpenAPI document as definitions
GET /api/openapithe OpenAPI document
GET /api/docs, GET /docsSwagger UI for that document
POST /api/simulatea table
POST /api/visualizea plot as SVG
POST /api/evaluatethe value of a metric against observations
POST /api/calibratea calibrated configuration
GET /, GET /dashboardthe bundled dashboard page, unless dashboard moved or disabled it

Requests are stateless, and nothing in a request is evaluated as code.

Model description

describe() returns, and GET /api/model serves, an object with contract (its version), model (name, module), systems, parameters, variables and tables. The entries of the last three share these fields:

FieldMeaning
system, name, aliaswhere the variable is declared and what it is called
pathconfiguration path, System.name, under which a parameter is set
declaredthe same path through the system that declared the variable, which differs for a mixin
targetaccess path used as target, index, stop, x and y, such as leaf.area; null without a fixed path
descriptionthe docstring
state, type, unitthe declared state (preserve, track, ...), type and unit
roleparameter, table (a provide input), output or internal
tags, dependenciespublic tags of the declaration, and the variables it reads
defaultparameters only: the value in effect under the configuration of the service, as a value object that can be sent back
index, columnstables only: the index column, and the columns that drive variables read, with their units

A variable is an output when its state holds a value (track, accumulate, integrate, advance, remember, flag and the like) and internal otherwise (a call, a produced system). A system that the model only produces while it runs is described too. Its parameters are configured like any other, but it has no fixed access path: its path in systems and the target of its parameters are null, and its other variables are not listed. The context is left out, apart from the parameters that the configuration sets, such as the clock step.

Requests

FieldOperationsValue
configallparameter values; see Configurations
configssimulate, evaluate, calibratean array of configurations, each merged over config; the results are stacked
target, indexsimulate, evaluate, calibratevariable paths for the columns of the result and its index
baseallthe path of a system (leaf) that variable paths are then relative to
metasimulate, evaluate, calibrateextra columns: a system name adds the configured parameters of that system, which tells the runs of configs apart; an object adds constant columns; an array combines them
stopallan update count (number), a duration ({"value", "unit"} in a unit of the clock time), or the path of a count or condition variable (string)
snapsimulate, visualizewhen a row is recorded: an interval ({"value", "unit"}, not a bare number), a boolean, or the path of a condition variable
seedsimulate, evaluate, calibratean integer for the random number generator
nounit, longsimulatebooleans: a table without units, a table in long format
observedevaluate, calibratea table of observations, with the index columns and the targets
metricevaluate, calibratermse (the default), nrmse, rmspe, mae, mape, ef or dr
parameterscalibratebounds for each parameter to calibrate: [lower, upper] or {"lower", "upper", "unit"}, keyed like config. Bounds may be in any unit of the parameter, and the calibrated value comes back in its declared unit. A path that several systems share through a mixin is calibrated through the path of one of them
weight, paretocalibrateweights of several targets, and whether to return the Pareto frontier of them
optimcalibrateoptions for the optimizer (MaxSteps, Method and so on), except those that write files where the service runs
x, y, kind, plot, xstep, datavisualizesee Plots

A field that an operation does not have is rejected, as is an unknown field of a value object, a table, plot or xstep. A misspelled request does not quietly run with defaults.

Values

A value is a plain JSON value, or an object with a value and optionally its unit, type and timezone:

{"value": 300, "unit": "cm"}
  • value may be an array, or an array of rows for a tabulate or interpolate parameter.
  • unit is a label such as m, K*d, m*s^-1 or °C. Responses write units in ASCII and in a form that requests read back.
  • type names a Julia type. Bool, Int, Float64, String, Symbol, Date, DateTime and ZonedDateTime convert the value on input; any other name is informative.
  • timezone is the IANA zone of a ZonedDateTime. A time without an offset requires it, and a time in a zone with daylight saving time carries it, so that the zone survives the round trip.

Configurations

config takes flat {"System.name": value} entries, nested {"System": {"name": value}} objects, or a mix of them. A parameter is addressed by its name or alias, under its system or a mixin of it. A flat path may name the system with its modules (Main.Plant.Leaf.a) to tell it from a system of the same name in another module; the plain name sets both, and a nested object may be keyed the same way, as a calibration answers. An object under the name of a system holds its parameters, whatever they are called ({"Model": {"value": 1}}), and one under a parameter path is its value. Clients set the parameters that the model declares, the clock and produced systems included; any other path is rejected.

A value is checked against its parameter before the model is built:

  • a parameter of a scalar type takes one value, not an array. A plain number is in its declared unit, and a quantity is converted to that unit; a unit that does not convert, or any unit for a parameter without one, is rejected;
  • a string is read as the declared type of the parameter;
  • nested arrays are the rows of the matrix that a tabulate or interpolate parameter takes;
  • a table input takes a table, and nothing else does.

Variable paths

target and index take a path, an array of paths, or an object that maps output column names to paths. Paths go by name or alias; * and leaf.* select every variable of a system, and time is the clock time. Unknown paths are rejected, except indexed ones such as leaves[1].area, which only resolve while the model runs.

Tables

A table is CSV text or columns with row arrays:

{
  "type": "CSV",
  "content": "index,Tair\n2024-01-01T00:00:00+09:00,4.2\n",
  "columns": [
    {"name": "index", "type": "ZonedDateTime", "timezone": "Asia/Seoul"},
    {"name": "Tair", "type": "Float64", "unit": "°C"}
  ]
}
{"type": "DataFrame", "columns": [{"name": "time", "unit": "hr"}, {"name": "height", "unit": "cm"}], "rows": [[0, 0.0], [1, 0.5]]}
  • columns declare what the values do not tell: type, unit and timezone. A CSV header may carry name (unit) or name (:Type) annotations instead when no columns are given. Annotations are parsed as units and as the type names above, never evaluated, and a column name that still ends in parentheses is rejected.
  • Timestamps may carry their own offset. timezone converts them to the named zone and is required for local timestamps without an offset. A temporal column cannot carry a unit.
  • Column names are unique, and a row has no more cells than there are columns, so no value is dropped. A cell that is absent or null is a missing value.
  • The table of a simulation response is itself a table of the second kind and can be sent back as it is: as data to plot, or as observed.

The tables of the model description list, for each provide input, the index column and the columns that drive variables read, so that a client can build a replacement.

Defaults

A request starts from what serve() was given, and what a client sends wins:

  1. config of serve() is the base configuration.
  2. The config of request_defaults is merged over it once, when the service starts. GET /api/model reports the result as the default of each parameter.
  3. The config of a request is merged over that, and each of configs over the result.

At every step a value replaces the one below it for the same parameter, whichever of the parameter's names (its alias, a mixin of its system) either used, and the parameters that the upper configuration does not set keep their values.

The other request_defaults (stop, snap, target, ...) stand in for the fields that a request leaves out. A default only applies to the operations that have the field: snap is not a field of evaluate and calibrate, which align observations themselves. Defaults are trusted and may hold Julia values that no request can express, such as a snap function.

options of serve(), the constructor options that give a system its extern values, reach every operation that runs the model. A request cannot set them.

Plots

POST /api/visualize draws one table, y against x:

FieldValue
xone variable or column; time by default
yone variable or column, or an array of them
kindscatter (the default), line, scatterline or step
plotoptions: title, xlab, ylab, legend, legendpos, names, colors, xlim and ylim ([lower, upper], either may be null), xunit, yunit, aspect, and linestyle (solid, dash, dot, dashdot or dashdotdot; one, or one for each of y)
dataa table to plot as it is; x and y then name its columns
config, base, stop, snapwithout data: the simulation whose table is plotted
xstepwithout data: a parameter to vary, {"path", "start", "stop", "step", "unit"} or {"path", "values", "unit"}, for one simulation per value

A single y is named on its axis and several in a legend. The series of one plot share an axis, so their units have to convert to each other.

This is the table form of visualize(). Its other forms have no request: a group, several systems or configurations side by side, observations against estimates, a response surface over two parameters, and layers added with visualize!. For those, request the tables and draw them in the client, or call Julia.

Responses

Every response carries status and contract_version.

  • A simulation returns a table: columns (name, type, unit) and rows of plain values, with null for non-finite numbers and missing values, and a summary with nrow and elapsed_ms.
  • A plot returns format (svg), content_type and the SVG text in body.
  • An evaluation returns metric, target and value, a value object with its unit, or an array of them for several targets.
  • A calibration returns config, nested objects of value objects in the declared units of their parameters, keyed by metric values when pareto is set.

Errors

{
  "status": "error",
  "contract_version": "0.2.0",
  "error": {"code": "invalid_request", "category": "validation", "message": "Unknown parameter `Plant.zzz`", "retryable": false, "path": "/api/simulate"}
}
StatuscategorycodeWhen
400syntaxinvalid_requestthe body is not a JSON object
404routingnot_foundthere is no such route
405methodmethod_not_allowedthe route does not take the method
422validationinvalid_requestthe model or its data cannot take the request
500executionexecution_failedthe model failed while it ran

A 422 is the client's to correct: an unknown field, parameter or variable, a value in a unit or shape that its parameter does not take, a stop or snap that is no duration, a table that would lose values, observations that match no output or lack the target, labels or units that a plot cannot take. A 500 reports the cause of a failure inside the model, without a stack trace.

Dashboard metadata

GET /api/dashboard serves what the bundled page is built from, for any other frontend to use:

FieldContent
title, modelthe title of the page and the name and module of the model
controlsone for each parameter and table input, and the runtime Clock.step: path, label, kind (number, checkbox, text or file), unit, default, value_type, min, max and step, among others
outputsthe variables to choose from, the clock time first, as entries of the model description
tablesthe table entries of the model description
simulationthe stop and snap of the request defaults, as a request takes them; five units of the clock time for stop when the service has none, and null for a snap that it has not or that only Julia can express
panelsplots of outputs: title, x, y, kind, and optionally linestyles, xmin, xmax, ymin, ymax and xstep, the path of a sweep
sweepsparameter sweeps for panels: the fields of xstep, with an optional label

ui of serve() and dashboard() arranges it:

KeyEffect
titlethe title
outputsthe variables offered, by target, name or alias; parameters may be among them
panelsthe plot panels; their variables are added to the outputs, so that one simulation can feed every panel
sweepsthe sweeps that panels refer to by xstep
table_inputslabel, description, accept, columns and default_filename for the control of a table input, found by its path

A name that the model does not have, or a key that ui does not have, is an error when the service starts.