From c5255cc25313f98f96b47fb32682e472a7d78991 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Thu, 2 Apr 2015 16:10:26 +0300 Subject: [PATCH] added tests for PY-15295 --- .../NumpyDocStringTypeProvider.java | 2 +- .../inspections/PyNumpyType/ArgSort.py | 89 +++++++++++++ .../testData/inspections/PyNumpyType/Empty.py | 46 +++++++ .../inspections/PyNumpyType/Transpose.py | 47 +++++++ .../inspections/PyNumpyType/Vectorize.py | 122 ++++++++++++++++++ .../python/inspections/PyNumpyTypeTest.java | 16 +++ 6 files changed, 321 insertions(+), 1 deletion(-) create mode 100644 python/testData/inspections/PyNumpyType/ArgSort.py create mode 100644 python/testData/inspections/PyNumpyType/Empty.py create mode 100644 python/testData/inspections/PyNumpyType/Transpose.py create mode 100644 python/testData/inspections/PyNumpyType/Vectorize.py diff --git a/python/src/com/jetbrains/numpy/codeInsight/NumpyDocStringTypeProvider.java b/python/src/com/jetbrains/numpy/codeInsight/NumpyDocStringTypeProvider.java index 2478a2d5a1cb..f5ee66376ddd 100644 --- a/python/src/com/jetbrains/numpy/codeInsight/NumpyDocStringTypeProvider.java +++ b/python/src/com/jetbrains/numpy/codeInsight/NumpyDocStringTypeProvider.java @@ -45,7 +45,7 @@ public class NumpyDocStringTypeProvider extends PyTypeProviderBase { private static final Map NUMPY_ALIAS_TO_REAL_TYPE = new HashMap(); static { - NUMPY_ALIAS_TO_REAL_TYPE.put("ndarray", "numpy.core.multiarray.ndarray or collections.Iterable"); + NUMPY_ALIAS_TO_REAL_TYPE.put("ndarray", "numpy.core.multiarray.ndarray"); NUMPY_ALIAS_TO_REAL_TYPE.put("numpy.ndarray", "numpy.core.multiarray.ndarray"); // 184 occurrences NUMPY_ALIAS_TO_REAL_TYPE.put("array_like", "numpy.core.multiarray.ndarray or collections.Iterable"); diff --git a/python/testData/inspections/PyNumpyType/ArgSort.py b/python/testData/inspections/PyNumpyType/ArgSort.py new file mode 100644 index 000000000000..59e682cb7a1d --- /dev/null +++ b/python/testData/inspections/PyNumpyType/ArgSort.py @@ -0,0 +1,89 @@ + + +def argsort(a, axis=-1, kind='quicksort', order=None): + """ + Returns the indices that would sort an array. + + Perform an indirect sort along the given axis using the algorithm specified + by the `kind` keyword. It returns an array of indices of the same shape as + `a` that index data along the given axis in sorted order. + + Parameters + ---------- + a : array_like + Array to sort. + axis : int or None, optional + Axis along which to sort. The default is -1 (the last axis). If None, + the flattened array is used. + kind : {'quicksort', 'mergesort', 'heapsort'}, optional + Sorting algorithm. + order : list, optional + When `a` is an array with fields defined, this argument specifies + which fields to compare first, second, etc. Not all fields need be + specified. + + Returns + ------- + index_array : ndarray, int + Array of indices that sort `a` along the specified axis. + In other words, ``a[index_array]`` yields a sorted `a`. + + See Also + -------- + sort : Describes sorting algorithms used. + lexsort : Indirect stable sort with multiple keys. + ndarray.sort : Inplace sort. + argpartition : Indirect partial sort. + + Notes + ----- + See `sort` for notes on the different sorting algorithms. + + As of NumPy 1.4.0 `argsort` works with real/complex arrays containing + nan values. The enhanced sort order is documented in `sort`. + + Examples + -------- + One dimensional array: + + >>> x = np.array([3, 1, 2]) + >>> np.argsort(x) + array([1, 2, 0]) + + Two-dimensional array: + + >>> x = np.array([[0, 3], [2, 2]]) + >>> x + array([[0, 3], + [2, 2]]) + + >>> np.argsort(x, axis=0) + array([[0, 1], + [1, 0]]) + + >>> np.argsort(x, axis=1) + array([[0, 1], + [0, 1]]) + + Sorting with keys: + + >>> x = np.array([(1, 0), (0, 1)], dtype=[('x', '>> x + array([(1, 0), (0, 1)], + dtype=[('x', '>> np.argsort(x, order=('x','y')) + array([1, 0]) + + >>> np.argsort(x, order=('y','x')) + array([0, 1]) + + """ + try: + argsort = a.argsort + except AttributeError: + return _wrapit(a, 'argsort', axis, kind, order) + return argsort(axis, kind, order) + +x = np.array([(1, 0), (0, 1)], dtype=[('x', '>> np.empty([2, 2]) + array([[ -9.74499359e+001, 6.69583040e-309], + [ 2.13182611e-314, 3.06959433e-309]]) #random + + >>> np.empty([2, 2], dtype=int) + array([[-1073741821, -1067949133], + [ 496041986, 19249760]]) #random + """ + pass + +empty([2, 2]) \ No newline at end of file diff --git a/python/testData/inspections/PyNumpyType/Transpose.py b/python/testData/inspections/PyNumpyType/Transpose.py new file mode 100644 index 000000000000..fe16529bdc27 --- /dev/null +++ b/python/testData/inspections/PyNumpyType/Transpose.py @@ -0,0 +1,47 @@ + +def transpose(a, axes=None): + """ + Permute the dimensions of an array. + + Parameters + ---------- + a : array_like + Input array. + axes : list of ints, optional + By default, reverse the dimensions, otherwise permute the axes + according to the values given. + + Returns + ------- + p : ndarray + `a` with its axes permuted. A view is returned whenever + possible. + + See Also + -------- + rollaxis + + Examples + -------- + >>> x = np.arange(4).reshape((2,2)) + >>> x + array([[0, 1], + [2, 3]]) + + >>> np.transpose(x) + array([[0, 2], + [1, 3]]) + + >>> x = np.ones((1, 2, 3)) + >>> np.transpose(x, (1, 0, 2)).shape + (2, 1, 3) + + """ + try: + transpose = a.transpose + except AttributeError: + return _wrapit(a, 'transpose', axes) + return transpose(axes) + +x = np.ones((1, 2, 3)) +a = transpose(x, (1, 0, 2)).shape \ No newline at end of file diff --git a/python/testData/inspections/PyNumpyType/Vectorize.py b/python/testData/inspections/PyNumpyType/Vectorize.py new file mode 100644 index 000000000000..520aca8f78b4 --- /dev/null +++ b/python/testData/inspections/PyNumpyType/Vectorize.py @@ -0,0 +1,122 @@ + +class vectorize(object): + """ + vectorize(pyfunc, otypes='', doc=None, excluded=None, cache=False) + + Generalized function class. + + Define a vectorized function which takes a nested sequence + of objects or numpy arrays as inputs and returns a + numpy array as output. The vectorized function evaluates `pyfunc` over + successive tuples of the input arrays like the python map function, + except it uses the broadcasting rules of numpy. + + The data type of the output of `vectorized` is determined by calling + the function with the first element of the input. This can be avoided + by specifying the `otypes` argument. + + Parameters + ---------- + pyfunc : callable + A python function or method. + otypes : str or list of dtypes, optional + The output data type. It must be specified as either a string of + typecode characters or a list of data type specifiers. There should + be one data type specifier for each output. + doc : str, optional + The docstring for the function. If `None`, the docstring will be the + ``pyfunc.__doc__``. + excluded : set, optional + Set of strings or integers representing the positional or keyword + arguments for which the function will not be vectorized. These will be + passed directly to `pyfunc` unmodified. + + .. versionadded:: 1.7.0 + + cache : bool, optional + If `True`, then cache the first function call that determines the number + of outputs if `otypes` is not provided. + + .. versionadded:: 1.7.0 + + Returns + ------- + vectorized : callable + Vectorized function. + + Examples + -------- + >>> def myfunc(a, b): + ... "Return a-b if a>b, otherwise return a+b" + ... if a > b: + ... return a - b + ... else: + ... return a + b + + >>> vfunc = np.vectorize(myfunc) + >>> vfunc([1, 2, 3, 4], 2) + array([3, 4, 1, 2]) + + The docstring is taken from the input function to `vectorize` unless it + is specified + + >>> vfunc.__doc__ + 'Return a-b if a>b, otherwise return a+b' + >>> vfunc = np.vectorize(myfunc, doc='Vectorized `myfunc`') + >>> vfunc.__doc__ + 'Vectorized `myfunc`' + + The output type is determined by evaluating the first element of the input, + unless it is specified + + >>> out = vfunc([1, 2, 3, 4], 2) + >>> type(out[0]) + + >>> vfunc = np.vectorize(myfunc, otypes=[np.float]) + >>> out = vfunc([1, 2, 3, 4], 2) + >>> type(out[0]) + + + The `excluded` argument can be used to prevent vectorizing over certain + arguments. This can be useful for array-like arguments of a fixed length + such as the coefficients for a polynomial as in `polyval`: + + >>> def mypolyval(p, x): + ... _p = list(p) + ... res = _p.pop(0) + ... while _p: + ... res = res*x + _p.pop(0) + ... return res + >>> vpolyval = np.vectorize(mypolyval, excluded=['p']) + >>> vpolyval(p=[1, 2, 3], x=[0, 1]) + array([3, 6]) + + Positional arguments may also be excluded by specifying their position: + + >>> vpolyval.excluded.add(0) + >>> vpolyval([1, 2, 3], x=[0, 1]) + array([3, 6]) + + Notes + ----- + The `vectorize` function is provided primarily for convenience, not for + performance. The implementation is essentially a for loop. + + If `otypes` is not specified, then a call to the function with the + first argument will be used to determine the number of outputs. The + results of this call will be cached if `cache` is `True` to prevent + calling the function twice. However, to implement the cache, the + original function must be wrapped which will slow down subsequent + calls, so only do this if your function is expensive. + + The new keyword argument interface and `excluded` argument support + further degrades performance. + + """ + + def __init__(self, pyfunc, otypes='', doc=None, excluded=None, + cache=False): + pass + +def mypolyval(): pass +vpolyval = vectorize(mypolyval, excluded=['p']) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/inspections/PyNumpyTypeTest.java b/python/testSrc/com/jetbrains/python/inspections/PyNumpyTypeTest.java index 26ad34baee3e..326f616e60ff 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyNumpyTypeTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyNumpyTypeTest.java @@ -33,4 +33,20 @@ public class PyNumpyTypeTest extends PyTestCase { public void testDtype() { doTest(); } + + public void testEmpty() { + doTest(); + } + + public void testTranspose() { + doTest(); + } + + public void testArgSort() { + doTest(); + } + + public void testVectorize() { + doTest(); + } }