Oms API Read Run Output table calculated value Ids - openmpp/openmpp.github.io GitHub Wiki

Read a "page" of output table calculated values from model run.

  • Calculate one or more values from output table expressions or accumulators.

  • Page is part of output table values defined by zero-based "start" row number and row count. If row count <= 0 then all rows below start row number returned.

  • Dimension(s) returned as enum id, not enum codes.

  • Calculations are done either on output table expressions or aggregated accumulators (see example below).

  • Method verb must be POST and Content-Type header "application/json".

JSON body POSTed to specify output table name, page size, row count, filters and row order. It is expected to be JSON representation of db.ReadCalculteTableLayout structure from Go library. See also: db.ReadLayout structure from Go library.

// ReadCalculteTableLayout describe table read layout and additional measures to calculte.
type ReadCalculteTableLayout struct {
	ReadLayout                         // output table name, run id, page size, where filters and order by
	Calculation []CalculateTableLayout // additional measures to calculate
}

// CalculateLayout describes calculation to output table values.
type CalculateTableLayout struct {
	CalculateLayout      // expression to calculate and layout
	IsAggr          bool // if true then select output table accumulator else expression
}

// CalculateLayout describes calculation to parameters or output table values.
type CalculateLayout struct {
	Calculate string // expression to calculate, ex.: Expr0[base] - Expr0[variant]
	CalcId    int    // calculated expression id, calc_id column in csv,     ex.: 0, 12000, 24000
	Name      string // calculated expression name, calc_name column in csv, ex.: Expr0, AVG_Expr0, RATIO_Expro0
}

// ReadLayout describes source and size of data page to read input parameter, output table values or microdata.
//
// Row filters combined by AND and allow to select dimension or attribute items,
// it can be enum codes or enum id's, ex.: dim0 = 'CA' AND dim1 IN (2010, 2011, 2012)
type ReadLayout struct {
	Name           string           // parameter name, output table name or entity microdata name
	FromId         int              // run id or set id to select input parameter, output table values or microdata from
	ReadPageLayout                  // read page first row offset, size and last page flag
	Filter         []FilterColumn   // dimension or attribute filters, final WHERE does join all filters by AND
	FilterById     []FilterIdColumn // dimension or attribute filters by enum ids, final WHERE does join filters by AND
	OrderBy        []OrderByColumn  // order by columnns, if empty then dimension id ascending order is used
}

Method:

POST /api/model/:model/run/:run/table/calc-id

For example:

curl -v -X POST -H "Content-Type: application/json" http://localhost:4040/api/model/modelOne/run/Default/table/calc-id -d @test.json
curl -v -X POST -H "Content-Type: application/json" http://localhost:4040/api/model/modelOne/run/2019_01_17_19_59_52_998/table/calc-id -d @test.json

Arguments:

:model - (required) model digest or model name

Model can be identified by digest or by model name. It is recommended to use digest because it is uniquely identifies model. It is possible to use model name, which is more human readable than digest, but if there are multiple models with same name in database than result is undefined.

:run - (required) model run digest, run stamp or run name

Model run can be identified by run digest, run stamp or run name. It is recommended to use digest because it is uniquely identifies model run. Run stamp, if not explicitly specified as model run option, automatically generated as timestamp string, ex.: 2016_08_17_21_07_55_123. It is also possible to use name, which is more human readable than digest, but if there are multiple runs with same name in database than result is undefined.

JSON body arguments:

Example 1. Calculate two values using modelOne output table salarySex:

  • ratio of expressions: expr1 / expr2
  • standard error of acc1 accumulator sub-values
{
  "Name":      "salarySex",
  "Calculation": [
    {
      "Calculate": "expr1 / expr2",
      "CalcId":    201,
      "Name":      "Expr1_div_expr2",
      "IsAggr": false
    },
    {
      "Calculate": "OM_SE(acc1)",
      "CalcId":    301,
      "Name":      "Se_of_acc1",
      "IsAggr": true
    }
  ]
}

Calcultion can be done over output table expressions if IsAggr: false or over accumulators if IsAggr: true. You cannot mix expressions and accumultors in the same calculation, it is mutually exclusive. Following aggregation functions avaliable for accumulators:

  • OM_AVG mean of accumulators sub-values
  • OM_SUM sum of accumulators sub-values
  • OM_COUNT count of accumulators sub-values (excluding NULL's)
  • OM_COUNT_IF count values matching condition
  • OM_MAX maximum of accumulators sub-values
  • OM_MIN minimum of accumulators sub-values
  • OM_VAR variance of accumulators sub-values
  • OM_SD standard deviation of accumulators sub-values
  • OM_SE standard error of accumulators sub-values
  • OM_CV coefficient of variation of accumulators sub-values

It is also possible to use parameter(s) in calculation, parameter must be a scalar of float or integer type. For example: OM_COUNT_IF(acc1 > param.High), where param.High is a value of scalar parameter High in that model run.

For more details please see: Model Output Expressions

Example 2:

  • ratio of expressions: expr1 / expr2, adjusted by using parameter StartingSeed values
  • standard error of acc1 accumulator sub-values, adjusted by using parameter StartingSeed values
  • read only first 100 rows: Offset: 0, Size: 100
  • apply WHERE filters and ORDER BY
{
  "Name": "salarySex",
  "Calculation": [
    {
      "Calculate": "expr1 / expr2 + param.StartingSeed / 100",
      "CalcId":    201,
      "Name":      "Expr1_div_expr2_adjusted",
      "IsAggr": false
    },
    {
      "Calculate": "OM_SE(acc1 - param.StartingSeed) + param.StartingSeed",
      "CalcId":    301,
      "Name":      "Se_of_acc1_adjusted",
      "IsAggr": true
    }
  ],
  "Offset": 0,
  "Size": 100,
  "IsFullPage": true,
  "IsSubId": true,
  "SubId": 2,
  "FilterById": [{
      "Name": "AgeGroup",
      "Op": "IN",
      "EnumIds": [100, 300]
    }, {
      "Name": "Province",
      "Op": "BETWEEN",
      "EnumIds": [1, 800]
    }
  ],
  "OrderBy": [{
      "IndexOne": 2,
      "IsDesc": true
    }, {
      "IndexOne": 3,
      "IsDesc": true
    }
  ]
}
Name       - (required) output table name
Offset     - (optional) zero-based start row to select output table values
Size       - (optional) max row count to select output table values, if size <= 0 then all rows selected
IsFullPage - (optional) if true then always return non-empty last page of data
IsSubId    - (optional) if true then select only single sub-value, default: all sub-values
SubId      - (optional) sub-value id to select if IsSubId is true
FilterById - (optional) conditions to filter dimension enum id's
OrderBy    - (optional) list of columns indexes (one based) to order by

Filter conditions joined by AND and can have following operations:

=       - enum id equal to:         AgeGroup = 20
!=      - enum id not equal to:     AgeGroup <> 20
>       - enum id greater than:     AgeGroup > 20
>=      - enum id greater or equal: AgeGroup >= 20
<       - enum id less than:        AgeGroup < 20
<=      - enum id less or equal:    AgeGroup <= 20
IN      - in the list of id's:      AgeGroup IN (20, 30, 40)
BETWEEN - between min and max:      AgeGroup BETWEEN 20 AND 40
IN_AUTO - automatically choose most suitable: = or != or IN or BETWEEN

Order by specified by one-based column(s) index(es) in result. Columns always contain enum id's, not enum codes and therefore result ordered by id's First two columns are run_id, calc_id:

  SELECT run_id, CalcId AS calc_id, dim0, dim1, ..., calc_value FROM .... ORDER BY 1, 2,...

Example 1:

JSON body:

{
  "Name":      "salarySex",
  "Calculation": [
    {
      "Calculate": "expr1 / expr2",
      "CalcId":    201,
      "Name":      "Expr1_div_expr2",
      "IsAggr": false
    },
    {
      "Calculate": "OM_AVG(acc1)",
      "CalcId":    301,
      "Name":      "Se_of_acc1",
      "IsAggr": true
    }
  ]
}

Result:

> POST /api/model/modelOne/run/Default/table/calc-id HTTP/1.1
> Host: localhost:4040
> User-Agent: curl/8.0.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 296
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Thu, 26 Oct 2023 02:52:01 GMT
< Content-Length: 1489
<
{
    "Page": [{
            "DimIds": [100, 0],
            "IsNull": false,
            "Value": 0,
            "CalcId": 201,
            "RunId": 201
        }, {
            "DimIds": [100, 1],
            "IsNull": false,
            "Value": 0.01639344262295082,
            "CalcId": 201,
            "RunId": 201
        }, {
            "DimIds": [100, 800],
            "IsNull": false,
            "Value": 0.9987515605493134,
            "CalcId": 201,
            "RunId": 201
        }, {
            "DimIds": [200, 0],
            "IsNull": false,
            "Value": 0.019011406844106467,
            "CalcId": 201,
            "RunId": 201
        }, {
            "DimIds": [200, 1],
            "IsNull": false,
            "Value": 0.03125,
            "CalcId": 201,
            "RunId": 201
        }, {
            "DimIds": [200, 800],
            "IsNull": false,
            "Value": 0.9975093399750934,
            "CalcId": 201,
            "RunId": 201
        }, {
            "DimIds": [300, 0],
            "IsNull": false,
            "Value": 0.036231884057971016,
            "CalcId": 201,
            "RunId": 201
        }, {
            "DimIds": [300, 1],
            "IsNull": false,
            "Value": 0.04477611940298507,
            "CalcId": 201,
            "RunId": 201
        }, {
            "DimIds": [300, 800],
            "IsNull": false,
            "Value": 0.9962732919254659,
            "CalcId": 201,
            "RunId": 201
        }, {
            "DimIds": [100, 0],
            "IsNull": false,
            "Value": 0,
            "CalcId": 301,
            "RunId": 201
        }, {
            "DimIds": [100, 1],
            "IsNull": false,
            "Value": 1,
            "CalcId": 301,
            "RunId": 201
        }, {
            "DimIds": [100, 800],
            "IsNull": false,
            "Value": 800,
            "CalcId": 301,
            "RunId": 201
        }, {
            "DimIds": [200, 0],
            "IsNull": false,
            "Value": 1,
            "CalcId": 301,
            "RunId": 201
        }, {
            "DimIds": [200, 1],
            "IsNull": false,
            "Value": 2,
            "CalcId": 301,
            "RunId": 201
        }, {
            "DimIds": [200, 800],
            "IsNull": false,
            "Value": 801,
            "CalcId": 301,
            "RunId": 201
        }, {
            "DimIds": [300, 0],
            "IsNull": false,
            "Value": 2,
            "CalcId": 301,
            "RunId": 201
        }, {
            "DimIds": [300, 1],
            "IsNull": false,
            "Value": 3,
            "CalcId": 301,
            "RunId": 201
        }, {
            "DimIds": [300, 800],
            "IsNull": false,
            "Value": 802,
            "CalcId": 301,
            "RunId": 201
        }
    ],
    "Layout": {
        "Offset": 0,
        "Size": 18,
        "IsLastPage": true,
        "IsFullPage": false
    }
}
⚠️ **GitHub.com Fallback** ⚠️