def test_parameters_with_quantity_powers(self): """Test if we can construct and use a Parameter instance passing pint.Quantity and pint.Unit objects to the constructor and interval setter. Use different powers of 10 in parameter initialization and value assignment.""" # Define the base unit of my parameter object. meter = Unit("meter") centimeter = Unit("centimeter") self.assertIsInstance(meter, Unit) minimum_undulator_length = 10.0 * meter undulator_length = Parameter("undulator_length", centimeter) self.assertIsInstance(undulator_length, Parameter) self.assertEqual(undulator_length.unit, Unit("centimeter")) undulator_length.add_interval( min_value=minimum_undulator_length, max_value=numpy.inf * meter, intervals_are_legal=True, ) print(undulator_length) self.assertTrue(undulator_length.is_legal(10.1 * meter)) self.assertFalse(undulator_length.is_legal(9.0 * centimeter)) self.assertTrue(undulator_length.is_legal(5.5e4 * Unit("centimeter")))
def test_units_assignment(self): par = Parameter("test", unit="kg") assert par.unit == Unit("kg") par.unit = "cm" assert par.unit == Unit("cm") par.unit = "meter" assert par.unit == Unit("m") assert par.unit == str(Unit("m")) assert par.unit == "meter" par.unit = "nounit" assert par.unit == "nounit"
def print_line(self) -> str: """ returns string with one line description of parameter """ if self.__unit is None or self.__unit == Unit(""): unit_string = "" else: unit_string = "[" + str(self.__unit) + "] " if self.value_no_conversion is None: string = self.name.ljust(40) + " " else: string = self.name.ljust(35) + " " string += str(self.value).ljust(10) + " " string += unit_string.ljust(20) + " " if self.comment is not None: string += self.comment string += 3 * " " for interval in self.__intervals: legal = "L" if self.__intervals_are_legal else "I" intervalstr = legal + "[" + str(interval[0]) + ", " + str(interval[1]) + "]" string += intervalstr.ljust(10) if len(self.__options) > 0: values = "(" for option in self.__options: values += str(option) + ", " values = values.strip(", ") values += ")" string += values return string
def test_values_different_units(self): par = Parameter("energy", unit="meV", comment="Energy of emitted particles") import pint ureg = pint.UnitRegistry() with pytest.raises(pint.errors.DimensionalityError): thisunit = Unit("meter") par.value = 5 * thisunit
def unit(self, uni: str) -> None: """ Assignment of the units :param uni: unit A pint.Unit is used if the string is recognized as a valid unit in the registry. It is stored as a string otherwise. """ try: self.__unit = Unit(uni) except pint.errors.UndefinedUnitError: self.__unit = uni
def check_axes_calibration(ax1, ax2, rtol=1e-7): """Check if the calibration of two Axis objects matches. Raises a logger warning if there is a mismatch. ``scale`` and ``offset`` are compared as floats using np.allclose, while ``units`` is compared with a simple inequality (!=). Parameters ---------- ax1, ax2 : Axis objects Axes objects that should be compared. rtol : float Tolerance passed to `np.allclose` for comparison. Default 1e-7. Returns ------- bool If the two axes have identical calibrations. """ if ax1.size == ax2.size: try: unit1 = Unit(ax1.units) except: unit1 = ax1.units pass try: unit2 = ax2.units unit2 = Unit(ax2.units) except: pass if np.allclose(ax1.axis, ax2.axis, atol=0, rtol=rtol) and unit1 == unit2: return True return False
def __init__( self, name: str, unit: str = "", comment: Union[str, None] = None, ): """ Creates parameter with given name, optionally unit and comment :param name: name of the parameter :param unit: physical units returning the parameter value :param comment: brief description of the parameter """ self.name: str = name self.__unit: Union[str, Unit] = Unit(unit) if unit != None else "" self.comment: Union[str, None] = comment self.__value: Union[ValueTypes, None] = None self.__intervals: List[Tuple[Quantity, Quantity]] = [] self.__intervals_are_legal: Union[bool, None] = None self.__options: List = [] self.__options_are_legal: Union[bool, None] = None self.__value_type: Union[ValueTypes, None] = None
def test_initialize_parameter_complex(self): par = Parameter("test", unit="cm", comment="comment string") self.assertEqual(par.name, "test") assert par.unit == str(Unit("cm")) self.assertEqual(par.comment, "comment string")