From f803620be9b4bce2f8b8e1d6067d3c36b2437e31 Mon Sep 17 00:00:00 2001 From: "Daniel A. Wagenaar" Date: Fri, 15 Nov 2024 17:45:10 -0800 Subject: [PATCH 1/4] Added "plain" property to quantity The new "plain" property returns a copy of the quantity as a plain number provided that the quantity is dimensionless. For example: import quantities as pq t = 2 * pq.ms f = 3 * pq.MHz n = (t*f).plain # n will be 6000 If the quantity is not dimensionless, a conversion error is raised. --- quantities/quantity.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/quantities/quantity.py b/quantities/quantity.py index e6046fd..bf8597b 100644 --- a/quantities/quantity.py +++ b/quantities/quantity.py @@ -250,6 +250,21 @@ def rescale_preferred(self): raise Exception("Preferred units for '%s' (or equivalent) not specified in " "quantites.quantity.PREFERRED." % self.dimensionality) + @property + def plain(self): + """ + Return a copy of the quantity as a plain number provided that the quantity + is dimensionless. For example: + ``` + import quantities as pq + t = 2 * pq.ms + f = 3 * pq.MHz + n = (t*f).plain # n will be 6000 + ``` + If the quantity is not dimensionless, a conversion error is raised. + """ + return self.rescale(unit_registry['dimensionless']).magnitude + @with_doc(np.ndarray.astype) def astype(self, dtype=None, **kwargs): '''Scalars are returned as scalar Quantity arrays.''' From 633cf9e409fbf7ed4cbbaf5ca4fb2a0c829c5cd0 Mon Sep 17 00:00:00 2001 From: "Daniel A. Wagenaar" Date: Tue, 19 Nov 2024 15:10:06 -0800 Subject: [PATCH 2/4] =?UTF-8?q?Renamed=20the=20=E2=80=9Cplain=E2=80=9D=20p?= =?UTF-8?q?roperty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Renamed the originally proposed “plain” property into “dimensionless_magnitude” as suggested. Improved documentation of the property. Added documentation to the “magnitude” property to clarify the distinction between the two. --- quantities/quantity.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/quantities/quantity.py b/quantities/quantity.py index bf8597b..74a425f 100644 --- a/quantities/quantity.py +++ b/quantities/quantity.py @@ -145,6 +145,16 @@ def _reference(self): @property def magnitude(self): + """ + Returns a view onto the numerical value of the quantity, stripping + away the associated units. For example: + ``` + import quantities as pq + t = 2 * pq.millisecond + n = t.magnitude # n will be 2 (not 0.002) + ``` + See also: dimensionless_magnitude. + """ return self.view(type=np.ndarray) @property @@ -251,17 +261,19 @@ def rescale_preferred(self): "quantites.quantity.PREFERRED." % self.dimensionality) @property - def plain(self): + def dimensionless_magnitude(self): """ - Return a copy of the quantity as a plain number provided that the quantity - is dimensionless. For example: + Returns the numerical value of a dimensionless quantity in the form of + a numpy array. Any decimal prefixes are normalized away first. + For example: ``` import quantities as pq t = 2 * pq.ms f = 3 * pq.MHz - n = (t*f).plain # n will be 6000 + n = (t*f).dimensionless_magnitude # n will be 6000 (not 6) ``` If the quantity is not dimensionless, a conversion error is raised. + See also: magnitude. """ return self.rescale(unit_registry['dimensionless']).magnitude From 856db64207e002d7c816c484bf396e9f6da6d746 Mon Sep 17 00:00:00 2001 From: "Daniel A. Wagenaar" Date: Wed, 20 Nov 2024 12:17:14 -0800 Subject: [PATCH 3/4] Added test coverage for dimensionless_magnitude --- quantities/tests/test_methods.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/quantities/tests/test_methods.py b/quantities/tests/test_methods.py index ba184a3..cf548ec 100644 --- a/quantities/tests/test_methods.py +++ b/quantities/tests/test_methods.py @@ -356,3 +356,9 @@ def test_rescale_integer_argument(self): Quantity(10, pq.deg).rescale(pq.rad), np.pi/18*pq.rad ) + + def test_dimensionless_magnitude(self): + self.assertQuantityEqual((self.q / pq.cm).dimensionless_magnitude, + 100 * self.q.magnitude) + self.assertRaises(ValueError, lambda x: x.dimensionless_magnitude, + self.q) From 966deea29e3bc17cb234cb010d23fbe39282065a Mon Sep 17 00:00:00 2001 From: "Daniel A. Wagenaar" Date: Wed, 20 Nov 2024 12:19:25 -0800 Subject: [PATCH 4/4] Added to test coverage for dimensionless_magnitude --- quantities/tests/test_methods.py | 1 + 1 file changed, 1 insertion(+) diff --git a/quantities/tests/test_methods.py b/quantities/tests/test_methods.py index cf548ec..ff84210 100644 --- a/quantities/tests/test_methods.py +++ b/quantities/tests/test_methods.py @@ -358,6 +358,7 @@ def test_rescale_integer_argument(self): ) def test_dimensionless_magnitude(self): + self.assertEqual((pq.kg/pq.g).dimensionless_magnitude, 1000) self.assertQuantityEqual((self.q / pq.cm).dimensionless_magnitude, 100 * self.q.magnitude) self.assertRaises(ValueError, lambda x: x.dimensionless_magnitude,