def test_fnirs_channel_naming_and_order_custom_optical_density(): """Ensure fNIRS channel checking on manually created data.""" data = np.random.normal(size=(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 760', 'S1_D1 850', 'S2_D1 760', 'S2_D1 850', 'S3_D1 760', 'S3_D1 850' ] ch_types = np.repeat("fnirs_od", 6) info = create_info(ch_names=ch_names, ch_types=ch_types, sfreq=1.0) raw = RawArray(data, info, verbose=True) freqs = np.tile([760, 850], 3) for idx, f in enumerate(freqs): raw.info["chs"][idx]["loc"][9] = f freqs = np.unique(_channel_frequencies(raw.info)) picks = _check_channels_ordered(raw.info, freqs) assert len(picks) == len(raw.ch_names) assert len(picks) == 6 # Check block naming for optical density ch_names = [ 'S1_D1 760', 'S2_D1 760', 'S3_D1 760', 'S1_D1 850', 'S2_D1 850', 'S3_D1 850' ] ch_types = np.repeat("fnirs_od", 6) info = create_info(ch_names=ch_names, ch_types=ch_types, sfreq=1.0) raw = RawArray(data, info, verbose=True) freqs = np.repeat([760, 850], 3) for idx, f in enumerate(freqs): raw.info["chs"][idx]["loc"][9] = f with pytest.raises(ValueError, match='channels not ordered correctly'): _check_channels_ordered(raw.info, [760, 850]) # and this is how you would fix the ordering, then it should pass raw.pick(picks=[0, 3, 1, 4, 2, 5]) _check_channels_ordered(raw.info, [760, 850]) # Check that if you mix types you get an error 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) raw2 = RawArray(data, info, verbose=True) raw.add_channels([raw2]) with pytest.raises(ValueError, match='does not support a combination'): _check_channels_ordered(raw.info, [760, 850])
def bv2fif(dataf, corf, ch_order=None, eogs=('VEOG', 'HEOG'), ecg='ECG', emg='EMG', preload='default', ref_ch='Fp1', dbs=False, new_sfreq=1000.0): montage = read_dig_montage(bvct=corf) if preload == 'default': preload = os.path.dirname(dataf) + '/workfile' raw = read_raw_brainvision(dataf, preload=preload) if dbs: event_ch = get_events(raw) # save downsampled raw for multitaper spectrogram raw_data = np.zeros( (raw._data.shape[0], int(raw._data.shape[1] / raw.info['sfreq'] * new_sfreq))) raw_info = raw.info.copy() raw_info['sfreq'] = new_sfreq for i in tqdm(range(len(raw._data))): ch = raw._data[i, ::int(raw.info['sfreq'] / new_sfreq)] raw_data[i] = ch del ch raw_resampled = RawArray(raw_data, raw_info) raw_resampled.annotations = raw.annotations if dbs: old_event_ch = [ch for ch in raw.info['ch_names'] if 'STI' in ch] if old_event_ch: raw_resampled.drop_channels([old_event_ch[0]]) event_ch._data = event_ch._data[:, ::int(raw.info['sfreq'] / new_sfreq)] event_ch.info['sfreq'] = new_sfreq event_ch.__len__ = len(event_ch._data[0]) event_ch.info['lowpass'] = raw_resampled.info['lowpass'] raw_resampled.add_channels([event_ch]) prepInst(raw_resampled, dataf, 'raw', montage, ref_ch, eogs, ecg, emg) events, event_ids = events_from_annotations(raw) if len(np.unique(events[:, 2])) > 1: events = events[np.where(events[:, 2] == events[1, 2])[0]] #skip new segment epochs = Epochs(raw, events, tmin=-2, tmax=2, proj=False, preload=op.dirname(dataf) + '/workfile-epo', baseline=(-0.5, -0.1), verbose=False, detrend=1) events = events[ epochs. selection] #in case any epochs don't have data and get thrown out (poorly placed at beginning or end) epo_data = np.zeros( (epochs._data.shape[0], epochs._data.shape[1], int(np.ceil(epochs._data.shape[2] / epochs.info['sfreq'] * new_sfreq)))) for i in tqdm(range(epochs._data.shape[0])): for j in range(epochs._data.shape[1]): epo_curr = epochs._data[i, j, ::int(epochs.info['sfreq'] / new_sfreq)] epo_data[i, j] = epo_curr del epo_curr events[:, 0] = np.array(events[:, 0] * new_sfreq / raw.info['sfreq'], dtype=int) epo_resampled = EpochsArray(epo_data, epochs.info.copy(), events, tmin=-2) epo_resampled.info['sfreq'] = new_sfreq epo_resampled.events[:, 2] = np.arange(len(events)) epo_resampled.event_id = {str(i): i for i in range(len(events))} prepInst(epo_resampled, dataf, 'epo', montage, ref_ch, eogs, ecg, emg)