def test_spectrum(): with tempfile.TemporaryDirectory() as dirpath: sample_folder = mne.datasets.sample.data_path() sample_fname = os.path.join(sample_folder, 'MEG', 'sample', 'sample_audvis_raw.fif') raw = mne.io.read_raw_fif(sample_fname, preload=True) psds, freqs = psd_welch(raw, fmin=1, fmax=40, tmin=1, tmax=10) ch_names = raw.info['ch_names'] name = 'TestSpectrum' cond_name = '1' spectrum_dir = os.path.join(dirpath, 'spectrums') # one meggie-Spectrum can hold many spectrums, thus content is dict-like content = {cond_name: psds} params = {'conditions': [cond_name]} # Create meggie-Spectrum object with spectrum array stored within # and save it to spectrum directory spectrum = Spectrum(name, spectrum_dir, params, content=content, freqs=freqs, info=raw.info) ensure_folders([spectrum_dir]) spectrum.save_content() # Creating meggie-Spectrum object with same name and folder should allow # accessing the saved content loaded_spectrum = Spectrum(name, spectrum_dir, params) assert (list(loaded_spectrum.content.keys())[0] == cond_name)
def test_evoked(): with tempfile.TemporaryDirectory() as dirpath: sample_folder = mne.datasets.sample.data_path() sample_fname = os.path.join(sample_folder, 'MEG', 'sample', 'sample_audvis_raw.fif') name = 'TestEvoked' cond_name = 'cond_1' evoked_dir = os.path.join(dirpath, 'evoked') raw = mne.io.read_raw_fif(sample_fname, preload=True) events = find_events(raw, id_=1) mne_evoked = mne.Epochs(raw, events).average() # As meggie-style evokeds can be based on multiple mne evoked objects, # content is dict-type. content = {cond_name: mne_evoked} params = {} # Create meggie-Evoked object # and save it to evoked directory evoked = Evoked(name, evoked_dir, params, content=content) ensure_folders([evoked_dir]) evoked.save_content() # Creating meggie-Evoked object with same name and folder should allow # accessing the saved content. loaded_evoked = Evoked(name, evoked_dir, params) assert(loaded_evoked.content[cond_name].nave == 72)
def test_epochs(): with tempfile.TemporaryDirectory() as dirpath: sample_folder = mne.datasets.sample.data_path() sample_fname = os.path.join(sample_folder, 'MEG', 'sample', 'sample_audvis_raw.fif') raw = mne.io.read_raw_fif(sample_fname, preload=True) events = find_events(raw, id_=1) mne_epochs = mne.Epochs(raw, events) name = 'TestEpochs' epochs_dir = os.path.join(dirpath, 'epochs') params = {} # Create meggie-Epochs object with mne-Epochs stored within # and save it to epochs directory epochs = Epochs(name, epochs_dir, params, content=mne_epochs) ensure_folders([epochs_dir]) epochs.save_content() # Creating meggie-Epochs object with same name and folder should allow # accessing the saved content loaded_epochs = Epochs(name, epochs_dir, params) assert(epochs.count == loaded_epochs.count == 72)
def ensure_folders(self): """ When called, checks that the subject folder with all datatype folders exist and if not, creates them. """ paths = [] datatype_specs = find_all_datatype_specs() for source, package, datatype_spec in datatype_specs.values(): datatype = datatype_spec['id'] path = getattr(self, datatype + '_directory') paths.append(path) try: filemanager.ensure_folders([self.path] + paths) except OSError: raise OSError("Couldn't create all the necessary folders. " "Please ensure that the experiment folder " "has write permissions everywhere.")
def ensure_folders(self): paths = [] for source in find_all_sources(): datatype_path = pkg_resources.resource_filename( source, 'datatypes') if not os.path.exists(datatype_path): continue for package in os.listdir(datatype_path): config_path = os.path.join(datatype_path, package, 'configuration.json') if os.path.exists(config_path): with open(config_path, 'r') as f: config = json.load(f) datatype = config['id'] path = getattr(self, datatype + '_directory') paths.append(path) try: filemanager.ensure_folders([self.path] + paths) except OSError: raise OSError("Couldn't create all the necessary folders. " "Do you have the necessary permissions?")
def test_tfr(): with tempfile.TemporaryDirectory() as dirpath: sample_folder = mne.datasets.sample.data_path() sample_fname = os.path.join(sample_folder, 'MEG', 'sample', 'sample_audvis_raw.fif') name = 'TestTFR' cond_name = 'cond_1' tfr_dir = os.path.join(dirpath, 'tfr') raw = mne.io.read_raw_fif(sample_fname, preload=True) events = find_events(raw, id_=1) freqs = [8, 9, 10, 11, 12] n_cycles = 2 mne_tfr = tfr_morlet(mne.Epochs(raw, events), freqs, n_cycles, return_itc=False) # As meggie-style tfrs can be based on multiple mne TFR objects, # content is dict-type. conditions-param is added to avoid accidents # in content loading.. content = {cond_name: mne_tfr} params = {'conditions': [cond_name]} # Create meggie-TFR object # and save it to tfr directory tfr = TFR(name, tfr_dir, params, content=content) ensure_folders([tfr_dir]) tfr.save_content() # Creating meggie-TFR object with same name and folder should allow # accessing the saved content. loaded_tfr = TFR(name, tfr_dir, params) assert (loaded_tfr.content[cond_name].nave == 72)