-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1346 lines (1148 loc) · 48.2 KB
/
app.py
File metadata and controls
1346 lines (1148 loc) · 48.2 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
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Shedding Hub Dashboard - New Version
Using shedding_hub package functions for visualization and statistics
"""
### Import packages
import matplotlib
matplotlib.use('Agg') # Use non-interactive backend before any other matplotlib imports
matplotlib.rcParams['axes.formatter.use_mathtext'] = False
matplotlib.rcParams['axes.formatter.useoffset'] = False
matplotlib.rcParams['text.usetex'] = False
import dash
from dash import Dash, html, dcc, callback, Output, Input, State, ALL, ctx
import pandas as pd
import json
import sys
# Import shedding_hub package functions
try:
from shedding_hub import load_dataset
from shedding_hub.viz import (
plot_time_course,
plot_time_courses,
plot_shedding_heatmap,
plot_mean_trajectory,
plot_value_distribution_by_time,
plot_detection_probability,
plot_clearance_curve
)
from shedding_hub.stats import (
calc_shedding_summary,
calc_detection_summary,
calc_clearance_summary,
calc_value_summary,
calc_dataset_summary,
compare_datasets
)
SHEDDING_HUB_AVAILABLE = True
except ImportError as e:
print(f"Warning: Could not import shedding_hub: {e}")
SHEDDING_HUB_AVAILABLE = False
import os
import glob
import yaml
from pathlib import Path
import io
import base64
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
### Global Variables
GITHUB_USERNAME = os.getenv("GITHUB_USERNAME")
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
# Data containers (to be populated)
datasets = {} # Dict of {dataset_id: dataset_dict}
list_biomarker = []
list_specimen = []
list_reference_events = []
dataset_study_map = {} # Map dataset_id to study info
### Data Loading Functions
def load_data():
"""Load all YAML datasets from the data directory"""
global datasets, dataset_study_map
data_dir = Path("data")
if not data_dir.exists():
print(f"Warning: Data directory '{data_dir}' not found")
return
yaml_files = [f for f in data_dir.glob("*.yaml") if not f.stem.startswith('.')]
print(f"Loading {len(yaml_files)} datasets...", end=" ", flush=True)
for yaml_file in yaml_files:
try:
# Load YAML file
with open(yaml_file, 'r', encoding='utf-8') as f:
dataset = yaml.safe_load(f)
# Extract dataset ID from filename
dataset_id = yaml_file.stem
dataset['dataset_id'] = dataset_id
# Store dataset
datasets[dataset_id] = dataset
# Store study info for dropdown (use dataset_id as label for brevity)
dataset_study_map[dataset_id] = {
'label': dataset_id,
'value': dataset_id
}
except Exception as e:
print(f"\nError loading {yaml_file}: {e}")
print(f"Done! Loaded {len(datasets)} datasets")
def get_unique_values():
"""Extract unique biomarkers, specimens, and reference events from datasets"""
global list_biomarker, list_specimen, list_reference_events
biomarkers = set()
specimens = set()
reference_events = set()
for dataset_id, dataset in datasets.items():
analytes = dataset.get('analytes', {})
for analyte_name, analyte_info in analytes.items():
# Collect biomarkers
biomarker = analyte_info.get('biomarker')
if biomarker:
biomarkers.add(biomarker)
# Collect specimens
specimen = analyte_info.get('specimen')
if isinstance(specimen, list):
specimens.update(specimen)
elif specimen:
specimens.add(specimen)
# Collect reference events
ref_event = analyte_info.get('reference_event')
if ref_event:
reference_events.add(ref_event)
list_biomarker = sorted(list(biomarkers))
list_specimen = sorted(list(specimens))
list_reference_events = sorted(list(reference_events))
print(f"Found {len(list_biomarker)} biomarkers, {len(list_specimen)} specimens, {len(list_reference_events)} reference events")
def create_welcome_overview():
"""Build an overview of all loaded datasets for the welcome page."""
# Summary counts
total_participants = 0
rows = []
for ds_id in sorted(datasets.keys()):
ds = datasets[ds_id]
analytes = ds.get('analytes', {})
participants = ds.get('participants', [])
n_participants = len(participants)
total_participants += n_participants
# Collect biomarkers and specimens for this dataset
bms = set()
specs = set()
for a_info in analytes.values():
bm = a_info.get('biomarker')
if bm:
bms.add(bm)
sp = a_info.get('specimen')
if isinstance(sp, list):
specs.update(sp)
elif sp:
specs.add(sp)
# Build DOI link for the study name
doi = ds.get('doi', '')
if doi:
doi_url = doi if doi.startswith('http') else f"https://doi.org/{doi}"
id_cell = html.Td(html.A(ds_id, href=doi_url, target="_blank"), className="td-bold")
else:
id_cell = html.Td(ds_id, className="td-bold")
rows.append(html.Tr([
id_cell,
html.Td(ds.get('title', 'N/A')[:80] + ('...' if len(ds.get('title', '')) > 80 else '')),
html.Td(', '.join(sorted(bms))),
html.Td(', '.join(sorted(specs))),
html.Td(str(n_participants), className="td-center"),
html.Td(str(len(analytes)), className="td-center"),
]))
overview = html.Div(
className="welcome-overview",
children=[
html.H3("Pathogen Shedding Data Analytics"),
html.P("Explore viral shedding patterns across different biomarkers, specimens, and time courses. "
"Click 'Create New Tab' to start."),
# Summary cards
html.Div(
className="summary-cards",
children=[
_summary_card("Datasets", len(datasets)),
_summary_card("Biomarkers", len(list_biomarker)),
_summary_card("Specimens", len(list_specimen)),
_summary_card("Reference Events", len(list_reference_events)),
_summary_card("Total Participants", total_participants),
],
),
# Dataset table
html.H5("Available Datasets"),
html.Div(
className="dataset-table-wrapper",
children=[
html.Table(
className="dataset-table",
children=[
html.Thead(html.Tr([
html.Th("ID"),
html.Th("Title"),
html.Th("Biomarker(s)"),
html.Th("Specimen(s)"),
html.Th("Participants"),
html.Th("Analytes"),
])),
html.Tbody(rows),
],
),
],
),
],
)
return overview
def _summary_card(label, value):
"""Create a small summary card with a number and label."""
return html.Div(
className="summary-card",
children=[
html.H3(str(value)),
html.P(label),
],
)
### Dashboard Component Functions
def create_banner():
"""Create the dashboard banner/header"""
return html.Div(
id="banner",
className="banner",
children=[
html.Img(src="assets/sh_logo.png"),
],
)
def create_description_card():
"""Create the description and welcome card"""
return html.Div(
id="description-card",
children=[
html.H5("Shedding Hub Dashboard"),
],
)
def create_dataset_browser():
"""Create a dataset browser grouped by pathogen/biomarker."""
# Group datasets by biomarker; assign each dataset to ONE group only (first biomarker)
pathogen_groups = {}
assigned = set()
for ds_id in sorted(datasets.keys()):
ds = datasets[ds_id]
analytes = ds.get('analytes', {})
bms = set()
for a_info in analytes.values():
bm = a_info.get('biomarker')
if bm:
bms.add(bm)
if not bms:
bms = {"Other"}
# Assign to first biomarker alphabetically to avoid duplicate IDs
primary_bm = sorted(bms)[0]
if ds_id not in assigned:
pathogen_groups.setdefault(primary_bm, []).append(ds_id)
assigned.add(ds_id)
# Build browser sections
sections = []
for pathogen in sorted(pathogen_groups.keys()):
ds_ids = pathogen_groups[pathogen]
study_buttons = []
for ds_id in ds_ids:
study_buttons.append(
html.Button(
ds_id,
id={"type": "browser-study-btn", "dataset_id": ds_id},
className="browser-study-item",
n_clicks=0,
)
)
sections.append(
html.Div(className="browser-pathogen-group", children=[
html.Div(
[html.Span("▶ ", className="collapse-indicator"), pathogen],
id={"type": "browser-pathogen-header", "pathogen": pathogen},
className="browser-pathogen-header",
n_clicks=0,
),
html.Div(
study_buttons,
id={"type": "browser-study-list", "pathogen": pathogen},
className="browser-study-list",
style={"display": "none"},
),
])
)
return html.Div(
id="control-card",
children=[
html.Button(
"+ New Tab",
id="create-tab-btn",
n_clicks=0,
className="btn-full-width btn-primary",
),
html.Hr(),
html.H6("Datasets"),
html.P("Click a study to open it", className="browser-hint"),
html.Div(
id="dataset-browser",
className="dataset-browser",
children=sections,
),
],
)
### Initialize the Dash app
app = Dash(
__name__,
meta_tags=[
{"name": "viewport", "content": "width=device-width, initial-scale=1"}
]
)
app.title = "Shedding Hub Dashboard"
app.index_string = '''
<!DOCTYPE html>
<html>
<head>
{%metas%}
<title>{%title%}</title>
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-03Z7167J99"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-03Z7167J99');
</script>
{%favicon%}
{%css%}
</head>
<body>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>
'''
server = app.server
load_data()
get_unique_values()
### App Layout
app.layout = html.Div(
id="app-container",
children=[
# Hidden stores for tab management
dcc.Store(id="tab-configs", data={}), # Store tab configurations
dcc.Store(id="tab-counter", data=0), # Counter for tab IDs
# Modal for creating new tab
html.Div(
id="new-tab-modal",
style={"display": "none"},
children=[
html.Div(
className="modal-dialog",
children=[
html.H4("Create New Tab"),
html.Hr(),
html.P("Tab Name:"),
dcc.Input(
id="new-tab-name",
type="text",
placeholder="Enter tab name...",
className="modal-input",
),
html.P("Study Type:"),
dcc.RadioItems(
id="new-tab-study-type",
options=[
{"label": "Individual Study", "value": "individual"},
{"label": "Multiple Studies", "value": "multiple"},
],
value="individual",
className="modal-radio",
),
html.P("Content Type:"),
dcc.RadioItems(
id="new-tab-content-type",
options=[
{"label": "Summary Statistics", "value": "statistics"},
{"label": "Plots/Visualizations", "value": "plots"},
],
value="plots",
className="modal-radio",
),
# Study selection (shown based on study type)
html.Div(
id="study-selection-container",
children=[
html.P("Select Study/Studies:"),
dcc.Dropdown(
id="new-tab-study-select",
options=[],
multi=False,
placeholder="Select a study...",
className="modal-radio",
),
],
),
html.Hr(),
html.Div(
className="modal-footer",
children=[
html.Button("Cancel", id="cancel-tab-btn", n_clicks=0),
html.Button("Create", id="confirm-tab-btn", n_clicks=0,
className="btn-primary"),
],
),
],
),
# Overlay background
html.Div(className="modal-overlay"),
],
),
# Left column - Controls
html.Div(
id="left-column",
className="three columns",
children=[
create_description_card(),
create_dataset_browser(),
],
),
# Right column - Tab-based interface
html.Div(
id="right-column",
className="nine columns",
children=[
html.Div(
id="tabs-container",
children=[
dcc.Tabs(
id="main-tabs",
value="welcome-tab",
children=[
dcc.Tab(
label="Welcome",
value="welcome-tab",
children=[create_welcome_overview()],
),
],
),
],
),
],
),
],
)
### Callbacks
# Tab Management Callbacks
@callback(
Output("new-tab-modal", "style"),
Output("new-tab-name", "value"),
Input("create-tab-btn", "n_clicks"),
Input("cancel-tab-btn", "n_clicks"),
Input("confirm-tab-btn", "n_clicks"),
State("new-tab-modal", "style"),
State("tab-counter", "data"),
prevent_initial_call=True
)
def toggle_new_tab_modal(create_clicks, cancel_clicks, confirm_clicks, current_style, tab_counter):
"""Show/hide the new tab creation modal and set default tab name"""
if ctx.triggered_id == "create-tab-btn":
# Generate default tab name based on current tab count
default_name = f"Tab {tab_counter + 1}"
return {"display": "block"}, default_name
elif ctx.triggered_id in ["cancel-tab-btn", "confirm-tab-btn"]:
return {"display": "none"}, ""
return current_style, ""
@callback(
Output("new-tab-study-select", "multi"),
Input("new-tab-study-type", "value")
)
def update_study_select_mode(study_type):
"""Enable multi-select for multiple studies, single-select for individual"""
return study_type == "multiple"
@callback(
Output("main-tabs", "children", allow_duplicate=True),
Output("tab-configs", "data", allow_duplicate=True),
Output("main-tabs", "value", allow_duplicate=True),
Input({"type": "close-tab-btn", "tab_id": ALL}, "n_clicks"),
State("main-tabs", "children"),
State("tab-configs", "data"),
State("main-tabs", "value"),
prevent_initial_call=True
)
def close_tab(n_clicks_list, current_tabs, tab_configs, current_active_tab):
"""Close a tab when the close button is clicked"""
# Check if any close button was clicked
if not any(n_clicks_list) or not ctx.triggered:
return current_tabs, tab_configs, current_active_tab
# Get the tab_id of the clicked close button
triggered_id = ctx.triggered_id
if not triggered_id or triggered_id == ".":
return current_tabs, tab_configs, current_active_tab
tab_id_to_remove = triggered_id["tab_id"]
# Remove the tab from the configs
if tab_id_to_remove in tab_configs:
del tab_configs[tab_id_to_remove]
# Remove the tab from the tab list
updated_tabs = [tab for tab in current_tabs if tab.get("props", {}).get("value") != tab_id_to_remove]
# If we're closing the currently active tab, switch to the Welcome tab
new_active_tab = current_active_tab
if current_active_tab == tab_id_to_remove:
new_active_tab = "welcome-tab"
return updated_tabs, tab_configs, new_active_tab
@callback(
Output("main-tabs", "children"),
Output("tab-configs", "data"),
Output("tab-counter", "data"),
Output("main-tabs", "value"),
Input("confirm-tab-btn", "n_clicks"),
State("new-tab-name", "value"),
State("new-tab-study-type", "value"),
State("new-tab-content-type", "value"),
State("new-tab-study-select", "value"),
State("main-tabs", "children"),
State("tab-configs", "data"),
State("tab-counter", "data"),
prevent_initial_call=True
)
def create_new_tab(n_clicks, tab_name, study_type, content_type, selected_studies,
current_tabs, tab_configs, tab_counter):
"""Create a new tab with user-specified configuration"""
if not n_clicks or not tab_name:
return current_tabs, tab_configs, tab_counter, None
# Generate new tab ID
new_tab_id = f"tab-{tab_counter}"
tab_counter += 1
# Store tab configuration
tab_configs[new_tab_id] = {
"name": tab_name,
"study_type": study_type,
"content_type": content_type,
"selected_studies": selected_studies,
}
# Create new tab content based on configuration
tab_content = create_tab_content(new_tab_id, tab_configs[new_tab_id])
# Add new tab to existing tabs
new_tab = dcc.Tab(
label=tab_name,
value=new_tab_id,
children=[tab_content],
)
current_tabs.append(new_tab)
return current_tabs, tab_configs, tab_counter, new_tab_id
def _get_dataset_filter_options(selected_studies):
"""Extract available biomarkers, specimens, and reference events from selected dataset(s)."""
biomarkers = set()
specimens = set()
ref_events = set()
# Normalize to list
if isinstance(selected_studies, str):
study_ids = [selected_studies]
elif isinstance(selected_studies, list):
study_ids = selected_studies
else:
study_ids = []
for ds_id in study_ids:
ds = datasets.get(ds_id, {})
for a_info in ds.get('analytes', {}).values():
bm = a_info.get('biomarker')
if bm:
biomarkers.add(bm)
sp = a_info.get('specimen')
if isinstance(sp, list):
specimens.update(sp)
elif sp:
specimens.add(sp)
ref = a_info.get('reference_event')
if ref:
ref_events.add(ref)
return sorted(biomarkers), sorted(specimens), sorted(ref_events)
def _create_filter_bar(tab_id, filter_prefix, selected_studies=None):
"""Create an inline filter bar for a tab, scoped to the selected dataset(s)."""
bms, specs, evts = _get_dataset_filter_options(selected_studies)
# Default to first option for each filter
default_bm = bms[0] if bms else None
default_spec = specs[0] if specs else None
default_evt = evts[0] if evts else None
return html.Div(
className="tab-filter-bar",
children=[
html.Div(className="tab-filter-item", children=[
html.Label("Biomarker"),
dcc.Dropdown(
id={"type": f"{filter_prefix}-biomarker", "tab_id": tab_id},
options=[{"label": bm, "value": bm} for bm in bms],
value=default_bm,
placeholder="All",
),
]),
html.Div(className="tab-filter-item", children=[
html.Label("Specimen"),
dcc.Dropdown(
id={"type": f"{filter_prefix}-specimen", "tab_id": tab_id},
options=[{"label": sp, "value": sp} for sp in specs],
value=default_spec,
placeholder="All",
),
]),
html.Div(className="tab-filter-item", children=[
html.Label("Reference Event"),
dcc.Dropdown(
id={"type": f"{filter_prefix}-ref-event", "tab_id": tab_id},
options=[{"label": evt, "value": evt} for evt in evts],
value=default_evt,
placeholder="All",
),
]),
html.Div(className="tab-filter-item", children=[
html.Label("Value Type"),
dcc.RadioItems(
id={"type": f"{filter_prefix}-value-type", "tab_id": tab_id},
options=[
{"label": "Concentration", "value": "concentration"},
{"label": "Ct Values", "value": "ct"},
],
value="concentration",
className="tab-filter-radio",
),
]),
],
)
def create_tab_content(tab_id, config):
"""Generate tab content based on configuration"""
study_type = config["study_type"]
content_type = config["content_type"]
# Common header with close button
header = html.Div(
className="tab-header",
children=[
html.H4(
f"{config['name']}{' - Summary Statistics' if content_type == 'statistics' else ''}"
f"{' - Multi-Study Comparison' if study_type == 'multiple' and content_type == 'plots' else ''}",
),
html.Button(
"✕ Close Tab",
id={"type": "close-tab-btn", "tab_id": tab_id},
n_clicks=0,
className="btn-close-tab",
),
],
)
# Use different filter prefixes so plot and stats callbacks have matching array lengths
filter_prefix = "plot-filter" if content_type == "plots" else "stats-filter"
if content_type == "statistics":
return html.Div(
className="tab-content-wrapper",
children=[
header,
_create_filter_bar(tab_id, filter_prefix, config.get("selected_studies")),
html.Hr(),
html.Div(id={"type": "tab-content", "tab_id": tab_id}),
],
)
else: # plots
if study_type == "individual":
plot_options = [
{"label": "Time Course Trajectories", "value": "time_course"},
{"label": "Mean Trajectory", "value": "mean_trajectory"},
{"label": "Detection Probability", "value": "detection"},
{"label": "Clearance Curve", "value": "clearance"},
{"label": "Shedding Heatmap", "value": "heatmap"},
{"label": "Value Distribution", "value": "distribution"},
]
else:
plot_options = [
{"label": "Comparison Time Courses", "value": "time_courses_compare"},
{"label": "Comparison Detection", "value": "detection_compare"},
{"label": "Comparison Clearance", "value": "clearance_compare"},
]
return html.Div(
className="tab-content-wrapper",
children=[
header,
_create_filter_bar(tab_id, filter_prefix, config.get("selected_studies")),
html.Hr(),
html.P("Select Plot Type:"),
dcc.Dropdown(
id={"type": "plot-type-select", "tab_id": tab_id},
options=plot_options,
value=plot_options[0]["value"],
className="plot-type-dropdown",
),
html.Div(
id={"type": "tab-plot", "tab_id": tab_id},
className="plot-container",
),
],
)
# Dynamic callbacks for tab content updates
@callback(
Output({"type": "tab-plot", "tab_id": ALL}, "children"),
Input({"type": "plot-type-select", "tab_id": ALL}, "value"),
Input({"type": "plot-filter-biomarker", "tab_id": ALL}, "value"),
Input({"type": "plot-filter-specimen", "tab_id": ALL}, "value"),
Input({"type": "plot-filter-ref-event", "tab_id": ALL}, "value"),
Input({"type": "plot-filter-value-type", "tab_id": ALL}, "value"),
Input("tab-configs", "data"),
)
def update_tab_plots(plot_types, biomarkers, specimens, ref_events, value_types, tab_configs):
"""Update plots in all tabs based on per-tab filters and plot type selection"""
if not SHEDDING_HUB_AVAILABLE:
return [html.Div("Shedding Hub package not available", className="error-message")] * max(len(plot_types), 1)
if not plot_types:
return []
# Get plot tabs in the order they appear
plot_tabs = [(k, v) for k, v in tab_configs.items() if v.get("content_type") == "plots"]
plot_elements = []
for idx, plot_type in enumerate(plot_types):
if idx >= len(plot_tabs):
plot_elements.append(html.Div("Tab configuration error", className="error-message"))
continue
tab_id, config = plot_tabs[idx]
study_type = config.get("study_type")
selected_studies = config.get("selected_studies")
# Per-tab filter values
biomarker = biomarkers[idx] if idx < len(biomarkers) else None
specimen = specimens[idx] if idx < len(specimens) else None
reference_event = ref_events[idx] if idx < len(ref_events) else None
value_type = value_types[idx] if idx < len(value_types) else "concentration"
try:
if study_type == "individual":
if not selected_studies:
plot_element = html.Div("Please select a study in the tab creation", className="error-message")
elif selected_studies not in datasets:
plot_element = html.Div(f"Dataset '{selected_studies}' not found", className="error-message")
else:
dataset = datasets[selected_studies]
print(f"Generating plot: {plot_type} for dataset: {selected_studies}")
plot_element = generate_individual_plot(
dataset, plot_type, biomarker, specimen, reference_event, value_type
)
else:
if not selected_studies or not isinstance(selected_studies, list):
plot_element = html.Div("Please select studies in the tab creation", className="error-message")
else:
study_datasets = [datasets[sid] for sid in selected_studies if sid in datasets]
if not study_datasets:
plot_element = html.Div("No valid datasets found", className="error-message")
else:
print(f"Generating comparison plot: {plot_type} for {len(study_datasets)} datasets")
plot_element = generate_comparison_plot(
study_datasets, plot_type, biomarker, specimen, reference_event, value_type
)
plot_elements.append(plot_element)
except Exception as e:
print(f"Error generating plot for tab {tab_id}: {e}")
import traceback
traceback.print_exc()
plot_elements.append(html.Div(f"Error: {str(e)}", className="error-message"))
return plot_elements
def _safe_tight_layout(*args, **kwargs):
"""Wrapper around plt.tight_layout that catches mathtext parsing errors."""
try:
_original_tight_layout(*args, **kwargs)
except (ValueError, Exception):
pass # Skip tight_layout if mathtext parsing fails
# Monkey-patch plt.tight_layout so the shedding_hub viz functions don't crash
_original_tight_layout = plt.tight_layout
plt.tight_layout = _safe_tight_layout
def matplotlib_to_img_src(mpl_fig):
"""Convert matplotlib figure to base64 encoded image source."""
buf = io.BytesIO()
try:
mpl_fig.savefig(buf, format='png', dpi=150, bbox_inches='tight')
except (ValueError, Exception):
# Fallback: save without bbox_inches='tight' if mathtext parsing fails
buf.seek(0)
buf.truncate()
mpl_fig.savefig(buf, format='png', dpi=150)
buf.seek(0)
img_base64 = base64.b64encode(buf.read()).decode('utf-8')
buf.close()
plt.close(mpl_fig)
return f'data:image/png;base64,{img_base64}'
def _call_viz_function(func, *args, **kwargs):
"""Safely call a shedding_hub viz function and return the matplotlib figure."""
return func(*args, **kwargs)
# Descriptions for each plot type (from viz.py docstrings)
PLOT_DESCRIPTIONS = {
"time_course": (
"Individual participant shedding trajectories over time. "
"Faceted line plots showing biomarker measurements for each participant, "
"organized by specimen type."
),
"mean_trajectory": (
"Mean/median trajectory with confidence bands across participants. "
"Shows the central tendency of measurements over time with a shaded band "
"showing the uncertainty range."
),
"detection": (
"Detection probability (proportion of positive measurements) over time. "
"Shows the probability of detecting a positive measurement at each time bin, "
"with 95% confidence intervals."
),
"clearance": (
"Kaplan-Meier style clearance curve showing proportion still shedding over time. "
"Clearance is defined as the time of the last positive measurement for each participant."
),
"heatmap": (
"Heatmap of shedding intensity over time across participants. "
"Rows represent participants, columns represent time bins, and color intensity "
"represents measurement values."
),
"distribution": (
"Distribution of measurement values at each time bin. "
"Box plots showing how measurement values are distributed at different time points, "
"useful for understanding variability in shedding patterns."
),
"time_courses_compare": (
"Comparison of individual participant shedding trajectories across multiple datasets. "
"Grid of faceted plots with each column representing a different study and rows "
"representing different specimen types."
),
"detection_compare": (
"Comparison of detection probability across multiple studies. "
"Shows the proportion of positive measurements over time for each study."
),
"clearance_compare": (
"Comparison of Kaplan-Meier clearance curves across multiple studies. "
"Shows the proportion of participants still shedding over time for each study."
),
}
def generate_individual_plot(dataset, plot_type, biomarker, specimen, reference_event, value_type):
"""Generate plot for individual study - returns html.Img element."""
kwargs = {}
if biomarker:
kwargs['biomarker'] = biomarker
if specimen:
kwargs['specimen'] = specimen
try:
if plot_type == "time_course":
# Uses figsize_width_per_specimen and figsize_height
if value_type:
kwargs['value'] = value_type
kwargs['figsize_width_per_specimen'] = 10
kwargs['figsize_height'] = 6
mpl_fig = _call_viz_function(plot_time_course, dataset, **kwargs)
elif plot_type == "mean_trajectory":
if value_type:
kwargs['value'] = value_type
kwargs['figsize'] = (10, 6)
mpl_fig = _call_viz_function(plot_mean_trajectory, dataset, **kwargs)
elif plot_type == "detection":
kwargs['figsize'] = (10, 6)
mpl_fig = _call_viz_function(plot_detection_probability, dataset, **kwargs)
elif plot_type == "clearance":
kwargs['figsize'] = (10, 6)
mpl_fig = _call_viz_function(plot_clearance_curve, dataset, **kwargs)
elif plot_type == "heatmap":
if value_type:
kwargs['value'] = value_type
kwargs['figsize'] = (12, 6)
mpl_fig = _call_viz_function(plot_shedding_heatmap, dataset, **kwargs)
elif plot_type == "distribution":
if value_type:
kwargs['value'] = value_type
mpl_fig = _call_viz_function(plot_value_distribution_by_time, dataset, **kwargs)
else:
return html.Div(f"Unknown plot type: {plot_type}", className="error-message")
img_src = matplotlib_to_img_src(mpl_fig)
description = PLOT_DESCRIPTIONS.get(plot_type, "")
return html.Div([
html.Img(src=img_src, className="plot-img"),
html.P(description, className="plot-description"),
])
except Exception as e:
print(f"Error in generate_individual_plot: {e}")
import traceback
traceback.print_exc()
return html.Div(f"Error: {str(e)}", className="error-message")
def generate_comparison_plot(datasets_list, plot_type, biomarker, specimen, reference_event, value_type):
"""Generate comparison plot for multiple studies - returns html.Img element"""
kwargs = {}
if biomarker:
kwargs['biomarker'] = biomarker
if specimen:
kwargs['specimen'] = specimen
try:
if plot_type == "time_courses_compare":
# Uses figsize_width_per_study and figsize_height_per_specimen