-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmodel.xml
More file actions
406 lines (355 loc) · 14 KB
/
model.xml
File metadata and controls
406 lines (355 loc) · 14 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<project heartbeat-interval="1" index="pi_HASH" luaroot="@ESP_PROJECT_OUTPUT@/luaroot" name="lua_snippet" pubsub="none" threads="4">
<description><![CDATA[This example simulates healthcare data streaming into a model and detects measurements that fall outside an acceptable range. A Lua snippet in the project subscribes to windows, stores values, and reads a JSON file. Lua connectors are also used.]]></description>
<metadata>
<meta id="layout">{"cq":{"addMetricInfo":{"x":610,"y":175},"alert":{"x":260,"y":50},"analysis":{"x":260,"y":175},"isAlert":{"x":750,"y":295},"metrics":{"x":610,"y":50},"patients":{"x":50,"y":50},"statistic":{"x":830,"y":50}}}</meta>
<meta id="studioUploadedBy">anonymous</meta>
<meta id="studioUploaded">1759849970074</meta>
<meta id="studioModifiedBy">anonymous</meta>
<meta id="studioModified">1759850110590</meta>
<meta id="studioTags">Example</meta>
</metadata>
<lua-snippets>
<lua-snippet name="snippet" init="init" destroy="done">
<description><![CDATA[This snippet subscribes to windows, stores values, and reads a JSON file.]]></description>
<lua-thread func="loadPatients" interval="5 seconds"/>
<code><![CDATA[local subscribers = {}
local metrics = {}
local stats = {}
function init()
subscribers[1] = esp_subscribe({window="statistic",handler="handleStats"})
subscribers[2] = esp_subscribe({window="metrics",handler="handleMetrics",snapshot=true})
subscribers[3] = esp_subscribe({window="isAlert",handler="handleAlert"})
end
function handleStats(events)
for _,stat in ipairs(events)
do
if (stats[stat.patient] == nil)
then
stats[stat.patient] = {}
end
stats[stat.patient][stat.statistic] = stat.value
end
end
function handleMetrics(events)
for _,metric in ipairs(events)
do
metrics[metric.name] = metric
end
esp_setProperty({name="metrics",value=metrics})
end
function handleAlert(events)
for _,event in ipairs(events)
do
local data = stats[event.patient]
if (data ~= nil)
then
local patientAlertCount = esp_getProperty({name="patient_alert_count"..event.patient})
if (patientAlertCount == nil)
then
patientAlertCount = {patient=event.patient,count=1}
else
patientAlertCount.count = patientAlertCount.count + 1
end
esp_setProperty({name="patient_alert_count"..event.patient,value=patientAlertCount})
local e = {}
e.patient = event.patient
for k,v in pairs(data)
do
e[k] = v
end
e[event.statistic] = event.value
e["patient_alert_count"] = patientAlertCount.count
e.patientAlert = event.value
esp_publishAsync({window="alert",events=e})
end
end
end
function
loadPatients()
local f = io.open("@ESP_PROJECT_HOME@/test_files/patients.json","r")
local contents = f:read("*all")
f:close()
local data = esp_parseJson(contents)
local patient_data = {}
for _,v in ipairs(data)
do
patient_data[v.name] = v
end
esp_setProperty({name="patient_data",value=patient_data})
return true
end
function done()
for _,s in ipairs(subscribers)
do
esp_unsubscribe(s)
end
end]]></code>
</lua-snippet>
</lua-snippets>
<contqueries>
<contquery include-singletons="true" name="cq" trace="analysis">
<windows>
<window-source index="pi_HASH" name="metrics">
<description><![CDATA[The "metrics" Source window has a Lua connector that publishes statistics related to patient health, including upper and lower limits for acceptable levels. Example: "systolic_bp,Systolic Blood Pressure,cardiovascular,60,80".]]></description>
<schema>
<fields>
<field key="true" name="name" type="string"/>
<field name="description" type="string"/>
<field name="system" type="string"/>
<field name="lower" type="double"/>
<field name="upper" type="double"/>
</fields>
</schema>
<connectors>
<connector class="lua" name="pub">
<properties>
<property name="type"><![CDATA[pub]]></property>
<property name="code"><![CDATA[local data = [[
systolic_bp,Systolic Blood Pressure,cardiovascular,60,80
diastolic_bp,Diastolic Blood Pressure,cardiovascular,90,120
pulse,Heart Rate,cardiovascular,60,100
sodium,Sodium Levels,cardiovascular,136,145
respiration,Respiration Rate,respiratory,12,18
oxygen,Blood Oxygen Level,respiratory,95,100
c02,Blood CO2 Level,respiratory,23,30
temperature,Body Temperature,nervous,97.8,99.1
glucose,Glucose Level,nervous,70,99
potassium,Potassium Levels,nervous,3.5,5.0
calcium,Calcium Levels,skeletal,8.6,10.2
cholesterol,Cholesterol Levels,skeletal,120,200
creatinine,Creatinine,skeletal,0.5,1.1
]]
function publish()
esp_injectCsv(data,{opcode="i",flags="n",quiesce=true})
return true
end]]></property>
</properties>
</connector>
</connectors>
</window-source>
<window-source index="pi_HASH" name="patients">
<description><![CDATA[The "patients" Source window has a Lua connector that generates patient IDs from 1 to n, depending on the value of the `numpatients` variable in the Lua code.]]></description>
<schema>
<fields>
<field key="true" name="name" type="string"/>
</fields>
</schema>
<connectors>
<connector class="lua" name="pub">
<properties>
<property name="type"><![CDATA[pub]]></property>
<property name="code"><![CDATA[local numpatients = 10
function publish()
local patients = {}
for i=1,numpatients
do
patients[#patients + 1] = {name="Patient "..tostring(i)}
end
esp_inject(patients,{opcode="i",flags="n",quiesce=true})
return true
end]]></property>
</properties>
</connector>
</connectors>
</window-source>
<window-source name="statistic">
<description><![CDATA[The "statistic" Source window has a Lua connector that generates random values for each metric. Twenty percent of the time, the code extends the boundaries for the generated values so that a value outside of the acceptable range might be generated.]]></description>
<schema>
<fields>
<field key="true" name="patient" type="string"/>
<field key="true" name="statistic" type="string"/>
<field name="value" type="double"/>
</fields>
</schema>
<connectors>
<connector class="lua" name="pub">
<properties>
<property name="type"><![CDATA[pub]]></property>
<property name="interval"><![CDATA[2000 milliseconds]]></property>
<property name="code"><![CDATA[math.randomseed(os.time())
function publish()
local patients = esp_query({window="patients"})
local metrics = esp_query({window="metrics"})
local events = {}
local rangepct = 20
for _,vital in ipairs(metrics.events)
do
for _,patient in ipairs(patients.events)
do
vital.from = vital.lower
vital.to = vital.upper
vital.range = vital.to - vital.from
vital.mid = vital.lower + (vital.range / 2)
if (math.random(0,5) == 0)
then
local pct = math.random(0,math.random(0,rangepct)) / 100
vital.from = vital.from - (vital.range * pct)
vital.to = vital.to + (vital.range * pct)
vital.range = vital.to - vital.from
end
local event = {}
event.patient = patient.name
event.statistic = vital.name
local offset = (vital.range * math.random()) / 2
if (math.random(0,1) == 0)
then
offset = -offset
end
event.value = vital.mid + offset
event._opcode = "upsert"
events[#events + 1] = event
end
end
if (#events > 0)
then
for i, evt in ipairs(events) do
esp_inject({evt}) -- inject a single event as a one-element table
esp_sleep(20) -- pause before the next event
end
end
end]]></property>
</properties>
</connector>
</connectors>
</window-source>
<window-source index="pi_EMPTY" name="alert">
<description><![CDATA[The "alert" Source window receives data from a Lua snippet.]]></description>
<schema>
<fields>
<field key="true" name="patient" type="string"/>
<field name="systolic_bp" type="int32"/>
<field name="diastolic_bp" type="int32"/>
<field name="respiration" type="int32"/>
<field name="pulse" type="int32"/>
<field name="temperature" type="double"/>
<field name="glucose" type="int32"/>
<field name="oxygen" type="int32"/>
<field name="c02" type="int32"/>
<field name="potassium" type="double"/>
<field name="sodium" type="int32"/>
<field name="calcium" type="double"/>
<field name="cholesterol" type="int32"/>
<field name="creatinine" type="double"/>
<field name="patient_alert_count" type="int32"/>
</fields>
</schema>
</window-source>
<window-join name="addMetricInfo">
<description><![CDATA[The "addMetricInfo" Join window joins the metrics from the "metrics" window with the values generated by the "statistic" window.]]></description>
<join type="leftouter" no-regenerates="true">
<conditions>
<fields left="statistic" right="name"/>
</conditions>
</join>
<output>
<left-select>value</left-select>
<right-select>lower,upper,system</right-select>
</output>
</window-join>
<window-filter init="init" name="isAlert" func="filter">
<description><![CDATA[The "isAlert" Filter window generates alerts only for the patients who are found in the data from the patients.json file.]]></description>
<code><![CDATA[local patient_data = nil
function init()
esp_addPropertyListener({name="patient_data",change="setPatientData",init=true})
end
function setPatientData(property)
patient_data = property.value
end
function filter(data)
if (patient_data == nil)
then
return false
end
local code = false
if (patient_data ~= nil)
then
local patient = patient_data[data.patient]
if (patient ~= nil)
then
if (data.value < data.lower or data.value > data.upper)
then
for _,sys in ipairs(patient.monitor_systems)
do
if (data.system == sys.name)
then
code = true
break
end
end
end
end
end
return code
end]]></code>
</window-filter>
<window-lua events="create" index="pi_EMPTY" init="init" name="analysis">
<description><![CDATA[The "analysis" Lua window generates summaries about values that are out of bounds and which doctors should be contacted. This window also keeps track of the total number of alerts per patient.]]></description>
<schema copy="alert">
<fields>
<field name="patient" type="string" key="true"/>
<field name="notes" type="string"/>
<field name="total_alerts" type="int32"/>
</fields>
</schema>
<code><![CDATA[local metrics = {}
local patient_data = {}
function init()
esp_addPropertyListener({name="metrics",change="setMetrics",init=true})
esp_addPropertyListener({name="patient_data",change="setPatientData",init=true})
end
function setMetrics(property)
metrics = property.value
end
function setPatientData(property)
patient_data = property.value
end
function create(data)
local info = patient_data[data.patient]
local notes = nil
if (info ~= nil)
then
for k,v in pairs(data)
do
if (metrics[k] ~= nil)
then
local metric = metrics[k]
if (v < metric.lower or v > metric.upper)
then
local sysinfo = nil
for _,msys in ipairs(info.monitor_systems)
do
if (msys.name == metric.system)
then
sysinfo = msys
break
end
end
notes = (notes == nil) and "" or notes.." "
if (v < metric.lower)
then
notes = notes.."The "..k.." is too low at "..v
else
notes = notes.."The "..k.." is too high at "..v
end
if (sysinfo ~= nil)
then
notes = notes.." so you should contact "..sysinfo.doctor.." about the "..sysinfo.name.." system"
end
notes = notes.."."
end
end
end
end
local e = (notes ~= nil) and {patient=data.patient,notes=notes,total_alerts=data.patient_alert_count} or nil
return e
end]]></code>
</window-lua>
</windows>
<edges>
<edge source="statistic metrics" target="addMetricInfo"/>
<edge source="addMetricInfo" target="isAlert"/>
<edge source="alert" target="analysis"/>
</edges>
</contquery>
</contqueries>
</project>