def test_from_equipment( self, num_sources: int, source_name: str, exception: Exception ): """Test the `from_equipment` factory and its exceptions. Parameters ---------- num_sources : Number of sources of the generated equipment source_name : Corresponding parameter of `from_equipment` exception : Expected exception """ sources = [ SignalSource(**self._default_source_kwargs({"name": f"source_{i}"})) for i in range(num_sources) ] equipment = MeasurementEquipment("Equipment", sources=sources) if exception is not None: with pytest.raises(exception): MeasurementChain.from_equipment( name="name", equipment=equipment, source_name=source_name ) else: MeasurementChain.from_equipment( name="name", equipment=equipment, source_name=source_name )
def test_add_transformation_exceptions( self, tf_kwargs: dict, input_signal_source: str, exception_type, test_name: str ): """Test the exceptions of the `add_transformation` method. Parameters ---------- tf_kwargs: A dictionary with keyword arguments that are used to construct the `SignalTransformation` that is passed to the `add_transformation` method. Missing arguments are added. input_signal_source : The value of the corresponding parameter of 'add_transformation' exception_type : The expected exception type test_name : Name of the test """ mc = MeasurementChain(**self._default_init_kwargs()) tf = self._default_transformation(tf_kwargs) with pytest.raises(exception_type): mc.add_transformation(tf, input_signal_source=input_signal_source)
def test_get_transformation(self): """Test the `get_transformation` method.""" mc = MeasurementChain(**self._default_init_kwargs()) mc.add_transformation(self._default_transformation()) transformation = mc.get_transformation("transformation") assert transformation == self._default_transformation()
def test_add_signal_data_exceptions( self, kwargs: dict, exception_type, test_name: str ): """Test the exceptions of the `add_signal_data` method. Parameters ---------- kwargs : A dictionary with keyword arguments that are passed to the `add_signal_data` method. Missing arguments are added. exception_type : The expected exception type test_name : Name of the test """ mc = MeasurementChain(**self._default_init_kwargs()) mc.add_transformation(self._default_transformation(), data=[1, 2, 3]) mc.add_transformation( self._default_transformation( dict(name="transformation 2", type_transformation="DA") ) ) full_kwargs = dict(data=xr.DataArray([1, 2])) full_kwargs.update(kwargs) with pytest.raises(exception_type): mc.add_signal_data(**full_kwargs)
def measurement_chain_with_equipment() -> MeasurementChain: """Get a default measurement chain with attached equipment.""" source = SignalSource( "Current measurement", output_signal=Signal(signal_type="analog", units="V"), error=Error(Q_(1, "percent")), ) ad_conversion = SignalTransformation( "AD conversion current measurement", error=Error(Q_(0, "percent")), func=MathematicalExpression(expression="a*x+b", parameters=dict(a=Q_(1, "1/V"), b=Q_(1, ""))), ) calibration = SignalTransformation( "Current measurement calibration", error=Error(Q_(1.2, "percent")), func=MathematicalExpression(expression="a*x+b", parameters=dict(a=Q_(1, "A"), b=Q_(1, "A"))), ) eq_source = MeasurementEquipment( name="Source Equipment", sources=[source], ) eq_ad_conversion = MeasurementEquipment(name="AD Equipment", transformations=[ad_conversion]) eq_calibration = MeasurementEquipment(name="Calibration Equipment", transformations=[calibration]) mc = MeasurementChain.from_equipment("Measurement chain", eq_source) mc.add_transformation_from_equipment(eq_ad_conversion) mc.add_transformation_from_equipment(eq_calibration) return mc
def test_get_equipment(self, signal_source, exception): """Test the `get_equipment` function and their exceptions. Parameters ---------- signal_source : Corresponding function parameter exception : Expected exception """ src_eq = MeasurementEquipment( "Source Eq", sources=[SignalSource(**self._default_source_kwargs())] ) tf_eq = MeasurementEquipment( "Transformation_eq", transformations=[ self._default_transformation({"name": "transformation_1"}) ], ) mc = MeasurementChain.from_equipment("Chain", src_eq) mc.add_transformation_from_equipment(tf_eq) mc.create_transformation("transformation_2", None, output_signal_unit="A") if exception is not None: with pytest.raises(exception): mc.get_equipment(signal_source=signal_source) else: mc.get_equipment(signal_source=signal_source)
def test_add_transformation_from_equipment( self, num_transformations: int, transformation_name: str, exception ): """Test `add_transformation_from_equipment` and its exceptions. Parameters ---------- num_transformations : Number of transformations of the generated equipment transformation_name : Corresponding parameter of `add_transformation_from_equipment` exception : Expected exception """ mc = MeasurementChain(**self._default_init_kwargs()) transformations = [ self._default_transformation({"name": f"transformation_{i}"}) for i in range(num_transformations) ] equipment = MeasurementEquipment(name="name", transformations=transformations) if exception is not None: with pytest.raises(exception): mc.add_transformation_from_equipment( equipment=equipment, transformation_name=transformation_name ) else: mc.add_transformation_from_equipment( equipment=equipment, transformation_name=transformation_name )
def measurement_chain_without_equipment() -> MeasurementChain: """Get a default measurement chain without attached equipment.""" mc = MeasurementChain( "Current measurement chain", SignalSource( "Current measurement", Signal("analog", "V"), Error(Q_(0.1, "percent")), ), ) mc.add_transformation( SignalTransformation( name="AD conversion", error=Error(Q_(0.5, "percent")), type_transformation="AD", )) mc.add_transformation( SignalTransformation( name="Calibration", error=Error(Q_(1.5, "percent")), func=MathematicalExpression("a*x+b", parameters=dict(a=Q_(3, "A/V"), b=Q_(2, "A"))), )) return mc
def test_get_transformation_exception(self): """Test that a `KeyError` is raised if the transformation does not exist.""" mc = MeasurementChain(**self._default_init_kwargs()) mc.add_transformation(self._default_transformation()) with pytest.raises(KeyError): mc.get_transformation("not found")
def test_add_transformation(self, tf_kwargs, exp_signal_type, exp_signal_unit): """Test the `add_transformation` method of the `MeasurementChain`. Parameters ---------- tf_kwargs: A dictionary with keyword arguments that are used to construct the `SignalTransformation` that is passed to the `add_transformation` method. Missing arguments are added. exp_signal_type : The expected signal type after the transformation exp_signal_unit : The expected unit after the transformation """ mc = MeasurementChain(**self._default_init_kwargs()) mc.add_transformation(self._default_transformation(tf_kwargs)) signal = mc.output_signal assert signal.signal_type == exp_signal_type assert U_(signal.units) == exp_signal_unit
def test_init(kwargs: dict, source_kwargs: dict): """Test the `__init__` method of the `MeasurementChain`. Parameters ---------- kwargs: A dictionary with keyword arguments that are passed to the `__init__` method. Missing arguments are added. source_kwargs : A dictionary with keyword arguments that are used to construct the `SignalSource` that is passed to the `__init__` method. Missing arguments are added. """ kwargs = TestMeasurementChain._default_init_kwargs(kwargs, source_kwargs) MeasurementChain(**kwargs)
def test_init_exceptions( kwargs: dict, source_kwargs: dict, exception_type, test_name: str ): """Test the exceptions of the `__init__` method. Parameters ---------- kwargs : A dictionary with keyword arguments that are passed to the `__init__` method. Missing arguments are added. source_kwargs : A dictionary with keyword arguments that are used to construct the `SignalSource` that is passed to the `__init__` method. Missing arguments are added. exception_type : The expected exception type test_name : Name of the test """ kwargs = TestMeasurementChain._default_init_kwargs(kwargs, source_kwargs) with pytest.raises(exception_type): MeasurementChain(**kwargs)
def test_add_signal_data(self, kwargs): """Test the `add_signal_data` method of the `MeasurementChain`. Parameters ---------- kwargs: A dictionary with keyword arguments that are passed to the `add_signal_data` method. If no name is in the kwargs, a default one is added. """ mc = MeasurementChain(**self._default_init_kwargs({"signal_data": None})) mc.add_transformation(self._default_transformation()) full_kwargs = dict(data=xr.DataArray([1, 2])) full_kwargs.update(kwargs) mc.add_signal_data(**full_kwargs)
def from_yaml_tree(self, node: dict, tag: str, ctx): """Reconstruct from tree.""" return MeasurementChain.from_dict(node)
def to_yaml_tree(self, obj: MeasurementChain, tag: str, ctx) -> dict: """Convert to python dict.""" return obj.to_dict()
def from_tree(cls, tree, ctx): obj = MeasurementChain(**tree) return obj
def test_get_signal_data(self): """Test the `get_signal_data` method. This test assures that the returned data is identical to the one passed to the measurement chain and that a key error is raised if the requested data is not present. """ data = xr.DataArray([1, 2, 3]) mc = MeasurementChain(**self._default_init_kwargs()) mc.add_transformation(self._default_transformation(), data=data) mc.create_transformation("transformation_2", None, output_signal_unit="A") assert np.all(mc.get_signal_data("transformation") == data) # no data with pytest.raises(KeyError): mc.get_signal_data("transformation_2") # source not present with pytest.raises(KeyError): mc.get_signal_data("not found")