Skip to content

Instantly share code, notes, and snippets.

@jhouck
Created May 23, 2014 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhouck/0a61a4f420c80356d54e to your computer and use it in GitHub Desktop.
Save jhouck/0a61a4f420c80356d54e to your computer and use it in GitHub Desktop.
Break compute_source_psd_epochs by setting n_jobs too high
#!/usr/bin/env python
import numpy as np
import mne
from mne.minimum_norm import read_inverse_operator, compute_source_psd_epochs
import os
import sys, traceback
import multiprocessing
# The rank of these data with 21 epochs for this label is 12. If n_jobs=12, the PSD will compute without error.
# It looks like n_jobs can't exceed the lowest rank
n_jobs = 13
n_cpu = multiprocessing.cpu_count()
print('You have %i CPUs. Try not to use them all in one place.\n' % n_cpu)
subject = 'sample'
# I can never remember where the sample data are supposed to go, this was my workaround.
data_path = os.getenv('MNE_SAMPLE') + '/MEG/' + subject
subjects_dir = os.getenv('MNE_SAMPLE') + '/subjects'
method = "MNE"
snr = 1.0
lambda2 = 1.0 / snr ** 2
fmin, fmax = 4., 20.
bandwidth = 4. # bandwidth of the windows in Hz
fname_raw = data_path + '/sample_audvis_raw.fif'
fname_event = data_path + '/sample_audvis_raw-eve.fif'
fname_inv = data_path + '/sample_audvis-meg-oct-6-meg-inv.fif'
# Get inverse operator
inverse_operator = read_inverse_operator(fname_inv)
# Load data
raw = mne.io.Raw(fname_raw)
# Read events
events = mne.read_events(fname_event)
# Set up pick list
include = []
picks = mne.pick_types(raw.info, meg=True, eeg=False, stim=True, eog=True,
exclude=raw.info['bads'])
src = inverse_operator['src']
# Get labels for FreeSurfer a2009s cortical parcellation
labels = mne.read_annot(subject, parc='aparc.a2009s', hemi='both',
subjects_dir=subjects_dir)
lh_labels = [label for label in labels if label.hemi.endswith('lh')]
lh_names = [label.name for label in lh_labels]
try:
event_id, tmin, tmax = 1, -0.1, 1.5
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks, baseline=(None, 0), preload=True, reject=None)
dropped_epochs=np.arange(21,72)
epochs.drop_epochs(dropped_epochs)
# Pick G_front_inf-Orbital-lh instead of enumerating through all 75...
label = lh_labels[13]
print 'Read label %s' % (label.name)
n_epochs_on, n_chan, n_times = epochs._data.shape
# Read epochs
print 'Computing PSD for %s onset, event %s in label %s' % ( subject, event_id, label.name)
# This will also fail if return_generator=False
stcs = compute_source_psd_epochs(epochs, inverse_operator, lambda2=lambda2,
method=method, fmin=fmin, fmax=fmax, bandwidth=bandwidth, adaptive=True,
label=label, n_jobs=n_jobs, return_generator=True)
for i, stc in enumerate(stcs):
if i >= n_epochs_on:
break
elif i == 0:
psd_avg_on = np.mean(stc.data, axis=0)
print i
else:
psd_avg_on += np.mean(stc.data, axis=0)
print i
psd_avg_on /= n_epochs_on
except:
print('\nThat didn\'t go well -- problem with %s onset, check debug output below.\n' % subject)
traceback.print_exc(file=sys.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment