Skip to content

Commit

Permalink
Before grand averaging, find a common set of non-bad channels
Browse files Browse the repository at this point in the history
  • Loading branch information
teekuningas committed Nov 27, 2023
1 parent 5ad0c37 commit 569c43a
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion meggie/actions/evoked_group_average/controller/evoked.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,36 @@ def group_average_evoked(experiment, evoked_name, groups, new_name):

assert_arrays_same(time_arrays)

# handle channel differences
ch_names = []
for group_key, group_subjects in groups.items():
for subject_name in group_subjects:
try:
subject = experiment.subjects.get(subject_name)
evoked = subject.evoked.get(evoked_name)

mne_evokeds = evoked.content
for mne_evoked in mne_evokeds.values():
ch_idxs = mne.pick_types(mne_evoked.info, meg=True, eeg=True)
ch_names.append(tuple(
[ch_name for ch_idx, ch_name
in enumerate(mne_evoked.info['ch_names'])
if ch_idx in ch_idxs]
))
except Exception as exc:
continue

if len(set(ch_names)) != 1:
logging.getLogger('ui_logger').debug(
"Evokeds contain different sets of good channels. Identifying common ones..")

common_ch_names = list(set.intersection(*map(set, ch_names)))

logging.getLogger('ui_logger').debug(
str(len(common_ch_names)) + ' common channels found.')
else:
common_ch_names = ch_names[0]

grand_evokeds = {}
for group_key, group_subjects in groups.items():
for subject in experiment.subjects.values():
Expand All @@ -51,7 +81,9 @@ def group_average_evoked(experiment, evoked_name, groups, new_name):

if grand_key not in grand_evokeds:
grand_evokeds[grand_key] = []
grand_evokeds[grand_key].append(evoked_item)
grand_evokeds[grand_key].append(
evoked_item.copy().pick(common_ch_names)
)

grand_averages = {}
new_keys = []
Expand Down

0 comments on commit 569c43a

Please sign in to comment.