Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests fails with numpy 1.13.1 #577

Closed
onovy opened this issue Oct 31, 2017 · 22 comments · Fixed by #630
Closed

Unit tests fails with numpy 1.13.1 #577

onovy opened this issue Oct 31, 2017 · 22 comments · Fixed by #630

Comments

@onovy
Copy link
Contributor

onovy commented Oct 31, 2017

Refs: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=876921

Thanks for fixing.

S.S................................................./usr/lib/python2.7/unittest/case.py:340: RuntimeWarning: TestResult has no addExpectedFailure method, reporting as passes
  RuntimeWarning)
.......S...S............................SSSSSSSSSSS.................S...................................................................................................................................................E.E...E.................................................................................................................................................................................................................................................................................................../build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/test_quantity.py:287: RuntimeWarning: to_compact applied to non numerical types has an undefined behavior.
  self.assertQuantityAlmostIdentical(q.to_compact(unit=unit),
.......................................F..F..........................................................................................................................................................................
======================================================================
ERROR: test_inplace_exponentiation (pint.testsuite.test_quantity.TestOffsetUnitMath) [with input = ((10, u'degC'), (2, u'')); expected_output = [u'error', (80173.92249999999, u'kelvin**2')]]
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/parameterized.py", line 116, in new_method
    return method(self, *param_values)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/parameterized.py", line 137, in newfunc
    return func(*arg, **kwargs)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/test_quantity.py", line 1165, in test_inplace_exponentiation
    self.assertEqual(op.ipow(in1_cp, in2).units, expected.units)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/quantity.py", line 961, in __ipow__
    self._magnitude **= _to_magnitude(other, self.force_ndarray)
TypeError: unsupported operand type(s) for ** or pow(): 'numpy.ndarray' and 'Quantity'

======================================================================
ERROR: test_inplace_exponentiation (pint.testsuite.test_quantity.TestOffsetUnitMath) [with input = ((10, u'kelvin'), (2, u'')); expected_output = [(100.0, u'kelvin**2'), (100.0, u'kelvin**2')]]
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/parameterized.py", line 116, in new_method
    return method(self, *param_values)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/parameterized.py", line 137, in newfunc
    return func(*arg, **kwargs)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/test_quantity.py", line 1165, in test_inplace_exponentiation
    self.assertEqual(op.ipow(in1_cp, in2).units, expected.units)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/quantity.py", line 961, in __ipow__
    self._magnitude **= _to_magnitude(other, self.force_ndarray)
TypeError: unsupported operand type(s) for ** or pow(): 'numpy.ndarray' and 'Quantity'

======================================================================
ERROR: test_inplace_exponentiation (pint.testsuite.test_quantity.TestOffsetUnitMath) [with input = ((10, u'degC'), (500.0, u'millikelvin/kelvin')); expected_output = [u'error', (16.827061537891872, u'kelvin**0.5')]]
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/parameterized.py", line 116, in new_method
    return method(self, *param_values)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/parameterized.py", line 137, in newfunc
    return func(*arg, **kwargs)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/test_quantity.py", line 1165, in test_inplace_exponentiation
    self.assertEqual(op.ipow(in1_cp, in2).units, expected.units)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/quantity.py", line 961, in __ipow__
    self._magnitude **= _to_magnitude(other, self.force_ndarray)
TypeError: unsupported operand type(s) for ** or pow(): 'numpy.ndarray' and 'Quantity'

======================================================================
FAIL: test_isfinite (pint.testsuite.test_umath.TestFloatingUfuncs)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/test_umath.py", line 617, in test_isfinite
    (self.q1, self.qm, self.qless))
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/test_umath.py", line 101, in _testn
    self._test1(func, ok_with, raise_with, output_units=None, results=results)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/test_umath.py", line 85, in _test1
    self.assertQuantityAlmostEqual(qm, res, rtol=rtol, msg=err_msg)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/__init__.py", line 117, in assertQuantityAlmostEqual
    np.testing.assert_allclose(m1, m2, rtol=rtol, atol=atol, err_msg=msg)
  File "/usr/lib/python2.7/dist-packages/numpy/testing/utils.py", line 1395, in assert_allclose
    verbose=verbose, header=header, equal_nan=equal_nan)
  File "/usr/lib/python2.7/dist-packages/numpy/testing/utils.py", line 778, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Not equal to tolerance rtol=1e-06, atol=0
At isreal with [ 1.  2.  3.  4.] joule
(mismatch 100.0%)
 x: array(False, dtype=bool)
 y: array([ True,  True,  True,  True], dtype=bool)

======================================================================
FAIL: test_isreal (pint.testsuite.test_umath.TestFloatingUfuncs)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/test_umath.py", line 609, in test_isreal
    (self.q1, self.qm, self.qless))
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/test_umath.py", line 101, in _testn
    self._test1(func, ok_with, raise_with, output_units=None, results=results)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/test_umath.py", line 85, in _test1
    self.assertQuantityAlmostEqual(qm, res, rtol=rtol, msg=err_msg)
  File "/build/1st/python-pint-0.8.1/.pybuild/pythonX.Y_2.7/build/pint/testsuite/__init__.py", line 117, in assertQuantityAlmostEqual
    np.testing.assert_allclose(m1, m2, rtol=rtol, atol=atol, err_msg=msg)
  File "/usr/lib/python2.7/dist-packages/numpy/testing/utils.py", line 1395, in assert_allclose
    verbose=verbose, header=header, equal_nan=equal_nan)
  File "/usr/lib/python2.7/dist-packages/numpy/testing/utils.py", line 778, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Not equal to tolerance rtol=1e-06, atol=0
At isreal with [ 1.  2.  3.  4.] joule
(mismatch 100.0%)
 x: array(False, dtype=bool)
 y: array([ True,  True,  True,  True], dtype=bool)

----------------------------------------------------------------------
Ran 779 tests in 40.510s
@cpascual
Copy link
Contributor

cpascual commented Jan 24, 2018

Is anyone looking into this?
I am concerned because if this is not fixed, pint may be dropped from debian (and our own package, taurus, would be dropped as well because it depends on pint)

@jetheurer
Copy link

Hey @cpascual, I've noticed this error appears in both numpy==1.13.0 and numpy==1.13.1 on Python 2.7 and I believe it is due to the lack of a __array_ufunc__ method on the _Quantity class. I'm going to look into making a fix along these lines.

@hgrecco Did you have any thoughts for this ticket?

@dalito
Copy link
Contributor

dalito commented Feb 14, 2018

Automated testing on Travis seems to be limited to numpy <= 1.11. So these failures are maybe not too surprising. Testing should be extended to include numpy 1.13.x and 1.14.

Numpy 1.13 included improvements to help other projects with subclassing ndarray. Maybe the numpy support in pint can be improved now when using newer numpy versions.

@cpascual
Copy link
Contributor

Hi @jetheurer , any progress?
The deadline for debian packages to be included in Ubuntu LTS 18.04 is 1st of March. I am not sure if https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=876921 precludes pint (and the packages depending on it) to be part of the next Ubuntu LTS...

@jetheurer
Copy link

Hey @cpascual, I don't have anything yet, but I'll try to get something up by Saturday. Thanks for the 1st March info, that deadline is pretty close!

@cpascual
Copy link
Contributor

Hi @jetheurer , do not worry: a debian dev told me that https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=876921 does not mean that pint will be dropped from Ubuntu

@bonderado
Copy link

For reference: pint just dropped off Debian Buster (https://tracker.debian.org/pkg/python-pint)

@df7cb
Copy link

df7cb commented Apr 5, 2018

All packages depending on pint have been removed from Debian Buster/testing last week. A fix would be very welcome.

@cpascual
Copy link
Contributor

cpascual commented Apr 5, 2018

I am going to give this a try myself.
@jetheurer did you get something done?

@jetheurer
Copy link

@cpascual, go for it. I dont have anything to show at the moment.

@cmft
Copy link
Contributor

cmft commented Apr 5, 2018

Hi @jetheurer, @cpascual ,
I have a look on that, and the problems seem to be in:

  • assertQuantityAlmostEqual method (in testsuite.init.py)
    The problem is in np.testing.assert_allclose(m1, m2, rtol=rtol, atol=atol, err_msg=msg)
    The np.testing.assert_allclose seems to be changed. At naked eye the values seems to be equals.

  • self._magnitude **= _to_magnitude(other, self.force_ndarray) ( in pint/quantity.py)
    If you replace this call for this code the problem is solved.

self._magnitude = np.power(self._magnitude,                                                                                                                   
                                             _to_magnitude(other,                                                                                                                       
                                             self.force_ndarray)) 

@jetheurer
Copy link

@cmft That looks like a pretty quick fix. For some reason I thought the issue was more complicated than that - but maybe it wasn't too bad after all!

@coreycb
Copy link

coreycb commented Apr 11, 2018

@cpascual hi, were you planning to submit a patch for this?

@cpascual
Copy link
Contributor

Hi @coreycb , yes, I am working on the patch. The problem is that I am not sure on how to approach it. For the moment I am focusing on the tests that give Error (the ones that test Quantity.ipow). I am also in touch with @cmft (we are co-workers) and we are trying to understand the ultimate cause of the failures with new numpy versions.

@cpascual
Copy link
Contributor

update: I managed to fix the ipow errors and the test_isfinite fail. Now I am working on the test_isreal. Once I get it I'll make the PR

@cpascual
Copy link
Contributor

Update: I finally created the PR. Anyone interested, please have a look at it in #630

bors bot added a commit that referenced this issue Apr 13, 2018
630: Fix unit tests errors with numpy >=1.13 (Fix 577) r=cpascual a=cpascual

This PR fixes the 4 test errors/failiures reported in #577 and adds new travis builds with numpy 1.14

3 of the 4 problems are relatively trivial to solve.

The remaining one (`test_isreal`) is not so trivial because it  involves allowing to compare a *non-dimensionless* quantity against zero. The reason is that `numpy.isreal(x)` essentially does `x.imag !=0` if the `imag` method is implemented in x... and since Quantity reimplements .imag to return units (see #171), the check always returns False if x is a non-dimensionless quantity.

As mentioned, this can be solved by allowing comparisons against zero. Actually, this has already been requested in #194, but it was not implemented.

In this PR, I implemented the comparison-to-zero feature and provide several new unit tests for it. AFAIK this implementation takes into account the concerns expressed by @hgrecco in #194. But if the feature is not wanted, we can just revert the last 3 commits of this PR and then `test_isreal` will left with an expected failure flag.

The rules that I implemented for the comparison-with-zero are:
```    
    a) comparing against (non-quantity) zero is allowed for any quantity
    b) comparing against zero-magnitude quantities of incompatible
    dimensionality raises a Dimensionality error, except for the case
    of equality, for which it always returns False
    
    Notes:
    1.- Numpy arrays of zeros are also supported and the comparison rules
    apply elementwise
    2.- In the case of non-multiplicative units, the rules above
    apply after converting to base units if the autoconvert offset flag
    is set. Otherwise they raise an OffsetUnitCalculusError.
```

Fixes #577


Co-authored-by: Carlos Pascual <[email protected]>
@bors bors bot closed this as completed in #630 Apr 13, 2018
@df7cb
Copy link

df7cb commented Apr 16, 2018

Thanks for the fix! Could we have a new release with it, or should we rather cherry-pick it for the Debian package?

@cpascual
Copy link
Contributor

cpascual commented Apr 16, 2018

Could we have a new release with it,

About this, I cannot say... maybe @hgrecco can say something.

or should we rather cherry-pick it for the Debian package?

I just tested cherry-picking the following 4 commits on top of the 0.8.1 tag : the tests work fine.

@hgrecco
Copy link
Owner

hgrecco commented Apr 16, 2018

There are still several things pending until 0.9 but I could release 0.8.2 if everyone agrees.

@cpascual
Copy link
Contributor

but I could release 0.8.2 if everyone agrees.

That would be nice, and it would simplify packaging in debian...

@onovy
Copy link
Contributor Author

onovy commented Apr 16, 2018

hi, fyi: I already uploaded cherry-picked version of this fix to Debian this morning.

@hgrecco
Copy link
Owner

hgrecco commented Apr 16, 2018

I have just updated CHANGES. Please notice that we will be releasing a 0.8.2 in which we have dropped support for Python 2.6, 3.0, 3.1 and 3.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants