-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive_charts.py
196 lines (169 loc) · 6.83 KB
/
interactive_charts.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
"""
Interactive Accelerator Timeline
********************************
This script allows you to interactively explore the accelerator data,
either by running the script and viewing the plots in a browser,
by running the script in interactive-mode e.g. in vscode
or by checking the from this script generated gallery.
To run the script, make sure your environment has the requirements
of `requirements_interactive_charts.txt` installed.
"""
#%%
# Preparations
# ------------
#
# Import modules and define plotting function.
# This code is omitted in the interactive gallery, so that you can immediately enjoy the interactive plots below.
# Check `interactive.py <https://github.com/pylhc/accelerator_timeline/blob/master/interactive_charts.py>`_
# for the full example code.
#
# No code to see here in the interactive gallery or the generated jupyter notebook.
# sphinx_gallery_start_ignore
from pathlib import Path
import numpy as np
import pandas as pd
import plotly
import plotly.graph_objects as go
from IPython.display import HTML, display
from utilities.csv_reader import Column, import_collider_data
from utilities.plot_helper import (PARTICLE_TYPES, EnergyConfiguration, LuminosityConfiguration,
LuminosityOverEnergyConfiguration, PlotConfiguration,
assign_textposition, check_all_types_accounted_for)
from utilities.sphinx_helper import get_gallery_dir, is_interactive, is_sphinx_build
# Hack for rendering LaTeX in VSCode
# (see https://github.com/microsoft/vscode-jupyter/issues/8131#issuecomment-1589961116)
if not is_sphinx_build() and is_interactive():
display(HTML(
'<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_SVG"></script>'
))
# Import Data ---
data = import_collider_data()
data = assign_textposition(data)
check_all_types_accounted_for(data)
# Plotting Function ---
# This is the definition of the actual plotting function,
# which creates the interactive plotly plots
def plot(data: pd.DataFrame, configuration: PlotConfiguration) -> go.Figure:
"""Generate interactive plots with plotly, based on the given configuration,
which defines the columns to use and the text positions.
Args:
data (pd.DataFrame): DataFrame containing the (modified) accelerator timeline data
configuration (PlotConfiguration): See :class:`utilities.plot_helper.PlotConfiguration`
Returns:
go.Figure: plotly figure
"""
fig = go.Figure()
for particle_type in PARTICLE_TYPES:
particle_mask = data[Column.TYPE] == particle_type.shorthand
for has_been_built in (True, False):
if has_been_built:
builtmask, marker_suffix, legend = data[Column.BUILT], "", "built"
else:
builtmask, marker_suffix, legend = ~data[Column.BUILT], "-open", "not built"
mask = particle_mask & builtmask
fig.add_trace(go.Scatter(
x=data.loc[mask & builtmask, configuration.xcolumn],
y=data.loc[mask & builtmask, configuration.ycolumn],
name=legend,
legendgroup=particle_type.name,
legendgrouptitle_text=particle_type.latex,
text=data.loc[mask, Column.NAME],
textposition=data.loc[mask, configuration.textposition],
mode="markers+text",
marker={"symbol": f"{particle_type.symbol}{marker_suffix}",
"color": particle_type.color},
customdata=np.transpose([
data.loc[mask, Column.NAME],
[particle_type.name] * sum(mask),
data.loc[mask, Column.COM_ENERGY],
data.loc[mask, Column.LUMINOSITY],
data.loc[mask, Column.LENGTH],
data.loc[mask, Column.YEARS],
data.loc[mask, Column.INSTITUTE],
data.loc[mask, Column.COUNTRY],
])
))
fig.update_traces(
hovertemplate="<br>".join([
"%{customdata[0]} (%{customdata[6]}, %{customdata[7]})",
"Particles: %{customdata[1]}",
"Center-of-Mass Energy [GeV]: %{customdata[2]}",
"Luminosity [cm^-2s^-1]: %{customdata[3]}", # sadly plotly does not support latex in hover
"Length [m]: %{customdata[4]}",
"Operation: %{customdata[5]}",
]) + "<extra></extra>"
)
logx, logy = "x" in configuration.logscale, "y" in configuration.logscale
fig.update_xaxes(
title=configuration.xlabel,
type="log" if logx else "linear",
dtick=1 if logx else 10,
minor=dict(dtick="D1" if logx else 1, ticks="outside"),
ticks='outside',
showline=True,
linecolor='black',
gridcolor='lightgrey'
)
fig.update_yaxes(
title=configuration.ylabel,
type="log" if "y" in configuration.logscale else "linear",
ticks='outside',
dtick=1 if logy else 10,
minor=dict(dtick="D1" if logy else None, ticks="outside", showgrid=False),
showline=True,
linecolor='black',
gridcolor='lightgrey',
# tickformat='e',
)
fig.update_layout(
plot_bgcolor='white',
)
return fig
# sphinx_gallery_end_ignore
#%%
# Energy Timeline
# ---------------
#
fig_com = plot(data, EnergyConfiguration)
# sphinx_gallery_start_ignore
if not is_sphinx_build() and not is_interactive():
fig_com.show()
fig_com
# sphinx_gallery_end_ignore
#%%
# Luminosity timeline
# -------------------
#
fig_lumi = plot(data, LuminosityConfiguration)
# sphinx_gallery_start_ignore
if not is_sphinx_build() and not is_interactive():
fig_lumi.show()
fig_lumi
# sphinx_gallery_end_ignore
#%%
# Luminosity vs. Energy
# ---------------------
#
fig_lumi_energy = plot(data, LuminosityOverEnergyConfiguration)
# sphinx_gallery_start_ignore
if not is_sphinx_build() and not is_interactive():
fig_lumi_energy.show()
fig_lumi_energy
# sphinx_gallery_end_ignore
#%%
# Save plots
# ----------
#
# Save the plots as PDF and PNG.
output_dir = Path("images")
# sphinx_gallery_start_ignore
if is_sphinx_build():
output_dir = get_gallery_dir()
# sphinx_gallery_end_ignore
plotly.io.write_image(fig_com, output_dir / "energy-plotly.pdf", format="pdf")
plotly.io.write_image(fig_com, output_dir / "energy-plotly.png", format="png")
plotly.io.write_image(fig_lumi, output_dir / "luminosity-plotly.pdf", format="pdf")
plotly.io.write_image(fig_lumi, output_dir / "luminosity-plotly.png", format="png")
plotly.io.write_image(fig_lumi_energy, output_dir / "luminosity-vs-energy-plotly.pdf", format="pdf")
plotly.io.write_image(fig_lumi_energy, output_dir / "luminosity-vs-energy-plotly.png", format="png")
# sphinx_gallery_thumbnail_path = 'gallery/luminosity-vs-energy-plotly.png'