def testArrayOperations(unit_database_len_time) -> None: unit_database = unit_database_len_time m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])])) km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])])) s1: Array[List[float]] = Array.CreateWithQuantity(m, [1]) s2: Array[List[float]] = Array.CreateWithQuantity(km_city, [0.01]) initial1 = s1.GetQuantity().GetComposingUnits() initial2 = s2.GetQuantity().GetComposingUnits() # Check that they doesn't raise ComposedUnitError s1.GetValues() s2.GetValues() quantity, value = unit_database.Multiply(m, km_city, 1, 0.01) assert initial1 == s1.GetQuantity().GetComposingUnits() assert initial2 == s2.GetQuantity().GetComposingUnits() calculated1: Array[List[float]] = Array.CreateWithQuantity(quantity, [value]) array = s1 * s2 str(array) # just to see if it works... assert calculated1 == s1 * s2 quantity, value = unit_database.Sum(m, km_city, 1, 0.01) assert Array.CreateWithQuantity(quantity, [value]) == s1 + s2 quantity, value = unit_database.Subtract(m, km_city, 1, 0.01) assert Array.CreateWithQuantity(quantity, [value]) == s1 - s2
def testDivision(unit_database_len_time): unit_database = unit_database_len_time m = Quantity.CreateDerived(odict([("Table size", ["m", 1])])) km_city = Quantity.CreateDerived(odict([("City size", ["km", 1])])) quantity, value = unit_database.Divide(m, km_city, 1, 0.01) calculated1 = Array.CreateWithQuantity(quantity, [value]) s1 = Array.CreateWithQuantity(m, [1]) s2 = Array.CreateWithQuantity(km_city, [0.01]) assert calculated1 == s1 / s2
def testDivision(unit_database_len_time) -> None: unit_database = unit_database_len_time m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])])) km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])])) quantity, value = unit_database.Divide(m, km_city, 1, 0.01) calculated1: Array[List[float]] = Array.CreateWithQuantity(quantity, [value]) s1: Array[List[float]] = Array.CreateWithQuantity(m, [1]) s2: Array[List[float]] = Array.CreateWithQuantity(km_city, [0.01]) assert calculated1 == s1 / s2
def testDivision(unit_database_len_time) -> None: unit_database = unit_database_len_time m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])])) km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])])) quantity, value = unit_database.Divide(m, km_city, 1, 0.01) calculated1 = Scalar.CreateWithQuantity(quantity, value) s1 = Scalar.CreateWithQuantity(m, 1) s2 = Scalar.CreateWithQuantity(km_city, 0.01) assert calculated1 == s1 / s2
def testCreationWithDerivedQuantity(unit_database_len_time): unit_database = unit_database_len_time m = Quantity.CreateDerived(odict([("Table size", ["m", 1])])) km_city = Quantity.CreateDerived(odict([("City size", ["km", 1])])) quantity, value = unit_database.Multiply(m, km_city, 1, 0.01) calculated1 = Scalar.CreateWithQuantity(quantity, value) assert six.text_type(calculated1) s1 = Scalar.CreateWithQuantity(m, 1) s2 = Scalar.CreateWithQuantity(km_city, 0.01) assert calculated1 == s1 * s2
def testDeepcopy(unit_database_len_time): import copy # Note: the part below is flaky for a test because it relies on getting a refcount to None # which could change if there are other threads running. The code is kept just as a # reference in case we have to debug such a situation again (there was a bug in odict where # it decreased references to None when it shouldn't and it crashed the program later on). # m = odict([('Table size', ['m', 1])]) # import gc # import sys # my_none = None # for i in range(100): # gc.collect() # # Note: # for i in range(200): # gc.collect() # if i < 100: # prev = sys.getrefcount(my_none) # else: # curr = sys.getrefcount(my_none) # self.assert_(curr >= prev, # 'The ref count cannot get lower for None (previous:%s current:%s).' % (prev, curr)) # # # Notice that print sys.getrefcount(None) is always decrementing (this is the error) # m = copy.deepcopy(m) m = Quantity.CreateDerived(odict([("Table size", ["m", 1])])) m0 = copy.deepcopy(m) assert m is m0 # Check if our cache is working.
def testValidUnits(unit_database_empty): # let'scalar clear up the unit manager unit_database = unit_database_empty unit_database.AddUnitBase("length", "meters", "m") unit_database.AddUnit("length", "centimeters", "cm", "%f * 100.0", "%f / 100.0") unit_database.AddUnit("length", "milimeters", "mm", "%f * 1000.0", "%f / 1000.0") unit_database.AddUnit("length", "kilometers", "km", "%f / 1000.0", "%f * 1000.0") unit_database.AddUnit("length", "miles", "mi", "%f / 1609.347", "%f * 1609.347") unit_database.AddUnit("length", "inches", "in", "%f / 0.0254", "%f * 0.0254") valid_units = ["m", "cm", "mm"] unit_database.AddCategory("length", "length") unit_database.AddCategory("well-length", "length", valid_units=valid_units) unit_database.AddCategory("well-diameter", "length") Quantity("well-length", "km") scalar = units.Scalar("well-length", 1, "m") assert valid_units == scalar.GetValidUnits() scalar = units.Scalar("well-diameter", 1, "m") assert ["m", "cm", "mm", "km", "mi", "in"] == scalar.GetValidUnits() # Creating a scalar in a unit that isn't valid shouldn't raise an error and the unit # will be added to valid units scalar = units.Scalar("well-length", 1, "mi") assert scalar.GetValidUnits() == ["m", "cm", "mm", "mi"]
def testGetValidUnitsEmptyQuantity(unit_database_custom_conversion) -> None: """ Test that retrieving the valid units for the category of the empty quantity (as created by Quantity.CreateEmpty() correctly returns an empty list. """ unit_database = unit_database_custom_conversion quantity = Quantity.CreateEmpty() assert unit_database.GetValidUnits(quantity.GetCategory()) == []
def testCreateWithQuantity(unit_database_start_units) -> None: units.FixedArray.CreateWithQuantity(Quantity.CreateDerived(OrderedDict()), [100, 150, 200], dimension=3) quantity = ObtainQuantity("m", "length") a1 = units.FixedArray(3, quantity, values=[1, 2, 3]) a2 = units.FixedArray(3, quantity, values=[1, 2, 3]) assert a1.GetValues("km") == [0.001, 0.002, 0.003] assert a2.GetValues() == [1, 2, 3]
def testDerivedQuantities(unit_database_len_time): # define a simple quantity ObtainQuantity(unit="s", category="Time") # see if it works ObtainQuantity(unit="m", category="Table size") # see if it works q3 = Quantity.CreateDerived( odict([("Table size", ["m", 2]), ("Time", ["s", -1])])) q4 = Quantity.CreateDerived( odict([("Table size", ["m", 2]), ("Time", ["s", -2])])) q5 = Quantity.CreateDerived( odict([("Table size", ["m", 1]), ("City size", ["m", 1]), ("Time", ["s", -2])])) q6 = Quantity.CreateDerived(odict([("Time", ["s", -2])])) q7 = Quantity.CreateDerived( odict([("Table size", ["m", 1]), ("Time", ["s", 2])])) with pytest.raises(InvalidUnitError): Quantity.CreateDerived( odict([ ("Table size", ["invalid", 1]), ("City size", ["m", 1]), ("Time", ["s", -2]), ])) assert "(Table size) ** 2 / Time" == q3.GetCategory() assert "m2/s" == q3.GetUnit() assert "(Table size) ** 2 / (Time) ** 2" == q4.GetCategory() assert "m2/s2" == q4.GetUnit() assert "1/s2" == q6.GetUnit() assert "m.s2" == q7.GetUnit() assert (("m", 2), ("s", -2)) == q4.GetComposingUnits() assert (("m", 1), ("m", 1), ("s", -2)) == q5.GetComposingUnits()
def testConvertionWithDerivedUnits(unit_database_len_time): empty = Quantity.CreateDerived(OrderedDict()) m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])])) m_city = Quantity.CreateDerived(OrderedDict([("City size", ["m", 1])])) cm = Quantity.CreateDerived(OrderedDict([("Table size", ["cm", 1])])) km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])])) m2 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2])])) s = Quantity.CreateDerived(OrderedDict([("Time", ["s", -1])])) m2s = Quantity.CreateDerived( OrderedDict([("Table size", ["m", 2]), ("Time", ["s", -1])])) cat_mix_m2 = Quantity.CreateDerived( OrderedDict([("Table size", ["m", 1]), ("City size", ["m", 1])])) unit_database = unit_database_len_time # multiplication assert (m2, 2) == unit_database.Multiply(m, m, 1, 2) assert (m2s, 2) == unit_database.Multiply(m2, s, 1, 2) assert (m2s, 2) == unit_database.Multiply(m2, s, 1, 2) assert (m2, 1) == unit_database.Multiply(m, cm, 1, 100) assert (cat_mix_m2, 1) == unit_database.Multiply(m, m_city, 1, 1) assert (cat_mix_m2, 1) == unit_database.Multiply(m, km_city, 1, 0.001) # division assert (m, 0.001) == unit_database.Divide(cat_mix_m2, km_city, 1, 1) # check division with cancelling units (and different categories) assert (empty, 1) == unit_database.Divide(m, m_city, 1, 1) # floor division assert (m, 3.0) == unit_database.FloorDivide(cat_mix_m2, km_city, 3.5, 0.001) # sum assert (m, 1 + 0.01) == unit_database.Sum(m, cm, 1, 1) assert (m, 2) == unit_database.Sum(m, m_city, 1, 1) # subtraction # check with operation so that we have an exact match (without need for almost equals, # as that should be the same exact operation done later) assert (m, 1 - 0.01) == unit_database.Subtract(m, cm, 1, 1) with pytest.raises(InvalidOperationError): unit_database.Subtract(m, s, 1, 1)
def testQuantityCaption(unit_database_posc_len) -> None: unit_database = unit_database_posc_len unit_database.AddUnitBase(UNKNOWN_QUANTITY_TYPE, UNKNOWN_QUANTITY_TYPE, UNKNOWN_UNIT) unit_database.AddCategory(UNKNOWN_QUANTITY_TYPE, UNKNOWN_QUANTITY_TYPE) q0 = ObtainQuantity(unit=UNKNOWN_UNIT, category=UNKNOWN_QUANTITY_TYPE) assert "" == q0.GetUnknownCaption() q = ObtainQuantity( unit=UNKNOWN_UNIT, unknown_unit_caption="Feeeet", category=UNKNOWN_QUANTITY_TYPE ) assert "Feeeet" == q.GetUnknownCaption() assert "Feeeet <unknown>" == q.GetUnitCaption() q = Quantity.CreateDerived(OrderedDict([("length", ("m", 1))]), unknown_unit_caption="Feeeet") assert "m" == q.GetUnitCaption()
def testNumberInteractions(unit_database_len_time): scalar = Scalar("Table size", 1, "m") scalar2 = Scalar.CreateWithQuantity(Quantity.CreateDerived(odict()), 0) assert scalar == scalar + scalar2 assert scalar == scalar + 0 assert scalar == 0 + scalar assert 9 == (10 - scalar).value assert -9 == (scalar - 10).value assert 10 == (10 * scalar).value assert 10 == (scalar * 10).value assert 10 == (10.0 / scalar).value assert 1 / 10.0 == (scalar / 10.0).value
def testNumberInteractions(unit_database_len_time): import numpy m = Quantity.CreateDerived(odict([("Table size", ["m", 1])])) s1 = Array.CreateWithQuantity(m, list(range(10))) s2 = Array.CreateWithQuantity(m, [x + x for x in range(10)]) assert s1 == 0 + s1 assert s1 == s1 + 0 assert s2 == s1 + s1 num_arr = Array.CreateWithQuantity(m, numpy.array([x for x in range(10)])) sum_array = num_arr + s1 assert isinstance(sum_array.values, numpy.ndarray) sum_array2 = num_arr + numpy.array([x for x in range(10)]) assert isinstance(sum_array2.values, numpy.ndarray) tup_arr = Array.CreateWithQuantity(m, tuple([x for x in range(10)])) tup_arr = tup_arr + 1 assert isinstance(tup_arr.values, tuple)
def testReadOnlyOperation(unit_database_len_time): unit_database = unit_database_len_time m_ro = ObtainQuantity("m", "Table size") m_rw = ObtainQuantity("m", "Table size") m2 = Quantity.CreateDerived(odict([("Table size", ["m", 2])])) # multiplication assert (m2, 2) == unit_database.Multiply(m_rw, m_rw, 1, 2) assert (m2, 2) == unit_database.Multiply(m_ro, m_rw, 1, 2) assert (m2, 2) == unit_database.Multiply(m_ro, m_ro, 1, 2) assert (m2, 2) == unit_database.Multiply(m_rw, m_ro, 1, 2) quantity, _ = unit_database.Multiply(m_ro, m_ro, 1, 2) assert isinstance(quantity, Quantity) quantity, _ = unit_database.Multiply(m_rw, m_rw, 1, 2) assert isinstance(quantity, Quantity) quantity, _ = unit_database.Multiply(m_rw, m_ro, 1, 2) assert isinstance(quantity, Quantity) quantity, _ = unit_database.Multiply(m_ro, m_rw, 1, 2) assert isinstance(quantity, Quantity)
def testDerivedUnits(unit_database_empty): unit_database = unit_database_empty unit_database.AddUnitBase("length", "meters", "m") unit_database.AddUnit("length", "milimeters", "mm", "x * 1000.0", "x / 1000.0") unit_database.AddUnit("length", "centimeters", "cm", "x * 100.0", "x / 100.0") unit_database.AddCategory("well-diameter", "length") unit_database.AddUnitBase("time", "seconds", "s") unit_database.AddCategory(category="Time", quantity_type="time") s1 = Scalar("well-diameter", 10, "m") s2 = Scalar("well-diameter", 10, "cm") s3 = Scalar("Time", 10, "s") assert Scalar("well-diameter", 10.10, "m") == s1 + s2 assert Scalar("well-diameter", 9.90, "m") == s1 - s2 quantity = Quantity.CreateDerived(odict()) assert Scalar.CreateWithQuantity(quantity, 100) == s1 / s2 assert Scalar("well-diameter", 9.90, "m") == s1 - s2 with pytest.raises(InvalidOperationError): s1.__sub__(s3)
def testNumberInteractions(unit_database_len_time) -> None: import numpy m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])])) s1: Array[List[int]] = Array.CreateWithQuantity(m, list(range(10))) s2: Array[List[int]] = Array.CreateWithQuantity(m, [x + x for x in range(10)]) assert s1 == 0 + s1 assert s1 == s1 + 0 assert s2 == s1 + s1 num_arr: Array[numpy.ndarray[numpy.int64]] = Array.CreateWithQuantity( m, numpy.array([x for x in range(10)]) ) sum_array = num_arr + s1 assert isinstance(sum_array.values, numpy.ndarray) sum_array2: Array[numpy.ndarray[numpy.int64]] = num_arr + numpy.array([x for x in range(10)]) assert isinstance(sum_array2.values, numpy.ndarray) tup_arr: Array[Tuple[int]] = Array.CreateWithQuantity(m, tuple([x for x in range(10)])) tup_arr = tup_arr + 1 assert isinstance(tup_arr.values, tuple) assert tup_arr.values == (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
def testQuantity(unit_database_well_length): Quantity("well-length", "m") with pytest.raises(units.InvalidQuantityTypeError): Quantity("foo", "m")