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

387 clean reset of applet state when scan is overwritten #395

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions ndscan/experiment/entry_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,17 +319,18 @@ def build(self,

def run(self):
"""Run the (possibly trivial) scan."""
self._broadcast_metadata()

if not self.spec.axes and not self._is_time_series:
self._broadcast_metadata()
self._run_continuous()
return None, {c: s.get_last() for c, s in self._scan_result_sinks.items()}

if self._is_time_series:
self._timestamp_sink = AppendingDatasetSink(
self, self.dataset_prefix + "points.axis_0")
self._timestamp_sink.clear()
self._coordinate_sinks = [self._timestamp_sink]
self._time_series_start = time.monotonic()
self._broadcast_metadata()
self._run_continuous()
else:
runner = select_runner_class(self.fragment)(
Expand All @@ -342,6 +343,9 @@ def run(self):
AppendingDatasetSink(self, self.dataset_prefix + f"points.axis_{i}")
for i in range(len(self.spec.axes))
]
for sink in self._coordinate_sinks:
sink.clear()
self._broadcast_metadata()
runner.run(self.fragment, self.spec, self._coordinate_sinks)
self._set_completed()

Expand Down Expand Up @@ -499,6 +503,9 @@ def push(name, value):
else:
push(name, ds_value)

for sink in self._scan_result_sinks.values():
sink.clear()

def create_applet(self, title: str, group: str = "ndscan"):
cmd = [
"${python}", "-m ndscan.applet", "--server=${server}",
Expand Down
6 changes: 6 additions & 0 deletions ndscan/experiment/result_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ def get_all(self) -> list[Any]:
"""Read back the previously pushed values from the target dataset (if any)."""
return [] if (self.last_value is None) else self.get_dataset(self.key)

def clear(self):
self.set_dataset(self.key, [], broadcast=self.broadcast)


class ScalarDatasetSink(ResultSink, HasEnvironment):
"""Sink that writes pushed results to a dataset, overwriting its previous value
Expand All @@ -143,6 +146,9 @@ def get_last(self) -> Any:
"""Return the last pushed value, or ``None`` if none yet."""
return self.get_dataset(self.key) if self.has_pushed else None

def clear(self):
pass


class ResultChannel:
"""
Expand Down
34 changes: 17 additions & 17 deletions ndscan/plots/model/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,25 @@ def d(name):
if schema_revision is None:
return

fqn = d("fragment_fqn")
if not self._title_set or self._context.get_title() != fqn:
source_id = d("source_id")
if self._context.get_source_id() != source_id:
# If the source_id changed, clear the applet.
self._model = None
self._title_set = False
self._source_id_set = False
self._axes_initialised = False
keys = list(values.keys())
for key in keys:
if "points." in key:
del values[key]

if not self._title_set:
fqn = d("fragment_fqn")
if fqn:
self._context.set_title(fqn)
self._title_set = True

source_id = d("source_id")
if not self._source_id_set or self._context.get_source_id() != source_id:
if not self._source_id_set:
if source_id:
self._context.set_source_id(source_id)
self._source_id_set = True
Expand Down Expand Up @@ -194,21 +205,10 @@ def data_changed(self, values: dict[str, Any], mods: Iterable[dict[str,
for name, source in self._analysis_result_sources.items():
source.set(values.get(self._prefix + "analysis_result." + name))

point_data_changed = False
for name in ([f"axis_{i}" for i in range(len(self.axes))] +
["channel_" + c for c in self._channel_schemata.keys()]):
point_values = values.get(self._prefix + "points." + name, [])
if not point_data_changed:
# Check if points were appended or rewritten.
if name in self._point_data:
imax = min(len(point_values), len(self._point_data[name]))
if point_values[:imax] != self._point_data[name][:imax]:
point_data_changed = True
self._point_data[name] = point_values
if point_data_changed:
self.points_rewritten.emit(self._point_data)
else:
self.points_appended.emit(self._point_data)
self._point_data[name] = values.get(self._prefix + "points." + name, [])
self.points_appended.emit(self._point_data)

def get_annotations(self) -> list[Annotation]:
return self._annotations
Expand Down
2 changes: 1 addition & 1 deletion ndscan/plots/rolling_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _initialise_series(self):

if len(self.panes) > 1:
self.link_x_axes()
add_source_id_label(self.panes[-1].getViewBox(), self.model.context)
add_source_id_label(self.panes[-1].getViewBox(), self.model.context)

self.ready.emit()

Expand Down
1 change: 1 addition & 0 deletions ndscan/plots/xy_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def __init__(self, model: ScanModel, get_alternate_plot_names):
self.model.channel_schemata_changed.connect(self._initialise_series)
self.model.points_appended.connect(self._update_points)
self.model.annotations_changed.connect(self._update_annotations)
# FIXME: Just re-set values instead of throwing away everything.
self.model.points_rewritten.connect(self._rewrite)

self.selected_point_model = SelectPointFromScanModel(self.model)
Expand Down
1 change: 1 addition & 0 deletions test/test_plots_model_subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def setUp(self):
self.context = Context()
self.root = SubscriberRoot("ndscan.", self.context)
self.datasets = Notifier({
"ndscan.source_id": (False, "rid_0", {}),
"ndscan.axes": (False, "[]", {}),
"ndscan.channels": (False,
json.dumps({
Expand Down