diff --git a/docs/conf.py b/docs/conf.py index 9666074..79ae4bc 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -3,17 +3,17 @@ See: https://www.sphinx-doc.org/en/master/usage/configuration.html """ -import os import sys from datetime import date +from pathlib import Path from intersphinx_registry import get_intersphinx_mapping import pybv -curdir = os.path.dirname(__file__) -sys.path.append(os.path.abspath(os.path.join(curdir, "..", "pybv"))) -sys.path.append(os.path.abspath(os.path.join(curdir, "sphinxext"))) +curdir = Path(__file__).parent +sys.path.append((curdir / ".." / "pybv").resolve()) +sys.path.append((curdir / ".." / "sphinxext").resolve()) # see: https://sphinx.readthedocs.io/en/1.3/extensions.html extensions = [ diff --git a/pybv/io.py b/pybv/io.py index 46a5f41..d7a5295 100644 --- a/pybv/io.py +++ b/pybv/io.py @@ -5,7 +5,7 @@ import os import shutil import sys -from os import path as op +from pathlib import Path from warnings import warn import numpy as np @@ -63,7 +63,7 @@ def write_brainvision( fname_base : str The base name for the output files. Three files will be created (*.vhdr*, *.vmrk*, *.eeg*), and all will share this base name. - folder_out : str + folder_out : str | pathlib.Path The folder where output files will be saved. Will be created if it does not exist. overwrite : bool @@ -182,6 +182,8 @@ def write_brainvision( """ # input checks + folder_out = Path(folder_out) + if not isinstance(data, np.ndarray): raise ValueError(f"data must be np.ndarray, but found: {type(data)}") @@ -299,13 +301,13 @@ def write_brainvision( ) # create output file names/paths, checking if they already exist - folder_out_created = not op.exists(folder_out) - os.makedirs(folder_out, exist_ok=True) - eeg_fname = op.join(folder_out, fname_base + ".eeg") - vmrk_fname = op.join(folder_out, fname_base + ".vmrk") - vhdr_fname = op.join(folder_out, fname_base + ".vhdr") + folder_out_created = not folder_out.exists() + folder_out.mkdir(parents=True, exist_ok=True) + eeg_fname = folder_out / f"{fname_base}.eeg" + vmrk_fname = folder_out / f"{fname_base}.vmrk" + vhdr_fname = folder_out / f"{fname_base}.vhdr" for fname in (eeg_fname, vmrk_fname, vhdr_fname): - if op.exists(fname) and not overwrite: + if fname.exists() and not overwrite: raise OSError( f"File already exists: {fname}.\n" f"Consider setting overwrite=True." ) @@ -341,7 +343,7 @@ def write_brainvision( else: # else, only remove the files we might have created for fname in (eeg_fname, vmrk_fname, vhdr_fname): - if op.exists(fname): # pragma: no cover + if fname.exists(): # pragma: no cover os.remove(fname) raise @@ -703,8 +705,8 @@ def _write_vhdr_file( print("", file=fout) print("[Common Infos]", file=fout) print("Codepage=UTF-8", file=fout) - print(f"DataFile={op.basename(eeg_fname)}", file=fout) - print(f"MarkerFile={op.basename(vmrk_fname)}", file=fout) + print(f"DataFile={eeg_fname.name}", file=fout) + print(f"MarkerFile={vmrk_fname.name}", file=fout) if format.startswith("binary"): print("DataFormat=BINARY", file=fout) diff --git a/pybv/tests/test_write_brainvision.py b/pybv/tests/test_write_brainvision.py index 3470fba..cd93fc6 100644 --- a/pybv/tests/test_write_brainvision.py +++ b/pybv/tests/test_write_brainvision.py @@ -2,7 +2,6 @@ import itertools import os -import os.path as op import re from datetime import datetime, timezone @@ -705,10 +704,10 @@ def test_cleanup(tmpdir): folder_out=folder_out, fmt="binary_float999", ) - assert not op.exists(folder_out) - assert not op.exists(folder_out / fname + ".eeg") - assert not op.exists(folder_out / fname + ".vmrk") - assert not op.exists(folder_out / fname + ".vhdr") + assert not (folder_out).exists() + assert not (folder_out / fname + ".eeg").exists() + assert not (folder_out / fname + ".vmrk").exists() + assert not (folder_out / fname + ".vhdr").exists() # if folder already existed before erroneous writing, it is not deleted os.makedirs(folder_out) @@ -721,12 +720,12 @@ def test_cleanup(tmpdir): folder_out=folder_out, fmt="binary_float999", ) - assert op.exists(folder_out) + assert folder_out.exists() # but all other (incomplete/erroneous) files are deleted - assert not op.exists(folder_out / fname + ".eeg") - assert not op.exists(folder_out / fname + ".vmrk") - assert not op.exists(folder_out / fname + ".vhdr") + assert not (folder_out / fname + ".eeg").exists() + assert not (folder_out / fname + ".vmrk").exists() + assert not (folder_out / fname + ".vhdr").exists() def test_overwrite(tmpdir):