Skip to content

Commit 999bd34

Browse files
Update examples/convert_empty_room.py
Co-authored-by: Richard Höchenberger <[email protected]>
1 parent 18602b1 commit 999bd34

File tree

6 files changed

+35
-41
lines changed

6 files changed

+35
-41
lines changed

doc/sphinxext/gen_cli.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
# SPDX-License-Identifier: BSD-3-Clause
99

1010
import glob
11-
import os
1211
import shutil
1312
import sys
14-
from os import path as op
13+
from pathlib import Path
1514

1615
import sphinx.util
1716
from mne.utils import hashfunc, run_subprocess
@@ -57,16 +56,12 @@ def setup(app):
5756

5857
def generate_cli_rst(app=None):
5958
"""Generate the command line interface docs."""
60-
out_dir = op.abspath(op.join(op.dirname(__file__), "..", "generated"))
61-
if not op.isdir(out_dir):
62-
os.mkdir(out_dir)
63-
out_fname = op.join(out_dir, "cli.rst.new")
64-
65-
cli_path = op.abspath(
66-
op.join(os.path.dirname(__file__), "..", "..", "mne_bids", "commands")
67-
)
59+
out_dir = Path(__file__).resolve().parent.parent / "generated"
60+
out_dir.mkdir(exist_ok=True)
61+
out_fname = out_dir / "cli.rst.new"
62+
cli_path = Path(__file__).resolve().parent.parent / "mne_bids" / "commands"
6863
fnames = sorted(
69-
[op.basename(fname) for fname in glob.glob(op.join(cli_path, "mne_bids*.py"))]
64+
[Path(fname).name for fname in glob.glob(str(cli_path / "mne_bids*.py"))]
7065
)
7166
iterator = sphinx.util.display.status_iterator(
7267
fnames, "generating MNE-BIDS cli help ... ", length=len(fnames)
@@ -75,7 +70,7 @@ def generate_cli_rst(app=None):
7570
f.write(header)
7671
for fname in iterator:
7772
cmd_name = fname[:-3]
78-
run_name = op.join(cli_path, fname)
73+
run_name = cli_path / fname
7974
output, _ = run_subprocess(
8075
[sys.executable, run_name, "--help"], verbose=False
8176
)
@@ -118,10 +113,10 @@ def generate_cli_rst(app=None):
118113
def _replace_md5(fname):
119114
"""Replace a file based on MD5sum."""
120115
# adapted from sphinx-gallery
121-
assert fname.endswith(".new")
122-
fname_old = fname[:-4]
123-
if os.path.isfile(fname_old) and hashfunc(fname) == hashfunc(fname_old):
124-
os.remove(fname)
116+
assert fname.suffix == ".new"
117+
fname_old = fname.with_suffix("")
118+
if fname_old.is_file() and hashfunc(fname) == hashfunc(fname_old):
119+
fname.unlink()
125120
else:
126121
shutil.move(fname, fname_old)
127122

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_empty_room.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
data_path = sample.data_path()
4343
raw_fname = data_path / "MEG" / "sample" / "sample_audvis_raw.fif"
4444

45-
bids_root = data_path / ".." / "MNE-sample-data-bids"
45+
bids_root = data_path.parent / "MNE-sample-data-bids"
4646

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

examples/convert_group_studies.py

Lines changed: 4 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,14 @@
5353
eegbci.load_data(subject=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+
mne_data_dir = Path(mne.get_config("MNE_DATASETS_EEGBCI_PATH"))
57+
data_dir = mne_data_dir / "MNE-eegbci-data"
5858

5959
# %%
6060
# Let us loop over the subjects and create BIDS-compatible folder
6161

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

6565
# %%
6666
# 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/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
)

0 commit comments

Comments
 (0)