Specifying period when running Predictors via API

Hi All,

According to this documentation Metadata - DHIS2 Documentation we can run all or single Predictors. However, how do we specify the period (month, day, year) that we want to run the Predictors for, when sending a POST request. @Gassim @micnice @dhis2-analytics

Thanks

Hi @antony.sibanda

I think in that case it will run for all the periods related to the predictor; however, based on your question it looks like you’re looking into something similar to this (Scheduling - DHIS2 Documentation)

If so, you might want to use the schedular instead… (Scheduling - DHIS2 Documentation)

Hi @Gassim thank you very much for your prompt response. Let me try it out and get back to you.

Thanks

@Gassim I managed to get what I wanted by doing a POST request to the following endpoint and supplying the startDate and endDate as follows : https://xxxxxxx/api/predictors/run?startDate=2024-03-01&endDate=2024-03-01

Once again, thanks alot

1 Like

@Gassim we have been trying to predict date value types. Is this possible? use case: using predictors to carry forward the expiry date of a commodity - the expiry date is constant - does not change

@antony.sibanda It might be possible if you create the predictor using metadata import or directly through the WebAPI.

Here’s why: When you enter a predictor expression through the GUI, it validates the expression with the backend. For the purpose of validation, the backend assumes that the predictor expression must return a numeric value. So if the predictor output data element has a date value type, and your predictor expression returns a date, it will still be considered invalid by the backend because it requires the predictor expression to return a numeric value and it will see that your expression returns a date value.

However, when predictors run, the predictor expression is checked to see that it returns the same datatype as the output data element. If your output data element is of type date and the expression generates a date, in theory it will work. (It has not been tested.)

So if you can create a predictor that outputs to a date data element and contains an expression that returns a date, it might work – as long as you create the predictor without using the Maintenance app GUI.

One way to do this is:

  • Create a predictor that outputs to your desired date data element but just references a numeric data element within the expression. This should pass the validation test (but it won’t actually work if you try to run it)

  • Use metadata export to export this predictor

  • Edit the exported predictor to replace the numeric input data element with your desired date data element

  • Re-import the predictor using metadata import.

This may work. Again, it hasn’t been tested, but I think your chances are good that it will work.

Meanwhile, I think your use case is valid, and I suggest that you write a Jira feature ticket to ask that predictors can support output to non-numeric data types. (Be sure to include the use case!)

Hi @Jim_Grace Thank you very much for your response. I have tried out your suggestions, unfortunately its not working. I am thinking maybe the its because of the aggregate function that I am using max() on the generator expression.

Below the payload the I have imported:

{

“predictors”: [{

“name”: “Assign ASLXXX200mg_expiry date”,

“created”: “2024-02-29T16:15:17.115”,

“lastUpdated”: “2024-03-05T16:38:09.786”,

“translations”: ,

“externalAccess”: false,

“userGroupAccesses”: ,

“userAccesses”: ,

“access”: {

“manage”: true,

“externalize”: true,

“write”: true,

“read”: true,

“update”: true,

“delete”: true

},

“favorites”: ,

“lastUpdatedBy”: {

“id”: “B9NqFzULOIv”,

“code”: null,

},

“sharing”: {

“external”: false,

“users”: {},

“userGroups”: {}

},

“shortName”: “Predict ASLXXX200mg_expiry date”,

“output”: {

“id”: “ko8IE1WxZyg”

},

“outputCombo”: {

“id”: “HllvX50cXC0”

},

“generator”: {

“translations”: ,

“expression”: “max(#{ko8IE1WxZyg})”,

“description”: “expiry date”,

“slidingWindow”: false,

“missingValueStrategy”: “NEVER_SKIP”,

“displayDescription”: “expiry date”

},

“periodType”: “Daily”,

“organisationUnitLevels”: [

{

“id”: “zQb6PQONp9m”

}

],

“organisationUnitDescendants”: “SELECTED”,

“sequentialSampleCount”: 1,

“annualSampleCount”: 0,

“displayShortName”: “Assign ASLXXX200mg_expiry date”,

“displayFormName”: “Assign ASLXXX200mg_expiry date”,

“displayName”: “Assign ASLXXX200mg_expiry date”,

“favorite”: false,

“id”: “Zu5ZOYDOVRS”,

“attributeValues”: ,

“predictorGroups”: [

{

“id”: “OLX3faVLsm2”

}

]

}]

}

As a work around, I changed the data value type of the expiry_date data element to Integer so that I can collect expiry date as an integer. eg 20240310 would represents 10 March 2024

then used predictors

@antony.sibanda I’m glad you found a workaround. I agree that the problem is using the max() function. Currently it works only for numeric values, but there is no reason why we should not extend this to work with other data types as well (for example: text, boolean, date)

If this is important to your future operations, please submit one or both of the following Jira tickets with a use case:

  1. Predictors should produce the data type of the output data element, not just numeric values.
  2. Predictor functions max() and min() should work on any data type, not just numeric.

Thanks.

Cheers,
Jim