Brian2.test() reports a TypeError in test_numpy_functions_logical

Description of problem

brian2.test() reports:
TypeError: logical_and() takes from 2 to 3 positional arguments but 1 were given

FAILED ../../../../../../opt/anaconda/envs/TheBrianBlank/lib/python3.9/site-packages/brian2/tests/test_units.py::test_numpy_functions_logical

Minimal code to reproduce problem

import brian2
brian2.test()

What you have aready tried

I first got the error in a pip managed environment (no anaconda).
I installed anaconda and set up a new environment. I followed the installation guide
conda config --add channels conda-forge
conda install matplotlib pytest ipython notebook brian2 sphinx doctest

But i still get the same error

Expected output

OK

Actual output

False

Full traceback of error

Running tests in /opt/anaconda/envs/TheBrianBlank/lib/python3.9/site-packages/brian2 for targets numpy, cython (excluding long tests)
Running Brian version 2.4.2 from ‘/opt/anaconda/envs/TheBrianBlank/lib/python3.9/site-packages/brian2’
Testing codegen-independent code
Resetting to default preferences

Running doctests
… [100%]
=============================== warnings summary ===============================
core/operations.py::brian2.core.operations.network_operation
/opt/anaconda/envs/TheBrianBlank/lib/python3.9/site-packages/brian2/core/clocks.py:95: DeprecationWarning: np.float is a deprecated alias for the builtin float. To silence this warning, use float by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.float64 here.
Deprecated in NumPy 1.20; for more details and guidance: (link removed)
dtype=np.float, read_only=True, constant=True,

monitors/spikemonitor.py::brian2.monitors.spikemonitor.EventMonitor.all_values
monitors/spikemonitor.py::brian2.monitors.spikemonitor.EventMonitor.values
monitors/spikemonitor.py::brian2.monitors.spikemonitor.SpikeMonitor
monitors/spikemonitor.py::brian2.monitors.spikemonitor.SpikeMonitor.all_values
monitors/spikemonitor.py::brian2.monitors.spikemonitor.SpikeMonitor.values
/opt/anaconda/envs/TheBrianBlank/lib/python3.9/site-packages/brian2/groups/neurongroup.py:313: DeprecationWarning: np.bool is a deprecated alias for the builtin bool. To silence this warning, use bool by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.bool_ here.
Deprecated in NumPy 1.20; for more details and guidance: (link removed)
self.variables.add_auxiliary_variable(‘_cond’, dtype=np.bool)

monitors/spikemonitor.py::brian2.monitors.spikemonitor.EventMonitor.all_values
monitors/spikemonitor.py::brian2.monitors.spikemonitor.EventMonitor.all_values
monitors/spikemonitor.py::brian2.monitors.spikemonitor.EventMonitor.values
monitors/spikemonitor.py::brian2.monitors.spikemonitor.SpikeMonitor.all_values
monitors/spikemonitor.py::brian2.monitors.spikemonitor.SpikeMonitor.all_values
monitors/spikemonitor.py::brian2.monitors.spikemonitor.SpikeMonitor.spike_trains
monitors/spikemonitor.py::brian2.monitors.spikemonitor.SpikeMonitor.values
/opt/anaconda/envs/TheBrianBlank/lib/python3.9/site-packages/brian2/groups/group.py:257: DeprecationWarning: np.bool is a deprecated alias for the builtin bool. To silence this warning, use bool by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.bool_ here.
Deprecated in NumPy 1.20; for more details and guidance: (link removed)
if index_array.dtype == np.bool:

– Docs: (link removed)
41 passed, 13 warnings in 7.26s
Running tests that do not use code generation

WARNING The expression “i1 / 1” divides two integer values. In previous versions of Brian, this would have used either an integer (“flooring”) or a floating point division, depending on the Python version and the code generation target. In the current version, it always uses a floating point division. Explicitly ask for an integer division (“//”), or turn one of the operands into a floating point value (e.g. replace “1/2” by “1.0/2”) to no longer receive this warning. [brian2.parsing.bast.floating_point_division]

… [ 19%]
… [ 39%]

WARNING The expression “(i + 1) / N” divides two integer values. In previous versions of Brian, this would have used either an integer (“flooring”) or a floating point division, depending on the Python version and the code generation target. In the current version, it always uses a floating point division. Explicitly ask for an integer division (“//”), or turn one of the operands into a floating point value (e.g. replace “1/2” by “1.0/2”) to no longer receive this warning. [brian2.parsing.bast.floating_point_division]

…ss… [ 59%]

WARNING The ‘independent’ state updater is deprecated and might be removed in future versions of Brian. [brian2.stateupdaters.exact.deprecated_independent]

… [ 79%]
…F… [ 99%]
… [100%]
=================================== FAILURES ===================================
_________________________ test_numpy_functions_logical _________________________

@pytest.mark.codegen_independent
def test_numpy_functions_logical():
    '''
    Assure that logical numpy functions work on all quantities and return
    unitless boolean arrays.
    '''
    unit_values1 = [3 * mV, np.array([1, 2]) * mV, np.ones((3, 3)) * mV]
    unit_values2 = [3 * second, np.array([1, 2]) * second,
                    np.ones((3, 3)) * second]
    for ufunc in UFUNCS_LOGICAL:
        for value1, value2 in zip(unit_values1, unit_values2):
            try:
                # one argument
              result_units = eval('np.%s(value1)' % ufunc)

/opt/anaconda/envs/TheBrianBlank/lib/python3.9/site-packages/brian2/tests/test_units.py:976:


???
E TypeError: logical_and() takes from 2 to 3 positional arguments but 1 were given

:1: TypeError

… Output continues but the rest is Ok. (I had to remove the links in the output since i am a new user)

Seems like the functions logical_and, logical_or and logical_xor raise a TypeError in case there is a missing argument:

for ufunc in UFUNCS_LOGICAL:
    for value1, value2 in zip(unit_values1, unit_values2):
        try:
            # one argument
            result_units = eval('np.%s(value1)' % ufunc)        
            result_array = eval('np.%s(np.array(value1))' % ufunc)
        except (ValueError, TypeError) as e:
            print(ufunc, type(e))

OUTPUT:

logical_and <class ‘TypeError’>
logical_and <class ‘TypeError’>
logical_and <class ‘TypeError’>
logical_or <class ‘TypeError’>
logical_or <class ‘TypeError’>
logical_or <class ‘TypeError’>
logical_xor <class ‘TypeError’>
logical_xor <class ‘TypeError’>
logical_xor <class ‘TypeError’>

I replaced line 978 in test_units.py
except ValueError:
with:
except TypeError:

This fixes the bug for me. Is this a general thing ?
(My package versions are numpy 1.21.2 and brian2 2.4.2)

best,
Moritz

Hi @moritz,

the test has already been fixed here in brian2 master and should pass in the next release. It seems the latest numpy version has changed the error type raised here. I think you can safely ignore the failed test.

Best,
Denis

2 Likes

Perfect, Thank you!

best,
Moritz