This repository has been archived by the owner on Feb 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
synthea_graphviz.rb
executable file
·254 lines (235 loc) · 7.91 KB
/
synthea_graphviz.rb
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
#!/usr/bin/ruby
require 'graphviz'
require 'synthea'
# color (true) or black & white (false)
COLOR = true
def generateRulesBasedGraph()
# Create a new graph
g = GraphViz.new( :G, :type => :digraph )
# Create the list of items
items = []
modules = {}
Synthea::Rules.metadata.each do |key,rule|
items << key
items << rule[:inputs]
items << rule[:outputs]
modules[ rule[:module_name] ] = true
end
items = items.flatten.uniq
# Choose a color for each module
# available_colors = GraphViz::Utils::Colors::COLORS.keys
available_colors = ['palevioletred','orange','lightgoldenrod','palegreen','lightblue','lavender','purple']
modules.keys.each_with_index do |key,index|
modules[key] = available_colors[index]
end
attribute_color = 'grey'
# Create a node for each item
nodes = {}
items.each{|i|nodes[i]=g.add_node(i.to_s)}
# Make items that are not rules boxes
components = nodes.keys - Synthea::Rules.metadata.keys
components.each do |i|
nodes[i]['shape']='Box'
if COLOR
nodes[i]['color']=attribute_color
nodes[i]['style']='filled'
end
end
# Create the edges
edges = []
Synthea::Rules.metadata.each do |key,rule|
node = nodes[key]
if COLOR
node['color'] = modules[rule[:module_name]]
node['style'] = 'filled'
end
begin
rule[:inputs].each do |input|
other = nodes[input]
if !edges.include?("#{input}:#{key}")
g.add_edge( other, node)
edges << "#{input}:#{key}"
end
end
rule[:outputs].each do |output|
other = nodes[output]
if !edges.include?("#{key}:#{output}")
g.add_edge( node, other)
edges << "#{key}:#{output}"
end
end
rescue Exception => e
binding.pry
end
end
# Generate output image
g.output( :png => "output/synthea_rules.png" )
end
def generateWorkflowBasedGraphs()
Dir.glob('../synthea/lib/generic/modules/*.json') do |wf_file|
# Create a new graph
g = GraphViz.new( :G, :type => :digraph )
# Create nodes based on states
nodeMap = {}
wf = JSON.parse(File.read(wf_file))
wf['states'].each do |name, state|
node = g.add_nodes(name, {'shape'=> 'record', 'style'=> 'rounded'})
details = ''
case state['type']
when 'Initial', 'Terminal'
node['color'] = 'black'
node['style'] = 'rounded,filled'
node['fontcolor'] = 'white'
when 'Guard'
details = "Allow if " + logicDetails(state['allow'])
when 'Delay', 'Death'
if state.has_key? 'range'
r = state['range']
details = "#{r['low']} - #{r['high']} #{r['unit']}"
elsif state.has_key? 'exact'
e = state['exact']
details = "#{e['quantity']} #{e['unit']}"
end
when 'Encounter'
if state['wellness']
details = 'Wait for regularly scheduled wellness encounter'
end
when 'SetAttribute'
v = state['value']
details = "Set '#{state['attribute']}' = #{v ? "'#{v}'" : 'nil'}"
when 'Symptom'
s = state['symptom']
if state.has_key? 'range'
r = state['range']
details = "#{s}: #{r['low']} - #{r['high']}"
elsif state.has_key? 'exact'
e = state['exact']
details = "#{s}: #{e['quantity']}"
end
end
# Things common to many states
if state.has_key? 'codes'
state['codes'].each do |code|
details = details + code['system'] + "[" + code['code'] + "]: " + code['display'] + "\\l"
end
end
if state.has_key? 'target_encounter'
verb = 'Perform'
case state['type']
when 'ConditionOnset'
verb = 'Diagnose'
when 'MedicationOrder'
verb = 'Prescribe'
end
details = details + verb + " at " + state['target_encounter'] + "\\l"
end
if state.has_key? 'reason'
details = details + "Reason: " + state['reason'] + "\\l"
end
if state.has_key? 'medication_order'
details = details + "Prescribed at: #{state['medication_order']}\\l"
end
if state.has_key? 'assign_to_attribute'
details = details + "Assign to Attribute: '#{state['assign_to_attribute']}'\\l"
end
if state.has_key? 'referenced_by_attribute'
details = details + "Referenced By Attribute: '#{state['referenced_by_attribute']}'\\l"
end
if details.empty?
node['label'] = (name == state['type']) ? name : "{ #{name} | #{state['type']} }"
else
node['label'] = "{ #{name} | { #{state['type']} | #{details} } }"
end
nodeMap[name] = node
end
# Create the edges based on the transitions
wf['states'].each do |name, state|
if state.has_key? 'direct_transition'
begin
g.add_edges( nodeMap[name], nodeMap[state['direct_transition']] )
rescue
puts "State '#{name}' is transitioning to an unknown state: '#{state['direct_transition']}'"
end
elsif state.has_key? 'distributed_transition'
state['distributed_transition'].each do |t|
pct = t['distribution'] * 100
pct = pct.to_i if pct == pct.to_i
begin
g.add_edges( nodeMap[name], nodeMap[t['transition']], {'label'=> "#{pct}%"})
rescue
puts "State '#{name}' is transitioning to an unknown state: '#{t['transition']}'"
end
end
elsif state.has_key? 'conditional_transition'
state['conditional_transition'].each_with_index do |t,i|
cnd = t.has_key?('condition') ? logicDetails(t['condition']) : 'else'
begin
g.add_edges( nodeMap[name], nodeMap[t['transition']], {'label'=> "#{i+1}. #{cnd}"})
rescue
puts "State '#{name}' is transitioning to an unknown state: '#{t['transition']}'"
end
end
elsif state.has_key? 'complex_transition'
transitions = Hash.new() { |hsh, key| hsh[key] = [] }
state['complex_transition'].each do |t|
cond = t.has_key?('condition') ? logicDetails(t['condition']) : 'else'
t['distributions'].each do |dist|
pct = dist['distribution'] * 100
pct = pct.to_i if pct == pct.to_i
nodes = [name, dist['transition']]
transitions[nodes] << "#{cond}: #{pct}%"
end
end
transitions.each do |nodes, labels|
begin
g.add_edges( nodeMap[nodes[0]], nodeMap[nodes[1]], {'label'=> labels.join(',\n')})
rescue
puts "State '#{nodes[0]}' is transitioning to an unknown state: '#{nodes[1]}'"
end
end
end
end
# Generate output image
g.output( :png => "output/#{wf['name']}.png" )
end
end
def logicDetails(logic)
case logic['condition_type']
when 'And', 'Or'
subs = logic['conditions'].map do |c|
if ['And','Or'].include?(c['condition_type'])
"(\\l" + logicDetails(c) + ")\\l"
else
logicDetails(c)
end
end
subs.join(logic['condition_type'].downcase + ' ')
when 'Not'
c = logic['condition']
if ['And','Or'].include?(c['condition_type'])
"not (\\l" + logicDetails(c) + ")\\l"
else
"not " + logicDetails(c)
end
when 'Gender'
"gender is '#{logic['gender']}'\\l"
when 'Age'
"age \\#{logic['operator']} #{logic['quantity']} #{logic['unit']}\\l"
when 'Socioeconomic Status'
"#{logic['category']} Socioeconomic Status\\l"
when 'Date'
"Year is \\#{logic['operator']} #{logic['year']}\\l"
when 'Symptom'
"Symptom: '#{logic['symptom']}' \\#{logic['operator']} #{logic['value']}\\l"
when 'PriorState'
"state '#{logic['name']}' has been processed\\l"
when 'Attribute'
"Attribute: '#{logic['attribute']}' \\#{logic['operator']} #{logic['value']}\\l"
else
"UNSUPPORTED_CONDITION(#{logic['condition_type']})\\l"
end
end
puts 'Rendering graphs to `./output` folder...'
generateRulesBasedGraph()
generateWorkflowBasedGraphs()
puts 'Done.'