def get_head_correct_info(subject, session, N=-1): filename = metadata.get_raw_filename(subject, session) trans = get_ctf_trans(filename) fiducials = get_ref_head_pos(subject, session, trans, N=N) raw = mne.io.ctf.read_raw_ctf(filename) info = replace_fiducials(raw.info, fiducials) return trans, fiducials, info
def get_block_meta(snum, session, block_in_raw, block_in_experiment): data = empirical.load_data() data = empirical.data_cleanup(data) filename = metadata.get_raw_filename(snum, session) raw = mne.io.read_raw_ctf(filename, system_clock='ignore') trials = blocks(raw) if not (block_in_raw in np.unique(trials['block'])): err_msg = 'Error when processing %i, %i, %i, %i, data file = %s' % ( snum, session, block_in_raw, block_in_experiment, filename) raise RuntimeError(err_msg) r, _ = load_block(raw, trials, block_in_raw) meta, timing = get_meta(data, r, snum, block_in_experiment, filename) return pd.concat((meta, timing), axis=1)
def get_leadfield(subject, session, head_model="three_layer"): """ Compute leadfield with presets for this subject Parameters head_model : str, 'three_layer' or 'single_layer' """ raw_filename = metadata.get_raw_filename(subject, session) epoch_filename = metadata.get_epoch_filename(subject, session, 0, "stimulus", "fif") trans = get_trans(subject, session) return pymegsr.get_leadfield( "S%02i" % subject, raw_filename, epoch_filename, trans, conductivity=(0.3, 0.006, 0.3), bem_sub_path="bem_ft", njobs=4, )
from conf_analysis.meg import artifacts, preprocessing from conf_analysis.behavior import empirical, metadata, keymap import mne, locale import numpy as np import pickle locale.setlocale(locale.LC_ALL, "en_US") result = {} raw = mne.io.read_raw_ctf(filename, system_clock='ignore') trials = preprocessing.blocks(raw, full_file_cache=True) trl, bl = trials['trial'], trials['block'] bcnt = 0 for b in np.unique(bl): if len(trl[bl == b]) >= 75: result[b] = bcnt bcnt += 1 print((b, bcnt)) return result block_map = {} for snum in range(1, 16): filenames = [metadata.get_raw_filename(snum, b) for b in range(4)] block_map[snum] = {} for session, filename in enumerate(filenames): block_map[snum][session] = executor.submit(do_one, filename) diagnostics.progress(block_map) block_map = executor.gather(block_map) pickle.dump(block_map, open('blockmap.pickle', 'w'))
def one_block(snum, session, block_in_raw, block_in_experiment): ''' Preprocess a single block and save results. Parameters ---------- snum, session : int Subject number and session number raw : mne.io.Raw object Raw data for an entire session of a subject. block_in_raw, block_in_experiment : int Each succesfull session consists out of five blocks, yet a sessions MEG data file sometimes contains more. This happens, for example, when a block is restarted. 'block_in_raw' refers to the actual block in a raw file, and block_in_experiment to the block in the metadata that block_in_raw should be mapped to. block_in_experiment will be used for saving. ''' try: art_fname = metadata.get_epoch_filename(snum, session, block_in_experiment, None, 'artifacts') data = empirical.load_data() data = empirical.data_cleanup(data) filename = metadata.get_raw_filename(snum, session) raw = mne.io.read_raw_ctf(filename, system_clock='ignore') trials = blocks(raw) if not (block_in_raw in np.unique(trials['block'])): err_msg = 'Error when processing %i, %i, %i, %i, data file = %s' % ( snum, session, block_in_raw, block_in_experiment, filename) raise RuntimeError(err_msg) # Load data and preprocess it. logging.info('Loading block of data: %s; block: %i' % (filename, block_in_experiment)) r, r_id = load_block(raw, trials, block_in_raw) r_id['filname'] = filename print(('Working on:', filename, block_in_experiment, block_in_raw)) logging.info('Starting artifact detection') r, ants, artdefs = pymegprepr.preprocess_block(r) #r.annotations = r print('Notch filtering') midx = np.where([x.startswith('M') for x in r.ch_names])[0] r.notch_filter(np.arange(50, 251, 50), picks=midx) logging.info('Aligning meta data') meta, timing = get_meta(data, r, snum, block_in_experiment, filename) idx = np.isnan(meta.response.values) meta = meta.loc[~idx, :] timing = timing.loc[~idx, :] artdefs['id'] = r_id filenames = [] for epoch, event, (tmin, tmax), (rmin, rmax) in zip( ['stimulus', 'response', 'feedback'], ['stim_onset_t', 'button_t', 'meg_feedback_t'], [(-.75, 1.5), (-1.5, 1), (-1, 1)], [(0, 1), (-1, 0.5), (-0.5, 0.5)]): logging.info('Processing epoch: %s' % epoch) try: m, s = pymegprepr.get_epoch( r, meta, timing, event=event, epoch_time=(tmin, tmax), reject_time=(rmin, rmax), ) except RuntimeError as e: print(e) continue if len(s) > 0: epo_fname = metadata.get_epoch_filename( snum, session, block_in_experiment, epoch, 'fif') epo_metaname = metadata.get_epoch_filename( snum, session, block_in_experiment, epoch, 'meta') s = s.resample(600, npad='auto') s.save(epo_fname) m.to_hdf(epo_metaname, 'meta') r_id[epoch] = len(s) filenames.append(epo_fname) pickle.dump(artdefs, open(art_fname, 'wb'), protocol=2) except MemoryError: print((snum, session, block_in_raw, block_in_experiment)) raise RuntimeError('MemoryError caught in one block ' + str(snum) + ' ' + str(session) + ' ' + str(block_in_raw) + ' ' + str(block_in_experiment)) return 'Finished', snum, session, block_in_experiment, filenames