Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions doc/sphinxext/gen_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
# Authors: The MNE-BIDS developers
# SPDX-License-Identifier: BSD-3-Clause

import glob
import os
import shutil
import sys
from os import path as op
from pathlib import Path

import sphinx.util
from mne.utils import hashfunc, run_subprocess
Expand Down Expand Up @@ -57,27 +55,22 @@ def setup(app):

def generate_cli_rst(app=None):
"""Generate the command line interface docs."""
out_dir = op.abspath(op.join(op.dirname(__file__), "..", "generated"))
if not op.isdir(out_dir):
os.mkdir(out_dir)
out_fname = op.join(out_dir, "cli.rst.new")
out_dir = Path(__file__).resolve().parent.parent / "generated"
out_dir.mkdir(exist_ok=True)
out_fname = out_dir / "cli.rst.new"

cli_path = op.abspath(
op.join(os.path.dirname(__file__), "..", "..", "mne_bids", "commands")
)
fnames = sorted(
[op.basename(fname) for fname in glob.glob(op.join(cli_path, "mne_bids*.py"))]
)
cli_path = Path(__file__).resolve().parent.parent.parent / "mne_bids" / "commands"
fnames = sorted([fname.name for fname in cli_path.glob("mne_bids*.py")])
iterator = sphinx.util.display.status_iterator(
fnames, "generating MNE-BIDS cli help ... ", length=len(fnames)
)
with open(out_fname, "w", encoding="utf-8") as f:
with out_fname.open("w", encoding="utf-8") as f:
f.write(header)
for fname in iterator:
cmd_name = fname[:-3]
run_name = op.join(cli_path, fname)
run_name = cli_path / fname
output, _ = run_subprocess(
[sys.executable, run_name, "--help"], verbose=False
[sys.executable, str(run_name), "--help"], verbose=False
)
output = output.splitlines()

Expand Down Expand Up @@ -118,12 +111,12 @@ def generate_cli_rst(app=None):
def _replace_md5(fname):
"""Replace a file based on MD5sum."""
# adapted from sphinx-gallery
assert fname.endswith(".new")
fname_old = fname[:-4]
if os.path.isfile(fname_old) and hashfunc(fname) == hashfunc(fname_old):
os.remove(fname)
assert fname.name.endswith(".new")
fname_old = fname.with_suffix("")
if fname_old.is_file() and hashfunc(fname) == hashfunc(fname_old):
fname.unlink()
else:
shutil.move(fname, fname_old)
shutil.move(str(fname), str(fname_old))


# This is useful for testing/iterating to see what the result looks like
Expand Down
14 changes: 7 additions & 7 deletions examples/convert_eeg_to_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

# %%
# We are importing everything we need for this example:
import os.path as op
import shutil
from pathlib import Path

import mne
from mne.datasets import eegbci
Expand Down Expand Up @@ -66,8 +66,8 @@
# of the directory tree.

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

print_dir_tree(data_dir)

Expand Down Expand Up @@ -114,7 +114,7 @@

# Get the electrode coordinates
testing_data = mne.datasets.testing.data_path()
captrak_path = op.join(testing_data, "montage", "captrak_coords.bvct")
captrak_path = testing_data / "montage" / "captrak_coords.bvct"
montage = mne.channels.read_dig_captrak(captrak_path)

# Rename the montage channel names only for this example, because as said
Expand Down Expand Up @@ -156,7 +156,7 @@

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

# %%
# To ensure the output path doesn't contain any leftover files from previous
Expand All @@ -165,7 +165,7 @@
# .. warning:: Do not delete directories that may contain important data!
#

if op.exists(bids_root):
if bids_root.exists():
shutil.rmtree(bids_root)

# %%
Expand Down Expand Up @@ -218,7 +218,7 @@
#
# If you are preparing a manuscript, please make sure to also cite MNE-BIDS
# there.
readme = op.join(bids_root, "README")
readme = bids_root / "README"
with open(readme, encoding="utf-8-sig") as fid:
text = fid.read()
print(text)
Expand Down
9 changes: 4 additions & 5 deletions examples/convert_empty_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#
# Let us first import mne_bids.

import os.path as op
import shutil
from datetime import datetime, timezone

Expand All @@ -41,9 +40,9 @@
# And define the paths and event_id dictionary.

data_path = sample.data_path()
raw_fname = op.join(data_path, "MEG", "sample", "sample_audvis_raw.fif")
raw_fname = data_path / "MEG" / "sample" / "sample_audvis_raw.fif"

bids_root = op.join(data_path, "..", "MNE-sample-data-bids")
bids_root = data_path.parent / "MNE-sample-data-bids"

# %%
# To ensure the output path doesn't contain any leftover files from previous
Expand All @@ -52,7 +51,7 @@
# .. warning:: Do not delete directories that may contain important data!
#

if op.exists(bids_root):
if bids_root.exists():
shutil.rmtree(bids_root)

# %%
Expand All @@ -68,7 +67,7 @@

# %%
# Specify some empty room data and run BIDS conversion on it.
er_raw_fname = op.join(data_path, "MEG", "sample", "ernoise_raw.fif")
er_raw_fname = data_path / "MEG" / "sample" / "ernoise_raw.fif"
er_raw = mne.io.read_raw_fif(er_raw_fname)
er_raw.info["line_freq"] = 60 # specify power line frequency as req. by BIDS

Expand Down
11 changes: 6 additions & 5 deletions examples/convert_group_studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# %%
# Let us import ``mne_bids``

import os.path as op
import shutil
from pathlib import Path

import mne
from mne.datasets import eegbci
Expand Down Expand Up @@ -53,14 +53,15 @@
eegbci.load_data(subjects=subject_id, runs=runs, update_path=True)

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

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

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

# %%
# To ensure the output path doesn't contain any leftover files from previous
Expand All @@ -69,7 +70,7 @@
# .. warning:: Do not delete directories that may contain important data!
#

if op.exists(bids_root):
if bids_root.exists():
shutil.rmtree(bids_root)

# %%
Expand Down
28 changes: 14 additions & 14 deletions examples/convert_ieeg_to_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@

# %%

import os.path as op
import shutil
from pathlib import Path

import mne
import nibabel as nib
Expand Down Expand Up @@ -77,9 +77,9 @@

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

# %%
# When the locations of the channels in this dataset were found in
Expand Down Expand Up @@ -153,10 +153,10 @@
task = "motor"

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

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

# %%
# To ensure the output path doesn't contain any leftover files from previous
Expand All @@ -165,7 +165,7 @@
# .. warning:: Do not delete directories that may contain important data!
#

if op.exists(bids_root):
if bids_root.exists():
shutil.rmtree(bids_root)

# %%
Expand All @@ -182,7 +182,7 @@
# plot T1 to show that it is ACPC-aligned
# note that the origin is centered on the anterior commissure (AC)
# with the y-axis passing through the posterior commissure (PC)
T1_fname = op.join(subjects_dir, "sample_seeg", "mri", "T1.mgz")
T1_fname = subjects_dir / "sample_seeg" / "mri" / "T1.mgz"
fig = plot_anat(T1_fname, cut_coords=(0, 0, 0))
fig.axes["x"].ax.annotate(
"AC",
Expand Down Expand Up @@ -295,7 +295,7 @@
# We can see that the appropriate citations are already written in the README.
# If you are preparing a manuscript, please make sure to also cite MNE-BIDS
# there.
readme = op.join(bids_root, "README")
readme = bids_root / "README"
with open(readme, encoding="utf-8-sig") as fid:
text = fid.read()
print(text)
Expand Down Expand Up @@ -330,11 +330,11 @@

# ensure the output path doesn't contain any leftover files from previous
# tests and example runs
if op.exists(bids_root):
if bids_root.exists():
shutil.rmtree(bids_root)

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

# get Talairach transform
Expand Down Expand Up @@ -459,19 +459,19 @@

# ensure the output path doesn't contain any leftover files from previous
# tests and example runs
if op.exists(bids_root):
if bids_root.exists():
shutil.rmtree(bids_root)

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

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

raw = mne.io.read_raw_fif(
op.join(misc_path, "seeg", "sample_seeg_ieeg.fif") # load our raw data again
misc_path / "seeg" / "sample_seeg_ieeg.fif" # load our raw data again
)
montage = raw.get_montage() # get the original montage
montage.apply_trans(trans) # head->mri
Expand Down
17 changes: 8 additions & 9 deletions examples/convert_mne_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
# sample data, and then finally the MNE-BIDS functions we need for this example

import json
import os.path as op
import shutil
from pprint import pprint

Expand Down Expand Up @@ -58,10 +57,10 @@
"Button": 32,
}

raw_fname = op.join(data_path, "MEG", "sample", "sample_audvis_raw.fif")
er_fname = op.join(data_path, "MEG", "sample", "ernoise_raw.fif") # empty room
events_fname = op.join(data_path, "MEG", "sample", "sample_audvis_raw-eve.fif")
output_path = op.join(data_path, "..", "MNE-sample-data-bids")
raw_fname = data_path / "MEG" / "sample" / "sample_audvis_raw.fif"
er_fname = data_path / "MEG" / "sample" / "ernoise_raw.fif" # empty room
events_fname = data_path / "MEG" / "sample" / "sample_audvis_raw-eve.fif"
output_path = data_path.parent / "MNE-sample-data-bids"

# %%
# To ensure the output path doesn't contain any leftover files from previous
Expand All @@ -70,7 +69,7 @@
# .. warning:: Do not delete directories that may contain important data!
#

if op.exists(output_path):
if output_path.exists():
shutil.rmtree(output_path)

# %%
Expand Down Expand Up @@ -122,8 +121,8 @@
# are required when processing Elekta/Neuromag/MEGIN data using MaxFilter®.
# Let's store these data in appropriate places, too.

cal_fname = op.join(data_path, "SSS", "sss_cal_mgh.dat")
ct_fname = op.join(data_path, "SSS", "ct_sparse_mgh.fif")
cal_fname = data_path / "SSS" / "sss_cal_mgh.dat"
ct_fname = data_path / "SSS" / "ct_sparse_mgh.fif"

write_meg_calibration(cal_fname, bids_path)
write_meg_crosstalk(ct_fname, bids_path)
Expand Down Expand Up @@ -174,7 +173,7 @@
# The README created by :func:`write_raw_bids` also takes care of the citation
# for mne-bids. If you are preparing a manuscript, please make sure to also
# cite MNE-BIDS there.
readme = op.join(output_path, "README")
readme = output_path / "README"
with open(readme, encoding="utf-8-sig") as fid:
text = fid.read()
print(text)
Expand Down
Loading