From 31073fbcd21541b3994538878936681c889ac263 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Thu, 17 Nov 2016 10:15:46 +0000 Subject: [PATCH 1/3] Add a test exposing some bugs when context=None It seems that a numpy update has fixed test_maximum? --- pint/testsuite/test_numpy.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index ea23c8e00..3c365fa27 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -422,9 +422,8 @@ def test_right_shift(self): 'same') -class TestNDArrayQunatityMath(QuantityTestCase): - - @helpers.requires_numpy() +@helpers.requires_numpy() +class TestNDArrayQuantityMath(QuantityTestCase): def test_exponentiation_array_exp(self): arr = np.array(range(3), dtype=np.float) q = self.Q_(arr, None) @@ -441,7 +440,6 @@ def test_exponentiation_array_exp(self): self.assertRaises(DimensionalityError, op_, q_cp, q2_cp) @unittest.expectedFailure - @helpers.requires_numpy() def test_exponentiation_array_exp_2(self): arr = np.array(range(3), dtype=np.float) #q = self.Q_(copy.copy(arr), None) @@ -456,3 +454,21 @@ def test_exponentiation_array_exp_2(self): arr_cp = copy.copy(arr) q_cp = copy.copy(q) self.assertRaises(DimensionalityError, op.ipow, arr_cp, q_cp) + + def test_maximum(self): + a = np.arange(4) * self.ureg.m + b = 2 * np.ones(4) * self.ureg.m + # this line would previously throw an error + x = np.maximum(a, b) + # TODO + # self.assertEqual(x.units, ureg.m * ureg.s) + + @unittest.expectedFailure + def test_linalg_solve(self): + ureg = self.ureg + A = np.eye(2) * ureg.s + b = np.ones(2) * ureg.m + # this line would previously throw an error + x = np.linalg.solve(A, b) + # TODO + # self.assertEqual(x.units, ureg.m * ureg.s) From e009f55adb7b8fdac4c6f5dc88c89fa5ab43bac3 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Tue, 9 Aug 2016 12:10:34 -0700 Subject: [PATCH 2/3] Prevent crash when calling functions from linalg Which call __array_prepare__ with context as None --- pint/quantity.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pint/quantity.py b/pint/quantity.py index 16b1dea3e..e0c551f63 100644 --- a/pint/quantity.py +++ b/pint/quantity.py @@ -1224,6 +1224,8 @@ def tolist(self): def __array_prepare__(self, obj, context=None): # If this uf is handled by Pint, write it down in the handling dictionary. + if context is None: + return obj # name of the ufunc, argument of the ufunc, domain of the ufunc # In ufuncs with multiple outputs, domain indicates which output From 2cccee1de17c2afe2e07881d10d9b9b42f56edbb Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Thu, 17 Nov 2016 10:28:19 +0000 Subject: [PATCH 3/3] Fix TypeError: 'NoneType' object is not iterable --- pint/quantity.py | 6 +++++- pint/testsuite/test_numpy.py | 1 - 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pint/quantity.py b/pint/quantity.py index e0c551f63..448d34091 100644 --- a/pint/quantity.py +++ b/pint/quantity.py @@ -1223,7 +1223,7 @@ def tolist(self): __array_priority__ = 17 def __array_prepare__(self, obj, context=None): - # If this uf is handled by Pint, write it down in the handling dictionary. + # If we have no context, assume this is unhandled if context is None: return obj @@ -1246,6 +1246,10 @@ def __array_prepare__(self, obj, context=None): return obj def __array_wrap__(self, obj, context=None): + # If we have no context, assume this is unhandled + if context is None: + return self.magnitude.__array_wrap__(obj, context) + uf, objs, huh = context # if this ufunc is not handled by Pint, pass it to the magnitude. diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index 3c365fa27..e3030fa6d 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -463,7 +463,6 @@ def test_maximum(self): # TODO # self.assertEqual(x.units, ureg.m * ureg.s) - @unittest.expectedFailure def test_linalg_solve(self): ureg = self.ureg A = np.eye(2) * ureg.s