Skip to content

Commit ce2ba52

Browse files
committed
more
1 parent 60397bf commit ce2ba52

16 files changed

+81
-95
lines changed

examples/convert_eeg_to_bids.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
# %%
2828
# We are importing everything we need for this example:
29-
import os.path as op
3029
import shutil
30+
from pathlib import Path
3131

3232
import mne
3333
from mne.datasets import eegbci
@@ -66,8 +66,8 @@
6666
# of the directory tree.
6767

6868
# get MNE directory with example data
69-
mne_data_dir = mne.get_config("MNE_DATASETS_EEGBCI_PATH")
70-
data_dir = op.join(mne_data_dir, "MNE-eegbci-data")
69+
mne_data_dir = Path(mne.get_config("MNE_DATASETS_EEGBCI_PATH"))
70+
data_dir = mne_data_dir / "MNE-eegbci-data"
7171

7272
print_dir_tree(data_dir)
7373

@@ -156,7 +156,7 @@
156156

157157
# define a task name and a directory where to save the data to
158158
task = "RestEyesClosed"
159-
bids_root = op.join(mne_data_dir, "eegmmidb_bids_eeg_example")
159+
bids_root = mne_data_dir / "eegmmidb_bids_eeg_example"
160160

161161
# %%
162162
# To ensure the output path doesn't contain any leftover files from previous
@@ -218,7 +218,7 @@
218218
#
219219
# If you are preparing a manuscript, please make sure to also cite MNE-BIDS
220220
# there.
221-
readme = op.join(bids_root, "README")
221+
readme = bids_root / "README"
222222
with open(readme, encoding="utf-8-sig") as fid:
223223
text = fid.read()
224224
print(text)

examples/convert_group_studies.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
# %%
1818
# Let us import ``mne_bids``
1919

20-
import os.path as op
2120
import shutil
21+
from pathlib import Path
2222

2323
import mne
2424
from mne.datasets import eegbci
@@ -53,14 +53,15 @@
5353
eegbci.load_data(subjects=subject_id, runs=runs, update_path=True)
5454

5555
# get path to MNE directory with the downloaded example data
56-
mne_data_dir = mne.get_config("MNE_DATASETS_EEGBCI_PATH")
57-
data_dir = op.join(mne_data_dir, "MNE-eegbci-data")
56+
data_dir = eegbci.data_path
57+
mne_data_dir = Path(mne.get_config("MNE_DATASETS_EEGBCI_PATH"))
58+
data_dir = mne_data_dir / "MNE-eegbci-data"
5859

5960
# %%
6061
# Let us loop over the subjects and create BIDS-compatible folder
6162

6263
# Make a path where we can save the data to
63-
bids_root = op.join(mne_data_dir, "eegmmidb_bids_group_conversion")
64+
bids_root = mne_data_dir / "eegmmidb_bids_group_conversion"
6465

6566
# %%
6667
# To ensure the output path doesn't contain any leftover files from previous

examples/convert_ieeg_to_bids.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545

4646
# %%
4747

48-
import os.path as op
4948
import shutil
49+
from pathlib import Path
5050

5151
import mne
5252
import nibabel as nib
@@ -77,9 +77,9 @@
7777

7878
# The electrode coords data are in the tsv file format
7979
# which is easily read in using numpy
80-
raw = mne.io.read_raw_fif(op.join(misc_path, "seeg", "sample_seeg_ieeg.fif"))
80+
raw = mne.io.read_raw_fif(misc_path / "seeg" / "sample_seeg_ieeg.fif")
8181
raw.info["line_freq"] = 60 # specify power line frequency as required by BIDS
82-
subjects_dir = op.join(misc_path, "seeg") # Freesurfer recon-all directory
82+
subjects_dir = misc_path / "seeg" # Freesurfer recon-all directory
8383

8484
# %%
8585
# When the locations of the channels in this dataset were found in
@@ -153,10 +153,10 @@
153153
task = "motor"
154154

155155
# get MNE-Python directory w/ example data
156-
mne_data_dir = mne.get_config("MNE_DATASETS_MISC_PATH")
156+
mne_data_dir = Path(mne.get_config("MNE_DATASETS_MISC_PATH"))
157157

158158
# There is the root directory for where we will write our data.
159-
bids_root = op.join(mne_data_dir, "ieeg_bids")
159+
bids_root = mne_data_dir / "ieeg_bids"
160160

161161
# %%
162162
# To ensure the output path doesn't contain any leftover files from previous
@@ -182,7 +182,7 @@
182182
# plot T1 to show that it is ACPC-aligned
183183
# note that the origin is centered on the anterior commissure (AC)
184184
# with the y-axis passing through the posterior commissure (PC)
185-
T1_fname = op.join(subjects_dir, "sample_seeg", "mri", "T1.mgz")
185+
T1_fname = subjects_dir / "sample_seeg" / "mri" / "T1.mgz"
186186
fig = plot_anat(T1_fname, cut_coords=(0, 0, 0))
187187
fig.axes["x"].ax.annotate(
188188
"AC",
@@ -295,7 +295,7 @@
295295
# We can see that the appropriate citations are already written in the README.
296296
# If you are preparing a manuscript, please make sure to also cite MNE-BIDS
297297
# there.
298-
readme = op.join(bids_root, "README")
298+
readme = bids_root / "README"
299299
with open(readme, encoding="utf-8-sig") as fid:
300300
text = fid.read()
301301
print(text)
@@ -334,7 +334,7 @@
334334
shutil.rmtree(bids_root)
335335

336336
# load our raw data again
337-
raw = mne.io.read_raw_fif(op.join(misc_path, "seeg", "sample_seeg_ieeg.fif"))
337+
raw = mne.io.read_raw_fif(misc_path / "seeg" / "sample_seeg_ieeg.fif")
338338
raw.info["line_freq"] = 60 # specify power line frequency as required by BIDS
339339

340340
# get Talairach transform
@@ -463,15 +463,15 @@
463463
shutil.rmtree(bids_root)
464464

465465
# get a template mgz image to transform the montage to voxel coordinates
466-
subjects_dir = op.join(mne.datasets.sample.data_path(), "subjects")
467-
template_T1 = nib.load(op.join(subjects_dir, "fsaverage", "mri", "T1.mgz"))
466+
subjects_dir = mne.datasets.sample.data_path() / "subjects"
467+
template_T1 = nib.load(subjects_dir / "fsaverage" / "mri" / "T1.mgz")
468468

469469
# get voxels to surface RAS and scanner RAS transforms
470470
vox_mri_t = template_T1.header.get_vox2ras_tkr() # surface RAS
471471
vox_ras_t = template_T1.header.get_vox2ras() # scanner RAS
472472

473473
raw = mne.io.read_raw_fif(
474-
op.join(misc_path, "seeg", "sample_seeg_ieeg.fif") # load our raw data again
474+
misc_path / "seeg" / "sample_seeg_ieeg.fif" # load our raw data again
475475
)
476476
montage = raw.get_montage() # get the original montage
477477
montage.apply_trans(trans) # head->mri

examples/convert_nirs_to_bids.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222

2323
# %%
2424
# We are importing everything we need for this example:
25-
import os.path as op
26-
import pathlib
2725
import shutil
26+
from pathlib import Path
2827

2928
import mne
3029
from mne_nirs import datasets # For convenient downloading of example data
@@ -42,7 +41,7 @@
4241
# We will use the MNE-NIRS package which includes convenient functions to
4342
# download openly available datasets.
4443

45-
data_dir = pathlib.Path(datasets.fnirs_motor_group.data_path())
44+
data_dir = Path(datasets.fnirs_motor_group.data_path())
4645

4746
# Let's see whether the data has been downloaded using a quick visualization
4847
# of the directory tree.
@@ -169,7 +168,7 @@
169168
#
170169
# If you are preparing a manuscript, please make sure to also cite MNE-BIDS
171170
# there.
172-
readme = op.join(bids_root, "README")
171+
readme = bids_root / "README"
173172
with open(readme, encoding="utf-8-sig") as fid:
174173
text = fid.read()
175174
print(text)

examples/mark_bad_channels.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
# Let's start by importing the required modules and functions, reading the
2626
# "sample" data, and writing it in the BIDS format.
2727

28-
import os.path as op
2928
import shutil
3029

3130
import mne
@@ -39,8 +38,8 @@
3938
)
4039

4140
data_path = mne.datasets.sample.data_path()
42-
raw_fname = op.join(data_path, "MEG", "sample", "sample_audvis_raw.fif")
43-
events_fname = op.join(data_path, "MEG", "sample", "sample_audvis_raw-eve.fif")
41+
raw_fname = data_path / "MEG" / "sample" / "sample_audvis_raw.fif"
42+
events_fname = data_path / "MEG" / "sample" / "sample_audvis_raw-eve.fif"
4443
event_id = {
4544
"Auditory/Left": 1,
4645
"Auditory/Right": 2,
@@ -49,7 +48,7 @@
4948
"Smiley": 5,
5049
"Button": 32,
5150
}
52-
bids_root = op.join(data_path, "..", "MNE-sample-data-bids")
51+
bids_root = data_path.parent / "MNE-sample-data-bids"
5352
bids_path = BIDSPath(
5453
subject="01", session="01", task="audiovisual", run="01", root=bids_root
5554
)

examples/read_bids_datasets.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
# Imports
3232
# -------
3333
# We are importing everything we need for this example:
34-
import os
35-
import os.path as op
3634

3735
import openneuro
3836
from mne.datasets import sample
@@ -65,9 +63,8 @@
6563
subject = "pd6"
6664

6765
# Download one subject's data from each dataset
68-
bids_root = op.join(op.dirname(sample.data_path()), dataset)
69-
if not op.isdir(bids_root):
70-
os.makedirs(bids_root)
66+
bids_root = sample.data_path().parent / dataset
67+
bids_root.mkdir(parents=True, exist_ok=True)
7168

7269
openneuro.download(dataset=dataset, target_dir=bids_root, include=[f"sub-{subject}"])
7370

examples/rename_brainvision_files.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434

3535
# %%
3636
# We are importing everything we need for this example:
37-
import os.path as op
38-
3937
import mne
4038

4139
from mne_bids.copyfiles import copyfile_brainvision
@@ -50,7 +48,7 @@
5048
# .. warning:: This will download 1.6 GB of data!
5149

5250
data_path = mne.datasets.testing.data_path()
53-
examples_dir = op.join(data_path, "Brainvision")
51+
examples_dir = data_path / "Brainvision"
5452

5553
# %%
5654
# Rename the recording
@@ -72,8 +70,8 @@
7270
# Here, we rename a test file name:
7371

7472
# Rename the file
75-
vhdr_file = op.join(examples_dir, "Analyzer_nV_Export.vhdr")
76-
vhdr_file_renamed = op.join(examples_dir, "test_renamed.vhdr")
73+
vhdr_file = examples_dir / "Analyzer_nV_Export.vhdr"
74+
vhdr_file_renamed = examples_dir / "test_renamed.vhdr"
7775
copyfile_brainvision(vhdr_file, vhdr_file_renamed, verbose=True)
7876

7977
# Check that MNE-Python can read in both, the original as well as the renamed

mne_bids/dig.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# SPDX-License-Identifier: BSD-3-Clause
55

66
import json
7-
import os.path as op
87
import re
98
import warnings
109
from collections import OrderedDict
@@ -722,8 +721,8 @@ def convert_montage_to_ras(montage, subject, subjects_dir=None, verbose=None):
722721
nib = _import_nibabel("converting a montage to RAS")
723722

724723
subjects_dir = get_subjects_dir(subjects_dir, raise_error=True)
725-
T1_fname = op.join(subjects_dir, subject, "mri", "T1.mgz")
726-
if not op.isfile(T1_fname):
724+
T1_fname = subjects_dir / subject / "mri" / "T1.mgz"
725+
if not T1_fname.is_file():
727726
raise RuntimeError(
728727
f"Freesurfer subject ({subject}) and/or "
729728
f"subjects_dir ({subjects_dir}, incorrectly "
@@ -767,8 +766,8 @@ def convert_montage_to_mri(montage, subject, subjects_dir=None, verbose=None):
767766
nib = _import_nibabel("converting a montage to MRI")
768767

769768
subjects_dir = get_subjects_dir(subjects_dir, raise_error=True)
770-
T1_fname = op.join(subjects_dir, subject, "mri", "T1.mgz")
771-
if not op.isfile(T1_fname):
769+
T1_fname = subjects_dir / subject / "mri" / "T1.mgz"
770+
if not T1_fname.is_file():
772771
raise RuntimeError(
773772
f"Freesurfer subject ({subject}) and/or "
774773
f"subjects_dir ({subjects_dir}, incorrectly "

mne_bids/read.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import json
77
import os
8-
import os.path as op
98
import re
109
from datetime import datetime, timedelta, timezone
1110
from difflib import get_close_matches
@@ -985,7 +984,7 @@ def read_raw_bids(
985984
# read in associated subject info from participants.tsv
986985
participants_tsv_path = bids_root / "participants.tsv"
987986
subject = f"sub-{bids_path.subject}"
988-
if op.exists(participants_tsv_path):
987+
if participants_tsv_path.exists():
989988
raw = _handle_participants_reading(
990989
participants_fname=participants_tsv_path, raw=raw, subject=subject
991990
)
@@ -1180,7 +1179,7 @@ def get_head_mri_trans(
11801179
fs_subject = f"sub-{meg_bids_path.subject}"
11811180

11821181
fs_subjects_dir = get_subjects_dir(fs_subjects_dir, raise_error=False)
1183-
fs_t1_path = Path(fs_subjects_dir) / fs_subject / "mri" / "T1.mgz"
1182+
fs_t1_path = fs_subjects_dir / fs_subject / "mri" / "T1.mgz"
11841183
if not fs_t1_path.exists():
11851184
raise ValueError(
11861185
f"Could not find {fs_t1_path}. Consider running FreeSurfer's "

mne_bids/report/_report.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# SPDX-License-Identifier: BSD-3-Clause
55

66
import json
7-
import os.path as op
87
import textwrap
98
from pathlib import Path
109

@@ -151,8 +150,8 @@ def _summarize_dataset(root):
151150
template_dict : dict
152151
A dictionary of values for various template strings.
153152
"""
154-
dataset_descrip_fpath = op.join(root, "dataset_description.json")
155-
if not op.exists(dataset_descrip_fpath):
153+
dataset_descrip_fpath = root / "dataset_description.json"
154+
if not dataset_descrip_fpath.exists():
156155
return dict()
157156

158157
# read file and 'REQUIRED' components of it
@@ -186,8 +185,8 @@ def _summarize_participants_tsv(root):
186185
template_dict : dict
187186
A dictionary of values for various template strings.
188187
"""
189-
participants_tsv_fpath = op.join(root, "participants.tsv")
190-
if not op.exists(participants_tsv_fpath):
188+
participants_tsv_fpath = root / "participants.tsv"
189+
if not participants_tsv_fpath.exists():
191190
return dict()
192191

193192
participants_tsv = _from_tsv(str(participants_tsv_fpath))
@@ -312,7 +311,7 @@ def _summarize_sidecar_json(root, scans_fpaths):
312311
for scan in scans:
313312
# summarize metadata of recordings
314313
bids_path, ext = _parse_ext(scan)
315-
datatype = op.dirname(scan)
314+
datatype = str(Path(scan).parent)
316315
if datatype not in ALLOWED_DATATYPES:
317316
continue
318317

@@ -400,7 +399,7 @@ def _summarize_channels_tsv(root, scans_fpaths):
400399
for scan in scans:
401400
# summarize metadata of recordings
402401
bids_path, _ = _parse_ext(scan)
403-
datatype = op.dirname(scan)
402+
datatype = str(Path(scan).parent)
404403
if datatype not in ["meg", "eeg", "ieeg"]:
405404
continue
406405

@@ -465,6 +464,8 @@ def make_report(root, session=None, verbose=None):
465464
The paragraph wrapped with 80 characters per line
466465
describing the summary of the subjects.
467466
"""
467+
root = Path(root)
468+
468469
# high level summary
469470
subjects = get_entity_vals(root, entity_key="subject")
470471
sessions = get_entity_vals(root, entity_key="session")

0 commit comments

Comments
 (0)