def test_temporal_derivative_distribution_repair(fname, tmp_path): """Test running artifact rejection.""" raw = read_raw_nirx(fname) raw_od = optical_density(raw) raw_hb = beer_lambert_law(raw_od) # With optical densities # Add a baseline shift artifact about half way through data max_shift = np.max(np.diff(raw_od._data[0])) shift_amp = 5 * max_shift raw_od._data[0, 0:30] = raw_od._data[0, 0:30] - shift_amp # make one channel zero std raw_od._data[1] = 0. raw_od._data[2] = 1. assert np.max(np.diff(raw_od._data[0])) > shift_amp # Ensure that applying the algorithm reduces the step change raw_od = tddr(raw_od) assert np.max(np.diff(raw_od._data[0])) < shift_amp assert_allclose(raw_od._data[1], 0.) # unchanged assert_allclose(raw_od._data[2], 1.) # unchanged # With Hb # Add a baseline shift artifact about half way through data max_shift = np.max(np.diff(raw_hb._data[0])) shift_amp = 5 * max_shift raw_hb._data[0, 0:30] = raw_hb._data[0, 0:30] - (1.1 * shift_amp) # make one channel zero std raw_hb._data[1] = 0. raw_hb._data[2] = 1. assert np.max(np.diff(raw_hb._data[0])) > shift_amp # Ensure that applying the algorithm reduces the step change raw_hb = tddr(raw_hb) assert np.max(np.diff(raw_hb._data[0])) < shift_amp assert_allclose(raw_hb._data[1], 0.) # unchanged assert_allclose(raw_hb._data[2], 1.) # unchanged
def test_order_agnostic(nirx_snirf): """Test that order does not matter to (pre)processing results.""" raw_nirx, raw_snirf = nirx_snirf raw_random = raw_nirx.copy().pick( np.random.RandomState(0).permutation(len(raw_nirx.ch_names))) raws = dict(nirx=raw_nirx, snirf=raw_snirf, random=raw_random) del raw_nirx, raw_snirf, raw_random orders = dict() # continuous wave for key, r in raws.items(): assert set(r.get_channel_types()) == {'fnirs_cw_amplitude'} orders[key] = [ r.ch_names.index(name) for name in raws['nirx'].ch_names ] assert_array_equal(raws['nirx'].ch_names, np.array(r.ch_names)[orders[key]]) assert_allclose(raws['nirx'].get_data(), r.get_data(orders[key]), err_msg=key) assert_array_equal(orders['nirx'], np.arange(len(raws['nirx'].ch_names))) # optical density for key, r in raws.items(): raws[key] = r = optical_density(r) assert_allclose(raws['nirx'].get_data(), r.get_data(orders[key]), err_msg=key) assert set(r.get_channel_types()) == {'fnirs_od'} # scalp-coupling index sci = dict() for key, r in raws.items(): sci[key] = r = scalp_coupling_index(r) assert_allclose(sci['nirx'], r[orders[key]], err_msg=key, rtol=0.01) # TDDR (on optical) tddrs = dict() for key, r in raws.items(): tddrs[key] = r = tddr(r) assert_allclose(tddrs['nirx'].get_data(), r.get_data(orders[key]), err_msg=key, atol=1e-4) assert set(r.get_channel_types()) == {'fnirs_od'} # beer-lambert for key, r in raws.items(): raws[key] = r = beer_lambert_law(r) assert_allclose(raws['nirx'].get_data(), r.get_data(orders[key]), err_msg=key, rtol=2e-7) assert set(r.get_channel_types()) == {'hbo', 'hbr'} # TDDR (on haemo) tddrs = dict() for key, r in raws.items(): tddrs[key] = r = tddr(r) assert_allclose(tddrs['nirx'].get_data(), r.get_data(orders[key]), err_msg=key, atol=1e-9) assert set(r.get_channel_types()) == {'hbo', 'hbr'}
def test_temporal_derivative_distribution_repair(fname, tmpdir): """Test running artifact rejection.""" raw = read_raw_nirx(fname) raw = optical_density(raw) # Add a baseline shift artifact about half way through data max_shift = np.max(np.diff(raw._data[0])) shift_amp = 5 * max_shift raw._data[0, 0:30] = raw._data[0, 0:30] - (shift_amp) assert np.max(np.diff(raw._data[0])) > shift_amp # Ensure that applying the algorithm reduces the step change raw = tddr(raw) assert np.max(np.diff(raw._data[0])) < shift_amp