Validation + predictors¶
ValidationAccessor on Dhis2Client.validation + PredictorsAccessor on Dhis2Client.predictors. Covers the DHIS2 validation-rule + predictor workflow endpoints. CRUD on the rules / predictors themselves stays on the generated client.resources.validation_rules / client.resources.predictors accessors.
validation
¶
DHIS2 validation-rule workflow — Dhis2Client.validation.
Companion to client.maintenance (data-integrity). Validation rules are
boolean formulas over data elements (e.g. ANC 1st visit >= ANC 4th visit
logically doesn't hold). Running the rules against captured data produces
violations — cells where the rule evaluates to false.
Three endpoint families covered:
- Ad-hoc analysis —
POST /api/dataAnalysis/validationRules. Runs the analysis synchronously + returns violations.persist=Truewrites the violations into DHIS2's/api/validationResultstable;notification=Truesends out the configured notification templates. - Stored results —
GET|DELETE /api/validationResults. Browse / purge results previously persisted. - Expression validation —
/api/expressions/description, plus per-context variants (/api/validationRules/expression/description,/api/indicators/expression/description,/api/predictors/expression/description,/api/programIndicators/expression/description). Ad-hoc check that an expression parses + references existing UIDs.
CRUD for validationRules / predictors themselves stays on the generated
client.resources accessors (client.resources.validation_rules.save_bulk(...)
etc.). This module is only for the workflow endpoints.
Classes¶
ValidationAnalysisResult
¶
Bases: BaseModel
One hit from POST /api/dataAnalysis/validationRules.
Distinct from the persisted ValidationResult (stored at
/api/validationResults): the ad-hoc analysis endpoint returns a flat
shape — IDs + display names inlined — while the persisted shape nests
full BaseIdentifiableObject refs. Both are useful, so we keep both
models; this one is what run_analysis() returns.
Source code in packages/dhis2w-client/src/dhis2w_client/validation.py
ExpressionDescription
¶
Bases: BaseModel
Result of /api/expressions/description — parse status + rendered description.
DHIS2 returns a WebMessage-ish envelope with message="Valid" on parse
success and description="…" rendering the expression with UIDs replaced
by names. Errors come back with status="ERROR" and a message naming
the problem (e.g. "Data element not found: abcUid").
Source code in packages/dhis2w-client/src/dhis2w_client/validation.py
ValidationAccessor
¶
Dhis2Client.validation — run validation rules + inspect stored results.
Read the docstrings on individual methods for the DHIS2-endpoint-level semantics (persist / notification flags, filter parameters, etc.).
Source code in packages/dhis2w-client/src/dhis2w_client/validation.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
Functions¶
__init__(client)
¶
run_analysis(*, org_unit, start_date, end_date, validation_rule_group=None, max_results=None, notification=False, persist=False)
async
¶
Run POST /api/dataAnalysis/validationRules synchronously; return violations.
Synchronous — DHIS2 returns the violations in the response body (no
task polling). For a whole-instance sweep pass the root org unit UID
(DHIS2 walks the sub-tree). Narrow with validation_rule_group when
you only want to evaluate one rule bundle.
persist=True writes each violation into DHIS2's
/api/validationResults table so later list_results() calls can
walk them; notification=True fires the configured notification
templates for each triggered rule. Both default off for
ad-hoc / exploratory runs.
Source code in packages/dhis2w-client/src/dhis2w_client/validation.py
list_results(*, org_unit=None, period=None, validation_rule=None, created_date=None, page=None, page_size=None, fields=None)
async
¶
List persisted validation results (GET /api/validationResults).
DHIS2 accepts ou, pe, vr, createdDate as filters (repeatable
for ou, pe, vr). Defaults to returning every result — narrow
with at least one filter on real instances where the table can run
to millions of rows.
fields defaults to a selector that pulls displayName + the
owning rule's importance + operator inline, so the resulting
ValidationResults carry readable data without a second lookup.
Override with a narrower selector for large-scale runs where only
counts / UIDs matter.
Source code in packages/dhis2w-client/src/dhis2w_client/validation.py
get_result(result_id, *, fields=None)
async
¶
Fetch a single persisted validation result by its numeric id.
fields defaults to the same display-friendly selector
list_results uses — override with a narrower selector if you
only need a subset.
Source code in packages/dhis2w-client/src/dhis2w_client/validation.py
delete_results(*, org_units=None, periods=None, validation_rules=None)
async
¶
Bulk-delete persisted validation results matching the filters.
DELETE /api/validationResults accepts the same filter keys as the
list endpoint. At least one of org_units / periods /
validation_rules must be non-empty — a filter-less delete would
wipe every row.
Source code in packages/dhis2w-client/src/dhis2w_client/validation.py
send_notifications()
async
¶
Fire the configured notification templates for every current violation.
Posts to /api/validation/sendNotifications — DHIS2 walks the
validationResults table + queues messages per template.
Source code in packages/dhis2w-client/src/dhis2w_client/validation.py
describe_expression(expression, *, context='generic')
async
¶
Parse-check an expression + render a human description.
context picks which DHIS2 parser runs — generic / validation-rule
/ indicator / predictor / program-indicator use different
allowed-reference sets. Returns ExpressionDescription.valid (bool)
+ message (the parse error, on failure).
Source code in packages/dhis2w-client/src/dhis2w_client/validation.py
predictors
¶
Predictor authoring + run — Dhis2Client.predictors.
Predictors generate data values from expressions over historical data (e.g. "3-month rolling average of X" → emit a synthetic DataElement row). The accessor covers both authoring (create / update / delete) and the run endpoints DHIS2 exposes:
POST /api/predictors/run?startDate=…&endDate=…— run every predictor on the instance.POST /api/predictors/{uid}/run?startDate=…&endDate=…— run one.POST /api/predictorGroups/{uid}/run?startDate=…&endDate=…— run a named group of predictors in one pass (exposed fromPredictorsAccessor.run_groupfor backward compatibility + also fromPredictorGroupsAccessor.run).
All three run shapes return a WebMessageResponse with a summary of
predictions written / ignored / failed; none of them kick a background
job, so there's no task to watch.
Authoring surface: Predictor.generator is an Expression sub-object
typed as Any on the generated schema. create(...) assembles the
minimal wrapper here so callers pass the expression string +
description, not the nested payload.
No *Spec builder — continues the spec-audit data point.
Classes¶
Predictor
¶
Bases: BaseModel
Generated model for DHIS2 Predictor.
DHIS2 Predictor - persisted metadata (generated from /api/schemas at DHIS2 v42).
API endpoint: /api/predictors.
Field Field(description=...) entries flag DHIS2 semantics the bare
type can't capture: which side of a relationship owns the link
(writable) vs the inverse side (ignored by the API), uniqueness
constraints, and length bounds.
Source code in packages/dhis2w-client/src/dhis2w_client/generated/v42/schemas/predictor.py
PredictorsAccessor
¶
Dhis2Client.predictors — CRUD + run helpers over /api/predictors.
Source code in packages/dhis2w-client/src/dhis2w_client/predictors.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
Functions¶
__init__(client)
¶
list_all(*, period_type=None, page=1, page_size=50)
async
¶
Page through Predictors, optionally filtered by periodType.
Source code in packages/dhis2w-client/src/dhis2w_client/predictors.py
get(uid)
async
¶
Fetch one Predictor with generator, output, OU scope resolved inline.
Source code in packages/dhis2w-client/src/dhis2w_client/predictors.py
create(*, name, short_name, expression, output_data_element_uid, period_type=PeriodType.MONTHLY, sequential_sample_count=3, annual_sample_count=0, sequential_skip_count=0, organisation_unit_descendants=OrganisationUnitDescendants.SELECTED, organisation_unit_level_uids=None, output_combo_uid=None, missing_value_strategy=MissingValueStrategy.SKIP_IF_ALL_VALUES_MISSING, generator_description=None, description=None, code=None, uid=None)
async
¶
Create a Predictor.
expression uses DHIS2's aggregate expression syntax; the
accessor wraps it in the generator Expression sub-object.
output_data_element_uid is the target DE the prediction writes
to — needs a TRACKER or AGGREGATE domain and a numeric
valueType.
sequential_sample_count + annual_sample_count control the
look-back window: 3 monthly samples with the default period
type averages the three prior months.
organisation_unit_level_uids scopes the run — pass the UIDs of
the OrganisationUnitLevel rows the predictor should cover
(typically the facility level for data-entry predictors).
Source code in packages/dhis2w-client/src/dhis2w_client/predictors.py
update(predictor)
async
¶
PUT an edited Predictor back. predictor.id must be set.
Source code in packages/dhis2w-client/src/dhis2w_client/predictors.py
rename(uid, *, name=None, short_name=None, description=None)
async
¶
Partial-update shortcut — read, mutate the label fields, PUT.
Source code in packages/dhis2w-client/src/dhis2w_client/predictors.py
delete(uid)
async
¶
Delete a Predictor. DHIS2 keeps any data values it already wrote.
Source code in packages/dhis2w-client/src/dhis2w_client/predictors.py
run_all(*, start_date, end_date)
async
¶
Run every predictor on the instance for the given date range.
Returns the summary envelope — .import_count() gives
imported / updated / ignored / deleted counts for the emitted
data values.
Source code in packages/dhis2w-client/src/dhis2w_client/predictors.py
run_one(predictor_uid, *, start_date, end_date)
async
¶
Run a single predictor by UID over the given date range.
Source code in packages/dhis2w-client/src/dhis2w_client/predictors.py
run_group(group_uid, *, start_date, end_date)
async
¶
Run every predictor in a PredictorGroup over the given date range.