Skip to content

Commit 2ed26ef

Browse files
committed
Add simple processing pipeline to DataDevice.
1 parent 90aa4cd commit 2ed26ef

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

microscope/devices.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ def __init__(self, buffer_length=0, **kwargs):
409409
self._acquiring = False
410410
# A condition to signal arrival of a new data and unblock grab_next_data
411411
self._new_data_condition = threading.Condition()
412+
# A data processing pipeline: a list of f(data) -> data.
413+
self.pipeline = []
412414

413415
def __del__(self):
414416
self.disable()
@@ -481,8 +483,12 @@ def _fetch_data(self):
481483
return None
482484

483485
def _process_data(self, data):
484-
"""Do any data processing and return data."""
485-
return data
486+
"""Do any data processing and return data.
487+
488+
Subclasses should call super()._process_data(data) after doing their
489+
own processing."""
490+
import functools
491+
return functools.reduce(lambda x, f: f(x), self.pipeline, data)
486492

487493
def _send_data(self, client, data, timestamp):
488494
"""Dispatch data to the client."""
@@ -667,18 +673,19 @@ def __init__(self, **kwargs):
667673
self.get_roi,
668674
self.set_roi,
669675
None)
676+
670677
def _process_data(self, data):
671678
"""Apply self._transform to data."""
672679
flips = (self._transform[0], self._transform[1])
673680
rot = self._transform[2]
674681

675682
# Choose appropriate transform based on (flips, rot).
676-
return {(0, 0): numpy.rot90(data, rot),
677-
(0, 1): numpy.flipud(numpy.rot90(data, rot)),
678-
(1, 0): numpy.fliplr(numpy.rot90(data, rot)),
679-
(1, 1): numpy.fliplr(numpy.flipud(numpy.rot90(data, rot)))
680-
}[flips]
681-
683+
data = {(0, 0): numpy.rot90(data, rot),
684+
(0, 1): numpy.flipud(numpy.rot90(data, rot)),
685+
(1, 0): numpy.fliplr(numpy.rot90(data, rot)),
686+
(1, 1): numpy.fliplr(numpy.flipud(numpy.rot90(data, rot)))
687+
}[flips]
688+
return super()._process_data(data)
682689

683690
def set_readout_mode(self, description):
684691
"""Set the readout mode and _readout_transform."""

0 commit comments

Comments
 (0)