问题
I have translated a Matlab function to create an Overcomplete Discrete Cosine Transform matrix to represent 1D signals in such vectorial space, to Python language.
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 translated 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'm using PyCharm as Python IDE, and this software raises a warning, that I don't understand, in the instruction v = np.transpose(np.cos(np.arange(0, n) * np.pi * k * l))
inside the for loop, specifically for the argument of the np.transpose
function, np.cos(np.arange(0, n) * np.pi * k * l)
.
Expected type 'Union[ndarray, Iterable]', got 'int' instead less...
This inspection detects type errors in function call expressions. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Types of function parameters can be specified in docstrings or in Python 3 function annotations.
Can you explain this warning for me? And how to correct it? What is the proper way to write such instruction?
回答1:
I'm guessing that PyCharm does not 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])
回答2:
These false positives happen a lot with numpy code in PyCharm. In a thread discussing this issue with JetBrains support, they say:
Almost any code written in reasonably elegant numpy style gets drowned in warning messages.
For arguments of your own functions you can write docstrings to let PyCharm know what type to expect. For a lot of numpy code however this will not be relevant. There are two solutions I found:
- Suppress the warnings per-line or per-function with the line
# noinspection PyTypeChecker
before the line or function where the warning occurs. See the official guide for more details on suppressing warnings Use type hinting as in this answer:
transpose_arg = np.cos(np.arange(0, n) * np.pi * k * l) # type: np.ndarray v = np.transpose(transpose_arg)
回答3:
To append to buzjwa's answer:
Option 3: Use mypy for type checking and manually add the 3rd party stub file created for numpy here
You will need to add this stub file to your internal python typeshed. Let us know how you get on!
来源:https://stackoverflow.com/questions/30599676/expected-type-unionndarray-iterable-warning-in-python-instruction