-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin-max-generator.py
361 lines (280 loc) · 12.3 KB
/
min-max-generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#https://aegis4048.github.io/transforming-non-normal-distribution-to-normal-distribution
from dhis2 import Api, pretty_json
import json
import pandas as pd
import numpy as np
import math
from scipy.stats import median_abs_deviation
from scipy import stats
from scipy.special import inv_boxcox
from tqdm import tqdm
import warnings
import argparse
## INITIALISATION
warnings.filterwarnings("ignore", category=RuntimeWarning) #scipy spamming with runtime values
parser=argparse.ArgumentParser()
parser.add_argument("--conf", help="Path to configuration file")
parser.add_argument("--auth", help="Path to authentication file")
parser.add_argument("--dryrun", help="Do not post min-max values to server", action='store_true')
parser.add_argument("--file", help="Save results to file. --file ALL for all values, --file OUTLIERS to only include outliers")
def initialise(arguments):
global conf, api, result, endDate, startDate, fileOutput, fileResult, dryRun
# Configuration
if arguments.conf == None:
print("Conf file must be specified with --conf argument")
raise Exception("Parsing config file")
else:
# Load conf file
with open(arguments.conf) as file:
conf = json.load(file)
# API
if arguments.auth == None:
print("Auth file must be specified with --auth argument")
raise Exception("Parsing auth file")
else:
# Initialise API
api = Api.from_auth_file(arguments.auth)
# Dry run
if arguments.dryrun:
dryRun = True
else:
dryRun = False
# File output
if arguments.file != None:
if arguments.file == "ALL":
fileOutput = "ALL"
elif arguments.file == "OUTLIERS":
fileOutput = "OUTLIERS"
else:
print("Unknown --file argument: ", arguments.file)
fileOutput = False
else:
fileOutput = False
fileResult = None
# Method - sort groups in conf file, make sure we categorise correctly after size
conf["groups"] = sorted(conf["groups"], key=lambda d: d['limitMedian'])
# Period - TODO - change to be dependent on period type of dataset, and make sure we don't include current period
endDate = pd.to_datetime('today') + pd.offsets.MonthEnd()
startDate = endDate - pd.DateOffset(years=conf["years"])
# Summary result
result = {
"missing": 0,
"valid": 0,
"outliers": 0,
"errors": 0
}
try:
initialise(parser.parse_args())
except:
print("Fail to initialise script, check parameters.")
exit(1)
## Get the number of periods we are looking at in total (i.e. denominator for looking at completeness)
def getPeriodCount(ds):
dsPeriodtype = api.get("dataSets/" + ds, params={"fields": "id,periodType"})
periodType = dsPeriodtype.json()["periodType"]
#subtracting one, since we don't expect any report in current period
if periodType == 'Monthly':
return (pd.Period(endDate, 'm') - pd.Period(startDate, 'm')).n - 1
elif periodType == 'Quarterly':
return (pd.Period(endDate, 'q') - pd.Period(startDate, 'q')).n - 1
else:
print("Periodtype not supported: " + periodType)
exit
## Get the reported data values
def getDataValues(ds, ou, sDate, eDate):
dvs = api.get("dataValueSets", params={
"dataSet": ds,
"startDate": sDate.strftime("%Y-%m-%d"),
"endDate": eDate.strftime("%Y-%m-%d"),
"orgUnit": ou,
"children": "true"
})
return dvs.json()["dataValues"]
## Get data element value types, so that we can ignore non-numeric ones
def getDataElementType(ds):
dsDataElements = api.get("dataSets/" + ds, params={"fields": "id,dataSetElements[dataElement[id,valueType]]"})
return dsDataElements.json()["dataSetElements"]
## Get the current min/max values that are NOT generated - don't want to overwrite them
def getManualMinMax(des, ous):
mm = api.get("minMaxDataElements", params={
"filter": "dataElement.id:in:[" + ",".join(des) + "]",
"filter": "source.id:in:[" + ",".join(ous) + "]",
"filter": "generated:eq:false"
})
return mm.json()["minMaxDataElements"]
## Analyse orgunit - data element - categoryOptionCombo combination for min/max
def findMinMax(ou, de, coc, values):
values = pd.DataFrame(values)
# if number of periods with data is less than threshold, ignore
if values["value"].count() <= math.ceil(periodCount*conf["completenessThreshold"]):
result["missing"] += 1
#TODO: delete previously generated min/max, since this is unlikely to be accurate?
return False
else:
result["valid"] += 1
#find the right group (by size of median reported number)
median = values["value"].median()
for group in conf["groups"]:
if median < group["limitMedian"]:
break
##Ignore chosen method if variance is 0 or very small
if noVariance(values):
val_max, val_min = mmPrevMax(values, 1.5)
elif group["method"] == "PREV_MAX" or (values["value"].min() == values["value"].max()):
val_max, val_min = mmPrevMax(values, group["threshold"])
elif group["method"] == "ZSCORE":
val_max, val_min = mmZScore(values, group["threshold"])
elif group["method"] == "MAD":
val_max, val_min = mmMAD(values, group["threshold"])
elif group["method"] == "BOXCOX":
val_max, val_min = mmBoxCox(values, group["threshold"])
else:
print("Unknown method")
return False
comment = group["method"]
# Check that method worked - otherwise count as error and fall back to PrexMax with 1.5 threshold
if math.isfinite(val_min) == False or math.isfinite(val_max) == False:
result["errors"] += 1
val_max, val_min = mmPrevMax(values, 1.5)
comment += " - Error"
# Round up/down
val_max = math.ceil(val_max)
val_min = math.floor(val_min)
# Count outliers
isOutlier = False
if max(values["value"]) > val_max or min(values["value"]) < val_min:
result["outliers"] += 1
comment += " - Outlier"
isOutlier = True
minMaxObject = {
"min": int(val_min),
"max": int(val_max),
"generated": True,
"dataElement": {
"id": de
},
"source": {
"id": ou
},
"optionCombo": {
"id": coc
}
}
# Push the values - need to do this one by one due to API limitations
if dryRun == False:
response = api.post("minMaxDataElements", minMaxObject)
if response.status_code != 201:
print("Update failed:")
pretty_json(minMaxObject)
return {"val_max": val_max, "val_min": val_min, "comment": comment, "outlier": isOutlier}
# Check that the series is not constant and/or with very low variance, in which case we fall back to prev min/max
def noVariance(values):
variance = values["value"].var()
median = values["value"].median()
# Check if variance is < 2% of the median
return (100*variance)/median < 2
def mmPrevMax(values, threshold):
val_max = max([max(values["value"]) * threshold, 10]) # never less than 10
val_min = max([max(values["value"]) * (1 - threshold), 0]) # never below 0
return val_max, val_min
def mmZScore(values, threshold):
mean = values["value"].mean()
zscore = np.std(values)
val_max = mean + threshold * zscore
val_min = max([float(mean - zscore * threshold), 0]) # avoid negative
return val_max, val_min
def mmMAD(values, threshold):
mad = values[["value"]].apply(median_abs_deviation)[0]
median = values["value"].median()
val_max = median + 3*mad
val_min = max([float(median - mad * threshold), 0]) # avoid negative
return val_max, val_min
def mmBoxCox(values, threshold):
try:
values_transformed, lmbda = stats.boxcox(values["value"])
except:
return np.nan, np.nan
mean_trans = np.mean(values_transformed)
std_trans = np.std(values_transformed)
upper_limit_trans = mean_trans + threshold * std_trans
lower_limit_trans = mean_trans - threshold * std_trans
val_max = inv_boxcox(upper_limit_trans, lmbda)
val_min = inv_boxcox(lower_limit_trans, lmbda)
# We're not too concerned with min, so set to 0 if it can't be calculated
if math.isfinite(val_min) == False:
val_min = 0
# Do some sanity checks - the methods fails for somer series
if val_max == val_min: #min and max are the same
return np.nan, np.nan
elif values[values["value"] > val_max].size > (values.size/2): #more than half of the values are high outliers
return np.nan, np.nan
elif values[values["value"] < val_min].size > (values.size/2): #more than half of the values are low outliers
return np.nan, np.nan
else:
return val_max, val_min
def filterNumeric(dvs):
dataElements = pd.json_normalize(getDataElementType(conf["dataset"]))
dataElements = dataElements.rename(columns={"dataElement.id": "dataElement", "dataElement.valueType": "valueType"})
dvs = pd.merge(dvs, dataElements, how="inner", on=["dataElement"])
validTypes = ["INTEGER", "INTEGER_POSITIVE", "INTEGER_ZERO_OR_POSITIVE", "NUMBER"]
return dvs[dvs['valueType'].isin(validTypes)]
def filterManualValues(dvs):
manualMinMax = pd.json_normalize(getManualMinMax(dvs["dataElement"].unique().tolist(), dvs["orgUnit"].unique().tolist()))
if manualMinMax.size > 0:
manualMinMax["minMaxEntry"] = manualMinMax["source.id"] + "." + manualMinMax["dataElement.id"] + "." + manualMinMax["optionCombo.id"]
dvs = dvs[~dvs['minMaxEntry'].isin(manualMinMax["minMaxEntry"].tolist())]
return dvs
def generateValues(ou, ds):
# Get the number of potential periods we should expect
global periodCount
periodCount = getPeriodCount(ds)
# Get data values for the orgunit and dataset in question, and save as DataFrame
dataValues = pd.DataFrame(getDataValues(ds, ou, startDate, endDate))
# Add column identifying the "unique" combinations of orgunit, data element and CoC. Each of these should get a min/max
dataValues["minMaxEntry"] = dataValues["orgUnit"] + "." + dataValues["dataElement"] + "." + dataValues["categoryOptionCombo"]
# Exclude non-numeric data elements
dataValues = filterNumeric(dataValues)
# Exclude min-max that have been manually set
dataValues = filterManualValues(dataValues)
# Change value type to numeric
dataValues['value'] = dataValues['value'].astype(float)
## For testing, build "pivoted" spreadsheet where min, max etc are included (since there is no min/max analysis anymore)
dvPivoted = dataValues.pivot_table(values=["value"], index=["minMaxEntry"], columns=["period"], aggfunc=np.sum)
# Iterate over unique
for entry in tqdm(pd.unique(dataValues["minMaxEntry"])):
values = dataValues.loc[dataValues['minMaxEntry'] == entry, 'value']
statsResult = findMinMax(entry.split(".")[0], entry.split(".")[1], entry.split(".")[2], values)
if statsResult:
dvPivoted.loc[entry, "min"] = statsResult["val_min"]
dvPivoted.loc[entry, "max"] = statsResult["val_max"]
dvPivoted.loc[entry, "comment"] = statsResult["comment"]
dvPivoted.loc[entry, "outlier"] = statsResult["outlier"]
print(ou, result)
global fileResult
if isinstance(fileResult, pd.DataFrame):
pd.concat([fileResult, dvPivoted])
else:
fileResult = dvPivoted
#Check if orgunit is level 1, in which case get children and do in batches
def processOrgunits(ous):
orgunits = api.get("organisationUnits", params={
"fields": "id,level,children[id]",
"paging": "false",
"filter": "id:in:[" + ",".join(ous) + "]"
})
orgunits = orgunits.json()["organisationUnits"]
filtered_orgunits = []
for orgunit in orgunits:
if orgunit["level"] == 1:
for child in orgunit["children"]:
filtered_orgunits.append(child["id"])
else:
filtered_orgunits.append(orgunit["id"])
return filtered_orgunits
for orgunit in processOrgunits(conf["orgunits"]):
generateValues(orgunit, conf["dataset"])
if fileOutput:
if fileOutput == "OUTLIERS":
fileResult = fileResult[fileResult['outlier'] == True]
fileName = "minMaxTest-" + conf["dataset"] + ".csv"
fileResult.to_csv(fileName, sep='\t', encoding='utf-8')