def test_data_type(): """Test if data class always returns the correct data type.""" # Change data type for the same object instance. d_int = np.random.randint(0, 10, size=(3, 50)) orig_type = type(d_int[0][0]) data = Data(d_int, dim_order='ps', normalise=False) # The concrete type depends on the platform: # https://mail.scipy.org/pipermail/numpy-discussion/2011-November/059261.html # Hence, compare against the type automatically assigned by Python or # against np.integer assert data.data_type is orig_type, 'Data type did not change.' assert issubclass(type(data.data[0, 0, 0]), np.integer), ('Data type is not an int.') d_float = np.random.randn(3, 50) data.set_data(d_float, dim_order='ps') assert data.data_type is np.float64, 'Data type did not change.' assert issubclass(type(data.data[0, 0, 0]), np.float), ('Data type is not a float.') # Check if data returned by the object have the correct type. d_int = np.random.randint(0, 10, size=(3, 50, 5)) data = Data(d_int, dim_order='psr', normalise=False) real = data.get_realisations((0, 5), [(1, 1), (1, 3)])[0] assert issubclass(type(real[0, 0]), np.integer), ('Realisations type is not an int.') sl = data._get_data_slice(0)[0] assert issubclass(type(sl[0, 0]), np.integer), ('Data slice type is not an int.') settings = {'perm_type': 'random'} sl_perm = data.slice_permute_samples(0, settings)[0] assert issubclass(type(sl_perm[0, 0]), np.integer), ('Permuted data slice type is not an int.') samples = data.permute_samples((0, 5), [(1, 1), (1, 3)], settings)[0] assert issubclass(type(samples[0, 0]), np.integer), ('Permuted samples type is not an int.')
def test_data_type(): """Test if data class always returns the correct data type.""" # Change data type for the same object instance. d_int = np.random.randint(0, 10, size=(3, 50)) orig_type = type(d_int[0][0]) data = Data(d_int, dim_order='ps', normalise=False) # The concrete type depends on the platform: # https://mail.scipy.org/pipermail/numpy-discussion/2011-November/059261.html # Hence, compare against the type automatically assigned by Python or # against np.integer assert data.data_type is orig_type, 'Data type did not change.' assert issubclass(type(data.data[0, 0, 0]), np.integer), ( 'Data type is not an int.') d_float = np.random.randn(3, 50) data.set_data(d_float, dim_order='ps') assert data.data_type is np.float64, 'Data type did not change.' assert issubclass(type(data.data[0, 0, 0]), np.float), ( 'Data type is not a float.') # Check if data returned by the object have the correct type. d_int = np.random.randint(0, 10, size=(3, 50, 5)) data = Data(d_int, dim_order='psr', normalise=False) real = data.get_realisations((0, 5), [(1, 1), (1, 3)])[0] assert issubclass(type(real[0, 0]), np.integer), ( 'Realisations type is not an int.') sl = data._get_data_slice(0)[0] assert issubclass(type(sl[0, 0]), np.integer), ( 'Data slice type is not an int.') settings = {'perm_type': 'random'} sl_perm = data.slice_permute_samples(0, settings)[0] assert issubclass(type(sl_perm[0, 0]), np.integer), ( 'Permuted data slice type is not an int.') samples = data.permute_samples((0, 5), [(1, 1), (1, 3)], settings)[0] assert issubclass(type(samples[0, 0]), np.integer), ( 'Permuted samples type is not an int.')
def test_get_data_slice(): n = 10 n_replications = 3 d = Data(np.vstack((np.zeros(n).astype(int), np.ones(n).astype(int), 2 * np.ones(n).astype(int))), 'rs', normalise=False) [s, i] = d._get_data_slice(process=0, offset_samples=0, shuffle=False) # test unshuffled slicing for r in range(n_replications): assert s[0][r] == i[r], 'Replication index {0} is not correct.'.format( r) # test shuffled slicing [s, i] = d._get_data_slice(process=0, offset_samples=0, shuffle=True) for r in range(n_replications): assert s[0][r] == i[r], 'Replication index {0} is not correct.'.format( r) offset = 3 d = Data(np.arange(n), 's', normalise=False) [s, i] = d._get_data_slice(process=0, offset_samples=offset, shuffle=False) assert s.shape[0] == (n - offset), 'Offset not handled correctly.'