-
Notifications
You must be signed in to change notification settings - Fork 8
/
create_model.py
286 lines (252 loc) · 14.1 KB
/
create_model.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
import sys
import model
import numpy
import datetime
from model import * #for the helpers
def create_test(model, testNo=1):
if testNo == 1:
model.create_node("root", name="variables", type="folder")
for var in ["f0", "f1", "f2", "f3", "count", "time", "back"]:
model.create_node("root.variables", name=var, type="column")
model.create_node_from_path('root.folder2.myconst', {"type": "const", "value": "21data"})
model.create_node_from_path('root.folder2.myfkt', {"type": "function"})
# for the visu
model.create_node_from_path('root.visualization.pipelines.demo1.url',
{"type": "const", "value": "http://localhost:6001/demo1.py"})
model.create_node_from_path('root.visualization.pipelines.demo2.url',
{"type": "const", "value": "http://localhost:6002/demo2.py"})
# create an official table
template = [
{
"name": "description",
"type": "const",
"value": "this is a great table"
},
{
"name": "columns",
"type": "referencer",
},
{
"name": "timeField",
"type": "referencer",
},
{
"name": "numberOfRows",
"type": "variable",
"value": 0
}
]
model.create_node("root", name="mytable", type="table")
model.create_nodes_from_template("root.mytable", template=template)
for var in ["f0", "f1", "f2", "f3", "time", "back"]:
model.add_forward_refs("root.mytable.columns", ["root.variables." + var])
model.add_forward_refs("root.mytable.timeField", ["root.variables.time"])
# add data
startTime = datetime.datetime(2018, 1, 1, 0, 0, 0, tzinfo=pytz.UTC)
vars = {"f0": 0.01, "f1": 0.02, "f2": 0.04, "f3": 0.1, "back": 0.01}
SIZE = 10 * 60 # in seconds units
STEP = 0.1
# !!! we are producing size/step time points
""" for i in range(SIZE):
dataDict = {}
for var in vars:
value = numpy.cos(2*numpy.pi*vars[var]*i/SIZE*3)
dataDict["root.variables."+var]=value
mytime = startTime + datetime.timedelta(seconds = i)
dataDict["root.variables.time"] = mytime
#print(mytime)
model.add_timeseries(dataDict)
"""
startEpoch = date2secs(startTime)
times = numpy.arange(startEpoch, startEpoch + SIZE, STEP, dtype=numpy.float64)
print("we have time:", times.shape)
for var in vars:
values = numpy.cos(2 * numpy.pi * vars[var] * times)
id = model.get_id("root.variables." + str(var))
if var == "back":
# we make -1,0,1 out of it
values = numpy.round(values)
model.model[id]["value"] = values.tolist()
id = model.get_id("root.variables.time")
model.model[id]["value"] = (1000 * times).tolist()
# now correct the background
# now make some widget stuff
model.create_node_from_path('root.visualization.widgets.timeseriesOne', {"type": "widget"})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.selectableVariables',
{"type": "referencer"})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.selectedVariables',
{"type": "referencer"})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.startTime',
{"type": "variable", "value": None})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.endTime',
{"type": "variable", "value": None})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.bins',
{"type": "const", "value": 300})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.hasAnnotation',
{"type": "const", "value": True})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.hasSelection',
{"type": "const", "value": False})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.hasAnnotation.annotations',
{"type": "referencer"})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.hasAnnotation.newAnnotations',
{"type": "folder"})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.hasAnnotation.tags',
{"type": "const", "value": ["one", "two"]})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.hasAnnotation.colors',
{"type": "const", "value": ["yellow", "brown", "grey", "green", "red"]})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.table',
{"type": "referencer"})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.lineColors',
{"type": "const", "value": ["blue", "yellow", "brown", "grey", "red"]})
model.add_forward_refs('root.visualization.widgets.timeseriesOne.selectedVariables',
['root.variables.f0', 'root.variables.f1', 'root.variables.f3'])
model.add_forward_refs('root.visualization.widgets.timeseriesOne.selectableVariables', ['root.variables'])
model.add_forward_refs('root.visualization.widgets.timeseriesOne.table', ['root.mytable'])
model.create_node_from_path('root.visualization.widgets.timeseriesOne.observer', {"type": "referencer"})
model.create_node_from_path('root.visualization.widgets.timeseriesOne.observerUpdate',
{"type": "const", "value": ["line", "background", "annotations"]})
# now the annotations
anno = [
{
"name": "tags",
"type": "const",
"value": ["one", "two"]
},
{
"name": "startTime",
"type": "const",
"value": None
},
{
"name": "endTime",
"type": "const",
"value": None
},
{
"name": "text",
"type": "const",
"value": "this is a great annotation"
}
]
tags = ["one", "two", "one", "one", "two", "two", "one", "one", "one", "two", "one", "one"]
model.create_node_from_path("root.annotations", {"type": "folder"})
startTime = datetime.datetime(2018, 1, 1, 0, 0, 0, tzinfo=pytz.UTC)
for i in range(10):
newAnno = copy.deepcopy(anno)
newAnno[1]["value"] = (startTime + datetime.timedelta(minutes=(i * 10))).isoformat()
newAnno[2]["value"] = (startTime + datetime.timedelta(minutes=(i * 10 + 1))).isoformat()
newAnno[0]["value"] = [tags[i], tags[i + 1]]
newAnnoPath = "root.annotations.anno" + str(i)
model.create_node_from_path(newAnnoPath, {"type": "annotation"})
model.create_nodes_from_template(newAnnoPath, newAnno)
# also add the annotations to the widget
model.add_forward_refs("root.visualization.widgets.timeseriesOne.hasAnnotation.annotations", ["root.annotations",
"root.visualization.widgets.timeseriesOne.hasAnnotation.newAnnotations"])
# make a real function
model.create_node_from_path("root.functions", {"type": "folder"})
model.create_nodes_from_template("root.functions", model.templates["testfunction.delayFunctionTemplate"])
# now make cutom function to trigger something
model.create_nodes_from_template("root.functions", model.templates["counterfunction.counterFunctionTemplate"])
# now hook the function output to the observer of the plot
model.add_forward_refs('root.visualization.widgets.timeseriesOne.observer',
['root.functions.counterFunction.output'])
# now make custom buttons
buttons = [
{
"name": "button1",
"type": "folder",
"children": [
{"name": "caption", "type": "const", "value": "start learner"},
{"name": "counter", "type": "variable", "value": 0},
{"name": "onClick", "type": "referencer"}
]
}
]
model.create_node_from_path("root.visualization.widgets.timeseriesOne.buttons", {"type": "folder"})
model.create_nodes_from_template("root.visualization.widgets.timeseriesOne.buttons", buttons)
model.add_forward_refs("root.visualization.widgets.timeseriesOne.buttons.button1.onClick",
["root.functions.counterFunction"])
# now the backgrounds
model.create_node_from_path("root.visualization.widgets.timeseriesOne.hasBackground",
{"type": "const", "value": True})
model.create_node_from_path("root.visualization.widgets.timeseriesOne.background", {"type": "referencer"})
model.add_forward_refs("root.visualization.widgets.timeseriesOne.background", ["root.variables.back"])
model.create_node_from_path("root.visualization.widgets.timeseriesOne.backgroundMap", {"type": "const",
"value": {"1": "black",
"0": "white",
"-1": "blue",
"default": "white"}})
model.show()
elif testNo == 2:
# we take the full test number 1 and rearrange some things
model.create_test(1)
import data.occupancy_data.occupancy as occ
occData = occ.read_occupancy("./data/occupancy_data/datatest2.txt")
# create an official table
template = [
{
"name": "description",
"type": "const",
"value": "this is the occupancy data table"
},
{
"name": "columns",
"type": "referencer",
},
{
"name": "timeField",
"type": "referencer",
},
{
"name": "variables",
"type": "folder",
}
]
model.create_node("root", name="occupancy", type="table")
model.create_nodes_from_template("root.occupancy", template=template)
for var in occData:
path = "root.occupancy.variables." + var
model.create_node_from_path(path, {"type": "column"})
model.set_value(path, occData[var])
model.add_forward_refs("root.occupancy.columns", [path])
model.add_forward_refs("root.occupancy.timeField", ["root.occupancy.variables.date"])
# now create the classification
model.create_node("root.occupancy", name="classification", type="column")
model.set_value("root.occupancy.classification", [0] * len(occData[list(occData.keys())[0]]))
model.add_forward_refs("root.occupancy.columns", ["root.occupancy.classification"])
# create another TS-widget
model.create_node_from_path('root.visualization.widgets.timeseriesOccupancy', {"type": "widget"})
model.create_nodes_from_template('root.visualization.widgets.timeseriesOccupancy',
modeltemplates.timeseriesWidget)
model.create_nodes_from_template('root.visualization.widgets.timeseriesOccupancy.buttons.button1',
modeltemplates.button)
model.add_forward_refs('root.visualization.widgets.timeseriesOccupancy.selectedVariables',
["root.occupancy.variables.Temperature"])
model.add_forward_refs('root.visualization.widgets.timeseriesOccupancy.selectableVariables',
["root.occupancy.variables"])
model.add_forward_refs('root.visualization.widgets.timeseriesOccupancy.table', ['root.occupancy'])
model.add_forward_refs('root.visualization.widgets.timeseriesOccupancy.background',
['root.occupancy.classification'])
# now create the logistic regression
model.create_nodes_from_template('root', model.templates["logisticregression.logisticRegressionTemplate"])
model.add_forward_refs('root.logisticRegression.input',
['root.occupancy.variables.Temperature', 'root.occupancy.variables.Light',
'root.occupancy.variables.CO2'])
model.add_forward_refs('root.logisticRegression.output', ['root.occupancy.classification'])
model.add_forward_refs('root.logisticRegression.annotations',
['root.visualization.widgets.timeseriesOccupancy.hasAnnotation.newAnnotations'])
# also hook the button on it
model.add_forward_refs('root.visualization.widgets.timeseriesOccupancy.buttons.button1.onClick',
['root.logisticRegression'])
model.add_forward_refs('root.visualization.widgets.timeseriesOccupancy.observer',
['root.logisticRegression.executionCounter']) # observe the execution of the scorer
if __name__ == '__main__':
toCreate = sys.argv[1]
modelName = sys.argv[2]
myModel = model.Model()
if toCreate == "occupancy":
create_test(myModel,2)
elif toCreate == "simple":
create_test(myModel,1)
#now save it
myModel.save(modelName)