def test_fnirs_channel_naming_and_order_readers(fname): """Ensure fNIRS channel checking on standard readers.""" # fNIRS data requires specific channel naming and ordering. # All standard readers should pass tests raw = read_raw_nirx(fname) freqs = np.unique(_channel_frequencies(raw.info)) assert_array_equal(freqs, [760, 850]) chroma = np.unique(_channel_chromophore(raw.info)) assert len(chroma) == 0 picks = _check_channels_ordered(raw.info, freqs) assert len(picks) == len(raw.ch_names) # as all fNIRS only data # Check that dropped channels are detected # For each source detector pair there must be two channels, # removing one should throw an error. raw_dropped = raw.copy().drop_channels(raw.ch_names[4]) with pytest.raises(ValueError, match='not ordered correctly'): _check_channels_ordered(raw_dropped.info, freqs) # The ordering must be increasing for the pairs, if provided raw_names_reversed = raw.copy().ch_names raw_names_reversed.reverse() raw_reversed = raw.copy().pick_channels(raw_names_reversed, ordered=True) with pytest.raises(ValueError, match='The frequencies.*sorted.*'): _check_channels_ordered(raw_reversed.info, [850, 760]) # So if we flip the second argument it should pass again picks = _check_channels_ordered(raw_reversed.info, freqs) got_first = set(raw_reversed.ch_names[pick].split()[1] for pick in picks[::2]) assert got_first == {'760'} got_second = set(raw_reversed.ch_names[pick].split()[1] for pick in picks[1::2]) assert got_second == {'850'} # Check on OD data raw = optical_density(raw) freqs = np.unique(_channel_frequencies(raw.info)) assert_array_equal(freqs, [760, 850]) chroma = np.unique(_channel_chromophore(raw.info)) assert len(chroma) == 0 picks = _check_channels_ordered(raw.info, freqs) assert len(picks) == len(raw.ch_names) # as all fNIRS only data # Check on haemoglobin data raw = beer_lambert_law(raw) freqs = np.unique(_channel_frequencies(raw.info)) assert len(freqs) == 0 assert len(_channel_chromophore(raw.info)) == len(raw.ch_names) chroma = np.unique(_channel_chromophore(raw.info)) assert_array_equal(chroma, ["hbo", "hbr"]) picks = _check_channels_ordered(raw.info, chroma) assert len(picks) == len(raw.ch_names) with pytest.raises(ValueError, match='chromophore in info'): _check_channels_ordered(raw.info, ["hbr", "hbo"])
def test_fnirs_channel_naming_and_order_readers(fname): """Ensure fNIRS channel checking on standard readers.""" # fNIRS data requires specific channel naming and ordering. # All standard readers should pass tests raw = read_raw_nirx(fname) freqs = np.unique(_channel_frequencies(raw)) assert_array_equal(freqs, [760, 850]) chroma = np.unique(_channel_chromophore(raw)) assert len(chroma) == 0 picks = _check_channels_ordered(raw, freqs) assert len(picks) == len(raw.ch_names) # as all fNIRS only data # Check that dropped channels are detected # For each source detector pair there must be two channels, # removing one should throw an error. raw_dropped = raw.copy().drop_channels(raw.ch_names[4]) with pytest.raises(ValueError, match='not ordered correctly'): _check_channels_ordered(raw_dropped, freqs) # The ordering must match the passed in argument raw_names_reversed = raw.copy().ch_names raw_names_reversed.reverse() raw_reversed = raw.copy().pick_channels(raw_names_reversed, ordered=True) with pytest.raises(ValueError, match='not ordered .* frequencies'): _check_channels_ordered(raw_reversed, freqs) # So if we flip the second argument it should pass again _check_channels_ordered(raw_reversed, [850, 760]) # Check on OD data raw = optical_density(raw) freqs = np.unique(_channel_frequencies(raw)) assert_array_equal(freqs, [760, 850]) chroma = np.unique(_channel_chromophore(raw)) assert len(chroma) == 0 picks = _check_channels_ordered(raw, freqs) assert len(picks) == len(raw.ch_names) # as all fNIRS only data # Check on haemoglobin data raw = beer_lambert_law(raw) freqs = np.unique(_channel_frequencies(raw)) assert len(freqs) == 0 assert len(_channel_chromophore(raw)) == len(raw.ch_names) chroma = np.unique(_channel_chromophore(raw)) assert_array_equal(chroma, ["hbo", "hbr"]) picks = _check_channels_ordered(raw, chroma) assert len(picks) == len(raw.ch_names) with pytest.raises(ValueError, match='not ordered .* chromophore'): _check_channels_ordered(raw, ["hbx", "hbr"])
def test_fnirs_channel_naming_and_order_custom_chroma(): """Ensure fNIRS channel checking on manually created data.""" data = np.random.RandomState(0).randn(6, 10) # Start with a correctly named raw intensity dataset # These are the steps required to build an fNIRS Raw object from scratch ch_names = [ 'S1_D1 hbo', 'S1_D1 hbr', 'S2_D1 hbo', 'S2_D1 hbr', 'S3_D1 hbo', 'S3_D1 hbr' ] ch_types = np.tile(["hbo", "hbr"], 3) info = create_info(ch_names=ch_names, ch_types=ch_types, sfreq=1.0) raw = RawArray(data, info, verbose=True) chroma = np.unique(_channel_chromophore(raw.info)) picks = _check_channels_ordered(raw.info, chroma) assert len(picks) == len(raw.ch_names) assert len(picks) == 6 # Test block creation fails ch_names = [ 'S1_D1 hbo', 'S2_D1 hbo', 'S3_D1 hbo', 'S1_D1 hbr', 'S2_D1 hbr', 'S3_D1 hbr' ] ch_types = np.repeat(["hbo", "hbr"], 3) info = create_info(ch_names=ch_names, ch_types=ch_types, sfreq=1.0) raw = RawArray(data, info, verbose=True) with pytest.raises(ValueError, match='not ordered .* chromophore'): _check_channels_ordered(raw.info, ["hbo", "hbr"]) # Reordering should fix raw.pick(picks=[0, 3, 1, 4, 2, 5]) _check_channels_ordered(raw.info, ["hbo", "hbr"]) # Wrong names should fail with pytest.raises(ValueError, match='not ordered .* chromophore'): _check_channels_ordered(raw.info, ["hbb", "hbr"]) # Test weird naming ch_names = [ 'S1_D1 hbb', 'S1_D1 hbr', 'S2_D1 hbb', 'S2_D1 hbr', 'S3_D1 hbb', 'S3_D1 hbr' ] ch_types = np.tile(["hbo", "hbr"], 3) info = create_info(ch_names=ch_names, ch_types=ch_types, sfreq=1.0) raw = RawArray(data, info, verbose=True) with pytest.raises(ValueError, match='naming conventions'): _check_channels_ordered(raw.info, ["hbb", "hbr"]) # Check more weird naming ch_names = [ 'S1_DX hbo', 'S1_DX hbr', 'S2_D1 hbo', 'S2_D1 hbr', 'S3_D1 hbo', 'S3_D1 hbr' ] ch_types = np.tile(["hbo", "hbr"], 3) info = create_info(ch_names=ch_names, ch_types=ch_types, sfreq=1.0) raw = RawArray(data, info, verbose=True) with pytest.raises(ValueError, match='can not be parsed'): _check_channels_ordered(raw.info, ["hbo", "hbr"])