-
Notifications
You must be signed in to change notification settings - Fork 0
/
xnat_dashboard.py
278 lines (248 loc) · 9.1 KB
/
xnat_dashboard.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
from pathlib import Path
import pandas as pd
import plotly.graph_objects as go
from dash import Dash, dcc, html
from dash.dependencies import Input, Output
root_dir = Path(__file__).parent
# Initialize the Dash app
app = Dash(
__name__,
requests_pathname_prefix="/dashboard/",
routes_pathname_prefix="/dashboard/"
)
# Projects data
projects_df = pd.read_csv(root_dir / "src" / "projects.csv")
session_project_graph = go.Figure(
data=[
go.Bar(
x=projects_df["secondary_ID"],
y=projects_df["session_count"],
text=projects_df["session_count"],
)
]
)
session_project_graph.update_layout(
title_text="Session count per Project",
xaxis_title="Project Name",
yaxis_title="Session Count",
height=600,
showlegend=False,
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
yaxis_gridcolor="lightgray"
)
session_project_graph.update_traces(marker_color="#663171")
subject_project_graph = go.Figure(
data=[
go.Bar(
x=projects_df["secondary_ID"],
y=projects_df["subject_count"],
text=projects_df["subject_count"],
)
]
)
subject_project_graph.update_layout(
title_text="Subject count per Project",
xaxis_title="Project Name",
yaxis_title="Subject Count",
height=600,
showlegend=False,
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
yaxis_gridcolor="lightgray"
)
subject_project_graph.update_traces(marker_color="#cf3a36")
overall_description = (
f"**Projects:** {len(projects_df)} - "
f"**Subjects:** {projects_df['subject_count'].sum():,} - "
f"**Subjects:** {projects_df['session_count'].sum():,}"
)
dropdown_options = [
{"label": project["secondary_ID"], "value": project["ID"]}
for _, project in projects_df.iterrows()
]
@app.callback(
Output("session-by-year-graph", "figure"), [Input("project-dropdown", "value")]
)
def session_by_year_graph(selected_project):
# Read the CSV data for the selected project
df = pd.read_csv(root_dir / "src" / "projects" / f"{selected_project}.csv")
# Convert date to datetime
df["date"] = pd.to_datetime(df["date"], errors="coerce")
# Group by year and count sessions
sessions_by_year = df.groupby(df["date"].dt.year).size().reset_index(name="count")
# Create a new bar chart for sessions by year
fig = go.Figure(
data=[
go.Bar(
x=sessions_by_year["date"], y=sessions_by_year["count"], name="Sessions"
)
],
)
# Update layout
fig.update_layout(
title_text="Sessions by Year",
height=400,
showlegend=False,
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
yaxis_gridcolor="lightgray",
yaxis_tickformat=",d",
)
fig.update_xaxes(type='category')
fig.update_traces(marker_color="#ea7428")
fig.update_xaxes(title_text="Year")
fig.update_yaxes(title_text="Number of Sessions")
if sessions_by_year["count"].max()<=10:
fig.update_layout(
yaxis_dtick=1,
)
return fig
@app.callback(Output("modality-graph", "figure"), [Input("project-dropdown", "value")])
def modality_graph(selected_project):
# Read the CSV data for the selected project
df = pd.read_csv(root_dir / "src" / "projects" / f"{selected_project}.csv")
# Group by modalities and count sessions
modalities = df.groupby(df["modality"]).size().reset_index(name="count")
# Create a new bar chart for modalities
fig = go.Figure(
data=[
go.Bar(x=modalities["modality"], y=modalities["count"], name="Modalities")
],
)
# Update layout
fig.update_layout(
title_text="Modalities",
height=400,
showlegend=False,
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
yaxis_gridcolor="lightgray",
yaxis_tickformat=",d",
)
fig.update_traces(marker_color="#e2998a")
fig.update_xaxes(title_text="Modalities")
fig.update_yaxes(title_text="Number of Sessions")
if modalities["count"].max()<=10:
fig.update_layout(
yaxis_dtick=1,
)
return fig
@app.callback(
Output("project-description", "children"), [Input("project-dropdown", "value")]
)
def project_description(selected_project):
# Read the CSV data for the selected project
row = projects_df.loc[projects_df["ID"] == selected_project].copy().reset_index()
description = (
f"**Project ID:** {selected_project}\n\n"
f"**Name:** {row.name[0]}\n\n"
f"**Description:** {row.description[0] if isinstance(row.description[0], str) else 'No description available'}\n\n"
f"**Session Count:** {row.session_count[0]:,}\n\n"
f"**Subject Count:** {row.subject_count[0]:,}\n\n"
)
return description
# Bar chart for sessions by year
fig_session_by_year = session_by_year_graph(dropdown_options[0]["value"])
# Bar chart for modalities
fig_modalities = modality_graph(dropdown_options[0]["value"])
# Layout of the app
app.layout = html.Div(
children=[
html.Div(
children=[
#html.H1(children="BIMCV-XNAT Dashboard", style={"fontSize": "32px", "fontWeight": "bold"}),
html.H2(children="Overview", style={"fontSize": "26px", "fontWeight": "bold"}),
dcc.Markdown(
id="overall-description",
children=overall_description,
style={
"width": "90%",
"padding": "1px",
"fontSize": "24px",
"text-align": "center",
"backgroundColor": "#f0f0f0",
}
),
html.Div(
children=[
dcc.Graph(
id="session-project-graph",
figure=session_project_graph,
style={"width": "50%"}
),
dcc.Graph(
id="subject-project-graph",
figure=subject_project_graph,
style={"width": "50%"}
),
],
style={
"display": "flex",
},
),
html.H2(children="Project Details", style={"fontSize": "26px", "fontWeight": "bold"}),
html.Div(
children=[
html.Div(
children=[
html.Div(
children=[
html.H3(children="Choose a project:", style={"padding-right": "20px", "verticalAlign": "top"}),
dcc.Dropdown(
id="project-dropdown",
options=dropdown_options,
value=dropdown_options[0]["value"],
clearable=False,
style={"width": "80%"}
),
],
style={
"display": "flex",
},
),
dcc.Markdown(
id="project-description",
children="",
style={
"width": "90%",
"padding": "10px",
"verticalAlign": "middle",
"backgroundColor": "#f0f0f0",
}
),
],
),
html.Div(
children=[
dcc.Graph(
id="session-by-year-graph",
figure=fig_session_by_year,
style={"width": "50%"}
),
dcc.Graph(
id="modality-graph",
figure=fig_modalities,
style={"width": "50%"}
),
],
style={
"display": "flex",
},
),
],
),
],
),
],
style={
"width": "100%",
"padding": "20px",
"font-family": "Arial",
"font-size": "14px",
"color": "#333"
},
)
# Run the app
if __name__ == "__main__":
app.run_server(debug=False, host="0.0.0.0", port="12222")