Skip to content

Commit

Permalink
Update examples/convert_empty_room.py
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Höchenberger <[email protected]>
  • Loading branch information
sappelhoff and hoechenberger committed Dec 11, 2024
1 parent 18602b1 commit 999bd34
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 41 deletions.
27 changes: 11 additions & 16 deletions doc/sphinxext/gen_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
# 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,16 +56,12 @@ 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")

cli_path = op.abspath(
op.join(os.path.dirname(__file__), "..", "..", "mne_bids", "commands")
)
out_dir = Path(__file__).resolve().parent.parent / "generated"
out_dir.mkdir(exist_ok=True)
out_fname = out_dir / "cli.rst.new"
cli_path = Path(__file__).resolve().parent.parent / "mne_bids" / "commands"
fnames = sorted(
[op.basename(fname) for fname in glob.glob(op.join(cli_path, "mne_bids*.py"))]
[Path(fname).name for fname in glob.glob(str(cli_path / "mne_bids*.py"))]
)
iterator = sphinx.util.display.status_iterator(
fnames, "generating MNE-BIDS cli help ... ", length=len(fnames)
Expand All @@ -75,7 +70,7 @@ def generate_cli_rst(app=None):
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
)
Expand Down Expand Up @@ -118,10 +113,10 @@ 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.suffix == ".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)

Expand Down
10 changes: 5 additions & 5 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 @@ -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 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
2 changes: 1 addition & 1 deletion examples/convert_empty_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
data_path = sample.data_path()
raw_fname = data_path / "MEG" / "sample" / "sample_audvis_raw.fif"

bids_root = 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 Down
8 changes: 4 additions & 4 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,14 @@
eegbci.load_data(subject=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")
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 Down
22 changes: 11 additions & 11 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 @@ -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 @@ -334,7 +334,7 @@
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 @@ -463,15 +463,15 @@
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
7 changes: 3 additions & 4 deletions examples/mark_bad_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
# Let's start by importing the required modules and functions, reading the
# "sample" data, and writing it in the BIDS format.

import os.path as op
import shutil

import mne
Expand All @@ -39,8 +38,8 @@
)

data_path = mne.datasets.sample.data_path()
raw_fname = op.join(data_path, "MEG", "sample", "sample_audvis_raw.fif")
events_fname = op.join(data_path, "MEG", "sample", "sample_audvis_raw-eve.fif")
raw_fname = data_path / "MEG" / "sample" / "sample_audvis_raw.fif"
events_fname = data_path / "MEG" / "sample" / "sample_audvis_raw-eve.fif"
event_id = {
"Auditory/Left": 1,
"Auditory/Right": 2,
Expand All @@ -49,7 +48,7 @@
"Smiley": 5,
"Button": 32,
}
bids_root = op.join(data_path, "..", "MNE-sample-data-bids")
bids_root = data_path.parent / "MNE-sample-data-bids"
bids_path = BIDSPath(
subject="01", session="01", task="audiovisual", run="01", root=bids_root
)
Expand Down

0 comments on commit 999bd34

Please sign in to comment.