Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for selecting a TOA range in detector plots #304

Merged
merged 4 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/beamlime/handlers/detector_data_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ..core.handler import (
Accumulator,
Config,
ConfigValueAccessor,
Handler,
HandlerFactory,
PeriodicAccumulatingHandler,
Expand Down Expand Up @@ -135,9 +136,29 @@ class DetectorCounts(Accumulator[sc.DataArray, sc.DataArray]):
def __init__(self, config: Config, detector_view: raw.RollingDetectorView):
self._config = config
self._det = detector_view
self._toa_range = ConfigValueAccessor(
config, 'toa_range', default=None, convert=self._convert_toa_range
)
self._current_toa_range = None

def _convert_toa_range(self, value: dict[str, Any] | None) -> None:
self.clear()
if value is None:
return None
return (
sc.scalar(value['low'], unit=value['unit']).to(unit='ns'),
sc.scalar(value['high'], unit=value['unit']).to(unit='ns'),
Comment on lines +149 to +150
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the unit always be 'ns', or should we instead just store floats and use the units of data.bins.coords['toa'] in apply_toa_range below?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bounds come via a message from Kafka. It is essential to send to unit with this (or you will waste hours debugging). Not sure what is the benefit from your suggestion, recreating the low and high variables in every single call below? It is just making it a bit slower?

)

def apply_toa_range(self, data: sc.DataArray) -> sc.DataArray:
if (toa_range := self._toa_range()) is None:
return data
low, high = toa_range
return data.bins.assign_coords(toa=data.bins.data).bins['toa', low:high]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need some explanation here 😄

  • What is data? (What shape shape and what fields is it expected to have?)
  • Why do we need to assign coords?
  • Why is toa=data.bins.data?


def add(self, timestamp: int, data: sc.DataArray) -> None:
_ = timestamp
data = self.apply_toa_range(data)
self._det.add_events(data)

def get(self) -> sc.DataArray:
Expand Down
37 changes: 37 additions & 0 deletions src/beamlime/services/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ def _setup_layout(self) -> None:
value=[45, 55],
marks={i: str(i) for i in range(0, 101, 20)},
),
html.Label('Time-of-arrival range (us)'),
dcc.Checklist(
id='toa-checkbox',
options=[
{'label': 'Filter by time-of-arrival [μs]', 'value': 'enabled'}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems it's microseconds here and nanoseconds above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text in the html.Label is 'Time-of-arrival range (us)', and the unit label in the square brackets is Filter by time-of-arrival [μs]?

Unless this is not the same time as in the _convert_toa_range function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand, us is just another spelling of microsecond?

Copy link
Member

@nvaytet nvaytet Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant was that on L149-150 in detector_data_handler.py you seem to have converted them to nanoseconds?
By 'above' I meant further up the page, not in the same file, sorry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is wrong with converting microseconds to nanoseconds? The dashboard uses microseconds, since nanoseconds are a bit unwieldy for a user to think about.

],
value=[],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(minor) checklist is slightly odd for what seems to be a "toggle" on/off, maybe consider https://dash.plotly.com/dash-daq/booleanswitch

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That appears to be from a different library (dash_daq)?

style={'margin': '10px 0'},
),
dcc.RangeSlider(
id='toa-range',
min=0,
max=71_000,
step=100,
value=[0, 71_000],
marks={i: str(i) for i in range(0, 71_001, 10_000)},
),
html.Button('Clear', id='clear-button', n_clicks=0),
]
self._app.layout = html.Div(
Expand Down Expand Up @@ -204,6 +221,17 @@ def _setup_callbacks(self) -> None:
Input('roi-y', 'value'),
)(self.update_roi)

self._app.callback(
Output('toa-range', 'disabled'),
Input('toa-checkbox', 'value'),
)(lambda value: len(value) == 0)

self._app.callback(
Output('toa-range', 'value'),
Input('toa-range', 'value'),
Input('toa-checkbox', 'value'),
)(self.update_toa_range)

def update_roi(self, roi_x, roi_y):
if roi_x is not None:
self._config_service.update_config(
Expand Down Expand Up @@ -236,6 +264,15 @@ def update_roi(self, roi_x, roi_y):

return roi_x, roi_y

def update_toa_range(self, toa_range, toa_enabled):
if len(toa_enabled) == 0:
self._config_service.update_config('toa_range', None)
else:
self._config_service.update_config(
'toa_range', {'low': toa_range[0], 'high': toa_range[1], 'unit': 'us'}
)
return toa_range

@staticmethod
def create_monitor_plot(key: str, data: sc.DataArray) -> go.Figure:
fig = go.Figure()
Expand Down
Loading