Skip to content

Commit

Permalink
hopefully at least 80%
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidtfa committed Dec 4, 2024
1 parent 4af73f0 commit 8f8203c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions almkanal/almkanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)):
Expand Down
19 changes: 12 additions & 7 deletions almkanal/src_utils/src_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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."""

Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down
18 changes: 8 additions & 10 deletions tests/test_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -107,3 +102,6 @@ def test_fwd(gen_mne_data_raw, source, atlas):
atlas=atlas,
return_parc=True,)




0 comments on commit 8f8203c

Please sign in to comment.