From 49468e0857801f51baf046d0c97f0581088f045c Mon Sep 17 00:00:00 2001 From: homosapien-lcy <102019577+homosapien-lcy@users.noreply.github.com> Date: Wed, 22 Mar 2023 16:57:50 +0900 Subject: [PATCH] MAINT: fix unit conversion (#174) The original unit conversion done in propagate_data_changes (scimath.units.quantity) can cause conversion error. This PR adds a fix and test for that. closes issue #171 --------- Co-authored-by: Chengyu Liu --- scimath/units/quantity.py | 3 +-- scimath/units/tests/test_units.py | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/scimath/units/quantity.py b/scimath/units/quantity.py index 30e92c2..53ceedd 100644 --- a/scimath/units/quantity.py +++ b/scimath/units/quantity.py @@ -303,8 +303,7 @@ def propagate_data_changes(self): return # Replace the predecessor's data with converted data. - new_quantity = self.change_unit_system(predecessor.units) - predecessor.data = new_quantity.data + predecessor.data = units_convert(self.data, self.units, predecessor.units) # Recursively continue propagating. predecessor.propagate_data_changes() diff --git a/scimath/units/tests/test_units.py b/scimath/units/tests/test_units.py index 1107d34..82e5de3 100644 --- a/scimath/units/tests/test_units.py +++ b/scimath/units/tests/test_units.py @@ -197,6 +197,15 @@ def test_propagation(self): self.assertAlmostEqual(30., q1.data, 1, "Propagation test expected data 30, got %s" % str(q1.data)) + def test_propagation_to_imperial(self): + """ Tests data propagation for a single converted quantity. """ + + q1 = Quantity(10.0, units='ft', family_name='depth') + q2 = q1.change_unit_system('METRIC') + q2.data = 2 * q2.data + q2.propagate_data_changes() + self.assertAlmostEqual(q1.data, 20.0) + def test_get_original(self): q1 = Quantity(10, units='m', family_name='depth')