def test_merge_wrapper(): dev = generate_DEV() data = pd.DataFrame({'amplitude': [4, 3, 2, 5]}) sev = SimpleVariable('simple', data, 'dummy') # Should break if asked to merge different classes with pytest.raises(ValueError, match='Variables of different classes'): merge_variables([dev, sev])
def test_merge_wrapper(): dev = generate_DEV() data = pd.DataFrame({'amplitude': [4, 3, 2, 5]}) sev = SimpleVariable('simple', data, 'dummy') # Should break if asked to merge different classes with pytest.raises(ValueError) as e: merge_variables([dev, sev]) assert "Variables of different classes" in str(e)
def test_merge_dense_run_variables(layout2): variables = [generate_DEV() for i in range(20)] variables += [generate_DEV(duration=400) for i in range(8)] n_rows = sum([len(c.values) for c in variables]) merged = merge_variables(variables) assert len(merged.values) == n_rows assert set(merged.index.columns) == set(variables[0].index.columns)
def test_merge_sparse_run_variables(layout1): dataset = load_variables(layout1, types='events', scan_length=480) runs = dataset.get_nodes('run') variables = [r.variables['RT'] for r in runs] n_rows = sum([len(c.values) for c in variables]) merged = merge_variables(variables) assert len(merged.values) == n_rows assert set(merged.index.columns) == set(variables[0].index.columns)
def test_merge_simple_variables(layout2): index = load_variables(layout2, types='sessions') subjects = index.get_nodes('subject') variables = [s.variables['panas_sad'] for s in subjects] n_rows = sum([len(c.values) for c in variables]) merged = merge_variables(variables) assert len(merged.values) == n_rows assert set(merged.index.columns) == set(variables[0].index.columns) assert variables[3].values.iloc[1] == merged.values.iloc[7]
def test_merge_densified_variables(layout1): SR = 10 dataset = load_variables(layout1, types='events', scan_length=480) runs = dataset.get_nodes('run') vars_ = [r.variables['RT'].to_dense(SR) for r in runs] dense = merge_variables(vars_) assert isinstance(dense, DenseRunVariable) n_rows = 480 * SR assert dense.values.shape == (len(runs) * n_rows, 1) for i in range(len(runs)): onset = i * n_rows offset = onset + n_rows run_vals = vars_[i].values dense_vals = dense.values.iloc[onset:offset].reset_index(drop=True) assert dense_vals.equals(run_vals)
def test_filter_simple_variable(layout2): dataset = load_variables(layout2, types=['scans']) sessions = dataset.get_nodes('session') variables = [s.variables['surroundings'] for s in sessions] merged = merge_variables(variables) assert merged.to_df().shape == (60, 9) filt = merged.filter({'acq': 'fullbrain'}) assert filt.to_df().shape == (40, 9) flt1 = merged.filter({'acq': 'fullbrain', 'subject': ['01', '02']}).to_df() assert flt1.shape == (8, 9) flt2 = merged.filter(query='acq=="fullbrain" and subject in ["01", "02"]') flt2 = flt2.to_df() assert flt1.equals(flt2) assert merged.filter({'nonexistent': 2}, strict=True) is None merged.filter({'acq': 'fullbrain'}, inplace=True) assert merged.to_df().shape == (40, 9)
def test_filter_simple_variable(layout2): dataset = load_variables(layout2, types=['scans']) sessions = dataset.get_nodes('session') variables = [s.variables['surroundings'] for s in sessions] merged = merge_variables(variables) assert merged.to_df().shape == (60, 9) filt = merged.filter({'acquisition': 'fullbrain'}) assert filt.to_df().shape == (40, 9) flt1 = merged.filter({'acquisition': 'fullbrain', 'subject': ['01', '02']}).to_df() assert flt1.shape == (8, 9) query = 'acquisition=="fullbrain" and subject in ["01", "02"]' flt2 = merged.filter(query=query) flt2 = flt2.to_df() assert flt1.equals(flt2) assert merged.filter({'nonexistent': 2}, strict=True) is None merged.filter({'acquisition': 'fullbrain'}, inplace=True) assert merged.to_df().shape == (40, 9)
def test_densify_merged_variables(layout1): SR = 10 dataset = load_variables(layout1, types='events', scan_length=480) runs = dataset.get_nodes('run') vars_ = [r.variables['RT'] for r in runs] var = merge_variables(vars_) dense = var.to_dense(SR) assert isinstance(dense, DenseRunVariable) n_rows = 480 * SR assert dense.values.shape == (len(runs) * n_rows, 1) for i in range(len(runs)): onset = i * n_rows offset = onset + n_rows # resampled values from split vs. merged will be trivially different # at boundaries due to interpolation, so leave a buffer run_vals = vars_[i].to_dense(SR).values.values.ravel() dense_vals = dense.values.values.ravel()[onset:offset] assert run_vals.shape == dense_vals.shape assert np.array_equal(run_vals, dense_vals)