def test_logsumexp(): # Test whether logsumexp() function correctly handles large inputs. a = np.arange(200) desired = np.log(np.sum(np.exp(a))) assert_almost_equal(logsumexp(a), desired) # Now test with large numbers b = [1000, 1000] desired = 1000.0 + np.log(2.0) assert_almost_equal(logsumexp(b), desired) n = 1000 b = np.ones(n) * 10000 desired = 10000.0 + np.log(n) assert_almost_equal(logsumexp(b), desired) x = np.array([1e-40] * 1000000) logx = np.log(x) X = np.vstack([x, x]) logX = np.vstack([logx, logx]) assert_array_almost_equal(np.exp(logsumexp(logX)), X.sum()) assert_array_almost_equal(np.exp(logsumexp(logX, axis=0)), X.sum(axis=0)) assert_array_almost_equal(np.exp(logsumexp(logX, axis=1)), X.sum(axis=1)) # Handling special values properly assert_equal(logsumexp(np.inf), np.inf) assert_equal(logsumexp(-np.inf), -np.inf) assert_equal(logsumexp(np.nan), np.nan) assert_equal(logsumexp([-np.inf, -np.inf]), -np.inf) # Handling an array with different magnitudes on the axes assert_array_almost_equal( logsumexp([[1e10, 1e-10], [-1e10, -np.inf]], axis=-1), [1e10, -1e10]) # Test keeping dimensions assert_array_almost_equal( logsumexp([[1e10, 1e-10], [-1e10, -np.inf]], axis=-1, keepdims=True), [[1e10], [-1e10]]) # Test multiple axes if NumpyVersion(np.__version__) >= NumpyVersion('1.7.0'): assert_array_almost_equal( logsumexp([[1e10, 1e-10], [-1e10, -np.inf]], axis=(-1, -2)), 1e10)
def generic_gradient_magnitude(input, derivative, output=None, mode="reflect", cval=0.0, extra_arguments=(), extra_keywords=None): """Gradient magnitude using a provided gradient function. Parameters ---------- %(input)s derivative : callable Callable with the following signature:: derivative(input, axis, output, mode, cval, *extra_arguments, **extra_keywords) See `extra_arguments`, `extra_keywords` below. `derivative` can assume that `input` and `output` are ndarrays. Note that the output from `derivative` is modified inplace; be careful to copy important inputs before returning them. %(output)s %(mode)s %(cval)s %(extra_keywords)s %(extra_arguments)s """ if extra_keywords is None: extra_keywords = {} input = numpy.asarray(input) output, return_value = _ni_support._get_output(output, input) axes = list(range(input.ndim)) if len(axes) > 0: derivative(input, axes[0], output, mode, cval, *extra_arguments, **extra_keywords) numpy.multiply(output, output, output) for ii in range(1, len(axes)): tmp = derivative(input, axes[ii], output.dtype, mode, cval, *extra_arguments, **extra_keywords) numpy.multiply(tmp, tmp, tmp) output += tmp # This allows the sqrt to work with a different default casting if NumpyVersion(numpy.__version__) > '1.6.1': numpy.sqrt(output, output, casting='unsafe') else: numpy.sqrt(output, output) else: output[...] = input[...] return return_value
import numpy as np from ._fortran import * from scipy._lib._version import NumpyVersion # Don't use deprecated Numpy C API. Define this to a fixed version instead of # NPY_API_VERSION in order not to break compilation for released SciPy versions # when Numpy introduces a new deprecation. Use in setup.py:: # # config.add_extension('_name', sources=['source_fname'], **numpy_nodepr_api) # if NumpyVersion(np.__version__) >= '1.10.0.dev': numpy_nodepr_api = dict(define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_9_API_VERSION")]) else: numpy_nodepr_api = dict() from scipy._lib._testutils import PytestTester test = PytestTester(__name__) del PytestTester
def test_dev0_a_b_rc_mixed(): assert_(NumpyVersion('1.9.0a2.dev0+f16acvda') == '1.9.0a2.dev0+11111111') assert_(NumpyVersion('1.9.0a2.dev0+6acvda54') < '1.9.0a2')
class TestRankData(TestCase): def test_empty(self): """stats.rankdata([]) should return an empty array.""" a = np.array([], dtype=int) r = rankdata(a) assert_array_equal(r, np.array([], dtype=np.float64)) r = rankdata([]) assert_array_equal(r, np.array([], dtype=np.float64)) def test_one(self): """Check stats.rankdata with an array of length 1.""" data = [100] a = np.array(data, dtype=int) r = rankdata(a) assert_array_equal(r, np.array([1.0], dtype=np.float64)) r = rankdata(data) assert_array_equal(r, np.array([1.0], dtype=np.float64)) def test_basic(self): """Basic tests of stats.rankdata.""" data = [100, 10, 50] expected = np.array([3.0, 1.0, 2.0], dtype=np.float64) a = np.array(data, dtype=int) r = rankdata(a) assert_array_equal(r, expected) r = rankdata(data) assert_array_equal(r, expected) data = [40, 10, 30, 10, 50] expected = np.array([4.0, 1.5, 3.0, 1.5, 5.0], dtype=np.float64) a = np.array(data, dtype=int) r = rankdata(a) assert_array_equal(r, expected) r = rankdata(data) assert_array_equal(r, expected) data = [20, 20, 20, 10, 10, 10] expected = np.array([5.0, 5.0, 5.0, 2.0, 2.0, 2.0], dtype=np.float64) a = np.array(data, dtype=int) r = rankdata(a) assert_array_equal(r, expected) r = rankdata(data) assert_array_equal(r, expected) # The docstring states explicitly that the argument is flattened. a2d = a.reshape(2, 3) r = rankdata(a2d) assert_array_equal(r, expected) @dec.skipif(NumpyVersion(np.__version__) < '1.7.0') def test_rankdata_object_string(self): min_rank = lambda a: [1 + sum(i < j for i in a) for j in a] max_rank = lambda a: [sum(i <= j for i in a) for j in a] ordinal_rank = lambda a: min_rank([(x, i) for i, x in enumerate(a)]) def average_rank(a): return [(i + j) / 2.0 for i, j in zip(min_rank(a), max_rank(a))] def dense_rank(a): b = np.unique(a) return [1 + sum(i < j for i in b) for j in a] rankf = dict(min=min_rank, max=max_rank, ordinal=ordinal_rank, average=average_rank, dense=dense_rank) def check_ranks(a): for method in 'min', 'max', 'dense', 'ordinal', 'average': out = rankdata(a, method=method) assert_array_equal(out, rankf[method](a)) val = ['foo', 'bar', 'qux', 'xyz', 'abc', 'efg', 'ace', 'qwe', 'qaz'] check_ranks(np.random.choice(val, 200)) check_ranks(np.random.choice(val, 200).astype('object')) val = np.array([0, 1, 2, 2.718, 3, 3.141], dtype='object') check_ranks(np.random.choice(val, 200).astype('object')) def test_large_int(self): data = np.array([2**60, 2**60 + 1], dtype=np.uint64) r = rankdata(data) assert_array_equal(r, [1.0, 2.0]) data = np.array([2**60, 2**60 + 1], dtype=np.int64) r = rankdata(data) assert_array_equal(r, [1.0, 2.0]) data = np.array([2**60, -2**60 + 1], dtype=np.int64) r = rankdata(data) assert_array_equal(r, [2.0, 1.0]) def test_big_tie(self): for n in [10000, 100000, 1000000]: data = np.ones(n, dtype=int) r = rankdata(data) expected_rank = 0.5 * (n + 1) assert_array_equal(r, expected_rank * data, "test failed with n=%d" % n)
def test_version_1_point_10(): # regression test for gh-2998. assert_(NumpyVersion('1.9.0') < '1.10.0') assert_(NumpyVersion('1.11.0') < '1.11.1') assert_(NumpyVersion('1.11.0') == '1.11.0') assert_(NumpyVersion('1.99.11') < '1.99.12')
def test_dev0_version(): assert_(NumpyVersion('1.9.0.dev0+Unknown') < '1.9.0') for ver in ['1.9.0', '1.9.0a1', '1.9.0b2', '1.9.0b2.dev0+ffffffff']: assert_(NumpyVersion('1.9.0.dev0+f16acvda') < ver) assert_(NumpyVersion('1.9.0.dev0+f16acvda') == '1.9.0.dev0+11111111')
"""Functions copypasted from newer versions of numpy. """ from __future__ import division, print_function, absolute_import import warnings import sys from warnings import WarningMessage import re from functools import wraps import numpy as np from scipy._lib._version import NumpyVersion if NumpyVersion(np.__version__) > '1.7.0.dev': _assert_warns = np.testing.assert_warns else: def _assert_warns(warning_class, func, *args, **kw): r""" Fail unless the given callable throws the specified warning. This definition is copypasted from numpy 1.9.0.dev. The version in earlier numpy returns None. Parameters ---------- warning_class : class The class defining the warning that `func` is expected to throw. func : callable The callable to test.
_check_save_and_load(dense_matrix) def test_save_and_load_empty(): dense_matrix = np.zeros((4, 6)) _check_save_and_load(dense_matrix) def test_save_and_load_one_entry(): dense_matrix = np.zeros((4, 6)) dense_matrix[1, 2] = 1 _check_save_and_load(dense_matrix) @dec.skipif( NumpyVersion(np.__version__) < '1.10.0', 'disabling unpickling requires numpy >= 1.10.0') def test_malicious_load(): class Executor(object): def __reduce__(self): return (assert_, (False, 'unexpected code execution')) fd, tmpfile = tempfile.mkstemp(suffix='.npz') os.close(fd) try: np.savez(tmpfile, format=Executor()) # Should raise a ValueError, not execute code assert_raises(ValueError, load_npz, tmpfile) finally: os.remove(tmpfile)
from scipy import fftpack, ndimage, signal import numpy as np import threading from scipy._lib._version import NumpyVersion _rfft_mt_safe = (NumpyVersion(np.__version__) >= '1.9.0.dev-e24486e') _rfft_lock = threading.Lock() import lenstronomy.Util.kernel_util as kernel_util import lenstronomy.Util.util as util import lenstronomy.Util.image_util as image_util def _centered(arr, newshape): # Return the center newshape portion of the array. newshape = np.asarray(newshape) currshape = np.array(arr.shape) startind = (currshape - newshape) // 2 endind = startind + newshape myslice = [slice(startind[k], endind[k]) for k in range(len(endind))] return arr[tuple(myslice)] class PixelKernelConvolution(object): """ class to compute convolutions for a given pixelized kernel (fft, grid) """ def __init__(self, kernel, convolution_type='fft_static'): """ :param kernel: 2d array, convolution kernel :param convolution_type: string, 'fft', 'grid', 'fft_static' mode of 2d convolution
# # Copyright (C) Tyler Reddy, Ross Hemsley, Edd Edmondson, # Nikolai Nowaczyk, Joe Pitt-Francis, 2015. # # Distributed under the same BSD license as Scipy. # from __future__ import print_function, division, absolute_import import numpy as np import scipy import itertools from scipy._lib._version import NumpyVersion from scipy.spatial import distance import math # Whether Numpy has stacked matrix linear algebra HAS_NUMPY_VEC_DET = (NumpyVersion(np.__version__) >= '1.8.0') __all__ = ['SphericalVoronoi'] def convert_cartesian_to_sphere(coords, angle_measure='radians'): ''' Take shape (3, ) cartesian coord_array and return an array of the same shape in spherical polar form (r, theta, phi). Based on StackOverflow response: http://stackoverflow.com/a/4116899 use radians for the angles by default, degrees if angle_measure == 'degrees' ''' spherical_coord = np.zeros(coords.shape)
class TestConstructUtils(object): def test_spdiags(self): diags1 = array([[1, 2, 3, 4, 5]]) diags2 = array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) diags3 = array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]) cases = [] cases.append((diags1, 0, 1, 1, [[1]])) cases.append((diags1, [0], 1, 1, [[1]])) cases.append((diags1, [0], 2, 1, [[1], [0]])) cases.append((diags1, [0], 1, 2, [[1, 0]])) cases.append((diags1, [1], 1, 2, [[0, 2]])) cases.append((diags1, [-1], 1, 2, [[0, 0]])) cases.append((diags1, [0], 2, 2, [[1, 0], [0, 2]])) cases.append((diags1, [-1], 2, 2, [[0, 0], [1, 0]])) cases.append((diags1, [3], 2, 2, [[0, 0], [0, 0]])) cases.append((diags1, [0], 3, 4, [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0]])) cases.append((diags1, [1], 3, 4, [[0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]])) cases.append((diags1, [2], 3, 5, [[0, 0, 3, 0, 0], [0, 0, 0, 4, 0], [0, 0, 0, 0, 5]])) cases.append((diags2, [0, 2], 3, 3, [[1, 0, 8], [0, 2, 0], [0, 0, 3]])) cases.append((diags2, [-1, 0], 3, 4, [[6, 0, 0, 0], [1, 7, 0, 0], [0, 2, 8, 0]])) cases.append((diags2, [2, -3], 6, 6, [[0, 0, 3, 0, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 5, 0], [6, 0, 0, 0, 0, 0], [0, 7, 0, 0, 0, 0], [0, 0, 8, 0, 0, 0]])) cases.append((diags3, [-1, 0, 1], 6, 6, [[6, 12, 0, 0, 0, 0], [1, 7, 13, 0, 0, 0], [0, 2, 8, 14, 0, 0], [0, 0, 3, 9, 15, 0], [0, 0, 0, 4, 10, 0], [0, 0, 0, 0, 5, 0]])) cases.append((diags3, [-4, 2, -1], 6, 5, [[0, 0, 8, 0, 0], [11, 0, 0, 9, 0], [0, 12, 0, 0, 10], [0, 0, 13, 0, 0], [1, 0, 0, 14, 0], [0, 2, 0, 0, 15]])) for d, o, m, n, result in cases: assert_equal(construct.spdiags(d, o, m, n).todense(), result) def test_diags(self): a = array([1, 2, 3, 4, 5]) b = array([6, 7, 8, 9, 10]) c = array([11, 12, 13, 14, 15]) cases = [] cases.append((a[:1], 0, (1, 1), [[1]])) cases.append(([a[:1]], [0], (1, 1), [[1]])) cases.append(([a[:1]], [0], (2, 1), [[1], [0]])) cases.append(([a[:1]], [0], (1, 2), [[1, 0]])) cases.append(([a[:1]], [1], (1, 2), [[0, 1]])) cases.append(([a[:2]], [0], (2, 2), [[1, 0], [0, 2]])) cases.append(([a[:1]], [-1], (2, 2), [[0, 0], [1, 0]])) cases.append(([a[:3]], [0], (3, 4), [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0]])) cases.append(([a[:3]], [1], (3, 4), [[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 3]])) cases.append(([a[:1]], [-2], (3, 5), [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 0]])) cases.append(([a[:2]], [-1], (3, 5), [[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [0, 2, 0, 0, 0]])) cases.append(([a[:3]], [0], (3, 5), [[1, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 0, 3, 0, 0]])) cases.append(([a[:3]], [1], (3, 5), [[0, 1, 0, 0, 0], [0, 0, 2, 0, 0], [0, 0, 0, 3, 0]])) cases.append(([a[:3]], [2], (3, 5), [[0, 0, 1, 0, 0], [0, 0, 0, 2, 0], [0, 0, 0, 0, 3]])) cases.append(([a[:2]], [3], (3, 5), [[0, 0, 0, 1, 0], [0, 0, 0, 0, 2], [0, 0, 0, 0, 0]])) cases.append(([a[:1]], [4], (3, 5), [[0, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])) cases.append(([a[:1]], [-4], (5, 3), [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [1, 0, 0]])) cases.append(([a[:2]], [-3], (5, 3), [[0, 0, 0], [0, 0, 0], [0, 0, 0], [1, 0, 0], [0, 2, 0]])) cases.append(([a[:3]], [-2], (5, 3), [[0, 0, 0], [0, 0, 0], [1, 0, 0], [0, 2, 0], [0, 0, 3]])) cases.append(([a[:3]], [-1], (5, 3), [[0, 0, 0], [1, 0, 0], [0, 2, 0], [0, 0, 3], [0, 0, 0]])) cases.append(([a[:3]], [0], (5, 3), [[1, 0, 0], [0, 2, 0], [0, 0, 3], [0, 0, 0], [0, 0, 0]])) cases.append(([a[:2]], [1], (5, 3), [[0, 1, 0], [0, 0, 2], [0, 0, 0], [0, 0, 0], [0, 0, 0]])) cases.append(([a[:1]], [2], (5, 3), [[0, 0, 1], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])) cases.append(([a[:3], b[:1]], [0, 2], (3, 3), [[1, 0, 6], [0, 2, 0], [0, 0, 3]])) cases.append(([a[:2], b[:3]], [-1, 0], (3, 4), [[6, 0, 0, 0], [1, 7, 0, 0], [0, 2, 8, 0]])) cases.append(([a[:4], b[:3]], [2, -3], (6, 6), [[0, 0, 1, 0, 0, 0], [0, 0, 0, 2, 0, 0], [0, 0, 0, 0, 3, 0], [6, 0, 0, 0, 0, 4], [0, 7, 0, 0, 0, 0], [0, 0, 8, 0, 0, 0]])) cases.append(([a[:4], b, c[:4]], [-1, 0, 1], (5, 5), [[6, 11, 0, 0, 0], [1, 7, 12, 0, 0], [0, 2, 8, 13, 0], [0, 0, 3, 9, 14], [0, 0, 0, 4, 10]])) cases.append(([a[:2], b[:3], c], [-4, 2, -1], (6, 5), [[0, 0, 6, 0, 0], [11, 0, 0, 7, 0], [0, 12, 0, 0, 8], [0, 0, 13, 0, 0], [1, 0, 0, 14, 0], [0, 2, 0, 0, 15]])) # too long arrays are OK cases.append(([a], [0], (1, 1), [[1]])) cases.append(([a[:3], b], [0, 2], (3, 3), [[1, 0, 6], [0, 2, 0], [0, 0, 3]])) cases.append((np.array([[1, 2, 3], [4, 5, 6]]), [0, -1], (3, 3), [[1, 0, 0], [4, 2, 0], [0, 5, 3]])) # scalar case: broadcasting cases.append(([1, -2, 1], [1, 0, -1], (3, 3), [[-2, 1, 0], [1, -2, 1], [0, 1, -2]])) for d, o, shape, result in cases: err_msg = "%r %r %r %r" % (d, o, shape, result) assert_equal(construct.diags(d, o, shape=shape).todense(), result, err_msg=err_msg) if shape[0] == shape[1] and hasattr( d[0], '__len__') and len(d[0]) <= max(shape): # should be able to find the shape automatically assert_equal(construct.diags(d, o).todense(), result, err_msg=err_msg) def test_diags_default(self): a = array([1, 2, 3, 4, 5]) assert_equal(construct.diags(a).todense(), np.diag(a)) def test_diags_default_bad(self): a = array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]]) assert_raises(ValueError, construct.diags, a) def test_diags_bad(self): a = array([1, 2, 3, 4, 5]) b = array([6, 7, 8, 9, 10]) c = array([11, 12, 13, 14, 15]) cases = [] cases.append(([a[:0]], 0, (1, 1))) cases.append(([a[:4], b, c[:3]], [-1, 0, 1], (5, 5))) cases.append(([a[:2], c, b[:3]], [-4, 2, -1], (6, 5))) cases.append(([a[:2], c, b[:3]], [-4, 2, -1], None)) cases.append(([], [-4, 2, -1], None)) cases.append(([1], [-5], (4, 4))) cases.append(([a], 0, None)) for d, o, shape in cases: assert_raises(ValueError, construct.diags, d, o, shape) assert_raises(TypeError, construct.diags, [[None]], [0]) def test_diags_vs_diag(self): # Check that # # diags([a, b, ...], [i, j, ...]) == diag(a, i) + diag(b, j) + ... # np.random.seed(1234) for n_diags in [1, 2, 3, 4, 5, 10]: n = 1 + n_diags // 2 + np.random.randint(0, 10) offsets = np.arange(-n + 1, n - 1) np.random.shuffle(offsets) offsets = offsets[:n_diags] diagonals = [np.random.rand(n - abs(q)) for q in offsets] mat = construct.diags(diagonals, offsets) dense_mat = sum( [np.diag(x, j) for x, j in zip(diagonals, offsets)]) assert_array_almost_equal_nulp(mat.todense(), dense_mat) if len(offsets) == 1: mat = construct.diags(diagonals[0], offsets[0]) dense_mat = np.diag(diagonals[0], offsets[0]) assert_array_almost_equal_nulp(mat.todense(), dense_mat) def test_diags_dtype(self): x = construct.diags([2.2], [0], shape=(2, 2), dtype=int) assert_equal(x.dtype, int) assert_equal(x.todense(), [[2, 0], [0, 2]]) def test_diags_one_diagonal(self): d = list(range(5)) for k in range(-5, 6): assert_equal( construct.diags(d, k).toarray(), construct.diags([d], [k]).toarray()) def test_diags_empty(self): x = construct.diags([]) assert_equal(x.shape, (0, 0)) def test_identity(self): assert_equal(construct.identity(1).toarray(), [[1]]) assert_equal(construct.identity(2).toarray(), [[1, 0], [0, 1]]) I = construct.identity(3, dtype='int8', format='dia') assert_equal(I.dtype, np.dtype('int8')) assert_equal(I.format, 'dia') for fmt in sparse_formats: I = construct.identity(3, format=fmt) assert_equal(I.format, fmt) assert_equal(I.toarray(), [[1, 0, 0], [0, 1, 0], [0, 0, 1]]) def test_eye(self): assert_equal(construct.eye(1, 1).toarray(), [[1]]) assert_equal(construct.eye(2, 3).toarray(), [[1, 0, 0], [0, 1, 0]]) assert_equal(construct.eye(3, 2).toarray(), [[1, 0], [0, 1], [0, 0]]) assert_equal( construct.eye(3, 3).toarray(), [[1, 0, 0], [0, 1, 0], [0, 0, 1]]) assert_equal( construct.eye(3, 3, dtype='int16').dtype, np.dtype('int16')) for m in [3, 5]: for n in [3, 5]: for k in range(-5, 6): assert_equal( construct.eye(m, n, k=k).toarray(), np.eye(m, n, k=k)) if m == n: assert_equal( construct.eye(m, k=k).toarray(), np.eye(m, n, k=k)) def test_eye_one(self): assert_equal(construct.eye(1).toarray(), [[1]]) assert_equal(construct.eye(2).toarray(), [[1, 0], [0, 1]]) I = construct.eye(3, dtype='int8', format='dia') assert_equal(I.dtype, np.dtype('int8')) assert_equal(I.format, 'dia') for fmt in sparse_formats: I = construct.eye(3, format=fmt) assert_equal(I.format, fmt) assert_equal(I.toarray(), [[1, 0, 0], [0, 1, 0], [0, 0, 1]]) def test_kron(self): cases = [] cases.append(array([[0]])) cases.append(array([[-1]])) cases.append(array([[4]])) cases.append(array([[10]])) cases.append(array([[0], [0]])) cases.append(array([[0, 0]])) cases.append(array([[1, 2], [3, 4]])) cases.append(array([[0, 2], [5, 0]])) cases.append(array([[0, 2, -6], [8, 0, 14]])) cases.append(array([[5, 4], [0, 0], [6, 0]])) cases.append(array([[5, 4, 4], [1, 0, 0], [6, 0, 8]])) cases.append(array([[0, 1, 0, 2, 0, 5, 8]])) cases.append(array([[0.5, 0.125, 0, 3.25], [0, 2.5, 0, 0]])) for a in cases: for b in cases: result = construct.kron(csr_matrix(a), csr_matrix(b)).todense() expected = np.kron(a, b) assert_array_equal(result, expected) def test_kronsum(self): cases = [] cases.append(array([[0]])) cases.append(array([[-1]])) cases.append(array([[4]])) cases.append(array([[10]])) cases.append(array([[1, 2], [3, 4]])) cases.append(array([[0, 2], [5, 0]])) cases.append(array([[0, 2, -6], [8, 0, 14], [0, 3, 0]])) cases.append(array([[1, 0, 0], [0, 5, -1], [4, -2, 8]])) for a in cases: for b in cases: result = construct.kronsum(csr_matrix(a), csr_matrix(b)).todense() expected = np.kron(np.eye(len(b)), a) + \ np.kron(b, np.eye(len(a))) assert_array_equal(result, expected) def test_vstack(self): A = coo_matrix([[1, 2], [3, 4]]) B = coo_matrix([[5, 6]]) expected = matrix([[1, 2], [3, 4], [5, 6]]) assert_equal(construct.vstack([A, B]).todense(), expected) assert_equal( construct.vstack([A, B], dtype=np.float32).dtype, np.float32) assert_equal( construct.vstack([A.tocsr(), B.tocsr()]).todense(), expected) assert_equal( construct.vstack([A.tocsr(), B.tocsr()], dtype=np.float32).dtype, np.float32) assert_equal( construct.vstack([A.tocsr(), B.tocsr()], dtype=np.float32).indices.dtype, np.int32) assert_equal( construct.vstack([A.tocsr(), B.tocsr()], dtype=np.float32).indptr.dtype, np.int32) def test_hstack(self): A = coo_matrix([[1, 2], [3, 4]]) B = coo_matrix([[5], [6]]) expected = matrix([[1, 2, 5], [3, 4, 6]]) assert_equal(construct.hstack([A, B]).todense(), expected) assert_equal( construct.hstack([A, B], dtype=np.float32).dtype, np.float32) assert_equal( construct.hstack([A.tocsc(), B.tocsc()]).todense(), expected) assert_equal( construct.hstack([A.tocsc(), B.tocsc()], dtype=np.float32).dtype, np.float32) def test_bmat(self): A = coo_matrix([[1, 2], [3, 4]]) B = coo_matrix([[5], [6]]) C = coo_matrix([[7]]) D = coo_matrix((0, 0)) expected = matrix([[1, 2, 5], [3, 4, 6], [0, 0, 7]]) assert_equal(construct.bmat([[A, B], [None, C]]).todense(), expected) expected = matrix([[1, 2, 0], [3, 4, 0], [0, 0, 7]]) assert_equal( construct.bmat([[A, None], [None, C]]).todense(), expected) expected = matrix([[0, 5], [0, 6], [7, 0]]) assert_equal( construct.bmat([[None, B], [C, None]]).todense(), expected) expected = matrix(np.empty((0, 0))) assert_equal(construct.bmat([[None, None]]).todense(), expected) assert_equal( construct.bmat([[None, D], [D, None]]).todense(), expected) # test bug reported in gh-5976 expected = matrix([[7]]) assert_equal( construct.bmat([[None, D], [C, None]]).todense(), expected) # test failure cases with assert_raises(ValueError) as excinfo: construct.bmat([[A], [B]]) excinfo.match(r'Got blocks\[1,0\]\.shape\[1\] == 1, expected 2') with assert_raises(ValueError) as excinfo: construct.bmat([[A, C]]) excinfo.match(r'Got blocks\[0,1\]\.shape\[0\] == 1, expected 2') @pytest.mark.slow def test_concatenate_int32_overflow(self): """ test for indptr overflow when concatenating matrices """ check_free_memory(30000) n = 33000 A = csr_matrix(np.ones((n, n), dtype=bool)) B = A.copy() C = construct._compressed_sparse_stack((A, B), 0) assert_(np.all(np.equal(np.diff(C.indptr), n))) assert_equal(C.indices.dtype, np.int64) assert_equal(C.indptr.dtype, np.int64) def test_block_diag_basic(self): """ basic test for block_diag """ A = coo_matrix([[1, 2], [3, 4]]) B = coo_matrix([[5], [6]]) C = coo_matrix([[7]]) expected = matrix([[1, 2, 0, 0], [3, 4, 0, 0], [0, 0, 5, 0], [0, 0, 6, 0], [0, 0, 0, 7]]) assert_equal(construct.block_diag((A, B, C)).todense(), expected) def test_block_diag_scalar_1d_args(self): """ block_diag with scalar and 1d arguments """ # one 1d matrix and a scalar assert_array_equal( construct.block_diag([[2, 3], 4]).toarray(), [[2, 3, 0], [0, 0, 4]]) def test_block_diag_1(self): """ block_diag with one matrix """ assert_equal( construct.block_diag([[1, 0]]).todense(), matrix([[1, 0]])) assert_equal( construct.block_diag([[[1, 0]]]).todense(), matrix([[1, 0]])) assert_equal( construct.block_diag([[[1], [0]]]).todense(), matrix([[1], [0]])) # just on scalar assert_equal(construct.block_diag([1]).todense(), matrix([[1]])) @pytest.mark.xfail( (NumpyVersion(np.__version__) < "1.11.0" and sys.maxsize <= 2**32), reason="randint not compatible") def test_random_sampling(self): # Simple sanity checks for sparse random sampling. for f in sprand, _sprandn: for t in [ np.float32, np.float64, np.longdouble, np.int32, np.int64, np.complex64, np.complex128 ]: x = f(5, 10, density=0.1, dtype=t) assert_equal(x.dtype, t) assert_equal(x.shape, (5, 10)) assert_equal(x.nnz, 5) x1 = f(5, 10, density=0.1, random_state=4321) assert_equal(x1.dtype, np.double) x2 = f(5, 10, density=0.1, random_state=np.random.RandomState(4321)) assert_array_equal(x1.data, x2.data) assert_array_equal(x1.row, x2.row) assert_array_equal(x1.col, x2.col) for density in [0.0, 0.1, 0.5, 1.0]: x = f(5, 10, density=density) assert_equal(x.nnz, int(density * np.prod(x.shape))) for fmt in ['coo', 'csc', 'csr', 'lil']: x = f(5, 10, format=fmt) assert_equal(x.format, fmt) assert_raises(ValueError, lambda: f(5, 10, 1.1)) assert_raises(ValueError, lambda: f(5, 10, -0.1)) def test_rand(self): # Simple distributional checks for sparse.rand. for random_state in None, 4321, np.random.RandomState(): x = sprand(10, 20, density=0.5, dtype=np.float64, random_state=random_state) assert_(np.all(np.less_equal(0, x.data))) assert_(np.all(np.less_equal(x.data, 1))) def test_randn(self): # Simple distributional checks for sparse.randn. # Statistically, some of these should be negative # and some should be greater than 1. for random_state in None, 4321, np.random.RandomState(): x = _sprandn(10, 20, density=0.5, dtype=np.float64, random_state=random_state) assert_(np.any(np.less(x.data, 0))) assert_(np.any(np.less(1, x.data))) def test_random_accept_str_dtype(self): # anything that np.dtype can convert to a dtype should be accepted # for the dtype a = construct.random(10, 10, dtype='d')
else: return ar if return_inverse or return_index: if return_index: perm = ar.argsort(kind='mergesort') else: perm = ar.argsort() aux = ar[perm] flag = np.concatenate(([True], aux[1:] != aux[:-1])) if return_inverse: iflag = np.cumsum(flag) - 1 iperm = perm.argsort() if return_index: return aux[flag], perm[flag], iflag[iperm] else: return aux[flag], iflag[iperm] else: return aux[flag], perm[flag] else: ar.sort() flag = np.concatenate(([True], ar[1:] != ar[:-1])) return ar[flag] if NumpyVersion(np.__version__) > '1.7.0-dev': _compat_unique = np.unique else: _compat_unique = _compat_unique_impl
""" from __future__ import division, print_function, absolute_import __docformat__ = "restructuredtext en" __all__ = ['lil_matrix', 'isspmatrix_lil'] from bisect import bisect_left import numpy as np from scipy._lib._version import NumpyVersion from scipy._lib.six import xrange, zip if NumpyVersion(np.__version__) >= '1.17.0': from scipy._lib._util import _broadcast_arrays else: from numpy import broadcast_arrays as _broadcast_arrays from .base import spmatrix, isspmatrix from .sputils import (getdtype, isshape, isscalarlike, IndexMixin, upcast_scalar, get_index_dtype, isintlike, check_shape, check_reshape_kwargs, asmatrix) from . import _csparsetools class lil_matrix(spmatrix, IndexMixin): """Row-based linked list sparse matrix This is a structure for constructing sparse matrices incrementally.
class TestPlacePoles(object): def _check(self, A, B, P, **kwargs): """ Perform the most common tests on the poles computed by place_poles and return the Bunch object for further specific tests """ fsf = place_poles(A, B, P, **kwargs) expected, _ = np.linalg.eig(A - np.dot(B, fsf.gain_matrix)) _assert_poles_close(expected, fsf.requested_poles) _assert_poles_close(expected, fsf.computed_poles) _assert_poles_close(P, fsf.requested_poles) return fsf def test_real(self): # Test real pole placement using KNV and YT0 algorithm and example 1 in # section 4 of the reference publication (see place_poles docstring) A = np.array([ 1.380, -0.2077, 6.715, -5.676, -0.5814, -4.290, 0, 0.6750, 1.067, 4.273, -6.654, 5.893, 0.0480, 4.273, 1.343, -2.104 ]).reshape(4, 4) B = np.array([0, 5.679, 1.136, 1.136, 0, 0, -3.146, 0]).reshape(4, 2) P = np.array([-0.2, -0.5, -5.0566, -8.6659]) # Check that both KNV and YT compute correct K matrix self._check(A, B, P, method='KNV0') self._check(A, B, P, method='YT') # Try to reach the specific case in _YT_real where two singular # values are almost equal. This is to improve code coverage but I # have no way to be sure this code is really reached # on some architectures this can lead to a RuntimeWarning invalid # value in divide (see gh-7590), so suppress it for now with np.errstate(invalid='ignore'): self._check(A, B, (2, 2, 3, 3)) def test_complex(self): # Test complex pole placement on a linearized car model, taken from L. # Jaulin, Automatique pour la robotique, Cours et Exercices, iSTE # editions p 184/185 A = np.array([0, 7, 0, 0, 0, 0, 0, 7 / 3., 0, 0, 0, 0, 0, 0, 0, 0]).reshape(4, 4) B = np.array([0, 0, 0, 0, 1, 0, 0, 1]).reshape(4, 2) # Test complex poles on YT P = np.array([-3, -1, -2 - 1j, -2 + 1j]) self._check(A, B, P) # Try to reach the specific case in _YT_complex where two singular # values are almost equal. This is to improve code coverage but I # have no way to be sure this code is really reached P = [0 - 1e-6j, 0 + 1e-6j, -10, 10] self._check(A, B, P, maxiter=1000) # Try to reach the specific case in _YT_complex where the rank two # update yields two null vectors. This test was found via Monte Carlo. A = np.array([ -2148, -2902, -2267, -598, -1722, -1829, -165, -283, -2546, -167, -754, -2285, -543, -1700, -584, -2978, -925, -1300, -1583, -984, -386, -2650, -764, -897, -517, -1598, 2, -1709, -291, -338, -153, -1804, -1106, -1168, -867, -2297 ]).reshape(6, 6) B = np.array([ -108, -374, -524, -1285, -1232, -161, -1204, -672, -637, -15, -483, -23, -931, -780, -1245, -1129, -1290, -1502, -952, -1374, -62, -964, -930, -939, -792, -756, -1437, -491, -1543, -686 ]).reshape(6, 5) P = [ -25. - 29.j, -25. + 29.j, 31. - 42.j, 31. + 42.j, 33. - 41.j, 33. + 41.j ] self._check(A, B, P) # Use a lot of poles to go through all cases for update_order # in _YT_loop big_A = np.ones((11, 11)) - np.eye(11) big_B = np.ones((11, 10)) - np.diag([1] * 10, 1)[:, 1:] big_A[:6, :6] = A big_B[:6, :5] = B P = [-10, -20, -30, 40, 50, 60, 70, -20 - 5j, -20 + 5j, 5 + 3j, 5 - 3j] self._check(big_A, big_B, P) #check with only complex poles and only real poles P = [-10, -20, -30, -40, -50, -60, -70, -80, -90, -100] self._check(big_A[:-1, :-1], big_B[:-1, :-1], P) P = [ -10 + 10j, -20 + 20j, -30 + 30j, -40 + 40j, -50 + 50j, -10 - 10j, -20 - 20j, -30 - 30j, -40 - 40j, -50 - 50j ] self._check(big_A[:-1, :-1], big_B[:-1, :-1], P) # need a 5x5 array to ensure YT handles properly when there # is only one real pole and several complex A = np.array([ 0, 7, 0, 0, 0, 0, 0, 7 / 3., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 9 ]).reshape(5, 5) B = np.array([0, 0, 0, 0, 1, 0, 0, 1, 2, 3]).reshape(5, 2) P = np.array([-2, -3 + 1j, -3 - 1j, -1 + 1j, -1 - 1j]) place_poles(A, B, P) # same test with an odd number of real poles > 1 # this is another specific case of YT P = np.array([-2, -3, -4, -1 + 1j, -1 - 1j]) self._check(A, B, P) def test_tricky_B(self): # check we handle as we should the 1 column B matrices and # n column B matrices (with n such as shape(A)=(n, n)) A = np.array([ 1.380, -0.2077, 6.715, -5.676, -0.5814, -4.290, 0, 0.6750, 1.067, 4.273, -6.654, 5.893, 0.0480, 4.273, 1.343, -2.104 ]).reshape(4, 4) B = np.array( [0, 5.679, 1.136, 1.136, 0, 0, -3.146, 0, 1, 2, 3, 4, 5, 6, 7, 8]).reshape(4, 4) # KNV or YT are not called here, it's a specific case with only # one unique solution P = np.array([-0.2, -0.5, -5.0566, -8.6659]) fsf = self._check(A, B, P) # rtol and nb_iter should be set to np.nan as the identity can be # used as transfer matrix assert_equal(fsf.rtol, np.nan) assert_equal(fsf.nb_iter, np.nan) # check with complex poles too as they trigger a specific case in # the specific case :-) P = np.array((-2 + 1j, -2 - 1j, -3, -2)) fsf = self._check(A, B, P) assert_equal(fsf.rtol, np.nan) assert_equal(fsf.nb_iter, np.nan) #now test with a B matrix with only one column (no optimisation) B = B[:, 0].reshape(4, 1) P = np.array((-2 + 1j, -2 - 1j, -3, -2)) fsf = self._check(A, B, P) # we can't optimize anything, check they are set to 0 as expected assert_equal(fsf.rtol, 0) assert_equal(fsf.nb_iter, 0) @pytest.mark.xfail(NumpyVersion(np.__version__) > '1.21.0', reason='fail with new numpy') def test_errors(self): # Test input mistakes from user A = np.array([0, 7, 0, 0, 0, 0, 0, 7 / 3., 0, 0, 0, 0, 0, 0, 0, 0]).reshape(4, 4) B = np.array([0, 0, 0, 0, 1, 0, 0, 1]).reshape(4, 2) #should fail as the method keyword is invalid assert_raises(ValueError, place_poles, A, B, (-2.1, -2.2, -2.3, -2.4), method="foo") #should fail as poles are not 1D array assert_raises(ValueError, place_poles, A, B, np.array((-2.1, -2.2, -2.3, -2.4)).reshape(4, 1)) #should fail as A is not a 2D array assert_raises(ValueError, place_poles, A[:, :, np.newaxis], B, (-2.1, -2.2, -2.3, -2.4)) #should fail as B is not a 2D array assert_raises(ValueError, place_poles, A, B[:, :, np.newaxis], (-2.1, -2.2, -2.3, -2.4)) #should fail as there are too many poles assert_raises(ValueError, place_poles, A, B, (-2.1, -2.2, -2.3, -2.4, -3)) #should fail as there are not enough poles assert_raises(ValueError, place_poles, A, B, (-2.1, -2.2, -2.3)) #should fail as the rtol is greater than 1 assert_raises(ValueError, place_poles, A, B, (-2.1, -2.2, -2.3, -2.4), rtol=42) #should fail as maxiter is smaller than 1 assert_raises(ValueError, place_poles, A, B, (-2.1, -2.2, -2.3, -2.4), maxiter=-42) # should fail as rank(B) is two assert_raises(ValueError, place_poles, A, B, (-2, -2, -2, -2)) #unctrollable system assert_raises(ValueError, place_poles, np.ones((4, 4)), np.ones( (4, 2)), (1, 2, 3, 4)) # Should not raise ValueError as the poles can be placed but should # raise a warning as the convergence is not reached with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") fsf = place_poles(A, B, (-1, -2, -3, -4), rtol=1e-16, maxiter=42) assert_(len(w) == 1) assert_(issubclass(w[-1].category, UserWarning)) assert_("Convergence was not reached after maxiter iterations" in str(w[-1].message)) assert_equal(fsf.nb_iter, 42) # should fail as a complex misses its conjugate assert_raises(ValueError, place_poles, A, B, (-2 + 1j, -2 - 1j, -2 + 3j, -2)) # should fail as A is not square assert_raises(ValueError, place_poles, A[:, :3], B, (-2, -3, -4, -5)) # should fail as B has not the same number of lines as A assert_raises(ValueError, place_poles, A, B[:3, :], (-2, -3, -4, -5)) # should fail as KNV0 does not support complex poles assert_raises(ValueError, place_poles, A, B, (-2 + 1j, -2 - 1j, -2 + 3j, -2 - 3j), method="KNV0")
class DateAttributeTest(TestCase): @dec.skipif(NumpyVersion(np.__version__) < '1.7.0', "No np.datetime64 in Numpy < 1.7.0") def setUp(self): self.data, self.meta = loadarff(test7) @dec.skipif(NumpyVersion(np.__version__) < '1.7.0', "No np.datetime64 in Numpy < 1.7.0") def test_year_attribute(self): expected = np.array([ '1999', '2004', '1817', '2100', '2013', '1631' ], dtype='datetime64[Y]') assert_array_equal(self.data["attr_year"], expected) @dec.skipif(NumpyVersion(np.__version__) < '1.7.0', "No np.datetime64 in Numpy < 1.7.0") def test_month_attribute(self): expected = np.array([ '1999-01', '2004-12', '1817-04', '2100-09', '2013-11', '1631-10' ], dtype='datetime64[M]') assert_array_equal(self.data["attr_month"], expected) @dec.skipif(NumpyVersion(np.__version__) < '1.7.0', "No np.datetime64 in Numpy < 1.7.0") def test_date_attribute(self): expected = np.array([ '1999-01-31', '2004-12-01', '1817-04-28', '2100-09-10', '2013-11-30', '1631-10-15' ], dtype='datetime64[D]') assert_array_equal(self.data["attr_date"], expected) @dec.skipif(NumpyVersion(np.__version__) < '1.7.0', "No np.datetime64 in Numpy < 1.7.0") def test_datetime_local_attribute(self): expected = np.array([ datetime.datetime(year=1999, month=1, day=31, hour=0, minute=1), datetime.datetime(year=2004, month=12, day=1, hour=23, minute=59), datetime.datetime(year=1817, month=4, day=28, hour=13, minute=0), datetime.datetime(year=2100, month=9, day=10, hour=12, minute=0), datetime.datetime(year=2013, month=11, day=30, hour=4, minute=55), datetime.datetime(year=1631, month=10, day=15, hour=20, minute=4) ], dtype='datetime64[m]') assert_array_equal(self.data["attr_datetime_local"], expected) @dec.skipif(NumpyVersion(np.__version__) < '1.7.0', "No np.datetime64 in Numpy < 1.7.0") def test_datetime_missing(self): expected = np.array([ 'nat', '2004-12-01T23:59Z', 'nat', 'nat', '2013-11-30T04:55Z', '1631-10-15T20:04Z' ], dtype='datetime64[m]') assert_array_equal(self.data["attr_datetime_missing"], expected) def test_datetime_timezone(self): assert_raises(ValueError, loadarff, test8)
"""Functions copypasted from newer versions of numpy. """ from __future__ import division, print_function, absolute_import import warnings import numpy as np from scipy._lib._version import NumpyVersion if NumpyVersion(np.__version__) > '1.7.0.dev': _assert_warns = np.testing.assert_warns else: def _assert_warns(warning_class, func, *args, **kw): r""" Fail unless the given callable throws the specified warning. This definition is copypasted from numpy 1.9.0.dev. The version in earlier numpy returns None. Parameters ---------- warning_class : class The class defining the warning that `func` is expected to throw. func : callable The callable to test. *args : Arguments Arguments passed to `func`. **kwargs : Kwargs
from numpy.distutils.system_info import (system_info, numpy_info, NotFoundError, BlasNotFoundError, LapackNotFoundError, AtlasNotFoundError, LapackSrcNotFoundError, BlasSrcNotFoundError, dict_append, get_info as old_get_info) from scipy._lib._version import NumpyVersion if NumpyVersion(np.__version__) >= "1.15.0.dev": # For new enough numpy.distutils, the ACCELERATE=None environment # variable in the top-level setup.py is enough, so no need to # customize BLAS detection. get_info = old_get_info else: # For NumPy < 1.15.0, we need overrides. def get_info(name, notfound_action=0): # Special case our custom *_opt_info. cls = {'lapack_opt': lapack_opt_info, 'blas_opt': blas_opt_info}.get(name.lower()) if cls is None: return old_get_info(name, notfound_action) return cls().get_info(notfound_action)
class TestInterop(object): # # Test that FITPACK-based spl* functions can deal with BSpline objects # def setup_method(self): xx = np.linspace(0, 4. * np.pi, 41) yy = np.cos(xx) b = make_interp_spline(xx, yy) self.tck = (b.t, b.c, b.k) self.xx, self.yy, self.b = xx, yy, b self.xnew = np.linspace(0, 4. * np.pi, 21) c2 = np.c_[b.c, b.c, b.c] self.c2 = np.dstack((c2, c2)) self.b2 = BSpline(b.t, self.c2, b.k) def test_splev(self): xnew, b, b2 = self.xnew, self.b, self.b2 # check that splev works with 1D array of coefficients # for array and scalar `x` assert_allclose(splev(xnew, b), b(xnew), atol=1e-15, rtol=1e-15) assert_allclose(splev(xnew, b.tck), b(xnew), atol=1e-15, rtol=1e-15) assert_allclose([splev(x, b) for x in xnew], b(xnew), atol=1e-15, rtol=1e-15) # With n-D coefficients, there's a quirck: # splev(x, BSpline) is equivalent to BSpline(x) with suppress_warnings() as sup: sup.filter( DeprecationWarning, "Calling splev.. with BSpline objects with c.ndim > 1 is not recommended." ) assert_allclose(splev(xnew, b2), b2(xnew), atol=1e-15, rtol=1e-15) # However, splev(x, BSpline.tck) needs some transposes. This is because # BSpline interpolates along the first axis, while the legacy FITPACK # wrapper does list(map(...)) which effectively interpolates along the # last axis. Like so: sh = tuple(range(1, b2.c.ndim)) + (0, ) # sh = (1, 2, 0) cc = b2.c.transpose(sh) tck = (b2.t, cc, b2.k) assert_allclose(splev(xnew, tck), b2(xnew).transpose(sh), atol=1e-15, rtol=1e-15) def test_splrep(self): x, y = self.xx, self.yy # test that "new" splrep is equivalent to _impl.splrep tck = splrep(x, y) t, c, k = _impl.splrep(x, y) assert_allclose(tck[0], t, atol=1e-15) assert_allclose(tck[1], c, atol=1e-15) assert_equal(tck[2], k) # also cover the `full_output=True` branch tck_f, _, _, _ = splrep(x, y, full_output=True) assert_allclose(tck_f[0], t, atol=1e-15) assert_allclose(tck_f[1], c, atol=1e-15) assert_equal(tck_f[2], k) # test that the result of splrep roundtrips with splev: # evaluate the spline on the original `x` points yy = splev(x, tck) assert_allclose(y, yy, atol=1e-15) # ... and also it roundtrips if wrapped in a BSpline b = BSpline(*tck) assert_allclose(y, b(x), atol=1e-15) @pytest.mark.xfail(NumpyVersion(np.__version__) < '1.14.0', reason='requires NumPy >= 1.14.0') def test_splrep_errors(self): # test that both "old" and "new" splrep raise for an n-D ``y`` array # with n > 1 x, y = self.xx, self.yy y2 = np.c_[y, y] with assert_raises(ValueError): splrep(x, y2) with assert_raises(ValueError): _impl.splrep(x, y2) # input below minimum size with assert_raises(TypeError, match="m > k must hold"): splrep(x[:3], y[:3]) with assert_raises(TypeError, match="m > k must hold"): _impl.splrep(x[:3], y[:3]) def test_splprep(self): x = np.arange(15).reshape((3, 5)) b, u = splprep(x) tck, u1 = _impl.splprep(x) # test the roundtrip with splev for both "old" and "new" output assert_allclose(u, u1, atol=1e-15) assert_allclose(splev(u, b), x, atol=1e-15) assert_allclose(splev(u, tck), x, atol=1e-15) # cover the ``full_output=True`` branch (b_f, u_f), _, _, _ = splprep(x, s=0, full_output=True) assert_allclose(u, u_f, atol=1e-15) assert_allclose(splev(u_f, b_f), x, atol=1e-15) def test_splprep_errors(self): # test that both "old" and "new" code paths raise for x.ndim > 2 x = np.arange(3 * 4 * 5).reshape((3, 4, 5)) with assert_raises(ValueError, match="too many values to unpack"): splprep(x) with assert_raises(ValueError, match="too many values to unpack"): _impl.splprep(x) # input below minimum size x = np.linspace(0, 40, num=3) with assert_raises(TypeError, match="m > k must hold"): splprep([x]) with assert_raises(TypeError, match="m > k must hold"): _impl.splprep([x]) # automatically calculated parameters are non-increasing # see gh-7589 x = [-50.49072266, -50.49072266, -54.49072266, -54.49072266] with assert_raises(ValueError, match="Invalid inputs"): splprep([x]) with assert_raises(ValueError, match="Invalid inputs"): _impl.splprep([x]) # given non-increasing parameter values u x = [1, 3, 2, 4] u = [0, 0.3, 0.2, 1] with assert_raises(ValueError, match="Invalid inputs"): splprep(*[[x], None, u]) def test_sproot(self): b, b2 = self.b, self.b2 roots = np.array([0.5, 1.5, 2.5, 3.5]) * np.pi # sproot accepts a BSpline obj w/ 1D coef array assert_allclose(sproot(b), roots, atol=1e-7, rtol=1e-7) assert_allclose(sproot((b.t, b.c, b.k)), roots, atol=1e-7, rtol=1e-7) # ... and deals with trailing dimensions if coef array is n-D with suppress_warnings() as sup: sup.filter( DeprecationWarning, "Calling sproot.. with BSpline objects with c.ndim > 1 is not recommended." ) r = sproot(b2, mest=50) r = np.asarray(r) assert_equal(r.shape, (3, 2, 4)) assert_allclose(r - roots, 0, atol=1e-12) # and legacy behavior is preserved for a tck tuple w/ n-D coef c2r = b2.c.transpose(1, 2, 0) rr = np.asarray(sproot((b2.t, c2r, b2.k), mest=50)) assert_equal(rr.shape, (3, 2, 4)) assert_allclose(rr - roots, 0, atol=1e-12) def test_splint(self): # test that splint accepts BSpline objects b, b2 = self.b, self.b2 assert_allclose(splint(0, 1, b), splint(0, 1, b.tck), atol=1e-14) assert_allclose(splint(0, 1, b), b.integrate(0, 1), atol=1e-14) # ... and deals with n-D arrays of coefficients with suppress_warnings() as sup: sup.filter( DeprecationWarning, "Calling splint.. with BSpline objects with c.ndim > 1 is not recommended." ) assert_allclose(splint(0, 1, b2), b2.integrate(0, 1), atol=1e-14) # and the legacy behavior is preserved for a tck tuple w/ n-D coef c2r = b2.c.transpose(1, 2, 0) integr = np.asarray(splint(0, 1, (b2.t, c2r, b2.k))) assert_equal(integr.shape, (3, 2)) assert_allclose(integr, splint(0, 1, b), atol=1e-14) def test_splder(self): for b in [self.b, self.b2]: # pad the c array (FITPACK convention) ct = len(b.t) - len(b.c) if ct > 0: b.c = np.r_[b.c, np.zeros((ct, ) + b.c.shape[1:])] for n in [1, 2, 3]: bd = splder(b) tck_d = _impl.splder((b.t, b.c, b.k)) assert_allclose(bd.t, tck_d[0], atol=1e-15) assert_allclose(bd.c, tck_d[1], atol=1e-15) assert_equal(bd.k, tck_d[2]) assert_(isinstance(bd, BSpline)) assert_(isinstance(tck_d, tuple)) # back-compat: tck in and out def test_splantider(self): for b in [self.b, self.b2]: # pad the c array (FITPACK convention) ct = len(b.t) - len(b.c) if ct > 0: b.c = np.r_[b.c, np.zeros((ct, ) + b.c.shape[1:])] for n in [1, 2, 3]: bd = splantider(b) tck_d = _impl.splantider((b.t, b.c, b.k)) assert_allclose(bd.t, tck_d[0], atol=1e-15) assert_allclose(bd.c, tck_d[1], atol=1e-15) assert_equal(bd.k, tck_d[2]) assert_(isinstance(bd, BSpline)) assert_(isinstance(tck_d, tuple)) # back-compat: tck in and out def test_insert(self): b, b2, xx = self.b, self.b2, self.xx j = b.t.size // 2 tn = 0.5 * (b.t[j] + b.t[j + 1]) bn, tck_n = insert(tn, b), insert(tn, (b.t, b.c, b.k)) assert_allclose(splev(xx, bn), splev(xx, tck_n), atol=1e-15) assert_(isinstance(bn, BSpline)) assert_(isinstance(tck_n, tuple)) # back-compat: tck in, tck out # for n-D array of coefficients, BSpline.c needs to be transposed # after that, the results are equivalent. sh = tuple(range(b2.c.ndim)) c_ = b2.c.transpose(sh[1:] + (0, )) tck_n2 = insert(tn, (b2.t, c_, b2.k)) bn2 = insert(tn, b2) # need a transpose for comparing the results, cf test_splev assert_allclose(np.asarray(splev(xx, tck_n2)).transpose(2, 0, 1), bn2(xx), atol=1e-15) assert_(isinstance(bn2, BSpline)) assert_(isinstance(tck_n2, tuple)) # back-compat: tck in, tck out
dense_matrix[dense_matrix > 0.7] = 0 _check_save_and_load(dense_matrix) def test_save_and_load_empty(): dense_matrix = np.zeros((4, 6)) _check_save_and_load(dense_matrix) def test_save_and_load_one_entry(): dense_matrix = np.zeros((4, 6)) dense_matrix[1, 2] = 1 _check_save_and_load(dense_matrix) @pytest.mark.skipif(NumpyVersion(np.__version__) < '1.10.0', reason='disabling unpickling requires numpy >= 1.10.0') def test_malicious_load(): class Executor(object): def __reduce__(self): return (assert_, (False, 'unexpected code execution')) fd, tmpfile = tempfile.mkstemp(suffix='.npz') os.close(fd) try: np.savez(tmpfile, format=Executor()) # Should raise a ValueError, not execute code assert_raises(ValueError, load_npz, tmpfile) finally: os.remove(tmpfile)
from __future__ import division, print_function, absolute_import import numpy import numpy as np from numpy import (exp, log, asarray, arange, newaxis, hstack, product, array, zeros, eye, poly1d, r_, sum, fromstring, isfinite, squeeze, amax, reshape, sign, broadcast_arrays) from scipy._lib._version import NumpyVersion __all__ = ['logsumexp', 'central_diff_weights', 'derivative', 'pade', 'lena', 'ascent', 'face'] _NUMPY_170 = (NumpyVersion(numpy.__version__) >= NumpyVersion('1.7.0')) def logsumexp(a, axis=None, b=None, keepdims=False, return_sign=False): """Compute the log of the sum of exponentials of input elements. Parameters ---------- a : array_like Input array. axis : None or int or tuple of ints, optional Axis or axes over which the sum is taken. By default `axis` is None, and all elements are summed. Tuple of ints is not accepted if NumPy version is lower than 1.7.0. .. versionadded:: 0.11.0
class TestPeriodogram(TestCase): def test_real_onesided_even(self): x = np.zeros(16) x[0] = 1 f, p = periodogram(x) assert_allclose(f, np.linspace(0, 0.5, 9)) q = np.ones(9) q[0] = 0 q[-1] /= 2.0 q /= 8 assert_allclose(p, q) def test_real_onesided_odd(self): x = np.zeros(15) x[0] = 1 f, p = periodogram(x) assert_allclose(f, np.arange(8.0)/15.0) q = np.ones(8) q[0] = 0 q *= 2.0/15.0 assert_allclose(p, q, atol=1e-15) def test_real_twosided(self): x = np.zeros(16) x[0] = 1 f, p = periodogram(x, return_onesided=False) assert_allclose(f, fftpack.fftfreq(16, 1.0)) q = np.ones(16)/16.0 q[0] = 0 assert_allclose(p, q) def test_real_spectrum(self): x = np.zeros(16) x[0] = 1 f, p = periodogram(x, scaling='spectrum') g, q = periodogram(x, scaling='density') assert_allclose(f, np.linspace(0, 0.5, 9)) assert_allclose(p, q/16.0) def test_integer_even(self): x = np.zeros(16, dtype=np.int) x[0] = 1 f, p = periodogram(x) assert_allclose(f, np.linspace(0, 0.5, 9)) q = np.ones(9) q[0] = 0 q[-1] /= 2.0 q /= 8 assert_allclose(p, q) def test_integer_odd(self): x = np.zeros(15, dtype=np.int) x[0] = 1 f, p = periodogram(x) assert_allclose(f, np.arange(8.0)/15.0) q = np.ones(8) q[0] = 0 q *= 2.0/15.0 assert_allclose(p, q, atol=1e-15) def test_integer_twosided(self): x = np.zeros(16, dtype=np.int) x[0] = 1 f, p = periodogram(x, return_onesided=False) assert_allclose(f, fftpack.fftfreq(16, 1.0)) q = np.ones(16)/16.0 q[0] = 0 assert_allclose(p, q) def test_complex(self): x = np.zeros(16, np.complex128) x[0] = 1.0 + 2.0j f, p = periodogram(x) assert_allclose(f, fftpack.fftfreq(16, 1.0)) q = 5.0*np.ones(16)/16.0 q[0] = 0 assert_allclose(p, q) def test_unk_scaling(self): assert_raises(ValueError, periodogram, np.zeros(4, np.complex128), scaling='foo') def test_nd_axis_m1(self): x = np.zeros(20, dtype=np.float64) x = x.reshape((2,1,10)) x[:,:,0] = 1.0 f, p = periodogram(x) assert_array_equal(p.shape, (2, 1, 6)) assert_array_almost_equal_nulp(p[0,0,:], p[1,0,:], 60) f0, p0 = periodogram(x[0,0,:]) assert_array_almost_equal_nulp(p0[np.newaxis,:], p[1,:], 60) def test_nd_axis_0(self): x = np.zeros(20, dtype=np.float64) x = x.reshape((10,2,1)) x[0,:,:] = 1.0 f, p = periodogram(x, axis=0) assert_array_equal(p.shape, (6,2,1)) assert_array_almost_equal_nulp(p[:,0,0], p[:,1,0], 60) f0, p0 = periodogram(x[:,0,0]) assert_array_almost_equal_nulp(p0, p[:,1,0]) def test_window_external(self): x = np.zeros(16) x[0] = 1 f, p = periodogram(x, 10, 'hanning') win = signal.get_window('hanning', 16) fe, pe = periodogram(x, 10, win) assert_array_almost_equal_nulp(p, pe) assert_array_almost_equal_nulp(f, fe) def test_padded_fft(self): x = np.zeros(16) x[0] = 1 f, p = periodogram(x) fp, pp = periodogram(x, nfft=32) assert_allclose(f, fp[::2]) assert_allclose(p, pp[::2]) assert_array_equal(pp.shape, (17,)) def test_empty_input(self): f, p = periodogram([]) assert_array_equal(f.shape, (0,)) assert_array_equal(p.shape, (0,)) for shape in [(0,), (3,0), (0,5,2)]: f, p = periodogram(np.empty(shape)) assert_array_equal(f.shape, shape) assert_array_equal(p.shape, shape) def test_empty_input_other_axis(self): for shape in [(3,0), (0,5,2)]: f, p = periodogram(np.empty(shape), axis=1) assert_array_equal(f.shape, shape) assert_array_equal(p.shape, shape) def test_short_nfft(self): x = np.zeros(18) x[0] = 1 f, p = periodogram(x, nfft=16) assert_allclose(f, np.linspace(0, 0.5, 9)) q = np.ones(9) q[0] = 0 q[-1] /= 2.0 q /= 8 assert_allclose(p, q) def test_nfft_is_xshape(self): x = np.zeros(16) x[0] = 1 f, p = periodogram(x, nfft=16) assert_allclose(f, np.linspace(0, 0.5, 9)) q = np.ones(9) q[0] = 0 q[-1] /= 2.0 q /= 8 assert_allclose(p, q) def test_real_onesided_even_32(self): x = np.zeros(16, 'f') x[0] = 1 f, p = periodogram(x) assert_allclose(f, np.linspace(0, 0.5, 9)) q = np.ones(9, 'f') q[0] = 0 q[-1] /= 2.0 q /= 8 assert_allclose(p, q) assert_(p.dtype == q.dtype) def test_real_onesided_odd_32(self): x = np.zeros(15, 'f') x[0] = 1 f, p = periodogram(x) assert_allclose(f, np.arange(8.0)/15.0) q = np.ones(8, 'f') q[0] = 0 q *= 2.0/15.0 assert_allclose(p, q, atol=1e-7) assert_(p.dtype == q.dtype) @dec.skipif(NumpyVersion(np.__version__) < '1.8.0') def test_real_twosided_32(self): x = np.zeros(16, 'f') x[0] = 1 f, p = periodogram(x, return_onesided=False) assert_allclose(f, fftpack.fftfreq(16, 1.0)) q = np.ones(16, 'f')/16.0 q[0] = 0 assert_allclose(p, q) assert_(p.dtype == q.dtype) @dec.skipif(NumpyVersion(np.__version__) < '1.8.0') def test_complex_32(self): x = np.zeros(16, 'F') x[0] = 1.0 + 2.0j f, p = periodogram(x) assert_allclose(f, fftpack.fftfreq(16, 1.0)) q = 5.0*np.ones(16, 'f')/16.0 q[0] = 0 assert_allclose(p, q) assert_(p.dtype == q.dtype)
from __future__ import division, print_function, absolute_import import inspect import warnings import numpy as np import numpy.testing as npt from scipy._lib._version import NumpyVersion from wafo import stats NUMPY_BELOW_1_7 = NumpyVersion(np.__version__) < '1.7.0' def check_normalization(distfn, args, distname): norm_moment = distfn.moment(0, *args) npt.assert_allclose(norm_moment, 1.0) # this is a temporary plug: either ncf or expect is problematic; # best be marked as a knownfail, but I've no clue how to do it. if distname == "ncf": atol, rtol = 1e-5, 0 else: atol, rtol = 1e-7, 1e-7 normalization_expect = distfn.expect(lambda x: 1, args=args) npt.assert_allclose(normalization_expect, 1.0, atol=atol, rtol=rtol, err_msg=distname,
class TestCSD: def test_pad_shorter_x(self): x = np.zeros(8) y = np.zeros(12) f = np.linspace(0, 0.5, 7) c = np.zeros(7,dtype=np.complex128) f1, c1 = csd(x, y, nperseg=12) assert_allclose(f, f1) assert_allclose(c, c1) def test_pad_shorter_y(self): x = np.zeros(12) y = np.zeros(8) f = np.linspace(0, 0.5, 7) c = np.zeros(7,dtype=np.complex128) f1, c1 = csd(x, y, nperseg=12) assert_allclose(f, f1) assert_allclose(c, c1) def test_real_onesided_even(self): x = np.zeros(16) x[0] = 1 x[8] = 1 f, p = csd(x, x, nperseg=8) assert_allclose(f, np.linspace(0, 0.5, 5)) q = np.array([0.08333333, 0.15277778, 0.22222222, 0.22222222, 0.11111111]) assert_allclose(p, q, atol=1e-7, rtol=1e-7) def test_real_onesided_odd(self): x = np.zeros(16) x[0] = 1 x[8] = 1 f, p = csd(x, x, nperseg=9) assert_allclose(f, np.arange(5.0)/9.0) q = np.array([0.15958227, 0.24193957, 0.24145224, 0.24100919, 0.24377353]) assert_allclose(p, q, atol=1e-7, rtol=1e-7) def test_real_twosided(self): x = np.zeros(16) x[0] = 1 x[8] = 1 f, p = csd(x, x, nperseg=8, return_onesided=False) assert_allclose(f, fftpack.fftfreq(8, 1.0)) q = np.array([0.08333333, 0.07638889, 0.11111111, 0.11111111, 0.11111111, 0.11111111, 0.11111111, 0.07638889]) assert_allclose(p, q, atol=1e-7, rtol=1e-7) def test_real_spectrum(self): x = np.zeros(16) x[0] = 1 x[8] = 1 f, p = csd(x, x, nperseg=8, scaling='spectrum') assert_allclose(f, np.linspace(0, 0.5, 5)) q = np.array([0.015625, 0.02864583, 0.04166667, 0.04166667, 0.02083333]) assert_allclose(p, q, atol=1e-7, rtol=1e-7) def test_integer_onesided_even(self): x = np.zeros(16, dtype=np.int) x[0] = 1 x[8] = 1 f, p = csd(x, x, nperseg=8) assert_allclose(f, np.linspace(0, 0.5, 5)) q = np.array([0.08333333, 0.15277778, 0.22222222, 0.22222222, 0.11111111]) assert_allclose(p, q, atol=1e-7, rtol=1e-7) def test_integer_onesided_odd(self): x = np.zeros(16, dtype=np.int) x[0] = 1 x[8] = 1 f, p = csd(x, x, nperseg=9) assert_allclose(f, np.arange(5.0)/9.0) q = np.array([0.15958227, 0.24193957, 0.24145224, 0.24100919, 0.24377353]) assert_allclose(p, q, atol=1e-7, rtol=1e-7) def test_integer_twosided(self): x = np.zeros(16, dtype=np.int) x[0] = 1 x[8] = 1 f, p = csd(x, x, nperseg=8, return_onesided=False) assert_allclose(f, fftpack.fftfreq(8, 1.0)) q = np.array([0.08333333, 0.07638889, 0.11111111, 0.11111111, 0.11111111, 0.11111111, 0.11111111, 0.07638889]) assert_allclose(p, q, atol=1e-7, rtol=1e-7) def test_complex(self): x = np.zeros(16, np.complex128) x[0] = 1.0 + 2.0j x[8] = 1.0 + 2.0j f, p = csd(x, x, nperseg=8) assert_allclose(f, fftpack.fftfreq(8, 1.0)) q = np.array([0.41666667, 0.38194444, 0.55555556, 0.55555556, 0.55555556, 0.55555556, 0.55555556, 0.38194444]) assert_allclose(p, q, atol=1e-7, rtol=1e-7) def test_unk_scaling(self): assert_raises(ValueError, csd, np.zeros(4, np.complex128), np.ones(4, np.complex128), scaling='foo', nperseg=4) def test_detrend_linear(self): x = np.arange(10, dtype=np.float64) + 0.04 f, p = csd(x, x, nperseg=10, detrend='linear') assert_allclose(p, np.zeros_like(p), atol=1e-15) def test_no_detrending(self): x = np.arange(10, dtype=np.float64) + 0.04 f1, p1 = csd(x, x, nperseg=10, detrend=False) f2, p2 = csd(x, x, nperseg=10, detrend=lambda x: x) assert_allclose(f1, f2, atol=1e-15) assert_allclose(p1, p2, atol=1e-15) def test_detrend_external(self): x = np.arange(10, dtype=np.float64) + 0.04 f, p = csd(x, x, nperseg=10, detrend=lambda seg: signal.detrend(seg, type='l')) assert_allclose(p, np.zeros_like(p), atol=1e-15) def test_detrend_external_nd_m1(self): x = np.arange(40, dtype=np.float64) + 0.04 x = x.reshape((2,2,10)) f, p = csd(x, x, nperseg=10, detrend=lambda seg: signal.detrend(seg, type='l')) assert_allclose(p, np.zeros_like(p), atol=1e-15) def test_detrend_external_nd_0(self): x = np.arange(20, dtype=np.float64) + 0.04 x = x.reshape((2,1,10)) x = np.rollaxis(x, 2, 0) f, p = csd(x, x, nperseg=10, axis=0, detrend=lambda seg: signal.detrend(seg, axis=0, type='l')) assert_allclose(p, np.zeros_like(p), atol=1e-15) def test_nd_axis_m1(self): x = np.arange(20, dtype=np.float64) + 0.04 x = x.reshape((2,1,10)) f, p = csd(x, x, nperseg=10) assert_array_equal(p.shape, (2, 1, 6)) assert_allclose(p[0,0,:], p[1,0,:], atol=1e-13, rtol=1e-13) f0, p0 = csd(x[0,0,:], x[0,0,:], nperseg=10) assert_allclose(p0[np.newaxis,:], p[1,:], atol=1e-13, rtol=1e-13) def test_nd_axis_0(self): x = np.arange(20, dtype=np.float64) + 0.04 x = x.reshape((10,2,1)) f, p = csd(x, x, nperseg=10, axis=0) assert_array_equal(p.shape, (6,2,1)) assert_allclose(p[:,0,0], p[:,1,0], atol=1e-13, rtol=1e-13) f0, p0 = csd(x[:,0,0], x[:,0,0], nperseg=10) assert_allclose(p0, p[:,1,0], atol=1e-13, rtol=1e-13) def test_window_external(self): x = np.zeros(16) x[0] = 1 x[8] = 1 f, p = csd(x, x, 10, 'hanning', 8) win = signal.get_window('hanning', 8) fe, pe = csd(x, x, 10, win, 8) assert_array_almost_equal_nulp(p, pe) assert_array_almost_equal_nulp(f, fe) def test_empty_input(self): f, p = csd([],np.zeros(10)) assert_array_equal(f.shape, (0,)) assert_array_equal(p.shape, (0,)) f, p = csd(np.zeros(10),[]) assert_array_equal(f.shape, (0,)) assert_array_equal(p.shape, (0,)) for shape in [(0,), (3,0), (0,5,2)]: f, p = csd(np.empty(shape), np.empty(shape)) assert_array_equal(f.shape, shape) assert_array_equal(p.shape, shape) f, p = csd(np.ones(10), np.empty((5,0))) assert_array_equal(f.shape, (5,0)) assert_array_equal(p.shape, (5,0)) f, p = csd(np.empty((5,0)), np.ones(10)) assert_array_equal(f.shape, (5,0)) assert_array_equal(p.shape, (5,0)) def test_empty_input_other_axis(self): for shape in [(3,0), (0,5,2)]: f, p = csd(np.empty(shape), np.empty(shape), axis=1) assert_array_equal(f.shape, shape) assert_array_equal(p.shape, shape) f, p = csd(np.empty((10,10,3)), np.zeros((10,0,1)), axis=1) assert_array_equal(f.shape, (10,0,3)) assert_array_equal(p.shape, (10,0,3)) f, p = csd(np.empty((10,0,1)), np.zeros((10,10,3)), axis=1) assert_array_equal(f.shape, (10,0,3)) assert_array_equal(p.shape, (10,0,3)) def test_short_data(self): x = np.zeros(8) x[0] = 1 with warnings.catch_warnings(): warnings.simplefilter('ignore', UserWarning) f, p = csd(x, x) f1, p1 = csd(x, x, nperseg=8) assert_allclose(f, f1) assert_allclose(p, p1) def test_window_long_or_nd(self): with warnings.catch_warnings(): warnings.simplefilter('ignore', UserWarning) assert_raises(ValueError, csd, np.zeros(4), np.ones(4), 1, np.array([1,1,1,1,1])) assert_raises(ValueError, csd, np.zeros(4), np.ones(4), 1, np.arange(6).reshape((2,3))) def test_nondefault_noverlap(self): x = np.zeros(64) x[::8] = 1 f, p = csd(x, x, nperseg=16, noverlap=4) q = np.array([0, 1./12., 1./3., 1./5., 1./3., 1./5., 1./3., 1./5., 1./6.]) assert_allclose(p, q, atol=1e-12) def test_bad_noverlap(self): assert_raises(ValueError, csd, np.zeros(4), np.ones(4), 1, 'hanning', 2, 7) def test_nfft_too_short(self): assert_raises(ValueError, csd, np.ones(12), np.zeros(12), nfft=3, nperseg=4) def test_real_onesided_even_32(self): x = np.zeros(16, 'f') x[0] = 1 x[8] = 1 f, p = csd(x, x, nperseg=8) assert_allclose(f, np.linspace(0, 0.5, 5)) q = np.array([0.08333333, 0.15277778, 0.22222222, 0.22222222, 0.11111111], 'f') assert_allclose(p, q, atol=1e-7, rtol=1e-7) assert_(p.dtype == q.dtype) def test_real_onesided_odd_32(self): x = np.zeros(16, 'f') x[0] = 1 x[8] = 1 f, p = csd(x, x, nperseg=9) assert_allclose(f, np.arange(5.0)/9.0) q = np.array([0.15958227, 0.24193957, 0.24145224, 0.24100919, 0.24377353], 'f') assert_allclose(p, q, atol=1e-7, rtol=1e-7) assert_(p.dtype == q.dtype) @dec.skipif(NumpyVersion(np.__version__) < '1.8.0') def test_real_twosided_32(self): x = np.zeros(16, 'f') x[0] = 1 x[8] = 1 f, p = csd(x, x, nperseg=8, return_onesided=False) assert_allclose(f, fftpack.fftfreq(8, 1.0)) q = np.array([0.08333333, 0.07638889, 0.11111111, 0.11111111, 0.11111111, 0.11111111, 0.11111111, 0.07638889], 'f') assert_allclose(p, q, atol=1e-7, rtol=1e-7) assert_(p.dtype == q.dtype) @dec.skipif(NumpyVersion(np.__version__) < '1.8.0') def test_complex_32(self): x = np.zeros(16, 'F') x[0] = 1.0 + 2.0j x[8] = 1.0 + 2.0j f, p = csd(x, x, nperseg=8) assert_allclose(f, fftpack.fftfreq(8, 1.0)) q = np.array([0.41666666, 0.38194442, 0.55555552, 0.55555552, 0.55555558, 0.55555552, 0.55555552, 0.38194442], 'f') assert_allclose(p, q, atol=1e-7, rtol=1e-7) assert_(p.dtype == q.dtype, 'dtype mismatch, %s, %s' % (p.dtype, q.dtype)) def test_padded_freqs(self): x = np.zeros(12) y = np.ones(12) nfft = 24 f = fftpack.fftfreq(nfft, 1.0)[:nfft//2+1] f[-1] *= -1 fodd, _ = csd(x, y, nperseg=5, nfft=nfft) feven, _ = csd(x, y, nperseg=6, nfft=nfft) assert_allclose(f, fodd) assert_allclose(f, feven) nfft = 25 f = fftpack.fftfreq(nfft, 1.0)[:(nfft + 1)//2] fodd, _ = csd(x, y, nperseg=5, nfft=nfft) feven, _ = csd(x, y, nperseg=6, nfft=nfft) assert_allclose(f, fodd) assert_allclose(f, feven)