forked from HBPMedical/algorithm-repository
-
Notifications
You must be signed in to change notification settings - Fork 1
/
algo.py
120 lines (104 loc) · 2.9 KB
/
algo.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
#!/usr/bin/env python3
import logging
import sys
import pandas
import database_connector # Library coming from the parent Docker image and used to manage input/output data
def main():
logging.basicConfig(level=logging.INFO)
# Get variables names
var = database_connector.get_var() # Dependent variable
gvars = database_connector.get_gvars() # Independent nominal variables
cvars = database_connector.get_covars() # Independent continuous variables
covs = [x for x in (gvars + cvars) if len(x) > 0] # All independent variables
# Check dependent variable type
if database_connector.var_type(var)['type'] not in ["integer", "real"]:
sys.exit("Dependent variable should be continuous !")
# Get data
data = database_connector.fetch_data()
# Compute results
results = compute_example(var, data)
pfa = generate_pfa(database_connector.get_code(), database_connector.get_name(),
database_connector.get_docker_image(), database_connector.get_model(), var, covs, results)
error = '' # You should store any error message in this variable
shape = 'pfa_json'
logging.info("PFA: %s", pfa)
# Store results
database_connector.save_results(pfa, error, shape)
# Compute example
def compute_example(var, data):
data = pandas.DataFrame(data['data'], columns=data['columns'])
return data[[var]].mean().to_json()
def generate_pfa(algo_code, algo_name, docker_image, model, variable, grps, results):
str_grps = '","'.join(grps)
output = ('''
{
"code": "%s",
"name": "%s",
"cells": {
"validations": [],
"data": {
"init": %s,
"type": {
"name": "example",
"doc": "example computation",
"namespace": "%s",
"type": "record",
"fields": [
{
"type": "map",
"values": "double",
"doc": "mean",
"name": "mean"
}
]
}
},
"query": {
"init": {
"grouping": ["%s"],
"variable": "%s"
},
"type": {
"type": "record",
"doc": "Definition of the inputs",
"fields": [
{
"type": {
"type": "string"
},
"doc": "Main example variable",
"name": "variable"
},
{
"type": {
"items": {
"type": "string"
},
"type": "array"
},
"doc": "Categories used for specific example",
"name": "groups"
}
],
"name": "Query"
}
}
},
"doc": "example computation",
"metadata": {
"docker_image": "%s"
},
"output": {
"type": "null"
},
"action": [
null
],
"input": {
"type": "null"
}
}
''' % (algo_code, algo_name, results, model, str_grps, variable, docker_image))
return output
if __name__ == '__main__':
main()