Exemple #1
0
def testNumpyWithMinMax(unit_database_custom_conversion) -> None:
    unit_database = unit_database_custom_conversion
    unit_database.AddCategory(
        "my length",
        "length",
        ["mm", "m"],
        default_unit="m",
        default_value=2,
        min_value=0,
        max_value=15,
        is_min_exclusive=False,
        is_max_exclusive=False,
        caption="My Length",
    )
    import numpy

    values = numpy.array(list(range(10)), numpy.float32)
    Array("my length", values, "mm")

    values = numpy.array(list(range(20000, 20010)), numpy.float32)
    # Raise validation error
    another = Array("my length", values, "mm")
    assert not another.IsValid()
    with pytest.raises(ValueError):
        another.CheckValidity()

    # Don't raise validation error if validate = False.
    arr = Array("my length", values, "mm")
    assert not arr.IsValid()

    another = arr.CreateCopy(values=values, unit="mm")
    assert not another.IsValid()

    # Same checks on FixedArray.
    values = numpy.array(list(range(10)), numpy.float32)
    FixedArray(len(values), "my length", values, "mm")

    values = numpy.array(list(range(20000, 20010)), numpy.float32)
    # Raise validation error
    another = FixedArray(len(values), "my length", values, "mm")
    assert not another.IsValid()

    # Don't raise validation error if validate = False.
    arr = FixedArray(len(values), "my length", values, "mm")
    assert not arr.IsValid()

    another = arr.CreateCopy(values=values, unit="mm")
    assert not another.IsValid()
Exemple #2
0
    def ChangingIndex(self, index, value, use_value_unit=True):
        """
        Creates a FixedArray from based on this FixedArray changing a single value based on the
        passed index.

        i.e.: array.ChangingIndex(0, Scalar(1.0, 'm'))
              will create a new FixedArray where the index == 0 is changed to the passed value.

        :param Scalar|float|tuple value:
            The value to be used to set at the given index.

        :param int index:
            The index which should be changed.

        :param bool use_value_unit:
            If True and a Scalar is passed, the newly created array will have the unit of the
            scalar, not of the original array, otherwise (if False) the FixedArray unit will be
            kept.

        :return FixedArray:
            The created FixedArray.
        """
        from barril.units import Scalar
        from barril.units import FixedArray

        if isinstance(value, tuple):
            scalar = Scalar(self.GetValues()[index],
                            self.GetUnit()).CreateCopy(*value)

        elif not isinstance(value, Scalar):
            scalar = Scalar(value, self.GetUnit())

        else:
            scalar = value

        if use_value_unit:
            quantity = scalar.GetQuantity()
        else:
            quantity = self.GetQuantity()

        values = list(self.GetValues(quantity.GetUnit()))
        values[index] = scalar.GetValue(quantity.GetUnit())
        return FixedArray(self.dimension, quantity, tuple(values))
Exemple #3
0
def testUnitSystemManager(unit_manager, units_mapping_1, units_mapping_2,
                          units_mapping_3):
    CreateUnitSystemTemplate(unit_manager)
    unit_manager.AddUnitSystem("system1", "system1", units_mapping_1, False)
    unit_manager.AddUnitSystem("system2", "system2", units_mapping_2, False)

    with pytest.raises(UnitSystemIDError):
        unit_manager.AddUnitSystem("system1", "system3", units_mapping_3,
                                   False)

    with pytest.raises(UnitSystemCategoriesError):
        unit_manager.AddUnitSystem("system3", "system3", units_mapping_3,
                                   False)

    scalar = Scalar("length", unit="cm")
    array = Array("length", unit="cm")
    fixed_array = FixedArray(3, "length", unit="cm")
    fraction_scalar = FractionScalar("length")

    unit_systems = unit_manager.GetUnitSystems()
    system1 = unit_systems["system1"]
    system2 = unit_systems["system2"]

    unit_manager.current = system1
    assert unit_manager.GetUnitSystemById("system1") is system1
    assert unit_manager.GetUnitSystemById("system2") is system2

    # make sure the unit system manager is not holding a strong ref to its objects
    scalar_ref = weakref.ref(scalar)
    array_ref = weakref.ref(array)
    fixed_array_ref = weakref.ref(fixed_array)
    fraction_scalar_ref = weakref.ref(fraction_scalar)

    del scalar
    del array
    del fixed_array
    del fraction_scalar

    assert scalar_ref() is None
    assert array_ref() is None
    assert fixed_array_ref() is None
    assert fraction_scalar_ref() is None