-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
524 lines (474 loc) · 18.3 KB
/
app.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
from json import dumps, load
import dash
from dash.dependencies import Input, Output
from dash import html
from dash import dcc
# todo update imports dash_table
from dash import dash_table
from pandas import read_hdf
df = read_hdf("all_indexes.h5", "/data/d1")
downloaded_xs_data = {}
app = dash.Dash(
__name__,
prevent_initial_callbacks=True,
meta_tags=[
# A description of the app, used by e.g.
# search engines when displaying search results.
{"name": "title", "content": "XSPlot neutron cross section plotter"},
{
"name": "description",
"content": "Online graph plotting tool for neutron cross sections from a range of nuclear data including TENDL ENDF",
},
{
"name": "keywords",
"keywords": "plot neutron nuclear cross section energy barns database plotter tendl endf",
},
{"name": "author", "content": "Jonathan Shimwell"},
# A tag that tells Internet Explorer (IE)
# to use the latest renderer version available
# to that browser (e.g. Edge)
{"http-equiv": "X-UA-Compatible", "content": "IE=edge"},
# A tag that tells the browser not to scale
# desktop widths to fit mobile screens.
# Sets the width of the viewport (browser)
# to the width of the device, and the zoom level
# (initial scale) to 1.
#
# Necessary for "true" mobile support.
{"name": "viewport", "content": "width=device-width, initial-scale=1.0"},
],
)
app.title = "XSPlot \U0001f4c9 neutron cross section plotter \U0001f4c8"
app.description = "Plot neutron cross sections. Nuclear data from the TENDL library."
# TODO add description, current google says Dash in description area
# https://github.com/plotly/dash/blob/1a40162dfce654b885e475ecb280d3cca9bff0a5/dash/dash.py#L193
# added to allow Gunicorn access to Dash Flask as discussed here
# https://ldnicolasmay.medium.com/deploying-a-free-dash-open-source-app-from-a-docker-container-with-gunicorn-3f426b5fd5df
server = app.server
components = [
# guide on plotly html https://dash.plotly.com/dash-html-components
html.Title("xsplot.com isotope cross section plotting"),
html.Iframe(
src="https://ghbtns.com/github-btn.html?user=openmc-data-storage&repo=isotope-xs-plotter&type=star&count=true&size=large",
width="170",
height="30",
title="GitHub",
style={"border": 0, "scrolling": "0"},
),
html.H1(
"XSPlot - Neutron cross section plotter for isotopes",
# TODO find a nicer font
# style={'font-family': 'Times New Roman, Times, serif'},
# style={'font-family': 'Georgia, serif'},
style={"text-align": "center"},
),
html.Div(
html.Iframe(
src="https://www.youtube.com/embed/aWXS9AqSkEk",
width="560",
height="315",
title="Tutorial video",
# style={},
style={"text-align": "center", "border": 0, "scrolling": "0"},
),
style={"text-align": "center"},
),
html.Div(
[
html.H3(
[
"\U0001f50e Search the cross sections database using any of the table headings. \U0001f50d",
],
style={'text-align': 'center'}
),
html.H3(
[
"Make use of logical expressions to refine the database filtering \U0001f449 = < > "
# "Make use of \U0001f449 ", A("MT reaction number", href="https://t2.lanl.gov/nis/endf/mts.html"),
# " or other table headings and then select neutron cross sections to plot. ",
# "Use logical expressions = < > to perform advanced filtering."
],
style={'text-align': 'center'}
),
html.H3(
[
"\U0000269b Make use of standard MT numbers to identify reactions \U0001f449 ",
html.A("reaction descriptions \U0001f517",href="https://t2.lanl.gov/nis/endf/mts.html") ,
# " or other table headings and then select neutron cross sections to plot. ",
# "Use logical expressions = < > to perform advanced filtering."
],
style={'text-align': 'center'}
),
html.H3(
[
'\U0001f4c8 The plot should update automatically \U0001f389'
],
style={'text-align': 'center'}
),
html.H3(
[
'\U0001f4c9 Customise you graph and download your cross section data \U0001f4be'
],
style={'text-align': 'center'}
),
]
),
dash_table.DataTable(
id="datatable-interactivity",
columns=[
{"name": i, "id": i, "selectable": True}
for i in df.columns
if i not in ["Temperature(K)", "Incident particle"]
],
data=df.to_dict("records"),
editable=False,
filter_action="native", # TODO change to equals instead of contains
sort_action="native",
sort_mode="multi",
row_selectable="multi",
row_deletable=False,
selected_columns=[],
selected_rows=[],
page_action="native",
page_current=0,
page_size=15,
style_cell={'fontSize':16, 'font-family':'sans-serif'},
),
html.Table(
[
html.Tr(
[
html.Th(
html.Button(
"clear selection",
title="Clear all selected data. You can also temporarily hide plots by clicked them in the legend",
id="clear",
)
),
html.Th(
dcc.RadioItems(
options=[
{"label": "log X axis", "value": "log"},
{"label": "linear X axis", "value": "linear"},
],
value="log",
id="xaxis_scale",
labelStyle={"display": "inline-block"},
),
),
html.Th(
# html.H5("X axis range (comma delimitated)"),
html.Div(title='Enter both X lower and X upper limit to use, works best on linear X scale.', children=[
dcc.Input(
id="x_lower_limit",
type='text',
placeholder='X axis lower limit in eV'
),
]),
),
html.Th(
html.Div(title='Enter both X lower and X upper limit to use, works best on linear X scale.', children=[
dcc.Input(
id="x_upper_limit",
type='text',
placeholder='X axis upper limit in eV'
),
]),
),
]
),
html.Tr([html.Br()]),
html.Tr(
[
html.Th(
html.Button(
"Download Plotted Data",
title="Download a text file of the data in JSON format",
id="btn_download2",
)
),
html.Th(
dcc.RadioItems(
options=[
{"label": "log Y axis", "value": "log"},
{"label": "linear Y axis", "value": "linear"},
],
value="log",
id="yaxis_scale",
),
),
# TODO add slider from SO
# https://stackoverflow.com/questions/61896144/dash-range-slider-with-input-on-each-side
html.Th(
html.Div(title='Enter both Y lower and Y upper limit to use, works best on linear Y scale.', children=[
dcc.Input(
id="y_lower_limit",
type='text',
placeholder='Y axis lower limit in Barns',
),
]),
),
html.Th(
html.Div(title='Enter both Y lower and Y upper limit to use, works best on linear Y scale.', children=[
dcc.Input(
id="y_upper_limit",
type='text',
placeholder='Y axis upper limit in Barns'
),
]),
),
]
),
],
style={"width": "100%"},
),
html.Br(),
dcc.Loading(
id="loading-1",
type="default",
children=html.Div(id="graph_container")
),
html.H5("X axis units"),
dcc.Slider(
min=0,
max=4,
marks={i: f"{s}" for i, s in enumerate(["μeV", "eV", "keV", "MeV", "GeV"])},
value=1,
id="x_axis_units",
),
dcc.Download(id="download-text-index"),
html.Br(),
html.Div(
[
html.Label("XSPlot is an open-source project powered by "),
html.A("OpenMC", href="https://docs.openmc.org/en/stable/"),
html.Label(", "),
html.A(" Plotly", href="https://plotly.com/"),
html.Label(", "),
html.A(" Dash", href="https://dash.plotly.com/"),
html.Label(", "),
html.A(" Dash datatable", href="https://dash.plotly.com/datatable"),
html.Label(", "),
html.A(" Flask", href="https://flask.palletsprojects.com/en/2.0.x/"),
html.Label(", "),
html.A(" Gunicorn", href="https://gunicorn.org/"),
html.Label(", "),
html.A(" Docker", href="https://www.docker.com"),
html.Label(", "),
html.A(" GCloud", href="https://cloud.google.com"),
html.Label(", "),
html.A(" Python", href="https://www.python.org/"),
html.Label(" with the source code available on "),
html.A(" GitHub", href="https://github.com/openmc-data-storage"),
],
style={"text-align": "center"},
),
html.Br(),
html.Div(
[
html.Label("Links to alternative cross section plotting websites: "),
html.A("NEA JANIS", href="https://www.oecd-nea.org/jcms/pl_39910/janis"),
html.Label(", "),
html.A(" IAEA ENDF", href="https://www-nds.iaea.org/exfor/endf.htm"),
html.Label(", "),
html.A(" IAEA Libraries", href="https://nds.iaea.org/dataexplorer"),
html.Label(", "),
html.A(" NNDC Sigma", href="https://www.nndc.bnl.gov/sigma/"),
html.Label(", "),
html.A(
" Nuclear Data Center JAEA",
href="https://wwwndc.jaea.go.jp/ENDF_Graph/",
),
html.Label(", "),
html.A("T2 LANL", href="https://t2.lanl.gov/nis/data/endf/index.html"),
html.Label(", "),
html.A("Nuclear Data Center KAERI", href="https://atom.kaeri.re.kr"),
],
style={"text-align": "center"},
),
]
app.layout = html.Div(components)
@app.callback(
Output("datatable-interactivity", "selected_rows"),
Input("clear", "n_clicks"),
)
def clear(n_clicks):
return []
@app.callback(
Output("datatable-interactivity", "style_data_conditional"),
[Input("datatable-interactivity", "selected_columns")],
)
def update_styles(selected_columns):
return [
{"if": {"column_id": i}, "background_color": "#D2F3FF"}
for i in selected_columns
]
def get_uuid_from_row(row):
atomic_symbol = row["Atomic symbol"].to_string(index=False)
mass_number = row["Mass number"].to_string(index=False)
library = row["Library"].to_string(index=False)
incident_particle_symbol = "n"
reaction = row["MT reaction number"].to_string(index=False)
if library == "TENDL-2019":
temperature = "294K"
else:
temperature = "300K" # FENDL 3.1d
uuid = "_".join(
[
atomic_symbol,
mass_number,
library,
incident_particle_symbol,
str(int(reaction)),
str(temperature),
]
)
return uuid
@app.callback(
Output("graph_container", "children"),
[
Input("datatable-interactivity", "selected_rows"),
Input("xaxis_scale", "value"),
Input("yaxis_scale", "value"),
Input("x_axis_units", "value"),
Input("x_lower_limit", "value"),
Input("x_upper_limit", "value"),
Input("y_lower_limit", "value"),
Input("y_upper_limit", "value"),
]
)
def update_graphs(
selected_rows,
xaxis_scale,
yaxis_scale,
x_axis_units,
x_lower_limit,
x_upper_limit,
y_lower_limit,
y_upper_limit,
):
# When the table is first rendered, `derived_virtual_data` and
# `selected_rows` will be `None`. This is due to an
# idiosyncracy in Dash (unsupplied properties are always None and Dash
# calls the dependent callbacks when the component is first rendered).
# So, if `rows` is `None`, then the component was just rendered
# and its value will be the same as the component's dataframe.
# Instead of setting `None` in here, you could also set
# `derived_virtual_data=df.to_rows('dict')` when you initialize
# the component.
if selected_rows is None:
selected_rows = []
global downloaded_xs_data
if len(downloaded_xs_data) > 0:
for entry in selected_rows:
if entry in downloaded_xs_data.keys():
downloaded_xs_data[entry]["plot"] = False
for entry in selected_rows:
row = df.iloc[[entry]]
uuid = get_uuid_from_row(row)
library = row["Library"].to_string().split()[1]
fn = library + "_json/" + uuid + ".json"
with open(fn) as json_file:
xs = load(json_file)
xs["plot"] = True
xs["legend"] = "{}{} (n,{}) {}".format(
xs["Atomic symbol"],
xs["Mass number"],
xs["Reaction products"],
xs["Library"],
)
downloaded_xs_data[entry] = xs
all_x_y_data = []
x_axis_units_multiplier = {0: -3, 1: 0, 2: 3, 3: 6, 4: 9}
for k, v in downloaded_xs_data.items():
import math
if k in selected_rows:
multiplier = math.pow(10, -1 * x_axis_units_multiplier[x_axis_units])
energy = [x * multiplier for x in downloaded_xs_data[k]["energy"]]
all_x_y_data.append(
{
"y": downloaded_xs_data[k]["cross section"],
"x": energy,
"type": "scatter",
"name": downloaded_xs_data[k]["legend"]
# "marker": {"color": colors},
}
)
# previous website had more complex unit logic
# https://github.com/Shimwell/database_GUI/blob/d670ca88feef8f41a0f20abd30bdb2a82cbab6bd/src/App.js#L305-L329
x_axis_units_text = {0: "μeV", 1: "eV", 2: "keV", 3: "MeV", 4: "GeV"}
energy_units = f"[{x_axis_units_text[x_axis_units]}]"
xs_units = "[barns]"
if len(selected_rows) != 0:
fig={
"data": all_x_y_data,
"layout": {
"height":800,
# "width":1600,
"margin": {"l": 3, "r": 2, "t": 15, "b": 60},
"xaxis": {
"title": {"text": f"Energy {energy_units}"},
"type": xaxis_scale,
"tickformat": ".1e",
"tickangle": 45,
"rangemode": 'nonnegative'
},
"yaxis": {
"automargin": True,
"title": {"text": f"Microscopic Cross Section {xs_units}"},
"type": yaxis_scale,
"tickformat": ".1e",
},
"showlegend": True,
# "height": 250,
# "margin": {"t": 10, "l": 10, "r": 10},
},
}
if x_upper_limit is not None and x_lower_limit is not None:
try:
float_values = (float(x_lower_limit), float(x_upper_limit))
fig["layout"]["xaxis"]["range"]=float_values
except:
fig["layout"]["xaxis"]["range"]=None
else:
fig["layout"]["xaxis"]["range"]=None
if y_upper_limit is not None and y_lower_limit is not None:
try:
float_values = (float(y_lower_limit), float(y_upper_limit))
fig["layout"]["yaxis"]["range"]=float_values
except:
fig["layout"]["yaxis"]["range"]=None
else:
fig["layout"]["yaxis"]["range"]=None
return [
dcc.Graph(
# config=dict(showSendToCloud=True),
figure=fig
)
]
# uses a trigger to identify the callback and if the button is used then jsonifys the selected data
@app.callback(
Output("download-text-index", "data"),
[
Input("btn_download2", "n_clicks"),
Input("datatable-interactivity", "selected_rows"),
],
)
def func2(n_clicks, selected_rows):
trigger_id = dash.callback_context.triggered[0]["prop_id"].split(".")[0]
global downloaded_xs_data
if trigger_id == "btn_download2":
if n_clicks is None:
raise dash.exceptions.PreventUpdate
else:
if len(downloaded_xs_data) > 0:
all_x_y_data = []
for k, v in downloaded_xs_data.items():
if k in selected_rows:
all_x_y_data.append(downloaded_xs_data[k])
return dict(
content=dumps(all_x_y_data, indent=2),
filename="xsplot_download.json",
)
if __name__ == "__main__":
app.run_server(debug=True, host="0.0.0.0", port=8080)