-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
348 lines (296 loc) · 10.7 KB
/
visualize.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
import os
import time
import pandas as pd
from pandas import DataFrame
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import itertools
from rich.progress import track
from utils import parse_keys, get_kmeans, get_density
col_pal = px.colors.qualitative.Plotly
seq_col_pal = px.colors.sequential.Viridis
seq_col_pal_r = px.colors.sequential.Viridis_r
col_pal_iter = itertools.cycle(col_pal)
GOAL_KEY = {}
def visualize(g: bool, c: bool, heat: bool, limit: int, orient: str, saved: bool):
"""
Reads snapshot data and creates visuals depending on
passed options.
Returns fig as type plotly Figure
"""
# make sure snapshots exist
snapshots = None
if os.path.exists("snapshot.csv"):
# read snapshots from snapshots.csv
snapshots = pd.read_csv("snapshot.csv").drop(columns=["Unnamed: 0"])
# check if include saved snapshots
if saved:
# get the saved snapshots
DATA_DIR = os.getenv("DATA_DIR")
saved_shots = os.listdir(DATA_DIR)
combined = []
for s in saved_shots:
if s != "logs":
# get the dataframe
s_df = pd.read_csv(DATA_DIR + s + "/" + "snapshot.csv").drop(
columns=["Unnamed: 0"]
)
combined.append(s_df)
# if no saved snapshots...
if not len(combined):
# if current snapshot
if snapshots is not None:
pass
else:
# if no saved or current snapshot
print("No data found. Please run load.")
return None, None
# else if at least 1 snapshot
else:
# and if snapshots.csv already exists
if snapshots is not None:
combined_df = pd.concat(combined)
snapshots = pd.concat([snapshots, combined_df])
# otherwise, combined_df is snapshot
else:
combined_df = pd.concat(combined)
snapshots = combined_df
# if snapshot data
if snapshots is not None:
# null fig for flagging
fig = None
layout = None
# if --limit 1 was passed, return error
if limit == 1:
return print("Limiting does not currently support --limit=1")
# check if heat option passed
if heat:
# break if incompatible options --g or --c passed
if c:
return print("Heatmap does not support clustering.")
if g:
return print("Heatmap does not support grouping by goal.")
# if --heat create heatmap visual from snapshots
fig, layout = create_heatmap(snapshots, limit, orient)
else:
# else create scatter visual from snapshots
fig, layout = create_scatter(snapshots, g, c, limit, orient)
# return the plotly fig
return fig, layout
# if no snapshot data...
else:
print("No data found. Please run load.")
return None, None
def create_scatter(df: DataFrame, g: bool, c: bool, limit: int, orient: str):
"""
Processes passed df data and options & creates plotly visual
Returns fig as type plotly Figure
"""
# parse the path_goals defined in goals_key.xml
GOAL_KEY = parse_keys()
# define plotly fig
fig = go.Figure()
# get unique snapshot names
uniques = df["snapshot"].unique()
# if --limit passed...
if limit > 0:
# TODO: account for limit=1
# check if --orient option passed...
# defaults to "btm"
if orient == "top":
# if option passed
# slice the first n=limit snapshots
uniques = uniques[:limit]
elif orient == "btm":
# else slice the last n=limit snapshots
uniques = uniques[-limit : len(uniques)]
# get unique path_goals
unique_goals = df["path_goal"].unique()
# TODO: remap goal color palette according to goal priority
# since plotly color palettes length = 10,
# we reindex the goals using only goals in this snapshot,
# but preserve the unique 'gid' specified by DF data structures
# create a goal_colors dict for use with --g option
goal_colors = {}
# for each unique goal in snapshot collection...
for i, gid in enumerate(unique_goals):
# if index < 10 (plotly color palette length)
if i < 10:
# add entry to goal_colors where
# key: gid (goal id)
# value: color hex code at index i of col_pal
goal_colors[gid] = col_pal[i]
else:
# if gid out of col_pal index range...
# 'reset' i to 0 and add entry
goal_colors[gid] = col_pal[i - 10]
# to be used as final traces list
traces = []
# for each snapshot id in uniques
total = 0
for sid in track(uniques, description="Tracing snapshots..."):
# cycle through trace_color iterator
trace_color = next(col_pal_iter)
# filter df by current sid
filtered = df[df["snapshot"] == sid]
# get unique path_id's from snapshot
pid_uniques = filtered["path_id"].unique()
# if 'c' cluster flag passed...
# process the cluster in the snapshot loop,
# 1 cluster trace per snapshot
if c:
# get kmeans for snapshot
cluster_center, inertia = get_kmeans(filtered)
# rename x,y,z coordinates
kmeans_x = cluster_center[0, 0]
kmeans_y = cluster_center[0, 1]
kmeans_z = cluster_center[0, 2]
# create cluster trace
trace = go.Scatter3d(
x=[kmeans_x],
y=[kmeans_y],
z=[kmeans_z],
name=str(sid) + "_cluster",
mode="markers",
marker=dict(size=12, opacity=1, color="White"),
legendgroup=str(sid),
legendgrouptitle_text=str(sid),
)
# append to traces list
traces.append(trace)
# for each unique pid in snapshot...
for pid in pid_uniques:
# filter df for current pid
vectors = filtered[filtered["path_id"] == pid]
# get the common goal for this path
goal = vectors["path_goal"].mode()[0]
# create the path trace
trace = go.Scatter3d(
x=vectors["x"],
y=vectors["y"],
z=vectors["z"],
name=str(GOAL_KEY[goal]),
mode="markers+lines",
marker=dict(
size=6,
opacity=0.5,
color=trace_color if not g else goal_colors[goal],
),
line=dict(width=1, color=trace_color if not g else goal_colors[goal]),
legendgroup=str(sid),
legendgrouptitle_text=str(sid),
)
# append to traces list
traces.append(trace)
# increment progress bar
time.sleep(0.01)
total += 1
# add all traces in traces list to fig
fig.add_traces(traces)
# adjust the camera perspective
camera = dict(up=dict(x=0.0, y=0.0, z=1), eye=dict(x=0.0, y=0.1, z=2))
# make final layout updates
fig.update_layout(
template="plotly_dark",
scene_camera=camera,
scene=dict(
xaxis=dict(nticks=6, autorange="reversed"),
yaxis=dict(nticks=6),
zaxis=dict(nticks=4),
),
margin=dict(l=20, r=20, t=40, b=20),
)
layout = fig.layout
# return the figure
return fig, layout
def create_heatmap(df: DataFrame, limit: int, orient: str):
""" """
# parse the path_goals defined in goals_key.xml
GOAL_KEY = parse_keys()
# define plotly fig
fig = go.Figure()
# perform kneighbors on df
heat_df = get_density(df)
# get unique path_id's
uniques = heat_df["path_id"].unique()
# if --limit passed...
if limit > 0:
# TODO: account for limit=1
# check if --orient option passed...
# defaults to "btm"
if orient == "top":
# if option passed
# slice the first n=limit snapshots
uniques = uniques[:limit]
elif orient == "btm":
# else slice the last n=limit snapshots
uniques = uniques[-limit : len(uniques)]
# get unique path_goals
unique_goals = heat_df["path_goal"].unique()
# TODO: remap goal color palette according to goal priority
# since plotly color palettes length = 10,
# we reindex the goals using only goals in this snapshot,
# but preserve the unique 'gid' specified by DF data structures
# create a goal_colors dict for use with --g option
goal_colors = {}
# for each unique goal in snapshot collection...
for i, gid in enumerate(unique_goals):
# if index < 10 (plotly color palette length)
if i < 10:
# add entry to goal_colors where
# key: gid (goal id)
# value: color hex code at index i of col_pal
goal_colors[gid] = col_pal[i]
else:
# if gid out of col_pal index range...
# 'reset' i to 0 and add entry
goal_colors[gid] = col_pal[i - 10]
# to be used as final traces list
traces = []
# for unique path_ids
total = 0
for pid in track(uniques, description="Creating traces..."):
# filter df by current pid
vectors = heat_df[heat_df["path_id"] == pid]
# get the common goal for this path
goal = vectors["path_goal"].mode()[0]
# create the path trace
trace = go.Scatter3d(
x=vectors["x"],
y=vectors["y"],
z=vectors["z"],
name=str(GOAL_KEY[goal]),
mode="markers+lines",
legendgroup=str(pid),
legendgrouptitle_text=str(pid),
)
# append to traces list
traces.append(trace)
# increment progress bar
time.sleep(0.01)
total += 1
# add all traces in traces list to fig
fig.add_traces(traces)
fig.update_traces(
overwrite=True,
marker=dict(
size=6, opacity=0.5, color=heat_df["n_density"], colorscale="Inferno_r"
),
line=dict(width=1, color=heat_df["n_density"], colorscale="Inferno_r"),
)
# adjust the camera perspective
camera = dict(up=dict(x=0.0, y=0.0, z=1), eye=dict(x=0.0, y=0.1, z=2))
# make final layout updates
fig.update_layout(
template="plotly_dark",
scene_camera=camera,
scene=dict(
xaxis=dict(nticks=6, autorange="reversed"),
yaxis=dict(nticks=6),
zaxis=dict(nticks=4),
),
)
layout = fig.layout
# return the figure
return fig, layout