Skip to content

Commit

Permalink
Replace pipeline_properties with pipeline_classes and pipeline_kwargs…
Browse files Browse the repository at this point in the history
… to match the new connect_pipelines() interface of the group observers.
  • Loading branch information
vsnever committed Mar 23, 2022
1 parent c3d467f commit a823630
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 30 deletions.
52 changes: 34 additions & 18 deletions cherab/tools/spectroscopy/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ class SpectroscopicInstrument:
:param str name: Instrument name.
:ivar list pipeline_properties: The list of properties (class, name, filter) of
the pipelines used with this instrument.
:ivar list pipeline_classes: The list of pipeline classes used with this instrument.
:ivar list pipeline_kwargs: The list of dicts with keywords passed to init methods of
pipeline classes used with this instrument.
:ivar float min_wavelength: Lower wavelength bound for spectral range.
:ivar float max_wavelength: Upper wavelength bound for spectral range.
:ivar int spectral_bins: The number of spectral samples over the wavelength range.
"""

def __init__(self, name=''):
self._pipeline_classes = None
self.name = name
self._clear_spectral_settings()

Expand All @@ -43,28 +45,39 @@ def name(self):
@name.setter
def name(self, value):
self._name = str(value)
self._pipeline_properties = None
self._pipeline_kwargs = None

@property
def pipeline_properties(self):
# The list of properties (class, name, filter) of the pipelines used with
# this instrument.
if self._pipeline_properties is None:
self._update_pipeline_properties()
def pipeline_classes(self):
# The list of pipeline classes used with this instrument.
if self._pipeline_classes is None:
self._update_pipeline_classes()

return self._pipeline_properties
return self._pipeline_classes

@property
def pipeline_kwargs(self):
# The list of dicts with keywords passed to init methods of
# pipeline classes used with this instrument.
if self._pipeline_kwargs is None:
self._update_pipeline_kwargs()

return self._pipeline_kwargs

def create_pipelines(self):
""" Returns a list of new pipelines created according to `pipeline_properties`."""
""" Returns a list of new pipelines created according to `pipeline_classes`
and keyword arguments."""
if self._pipeline_classes is None:
self._update_pipeline_classes()
if self._pipeline_kwargs is None:
self._update_pipeline_kwargs()

pl_list = []
for (pl_class, pl_name, pl_filter) in self.pipeline_properties:
if pl_filter is None:
pl_list.append(pl_class(name=pl_name))
else:
pl_list.append(pl_class(name=pl_name, filter=pl_filter))
pipelines = []
for PipelineClass, kwargs in zip(self._pipeline_classes, self._pipeline_kwargs):
pipeline = PipelineClass(**kwargs)
pipelines.append(pipeline)

return pl_list
return pipelines

@property
def min_wavelength(self):
Expand Down Expand Up @@ -98,5 +111,8 @@ def _clear_spectral_settings(self):
def _update_spectral_settings(self):
raise NotImplementedError("To be defined in subclass.")

def _update_pipeline_properties(self):
def _update_pipeline_classes(self):
raise NotImplementedError("To be defined in subclass.")

def _update_pipeline_kwargs(self):
raise NotImplementedError("To be defined in subclass.")
10 changes: 7 additions & 3 deletions cherab/tools/spectroscopy/polychromator.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,14 @@ def filters(self, value):

self._filters = value
self._clear_spectral_settings()
self._pipeline_properties = None
self._pipeline_classes = None
self._pipeline_kwargs = None

def _update_pipeline_properties(self):
self._pipeline_properties = [(RadiancePipeline0D, self._name + ': ' + poly_filter.name, poly_filter) for poly_filter in self._filters]
def _update_pipeline_classes(self):
self._pipeline_classes = [RadiancePipeline0D for poly_filter in self._filters]

def _update_pipeline_kwargs(self):
self._pipeline_kwargs = [{'name': self._name + ': ' + poly_filter.name, 'filter': poly_filter} for poly_filter in self._filters]

def _update_spectral_settings(self):

Expand Down
7 changes: 5 additions & 2 deletions cherab/tools/spectroscopy/spectrometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ def min_bins_per_pixel(self, value):
self._min_bins_per_pixel = value
self._clear_spectral_settings()

def _update_pipeline_properties(self):
self._pipeline_properties = [(SpectralRadiancePipeline0D, self._name, None)]
def _update_pipeline_classes(self):
self._pipeline_classes = [SpectralRadiancePipeline0D]

def _update_pipeline_kwargs(self):
self._pipeline_kwargs = [{'name': self._name}]

def _update_spectral_settings(self):
self._min_wavelength = min(wl2pix[0] for wl2pix in self._wavelength_to_pixel)
Expand Down
23 changes: 16 additions & 7 deletions cherab/tools/tests/test_spectroscopic_instruments.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ class TestPolychromator(unittest.TestCase):
TrapezoidalFilter(700., 8., 4., 'filter 2'))
min_bins_per_window_default = 10

def test_pipeline_properties(self):
def test_pipeline_classes(self):
polychromator = Polychromator(self.poly_filters_default, self.min_bins_per_window_default, 'test polychromator')
pipeline_properties_true = [(RadiancePipeline0D, 'test polychromator: filter 1', self.poly_filters_default[0]),
(RadiancePipeline0D, 'test polychromator: filter 2', self.poly_filters_default[1])]
self.assertSequenceEqual(pipeline_properties_true, polychromator.pipeline_properties)
pipeline_classes_true = [RadiancePipeline0D, RadiancePipeline0D]
self.assertSequenceEqual(pipeline_classes_true, polychromator.pipeline_classes)

def test_pipeline_kwargs(self):
polychromator = Polychromator(self.poly_filters_default, self.min_bins_per_window_default, 'test polychromator')
pipeline_kwargs_true = [{'name': 'test polychromator: filter 1', 'filter': self.poly_filters_default[0]},
{'name': 'test polychromator: filter 2', 'filter': self.poly_filters_default[1]}]
self.assertSequenceEqual(pipeline_kwargs_true, polychromator.pipeline_kwargs)

def test_spectral_properties(self):
polychromator = Polychromator(self.poly_filters_default, self.min_bins_per_window_default)
min_wavelength_true = 397.
Expand Down Expand Up @@ -98,11 +103,15 @@ class TestSpectrometer(unittest.TestCase):
Test cases for Spectrometer class.
"""

def test_pipeline_properties(self):
def test_pipeline_classes(self):
wavelength_to_pixel = ([400., 400.5],)
spectrometer = Spectrometer(wavelength_to_pixel, name='test spectrometer')
self.assertSequenceEqual([SpectralRadiancePipeline0D], spectrometer.pipeline_classes)

def test_pipeline_kwargs(self):
wavelength_to_pixel = ([400., 400.5],)
spectrometer = Spectrometer(wavelength_to_pixel, name='test spectrometer')
pipeline_properties_true = [(SpectralRadiancePipeline0D, 'test spectrometer', None)]
self.assertSequenceEqual(pipeline_properties_true, spectrometer.pipeline_properties)
self.assertSequenceEqual([{'name': 'test spectrometer'}], spectrometer.pipeline_kwargs)

def test_spectral_properties(self):
wavelength_to_pixel = ([400., 400.5, 401.5, 402., 404.], [600., 600.5, 601.5, 602., 604., 607.])
Expand Down

0 comments on commit a823630

Please sign in to comment.