Expected type "Union [ndarray, Iterable]" warning in Python statement

I translated a Matlab function to create an Overcomplete Discrete Cosine Transform matrix to represent 1D signals in such a vector space into Python.

Matlab function

function D = odctdict(n,L)
%ODCTDICT Overcomplete DCT dictionary.
%  D = ODCTDICT(N,L) returns the overcomplete DCT dictionary of size NxL
%  for signals of length N.
%
%  See also ODCT2DICT, ODCT3DICT, ODCTNDICT.    

D = zeros(n,L);
D(:,1) = 1/sqrt(n);
for k = 2:L
  v = cos((0:n-1)*pi*(k-1)/L)';
  v = v-mean(v);
  D(:,k) = v/norm(v);
end

      

Python translation function

import numpy as np


def odct1dict(n, l):
    """
    1-D Overcomplete DCT dictionary.

    D = odct1dict(N, L) returns the overcomplete DCT dictionary of size NxL
    for signals of length N.

    :param n: signal size
    :type n: int
    :param l: number of atoms
    :type l: int
    :return: 1-D Overcomplete DCT dictionary NumPy array
    """

    d = np.zeros((n, l))
    d[:, 0] = 1 / np.sqrt(n)

    for k in range(1, l):
        v = np.transpose(np.cos(np.arange(0, n) * np.pi * k * l))
        v = v - np.mean(v)
        d[:, k] = v / np.linalg.norm(v)

    return d

      

I use both PyCharm IDE Python, and this software is a warning that I do not understand the instructions v = np.transpose(np.cos(np.arange(0, n) * np.pi * k * l))

inside the loop for, in particular, for the argument of the function np.transpose

, np.cos(np.arange(0, n) * np.pi * k * l)

.

The expected type is "Union [ndarray, Iterable]", instead "int" is less ...

This check detects type errors in function call expressions. In connection with dynamic dispatch and duck printing, this is possible in a limited but useful number of cases. You can specify the types of function parameters in docstrings or in Python 3 function annotations.

Can you explain this warning for me? And how do you fix it? What is the correct way to write such an instruction?

+3


source to share


3 answers


My guess is that PyCharm doesn't fully understand numpy. It looks and acts like valid Python:

Using my IDE Ipython

, I can do:



In [84]: n,k,l=3, .4, 1

In [85]: v = np.transpose(np.cos(np.arange(0, n) * np.pi * k * l))

In [86]: v
Out[86]: array([ 1.        ,  0.30901699, -0.80901699])

      

+7


source


These false positives happen a lot with numpy code in PyCharm. In a thread discussing this issue with JetBrains support , they say:

Pretty much any code written in reasonably elegant numpy style is drowned in warning messages.



For the arguments of your own functions, you can write docstrings so that PyCharm knows what type to expect . However, for a multi-valued code, this will not be relevant. There are two solutions I have found:

  • Before the line or function where the warning occurs, issue warnings on the line or each function with the line # noinspection PyTypeChecker

    . For more information on suppressing warnings, see the official guide.
  • Use type hinting like in this answer :

    transpose_arg = np.cos(np.arange(0, n) * np.pi * k * l)  # type: np.ndarray
    v = np.transpose(transpose_arg)
    
          

+5


source


To add to buzjwa's answer:

Option 3: . Use mypy for type checking and manually add the third stub file generated for numpy here

You will need to add this stub file to your internal python python. Let us know how you are progressing!

0


source







All Articles