def test_rank_estimation(): """Test raw rank estimation """ iter_tests = itt.product([fif_fname, hp_fif_fname], ["norm", dict(mag=1e11, grad=1e9, eeg=1e5)]) # sss for fname, scalings in iter_tests: raw = Raw(fname) (_, picks_meg), (_, picks_eeg) = _picks_by_type(raw.info, meg_combined=True) n_meg = len(picks_meg) n_eeg = len(picks_eeg) raw = Raw(fname, preload=True) if "proc_history" not in raw.info: expected_rank = n_meg + n_eeg else: mf = raw.info["proc_history"][0]["max_info"] expected_rank = _get_sss_rank(mf) + n_eeg assert_array_equal(raw.estimate_rank(scalings=scalings), expected_rank) assert_array_equal(raw.estimate_rank(picks=picks_eeg, scalings=scalings), n_eeg) raw = Raw(fname, preload=False) if "sss" in fname: tstart, tstop = 0.0, 30.0 raw.add_proj(compute_proj_raw(raw)) raw.apply_proj() else: tstart, tstop = 10.0, 20.0 raw.apply_proj() n_proj = len(raw.info["projs"]) assert_array_equal( raw.estimate_rank(tstart=tstart, tstop=tstop, scalings=scalings), expected_rank - (1 if "sss" in fname else n_proj), )
def test_set_channel_types(): """Test set_channel_types """ raw = Raw(raw_fname) # Error Tests # Test channel name exists in ch_names mapping = {'EEG 160': 'EEG060'} assert_raises(ValueError, raw.set_channel_types, mapping) # Test change to illegal channel type mapping = {'EOG 061': 'xxx'} assert_raises(ValueError, raw.set_channel_types, mapping) # Test changing type if in proj (avg eeg ref here) mapping = { 'EEG 058': 'ecog', 'EEG 059': 'ecg', 'EEG 060': 'eog', 'EOG 061': 'seeg', 'MEG 2441': 'eeg', 'MEG 2443': 'eeg' } assert_raises(RuntimeError, raw.set_channel_types, mapping) # Test type change raw2 = Raw(raw_fname, add_eeg_ref=False) raw2.info['bads'] = ['EEG 059', 'EEG 060', 'EOG 061'] with warnings.catch_warnings(record=True): # MEG channel change assert_raises(RuntimeError, raw2.set_channel_types, mapping) # has prj raw2.add_proj([], remove_existing=True) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') raw2.set_channel_types(mapping) assert_true(len(w) >= 1, msg=[str(ww.message) for ww in w]) assert_true(all('The unit for channel' in str(ww.message) for ww in w)) info = raw2.info assert_true(info['chs'][372]['ch_name'] == 'EEG 058') assert_true(info['chs'][372]['kind'] == FIFF.FIFFV_ECOG_CH) assert_true(info['chs'][372]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][372]['coil_type'] == FIFF.FIFFV_COIL_EEG) assert_true(info['chs'][373]['ch_name'] == 'EEG 059') assert_true(info['chs'][373]['kind'] == FIFF.FIFFV_ECG_CH) assert_true(info['chs'][373]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][373]['coil_type'] == FIFF.FIFFV_COIL_NONE) assert_true(info['chs'][374]['ch_name'] == 'EEG 060') assert_true(info['chs'][374]['kind'] == FIFF.FIFFV_EOG_CH) assert_true(info['chs'][374]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][374]['coil_type'] == FIFF.FIFFV_COIL_NONE) assert_true(info['chs'][375]['ch_name'] == 'EOG 061') assert_true(info['chs'][375]['kind'] == FIFF.FIFFV_SEEG_CH) assert_true(info['chs'][375]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][375]['coil_type'] == FIFF.FIFFV_COIL_EEG) for idx in pick_channels(raw.ch_names, ['MEG 2441', 'MEG 2443']): assert_true(info['chs'][idx]['kind'] == FIFF.FIFFV_EEG_CH) assert_true(info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][idx]['coil_type'] == FIFF.FIFFV_COIL_EEG) # Test meaningful error when setting channel type with unknown unit raw.info['chs'][0]['unit'] = 0. ch_types = {raw.ch_names[0]: 'misc'} assert_raises(ValueError, raw.set_channel_types, ch_types)
def test_proj(): """Test SSP proj operations """ tempdir = _TempDir() for proj in [True, False]: raw = Raw(fif_fname, preload=False, proj=proj) assert_true(all(p['active'] == proj for p in raw.info['projs'])) data, times = raw[0:2, :] data1, times1 = raw[0:2] assert_array_equal(data, data1) assert_array_equal(times, times1) # test adding / deleting proj if proj: assert_raises(ValueError, raw.add_proj, [], {'remove_existing': True}) assert_raises(ValueError, raw.del_proj, 0) else: projs = deepcopy(raw.info['projs']) n_proj = len(raw.info['projs']) raw.del_proj(0) assert_true(len(raw.info['projs']) == n_proj - 1) raw.add_proj(projs, remove_existing=False) assert_true(len(raw.info['projs']) == 2 * n_proj - 1) raw.add_proj(projs, remove_existing=True) assert_true(len(raw.info['projs']) == n_proj) # test apply_proj() with and without preload for preload in [True, False]: raw = Raw(fif_fname, preload=preload, proj=False) data, times = raw[:, 0:2] raw.apply_proj() data_proj_1 = np.dot(raw._projector, data) # load the file again without proj raw = Raw(fif_fname, preload=preload, proj=False) # write the file with proj. activated, make sure proj has been applied raw.save(op.join(tempdir, 'raw.fif'), proj=True, overwrite=True) raw2 = Raw(op.join(tempdir, 'raw.fif'), proj=False) data_proj_2, _ = raw2[:, 0:2] assert_allclose(data_proj_1, data_proj_2) assert_true(all(p['active'] for p in raw2.info['projs'])) # read orig file with proj. active raw2 = Raw(fif_fname, preload=preload, proj=True) data_proj_2, _ = raw2[:, 0:2] assert_allclose(data_proj_1, data_proj_2) assert_true(all(p['active'] for p in raw2.info['projs'])) # test that apply_proj works raw.apply_proj() data_proj_2, _ = raw[:, 0:2] assert_allclose(data_proj_1, data_proj_2) assert_allclose(data_proj_2, np.dot(raw._projector, data_proj_2))
def test_proj(): """Test SSP proj operations """ tempdir = _TempDir() for proj in [True, False]: raw = Raw(fif_fname, preload=False, proj=proj) assert_true(all(p['active'] == proj for p in raw.info['projs'])) data, times = raw[0:2, :] data1, times1 = raw[0:2] assert_array_equal(data, data1) assert_array_equal(times, times1) # test adding / deleting proj if proj: assert_raises(ValueError, raw.add_proj, [], {'remove_existing': True}) assert_raises(ValueError, raw.del_proj, 0) else: projs = deepcopy(raw.info['projs']) n_proj = len(raw.info['projs']) raw.del_proj(0) assert_true(len(raw.info['projs']) == n_proj - 1) raw.add_proj(projs, remove_existing=False) assert_true(len(raw.info['projs']) == 2 * n_proj - 1) raw.add_proj(projs, remove_existing=True) assert_true(len(raw.info['projs']) == n_proj) # test apply_proj() with and without preload for preload in [True, False]: raw = Raw(fif_fname, preload=preload, proj=False) data, times = raw[:, 0:2] raw.apply_proj() data_proj_1 = np.dot(raw._projector, data) # load the file again without proj raw = Raw(fif_fname, preload=preload, proj=False) # write the file with proj. activated, make sure proj has been applied raw.save(op.join(tempdir, 'raw.fif'), proj=True, overwrite=True) raw2 = Raw(op.join(tempdir, 'raw.fif'), proj=False) data_proj_2, _ = raw2[:, 0:2] assert_allclose(data_proj_1, data_proj_2) assert_true(all(p['active'] for p in raw2.info['projs'])) # read orig file with proj. active raw2 = Raw(fif_fname, preload=preload, proj=True) data_proj_2, _ = raw2[:, 0:2] assert_allclose(data_proj_1, data_proj_2) assert_true(all(p['active'] for p in raw2.info['projs'])) # test that apply_proj works raw.apply_proj() data_proj_2, _ = raw[:, 0:2] assert_allclose(data_proj_1, data_proj_2) assert_allclose(data_proj_2, np.dot(raw._projector, data_proj_2))
def test_set_channel_types(): """Test set_channel_types """ raw = Raw(raw_fname) # Error Tests # Test channel name exists in ch_names mapping = {'EEG 160': 'EEG060'} assert_raises(ValueError, raw.set_channel_types, mapping) # Test change to illegal channel type mapping = {'EOG 061': 'xxx'} assert_raises(ValueError, raw.set_channel_types, mapping) # Test changing type if in proj (avg eeg ref here) mapping = {'EEG 058': 'ecog', 'EEG 059': 'ecg', 'EEG 060': 'eog', 'EOG 061': 'seeg', 'MEG 2441': 'eeg', 'MEG 2443': 'eeg'} assert_raises(RuntimeError, raw.set_channel_types, mapping) # Test type change raw2 = Raw(raw_fname, add_eeg_ref=False) raw2.info['bads'] = ['EEG 059', 'EEG 060', 'EOG 061'] with warnings.catch_warnings(record=True): # MEG channel change assert_raises(RuntimeError, raw2.set_channel_types, mapping) # has prj raw2.add_proj([], remove_existing=True) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') raw2.set_channel_types(mapping) assert_true(len(w) >= 1, msg=[str(ww.message) for ww in w]) assert_true(all('The unit for channel' in str(ww.message) for ww in w)) info = raw2.info assert_true(info['chs'][372]['ch_name'] == 'EEG 058') assert_true(info['chs'][372]['kind'] == FIFF.FIFFV_ECOG_CH) assert_true(info['chs'][372]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][372]['coil_type'] == FIFF.FIFFV_COIL_EEG) assert_true(info['chs'][373]['ch_name'] == 'EEG 059') assert_true(info['chs'][373]['kind'] == FIFF.FIFFV_ECG_CH) assert_true(info['chs'][373]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][373]['coil_type'] == FIFF.FIFFV_COIL_NONE) assert_true(info['chs'][374]['ch_name'] == 'EEG 060') assert_true(info['chs'][374]['kind'] == FIFF.FIFFV_EOG_CH) assert_true(info['chs'][374]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][374]['coil_type'] == FIFF.FIFFV_COIL_NONE) assert_true(info['chs'][375]['ch_name'] == 'EOG 061') assert_true(info['chs'][375]['kind'] == FIFF.FIFFV_SEEG_CH) assert_true(info['chs'][375]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][375]['coil_type'] == FIFF.FIFFV_COIL_EEG) for idx in pick_channels(raw.ch_names, ['MEG 2441', 'MEG 2443']): assert_true(info['chs'][idx]['kind'] == FIFF.FIFFV_EEG_CH) assert_true(info['chs'][idx]['unit'] == FIFF.FIFF_UNIT_V) assert_true(info['chs'][idx]['coil_type'] == FIFF.FIFFV_COIL_EEG) # Test meaningful error when setting channel type with unknown unit raw.info['chs'][0]['unit'] = 0. ch_types = {raw.ch_names[0]: 'misc'} assert_raises(ValueError, raw.set_channel_types, ch_types)
def test_rank_estimation(): """Test raw rank estimation """ iter_tests = itt.product( [fif_fname, hp_fif_fname], # sss ['norm', dict(mag=1e11, grad=1e9, eeg=1e5)] ) for fname, scalings in iter_tests: raw = Raw(fname) (_, picks_meg), (_, picks_eeg) = _picks_by_type(raw.info, meg_combined=True) n_meg = len(picks_meg) n_eeg = len(picks_eeg) raw = Raw(fname, preload=True) if 'proc_history' not in raw.info: expected_rank = n_meg + n_eeg else: mf = raw.info['proc_history'][0]['max_info'] expected_rank = _get_sss_rank(mf) + n_eeg assert_array_equal(raw.estimate_rank(scalings=scalings), expected_rank) assert_array_equal(raw.estimate_rank(picks=picks_eeg, scalings=scalings), n_eeg) raw = Raw(fname, preload=False) if 'sss' in fname: tstart, tstop = 0., 30. raw.add_proj(compute_proj_raw(raw)) raw.apply_proj() else: tstart, tstop = 10., 20. raw.apply_proj() n_proj = len(raw.info['projs']) assert_array_equal(raw.estimate_rank(tstart=tstart, tstop=tstop, scalings=scalings), expected_rank - (1 if 'sss' in fname else n_proj))
def test_proj(): """Test SSP proj operations """ tempdir = _TempDir() for proj in [True, False]: raw = Raw(fif_fname, preload=False, proj=proj) assert_true(all(p['active'] == proj for p in raw.info['projs'])) data, times = raw[0:2, :] data1, times1 = raw[0:2] assert_array_equal(data, data1) assert_array_equal(times, times1) # test adding / deleting proj if proj: assert_raises(ValueError, raw.add_proj, [], {'remove_existing': True}) assert_raises(ValueError, raw.del_proj, 0) else: projs = deepcopy(raw.info['projs']) n_proj = len(raw.info['projs']) raw.del_proj(0) assert_equal(len(raw.info['projs']), n_proj - 1) raw.add_proj(projs, remove_existing=False) # Test that already existing projections are not added. assert_equal(len(raw.info['projs']), n_proj) raw.add_proj(projs[:-1], remove_existing=True) assert_equal(len(raw.info['projs']), n_proj - 1) # test apply_proj() with and without preload for preload in [True, False]: raw = Raw(fif_fname, preload=preload, proj=False) data, times = raw[:, 0:2] raw.apply_proj() data_proj_1 = np.dot(raw._projector, data) # load the file again without proj raw = Raw(fif_fname, preload=preload, proj=False) # write the file with proj. activated, make sure proj has been applied raw.save(op.join(tempdir, 'raw.fif'), proj=True, overwrite=True) raw2 = Raw(op.join(tempdir, 'raw.fif'), proj=False) data_proj_2, _ = raw2[:, 0:2] assert_allclose(data_proj_1, data_proj_2) assert_true(all(p['active'] for p in raw2.info['projs'])) # read orig file with proj. active raw2 = Raw(fif_fname, preload=preload, proj=True) data_proj_2, _ = raw2[:, 0:2] assert_allclose(data_proj_1, data_proj_2) assert_true(all(p['active'] for p in raw2.info['projs'])) # test that apply_proj works raw.apply_proj() data_proj_2, _ = raw[:, 0:2] assert_allclose(data_proj_1, data_proj_2) assert_allclose(data_proj_2, np.dot(raw._projector, data_proj_2)) tempdir = _TempDir() out_fname = op.join(tempdir, 'test_raw.fif') raw = read_raw_fif(test_fif_fname, preload=True).crop(0, 0.002, copy=False) raw.pick_types(meg=False, eeg=True) raw.info['projs'] = [raw.info['projs'][-1]] raw._data.fill(0) raw._data[-1] = 1. raw.save(out_fname) raw = read_raw_fif(out_fname, proj=True, preload=False) assert_allclose(raw[:, :][0][:1], raw[0, :][0])
def test_multiple_files(): """Test loading multiple files simultaneously """ # split file tempdir = _TempDir() raw = Raw(fif_fname).crop(0, 10) raw.load_data() raw.load_data() # test no operation split_size = 3. # in seconds sfreq = raw.info['sfreq'] nsamp = (raw.last_samp - raw.first_samp) tmins = np.round(np.arange(0., nsamp, split_size * sfreq)) tmaxs = np.concatenate((tmins[1:] - 1, [nsamp])) tmaxs /= sfreq tmins /= sfreq assert_equal(raw.n_times, len(raw.times)) # going in reverse order so the last fname is the first file (need later) raws = [None] * len(tmins) for ri in range(len(tmins) - 1, -1, -1): fname = op.join(tempdir, 'test_raw_split-%d_raw.fif' % ri) raw.save(fname, tmin=tmins[ri], tmax=tmaxs[ri]) raws[ri] = Raw(fname) events = [find_events(r, stim_channel='STI 014') for r in raws] last_samps = [r.last_samp for r in raws] first_samps = [r.first_samp for r in raws] # test concatenation of split file assert_raises(ValueError, concatenate_raws, raws, True, events[1:]) all_raw_1, events1 = concatenate_raws(raws, preload=False, events_list=events) assert_equal(raw.first_samp, all_raw_1.first_samp) assert_equal(raw.last_samp, all_raw_1.last_samp) assert_allclose(raw[:, :][0], all_raw_1[:, :][0]) raws[0] = Raw(fname) all_raw_2 = concatenate_raws(raws, preload=True) assert_allclose(raw[:, :][0], all_raw_2[:, :][0]) # test proper event treatment for split files events2 = concatenate_events(events, first_samps, last_samps) events3 = find_events(all_raw_2, stim_channel='STI 014') assert_array_equal(events1, events2) assert_array_equal(events1, events3) # test various methods of combining files raw = Raw(fif_fname, preload=True) n_times = raw.n_times # make sure that all our data match times = list(range(0, 2 * n_times, 999)) # add potentially problematic points times.extend([n_times - 1, n_times, 2 * n_times - 1]) raw_combo0 = Raw([fif_fname, fif_fname], preload=True) _compare_combo(raw, raw_combo0, times, n_times) raw_combo = Raw([fif_fname, fif_fname], preload=False) _compare_combo(raw, raw_combo, times, n_times) raw_combo = Raw([fif_fname, fif_fname], preload='memmap8.dat') _compare_combo(raw, raw_combo, times, n_times) assert_raises(ValueError, Raw, [fif_fname, ctf_fname]) assert_raises(ValueError, Raw, [fif_fname, fif_bad_marked_fname]) assert_equal(raw[:, :][0].shape[1] * 2, raw_combo0[:, :][0].shape[1]) assert_equal(raw_combo0[:, :][0].shape[1], raw_combo0.n_times) # with all data preloaded, result should be preloaded raw_combo = Raw(fif_fname, preload=True) raw_combo.append(Raw(fif_fname, preload=True)) assert_true(raw_combo.preload is True) assert_equal(raw_combo.n_times, raw_combo._data.shape[1]) _compare_combo(raw, raw_combo, times, n_times) # with any data not preloaded, don't set result as preloaded raw_combo = concatenate_raws( [Raw(fif_fname, preload=True), Raw(fif_fname, preload=False)]) assert_true(raw_combo.preload is False) assert_array_equal(find_events(raw_combo, stim_channel='STI 014'), find_events(raw_combo0, stim_channel='STI 014')) _compare_combo(raw, raw_combo, times, n_times) # user should be able to force data to be preloaded upon concat raw_combo = concatenate_raws( [Raw(fif_fname, preload=False), Raw(fif_fname, preload=True)], preload=True) assert_true(raw_combo.preload is True) _compare_combo(raw, raw_combo, times, n_times) raw_combo = concatenate_raws( [Raw(fif_fname, preload=False), Raw(fif_fname, preload=True)], preload='memmap3.dat') _compare_combo(raw, raw_combo, times, n_times) raw_combo = concatenate_raws( [Raw(fif_fname, preload=True), Raw(fif_fname, preload=True)], preload='memmap4.dat') _compare_combo(raw, raw_combo, times, n_times) raw_combo = concatenate_raws( [Raw(fif_fname, preload=False), Raw(fif_fname, preload=False)], preload='memmap5.dat') _compare_combo(raw, raw_combo, times, n_times) # verify that combining raws with different projectors throws an exception raw.add_proj([], remove_existing=True) assert_raises(ValueError, raw.append, Raw(fif_fname, preload=True)) # now test event treatment for concatenated raw files events = [ find_events(raw, stim_channel='STI 014'), find_events(raw, stim_channel='STI 014') ] last_samps = [raw.last_samp, raw.last_samp] first_samps = [raw.first_samp, raw.first_samp] events = concatenate_events(events, first_samps, last_samps) events2 = find_events(raw_combo0, stim_channel='STI 014') assert_array_equal(events, events2) # check out the len method assert_equal(len(raw), raw.n_times) assert_equal(len(raw), raw.last_samp - raw.first_samp + 1)
def test_rank(): """Test cov rank estimation""" raw_sample = Raw(raw_fname) raw_sss = Raw(hp_fif_fname) raw_sss.add_proj(compute_proj_raw(raw_sss)) cov_sample = compute_raw_data_covariance(raw_sample) cov_sample_proj = compute_raw_data_covariance( raw_sample.copy().apply_proj()) cov_sss = compute_raw_data_covariance(raw_sss) cov_sss_proj = compute_raw_data_covariance(raw_sss.copy().apply_proj()) picks_all_sample = pick_types(raw_sample.info, meg=True, eeg=True) picks_all_sss = pick_types(raw_sss.info, meg=True, eeg=True) info_sample = pick_info(raw_sample.info, picks_all_sample) picks_stack_sample = [('eeg', pick_types(info_sample, meg=False, eeg=True))] picks_stack_sample += [('meg', pick_types(info_sample, meg=True))] picks_stack_sample += [('all', pick_types(info_sample, meg=True, eeg=True))] info_sss = pick_info(raw_sss.info, picks_all_sss) picks_stack_somato = [('eeg', pick_types(info_sss, meg=False, eeg=True))] picks_stack_somato += [('meg', pick_types(info_sss, meg=True))] picks_stack_somato += [('all', pick_types(info_sss, meg=True, eeg=True))] iter_tests = list( itt.product( [(cov_sample, picks_stack_sample, info_sample), (cov_sample_proj, picks_stack_sample, info_sample), (cov_sss, picks_stack_somato, info_sss), (cov_sss_proj, picks_stack_somato, info_sss)], # sss [dict(mag=1e15, grad=1e13, eeg=1e6)])) for (cov, picks_list, this_info), scalings in iter_tests: for ch_type, picks in picks_list: this_very_info = pick_info(this_info, picks) # compute subset of projs this_projs = [ c['active'] and len( set(c['data']['col_names']).intersection( set(this_very_info['ch_names']))) > 0 for c in cov['projs'] ] n_projs = sum(this_projs) # count channel types ch_types = [ channel_type(this_very_info, idx) for idx in range(len(picks)) ] n_eeg, n_mag, n_grad = [ ch_types.count(k) for k in ['eeg', 'mag', 'grad'] ] n_meg = n_mag + n_grad if ch_type in ('all', 'eeg'): n_projs_eeg = 1 else: n_projs_eeg = 0 # check sss if 'proc_history' in this_very_info: mf = this_very_info['proc_history'][0]['max_info'] n_free = _get_sss_rank(mf) if 'mag' not in ch_types and 'grad' not in ch_types: n_free = 0 # - n_projs XXX clarify expected_rank = n_free + n_eeg if n_projs > 0 and ch_type in ('all', 'eeg'): expected_rank -= n_projs_eeg else: expected_rank = n_meg + n_eeg - n_projs C = cov['data'][np.ix_(picks, picks)] est_rank = _estimate_rank_meeg_cov(C, this_very_info, scalings=scalings) assert_equal(expected_rank, est_rank)
def test_proj(): """Test SSP proj operations """ tempdir = _TempDir() for proj in [True, False]: raw = Raw(fif_fname, preload=False, proj=proj) assert_true(all(p['active'] == proj for p in raw.info['projs'])) data, times = raw[0:2, :] data1, times1 = raw[0:2] assert_array_equal(data, data1) assert_array_equal(times, times1) # test adding / deleting proj if proj: assert_raises(ValueError, raw.add_proj, [], {'remove_existing': True}) assert_raises(ValueError, raw.del_proj, 0) else: projs = deepcopy(raw.info['projs']) n_proj = len(raw.info['projs']) raw.del_proj(0) assert_equal(len(raw.info['projs']), n_proj - 1) raw.add_proj(projs, remove_existing=False) # Test that already existing projections are not added. assert_equal(len(raw.info['projs']), n_proj) raw.add_proj(projs[:-1], remove_existing=True) assert_equal(len(raw.info['projs']), n_proj - 1) # test apply_proj() with and without preload for preload in [True, False]: raw = Raw(fif_fname, preload=preload, proj=False) data, times = raw[:, 0:2] raw.apply_proj() data_proj_1 = np.dot(raw._projector, data) # load the file again without proj raw = Raw(fif_fname, preload=preload, proj=False) # write the file with proj. activated, make sure proj has been applied raw.save(op.join(tempdir, 'raw.fif'), proj=True, overwrite=True) raw2 = Raw(op.join(tempdir, 'raw.fif'), proj=False) data_proj_2, _ = raw2[:, 0:2] assert_allclose(data_proj_1, data_proj_2) assert_true(all(p['active'] for p in raw2.info['projs'])) # read orig file with proj. active raw2 = Raw(fif_fname, preload=preload, proj=True) data_proj_2, _ = raw2[:, 0:2] assert_allclose(data_proj_1, data_proj_2) assert_true(all(p['active'] for p in raw2.info['projs'])) # test that apply_proj works raw.apply_proj() data_proj_2, _ = raw[:, 0:2] assert_allclose(data_proj_1, data_proj_2) assert_allclose(data_proj_2, np.dot(raw._projector, data_proj_2)) tempdir = _TempDir() out_fname = op.join(tempdir, 'test_raw.fif') raw = read_raw_fif(test_fif_fname, preload=True).crop(0, 0.002, copy=False) raw.pick_types(meg=False, eeg=True) raw.info['projs'] = [raw.info['projs'][-1]] raw._data.fill(0) raw._data[-1] = 1. raw.save(out_fname) raw = read_raw_fif(out_fname, proj=True, preload=False) assert_allclose(raw[:, :][0][:1], raw[0, :][0])
def test_multiple_files(): """Test loading multiple files simultaneously """ # split file tempdir = _TempDir() raw = Raw(fif_fname).crop(0, 10, False) raw.load_data() raw.load_data() # test no operation split_size = 3. # in seconds sfreq = raw.info['sfreq'] nsamp = (raw.last_samp - raw.first_samp) tmins = np.round(np.arange(0., nsamp, split_size * sfreq)) tmaxs = np.concatenate((tmins[1:] - 1, [nsamp])) tmaxs /= sfreq tmins /= sfreq assert_equal(raw.n_times, len(raw.times)) # going in reverse order so the last fname is the first file (need later) raws = [None] * len(tmins) for ri in range(len(tmins) - 1, -1, -1): fname = op.join(tempdir, 'test_raw_split-%d_raw.fif' % ri) raw.save(fname, tmin=tmins[ri], tmax=tmaxs[ri]) raws[ri] = Raw(fname) events = [find_events(r, stim_channel='STI 014') for r in raws] last_samps = [r.last_samp for r in raws] first_samps = [r.first_samp for r in raws] # test concatenation of split file assert_raises(ValueError, concatenate_raws, raws, True, events[1:]) all_raw_1, events1 = concatenate_raws(raws, preload=False, events_list=events) assert_equal(raw.first_samp, all_raw_1.first_samp) assert_equal(raw.last_samp, all_raw_1.last_samp) assert_allclose(raw[:, :][0], all_raw_1[:, :][0]) raws[0] = Raw(fname) all_raw_2 = concatenate_raws(raws, preload=True) assert_allclose(raw[:, :][0], all_raw_2[:, :][0]) # test proper event treatment for split files events2 = concatenate_events(events, first_samps, last_samps) events3 = find_events(all_raw_2, stim_channel='STI 014') assert_array_equal(events1, events2) assert_array_equal(events1, events3) # test various methods of combining files raw = Raw(fif_fname, preload=True) n_times = raw.n_times # make sure that all our data match times = list(range(0, 2 * n_times, 999)) # add potentially problematic points times.extend([n_times - 1, n_times, 2 * n_times - 1]) raw_combo0 = Raw([fif_fname, fif_fname], preload=True) _compare_combo(raw, raw_combo0, times, n_times) raw_combo = Raw([fif_fname, fif_fname], preload=False) _compare_combo(raw, raw_combo, times, n_times) raw_combo = Raw([fif_fname, fif_fname], preload='memmap8.dat') _compare_combo(raw, raw_combo, times, n_times) assert_raises(ValueError, Raw, [fif_fname, ctf_fname]) assert_raises(ValueError, Raw, [fif_fname, fif_bad_marked_fname]) assert_equal(raw[:, :][0].shape[1] * 2, raw_combo0[:, :][0].shape[1]) assert_equal(raw_combo0[:, :][0].shape[1], raw_combo0.n_times) # with all data preloaded, result should be preloaded raw_combo = Raw(fif_fname, preload=True) raw_combo.append(Raw(fif_fname, preload=True)) assert_true(raw_combo.preload is True) assert_equal(raw_combo.n_times, raw_combo._data.shape[1]) _compare_combo(raw, raw_combo, times, n_times) # with any data not preloaded, don't set result as preloaded raw_combo = concatenate_raws([Raw(fif_fname, preload=True), Raw(fif_fname, preload=False)]) assert_true(raw_combo.preload is False) assert_array_equal(find_events(raw_combo, stim_channel='STI 014'), find_events(raw_combo0, stim_channel='STI 014')) _compare_combo(raw, raw_combo, times, n_times) # user should be able to force data to be preloaded upon concat raw_combo = concatenate_raws([Raw(fif_fname, preload=False), Raw(fif_fname, preload=True)], preload=True) assert_true(raw_combo.preload is True) _compare_combo(raw, raw_combo, times, n_times) raw_combo = concatenate_raws([Raw(fif_fname, preload=False), Raw(fif_fname, preload=True)], preload='memmap3.dat') _compare_combo(raw, raw_combo, times, n_times) raw_combo = concatenate_raws([Raw(fif_fname, preload=True), Raw(fif_fname, preload=True)], preload='memmap4.dat') _compare_combo(raw, raw_combo, times, n_times) raw_combo = concatenate_raws([Raw(fif_fname, preload=False), Raw(fif_fname, preload=False)], preload='memmap5.dat') _compare_combo(raw, raw_combo, times, n_times) # verify that combining raws with different projectors throws an exception raw.add_proj([], remove_existing=True) assert_raises(ValueError, raw.append, Raw(fif_fname, preload=True)) # now test event treatment for concatenated raw files events = [find_events(raw, stim_channel='STI 014'), find_events(raw, stim_channel='STI 014')] last_samps = [raw.last_samp, raw.last_samp] first_samps = [raw.first_samp, raw.first_samp] events = concatenate_events(events, first_samps, last_samps) events2 = find_events(raw_combo0, stim_channel='STI 014') assert_array_equal(events, events2) # check out the len method assert_equal(len(raw), raw.n_times) assert_equal(len(raw), raw.last_samp - raw.first_samp + 1)
def test_rank(): """Test cov rank estimation""" raw_sample = Raw(raw_fname) raw_sss = Raw(hp_fif_fname) raw_sss.add_proj(compute_proj_raw(raw_sss)) cov_sample = compute_raw_data_covariance(raw_sample) cov_sample_proj = compute_raw_data_covariance( raw_sample.copy().apply_proj()) cov_sss = compute_raw_data_covariance(raw_sss) cov_sss_proj = compute_raw_data_covariance( raw_sss.copy().apply_proj()) picks_all_sample = pick_types(raw_sample.info, meg=True, eeg=True) picks_all_sss = pick_types(raw_sss.info, meg=True, eeg=True) info_sample = pick_info(raw_sample.info, picks_all_sample) picks_stack_sample = [('eeg', pick_types(info_sample, meg=False, eeg=True))] picks_stack_sample += [('meg', pick_types(info_sample, meg=True))] picks_stack_sample += [('all', pick_types(info_sample, meg=True, eeg=True))] info_sss = pick_info(raw_sss.info, picks_all_sss) picks_stack_somato = [('eeg', pick_types(info_sss, meg=False, eeg=True))] picks_stack_somato += [('meg', pick_types(info_sss, meg=True))] picks_stack_somato += [('all', pick_types(info_sss, meg=True, eeg=True))] iter_tests = list(itt.product( [(cov_sample, picks_stack_sample, info_sample), (cov_sample_proj, picks_stack_sample, info_sample), (cov_sss, picks_stack_somato, info_sss), (cov_sss_proj, picks_stack_somato, info_sss)], # sss [dict(mag=1e15, grad=1e13, eeg=1e6)] )) for (cov, picks_list, this_info), scalings in iter_tests: for ch_type, picks in picks_list: this_very_info = pick_info(this_info, picks) # compute subset of projs this_projs = [c['active'] and len(set(c['data']['col_names']) .intersection(set(this_very_info['ch_names']))) > 0 for c in cov['projs']] n_projs = sum(this_projs) # count channel types ch_types = [channel_type(this_very_info, idx) for idx in range(len(picks))] n_eeg, n_mag, n_grad = [ch_types.count(k) for k in ['eeg', 'mag', 'grad']] n_meg = n_mag + n_grad if ch_type in ('all', 'eeg'): n_projs_eeg = 1 else: n_projs_eeg = 0 # check sss if 'proc_history' in this_very_info: mf = this_very_info['proc_history'][0]['max_info'] n_free = _get_sss_rank(mf) if 'mag' not in ch_types and 'grad' not in ch_types: n_free = 0 # - n_projs XXX clarify expected_rank = n_free + n_eeg if n_projs > 0 and ch_type in ('all', 'eeg'): expected_rank -= n_projs_eeg else: expected_rank = n_meg + n_eeg - n_projs C = cov['data'][np.ix_(picks, picks)] est_rank = _estimate_rank_meeg_cov(C, this_very_info, scalings=scalings) assert_equal(expected_rank, est_rank)
def test_rank(): """Test cov rank estimation""" # Test that our rank estimation works properly on a simple case evoked = read_evokeds(ave_fname, condition=0, baseline=(None, 0), proj=False) cov = read_cov(cov_fname) ch_names = [ch for ch in evoked.info['ch_names'] if '053' not in ch and ch.startswith('EEG')] cov = prepare_noise_cov(cov, evoked.info, ch_names, None) assert_equal(cov['eig'][0], 0.) # avg projector should set this to zero assert_true((cov['eig'][1:] > 0).all()) # all else should be > 0 # Now do some more comprehensive tests raw_sample = Raw(raw_fname) raw_sss = Raw(hp_fif_fname) raw_sss.add_proj(compute_proj_raw(raw_sss)) cov_sample = compute_raw_covariance(raw_sample) cov_sample_proj = compute_raw_covariance( raw_sample.copy().apply_proj()) cov_sss = compute_raw_covariance(raw_sss) cov_sss_proj = compute_raw_covariance( raw_sss.copy().apply_proj()) picks_all_sample = pick_types(raw_sample.info, meg=True, eeg=True) picks_all_sss = pick_types(raw_sss.info, meg=True, eeg=True) info_sample = pick_info(raw_sample.info, picks_all_sample) picks_stack_sample = [('eeg', pick_types(info_sample, meg=False, eeg=True))] picks_stack_sample += [('meg', pick_types(info_sample, meg=True))] picks_stack_sample += [('all', pick_types(info_sample, meg=True, eeg=True))] info_sss = pick_info(raw_sss.info, picks_all_sss) picks_stack_somato = [('eeg', pick_types(info_sss, meg=False, eeg=True))] picks_stack_somato += [('meg', pick_types(info_sss, meg=True))] picks_stack_somato += [('all', pick_types(info_sss, meg=True, eeg=True))] iter_tests = list(itt.product( [(cov_sample, picks_stack_sample, info_sample), (cov_sample_proj, picks_stack_sample, info_sample), (cov_sss, picks_stack_somato, info_sss), (cov_sss_proj, picks_stack_somato, info_sss)], # sss [dict(mag=1e15, grad=1e13, eeg=1e6)] )) for (cov, picks_list, this_info), scalings in iter_tests: for ch_type, picks in picks_list: this_very_info = pick_info(this_info, picks) # compute subset of projs this_projs = [c['active'] and len(set(c['data']['col_names']) .intersection(set(this_very_info['ch_names']))) > 0 for c in cov['projs']] n_projs = sum(this_projs) # count channel types ch_types = [channel_type(this_very_info, idx) for idx in range(len(picks))] n_eeg, n_mag, n_grad = [ch_types.count(k) for k in ['eeg', 'mag', 'grad']] n_meg = n_mag + n_grad if ch_type in ('all', 'eeg'): n_projs_eeg = 1 else: n_projs_eeg = 0 # check sss if 'proc_history' in this_very_info: mf = this_very_info['proc_history'][0]['max_info'] n_free = _get_sss_rank(mf) if 'mag' not in ch_types and 'grad' not in ch_types: n_free = 0 # - n_projs XXX clarify expected_rank = n_free + n_eeg if n_projs > 0 and ch_type in ('all', 'eeg'): expected_rank -= n_projs_eeg else: expected_rank = n_meg + n_eeg - n_projs C = cov['data'][np.ix_(picks, picks)] est_rank = _estimate_rank_meeg_cov(C, this_very_info, scalings=scalings) assert_equal(expected_rank, est_rank)
def test_rank(): """Test cov rank estimation""" # Test that our rank estimation works properly on a simple case evoked = read_evokeds(ave_fname, condition=0, baseline=(None, 0), proj=False) cov = read_cov(cov_fname) ch_names = [ ch for ch in evoked.info['ch_names'] if '053' not in ch and ch.startswith('EEG') ] cov = prepare_noise_cov(cov, evoked.info, ch_names, None) assert_equal(cov['eig'][0], 0.) # avg projector should set this to zero assert_true((cov['eig'][1:] > 0).all()) # all else should be > 0 # Now do some more comprehensive tests raw_sample = Raw(raw_fname) raw_sss = Raw(hp_fif_fname) raw_sss.add_proj(compute_proj_raw(raw_sss)) cov_sample = compute_raw_covariance(raw_sample) cov_sample_proj = compute_raw_covariance(raw_sample.copy().apply_proj()) cov_sss = compute_raw_covariance(raw_sss) cov_sss_proj = compute_raw_covariance(raw_sss.copy().apply_proj()) picks_all_sample = pick_types(raw_sample.info, meg=True, eeg=True) picks_all_sss = pick_types(raw_sss.info, meg=True, eeg=True) info_sample = pick_info(raw_sample.info, picks_all_sample) picks_stack_sample = [('eeg', pick_types(info_sample, meg=False, eeg=True))] picks_stack_sample += [('meg', pick_types(info_sample, meg=True))] picks_stack_sample += [('all', pick_types(info_sample, meg=True, eeg=True))] info_sss = pick_info(raw_sss.info, picks_all_sss) picks_stack_somato = [('eeg', pick_types(info_sss, meg=False, eeg=True))] picks_stack_somato += [('meg', pick_types(info_sss, meg=True))] picks_stack_somato += [('all', pick_types(info_sss, meg=True, eeg=True))] iter_tests = list( itt.product( [(cov_sample, picks_stack_sample, info_sample), (cov_sample_proj, picks_stack_sample, info_sample), (cov_sss, picks_stack_somato, info_sss), (cov_sss_proj, picks_stack_somato, info_sss)], # sss [dict(mag=1e15, grad=1e13, eeg=1e6)])) for (cov, picks_list, this_info), scalings in iter_tests: for ch_type, picks in picks_list: this_very_info = pick_info(this_info, picks) # compute subset of projs this_projs = [ c['active'] and len( set(c['data']['col_names']).intersection( set(this_very_info['ch_names']))) > 0 for c in cov['projs'] ] n_projs = sum(this_projs) # count channel types ch_types = [ channel_type(this_very_info, idx) for idx in range(len(picks)) ] n_eeg, n_mag, n_grad = [ ch_types.count(k) for k in ['eeg', 'mag', 'grad'] ] n_meg = n_mag + n_grad if ch_type in ('all', 'eeg'): n_projs_eeg = 1 else: n_projs_eeg = 0 # check sss if 'proc_history' in this_very_info: mf = this_very_info['proc_history'][0]['max_info'] n_free = _get_sss_rank(mf) if 'mag' not in ch_types and 'grad' not in ch_types: n_free = 0 # - n_projs XXX clarify expected_rank = n_free + n_eeg if n_projs > 0 and ch_type in ('all', 'eeg'): expected_rank -= n_projs_eeg else: expected_rank = n_meg + n_eeg - n_projs C = cov['data'][np.ix_(picks, picks)] est_rank = _estimate_rank_meeg_cov(C, this_very_info, scalings=scalings) assert_equal(expected_rank, est_rank)