From 8c19518b4bd5d32f2f0dcf3d4cdf2c401bd626e9 Mon Sep 17 00:00:00 2001 From: Remi Gau Date: Sun, 10 Nov 2024 11:26:26 +0100 Subject: [PATCH] fix PATH error flagged by ruff --- eye2bids/_base.py | 10 +++++++--- eye2bids/edf2bids.py | 6 +++--- pyproject.toml | 1 - tests/test_edf2bids.py | 12 ++++++------ tools/download_test_data.py | 2 +- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/eye2bids/_base.py b/eye2bids/_base.py index 6327346..8f82447 100644 --- a/eye2bids/_base.py +++ b/eye2bids/_base.py @@ -68,7 +68,9 @@ def write( ) -> None: """Write to json.""" content = {key: value for key, value in self.items() if self[key] is not None} - with open(output_dir / self.output_filename(recording=recording), "w") as outfile: + with (output_dir / self.output_filename(recording=recording)).open( + "w" + ) as outfile: json.dump(content, outfile, indent=4) @@ -110,7 +112,7 @@ def write( self[key] = value content = {key: value for key, value in self.items() if self[key] is not None} - with open(output_dir / self.output_filename(), "w") as outfile: + with (output_dir / self.output_filename()).open("w") as outfile: json.dump(content, outfile, indent=4) @@ -196,5 +198,7 @@ def write( self[key] = value content = {key: value for key, value in self.items() if self[key] is not None} - with open(output_dir / self.output_filename(recording=recording), "w") as outfile: + with (output_dir / self.output_filename(recording=recording)).open( + "w" + ) as outfile: json.dump(content, outfile, indent=4) diff --git a/eye2bids/edf2bids.py b/eye2bids/edf2bids.py index 47d0e86..1fc3710 100644 --- a/eye2bids/edf2bids.py +++ b/eye2bids/edf2bids.py @@ -312,7 +312,7 @@ def _extract_StopTime(events: list[str]) -> int: def _load_asc_file(events_asc_file: str | Path) -> list[str]: - with open(events_asc_file) as f: + with Path(events_asc_file).open() as f: return f.readlines() @@ -424,7 +424,7 @@ def generate_physio_json( if metadata_file is None: metadata = {} else: - with open(metadata_file) as f: + with Path(metadata_file).open() as f: metadata = yaml.load(f, Loader=SafeLoader) events = _load_asc_file(events_asc_file) @@ -543,7 +543,7 @@ def edf2bids( if metadata_file is None: metadata = {} else: - with open(metadata_file) as f: + with metadata_file.open() as f: metadata = yaml.load(f, Loader=SafeLoader) events_json = BaseEventsJson(metadata) diff --git a/pyproject.toml b/pyproject.toml index b9bf406..9246d18 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -133,7 +133,6 @@ skip-magic-trailing-comma = false dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" fixable = ["ALL"] ignore = [ - "PTH123", "PD901", "N802", "D205", diff --git a/tests/test_edf2bids.py b/tests/test_edf2bids.py index 6bddb1e..850f1ef 100644 --- a/tests/test_edf2bids.py +++ b/tests/test_edf2bids.py @@ -69,7 +69,7 @@ def _check_output_content(output_dir, input_file, eye=1): ).with_suffix(".json") df = pd.read_csv(tsv_file, sep="\t", header=None) - with open(json_file) as f: + with json_file.open() as f: metadata = json.load(f) assert len(df.columns) == len(metadata["Columns"]) @@ -141,12 +141,12 @@ def test_edf_end_to_end(eyelink_test_data_dir): _check_output_exists(output_dir, input_file) expected_events_sidecar = output_dir / f"{input_file.stem}_events.json" - with open(expected_events_sidecar) as f: + with expected_events_sidecar.open() as f: events = json.load(f) assert events["StimulusPresentation"]["ScreenResolution"] == [1919, 1079] expected_data_sidecar = output_dir / f"{input_file.stem}_recording-eye1_physio.json" - with open(expected_data_sidecar) as f: + with expected_data_sidecar.open() as f: eyetrack = json.load(f) assert eyetrack["SamplingFrequency"] == 500 assert eyetrack["RecordedEye"] == "Right" @@ -189,14 +189,14 @@ def test_edf_end_to_end_2eyes(eyelink_test_data_dir): _check_output_content(output_dir, input_file) expected_events_sidecar_eye1 = output_dir / f"{input_file.stem}_events.json" - with open(expected_events_sidecar_eye1) as f: + with expected_events_sidecar_eye1.open() as f: events = json.load(f) assert events["StimulusPresentation"]["ScreenResolution"] == [1919, 1079] expected_data_sidecar_eye1 = ( output_dir / f"{input_file.stem}_recording-eye1_physio.json" ) - with open(expected_data_sidecar_eye1) as f: + with expected_data_sidecar_eye1.open() as f: eyetrack = json.load(f) assert eyetrack["SamplingFrequency"] == 1000 assert eyetrack["AverageCalibrationError"] == [[0.29]] @@ -208,7 +208,7 @@ def test_edf_end_to_end_2eyes(eyelink_test_data_dir): expected_data_sidecar_eye2 = ( output_dir / f"{input_file.stem}_recording-eye2_physio.json" ) - with open(expected_data_sidecar_eye2) as f: + with expected_data_sidecar_eye2.open() as f: eyetrack = json.load(f) assert eyetrack["AverageCalibrationError"] == [[0.35]] assert eyetrack["RecordedEye"] == "Right" diff --git a/tools/download_test_data.py b/tools/download_test_data.py index 089e709..3a5a445 100644 --- a/tools/download_test_data.py +++ b/tools/download_test_data.py @@ -25,7 +25,7 @@ # Download the zip file r = requests.get(download_url, stream=True) -with open(output_file, "wb") as f: +with output_file.open("wb") as f: for chunk in r.iter_content(chunk_size=1024): if chunk: f.write(chunk)