Skip to content

Core: Convolve and Correlate mode check in core/numeric.py #17476

@aitikgupta

Description

@aitikgupta

Reproducing code example:

import numpy as np
y = np.convolve(np.ones(1,), np.ones((1,))/1, mode='random_mode')

Error message:

637 def _mode_from_name(mode):                                  <--- numpy/core/numeric.py
638     if isinstance(mode, basestring):
639         return _mode_from_name_dict[mode.lower()[0]]            
640     return mode

KeyError: 'r'

NumPy/Python version information:

1.18.5 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0]

Discussion:

Current modes for numpy.convolve, and numpy.correlate are 'valid', 'same' or 'full'. If there is any other string provided it raises a KeyError with the first letter.
In fact,

import numpy as np
y = np.convolve(np.ones(1,), np.ones((1,))/1, mode='very_random_mode')

Does NOT raise an error. As it is simply mistaken for 'valid' (the very first character is checked)
Think there should be a check here:

def _mode_from_name(mode):
which makes sure the mode is in fact, corresponding to (modified?) keys of

numpy/numpy/core/numeric.py

Lines 665 to 667 in 2eed878

_mode_from_name_dict = {'v': 0,
's': 1,
'f': 2}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions