Skip to content

Commit

Permalink
Fixes based on comissioning.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmccully committed Oct 30, 2024
1 parent b1e6cf7 commit ed54990
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 25 deletions.
68 changes: 52 additions & 16 deletions banzai_floyds_ui/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from dash import dcc, html
import dash_bootstrap_components as dbc
from django_plotly_dash import DjangoDash
import logging
import datetime
import requests
import asyncio
Expand All @@ -12,7 +11,7 @@
from banzai_floyds_ui.gui.utils.file_utils import get_filename, cache_fits, get_cached_fits
from banzai_floyds_ui.gui.utils.plot_utils import extraction_region_traces
from dash.exceptions import PreventUpdate
from banzai_floyds_ui.gui.utils.plot_utils import json_to_polynomial, plot_extracted_data
from banzai_floyds_ui.gui.utils.plot_utils import json_to_polynomial
from banzai_floyds_ui.gui.utils.plot_utils import EXTRACTION_REGION_LINE_ORDER
from banzai.utils import import_utils
from banzai.utils.stage_utils import get_stages_for_individual_frame
Expand Down Expand Up @@ -50,15 +49,35 @@ def layout():
id='options-container',
children=[
dbc.Modal([
dbc.ModalHeader(dbc.ModalTitle("Header"), className='bg-danger text-white'),
dbc.ModalHeader(dbc.ModalTitle("Error"), className='bg-danger text-white'),
dbc.ModalBody("You must be logged in to save an extraction."),
dbc.ModalFooter(
dbc.Button("Close", id="close", className="ms-auto", n_clicks=0)
dbc.Button("Close", id="logged-in-close", className="ms-auto", n_clicks=0)
),
],
id="error-logged-in-modal",
is_open=False,
),
dbc.Modal([
dbc.ModalHeader(dbc.ModalTitle("Error"), className='bg-danger text-white'),
dbc.ModalBody("Error extracting spectrum. Plots may not reflect extraction paramters."),
dbc.ModalFooter(
dbc.Button("Close", id="extract-fail-close", className="ms-auto", n_clicks=0)
),
],
id="error-extract-failed-modal",
is_open=False,
),
dbc.Modal([
dbc.ModalHeader(dbc.ModalTitle("Error"), className='bg-danger text-white'),
dbc.ModalBody("Error extracting spectrum. Spectrum will not be saved."),
dbc.ModalFooter(
dbc.Button("Close", id="save-fail-close", className="ms-auto", n_clicks=0)
),
],
id="error-extract-failed-on-save-modal",
is_open=False,
),
html.Div(
children=[
dcc.DatePickerRange(
Expand Down Expand Up @@ -394,8 +413,10 @@ def reextract(hdu, filename, extraction_positions, initial_extraction_info, runt
frame.orders, frame.wavelengths.data)
extraction_windows = []
for order in [1, 2]:
lower = extraction_positions[str(order)]['extract_lower'] / initial_extraction_info['refsigma'][str(order)]
upper = extraction_positions[str(order)]['extract_upper'] / initial_extraction_info['refsigma'][str(order)]
lower = extraction_positions[str(order)]['extract_lower'] - extraction_positions[str(order)]['center']
lower /= initial_extraction_info['refsigma'][str(order)]
upper = extraction_positions[str(order)]['extract_upper'] - extraction_positions[str(order)]['center']
upper /= initial_extraction_info['refsigma'][str(order)]
extraction_windows.append([lower, upper])

frame.extraction_windows = extraction_windows
Expand All @@ -407,9 +428,9 @@ def reextract(hdu, filename, extraction_positions, initial_extraction_info, runt
for order in [1, 2]:
order_background = []
for region in ['left', 'right']:
inner = extraction_positions[str(order)][f'bkg_{region}_inner']
inner = extraction_positions[str(order)][f'bkg_{region}_inner'] - extraction_positions[str(order)]['center']
inner /= initial_extraction_info['refsigma'][str(order)]
outer = extraction_positions[str(order)][f'bkg_{region}_outer']
outer = extraction_positions[str(order)][f'bkg_{region}_outer'] - extraction_positions[str(order)]['center']
outer /= initial_extraction_info['refsigma'][str(order)]
this_background = [inner, outer]
this_background.sort()
Expand All @@ -431,10 +452,12 @@ def reextract(hdu, filename, extraction_positions, initial_extraction_info, runt
if not frames:
logger.error('Reduction stopped', extra_tags={'filename': filename})
return
logger.info('Reduction complete', extra_tags={'filename': filename})
return frame


@app.expanded_callback(dash.dependencies.Output('extractions', 'data'),
@app.expanded_callback([dash.dependencies.Output('extractions', 'data'),
dash.dependencies.Output('error-extract-failed-modal', 'is_open')],
[dash.dependencies.Input('extraction-positions', 'data'),
dash.dependencies.Input('extraction-type', 'value')],
[dash.dependencies.State('initial-extraction-info', 'data'),
Expand All @@ -452,7 +475,18 @@ def trigger_reextract(extraction_positions, extraction_type, initial_extraction_
filename = get_filename(frame_id, frame_data)
frame = reextract(science_frame, filename, extraction_positions, initial_extraction_info,
RUNTIME_CONTEXT, extraction_type=extraction_type.lower())
return plot_extracted_data(frame.extracted)

if frame is None:
return dash.no_update, True

x = []
y = []
for order in [2, 1]:
where_order = frame.extracted['order'] == order
for flux in ['flux', 'fluxraw', 'background']:
x.append(frame.extracted['wavelength'][where_order])
y.append(frame.extracted[flux][where_order])
return {'x': x, 'y': y}, False


app.clientside_callback(
Expand All @@ -461,11 +495,10 @@ def trigger_reextract(extraction_positions, extraction_type, initial_extraction_
if (typeof extraction_data === "undefined") {
return window.dash_clientside.no_update;
}
var dccGraph = document.getElementById('sci-1d-plot');
var dccGraph = document.getElementById('extraction-plot');
var jsFigure = dccGraph.querySelector('.js-plotly-plot');
var trace_ids = [];
for (let i = 1; i <= extraction_data.x.length; i++) {
for (let i = 0; i < extraction_data.x.length; i++) {
trace_ids.push(i);
}
Plotly.restyle(jsFigure, extraction_data, trace_ids);
Expand All @@ -476,7 +509,8 @@ def trigger_reextract(extraction_positions, extraction_type, initial_extraction_
prevent_initial_call=True)


@app.expanded_callback(dash.dependencies.Output("error-logged-in-modal", "is_open"),
@app.expanded_callback([dash.dependencies.Output("error-logged-in-modal", "is_open"),
dash.dependencies.Output('error-extract-failed-on-save-modal', 'is_open')],
dash.dependencies.Input('extract-button', 'n_clicks'),
[dash.dependencies.State('extraction-type', 'value'),
dash.dependencies.State('extraction-positions', 'data'),
Expand All @@ -493,15 +527,17 @@ def save_extraction(n_clicks, extraction_type, extraction_positions, initial_ext
# If not logged in, open a modal saying you can only save if you are.
username = kwargs['session_state'].get('username')
if username is None:
return True
return True, dash.no_update

filename = get_filename(frame_id, frame_data)
# Run the reextraction
extracted_frame = reextract(science_frame, filename, extraction_positions, initial_extraction_info,
RUNTIME_CONTEXT, extraction_type=extraction_type.lower())
if extracted_frame is None:
return dash.no_update, True
# Save the reducer into the header
extracted_frame.meta['REDUCER'] = username
# Push the results to the archive
extracted_frame.write(RUNTIME_CONTEXT)
# Return false to keep the error modal closed
return False
return dash.no_update, dash.no_update
18 changes: 9 additions & 9 deletions banzai_floyds_ui/gui/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def make_2d_sci_plot(frame, filename):
frame['SCI'].data.shape)
order_polynomial = np.polynomial.Legendre(orders.coeffs[order - 1], domain=orders.domains[order - 1])

wavelength_solution = WavelengthSolution.from_header(frame['WAVELENGTHS'].header, orders)
wavelengths_polynomial = Legendre(coef=wavelength_solution.coefficients[order - 1],
domain=wavelength_solution.domains[order - 1])
wavelenth_solution = WavelengthSolution.from_header(frame['WAVELENGTHS'].header, orders)
wavelengths_polynomial = Legendre(coef=wavelenth_solution.coefficients[order - 1],
domain=wavelenth_solution.domains[order - 1])
center_polynomial = header_to_polynomial(frame['PROFILEFITS'].header, 'CTR', order)
width_polynomal = header_to_polynomial(frame['PROFILEFITS'].header, 'WID', order)
for polynomial, key in zip([order_polynomial, center_polynomial, width_polynomal, wavelengths_polynomial],
Expand All @@ -97,9 +97,9 @@ def make_2d_sci_plot(frame, filename):

x, traces = extraction_region_traces(order_polynomial, center_polynomial, width_polynomal,
wavelengths_polynomial, extract_lower_n_sigma, upper_lower_n_sigma,
bkg_left_lower_n_sigma, bkg_right_lower_n_sigma, bkg_left_upper_n_sigma,
bkg_left_upper_n_sigma, bkg_right_lower_n_sigma, bkg_left_lower_n_sigma,
bkg_right_upper_n_sigma)

print('Got here', f'{extract_lower_n_sigma=}', f'{upper_lower_n_sigma=}', f'{bkg_left_upper_n_sigma=}', f'{bkg_right_lower_n_sigma=}', f'{bkg_left_lower_n_sigma=}', f'{bkg_right_upper_n_sigma=}')
if order == 2:
center_name = 'Extraction Center'
center_legend = True
Expand Down Expand Up @@ -330,13 +330,13 @@ def make_profile_plot(sci_2d_frame):
'range': [-0.1, 1.5], 'domain': [0.565, 1.0]},
'yaxis2': {'anchor': 'x2', 'title': {'text': 'y (pixel)'}, 'domain': [0.565, 1.0]},
'yaxis3': {'anchor': 'x3', 'title': {'text': 'Normalized Flux'},
'range': [-0.1, 1.5],'domain': [0.0, 0.435]},
'range': [-0.1, 1.5], 'domain': [0.0, 0.435]},
'yaxis4': {'anchor': 'x4', 'title': {'text': 'y (pixel)'}, 'domain': [0.0, 0.435]}
}

figure_data = []
plot_row = {2: 1, 1: 2}
# Define the coordinate reference plot manually per order
# Define the coordinate refernce plot manually per order
reference_axes = {2: 1, 1: 3}
# Approximate wavelength center to plot the profile
order_center = {1: 7000, 2: 4500}
Expand Down Expand Up @@ -412,8 +412,8 @@ def make_profile_plot(sci_2d_frame):

bkg_right_lower_n_sigma = sci_2d_frame['SCI'].header[f'BKWO{order}10']
xs = [extract_center + n_sigma * extract_sigma for n_sigma in
[0, extract_lower_n_sigma, extract_upper_n_sigma, bkg_left_lower_n_sigma, bkg_left_upper_n_sigma,
bkg_right_lower_n_sigma, bkg_right_upper_n_sigma]]
[0, extract_lower_n_sigma, extract_upper_n_sigma, bkg_left_upper_n_sigma,
bkg_right_lower_n_sigma, bkg_left_lower_n_sigma, bkg_right_upper_n_sigma]]
for position, key in zip(xs, EXTRACTION_REGION_LINE_ORDER):
initial_extraction_info['positions'][str(order)][key] = position

Expand Down

0 comments on commit ed54990

Please sign in to comment.