Ejemplo n.º 1
0
    def test_nddata_core_add(self):
        for ix in range(num_random_tests):
            random_coords = [np.r_[0:random.randint(1,6)] for dim in test_dims]

            random_axis = list(zip(test_dims, random_coords))

            random_axis1 = random.sample(random_axis, 3)
            random_axis2 = random.sample(random_axis, 3)

            dims1 = [axis[0] for axis in random_axis1]
            coords1 = [axis[1] for axis in random_axis1]
            shape1 = [coord.size for coord in coords1]
            values1 = np.random.randn(*shape1)
            data1 = nddata.nddata_core(values1, dims1, coords1)

            dims2 = [axis[0] for axis in random_axis2]
            coords2 = [axis[1] for axis in random_axis2]
            shape2 = [coord.size for coord in coords2]
            values2 = np.random.randn(*shape2)
            data2 = nddata.nddata_core(values2, dims2, coords2)

            data = data1 + data2

            self.assertTrue(data._self_consistent())
            assert_array_equal((data1+1).values, values1+1)
            assert_array_equal((data1+1.).values, values1+1.)
            assert_array_equal((data1+1.j).values, values1+1.j)
Ejemplo n.º 2
0
    def test_ndim(self):
        values = np.r_[1:10].reshape(3, 3)
        x = np.r_[0:3]
        y = np.r_[0:3]

        data = nddata.nddata_core(values, ["x", "y"], [x, y])

        self.assertEqual(data.ndim, 2)
Ejemplo n.º 3
0
    def construct_random_data(self):
        random_dims = random.sample(test_dims, random.randint(1,len(test_dims)))

        random_coords = [np.r_[0:random.randint(1,6)] for dim in random_dims]
        shape = [coord.size for coord in random_coords]

        random_values = np.random.randn(*shape)
        data = nddata.nddata_core(random_values, random_dims, random_coords)
        return data,random_dims,random_values,random_coords
Ejemplo n.º 4
0
def get_random_nddata_list(seed_axis=0, seed_data=0):
    """
    Pre-generate a list of randomized tuple (nddata, np.array) for all the
    test cases here
    """
    random.seed(seed_axis)
    random_axes = [
        random.sample(
            list(
                zip(test_dims,
                    [np.r_[0:random.randint(1, 6)] for _ in test_dims])),
            3,
        ) for _ in range(3)
    ]
    random.seed(seed_data)
    nddata_list = []
    for random_axis in random_axes:
        dims = [axis[0] for axis in random_axis]
        coords = [axis[1] for axis in random_axis]
        shape = [coord.size for coord in coords]
        values = np.random.randn(*shape)
        nddata_list.append((nddata.nddata_core(values, dims, coords), values))
    # nddata_list.append((nddata.nddata_core(), np.array([])))  # UserWarning: Github #37
    return nddata_list
Ejemplo n.º 5
0
import sys

sys.path.append('..')
import dnplab
from dnplab.core.nddata import nddata_core

import numpy as np

x = np.r_[1:100]
values = np.random.randn(len(x))

data = nddata_core(values, ['x'], [x])
print(data)

dnplab.dnpResults.plot(data)
dnplab.dnpResults.show()


Ejemplo n.º 6
0
def test_nddata_core_math_div_by_zero():
    with pytest.warns(RuntimeWarning):
        nddata.nddata_core(values=np.array([1, 2, 3]),
                           coords=[np.array([3, 2, 1])],
                           dims=["x"]) / 0
Ejemplo n.º 7
0
    def test_nddata_core_math_operators(self):
        for ix in range(num_random_tests):
            random_coords = [np.r_[0:random.randint(1,6)] for dim in test_dims]

            random_axis = list(zip(test_dims, random_coords))

            random_axis = random.sample(random_axis, 3)

            dims = [axis[0] for axis in random_axis]
            coords = [axis[1] for axis in random_axis]
            shape = [coord.size for coord in coords]
            values = np.random.randn(*shape)
            data = nddata.nddata_core(values, dims, coords)

            random_array = np.random.randn(*shape)

            #__add__
            assert_array_equal((data+1).values, values+1)
            assert_array_equal((data+1.).values, values+1.)
            assert_array_equal((data+1.j).values, values+1.j)
            assert_array_equal((data+random_array).values, values+random_array)

            #__sub__
            assert_array_equal((data-1).values, values-1)
            assert_array_equal((data-1.).values, values-1.)
            assert_array_equal((data-1.j).values, values-1.j)
            assert_array_equal((data-random_array).values, values-random_array)

            #__mult__
            assert_array_equal((data*1).values, values*1)
            assert_array_equal((data*1.).values, values*1.)
            assert_array_equal((data*1.j).values, values*1.j)
            assert_array_equal((data*random_array).values, values*random_array)

            #__truediv__
            assert_array_equal((data/1).values, values/1)
            assert_array_equal((data/1.).values, values/1.)
            assert_array_equal((data/1.j).values, values/1.j)
            assert_array_equal((data/random_array).values, values/random_array)

            #__radd__
            assert_array_equal((1+data).values, 1+values)
            assert_array_equal((1.+data).values, 1.+values)
            assert_array_equal((1.j+data).values, 1.j+values)
            assert_array_equal((random_array+data).values, random_array+values)

            #__rsub__
            assert_array_equal((1-data).values, 1-values)
            assert_array_equal((1.-data).values, 1.-values)
            assert_array_equal((1.j-data).values, 1.j-values)
            assert_array_equal((random_array-data).values, random_array-values)

            #__rmult__
            assert_array_equal((1*data).values, 1*values)
            assert_array_equal((1.*data).values, 1.*values)
            assert_array_equal((1.j*data).values, 1.j*values)
            assert_array_equal((random_array*data).values, random_array*values)

            #__rtruediv__
            assert_array_equal((1/data).values, 1/values)
            assert_array_equal((1./data).values, 1./values)
            assert_array_equal((1.j/data).values, 1.j/values)
            assert_array_equal((random_array/data).values, random_array/values)