Example #1
0
 def test_padecases_dtype_sparse_complex(self):
     # float32 and complex64 lead to errors in spsolve/UMFpack
     dtype = np.complex128
     for scale in [1e-2, 1e-1, 5e-1, 1, 10]:
         a = scale * speye(3, 3, dtype=dtype, format='csc')
         e = exp(scale) * eye(3, dtype=dtype)
         assert_array_almost_equal_nulp(expm(a).toarray(), e, nulp=100)
Example #2
0
 def test_interp_linear_array_fill(self):
     x = np.linspace(1., 10., 10)
     y = self.f(x)
     xval = np.linspace(0., 10., 100)
     ref = self.f(xval)
     ref[xval < x[0]] = -2.
     assert_array_almost_equal_nulp(self.interp(x, y, xval, bounds_error=False, fill_value=-2.), ref, 10)
Example #3
0
    def test_size_accuracy_large(self, size):
        x = np.random.rand(size, 3) + 1j*np.random.rand(size, 3)
        y1 = fftn(x.real.astype(np.float32))
        y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)

        assert_equal(y1.dtype, np.complex64)
        assert_array_almost_equal_nulp(y1, y2, 2000)
Example #4
0
    def test_unity_1_withnan(self, boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, inval, outval):
        """
        Test that a unit kernel with three elements returns the same array
        (except when boundary is None). This version includes a NaN value in
        the original array.
        """

        x = inval

        y = np.array([1.0], dtype="float64")

        z = convolve_fft(
            x,
            y,
            boundary=boundary,
            interpolate_nan=interpolate_nan,
            normalize_kernel=normalize_kernel,
            ignore_edge_zeros=ignore_edge_zeros,
        )

        # for whatever reason, numpy's fft has very limited precision, and
        # the comparison fails unless you cast the float64 to a float16
        if hasattr(np, "float16"):
            assert_array_almost_equal_nulp(
                np.asarray(z, dtype=np.float16), np.array([1.0, 0.0, 3.0], dtype=np.float16), 10
            )
        assert np.all(np.abs(z - outval) < 1e-14)
Example #5
0
    def test_unity_3x3_withnan(self, boundary, interpolate_nan,
                               normalize_kernel, ignore_edge_zeros):
        '''
        Test that a 3x3 unit kernel returns the same array (except when
        boundary is None). This version includes a NaN value in the original
        array.
        '''

        x = np.array([[1., 2., 3.],
                      [4., np.nan, 6.],
                      [7., 8., 9.]], dtype='float64')

        y = np.array([[0., 0., 0.],
                      [0., 1., 0.],
                      [0., 0., 0.]], dtype='float64')

        z = convolve_fft(x, y, boundary=boundary,
                         interpolate_nan=interpolate_nan,
                         normalize_kernel=normalize_kernel,
                         ignore_edge_zeros=ignore_edge_zeros,
                         )

        a = x
        a[1, 1] = 0

        # for whatever reason, numpy's fft has very limited precision, and
        # the comparison fails unless you cast the float64 to a float16
        if hasattr(np, 'float16'):
            assert_array_almost_equal_nulp(np.asarray(z, dtype=np.float16),
                                           np.asarray(a, dtype=np.float16), 10)
        assert np.all(np.abs(z - a) < 1e-14)
Example #6
0
 def test_apply(self, k, flag):
     """flag allows the specification of various calling sequences:
     - flag = 0: apply(b, tau)
     - flag = 1: apply(b, tau, tau)
     - flag = 2: apply(b, tau, eta)
     """
     mat = self.material()
     expected = green_matrix(k,  mat)
     green = mat.green_operator()
     tau = np.zeros((green.isize,), np.float64)
     if flag == 0:
         base = None
     elif flag == 1:
         base = tau
     elif flag == 2:
         base = np.empty((green.osize,), np.float64)
     else:
         raise ValueError()
     for j in range(green.isize):
         tau[:] = 0.
         tau[j] = 1.
         green.set_frequency(k)
         actual = green.apply(tau, base)
         if flag != 0:
             assert actual.base is base
         assert_array_almost_equal_nulp(expected[:, j], actual, 325)
 def test_padecases_dtype(self):
     for dtype in [np.float32, np.float64, np.complex64, np.complex128]:
         # test double-precision cases
         for scale in [1e-2, 1e-1, 5e-1, 1, 10]:
             a = scale * eye(3, dtype=dtype)
             e = exp(scale) * eye(3, dtype=dtype)
             assert_array_almost_equal_nulp(expm(a), e, nulp=100)
Example #8
0
    def testArrays(self):
        arr = np.arange(100);

        arr = arr.reshape((10, 10))
        assert_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
        assert_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)

        arr = arr.reshape((5, 5, 4))
        assert_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
        assert_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)

        arr = arr.reshape((100, 1))
        assert_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
        assert_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)

        arr = np.arange(96);
        arr = arr.reshape((2, 2, 2, 2, 3, 2))
        assert_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
        assert_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)

        l = ['a', list(), dict(), dict(), list(),
             42, 97.8, ['a', 'b'], {'key': 'val'}]
        arr = np.array(l)
        assert_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)

        arr = np.arange(100.202, 200.202, 1, dtype=np.float32);
        arr = arr.reshape((5, 5, 4))
        outp = np.array(ujson.decode(ujson.encode(arr)), dtype=np.float32)
        assert_array_almost_equal_nulp(arr, outp)
        outp = ujson.decode(ujson.encode(arr), numpy=True, dtype=np.float32)
        assert_array_almost_equal_nulp(arr, outp)
Example #9
0
    def test_unity_3_withnan(self, boundary, interpolate_nan, normalize_kernel,
                             ignore_edge_zeros):
        '''
        Test that a unit kernel with three elements returns the same array
        (except when boundary is None). This version includes a NaN value in
        the original array.
        '''

        x = np.array([1., np.nan, 3.], dtype='float64')

        y = np.array([0., 1., 0.], dtype='float64')

        z = convolve_fft(x, y, boundary=boundary,
                         interpolate_nan=interpolate_nan,
                         normalize_kernel=normalize_kernel,
                         ignore_edge_zeros=ignore_edge_zeros)

        # for whatever reason, numpy's fft has very limited precision, and
        # the comparison fails unless you cast the float64 to a float16
        if hasattr(np, 'float16'):
            assert_array_almost_equal_nulp(
                np.asarray(z, dtype=np.float16),
                np.array([1., 0., 3.], dtype=np.float16), 10)
        # ASSERT equality to better than 16 bit but worse than 32 bit precision
        assert np.all(np.abs(z - np.array([1., 0., 3.])) < 1e-14)
Example #10
0
    def test_unity_1x1_none(self, boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, fft_type):
        """
        Test that a 1x1 unit kernel returns the same array
        """

        if fft_type == "fftw" and not HAS_FFTW:
            pytest.skip("fftw3 is not installed")
        elif fft_type == "scipy" and not HAS_SCIPY:
            pytest.skip("scipy is not installed")

        x = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], dtype="float64")

        y = np.array([[1.0]], dtype="float64")

        z = convolve_fft(
            x,
            y,
            boundary=boundary,
            interpolate_nan=interpolate_nan,
            normalize_kernel=normalize_kernel,
            ignore_edge_zeros=ignore_edge_zeros,
            fft_type=fft_type,
        )

        assert_array_almost_equal_nulp(z, x, 10)
Example #11
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)
Example #12
0
    def test_unity_1_withnan(self, boundary, interpolate_nan, normalize_kernel,
            ignore_edge_zeros, fft_type):
        '''
        Test that a unit kernel with three elements returns the same array
        (except when boundary is None). This version includes a NaN value in
        the original array.
        '''

        if fft_type == 'fftw' and not HAS_FFTW:
            pytest.skip('fftw3 is not installed')
        elif fft_type == 'scipy' and not HAS_SCIPY:
            pytest.skip('scipy is not installed')

        x = np.array([1., np.nan, 3.], dtype='float64')

        y = np.array([1.], dtype='float64')

        z = convolve_fft(x, y, boundary=boundary,
                interpolate_nan=interpolate_nan,
                normalize_kernel=normalize_kernel,
                ignore_edge_zeros=ignore_edge_zeros,
                fft_type=fft_type)

        if fft_type == 'numpy':
            # for whatever reason, numpy's fft has very limited precision, and
            # the comparison fails unless you cast the float64 to a float16
            # np1.4 incompatible assert_array_almost_equal_nulp(np.asarray(z, dtype=np.float16), np.array([1.,0.,3.], dtype=np.float16), 10)
            assert np.all(np.abs(z - np.array([1., 0., 3.])) < 1e-14)
        else:
            assert_array_almost_equal_nulp(z, np.array([1., 0., 3.], dtype='float64'), 10)
Example #13
0
    def test_float16_input_large(self, size):
        x = np.random.rand(size, 3) + 1j*np.random.rand(size, 3)
        y1 = fftn(x.real.astype(np.float16))
        y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)

        assert_equal(y1.dtype, np.complex64)
        assert_array_almost_equal_nulp(y1, y2, 2e6)
Example #14
0
def test_mask():
    length = 100
    ramps = [np.linspace(0, 4 * np.pi, length),
             np.linspace(0, 8 * np.pi, length),
             np.linspace(0, 6 * np.pi, length)]
    image = np.vstack(ramps)
    mask_1d = np.ones((length,), dtype=np.bool)
    mask_1d[0] = mask_1d[-1] = False
    for i in range(len(ramps)):
        # mask all ramps but the i'th one
        mask = np.zeros(image.shape, dtype=np.bool)
        mask |= mask_1d.reshape(1, -1)
        mask[i, :] = False   # unmask i'th ramp
        image_wrapped = np.ma.array(np.angle(np.exp(1j * image)), mask=mask)
        image_unwrapped = unwrap_phase(image_wrapped)
        image_unwrapped -= image_unwrapped[0, 0]    # remove phase shift
        # The end of the unwrapped array should have value equal to the
        # endpoint of the unmasked ramp
        assert_array_almost_equal_nulp(image_unwrapped[:, -1], image[i, -1])

        # Same tests, but forcing use of the 3D unwrapper by reshaping
        image_wrapped_3d = image_wrapped.reshape((1,) + image_wrapped.shape)
        image_unwrapped_3d = unwrap_phase(image_wrapped_3d)
        image_unwrapped_3d -= image_unwrapped_3d[0, 0, 0]  # remove phase shift
        assert_array_almost_equal_nulp(image_unwrapped_3d[:, :, -1], image[i, -1])
Example #15
0
    def test_unity_1_withnan(self, boundary, nan_treatment, normalize_kernel,
                             preserve_nan, inval, outval):
        '''
        Test that a unit kernel with three elements returns the same array
        (except when boundary is None). This version includes a NaN value in
        the original array.
        '''

        x = inval

        y = np.array([1.], dtype='float64')

        z = convolve_fft(x, y, boundary=boundary,
                         nan_treatment=nan_treatment,
                         normalize_kernel=normalize_kernel,
                         preserve_nan=preserve_nan)

        if preserve_nan:
            assert np.isnan(z[1])

        z = np.nan_to_num(z)

        # for whatever reason, numpy's fft has very limited precision, and
        # the comparison fails unless you cast the float64 to a float16
        if hasattr(np, 'float16'):
            assert_array_almost_equal_nulp(np.asarray(z, dtype=np.float16),
                                           np.array([1., 0., 3.], dtype=np.float16), 10)
        assert_allclose(z, outval, atol=1e-14)
Example #16
0
    def test_unity_3x3(self, boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, fft_type):
        '''
        Test that a 3x3 unit kernel returns the same array (except when
        boundary is None).
        '''

        if fft_type == 'fftw' and not HAS_FFTW:
            pytest.skip('fftw3 is not installed')
        elif fft_type == 'scipy' and not HAS_SCIPY:
            pytest.skip('scipy is not installed')

        x = np.array([[1., 2., 3.],
                      [4., 5., 6.],
                      [7., 8., 9.]], dtype='float64')

        y = np.array([[0., 0., 0.],
                      [0., 1., 0.],
                      [0., 0., 0.]], dtype='float64')

        z = convolve_fft(x, y, boundary=boundary,
                interpolate_nan=interpolate_nan,
                normalize_kernel=normalize_kernel,
                ignore_edge_zeros=ignore_edge_zeros,
                fft_type=fft_type)

        assert_array_almost_equal_nulp(z, x, 10)
Example #17
0
    def test_image_attributes_distance(self):

        image = self.m.get_image(group=0, units='ergs/cm^2/s', distance=100.)

        assert image.x_min == -1.
        assert image.x_max == +2.
        assert image.y_min == -3.
        assert image.y_max == +4.

        lon_min = np.degrees(np.arctan(-1. / 100.))
        lon_max = np.degrees(np.arctan(+2. / 100.))
        lat_min = np.degrees(np.arctan(-3. / 100.))
        lat_max = np.degrees(np.arctan(+4. / 100.))

        assert_array_almost_equal_nulp(image.lon_min, lon_min, 5)
        assert_array_almost_equal_nulp(image.lon_max, lon_max, 5)
        assert_array_almost_equal_nulp(image.lat_min, lat_min, 5)
        assert_array_almost_equal_nulp(image.lat_max, lat_max, 5)

        pix_area_sr = np.radians(lon_max - lon_min) * np.radians(lat_max - lat_min) / 200

        assert_array_almost_equal_nulp(image.pix_area_sr, pix_area_sr, 5)

        assert image.distance == 100.

        assert not image.inside_observer

        assert image.units == 'ergs/cm^2/s'

        assert image.nu.shape == (5,)
        assert image.wav.shape == (5,)
        assert image.val.shape == (2, 20, 10, 5)
Example #18
0
    def test_unity_3(self, boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, fft_type):
        """
        Test that a unit kernel with three elements returns the same array
        (except when boundary is None).
        """

        if fft_type == "fftw" and not HAS_FFTW:
            pytest.skip("fftw3 is not installed")
        elif fft_type == "scipy" and not HAS_SCIPY:
            pytest.skip("scipy is not installed")

        x = np.array([1.0, 2.0, 3.0], dtype="float64")

        y = np.array([0.0, 1.0, 0.0], dtype="float64")

        z = convolve_fft(
            x,
            y,
            boundary=boundary,
            interpolate_nan=interpolate_nan,
            normalize_kernel=normalize_kernel,
            ignore_edge_zeros=ignore_edge_zeros,
            fft_type=fft_type,
        )

        assert_array_almost_equal_nulp(z, x, 10)
Example #19
0
 def test_padecases_dtype_float(self):
     for dtype in [np.float32, np.float64]:
         for scale in [1e-2, 1e-1, 5e-1, 1, 10]:
             A = scale * eye(3, dtype=dtype)
             observed = expm(A)
             expected = exp(scale) * eye(3, dtype=dtype)
             assert_array_almost_equal_nulp(observed, expected, nulp=100)
Example #20
0
    def test_unity_3_withnan(self, boundary, interpolate_nan, normalize_kernel, ignore_edge_zeros, fft_type):
        """
        Test that a unit kernel with three elements returns the same array
        (except when boundary is None). This version includes a NaN value in
        the original array.
        """

        if fft_type == "fftw" and not HAS_FFTW:
            pytest.skip("fftw3 is not installed")
        elif fft_type == "scipy" and not HAS_SCIPY:
            pytest.skip("scipy is not installed")

        x = np.array([1.0, np.nan, 3.0], dtype="float64")

        y = np.array([0.0, 1.0, 0.0], dtype="float64")

        z = convolve_fft(
            x,
            y,
            boundary=boundary,
            interpolate_nan=interpolate_nan,
            normalize_kernel=normalize_kernel,
            ignore_edge_zeros=ignore_edge_zeros,
            fft_type=fft_type,
        )

        if fft_type == "numpy":
            # for whatever reason, numpy's fft has very limited precision, and
            # the comparison fails unless you cast the float64 to a float16
            # REMOVED because of numpy 1.4 incompatibility assert_array_almost_equal_nulp(np.asarray(z, dtype=np.float16), np.array([1.,0.,3.], dtype=np.float16), 10)
            # ASSERT equality to better than 16 bit but worse than 32 bit precision
            assert np.all(np.abs(z - np.array([1.0, 0.0, 3.0])) < 1e-14)
        else:
            assert_array_almost_equal_nulp(z, np.array([1.0, 0.0, 3.0], dtype="float64"), 10)
Example #21
0
    def test_uniform_3_withnan(self, boundary, nan_treatment,
                               normalize_kernel):
        '''
        Test that the different modes are producing the correct results using
        a uniform kernel with three elements. This version includes a NaN
        value in the original array.
        '''

        x = np.array([1., np.nan, 3.], dtype='float64')

        y = np.array([1., 1., 1.], dtype='float64')

        # if nan_treatment and not normalize_kernel:
        #     with pytest.raises(ValueError):
        #         z = convolve_fft(x, y, boundary=boundary,
        #                          nan_treatment=nan_treatment,
        #                          normalize_kernel=normalize_kernel,
        #                          ignore_edge_zeros=ignore_edge_zeros)
        #     return

        z = convolve_fft(x, y, boundary=boundary,
                         nan_treatment=nan_treatment,
                         normalize_kernel=normalize_kernel)

        answer_dict = {
            'sum': np.array([1., 4., 3.], dtype='float64'),
            'sum_nozeros': np.array([1., 4., 3.], dtype='float64'),
            'sum_zeros': np.array([1., 4., 3.], dtype='float64'),
            'sum_nozeros_interpnan': np.array([1., 4., 3.], dtype='float64'),
            'average': np.array([1., 2., 3.], dtype='float64'),
            'sum_wrap': np.array([4., 4., 4.], dtype='float64'),
            'average_wrap': np.array([4/3., 4/3., 4/3.], dtype='float64'),
            'average_wrap_interpnan': np.array([2, 2, 2], dtype='float64'),
            'average_nozeros': np.array([1/2., 4/3., 3/2.], dtype='float64'),
            #'average_nozeros_interpnan': np.array([1 / 2., 4 / 3., 3 / 2.], dtype='float64'),
            'average_nozeros_interpnan': np.array([1., 2., 3.], dtype='float64'),
            'average_zeros': np.array([1 / 3., 4 / 3., 3 / 3.], dtype='float64'),
            'average_zeros_interpnan': np.array([1 / 2., 4 / 2., 3 / 2.], dtype='float64'),
        }

        for key in list(answer_dict.keys()):
            if 'sum' in key:
                answer_dict[key+"_interpnan"] = answer_dict[key] * 3./2.

        if normalize_kernel:
            answer_key = 'average'
        else:
            answer_key = 'sum'

        if boundary == 'wrap':
            answer_key += '_wrap'
        else:
            # average = average_zeros; sum = sum_zeros
            answer_key += '_zeros'

        if nan_treatment == 'interpolate':
            answer_key += '_interpnan'

        assert_array_almost_equal_nulp(z, answer_dict[answer_key], 10)
Example #22
0
 def test_nd_axis_0(self):
     x = np.arange(20, dtype=np.float64)+0.04
     x = x.reshape((10,2,1))
     f, p = welch(x, nperseg=10, axis=0)
     assert_array_equal(p.shape, (6,2,1))
     assert_array_almost_equal_nulp(p[:,0,0], p[:,1,0], 60)
     f0, p0 = welch(x[:,0,0], nperseg=10)
     assert_array_almost_equal_nulp(p0, p[:,1,0])
Example #23
0
 def test_definition_float16(self):
     x = [[1, 2, 3],
          [4, 5, 6],
          [7, 8, 9]]
     y = fftn(np.array(x, np.float16))
     assert_equal(y.dtype, np.complex64)
     y_r = np.array(fftn(x), np.complex64)
     assert_array_almost_equal_nulp(y, y_r)
Example #24
0
def test_power_law_envelope_mass_calc():
    e = PowerLawEnvelope()
    e.rmin = 9.
    e.rmax = 18.
    e.r_0 = 3.
    e.power = -1.
    e.rho_0 = 2.
    assert_array_almost_equal_nulp(e.mass, 4. * np.pi * 27 * 27, 2)
Example #25
0
 def test_window_external(self):
     x = np.zeros(16)
     x[0] = 1
     f, p = periodogram(x, 10, 'hann')
     win = signal.get_window('hann', 16)
     fe, pe = periodogram(x, 10, win)
     assert_array_almost_equal_nulp(p, pe)
     assert_array_almost_equal_nulp(f, fe)
Example #26
0
    def testFloatArray(self):
        arr = np.arange(12.5, 185.72, 1.7322, dtype=np.float)
        dtypes = (np.float, np.float32, np.float64)

        for dtype in dtypes:
            inpt = arr.astype(dtype)
            outp = np.array(ujson.decode(ujson.encode(inpt, double_precision=15)), dtype=dtype)
            assert_array_almost_equal_nulp(inpt, outp)
Example #27
0
def test_power_law_envelope_rho0_calc():
    e = PowerLawEnvelope()
    e.rmin = 9.0
    e.rmax = 18.0
    e.r_0 = 3.0
    e.power = -1.0
    e.mass = 4.0 * np.pi * 27 * 27
    assert_array_almost_equal_nulp(e.rho_0, 2.0, 2)
Example #28
0
def test_random_stochastic_matrix_sparse():
    sparse = True
    n, k = 5, 3
    Ps = [random_stochastic_matrix(n, sparse=sparse),
          random_stochastic_matrix(n, k, sparse=sparse)]
    for P in Ps:
        ok_(np.all(P.data >= 0))
        assert_array_almost_equal_nulp(P.sum(axis=1), np.ones(n))
Example #29
0
    def test_definition(self):
        x = [[1,2,3],[4,5,6],[7,8,9]]
        y = fftn(np.array(x, np.float32))
        if not y.dtype == np.complex64:
            raise ValueError("double precision output with single precision")

        y_r = np.array(fftn(x), np.complex64)
        assert_array_almost_equal_nulp(y, y_r)
Example #30
0
def test_random_stochastic_matrix_k_1():
    n, k = 3, 1
    P_dense = random_stochastic_matrix(n, k, sparse=False)
    P_sparse = random_stochastic_matrix(n, k, sparse=True)
    assert_array_equal(P_dense[P_dense != 0], np.ones(n))
    assert_array_equal(P_sparse.data, np.ones(n))
    for P in [P_dense, P_sparse]:
        assert_array_almost_equal_nulp(P.sum(axis=1), np.ones(n))
Example #31
0
def test_to_float_with_max_value_specified(max_value):
    img = np.ones((100, 100, 3), dtype=np.uint16)
    expected = img.astype('float32') / max_value
    assert_array_almost_equal_nulp(F.to_float(img, max_value=max_value), expected)
 def assert_array_almost_equal_nulp(self, *args, **kwargs):
     """
     Compare two arrays relatively to their spacing.
     """
     return assert_array_almost_equal_nulp(*args, **kwargs)
Example #33
0
def test_normalize():
    img = np.ones((100, 100, 3), dtype=np.uint8) * 127
    normalized = F.normalize(img, mean=50, std=3)
    expected = (np.ones((100, 100, 3), dtype=np.float32) * 127 / 255 - 50) / 3
    assert_array_almost_equal_nulp(normalized, expected)
Example #34
0
def test_loglog_subset_special2(xmin, xmax):
    # Special case for loglog is close to y = x^-1
    x = np.array([1., 2., 3., 4., 5.])
    y = x**-1.1
    assert_array_almost_equal_nulp(integrate_loglog_subset(x, y, xmin, xmax),
                                   -10. * (xmax**-0.1 - xmin**-0.1), 50)
Example #35
0
def test_set_alpha_range(plane):
    plane.set_alpha_range()
    new_alpha_range = plane.alpha_range
    x = list(range(-10, 13))
    npt.assert_array_almost_equal_nulp(new_alpha_range, x, nulp=0)
Example #36
0
def test_clip_float():
    img = np.array([[-0.02, 0], [0.5, 2.2]], dtype=np.float32)
    expected = np.array([[0, 0], [0.5, 1.0]], dtype=np.float32)
    clipped = F.clip(img, dtype=np.float32, maxval=1.0)
    assert_array_almost_equal_nulp(clipped, expected)
Example #37
0
def test_eig():
    size = 10
    ci = chinfo3
    l = gen_random_legcharge(ci, size)
    A = npc.Array.from_func(np.random.random, [l, l.conj()],
                            qtotal=None,
                            shape_kw='size')
    print("hermitian A")
    A += A.conj().itranspose()
    Aflat = A.to_ndarray()
    W, V = npc.eigh(A, sort='m>')
    V.test_sanity()
    V_W = V.scale_axis(W, axis=-1)
    recalc = npc.tensordot(V_W, V.conj(), axes=[1, 1])
    npt.assert_array_almost_equal_nulp(Aflat, recalc.to_ndarray(), size**3)
    Wflat, Vflat = np.linalg.eigh(Aflat)
    npt.assert_array_almost_equal_nulp(np.sort(W), Wflat, size**3)
    W2 = npc.eigvalsh(A, sort='m>')
    npt.assert_array_almost_equal_nulp(W, W2, size**3)

    print("check complex B")
    B = 1.j * npc.Array.from_func(np.random.random, [l, l.conj()],
                                  shape_kw='size')
    B += B.conj().itranspose()
    B = A + B
    Bflat = B.to_ndarray()
    W, V = npc.eigh(B, sort='m>')
    V.test_sanity()
    recalc = npc.tensordot(V.scale_axis(W, axis=-1), V.conj(), axes=[1, 1])
    npt.assert_array_almost_equal_nulp(Bflat, recalc.to_ndarray(), size**3)
    Wflat, Vflat = np.linalg.eigh(Bflat)
    npt.assert_array_almost_equal_nulp(np.sort(W), Wflat, size**3)

    print("calculate without 'hermitian' knownledge")
    W, V = npc.eig(B, sort='m>')
    assert (np.max(np.abs(W.imag)) < EPS * size**3)
    npt.assert_array_almost_equal_nulp(np.sort(W.real), Wflat, size**3)

    print("sparse speigs")
    qi = 1
    ch_sect = B.legs[0].get_charge(qi)
    k = min(3, B.legs[0].slices[qi + 1] - B.legs[0].slices[qi])
    Wsp, Vsp = npc.speigs(B, ch_sect, k=k, which='LM')
    for W_i, V_i in zip(Wsp, Vsp):
        V_i.test_sanity()
        diff = npc.tensordot(B, V_i, axes=1) - V_i * W_i
        assert (npc.norm(diff, np.inf) < EPS * size**3)

    print("for trivial charges")
    A = npc.Array.from_func(np.random.random, [lcTr, lcTr.conj()],
                            shape_kw='size')
    A = A + A.conj().itranspose()
    Aflat = A.to_ndarray()
    W, V = npc.eigh(A)
    recalc = npc.tensordot(V.scale_axis(W, axis=-1), V.conj(), axes=[1, 1])
    npt.assert_array_almost_equal_nulp(Aflat, recalc.to_ndarray(),
                                       5 * A.shape[0]**3)
Example #38
0
 def test_random_complex(self, maxnlp, size):
     x = random([size, size]) + 1j * random([size, size])
     assert_array_almost_equal_nulp(ifftn(fftn(x)), x, maxnlp)
     assert_array_almost_equal_nulp(fftn(ifftn(x)), x, maxnlp)
Example #39
0
def assert_csc_almost_equal(r, l):
    r = csc_matrix(r)
    l = csc_matrix(l)
    assert_equal(r.indptr, l.indptr)
    assert_equal(r.indices, l.indices)
    assert_array_almost_equal_nulp(r.data, l.data, 10000)
Example #40
0
def test_random_flip_float(code, func, target):
    img = np.array([[0.4, 0.4, 0.4], [0.0, 0.4, 0.4], [0.0, 0.0, 0.4]],
                   dtype=np.float32)
    img = convert_2d_to_target_format([img], target=target)
    assert_array_almost_equal_nulp(F.random_flip(img, code), func(img))
Example #41
0
def test_from_float_with_max_value_specified(max_value):
    img = np.ones((100, 100, 3), dtype=np.float32)
    expected = (img * max_value).astype(np.uint32)
    assert_array_almost_equal_nulp(
        F.from_float(img, dtype=np.uint32, max_value=max_value), expected)
Example #42
0
def test_from_float_without_max_value_specified(dtype, multiplier):
    img = np.ones((100, 100, 3), dtype=np.float32)
    expected = (img * multiplier).astype(dtype)
    assert_array_almost_equal_nulp(F.from_float(img, np.dtype(dtype)),
                                   expected)
Example #43
0
    def test_uniform_3_withnan(self, boundary, nan_treatment,
                               normalize_kernel):
        '''
        Test that the different modes are producing the correct results using
        a uniform kernel with three elements. This version includes a NaN
        value in the original array.
        '''

        x = np.array([1., np.nan, 3.], dtype='float64')

        y = np.array([1., 1., 1.], dtype='float64')

        # if nan_treatment and not normalize_kernel:
        #     with pytest.raises(ValueError):
        #         z = convolve_fft(x, y, boundary=boundary,
        #                          nan_treatment=nan_treatment,
        #                          normalize_kernel=normalize_kernel,
        #                          ignore_edge_zeros=ignore_edge_zeros)
        #     return

        z = convolve_fft(x,
                         y,
                         boundary=boundary,
                         nan_treatment=nan_treatment,
                         normalize_kernel=normalize_kernel)

        answer_dict = {
            'sum':
            np.array([1., 4., 3.], dtype='float64'),
            'sum_nozeros':
            np.array([1., 4., 3.], dtype='float64'),
            'sum_zeros':
            np.array([1., 4., 3.], dtype='float64'),
            'sum_nozeros_interpnan':
            np.array([1., 4., 3.], dtype='float64'),
            'average':
            np.array([1., 2., 3.], dtype='float64'),
            'sum_wrap':
            np.array([4., 4., 4.], dtype='float64'),
            'average_wrap':
            np.array([4 / 3., 4 / 3., 4 / 3.], dtype='float64'),
            'average_wrap_interpnan':
            np.array([2, 2, 2], dtype='float64'),
            'average_nozeros':
            np.array([1 / 2., 4 / 3., 3 / 2.], dtype='float64'),
            #'average_nozeros_interpnan': np.array([1 / 2., 4 / 3., 3 / 2.], dtype='float64'),
            'average_nozeros_interpnan':
            np.array([1., 2., 3.], dtype='float64'),
            'average_zeros':
            np.array([1 / 3., 4 / 3., 3 / 3.], dtype='float64'),
            'average_zeros_interpnan':
            np.array([1 / 2., 4 / 2., 3 / 2.], dtype='float64'),
        }

        for key in list(answer_dict.keys()):
            if 'sum' in key:
                answer_dict[key + "_interpnan"] = answer_dict[key] * 3. / 2.

        if normalize_kernel:
            answer_key = 'average'
        else:
            answer_key = 'sum'

        if boundary == 'wrap':
            answer_key += '_wrap'
        else:
            # average = average_zeros; sum = sum_zeros
            answer_key += '_zeros'

        if nan_treatment == 'interpolate':
            answer_key += '_interpnan'

        assert_array_almost_equal_nulp(z, answer_dict[answer_key], 10)
Example #44
0
def test_random_brightness_float(beta, expected):
    img = np.ones((100, 100, 3), dtype=np.float32) * 0.4
    expected = np.ones_like(img) * expected
    img = F.brightness_contrast_adjust(img, beta=beta)
    assert img.dtype == np.dtype("float32")
    assert_array_almost_equal_nulp(img, expected)
Example #45
0
def test_ext_flux():
    Lx, Ly = 3, 4
    lat = lattice.Square(Lx,
                         Ly,
                         fermion_site,
                         bc=['periodic', 'periodic'],
                         bc_MPS='infinite')
    M = model.CouplingModel(lat)
    strength = 1.23
    strength_array = np.ones((Lx, Ly)) * strength
    for phi in [0, 2 * np.pi]:  # flux shouldn't do anything
        print("phi = ", phi)
        for dx in [1, 0], [0, 1], [0, 2], [1, -1], [-2, 2]:
            print("dx = ", dx)
            strength_flux = M.coupling_strength_add_ext_flux(
                strength, [1, 0], [0, phi])
            npt.assert_array_almost_equal_nulp(strength_flux, strength_array,
                                               10)
    for phi in [np.pi / 2, 0.123]:
        print("phi = ", phi)
        strength_hop_x = M.coupling_strength_add_ext_flux(
            strength, [1, 0], [0, phi])
        npt.assert_array_almost_equal_nulp(strength_hop_x, strength_array, 10)
        expect_y_1 = np.array(strength_array, dtype=np.complex128)
        expect_y_1[:, -1:] = strength * np.exp(1.j * phi)
        for dx in [[0, 1], [0, -1], [1, -1], [1, 1]]:
            print("dx = ", dx)
            strength_hop_y_1 = M.coupling_strength_add_ext_flux(
                strength, dx, [0, phi])
            if dx[1] < 0:
                npt.assert_array_almost_equal_nulp(strength_hop_y_1,
                                                   expect_y_1, 10)
            else:
                npt.assert_array_almost_equal_nulp(strength_hop_y_1,
                                                   np.conj(expect_y_1), 10)
        expect_y_2 = np.array(strength_array, dtype=np.complex128)
        expect_y_2[:, -2:] = strength * np.exp(1.j * phi)
        for dx in [[0, 2], [0, -2], [1, 2], [3, 2]]:
            print("dx = ", dx)
            strength_hop_y_2 = M.coupling_strength_add_ext_flux(
                strength, dx, [0, phi])
            if dx[1] < 0:
                npt.assert_array_almost_equal_nulp(strength_hop_y_2,
                                                   expect_y_2, 10)
            else:
                npt.assert_array_almost_equal_nulp(strength_hop_y_2,
                                                   np.conj(expect_y_2), 10)
Example #46
0
def test_npc_tensordot():
    for sort in [True, False]:
        print("sort =", sort)
        a = random_Array((10, 12, 15), chinfo3, qtotal=[0], sort=sort)
        aflat = a.to_ndarray()
        legs_b = [l.conj() for l in a.legs[::-1]]
        b = npc.Array.from_func(np.random.random,
                                legs_b,
                                qtotal=[1],
                                shape_kw='size')
        bflat = b.to_ndarray()
        print("axes = 1")  # start simple: only one axes
        c = npc.tensordot(a, b, axes=1)
        c.test_sanity()
        a.test_sanity()
        b.test_sanity()
        npt.assert_array_almost_equal_nulp(a.to_ndarray(), aflat, 1)
        npt.assert_array_almost_equal_nulp(b.to_ndarray(), bflat, 1)
        cflat = np.tensordot(aflat, bflat, axes=1)
        npt.assert_array_almost_equal_nulp(c.to_ndarray(), cflat, sum(a.shape))
        print("axes = 2")  # second: more than one axis
        c = npc.tensordot(a, b, axes=([1, 2], [1, 0]))
        a.test_sanity()
        b.test_sanity()
        npt.assert_array_almost_equal_nulp(a.to_ndarray(), aflat, 1)
        npt.assert_array_almost_equal_nulp(b.to_ndarray(), bflat, 1)
        c.test_sanity()
        cflat = np.tensordot(aflat, bflat, axes=([1, 2], [1, 0]))
        npt.assert_array_almost_equal_nulp(c.to_ndarray(), cflat, sum(a.shape))
        for i in range(b.shape[0]):
            b2 = b[i, :, :]
            if b2.stored_blocks > 0:
                break
        b2flat = b2.to_ndarray()
        print("right tensor fully contracted")
        print(a.shape, b2.shape)
        d = npc.tensordot(a, b2, axes=([0, 1], [1, 0]))
        d.test_sanity()
        dflat = np.tensordot(aflat, b2flat, axes=([0, 1], [1, 0]))
        npt.assert_array_almost_equal_nulp(d.to_ndarray(), dflat, sum(a.shape))
        print("left tensor fully contracted")
        d = npc.tensordot(b2, a, axes=([0, 1], [1, 0]))
        d.test_sanity()
        dflat = np.tensordot(b2flat, aflat, axes=([0, 1], [1, 0]))
        npt.assert_array_almost_equal_nulp(d.to_ndarray(), dflat, sum(a.shape))
    # full/no contraction is tested in test_npc_inner/test_npc_outer
    print("for trivial charge")
    a = npc.Array.from_func(np.random.random, [lcTr, lcTr.conj()],
                            shape_kw='size')
    aflat = a.to_ndarray()
    b = npc.tensordot(a, a, axes=1)
    bflat = np.tensordot(aflat, aflat, axes=1)
    npt.assert_array_almost_equal_nulp(b.to_ndarray(), bflat, sum(a.shape))
Example #47
0
 def test_definition_float16(self):
     x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
     y = fftn(np.array(x, np.float16))
     assert_equal(y.dtype, np.complex64)
     y_r = np.array(fftn(x), np.complex64)
     assert_array_almost_equal_nulp(y, y_r)
Example #48
0
 def test_parse_csv(self):
     csv = parse_csv(self.csv)
     assert_array_almost_equal_nulp(csv.col1, self.col1)
     assert_array_almost_equal_nulp(csv.col2, self.col2)
Example #49
0
    def test_complex128_pass(self):
        nulp = 5
        x = np.linspace(-20, 20, 50, dtype=np.float64)
        x = 10**x
        x = np.r_[-x, x]
        xi = x + x*1j

        eps = np.finfo(x.dtype).eps
        y = x + x*eps*nulp/2.
        assert_array_almost_equal_nulp(xi, x + y*1j, nulp)
        assert_array_almost_equal_nulp(xi, y + x*1j, nulp)
        # The test condition needs to be at least a factor of sqrt(2) smaller
        # because the real and imaginary parts both change
        y = x + x*eps*nulp/4.
        assert_array_almost_equal_nulp(xi, y + y*1j, nulp)

        epsneg = np.finfo(x.dtype).epsneg
        y = x - x*epsneg*nulp/2.
        assert_array_almost_equal_nulp(xi, x + y*1j, nulp)
        assert_array_almost_equal_nulp(xi, y + x*1j, nulp)
        y = x - x*epsneg*nulp/4.
        assert_array_almost_equal_nulp(xi, y + y*1j, nulp)
Example #50
0
def test_normalize_float():
    img = np.ones((100, 100, 3), dtype=np.float32) * 0.4
    normalized = F.normalize(img, mean=50, std=3, max_pixel_value=1.0)
    expected = (np.ones((100, 100, 3), dtype=np.float32) * 0.4 - 50) / 3
    assert_array_almost_equal_nulp(normalized, expected)
Example #51
0
    def test_uniform_3_withnan(self, boundary, interpolate_nan,
                               normalize_kernel, ignore_edge_zeros):
        '''
        Test that the different modes are producing the correct results using
        a uniform kernel with three elements. This version includes a NaN
        value in the original array.
        '''

        x = np.array([1., np.nan, 3.], dtype='float64')

        y = np.array([1., 1., 1.], dtype='float64')

        z = convolve_fft(x,
                         y,
                         boundary=boundary,
                         interpolate_nan=interpolate_nan,
                         normalize_kernel=normalize_kernel,
                         ignore_edge_zeros=ignore_edge_zeros)

        answer_dict = {
            'sum':
            np.array([1., 4., 3.], dtype='float64'),
            'sum_nozeros':
            np.array([1., 4., 3.], dtype='float64'),
            'sum_zeros':
            np.array([1., 4., 3.], dtype='float64'),
            'sum_zeros_noignan':
            np.array([1., 4., 3.], dtype='float64'),
            'sum_nozeros_noignan':
            np.array([1., 4., 3.], dtype='float64'),
            'average':
            np.array([1., 2., 3.], dtype='float64'),
            'sum_wrap':
            np.array([4., 4., 4.], dtype='float64'),
            'sum_wrap_noignan':
            np.array([4., 4., 4.], dtype='float64'),
            'average_wrap':
            np.array([(1 + 3) / 2., 2., 2.], dtype='float64'),
            'average_wrap_noignan':
            np.array([4 / 3., 4 / 3., 4 / 3.], dtype='float64'),
            'average_nozeros':
            np.array([1, 2, 3], dtype='float64'),
            'average_nozeros_noignan':
            np.array([1 / 2., 4 / 3., 3 / 2.], dtype='float64'),
            'average_zeros':
            np.array([1 / 2., 4 / 2., 3 / 2.], dtype='float64'),
            'average_zeros_noignan':
            np.array([1 / 3., 4 / 3., 3 / 3.], dtype='float64'),
        }

        if normalize_kernel:
            answer_key = 'average'
        else:
            answer_key = 'sum'

        if boundary == 'wrap':
            answer_key += '_wrap'
        elif ignore_edge_zeros:
            answer_key += '_nozeros'
        else:
            # average = average_zeros; sum = sum_zeros
            answer_key += '_zeros'

        if not interpolate_nan:
            answer_key += '_noignan'

        assert_array_almost_equal_nulp(z, answer_dict[answer_key], 10)
Example #52
0
    def test_complex64_pass(self):
        nulp = 5
        x = np.linspace(-20, 20, 50, dtype=np.float32)
        x = 10**x
        x = np.r_[-x, x]
        xi = x + x*1j

        eps = np.finfo(x.dtype).eps
        y = x + x*eps*nulp/2.
        assert_array_almost_equal_nulp(xi, x + y*1j, nulp)
        assert_array_almost_equal_nulp(xi, y + x*1j, nulp)
        y = x + x*eps*nulp/4.
        assert_array_almost_equal_nulp(xi, y + y*1j, nulp)

        epsneg = np.finfo(x.dtype).epsneg
        y = x - x*epsneg*nulp/2.
        assert_array_almost_equal_nulp(xi, x + y*1j, nulp)
        assert_array_almost_equal_nulp(xi, y + x*1j, nulp)
        y = x - x*epsneg*nulp/4.
        assert_array_almost_equal_nulp(xi, y + y*1j, nulp)
Example #53
0
 def test_random_complex(self):
     for size in [1, 2, 51, 32, 64, 92]:
         x = random([size, size]) + 1j * random([size, size])
         assert_array_almost_equal_nulp(ifftn(fftn(x)), x, self.maxnlp)
         assert_array_almost_equal_nulp(fftn(ifftn(x)), x, self.maxnlp)
Example #54
0
def test_random_contrast_float(alpha, expected):
    img = np.ones((100, 100, 3), dtype=np.float32) * 0.4
    expected = np.ones((100, 100, 3), dtype=np.float32) * expected
    img = F.brightness_contrast_adjust(img, alpha=alpha)
    assert img.dtype == np.dtype('float32')
    assert_array_almost_equal_nulp(img, expected)
Example #55
0
 def test_simulate(self):
     beliefs_sequence = \
         self.fp.simulate(ts_length=3, init_actions=(0, 1))
     # played actions: (0, 1), (1, 0), (0, 1)
     assert_array_almost_equal_nulp(
         beliefs_sequence[0], [[0, 1], [1 / 2, 1 / 2], [1 / 3, 2 / 3]])
Example #56
0
def test_unit_conversions():
    """
    Test operations that convert to different units or cast to ndarray

    """
    from yt.units.yt_array import YTQuantity
    from yt.units.unit_object import Unit

    km = YTQuantity(1, 'km')
    km_in_cm = km.in_units('cm')
    cm_unit = Unit('cm')
    kpc_unit = Unit('kpc')

    assert_equal(km_in_cm, km)
    assert_equal(km_in_cm.in_cgs(), 1e5)
    assert_equal(km_in_cm.in_mks(), 1e3)
    assert_equal(km_in_cm.units, cm_unit)

    km_view = km.ndarray_view()
    km.convert_to_units('cm')
    assert_true(km_view.base is km.base)

    assert_equal(km, YTQuantity(1, 'km'))
    assert_equal(km.in_cgs(), 1e5)
    assert_equal(km.in_mks(), 1e3)
    assert_equal(km.units, cm_unit)

    km.convert_to_units('kpc')
    assert_true(km_view.base is km.base)

    assert_array_almost_equal_nulp(km, YTQuantity(1, 'km'))
    assert_array_almost_equal_nulp(km.in_cgs(), YTQuantity(1e5, 'cm'))
    assert_array_almost_equal_nulp(km.in_mks(), YTQuantity(1e3, 'm'))
    assert_equal(km.units, kpc_unit)

    assert_isinstance(km.to_ndarray(), np.ndarray)
    assert_isinstance(km.ndarray_view(), np.ndarray)

    dyne = YTQuantity(1.0, 'dyne')

    assert_equal(dyne.in_cgs(), dyne)
    assert_equal(dyne.in_cgs(), 1.0)
    assert_equal(dyne.in_mks(), dyne)
    assert_equal(dyne.in_mks(), 1e-5)
    assert_equal(str(dyne.in_mks().units), 'kg*m/s**2')
    assert_equal(str(dyne.in_cgs().units), 'cm*g/s**2')

    em3 = YTQuantity(1.0, 'erg/m**3')

    assert_equal(em3.in_cgs(), em3)
    assert_equal(em3.in_cgs(), 1e-6)
    assert_equal(em3.in_mks(), em3)
    assert_equal(em3.in_mks(), 1e-7)
    assert_equal(str(em3.in_mks().units), 'kg/(m*s**2)')
    assert_equal(str(em3.in_cgs().units), 'g/(cm*s**2)')

    em3_converted = YTQuantity(1545436840.386756, 'Msun/(Myr**2*kpc)')
    assert_equal(em3.in_base(unit_system="galactic"), em3)
    assert_array_almost_equal(em3.in_base(unit_system="galactic"),
                              em3_converted)
    assert_equal(str(em3.in_base(unit_system="galactic").units),
                 'Msun/(Myr**2*kpc)')

    dimless = YTQuantity(1.0, "")
    assert_equal(dimless.in_cgs(), dimless)
    assert_equal(dimless.in_cgs(), 1.0)
    assert_equal(dimless.in_mks(), dimless)
    assert_equal(dimless.in_mks(), 1.0)
    assert_equal(str(dimless.in_cgs().units), "dimensionless")
Example #57
0
def test_to_float_without_max_value_specified(dtype, divider):
    img = np.ones((100, 100, 3), dtype=dtype)
    expected = img.astype("float32") / divider
    assert_array_almost_equal_nulp(F.to_float(img), expected)
Example #58
0
def assert_fp_equal(x, y, err_msg="", nulp=50):
    """Assert two arrays are equal, up to some floating-point rounding error"""
    try:
        assert_array_almost_equal_nulp(x, y, nulp)
    except AssertionError as e:
        raise AssertionError("%s\n%s" % (e, err_msg))
Example #59
0
def test_to_float_unknown_dtype_with_max_value(max_value):
    img = np.ones((100, 100, 3), dtype=np.int16)
    expected = img.astype("float32") / max_value
    assert_array_almost_equal_nulp(F.to_float(img, max_value=max_value),
                                   expected)
 def assert_array_almost_equal_nulp(self, x, y, nulp=1):
     x = x.tondarray() if hasattr(x, 'tondarray') else x
     y = y.tondarray() if hasattr(y, 'tondarray') else y
     return npt.assert_array_almost_equal_nulp(x, y, nulp)