6

In numpy.testing, there's assert_array_less and assert_array_equal, but there isn't an assert_array_less_equal function or even an assert_array_greater. I have two questions:

  1. Is there a reason these functions are missing, but assert_array_less is not?
  2. I've written my own versions of these missing functions by using numpy.testing.util.assert_array_compare, e.g.:
def assert_array_greater(aa, bb):
  assert_array_compare(np.greater, aa, bb)

Is this safe? I.e. is there a reason why assert_array_compare is hidden away in numpy.testing.util, rather than living in numpy.testing?

Forgive my paranoia; it just seems weird that these functions don't exist, to the extent that I fear it's for some good reason that I shouldn't be working around.

1 Answer 1

7

np.testing is a module that collects tests and tools used by various of the numpy unittesting files. So it's designed more for internal use, than for end user use. So the simple answer would be that those extra tests aren't needed.

But looking at the source code for one of those functions:

def assert_array_less(x, y, err_msg='', verbose=True):
    assert_array_compare(operator.__lt__, x, y, err_msg=err_msg,
                         verbose=verbose,
                         header='Arrays are not less-ordered',
                         equal_inf=False)

It looks like it would be easy to write a variation the uses one of the other operator methods.

The 'root' of np.testing is numpy/testing/__init__.py, which is a short file. Looks like its main task is from .utils import *. This is typical subpackage organization. The __init__ collects the necessary imports, but often doesn't have significant code of its own.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Do you have any guesses as to why they use operator.__lt__ rather than numpy.less? (As in, is there a good reason why I should as well?)
That file only uses operator twice. I think operator.__lt__(a,b) delegates to a.__lt__(b), the same as a<b. np.less is ufunc version, which at some compiled level probably ends up calling the same code, but details like that can be hard to trace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.