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

NumPy booleans not supported by param validation #1667

Open
arvoelke opened this issue Feb 13, 2021 · 1 comment
Open

NumPy booleans not supported by param validation #1667

arvoelke opened this issue Feb 13, 2021 · 1 comment

Comments

@arvoelke
Copy link
Contributor

arvoelke commented Feb 13, 2021

Describe the bug

The elements of a NumPy array of dtype=bool actually have type np.bool and not type bool. Using such elements in any place where Nengo expects a boolean (i.e., nengo.params.BoolParam) results in a validation error.

To reproduce

A minimal code snippet to reproduce the behavior:

import nengo

with nengo.Network() as model:
    for flag in np.zeros(1, dtype=bool):
        print(flag)
        nengo.Ensemble(1, 1, normalize_encoders=flag)

Expected behavior

I expect this to create an ensemble with normalize_encoders=False, since a np.bool behaves like a normal Python bool (even though np.bool is not an instance of bool).

Error message and logs

False
...
~/anaconda3/envs/gyrus/lib/python3.8/site-packages/nengo/ensemble.py in __init__(self, n_neurons, dimensions, radius, encoders, intercepts, max_rates, eval_points, n_eval_points, neuron_type, gain, bias, noise, normalize_encoders, label, seed)
    171         self.neuron_type = neuron_type
    172         self.noise = noise
--> 173         self.normalize_encoders = normalize_encoders
    174 
    175     def __getitem__(self, key):

~/anaconda3/envs/gyrus/lib/python3.8/site-packages/nengo/base.py in __setattr__(self, name, val)
    106                 SyntaxWarning,
    107             )
--> 108         super().__setattr__(name, val)
    109 
    110     def __str__(self):

~/anaconda3/envs/gyrus/lib/python3.8/site-packages/nengo/config.py in __setattr__(self, name, val)
    482             except ValidationError as e:
    483                 exc_info = sys.exc_info()
--> 484                 raise exc_info[1].with_traceback(None) from e
    485         else:
    486             super().__setattr__(name, val)

ValidationError: Ensemble.normalize_encoders: Must be of type 'bool' (got type 'bool_').

Versions

  • OS: Ubuntu
  • Python: Anaconda Python 3.8
  • Nengo: nengo==3.1.0
  • Other: numpy==1.17.4

Additional context

This sort of thing can happen when putting Nengo code inside of np.vectorize calls, which will convert bool arguments into np.bool. For example, this raises the same validation error:

with nengo.Network() as model:
    
    @np.vectorize
    def g(flag):
        nengo.Ensemble(1, 1, normalize_encoders=flag)
    
    g([True, False])

This issue was found in connection with nengo/nengo-gyrus#5.

arvoelke added a commit to nengo/nengo-gyrus that referenced this issue Feb 13, 2021
@hunse
Copy link
Collaborator

hunse commented Feb 16, 2021

I don't see a problem with allowing BoolParam to accept both bool and np.bool_.

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

No branches or pull requests

2 participants