Skip to content

Commit

Permalink
ditch os.path
Browse files Browse the repository at this point in the history
  • Loading branch information
sappelhoff committed Nov 23, 2024
1 parent 89a2fed commit 3af1315
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
8 changes: 4 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
24 changes: 13 additions & 11 deletions pybv/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)}")

Expand Down Expand Up @@ -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."
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 8 additions & 9 deletions pybv/tests/test_write_brainvision.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import itertools
import os
import os.path as op
import re
from datetime import datetime, timezone

Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit 3af1315

Please sign in to comment.