Skip to content

Commit

Permalink
Merge pull request #32 from cibr-jyu/fix-bugs
Browse files Browse the repository at this point in the history
Fix bugs
  • Loading branch information
teekuningas authored Nov 27, 2023
2 parents 91ac78a + 569c43a commit 24b6029
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
41 changes: 38 additions & 3 deletions 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 All @@ -63,10 +95,13 @@ def group_average_evoked(experiment, evoked_name, groups, new_name):
grand_averages[new_key] = mne.grand_average(grand_evoked)
new_keys.append(new_key)

subject = experiment.active_subject
# to avoid mne complaints, set dev_head_t manually to same for all
keys = sorted(grand_averages.keys())
for key in keys:
grand_averages[key].info['dev_head_t'] = grand_averages[keys[0]].info['dev_head_t']

subject = experiment.active_subject
evoked_directory = subject.evoked_directory

params = {'conditions': new_keys,
'groups': groups}

Expand Down
20 changes: 14 additions & 6 deletions meggie/actions/tfr_plot_tse/controller/tfr.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ def individual_plot(ax, info_idx, names_idx):
ax.legend()

ax.set_xlabel('Time (s)')
ax.set_ylabel('Power ({})'.format(get_power_unit(
mne.io.pick.channel_type(info, info_idx),
False
)))

if blmode and blmode != "mean":
ax.set_ylabel('Power')
else:
ax.set_ylabel('Power ({})'.format(get_power_unit(
mne.io.pick.channel_type(info, info_idx),
log=False
)))

plt.show()

Expand Down Expand Up @@ -158,8 +162,12 @@ def plot_fun(ax_idx, ax):
ch_group = ch_groups[ax_idx]
ax.set_title(ch_group)
ax.set_xlabel('Time (s)')
ax.set_ylabel('Power ({})'.format(get_power_unit(
ch_type, False)))

if blmode and blmode != "mean":
ax.set_ylabel('Power')
else:
ax.set_ylabel('Power ({})'.format(get_power_unit(
ch_type, log=False)))

for color_idx, (key, times, curve) in enumerate(averages[(ch_type, ch_group)]):
ax.plot(times, curve, color=colors[color_idx], label=key)
Expand Down

0 comments on commit 24b6029

Please sign in to comment.