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_permute_replications(): """Test surrogate creation by permuting replications.""" n = 20 data = Data(np.vstack((np.zeros(n), np.ones(n) * 1, np.ones(n) * 2, np.ones(n) * 3)).astype(int), 'rs', normalise=False) current_value = (0, n - 1) l = [(0, 1), (0, 3), (0, 7)] [perm, perm_idx] = data.permute_replications(current_value=current_value, idx_list=l) assert (np.all(perm[:, 0] == perm_idx)), 'Permutation did not work.' # Assert that samples have been swapped within the permutation range for # the first replication. rng = 3 current_value = (0, 3) l = [(0, 0), (0, 1), (0, 2)] # data = Data(np.arange(n), 's', normalise=False) data = Data(np.vstack((np.arange(n), np.arange(n))).astype(int), 'rs', normalise=False) perm_opts = {'perm_type': 'local', 'perm_range': rng} [perm, perm_idx] = data.permute_samples(current_value=current_value, idx_list=l, perm_opts=perm_opts) samples = np.arange(rng) i = 0 n_per_repl = int(data.n_realisations(current_value) / data.n_replications) for p in range(n_per_repl // rng): assert (np.unique(perm[i:i + rng, 0]) == samples).all(), ( 'The permutation range was not respected.') samples += rng i += rng rem = n_per_repl % rng if rem > 0: assert (np.unique(perm[i:i + rem, 0]) == samples[0:rem]).all(), ( 'The remainder did not contain the same realisations.') # Test assertions that perm_range is not too low or too high. perm_opts = {'perm_type': 'local', 'perm_range': 1} with pytest.raises(AssertionError): data.permute_samples(current_value=current_value, idx_list=l, perm_opts=perm_opts) perm_opts['perm_range'] = np.inf with pytest.raises(AssertionError): data.permute_samples(current_value=current_value, idx_list=l, perm_opts=perm_opts) # Test ValueError if a string other than 'max' is given for perm_range. perm_opts['perm_range'] = 'foo' with pytest.raises(ValueError): data.permute_samples(current_value=current_value, idx_list=l, perm_opts=perm_opts)
def test_permute_replications(): """Test surrogate creation by permuting replications.""" n = 20 data = Data(np.vstack((np.zeros(n), np.ones(n) * 1, np.ones(n) * 2, np.ones(n) * 3)).astype(int), 'rs', normalise=False) current_value = (0, n) l = [(0, 1), (0, 3), (0, 7)] [perm, perm_idx] = data.permute_replications(current_value=current_value, idx_list=l) assert (np.all(perm[:, 0] == perm_idx)), 'Permutation did not work.' # Assert that samples have been swapped within the permutation range for # the first replication. rng = 3 current_value = (0, 3) l = [(0, 0), (0, 1), (0, 2)] #data = Data(np.arange(n), 's', normalise=False) data = Data(np.vstack((np.arange(n), np.arange(n))).astype(int), 'rs', normalise=False) [perm, perm_idx] = data.permute_samples(current_value=current_value, idx_list=l, perm_range=rng) samples = np.arange(rng) i = 0 n_per_repl = int(data.n_realisations(current_value) / data.n_replications) for p in range(n_per_repl // rng): assert (np.unique(perm[i:i + rng, 0]) == samples).all(), ('The ' 'permutation range was not respected.') samples += rng i += rng rem = n_per_repl % rng if rem > 0: assert (np.unique(perm[i:i + rem, 0]) == samples[0:rem]).all(), ('The ' 'remainder did not contain the same realisations.') # Test assertions that perm_range is not too low or too high. with pytest.raises(AssertionError): data.permute_samples(current_value=current_value, idx_list=l, perm_range=1) with pytest.raises(AssertionError): data.permute_samples(current_value=current_value, idx_list=l, perm_range=np.inf) # Test ValueError if a string other than 'max' is given for perm_range. with pytest.raises(ValueError): data.permute_samples(current_value=current_value, idx_list=l, perm_range='foo')
def test_permute_replications(): """Test surrogate creation by permuting replications.""" n = 20 data = Data(np.vstack((np.zeros(n), np.ones(n) * 1, np.ones(n) * 2, np.ones(n) * 3)).astype(int), 'rs', normalise=False) current_value = (0, n - 1) l = [(0, 1), (0, 3), (0, 7)] [perm, perm_idx] = data.permute_replications(current_value=current_value, idx_list=l) assert (np.all(perm[:, 0] == perm_idx)), 'Permutation did not work.' # Assert that samples have been swapped within the permutation range for # the first replication. rng = 3 current_value = (0, 3) l = [(0, 0), (0, 1), (0, 2)] # data = Data(np.arange(n), 's', normalise=False) data = Data(np.vstack((np.arange(n), np.arange(n))).astype(int), 'rs', normalise=False) perm_settings = { 'perm_type': 'local', 'perm_range': rng } [perm, perm_idx] = data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings) samples = np.arange(rng) i = 0 n_per_repl = int(data.n_realisations(current_value) / data.n_replications) for p in range(n_per_repl // rng): assert (np.unique(perm[i:i + rng, 0]) == samples).all(), ( 'The permutation range was not respected.') samples += rng i += rng rem = n_per_repl % rng if rem > 0: assert (np.unique(perm[i:i + rem, 0]) == samples[0:rem]).all(), ( 'The remainder did not contain the same realisations.')
def test_permute_samples(): """Test surrogate creation by permuting samples.""" n = 20 dat = Data(np.arange(n), 's', normalise=False) perm = dat.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_opts=None)[0] assert (sorted(np.squeeze(perm)) == np.arange(n)).all(), ( 'Permutation did not contain the correct values.') opts = {'perm_type': 'circular'} perm = dat.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_opts=opts)[0] idx_start = np.where(np.squeeze(perm) == 0)[0][0] assert (np.squeeze(np.vstack( (perm[idx_start:], perm[:idx_start]))) == np.arange(n)).all(), ( 'Circular shifting went wrong.') opts = {'perm_type': 'block'} perm = dat.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_opts=opts)[0] block_size = int(round(n / 10)) for b in range(0, n, block_size): assert perm[b + 1] - perm[b] == 1, 'Block permutation went wrong.' opts = {'perm_type': 'block', 'block_size': 3} perm = dat.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_opts=opts)[0] for b in range(0, n, opts['block_size']): assert perm[b + 1] - perm[b] == 1, 'Block permutation went wrong.' opts = {'perm_type': 'block', 'block_size': 3, 'perm_range': 2} perm = dat.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_opts=opts)[0] opts = {'perm_type': 'local'} perm = dat.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_opts=opts)[0] perm_range = int(round(n / 10)) for b in range(0, n, perm_range): assert abs(perm[b + 1] - perm[b]) == 1, 'Block permutation went wrong.'
def test_permute_samples(): """Test surrogate creation by permuting samples.""" n = 20 data = Data(np.arange(n), 's', normalise=False) # Test random permutation settings = {'perm_type': 'random'} perm = data.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_settings=settings)[0] assert (sorted(np.squeeze(perm)) == np.arange(n)).all(), ( 'Permutation did not contain the correct values.') # Test circular shifting settings = {'perm_type': 'circular', 'max_shift': 4} perm = data.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_settings=settings)[0] idx_start = np.where(np.squeeze(perm) == 0)[0][0] assert (np.squeeze(np.vstack( (perm[idx_start:], perm[:idx_start]))) == np.arange(n)).all(), ( 'Circular shifting went wrong.') # Test shifting of data blocks block_size = round(n / 10) settings = { 'perm_type': 'block', 'block_size': block_size, 'perm_range': round(n / block_size) } perm = data.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_settings=settings)[0] block_size = int(round(n / 10)) for b in range(0, n, block_size): assert perm[b + 1] - perm[b] == 1, 'Block permutation went wrong.' # Test shifting of data blocks with n % block_size != 0 block_size = 3 settings = { 'perm_type': 'block', 'block_size': block_size, 'perm_range': round(n / block_size) } perm = data.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_settings=settings)[0] for b in range(0, n, settings['block_size']): assert perm[b + 1] - perm[b] == 1, 'Block permutation went wrong.' settings = {'perm_type': 'block', 'block_size': 3, 'perm_range': 2} perm = data.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_settings=settings)[0] # Test local shifting perm_range = int(round(n / 10)) settings = {'perm_type': 'local', 'perm_range': perm_range} perm = data.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_settings=settings)[0] for b in range(0, n, perm_range): assert abs(perm[b + 1] - perm[b]) == 1, 'Local shifting went wrong.' # Test assertions that perm_range is not too low or too high. current_value = (0, 3) l = [(0, 0), (0, 1), (0, 2)] perm_settings = {'perm_type': 'local', 'perm_range': 1} # Test Assertion if perm_range too small with pytest.raises(AssertionError): data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings) # Test TypeError if settings are no integers perm_settings['perm_range'] = np.inf with pytest.raises(TypeError): data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings) perm_settings['perm_range'] = 'foo' with pytest.raises(TypeError): data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings) perm_settings['perm_type'] = 'block' perm_settings['block_size'] = 3 with pytest.raises(TypeError): data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings) perm_settings['block_size'] = 3.5 with pytest.raises(TypeError): data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings) perm_settings['perm_type'] = 'circular' perm_settings['max_shift'] = 3.5 with pytest.raises(TypeError): data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings)
def test_permute_samples(): """Test surrogate creation by permuting samples.""" n = 20 data = Data(np.arange(n), 's', normalise=False) # Test random permutation settings = {'perm_type': 'random'} perm = data.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_settings=settings)[0] assert (sorted(np.squeeze(perm)) == np.arange(n)).all(), ( 'Permutation did not contain the correct values.') # Test circular shifting settings = {'perm_type': 'circular', 'max_shift': 4} perm = data.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_settings=settings)[0] idx_start = np.where(np.squeeze(perm) == 0)[0][0] assert (np.squeeze(np.vstack((perm[idx_start:], perm[:idx_start]))) == np.arange(n)).all(), ('Circular shifting went wrong.') # Test shifting of data blocks block_size = round(n / 10) settings = {'perm_type': 'block', 'block_size': block_size, 'perm_range': round(n / block_size)} perm = data.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_settings=settings)[0] block_size = int(round(n / 10)) for b in range(0, n, block_size): assert perm[b + 1] - perm[b] == 1, 'Block permutation went wrong.' # Test shifting of data blocks with n % block_size != 0 block_size = 3 settings = {'perm_type': 'block', 'block_size': block_size, 'perm_range': round(n / block_size)} perm = data.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_settings=settings)[0] for b in range(0, n, settings['block_size']): assert perm[b + 1] - perm[b] == 1, 'Block permutation went wrong.' settings = {'perm_type': 'block', 'block_size': 3, 'perm_range': 2} perm = data.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_settings=settings)[0] # Test local shifting perm_range = int(round(n / 10)) settings = {'perm_type': 'local', 'perm_range': perm_range} perm = data.permute_samples(current_value=(0, 0), idx_list=[(0, 0)], perm_settings=settings)[0] for b in range(0, n, perm_range): assert abs(perm[b + 1] - perm[b]) == 1, 'Local shifting went wrong.' # Test assertions that perm_range is not too low or too high. current_value = (0, 3) l = [(0, 0), (0, 1), (0, 2)] perm_settings = {'perm_type': 'local', 'perm_range': 1} # Test Assertion if perm_range too small with pytest.raises(AssertionError): data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings) # Test TypeError if settings are no integers perm_settings['perm_range'] = np.inf with pytest.raises(TypeError): data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings) perm_settings['perm_range'] = 'foo' with pytest.raises(TypeError): data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings) perm_settings['perm_type'] = 'block' perm_settings['block_size'] = 3 with pytest.raises(TypeError): data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings) perm_settings['block_size'] = 3.5 with pytest.raises(TypeError): data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings) perm_settings['perm_type'] = 'circular' perm_settings['max_shift'] = 3.5 with pytest.raises(TypeError): data.permute_samples(current_value=current_value, idx_list=l, perm_settings=perm_settings)