02 working_with_BIDS _data
envs:
- matplotlib 3.4.3
- pathlib 1.0.1
- mne 0.23.0
- mne-bids 0.8
Working with BIDS data
import matplotlib
import pathlib
import mne
import mne_bids
import warnings
warnings.filterwarnings("ignore")
matplotlib.use("Qt5Agg")
Read the input data we wish to convert to BIDS
sample_data_dir = mne.datasets.sample.data_path()
sample_data_dir = pathlib.Path(sample_data_dir)
raw_path = sample_data_dir / 'MEG' / 'sample' / 'sample_audvis_raw.fif'
raw = mne.io.read_raw(raw_path)
events = mne.find_events(raw)
event_id = {
'Auditory/Left': 1,
'Auditory/Right': 2,
'Visual/Left': 3,
'Visual/Right': 4,
'Smiley': 5,
'Button': 32
}
Write the raw data to BIDS
We need to specify the power line frequency
raw.info['line_freq'] = 60
subject_info = {
'birthday': (1988, 10, 1),
'sex': 2,
'hand': 3
}
raw.info['subject_info'] = subject_info
raw.info
Measurement date December 03, 2002 19:01:10 GMT
Experimenter MEG
Participant
Digitized points 146 points
Good channels 102 magnetometer, 203 gradiometer, and 59 EEG channels
Bad channels MEG 2443, EEG 053
EOG channels EOG 061
ECG channels Not available
Sampling frequency 600.61 Hz
Highpass 0.10 Hz
Lowpass 172.18 Hz
Now,actually write the data
out_path = pathlib.Path('out_data/sample_BIDS')
bids_path = mne_bids.BIDSPath(subject='01',
session='01',
task='audiovisual',
run='01',
root=out_path)
mne_bids.write_raw_bids(raw, bids_path=bids_path, events_data=events,
event_id=event_id,overwrite=True)
BIDSPath(
root: out_data/sample_BIDS
datatype: meg
basename: sub-01_ses-01_task-audiovisual_run-01_meg.fif)
Write MEGIN/Elekta/NeuroMag fine-calibration and crosstalk files
cal_path = sample_data_dir / 'SSS' / 'sss_cal_mgh.dat'
ct_path = sample_data_dir / 'SSS' / 'ct_sparse_mgh.fif'
mne_bids.write_meg_calibration(cal_path, bids_path=bids_path)
mne_bids.write_meg_crosstalk(ct_path, bids_path=bids_path)
Print the created file & dirctory structure
mne_bids.print_dir_tree(out_path)
Generate a data summary
print(mne_bids.make_report(out_path))
Reading BIDS data
Let’s try to read back the data we just stored according to the BIDS specification.
bids_root = pathlib.Path('out_data/sample_BIDS')
bids_path = mne_bids.BIDSPath(subject='01',
session='01',
task='audiovisual',
run='01',
datatype='meg',
root=bids_root)
raw = mne_bids.read_raw_bids(bids_path)
raw.plot();
Events are sorted as annotations-but we convert between the two.
raw.annotations[0]
OrderedDict([(‘onset’, 46.58058882905339),
(‘duration’, 0.0),
(‘description’, ‘Auditory/Right’),
(‘orig_time’,
datetime.datetime(2002, 12, 3, 19, 1, 10, 720100, tzinfo=datetime.timezone.utc))])
events, event_id = mne.events_from_annotations(raw)
mne.viz.plot_events(events, event_id=event_id, sfreq=raw.info['sfreq']);
Fine the MEG fine-calibration and crosstalk files
bids_path.meg_calibration_fpath
WindowsPath(‘out_data/sample_BIDS/sub-01/ses-01/meg/sub-01_ses-01_acq-calibration_meg.dat’)
bids_path.meg_crosstalk_fpath
WindowsPath(‘out_data/sample_BIDS/sub-01/ses-01/meg/sub-01_ses-01_acq-crosstalk_meg.fif’)
摘自B站:https://www.bilibili.com/video/BV1dt4y1k7LJ/?spm_id_from=333.788.recommend_more_video.0
2021/8/22