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.')