diff --git a/almkanal/almkanal.py b/almkanal/almkanal.py index e0b2df4..edfaa57 100644 --- a/almkanal/almkanal.py +++ b/almkanal/almkanal.py @@ -256,7 +256,7 @@ def do_src( self, data_cov: None | NDArray = None, noise_cov: None | NDArray = None, - empty_room_path: None | str = None, + empty_room: None | str | mne.io.Raw = None, return_parc: bool = False, subject_id: None | str = None, subjects_dir: None | str = None, @@ -283,7 +283,7 @@ def do_src( data_cov=data_cov, noise_cov=noise_cov, preproc_info=self.info, - empty_room_path=empty_room_path, + empty_room=empty_room, ) if np.logical_and(return_parc, np.logical_and(subject_id is not None, subjects_dir is not None)): diff --git a/almkanal/src_utils/src_utils.py b/almkanal/src_utils/src_utils.py index bf8b17c..7fb2f85 100644 --- a/almkanal/src_utils/src_utils.py +++ b/almkanal/src_utils/src_utils.py @@ -96,12 +96,16 @@ def process_empty_room( icas: None | list, ica_ids: None | list, preproc_info: InfoClass, - empty_room_path: str, + empty_room: str | mne.io.Raw, get_nearest: bool = False, ) -> tuple[NDArray, NDArray]: - fname_empty_room = get_nearest_empty_room(info, empty_room_dir=empty_room_path) if get_nearest else empty_room_path - - raw_er = mne.io.read_raw(fname_empty_room, preload=True) + if np.logical_and(get_nearest, isinstance(empty_room, str)): + fname_empty_room = get_nearest_empty_room(info, empty_room_dir=empty_room) + raw_er = mne.io.read_raw(fname_empty_room, preload=True) + elif np.logical_and(not get_nearest, isinstance(empty_room, str)): + raw_er = mne.io.read_raw(empty_room, preload=True) + elif isinstance(empty_room, mne.io.Raw): + raw_er = empty_room raw_er = preproc_empty_room(raw_er=raw_er, data=data, preproc_info=preproc_info, icas=icas, ica_ids=ica_ids) @@ -129,7 +133,7 @@ def data2source( ica_ids: None | list = None, data_cov: None | NDArray = None, noise_cov: None | NDArray = None, - empty_room_path: None | str = None, + empty_room: None | str | mne.io.Raw = None, ) -> tuple[mne.SourceEstimate, mne.beamformer.Beamformer]: """This function does source reconstruction using lcmv beamformers based on raw data.""" @@ -167,7 +171,8 @@ def data2source( # per default we take this from an empty room recording # importantly this should be preprocessed similarly to the actual data (except for ICA) if np.logical_and(n_ch_types > 1, noise_cov is None): - assert isinstance(empty_room_path, str), """Please specify either a path that leads directly + assert np.logical_or(isinstance(empty_room, str), isinstance(empty_room, mne.io.Raw)), """Please + supply either a mne.io.raw object, a path that leads directly to an empty_room recording or a folder with a bunch of empty room recordings""" true_rank, noise_cov = process_empty_room( data=data, @@ -176,7 +181,7 @@ def data2source( preproc_info=preproc_info, icas=icas, ica_ids=ica_ids, - empty_room_path=empty_room_path, + empty_room=empty_room, ) elif n_ch_types == 1: diff --git a/tests/test_raw.py b/tests/test_raw.py index caead0c..da784a1 100644 --- a/tests/test_raw.py +++ b/tests/test_raw.py @@ -3,23 +3,18 @@ from .settings import CH_PICKS, ICA_TRAIN, ICA_EOG, ICA_ECG, ICA_THRESH, ICA_RESAMPLE, ICA_NCOMPS, SOURCE import mne - - -@pytest.mark.parametrize('ch_picks', CH_PICKS, scope='session') -def test_src(gen_mne_data_raw, ch_picks): #, ch_picks +#@pytest.mark.parametrize('ch_picks', CH_PICKS, scope='session') +def test_src(gen_mne_data_raw): #, ch_picks data_path = mne.datasets.sample.data_path() meg_path = data_path / 'MEG' / 'sample' - raw_fname = meg_path / 'sample_audvis_raw.fif' ak = AlmKanal(raw=gen_mne_data_raw) fwd_fname = meg_path / 'sample_audvis-meg-vol-7-fwd.fif' fwd = mne.read_forward_solution(fwd_fname) - ak.pick_dict['meg'] = ch_picks + ak.pick_dict['meg'] = True ak.fwd = fwd - if ch_picks: - ak.do_src(empty_room_path=raw_fname) - else: - ak.do_src() + ak.do_src(empty_room=gen_mne_data_raw) + def test_maxwell(gen_mne_data_raw): @@ -107,3 +102,6 @@ def test_fwd(gen_mne_data_raw, source, atlas): atlas=atlas, return_parc=True,) + + +