Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os.path to pathlib #1339

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions examples/convert_eeg_to_bids.py
Original file line number Diff line number Diff line change
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 @@ -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
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
2 changes: 1 addition & 1 deletion examples/convert_group_studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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
6 changes: 3 additions & 3 deletions examples/convert_ieeg_to_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -330,7 +330,7 @@

# 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
Expand Down Expand Up @@ -459,7 +459,7 @@

# 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
Expand Down
2 changes: 1 addition & 1 deletion examples/convert_nirs_to_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,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
2 changes: 1 addition & 1 deletion examples/mark_bad_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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
2 changes: 1 addition & 1 deletion mne_bids/commands/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)

data_path = testing.data_path(download=False)
base_path = op.join(op.dirname(mne.__file__), "io")
base_path = Path(mne.__file__).parent / "io"
subject_id = "01"
task = "testing"
datatype = "meg"
Expand Down
2 changes: 1 addition & 1 deletion mne_bids/tests/test_copyfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from mne_bids.path import _parse_ext

testing_path = testing.data_path(download=False)
base_path = op.join(op.dirname(mne.__file__), "io")
base_path = Path(mne.__file__).parent / "io"


@testing.requires_testing_data
Expand Down
3 changes: 2 additions & 1 deletion mne_bids/tests/test_dig.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import os.path as op
import warnings
from pathlib import Path

import mne
import numpy as np
Expand All @@ -31,7 +32,7 @@
template_to_head,
)

base_path = op.join(op.dirname(mne.__file__), "io")
base_path = Path(mne.__file__).parent / "io"
subject_id = "01"
session_id = "01"
run = "01"
Expand Down
4 changes: 1 addition & 3 deletions mne_bids/tests/test_pick.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# Authors: The MNE-BIDS developers
# SPDX-License-Identifier: BSD-3-Clause

import os.path as op

from mne.datasets import testing
from mne.io import read_raw_fif

Expand All @@ -16,7 +14,7 @@
@testing.requires_testing_data
def test_coil_type():
"""Test the correct coil type is retrieved."""
raw_fname = op.join(data_path, "MEG", "sample", "sample_audvis_trunc_raw.fif")
raw_fname = data_path / "MEG" / "sample" / "sample_audvis_trunc_raw.fif"
raw = read_raw_fif(raw_fname)
assert coil_type(raw.info, 0) == "meggradplanar"
assert coil_type(raw.info, 2) == "megmag"
Expand Down
3 changes: 1 addition & 2 deletions mne_bids/tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Authors: The MNE-BIDS developers
# SPDX-License-Identifier: BSD-3-Clause

import os.path as op
import textwrap

import mne
Expand All @@ -26,7 +25,7 @@

# Get the MNE testing sample data
data_path = testing.data_path(download=False)
raw_fname = op.join(data_path, "MEG", "sample", "sample_audvis_trunc_raw.fif")
raw_fname = data_path / "MEG" / "sample" / "sample_audvis_trunc_raw.fif"

warning_str = dict(
channel_unit_changed="ignore:The unit for chann*.:RuntimeWarning:mne",
Expand Down
2 changes: 1 addition & 1 deletion mne_bids/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
_infer_eeg_placement_scheme,
)

base_path = op.join(op.dirname(mne.__file__), "io")
base_path = Path(mne.__file__).parent / "io"
subject_id = "01"
session_id = "01"
run = "01"
Expand Down
50 changes: 24 additions & 26 deletions mne_bids/tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
)
from mne_bids.write import _get_fid_coords

base_path = op.join(op.dirname(mne.__file__), "io")
base_path = Path(mne.__file__).parent / "io"
subject_id = "01"
subject_id2 = "02"
session_id = "01"
Expand Down Expand Up @@ -482,8 +482,8 @@ def test_create_fif(_bids_validate, tmp_path):
rng = np.random.RandomState(99)
raw = mne.io.RawArray(rng.random((5, n_points)) * 1e-6, info)
raw.info["line_freq"] = 60
raw.save(op.join(out_dir, "test-raw.fif"))
raw = _read_raw_fif(op.join(out_dir, "test-raw.fif"))
raw.save(out_dir / "test-raw.fif")
raw = _read_raw_fif(out_dir / "test-raw.fif")
write_raw_bids(raw, bids_path, verbose=False, overwrite=True)
_bids_validate(bids_root)

Expand All @@ -500,8 +500,8 @@ def test_line_freq(line_freq, _bids_validate, tmp_path):
rng = np.random.RandomState(99)
raw = mne.io.RawArray(rng.random((5, n_points)) * 1e-6, info)

raw.save(op.join(out_dir, "test-raw.fif"))
raw = _read_raw_fif(op.join(out_dir, "test-raw.fif"))
raw.save(out_dir / "test-raw.fif")
raw = _read_raw_fif(out_dir / "test-raw.fif")
raw.info["line_freq"] = line_freq
write_raw_bids(raw, bids_path, verbose=False, overwrite=True)
_bids_validate(bids_root)
Expand Down Expand Up @@ -853,15 +853,13 @@ def test_fif(_bids_validate, tmp_path):
def test_chpi(_bids_validate, tmp_path, fmt):
"""Test writing of cHPI information."""
if fmt == "fif_no_chpi":
fif_raw_fname = op.join(
data_path, "MEG", "sample", "sample_audvis_trunc_raw.fif"
)
fif_raw_fname = data_path / "MEG" / "sample" / "sample_audvis_trunc_raw.fif"
raw = _read_raw_fif(fif_raw_fname)
elif fmt == "fif":
fif_raw_fname = op.join(data_path, "SSS", "test_move_anon_raw.fif")
fif_raw_fname = data_path / "SSS" / "test_move_anon_raw.fif"
raw = _read_raw_fif(fif_raw_fname, allow_maxshield="yes")
elif fmt == "ctf":
ctf_raw_fname = op.join(data_path, "CTF", "testdata_ctf.ds")
ctf_raw_fname = data_path / "CTF" / "testdata_ctf.ds"
raw = _read_raw_ctf(ctf_raw_fname)
elif fmt == "kit":
kit_data_path = op.join(base_path, "kit", "tests", "data")
Expand Down Expand Up @@ -1046,14 +1044,14 @@ def test_fif_exci(tmp_path):
def test_kit(_bids_validate, tmp_path):
"""Test functionality of the write_raw_bids conversion for KIT data."""
bids_root = tmp_path / "bids"
kit_path = op.join(base_path, "kit", "tests", "data")
raw_fname = op.join(kit_path, "test.sqd")
events_fname = op.join(kit_path, "test-eve.txt")
hpi_fname = op.join(kit_path, "test_mrk.sqd")
hpi_pre_fname = op.join(kit_path, "test_mrk_pre.sqd")
hpi_post_fname = op.join(kit_path, "test_mrk_post.sqd")
electrode_fname = op.join(kit_path, "test.elp")
headshape_fname = op.join(kit_path, "test.hsp")
kit_path = base_path / "kit" / "tests" / "data"
raw_fname = kit_path / "test.sqd"
events_fname = kit_path / "test-eve.txt"
hpi_fname = kit_path / "test_mrk.sqd"
hpi_pre_fname = kit_path / "test_mrk_pre.sqd"
hpi_post_fname = kit_path / "test_mrk_post.sqd"
electrode_fname = kit_path / "test.elp"
headshape_fname = kit_path / "test.hsp"
event_id = dict(cond=128)

kit_bids_path = _bids_path.copy().update(
Expand All @@ -1068,7 +1066,7 @@ def test_kit(_bids_validate, tmp_path):
)

_bids_validate(bids_root)
assert op.exists(bids_root / "participants.tsv")
assert (bids_root / "participants.tsv").exists()
read_raw_bids(bids_path=kit_bids_path)

# ensure the marker file is produced in the right place
Expand All @@ -1082,7 +1080,7 @@ def test_kit(_bids_validate, tmp_path):
datatype="meg",
root=bids_root,
)
assert op.exists(marker_fname)
assert marker_fname.exists()

# test anonymize
output_path = _test_anonymize(
Expand All @@ -1098,7 +1096,7 @@ def test_kit(_bids_validate, tmp_path):
assert "STI 014" not in data["name"]

# ensure the marker file is produced in the right place
assert op.exists(marker_fname)
assert marker_fname.exists()

# test attempts at writing invalid event data
event_data = np.loadtxt(events_fname)
Expand Down Expand Up @@ -1154,11 +1152,11 @@ def test_kit(_bids_validate, tmp_path):
)

# check that everything works with MRK markers, and CON files
kit_path = op.join(data_path, "KIT")
raw_fname = op.join(kit_path, "data_berlin.con")
hpi_fname = op.join(kit_path, "MQKIT_125.mrk")
electrode_fname = op.join(kit_path, "MQKIT_125.elp")
headshape_fname = op.join(kit_path, "MQKIT_125.hsp")
kit_path = data_path / "KIT"
raw_fname = kit_path / "data_berlin.con"
hpi_fname = kit_path / "MQKIT_125.mrk"
electrode_fname = kit_path / "MQKIT_125.elp"
headshape_fname = kit_path / "MQKIT_125.hsp"
bids_root = tmp_path / "bids_kit_mrk"
kit_bids_path = _bids_path.copy().update(
acquisition=None, root=bids_root, suffix="meg"
Expand Down
Loading