Spaces:
Running
Running
Commit ·
d8c59a5
1
Parent(s): 91edb6a
add cross-spectra and average cross spectra
Browse files- modules/QuickLook/AverageCrossSpectrum.py +312 -0
- modules/QuickLook/CrossSpectrum.py +297 -0
- utils/sidebar.py +65 -2
modules/QuickLook/AverageCrossSpectrum.py
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
| 1 |
+
import panel as pn
|
| 2 |
+
import holoviews as hv
|
| 3 |
+
from utils.globals import loaded_event_data
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
import warnings
|
| 7 |
+
import hvplot.pandas
|
| 8 |
+
from utils.DashboardClasses import (
|
| 9 |
+
MainHeader,
|
| 10 |
+
MainArea,
|
| 11 |
+
OutputBox,
|
| 12 |
+
WarningBox,
|
| 13 |
+
HelpBox,
|
| 14 |
+
Footer,
|
| 15 |
+
WarningHandler,
|
| 16 |
+
FloatingPlot,
|
| 17 |
+
PlotsContainer,
|
| 18 |
+
)
|
| 19 |
+
from stingray import AveragedCrossspectrum
|
| 20 |
+
|
| 21 |
+
# Create a warning handler
|
| 22 |
+
def create_warning_handler():
|
| 23 |
+
warning_handler = WarningHandler()
|
| 24 |
+
warnings.showwarning = warning_handler.warn
|
| 25 |
+
return warning_handler
|
| 26 |
+
|
| 27 |
+
""" Header Section """
|
| 28 |
+
def create_quicklook_avg_cross_spectrum_header(
|
| 29 |
+
header_container,
|
| 30 |
+
main_area_container,
|
| 31 |
+
output_box_container,
|
| 32 |
+
warning_box_container,
|
| 33 |
+
plots_container,
|
| 34 |
+
help_box_container,
|
| 35 |
+
footer_container,
|
| 36 |
+
):
|
| 37 |
+
home_heading_input = pn.widgets.TextInput(
|
| 38 |
+
name="Heading", value="QuickLook Averaged Cross Spectrum"
|
| 39 |
+
)
|
| 40 |
+
home_subheading_input = pn.widgets.TextInput(name="Subheading", value="")
|
| 41 |
+
|
| 42 |
+
return MainHeader(heading=home_heading_input, subheading=home_subheading_input)
|
| 43 |
+
|
| 44 |
+
""" Output Box Section """
|
| 45 |
+
def create_loadingdata_output_box(content):
|
| 46 |
+
return OutputBox(output_content=content)
|
| 47 |
+
|
| 48 |
+
""" Warning Box Section """
|
| 49 |
+
def create_loadingdata_warning_box(content):
|
| 50 |
+
return WarningBox(warning_content=content)
|
| 51 |
+
|
| 52 |
+
""" Float Panel """
|
| 53 |
+
def create_floatpanel_area(content, title):
|
| 54 |
+
return FloatingPlot(content=content, title=title)
|
| 55 |
+
|
| 56 |
+
""" Main Area Section """
|
| 57 |
+
def create_avg_cross_spectrum_tab(
|
| 58 |
+
output_box_container,
|
| 59 |
+
warning_box_container,
|
| 60 |
+
warning_handler,
|
| 61 |
+
plots_container,
|
| 62 |
+
header_container,
|
| 63 |
+
float_panel_container,
|
| 64 |
+
):
|
| 65 |
+
# Define Widgets
|
| 66 |
+
event_list_dropdown_1 = pn.widgets.Select(
|
| 67 |
+
name="Select Event List 1",
|
| 68 |
+
options={name: i for i, (name, event) in enumerate(loaded_event_data)},
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
event_list_dropdown_2 = pn.widgets.Select(
|
| 72 |
+
name="Select Event List 2",
|
| 73 |
+
options={name: i for i, (name, event) in enumerate(loaded_event_data)},
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
dt_slider = pn.widgets.FloatSlider(
|
| 77 |
+
name="Select dt",
|
| 78 |
+
start=0.1,
|
| 79 |
+
end=1000,
|
| 80 |
+
step=0.1,
|
| 81 |
+
value=1,
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
norm_select = pn.widgets.Select(
|
| 85 |
+
name="Normalization",
|
| 86 |
+
options=["frac", "leahy", "abs", "none"],
|
| 87 |
+
value="leahy",
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
segment_size_input = pn.widgets.FloatInput(
|
| 91 |
+
name="Segment Size", value=10, step=1
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
floatpanel_plots_checkbox = pn.widgets.Checkbox(
|
| 95 |
+
name="Add Plot to FloatingPanel", value=False
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
dataframe_checkbox = pn.widgets.Checkbox(
|
| 99 |
+
name="Add DataFrame to FloatingPanel", value=False
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
def create_dataframe(selected_event_list_index_1, selected_event_list_index_2, dt, norm, segment_size):
|
| 103 |
+
if selected_event_list_index_1 is not None and selected_event_list_index_2 is not None:
|
| 104 |
+
event_list_1 = loaded_event_data[selected_event_list_index_1][1]
|
| 105 |
+
event_list_2 = loaded_event_data[selected_event_list_index_2][1]
|
| 106 |
+
|
| 107 |
+
# Create an AveragedCrossspectrum object using from_lightcurve
|
| 108 |
+
try:
|
| 109 |
+
lc1 = event_list_1.to_lc(dt=dt)
|
| 110 |
+
lc2 = event_list_2.to_lc(dt=dt)
|
| 111 |
+
cs = AveragedCrossspectrum.from_lightcurve(lc1, lc2, segment_size, norm=norm)
|
| 112 |
+
|
| 113 |
+
df = pd.DataFrame(
|
| 114 |
+
{
|
| 115 |
+
"Frequency": cs.freq,
|
| 116 |
+
"Cross Power": np.abs(cs.power),
|
| 117 |
+
}
|
| 118 |
+
)
|
| 119 |
+
return df, cs
|
| 120 |
+
|
| 121 |
+
except AssertionError as ae:
|
| 122 |
+
if "No GTIs are equal to or longer than segment_size" in str(ae):
|
| 123 |
+
output_box_container[:] = [
|
| 124 |
+
create_loadingdata_output_box(
|
| 125 |
+
f"Error: No GTIs are long enough to accommodate the segment size {segment_size}s. "
|
| 126 |
+
"Please reduce the segment size or check your GTIs."
|
| 127 |
+
)
|
| 128 |
+
]
|
| 129 |
+
else:
|
| 130 |
+
output_box_container[:] = [
|
| 131 |
+
create_loadingdata_output_box(
|
| 132 |
+
f"Error generating Averaged Cross Spectrum: {ae}. "
|
| 133 |
+
)
|
| 134 |
+
]
|
| 135 |
+
return None, None
|
| 136 |
+
|
| 137 |
+
except Exception as e:
|
| 138 |
+
if "requested segment size" in str(e):
|
| 139 |
+
output_box_container[:] = [
|
| 140 |
+
create_loadingdata_output_box(
|
| 141 |
+
f"Failed to create cross spectrum: dt is too large or the segment size is too small."
|
| 142 |
+
)
|
| 143 |
+
]
|
| 144 |
+
else:
|
| 145 |
+
output_box_container[:] = [
|
| 146 |
+
create_loadingdata_output_box(
|
| 147 |
+
f"Error generating Averaged Cross Spectrum: {e}. "
|
| 148 |
+
"Try using a different segment size."
|
| 149 |
+
)]
|
| 150 |
+
return None, None
|
| 151 |
+
return None, None
|
| 152 |
+
|
| 153 |
+
def create_holoviews_panes(plot):
|
| 154 |
+
return pn.pane.HoloViews(plot, width=600, height=600)
|
| 155 |
+
|
| 156 |
+
def create_holoviews_plots(cs, title, dt, norm, segment_size):
|
| 157 |
+
label = f"{title} (dt={dt}, norm={norm}, segment={segment_size})"
|
| 158 |
+
return hv.Curve((cs.freq, np.abs(cs.power)), label=label).opts(
|
| 159 |
+
xlabel="Frequency (Hz)",
|
| 160 |
+
ylabel="Cross Spectral Amplitude",
|
| 161 |
+
title=f"{title} (dt={dt}, norm={norm}, segment={segment_size})",
|
| 162 |
+
width=600,
|
| 163 |
+
height=600,
|
| 164 |
+
shared_axes=False,
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
def create_dataframe_panes(df, title, dt, norm, segment_size):
|
| 168 |
+
return pn.FlexBox(
|
| 169 |
+
pn.pane.Markdown(f"**{title} (dt={dt}, norm={norm}, segment={segment_size})**"),
|
| 170 |
+
pn.pane.DataFrame(df, width=600, height=600),
|
| 171 |
+
align_items="center",
|
| 172 |
+
justify_content="center",
|
| 173 |
+
flex_wrap="nowrap",
|
| 174 |
+
flex_direction="column",
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
def generate_avg_cross_spectrum(event=None):
|
| 178 |
+
if not loaded_event_data:
|
| 179 |
+
output_box_container[:] = [
|
| 180 |
+
create_loadingdata_output_box("No loaded event data available.")
|
| 181 |
+
]
|
| 182 |
+
return
|
| 183 |
+
|
| 184 |
+
selected_event_list_index_1 = event_list_dropdown_1.value
|
| 185 |
+
selected_event_list_index_2 = event_list_dropdown_2.value
|
| 186 |
+
if selected_event_list_index_1 is None or selected_event_list_index_2 is None:
|
| 187 |
+
output_box_container[:] = [
|
| 188 |
+
create_loadingdata_output_box("Both event lists must be selected.")
|
| 189 |
+
]
|
| 190 |
+
return
|
| 191 |
+
|
| 192 |
+
dt = dt_slider.value
|
| 193 |
+
norm = norm_select.value
|
| 194 |
+
segment_size = segment_size_input.value
|
| 195 |
+
df, cs = create_dataframe(selected_event_list_index_1, selected_event_list_index_2, dt, norm, segment_size)
|
| 196 |
+
if df is not None:
|
| 197 |
+
plot_title = f"Averaged Cross Spectrum for {loaded_event_data[selected_event_list_index_1][0]} vs {loaded_event_data[selected_event_list_index_2][0]}"
|
| 198 |
+
plot_hv = create_holoviews_plots(cs, title=plot_title, dt=dt, norm=norm, segment_size=segment_size)
|
| 199 |
+
holoviews_output = create_holoviews_panes(plot_hv)
|
| 200 |
+
|
| 201 |
+
if floatpanel_plots_checkbox.value:
|
| 202 |
+
float_panel_container.append(
|
| 203 |
+
create_floatpanel_area(
|
| 204 |
+
content=holoviews_output, title=plot_title
|
| 205 |
+
)
|
| 206 |
+
)
|
| 207 |
+
else:
|
| 208 |
+
markdown_content = f"## {plot_title}"
|
| 209 |
+
plots_container.append(
|
| 210 |
+
pn.FlexBox(
|
| 211 |
+
pn.pane.Markdown(markdown_content),
|
| 212 |
+
holoviews_output,
|
| 213 |
+
align_items="center",
|
| 214 |
+
justify_content="center",
|
| 215 |
+
flex_wrap="nowrap",
|
| 216 |
+
flex_direction="column",
|
| 217 |
+
)
|
| 218 |
+
)
|
| 219 |
+
output_box_container[:] = [
|
| 220 |
+
create_loadingdata_output_box("Averaged Cross Spectrum generated successfully.")
|
| 221 |
+
]
|
| 222 |
+
else:
|
| 223 |
+
output_box_container[:] = [
|
| 224 |
+
create_loadingdata_output_box("Failed to create averaged cross spectrum.")
|
| 225 |
+
]
|
| 226 |
+
|
| 227 |
+
def show_dataframe(event=None):
|
| 228 |
+
if not loaded_event_data:
|
| 229 |
+
output_box_container[:] = [
|
| 230 |
+
create_loadingdata_output_box("No loaded event data available.")
|
| 231 |
+
]
|
| 232 |
+
return
|
| 233 |
+
|
| 234 |
+
selected_event_list_index_1 = event_list_dropdown_1.value
|
| 235 |
+
selected_event_list_index_2 = event_list_dropdown_2.value
|
| 236 |
+
if selected_event_list_index_1 is None or selected_event_list_index_2 is None:
|
| 237 |
+
output_box_container[:] = [
|
| 238 |
+
create_loadingdata_output_box("Both event lists must be selected.")
|
| 239 |
+
]
|
| 240 |
+
return
|
| 241 |
+
|
| 242 |
+
dt = dt_slider.value
|
| 243 |
+
norm = norm_select.value
|
| 244 |
+
segment_size = segment_size_input.value
|
| 245 |
+
df, cs = create_dataframe(selected_event_list_index_1, selected_event_list_index_2, dt, norm, segment_size)
|
| 246 |
+
if df is not None:
|
| 247 |
+
dataframe_output = create_dataframe_panes(df, f"{loaded_event_data[selected_event_list_index_1][0]} vs {loaded_event_data[selected_event_list_index_2][0]}", dt, norm, segment_size)
|
| 248 |
+
if dataframe_checkbox.value:
|
| 249 |
+
float_panel_container.append(
|
| 250 |
+
create_floatpanel_area(
|
| 251 |
+
content=dataframe_output,
|
| 252 |
+
title=f"DataFrame for {loaded_event_data[selected_event_list_index_1][0]} vs {loaded_event_data[selected_event_list_index_2][0]}",
|
| 253 |
+
)
|
| 254 |
+
)
|
| 255 |
+
else:
|
| 256 |
+
plots_container.append(dataframe_output)
|
| 257 |
+
output_box_container[:] = [
|
| 258 |
+
create_loadingdata_output_box("DataFrame generated successfully.")
|
| 259 |
+
]
|
| 260 |
+
else:
|
| 261 |
+
output_box_container[:] = [
|
| 262 |
+
create_loadingdata_output_box("Failed to create dataframe.")
|
| 263 |
+
]
|
| 264 |
+
|
| 265 |
+
generate_cross_spectrum_button = pn.widgets.Button(
|
| 266 |
+
name="Generate Averaged Cross Spectrum", button_type="primary"
|
| 267 |
+
)
|
| 268 |
+
generate_cross_spectrum_button.on_click(generate_avg_cross_spectrum)
|
| 269 |
+
|
| 270 |
+
show_dataframe_button = pn.widgets.Button(
|
| 271 |
+
name="Show DataFrame", button_type="primary"
|
| 272 |
+
)
|
| 273 |
+
show_dataframe_button.on_click(show_dataframe)
|
| 274 |
+
|
| 275 |
+
tab_content = pn.Column(
|
| 276 |
+
event_list_dropdown_1,
|
| 277 |
+
event_list_dropdown_2,
|
| 278 |
+
dt_slider,
|
| 279 |
+
norm_select,
|
| 280 |
+
segment_size_input,
|
| 281 |
+
floatpanel_plots_checkbox,
|
| 282 |
+
dataframe_checkbox,
|
| 283 |
+
pn.Row(generate_cross_spectrum_button, show_dataframe_button),
|
| 284 |
+
)
|
| 285 |
+
return tab_content
|
| 286 |
+
|
| 287 |
+
def create_quicklook_avg_cross_spectrum_main_area(
|
| 288 |
+
header_container,
|
| 289 |
+
main_area_container,
|
| 290 |
+
output_box_container,
|
| 291 |
+
warning_box_container,
|
| 292 |
+
plots_container,
|
| 293 |
+
help_box_container,
|
| 294 |
+
footer_container,
|
| 295 |
+
float_panel_container,
|
| 296 |
+
):
|
| 297 |
+
warning_handler = create_warning_handler()
|
| 298 |
+
tabs_content = {
|
| 299 |
+
"Averaged Cross Spectrum": create_avg_cross_spectrum_tab(
|
| 300 |
+
output_box_container=output_box_container,
|
| 301 |
+
warning_box_container=warning_box_container,
|
| 302 |
+
warning_handler=warning_handler,
|
| 303 |
+
plots_container=plots_container,
|
| 304 |
+
header_container=header_container,
|
| 305 |
+
float_panel_container=float_panel_container,
|
| 306 |
+
),
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
return MainArea(tabs_content=tabs_content)
|
| 310 |
+
|
| 311 |
+
def create_quicklook_avg_cross_spectrum_area():
|
| 312 |
+
return PlotsContainer()
|
modules/QuickLook/CrossSpectrum.py
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import panel as pn
|
| 2 |
+
import holoviews as hv
|
| 3 |
+
from utils.globals import loaded_event_data
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import warnings
|
| 6 |
+
import hvplot.pandas
|
| 7 |
+
import numpy as np
|
| 8 |
+
from utils.DashboardClasses import (
|
| 9 |
+
MainHeader,
|
| 10 |
+
MainArea,
|
| 11 |
+
OutputBox,
|
| 12 |
+
WarningBox,
|
| 13 |
+
HelpBox,
|
| 14 |
+
Footer,
|
| 15 |
+
WarningHandler,
|
| 16 |
+
FloatingPlot,
|
| 17 |
+
PlotsContainer,
|
| 18 |
+
)
|
| 19 |
+
from stingray import Crossspectrum
|
| 20 |
+
|
| 21 |
+
# Create a warning handler
|
| 22 |
+
def create_warning_handler():
|
| 23 |
+
warning_handler = WarningHandler()
|
| 24 |
+
warnings.showwarning = warning_handler.warn
|
| 25 |
+
return warning_handler
|
| 26 |
+
|
| 27 |
+
""" Header Section """
|
| 28 |
+
def create_quicklook_cross_spectrum_header(
|
| 29 |
+
header_container,
|
| 30 |
+
main_area_container,
|
| 31 |
+
output_box_container,
|
| 32 |
+
warning_box_container,
|
| 33 |
+
plots_container,
|
| 34 |
+
help_box_container,
|
| 35 |
+
footer_container,
|
| 36 |
+
):
|
| 37 |
+
home_heading_input = pn.widgets.TextInput(
|
| 38 |
+
name="Heading", value="QuickLook Cross Spectrum"
|
| 39 |
+
)
|
| 40 |
+
home_subheading_input = pn.widgets.TextInput(name="Subheading", value="")
|
| 41 |
+
|
| 42 |
+
return MainHeader(heading=home_heading_input, subheading=home_subheading_input)
|
| 43 |
+
|
| 44 |
+
""" Output Box Section """
|
| 45 |
+
def create_loadingdata_output_box(content):
|
| 46 |
+
return OutputBox(output_content=content)
|
| 47 |
+
|
| 48 |
+
""" Warning Box Section """
|
| 49 |
+
def create_loadingdata_warning_box(content):
|
| 50 |
+
return WarningBox(warning_content=content)
|
| 51 |
+
|
| 52 |
+
""" Float Panel """
|
| 53 |
+
def create_floatpanel_area(content, title):
|
| 54 |
+
return FloatingPlot(content=content, title=title)
|
| 55 |
+
|
| 56 |
+
""" Main Area Section """
|
| 57 |
+
def create_cross_spectrum_tab(
|
| 58 |
+
output_box_container,
|
| 59 |
+
warning_box_container,
|
| 60 |
+
warning_handler,
|
| 61 |
+
plots_container,
|
| 62 |
+
header_container,
|
| 63 |
+
float_panel_container,
|
| 64 |
+
):
|
| 65 |
+
event_list_dropdown_1 = pn.widgets.Select(
|
| 66 |
+
name="Select Event List 1",
|
| 67 |
+
options={name: i for i, (name, event) in enumerate(loaded_event_data)},
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
event_list_dropdown_2 = pn.widgets.Select(
|
| 71 |
+
name="Select Event List 2",
|
| 72 |
+
options={name: i for i, (name, event) in enumerate(loaded_event_data)},
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
dt_slider = pn.widgets.FloatSlider(
|
| 76 |
+
name="Select dt",
|
| 77 |
+
start=0.1,
|
| 78 |
+
end=1000,
|
| 79 |
+
step=0.1,
|
| 80 |
+
value=1,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
norm_select = pn.widgets.Select(
|
| 84 |
+
name="Normalization",
|
| 85 |
+
options=["frac", "leahy", "abs", "none"],
|
| 86 |
+
value="leahy",
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
floatpanel_plots_checkbox = pn.widgets.Checkbox(
|
| 90 |
+
name="Add Plot to FloatingPanel", value=False
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
dataframe_checkbox = pn.widgets.Checkbox(
|
| 94 |
+
name="Add DataFrame to FloatingPanel", value=False
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
def create_holoviews_panes(plot):
|
| 98 |
+
return pn.pane.HoloViews(plot, width=600, height=600)
|
| 99 |
+
|
| 100 |
+
def create_holoviews_plots(cs, event_list_name_1, event_list_name_2, dt, norm):
|
| 101 |
+
label = f"{event_list_name_1} vs {event_list_name_2} (dt={dt}, norm={norm})"
|
| 102 |
+
return hv.Curve((cs.freq, np.abs(cs.power)), label=label).opts(
|
| 103 |
+
xlabel="Frequency (Hz)",
|
| 104 |
+
ylabel="Cross Spectral Amplitude",
|
| 105 |
+
title=label,
|
| 106 |
+
width=600,
|
| 107 |
+
height=600,
|
| 108 |
+
shared_axes=False,
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
def create_dataframe_panes(df, title):
|
| 112 |
+
return pn.FlexBox(
|
| 113 |
+
pn.pane.Markdown(f"**{title}**"),
|
| 114 |
+
pn.pane.DataFrame(df, width=600, height=600),
|
| 115 |
+
align_items="center",
|
| 116 |
+
justify_content="center",
|
| 117 |
+
flex_wrap="nowrap",
|
| 118 |
+
flex_direction="column",
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
def create_dataframe(selected_event_list_index_1, selected_event_list_index_2, dt, norm):
|
| 122 |
+
if selected_event_list_index_1 is not None and selected_event_list_index_2 is not None:
|
| 123 |
+
event_list_1 = loaded_event_data[selected_event_list_index_1][1]
|
| 124 |
+
event_list_2 = loaded_event_data[selected_event_list_index_2][1]
|
| 125 |
+
|
| 126 |
+
try:
|
| 127 |
+
# Ensure GTIs are not empty before proceeding
|
| 128 |
+
if event_list_1.gti.shape[0] == 0 or event_list_2.gti.shape[0] == 0:
|
| 129 |
+
raise ValueError("GTIs are empty for one or both event lists.")
|
| 130 |
+
|
| 131 |
+
# Create a CrossSpectrum object using from_events
|
| 132 |
+
cs = Crossspectrum.from_events(events1=event_list_1, events2=event_list_2, dt=dt, norm=norm)
|
| 133 |
+
|
| 134 |
+
df = pd.DataFrame(
|
| 135 |
+
{
|
| 136 |
+
"Frequency": cs.freq,
|
| 137 |
+
"Power": cs.power,
|
| 138 |
+
}
|
| 139 |
+
)
|
| 140 |
+
return df, cs
|
| 141 |
+
|
| 142 |
+
except ValueError as e:
|
| 143 |
+
if "zero-size array" in str(e):
|
| 144 |
+
output_box_container[:] = [
|
| 145 |
+
create_loadingdata_output_box(
|
| 146 |
+
"Error: GTIs are empty or invalid, leading to zero-size array operations. Please check your event lists."
|
| 147 |
+
)
|
| 148 |
+
]
|
| 149 |
+
else:
|
| 150 |
+
output_box_container[:] = [
|
| 151 |
+
create_loadingdata_output_box(
|
| 152 |
+
f"Error generating Cross Spectrum: {e}. Please check your inputs."
|
| 153 |
+
)
|
| 154 |
+
]
|
| 155 |
+
return None, None
|
| 156 |
+
|
| 157 |
+
except Exception as e:
|
| 158 |
+
output_box_container[:] = [
|
| 159 |
+
create_loadingdata_output_box(
|
| 160 |
+
f"Unexpected error generating Cross Spectrum: {e}. Please try again."
|
| 161 |
+
)
|
| 162 |
+
]
|
| 163 |
+
return None, None
|
| 164 |
+
|
| 165 |
+
return None, None
|
| 166 |
+
|
| 167 |
+
def show_dataframe(event=None):
|
| 168 |
+
if not loaded_event_data:
|
| 169 |
+
output_box_container[:] = [
|
| 170 |
+
create_loadingdata_output_box("No loaded event data available.")
|
| 171 |
+
]
|
| 172 |
+
return
|
| 173 |
+
|
| 174 |
+
selected_event_list_index_1 = event_list_dropdown_1.value
|
| 175 |
+
selected_event_list_index_2 = event_list_dropdown_2.value
|
| 176 |
+
if selected_event_list_index_1 is None or selected_event_list_index_2 is None:
|
| 177 |
+
output_box_container[:] = [
|
| 178 |
+
create_loadingdata_output_box("Both event lists must be selected.")
|
| 179 |
+
]
|
| 180 |
+
return
|
| 181 |
+
|
| 182 |
+
dt = dt_slider.value
|
| 183 |
+
norm = norm_select.value
|
| 184 |
+
df, cs = create_dataframe(selected_event_list_index_1, selected_event_list_index_2, dt, norm)
|
| 185 |
+
if df is not None:
|
| 186 |
+
event_list_name_1 = loaded_event_data[selected_event_list_index_1][0]
|
| 187 |
+
event_list_name_2 = loaded_event_data[selected_event_list_index_2][0]
|
| 188 |
+
dataframe_title = f"{event_list_name_1} vs {event_list_name_2} (dt={dt}, norm={norm})"
|
| 189 |
+
dataframe_output = create_dataframe_panes(df, dataframe_title)
|
| 190 |
+
if dataframe_checkbox.value:
|
| 191 |
+
float_panel_container.append(
|
| 192 |
+
create_floatpanel_area(
|
| 193 |
+
content=dataframe_output,
|
| 194 |
+
title=f"DataFrame for {dataframe_title}",
|
| 195 |
+
)
|
| 196 |
+
)
|
| 197 |
+
else:
|
| 198 |
+
plots_container.append(dataframe_output)
|
| 199 |
+
else:
|
| 200 |
+
output_box_container[:] = [
|
| 201 |
+
create_loadingdata_output_box("Failed to create dataframe.")
|
| 202 |
+
]
|
| 203 |
+
|
| 204 |
+
def generate_cross_spectrum(event=None):
|
| 205 |
+
if not loaded_event_data:
|
| 206 |
+
output_box_container[:] = [
|
| 207 |
+
create_loadingdata_output_box("No loaded event data available.")
|
| 208 |
+
]
|
| 209 |
+
return
|
| 210 |
+
|
| 211 |
+
selected_event_list_index_1 = event_list_dropdown_1.value
|
| 212 |
+
selected_event_list_index_2 = event_list_dropdown_2.value
|
| 213 |
+
if selected_event_list_index_1 is None or selected_event_list_index_2 is None:
|
| 214 |
+
output_box_container[:] = [
|
| 215 |
+
create_loadingdata_output_box("Both event lists must be selected.")
|
| 216 |
+
]
|
| 217 |
+
return
|
| 218 |
+
|
| 219 |
+
dt = dt_slider.value
|
| 220 |
+
norm = norm_select.value
|
| 221 |
+
df, cs = create_dataframe(selected_event_list_index_1, selected_event_list_index_2, dt, norm)
|
| 222 |
+
if df is not None:
|
| 223 |
+
event_list_name_1 = loaded_event_data[selected_event_list_index_1][0]
|
| 224 |
+
event_list_name_2 = loaded_event_data[selected_event_list_index_2][0]
|
| 225 |
+
plot_hv = create_holoviews_plots(cs, event_list_name_1, event_list_name_2, dt, norm)
|
| 226 |
+
holoviews_output = create_holoviews_panes(plot_hv)
|
| 227 |
+
|
| 228 |
+
if floatpanel_plots_checkbox.value:
|
| 229 |
+
float_panel_container.append(
|
| 230 |
+
create_floatpanel_area(
|
| 231 |
+
content=holoviews_output, title=f"Cross Spectrum for {event_list_name_1} vs {event_list_name_2} (dt={dt}, norm={norm})"
|
| 232 |
+
)
|
| 233 |
+
)
|
| 234 |
+
else:
|
| 235 |
+
markdown_content = f"## Cross Spectrum for {event_list_name_1} vs {event_list_name_2} (dt={dt}, norm={norm})"
|
| 236 |
+
plots_container.append(
|
| 237 |
+
pn.FlexBox(
|
| 238 |
+
pn.pane.Markdown(markdown_content),
|
| 239 |
+
holoviews_output,
|
| 240 |
+
align_items="center",
|
| 241 |
+
justify_content="center",
|
| 242 |
+
flex_wrap="nowrap",
|
| 243 |
+
flex_direction="column",
|
| 244 |
+
)
|
| 245 |
+
)
|
| 246 |
+
else:
|
| 247 |
+
output_box_container[:] = [
|
| 248 |
+
create_loadingdata_output_box("Failed to create cross spectrum.")
|
| 249 |
+
]
|
| 250 |
+
|
| 251 |
+
generate_cross_spectrum_button = pn.widgets.Button(
|
| 252 |
+
name="Generate Cross Spectrum", button_type="primary"
|
| 253 |
+
)
|
| 254 |
+
generate_cross_spectrum_button.on_click(generate_cross_spectrum)
|
| 255 |
+
|
| 256 |
+
show_dataframe_button = pn.widgets.Button(
|
| 257 |
+
name="Show DataFrame", button_type="primary"
|
| 258 |
+
)
|
| 259 |
+
show_dataframe_button.on_click(show_dataframe)
|
| 260 |
+
|
| 261 |
+
tab_content = pn.Column(
|
| 262 |
+
event_list_dropdown_1,
|
| 263 |
+
event_list_dropdown_2,
|
| 264 |
+
dt_slider,
|
| 265 |
+
norm_select,
|
| 266 |
+
floatpanel_plots_checkbox,
|
| 267 |
+
dataframe_checkbox,
|
| 268 |
+
pn.Row(generate_cross_spectrum_button, show_dataframe_button),
|
| 269 |
+
)
|
| 270 |
+
return tab_content
|
| 271 |
+
|
| 272 |
+
def create_quicklook_cross_spectrum_main_area(
|
| 273 |
+
header_container,
|
| 274 |
+
main_area_container,
|
| 275 |
+
output_box_container,
|
| 276 |
+
warning_box_container,
|
| 277 |
+
plots_container,
|
| 278 |
+
help_box_container,
|
| 279 |
+
footer_container,
|
| 280 |
+
float_panel_container,
|
| 281 |
+
):
|
| 282 |
+
warning_handler = create_warning_handler()
|
| 283 |
+
tabs_content = {
|
| 284 |
+
"Cross Spectrum": create_cross_spectrum_tab(
|
| 285 |
+
output_box_container=output_box_container,
|
| 286 |
+
warning_box_container=warning_box_container,
|
| 287 |
+
warning_handler=warning_handler,
|
| 288 |
+
plots_container=plots_container,
|
| 289 |
+
header_container=header_container,
|
| 290 |
+
float_panel_container=float_panel_container,
|
| 291 |
+
),
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
return MainArea(tabs_content=tabs_content)
|
| 295 |
+
|
| 296 |
+
def create_quicklook_cross_spectrum_area():
|
| 297 |
+
return PlotsContainer()
|
utils/sidebar.py
CHANGED
|
@@ -33,6 +33,16 @@ from modules.QuickLook.AveragePowerSpectrum import (
|
|
| 33 |
create_quicklook_avg_powerspectrum_main_area,
|
| 34 |
create_quicklook_avg_powerspectrum_area,
|
| 35 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
from assets.icons.svg import HOME_ICON_SVG, LOAD_DATA_ICON_SVG
|
| 37 |
|
| 38 |
def create_sidebar(
|
|
@@ -40,9 +50,10 @@ def create_sidebar(
|
|
| 40 |
):
|
| 41 |
menu_items_quicklook_stingray = [
|
| 42 |
("Light Curve", "QuickLookLightCurve"),
|
| 43 |
-
("Power
|
| 44 |
("Averaged Power Spectrum", "QuickLookAvgPowerspectra"),
|
| 45 |
-
("Cross
|
|
|
|
| 46 |
]
|
| 47 |
|
| 48 |
# Home Button
|
|
@@ -197,6 +208,58 @@ def create_sidebar(
|
|
| 197 |
]
|
| 198 |
plots_container[:] = [create_quicklook_avg_powerspectrum_area()]
|
| 199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
quicklook_stingray_button.on_click(handle_quicklook_button_selection)
|
| 201 |
|
| 202 |
sidebar = pn.FlexBox(
|
|
|
|
| 33 |
create_quicklook_avg_powerspectrum_main_area,
|
| 34 |
create_quicklook_avg_powerspectrum_area,
|
| 35 |
)
|
| 36 |
+
from modules.QuickLook.CrossSpectrum import (
|
| 37 |
+
create_quicklook_cross_spectrum_header,
|
| 38 |
+
create_quicklook_cross_spectrum_main_area,
|
| 39 |
+
create_quicklook_cross_spectrum_area,
|
| 40 |
+
)
|
| 41 |
+
from modules.QuickLook.AverageCrossSpectrum import (
|
| 42 |
+
create_quicklook_avg_cross_spectrum_header,
|
| 43 |
+
create_quicklook_avg_cross_spectrum_main_area,
|
| 44 |
+
create_quicklook_avg_cross_spectrum_area,
|
| 45 |
+
)
|
| 46 |
from assets.icons.svg import HOME_ICON_SVG, LOAD_DATA_ICON_SVG
|
| 47 |
|
| 48 |
def create_sidebar(
|
|
|
|
| 50 |
):
|
| 51 |
menu_items_quicklook_stingray = [
|
| 52 |
("Light Curve", "QuickLookLightCurve"),
|
| 53 |
+
("Power Spectrum", "QuickLookPowerspectra"),
|
| 54 |
("Averaged Power Spectrum", "QuickLookAvgPowerspectra"),
|
| 55 |
+
("Cross Spectrum", "QuickLookCrossSpectrum"),
|
| 56 |
+
("Averaged Cross Spectrum", "QuickLookAvgCrossSpectrum"),
|
| 57 |
]
|
| 58 |
|
| 59 |
# Home Button
|
|
|
|
| 208 |
]
|
| 209 |
plots_container[:] = [create_quicklook_avg_powerspectrum_area()]
|
| 210 |
|
| 211 |
+
elif clicked == "QuickLookCrossSpectrum":
|
| 212 |
+
header[:] = [
|
| 213 |
+
create_quicklook_cross_spectrum_header(
|
| 214 |
+
header_container=header,
|
| 215 |
+
main_area_container=main_area,
|
| 216 |
+
output_box_container=output_box,
|
| 217 |
+
warning_box_container=warning_box,
|
| 218 |
+
plots_container=plots_container,
|
| 219 |
+
help_box_container=help_box,
|
| 220 |
+
footer_container=footer,
|
| 221 |
+
)
|
| 222 |
+
]
|
| 223 |
+
main_area[:] = [
|
| 224 |
+
create_quicklook_cross_spectrum_main_area(
|
| 225 |
+
header_container=header,
|
| 226 |
+
main_area_container=main_area,
|
| 227 |
+
output_box_container=output_box,
|
| 228 |
+
warning_box_container=warning_box,
|
| 229 |
+
plots_container=plots_container,
|
| 230 |
+
help_box_container=help_box,
|
| 231 |
+
footer_container=footer,
|
| 232 |
+
float_panel_container=float_panel_container,
|
| 233 |
+
)
|
| 234 |
+
]
|
| 235 |
+
plots_container[:] = [create_quicklook_cross_spectrum_area()]
|
| 236 |
+
|
| 237 |
+
elif clicked == "QuickLookAvgCrossSpectrum":
|
| 238 |
+
header[:] = [
|
| 239 |
+
create_quicklook_avg_cross_spectrum_header(
|
| 240 |
+
header_container=header,
|
| 241 |
+
main_area_container=main_area,
|
| 242 |
+
output_box_container=output_box,
|
| 243 |
+
warning_box_container=warning_box,
|
| 244 |
+
plots_container=plots_container,
|
| 245 |
+
help_box_container=help_box,
|
| 246 |
+
footer_container=footer,
|
| 247 |
+
)
|
| 248 |
+
]
|
| 249 |
+
main_area[:] = [
|
| 250 |
+
create_quicklook_avg_cross_spectrum_main_area(
|
| 251 |
+
header_container=header,
|
| 252 |
+
main_area_container=main_area,
|
| 253 |
+
output_box_container=output_box,
|
| 254 |
+
warning_box_container=warning_box,
|
| 255 |
+
plots_container=plots_container,
|
| 256 |
+
help_box_container=help_box,
|
| 257 |
+
footer_container=footer,
|
| 258 |
+
float_panel_container=float_panel_container,
|
| 259 |
+
)
|
| 260 |
+
]
|
| 261 |
+
plots_container[:] = [create_quicklook_avg_cross_spectrum_area()]
|
| 262 |
+
|
| 263 |
quicklook_stingray_button.on_click(handle_quicklook_button_selection)
|
| 264 |
|
| 265 |
sidebar = pn.FlexBox(
|