-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexploration.py
703 lines (632 loc) · 28.6 KB
/
exploration.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
import pandas as pd
import numpy as np
import seaborn as sns
import tslearn as tsl
import matplotlib.pyplot as plt
#import ts_preprocessing as tsp
import properties
import plotly
import numpy as np
import json
from pathlib import Path
class Explore:
def __init__(self, simulate=False, method="mean"):
self.tyt_file_path = properties.ts_file_location
self.simulate_file_path = properties.simulate_ts_file_location
self.features_df = self.preprocess_df(simulate)
@staticmethod
def categorize_days(val):
if (val > 4) and (val <= 8):
return 'Early Morning'
elif (val > 8) and (val <= 12):
return 'Morning'
elif (val > 12) and (val <= 16):
return 'Afternoon'
elif (val > 16) and (val <= 20):
return 'Evening'
elif (val > 20) and (val <= 24):
return 'Night'
elif val <= 4:
return 'Late Night'
@staticmethod
def create_hour_num(x):
if x[1] > 9:
return float(str(x[0]) + "." + str(x[1]))
else:
return float(str(x[0]) + "." + "".join("0" + str(x[1])))
def preprocess_df(self, simulate):
tyt_data = pd.read_pickle(self.tyt_file_path)
if simulate:
# Do the simulation concatenation if the file exists
p = Path(self.simulate_file_path)
if p.exists() and p.is_file():
simulate_tyt_data = pd.read_pickle(self.simulate_file_path)
simulate_tyt_data["user_id"] = simulate_tyt_data["user_id"].apply(int)
overall_data = tyt_data.append(simulate_tyt_data)
time_data_set = overall_data.copy()
else:
# Original data copy, avoid the error and go ahead to visualization.
print("No synthetic data present, however extracting over original data only.")
time_data_set = tyt_data.copy()
else:
# Original data copy
time_data_set = tyt_data.copy()
# Create some features
# Lets simply extract year month date hour min sec from the time series as a separate feature.
# Converting to timestamp format for observation
time_data_set["created_at"] = pd.to_datetime(time_data_set["created_at"])
# Exploring the datetime
daysofweek = {
0: "Monday",
1: "Tuesday",
2: "Wednesday",
3: "Thursday",
4: "Friday",
5: "Saturday",
6: "Sunday"
}
time_data_set["year"] = time_data_set["created_at"].apply(lambda x: x.year)
time_data_set["month"] = time_data_set["created_at"].apply(lambda x: x.month)
time_data_set["date"] = time_data_set["created_at"].apply(lambda x: x.date())
time_data_set["hour"] = time_data_set["created_at"].apply(lambda x: x.hour)
time_data_set["minute"] = time_data_set["created_at"].apply(lambda x: x.minute)
time_data_set["day"] = time_data_set["created_at"].apply(lambda x: x.day)
time_data_set["day_of_record"] = time_data_set["created_at"].apply(lambda x: x.dayofweek)
# time_data_set["day_of_record"] = time_data_set["created_at"].apply(lambda x: daysofweek[x.dayofweek])
#Preprocess from 0 to 1
time_data_set["s02"] = time_data_set["s02"].apply(lambda x: x / 100)
time_data_set["s03"] = time_data_set["s03"].apply(lambda x: x / 100)
time_data_set["s04"] = time_data_set["s04"].apply(lambda x: x / 100)
time_data_set["s05"] = time_data_set["s05"].apply(lambda x: x / 100)
time_data_set["s06"] = time_data_set["s06"].apply(lambda x: x / 100)
time_data_set["s07"] = time_data_set["s07"].apply(lambda x: x / 100)
#Create feature by binning categories for boxplot such as morning,
# evening, late evening, night
time_data_set["hour_num"] = time_data_set[["hour", "minute"]].apply(self.create_hour_num, axis=1)
time_data_set["hour_bins"] = time_data_set["hour_num"].apply(self.categorize_days)
return time_data_set
def create_number_of_observations_user(user_id, year, month, plot_type):
explore = Explore()
time_data_set = explore.features_df
user_data_ym = time_data_set[(time_data_set["user_id"] == int(user_id)) &
(time_data_set["year"] == int(year)) &
(time_data_set["month"] == int(month))]
#user_data = time_data_set[time_data_set["user_id"] == int(user_id)]
#user_data_y = user_data[user_data["year"] == int(year)]
#user_data_ym = user_data_y[user_data_y["month"] == int(month) ]
if plot_type == "bar":
# Hour based grouping
print("Grouping by user, day")
user_day_grp = user_data_ym.groupby(by=["user_id", "day"])
number_of_obs_s02 = []
number_of_obs_s03 = []
day_user_id = []
grp_day_usr = []
for grp in user_day_grp:
s03_list = [val for val in grp[1]["s03"].to_numpy() if not np.isnan(val)]
s02_list = [val for val in grp[1]["s02"].to_numpy() if not np.isnan(val)]
n_count_s03 = len(s03_list)
n_count_s02 = len(s02_list)
prev_day = grp[0][1]
# prev_hr = grp[0][2]
prev_user = grp[0][0]
number_of_obs_s02.append(n_count_s02)
number_of_obs_s03.append(n_count_s03)
day_user_id.append(prev_user)
grp_day_usr.append(prev_day)
# grp_hr_usr.append(prev_hr)
print("Length of n_observations s02 {}, s03 {} length of user_id {} and length of day {} for grouping by hour".format(
len(number_of_obs_s02), len(number_of_obs_s03),
len(day_user_id),
len(grp_day_usr)
))
dict_df_nobs2 = {
"user_id": day_user_id,
"days": grp_day_usr,
"n_obs_s02": number_of_obs_s02,
"n_obs_s03": number_of_obs_s03
}
df_user_nobs_day = pd.DataFrame(dict_df_nobs2)
return df_user_nobs_day
else:
# Day based grouping
print("Grouping by user, day and hour")
user_day_grp = user_data_ym.groupby(by=["user_id", "day", "hour"])
number_of_obs_s03_dh = []
number_of_obs_s02_dh = []
day_hruser_id = []
grp_hrday_usr_day = []
grp_hrday_usr_hr = []
for grp in user_day_grp:
s03_list = [val for val in grp[1]["s03"].to_numpy() if not np.isnan(val)]
s02_list = [val for val in grp[1]["s02"].to_numpy() if not np.isnan(val)]
n_count_s03 = len(s03_list)
n_count_s02 = len(s02_list)
prev_day = grp[0][1]
prev_hr = grp[0][2]
prev_user = grp[0][0]
number_of_obs_s02_dh.append(n_count_s02)
number_of_obs_s03_dh.append(n_count_s03)
day_hruser_id.append(prev_user)
grp_hrday_usr_day.append(prev_day)
grp_hrday_usr_hr.append(prev_hr)
dict_df_nobs1 = {
"user_id": day_hruser_id,
"days": grp_hrday_usr_day,
"hour": grp_hrday_usr_hr,
"n_obs_s02": number_of_obs_s02_dh,
"n_obs_s03": number_of_obs_s03_dh
}
df_user_nobs_day_hr = pd.DataFrame(dict_df_nobs1)
return df_user_nobs_day_hr
def plot_monthly_overview(user_summ_data, user_id):
monthly_dict = {1: "January", 2: "February", 3: "March", 4: "April", 5:"May", 6:"June",
7: "July", 8: "August", 9: "September", 10: "October", 11: "November", 12: "December"}
graph_overview_dict = dict(
data=[
dict(
x=[monthly_dict[int(month)] for month in np.unique(user_summ_data["month"].to_numpy())],
y=user_summ_data["s03"],
width = [0.8 for _ in range(0, len(user_summ_data))],
text=[str(val) for val in user_summ_data["s03"].to_numpy()],
textposition='auto',
hoverinfo='none',
fillcolor='rgba(0,128,128,0.4)',
marker=dict(
color='rgba(0,128,128,0.4)',
line=dict(
color='rgba(255, 63, 20, 0.7)',
width=1
)),
type="bar"
),
],
layout=dict(
title="".join('User - ' + str(user_id) + " Overview of s03 Observations"),
width="600",
height="400",
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
yaxis=dict(
showgrid=False,
title='Number of Observations',
zeroline=False,
gridwidth=2
),
xaxis=dict(
showgrid=False,
title='Months',
#tickvals=[int(month) for month in np.unique(user_summ_data["month"].to_numpy())],
#ticktext=[monthly_dict[int(month)] for month in np.unique(user_summ_data["month"].to_numpy())]
)
)
)
graph_json = json.dumps(graph_overview_dict, cls=plotly.utils.PlotlyJSONEncoder)
return graph_json
'''
Creates a box plot for the dataframe provided.
'''
def call_hour_boxplot(user_data, var_dict, user_id, col_name="s03"):
import plotly
import plotly.graph_objs as go
days_list = np.sort(user_data["date"].unique())
hours_list = np.sort(user_data["hour"].unique())
col_list = ["rgba(0,128,128, 0.4)" for i in range(len(hours_list))]
fig = go.Figure()
for day, colors, hours in zip(days_list, col_list, hours_list):
fig.add_trace(go.Box(
y=user_data[user_data["hour"] == hours][str(col_name)].to_numpy(),
name="".join("Hour-" + str(hours)),
hovertext=["".join("(Date:" + str(d) + ")") for d in
user_data[user_data["hour"] == hours]["date"].to_numpy()],
jitter=0.3,
pointpos=0,
boxmean=True,
boxpoints='all', # all points are shown
marker=dict(
color='rgba(24, 38, 114, 1)',
size=2.0,
line=dict(
color='rgba(255, 63, 20, 1)',
width=1
)),
fillcolor=str(colors),
width=0.45
))
fig.update_layout(
boxgap=0.05,
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
title_text="(User - {},{}-{}) at hour level"
.format(str(user_id), str(col_name), str(var_dict[str(col_name)])))
fig.update_xaxes(title_text="Hours")
fig.update_yaxes(title_text=str(var_dict[str(col_name)]))
fig.update_layout(showlegend=False)
return fig.to_dict()
#plotly.offline.plot(fig)
# plotly.offline.plot(fig, filename="users_distress_each_days_s03_notch_msd.html")
# plotly.offline.plot(fig, filename="users_distress_each_day_notch_msd_total_day_s03.html")
def create_box_plot(user_data, user_id, col_name="s03"):
# Create the plot using plotly to be used from UI and ajax
var_dict = {
"s02": "Tinnitus Loudness",
"s03": "Tinnitus Distress",
"s04": "Wellness of hearing",
"s05": "Limited by hearing ability",
"s06": "Level of stress",
"s07": "Level of Exhaustion"
}
graphs = [
dict(
data=[
dict(
x=user_data["day"],
y=user_data[col_name],
#x=["".join("Day-" + str(day)) for day in np.unique(user_data["day"].to_numpy())],
boxpoints='all',
jitter=0.3,
pointpos=0,
boxmean=True,
fillcolor='rgba(0,128,128,0.4)',
marker=dict(
color='rgba(24, 38, 114, 1)',
size=1.5,
line=dict(
color='rgba(255, 63, 20, 1)',
width=1
)),
type="box"
),
],
layout=dict(
title="".join('(User ' + str(user_id) + "," + str(col_name) + "-" + var_dict[str(col_name)] + ") at day level"),
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
#width="950",
#height="400",
yaxis=dict(
showgrid = False,
title = "".join(var_dict[str(col_name)] + " - " + str(col_name)),
rangemode='tozero'
),
xaxis=dict(
showgrid=False,
title='Days',
tickvals=[int(day) for day in np.unique(user_data["day"].to_numpy())],
ticktext=["".join("Day-" + str(day)) for day in np.unique(user_data["day"].to_numpy())]
)
)
),
call_hour_boxplot(user_data, var_dict, user_id, col_name)
]
graph_json = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder)
return graph_json
'''
Creates a bar plot for the dataframe provided for the users
'''
def call_nobs_hr_boxplot(nobs_data, var, user_id, col_name):
import plotly
import plotly.graph_objs as go
days_list = np.sort(nobs_data["days"].unique())
hours_list = np.sort(nobs_data["hour"].unique())
col_list = ["rgba(41, 168, 214, 1)" for _ in range(len(hours_list))]
fig = go.Figure()
for day, colors, hours in zip(days_list, col_list, hours_list):
fig.add_trace(go.Box(
y=nobs_data[nobs_data["days"] == day][str(col_name)].to_numpy(),
name="".join("Day-" + str(day)),
hovertext=["".join("(Hour of Day:" + str(h) + ")") for h in
nobs_data[nobs_data["days"] == day]["hour"].to_numpy()],
jitter=0.3,
pointpos=0,
boxmean=True,
boxpoints='all', # all points are shown
marker=dict(
color='rgba(24, 38, 114, 1)',
size=2.0,
line=dict(
color='rgba(255, 63, 20, 1)',
width=1
)),
fillcolor=str(colors),
width=0.45
))
fig.update_layout(
boxgap=0.05,
title_text="Number of observations for User - {} over days for {}"
.format(str(user_id), str(var)))
fig.update_xaxes(title_text="Hours")
fig.update_yaxes(title_text="Number of observations")
return fig.to_dict()
def create_bar_plot(nobs_data, user_id, plot_type):
# Create the plot and mock plotly json to be used from UI and ajax requests
if plot_type == "bar":
graphs = [
dict(
data=[
dict(
x=nobs_data["days"],
y=nobs_data["n_obs_s03"],
text=[str(val) for val in nobs_data["n_obs_s03"].to_numpy()],
width=[0.8 for _ in range(0, len(nobs_data))],
textposition= 'auto',
hoverinfo= 'none',
fillcolor='rgba(0,128,128,0.4)',
marker=dict(
color='rgba(0,128,128,0.4)',
line=dict(
color='rgba(255, 63, 20, 0.7)',
width=1
)),
type="bar"
),
],
layout=dict(
title="".join('User - ' + str(user_id) + " "),
#width="950",
#height="400",
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
yaxis=dict(
showgrid=False,
title = 'Total number of observations (Tinnitus Distress - S03)',
zeroline= False,
gridwidth= 2
),
xaxis=dict(
showgrid=False,
title='Days',
tickvals=[int(day) for day in np.unique(nobs_data["days"].to_numpy())],
ticktext=["".join("Day-" + str(day)) for day in np.unique(nobs_data["days"].to_numpy())]
)
)
),
dict(
data=[
dict(
x=nobs_data["days"],
y=nobs_data["n_obs_s02"],
text=[str(val) for val in nobs_data["n_obs_s02"].to_numpy()],
textposition='auto',
hoverinfo='none',
fillcolor='#29a8d6',
marker=dict(
color='#29a8d6',
line=dict(
color='rgba(255, 63, 20, 0.7)',
width=1
)),
type="bar"
),
],
layout=dict(
title="".join('User - ' + str(user_id) + "bar plot for tinnitus loudness"),
#width="950",
#height="400",
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
yaxis=dict(
title='Total number of observations (Tinnitus Loudness - (S02))',
zeroline=False,
gridwidth=2
),
xaxis=dict(
title='Days',
tickvals=[int(day) for day in np.unique(nobs_data["days"].to_numpy())],
ticktext=["".join("Day-" + str(day)) for day in np.unique(nobs_data["days"].to_numpy())]
)
)
)
]
graph_json = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder)
elif plot_type == "box":
'''
graphs = [
dict(
data=[
dict(
x=nobs_data["days"],
y=nobs_data["n_obs_s03"],
#hovertext = ["".join("Hour of Day - " + str(hr)) for hr in nobs_data["hour"].to_numpy()],
boxpoints='all',
jitter=0.3,
pointpos=0,
boxmean=True,
fillcolor='rgba(41, 168, 214, 1)',
marker=dict(
color='rgba(24, 38, 114, 1)',
size=1.5,
line=dict(
color='rgba(255, 63, 20, 1)',
width=1
)),
type="box"
),
],
layout=dict(
title="".join('Tinnitus Distress Box plot for the User - ' + str(user_id)),
# width="950",
# height="400",
yaxis=dict(
title='Number of observations (Tinnitus Distress - S03) per day',
rangemode='tozero'
),
xaxis=dict(
title='Days',
tickvals=[int(day) for day in np.unique(nobs_data["days"].to_numpy())],
ticktext=["".join("Day-" + str(day)) for day in np.unique(nobs_data["days"].to_numpy())]
)
)
),
dict(
data=[
dict(
x=nobs_data["days"],
y=nobs_data["n_obs_s02"],
boxpoints='all',
jitter=0.3,
pointpos=0,
boxmean=True,
fillcolor='rgba(41, 168, 214, 1)',
marker=dict(
color='rgba(24, 38, 114, 1)',
size=1.5,
line=dict(
color='rgba(255, 63, 20, 1)',
width=1
)),
type="box"
),
],
layout=dict(
title="".join('Tinnitus Loudness Box plot for the User - ' + str(user_id)),
# width="950",
# height="400",
yaxis=dict(
title='Number of observations (Tinnitus Loudness - (S02)) per day',
rangemode='tozero'
),
xaxis=dict(
title='Days',
tickvals=[int(day) for day in np.unique(nobs_data["days"].to_numpy())],
ticktext=["".join("Day-" + str(day)) for day in np.unique(nobs_data["days"].to_numpy())]
)
)
)
]
'''
graphs = [call_nobs_hr_boxplot(nobs_data, "tinnitus distress", user_id, "n_obs_s03"),
call_nobs_hr_boxplot(nobs_data, "tinnitus loudness", user_id, "n_obs_s02")]
graph_json = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder)
return graph_json
def visualize_all_users_box(g_type, variable):
import plotly.graph_objs as go
if g_type == "day_hour":
nobs_data = create_number_of_observations_user(grouping=["user_id", "day", "hour"])
# Do it by day and hour
df_grp_day_nobs_dh = nobs_data.groupby(["user_id", "days"]).sum().reset_index()
total_day_users_dayhr = pd.DataFrame({"day": df_grp_day_nobs_dh["days"].value_counts().index,
"total_users": df_grp_day_nobs_dh["days"].value_counts().values})
total_users_dh = total_day_users_dayhr.sort_values(["day"])["total_users"].to_numpy()
if variable == "Tinnitus_Loudness":
col_list = ["#778899" for _ in range(31)]
days_list = np.sort(nobs_data["days"].unique())
user_ids = nobs_data["user_id"].unique()
fig = go.Figure()
for day, colors, t_users in zip(days_list, col_list, total_users_dh):
fig.add_trace(go.Box(
y=nobs_data[nobs_data["days"] == day]["n_obs_s02"].to_numpy(),
name="".join("(Day-" + str(day) + " \n total_users-" + str(t_users) + ")"),
hovertext=["".join("(User_id: " + str(id) + "\n Hour: " + str(hr) + ")") for id, hr in
zip(nobs_data[nobs_data["days"] == day]["user_id"].to_numpy(),
nobs_data[nobs_data["days"] == day]["hour"].to_numpy())],
jitter=0.3,
pointpos=0,
boxmean=True,
boxpoints='all', # all points are shown
fillcolor=str(colors),
marker_color='rgb(7,40,89)',
line_color='rgb(7,40,89)',
width=.55
))
fig.update_layout(
boxgap=0.05,
title_text="Users (S02 - Tinnitus Loudness) sequence of observations at day level")
fig.update_xaxes(title_text="Days")
fig.update_yaxes(title_text="number_of_observations")
# plotly.offline.plot(fig, filename="users_distress_each_days_s03_notch_msd.html")
# plotly.offline.plot(fig, filename="users_distress_each_day_notch_msd1.html")
return fig.to_json()
else:
col_list = ["#778899" for i in range(31)]
days_list = np.sort(nobs_data["days"].unique())
user_ids = nobs_data["user_id"].unique()
fig = go.Figure()
for day, colors, t_users in zip(days_list, col_list, total_users_dh):
fig.add_trace(go.Box(
y=nobs_data[nobs_data["days"] == day]["n_obs_s03"].to_numpy(),
name="".join("(Day-" + str(day) + " \n total_users-" + str(t_users) + ")"),
hovertext=["".join("(User_id: " + str(id) + "\n Hour: " + str(hr) + ")") for id, hr in
zip(nobs_data[nobs_data["days"] == day]["user_id"].to_numpy(),
nobs_data[nobs_data["days"] == day]["hour"].to_numpy())],
jitter=0.3,
pointpos=0,
boxmean=True,
boxpoints='all', # all points are shown
fillcolor=str(colors),
marker_color='rgb(7,40,89)',
line_color='rgb(7,40,89)',
width=.55
))
fig.update_layout(
boxgap=0.05,
title_text="Users (S03 - Tinnitus Distress) sequence of observations at day level ")
fig.update_xaxes(title_text="Days")
fig.update_yaxes(title_text="number_of_observations")
# plotly.offline.plot(fig, filename="users_distress_each_days_s03_notch_msd.html")
#plotly.offline.plot(fig, filename="users_distress_each_day_notch_msd1.html")
return fig.to_json
else:
nobs_data = create_number_of_observations_user(grouping=["user_id", "day"])
total_day_users = pd.DataFrame({"day": nobs_data["days"].value_counts().index,
"total_users": nobs_data["days"].value_counts().values})
total_users = total_day_users.sort_values(["day"])["total_users"].to_numpy()
# This is by day
if variable == "Tinnitus_Loudness":
col_list = ["#778899" for i in range(31)]
import plotly.graph_objects as go
days_list = np.sort(nobs_data["days"].unique())
user_ids = nobs_data["user_id"].unique()
fig = go.Figure()
for day, colors, t_users in zip(days_list, col_list, total_users):
fig.add_trace(go.Box(
y=nobs_data[nobs_data["days"] == day]["n_obs_s02"].to_numpy(),
name="".join("(Day-" + str(day) + " \n total_users-" + str(t_users) + ")"),
hovertext=["".join("(User_id: " + str(id) + ")") for id in
nobs_data[nobs_data["days"] == day]["user_id"].to_numpy()],
jitter=0.3,
pointpos=0,
boxmean="sd",
boxpoints='all', # all points are shown
fillcolor=str(colors),
marker_color='rgb(7,40,89)',
line_color='rgb(7,40,89)',
width=.55
))
fig.update_layout(
boxgap=0.05,
title_text="Users (S02 - Tinnitus Loudness) sequence of observations at day level ")
fig.update_xaxes(title_text="Days")
fig.update_yaxes(title_text="Total number_of_observations (Tinnitus Loudness - (S02))")
# plotly.offline.plot(fig, filename="users_distress_each_days_s03_notch_msd.html")
#plotly.offline.plot(fig, filename="users_distress_each_day_notch_msd_total_day_s03.html")
return fig.to_json
else:
col_list = ["#778899" for i in range(31)]
import plotly.graph_objects as go
days_list = np.sort(nobs_data["days"].unique())
user_ids = nobs_data["user_id"].unique()
fig = go.Figure()
for day, colors, t_users in zip(days_list, col_list, total_users):
fig.add_trace(go.Box(
y=nobs_data[nobs_data["days"] == day]["n_obs_s03"].to_numpy(),
name="".join("(Day-" + str(day) + " \n total_users-" + str(t_users) + ")"),
hovertext=["".join("(User_id: " + str(id) + ")") for id in
nobs_data[nobs_data["days"] == day]["user_id"].to_numpy()],
jitter=0.3,
pointpos=0,
boxmean="sd",
boxpoints='all', # all points are shown
fillcolor=str(colors),
marker_color='rgb(7,40,89)',
line_color='rgb(7,40,89)',
width=.55
))
fig.update_layout(
boxgap=0.05,
title_text="Users (S03 - Tinnitus Distress) sequence of observations at day level ")
fig.update_xaxes(title_text="Days")
fig.update_yaxes(title_text="Total number_of_observations (Tinnitus Distress - (S03))")
#plotly.offline.plot(fig, filename="users_distress_each_days_s03_notch_msd.html")
#plotly.offline.plot(fig, filename="users_distress_each_day_notch_msd_total_day_s03.html")
return fig.to_json