Skip to content

Commit

Permalink
Enable flake8-use-pathlib in ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
markbandstra committed May 26, 2024
1 parent 5e4c0b2 commit 880eaff
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 120 deletions.
2 changes: 1 addition & 1 deletion becquerel/parsers/cnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def read(filename, verbose=False, cal_kwargs=None):

# read all of the file into memory
file_bytes = []
with open(filename, "rb") as f:
with Path.open(filename, "rb") as f:
byte = f.read(1)
while byte:
byte_int = struct.unpack("1B", byte)
Expand Down
2 changes: 1 addition & 1 deletion becquerel/parsers/iec1455.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def read(filename, verbose=False, cal_kwargs=None):
energy_channel_pairs = []
energy_res_pairs = [] # currently unused
energy_eff_pairs = [] # currently unused
with open(filename) as f:
with Path.open(filename) as f:
record = 1
# loop over lines
for line in f:
Expand Down
2 changes: 1 addition & 1 deletion becquerel/parsers/spc.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def read(filename, verbose=False, cal_kwargs=None):
# initialize a dictionary of spectrum data to populate as we parse
data = {}

with open(filename, "rb") as f:
with Path.open(filename, "rb") as f:
# read the file in chunks of 128 bytes
data_records = []
binary_data = None
Expand Down
2 changes: 1 addition & 1 deletion becquerel/parsers/spe.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def read(filename, verbose=False, cal_kwargs=None):
counts = []
channels = []
cal_coeff = []
with open(filename) as f:
with Path.open(filename) as f:
# read & remove newlines from end of each line
lines = [line.strip() for line in f]
i = 0
Expand Down
18 changes: 10 additions & 8 deletions becquerel/tools/df_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""A simple class for caching a pandas DataFrame."""

import os
from pathlib import Path

import pandas as pd

Expand Down Expand Up @@ -42,9 +42,9 @@ def __init__(self):
"""

if self.path is None:
self.path = os.path.split(__file__)[0]
self.path = Path(__file__).parent
self.check_path()
self.filename = os.path.join(self.path, "__df_cache__" + self.name + ".csv")
self.filename = self.path / ("__df_cache__" + self.name + ".csv")
self.df = None
self.loaded = False

Expand All @@ -55,9 +55,10 @@ def check_path(self):
CacheError: if the path does not exist.
"""

if not os.path.exists(self.path):
self.path = Path(self.path)
if not self.path.exists():
raise CacheError(f"Cache path does not exist: {self.path}")
if not os.path.isdir(self.path):
if not self.path.is_dir():
raise CacheError(f"Cache path is not a directory: {self.path}")

def check_file(self):
Expand All @@ -67,9 +68,10 @@ def check_file(self):
CacheError: if the file does not exist.
"""

if not os.path.exists(self.filename):
self.filename = Path(self.filename)
if not self.filename.exists():
raise CacheError(f"Cache filename does not exist: {self.filename}")
if not os.path.isfile(self.filename):
if not self.filename.is_file():
raise CacheError(f"Cache filename is not a file: {self.filename}")

def write_file(self):
Expand Down Expand Up @@ -113,7 +115,7 @@ def delete_file(self):

self.check_file()
try:
os.remove(self.filename)
self.filename.unlink()
except Exception as exc:
raise CacheError(f"Problem deleting cache file {self.filename}") from exc
try:
Expand Down
18 changes: 9 additions & 9 deletions becquerel/tools/materials.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Load material data for use in attenuation calculations with XCOM."""

import csv
import os
import warnings
from pathlib import Path

import numpy as np

from .materials_compendium import fetch_compendium_data
from .materials_error import MaterialsError, MaterialsWarning
from .materials_nist import fetch_compound_data, fetch_element_data

FILENAME = os.path.join(os.path.split(__file__)[0], "materials.csv")
FILENAME = Path(__file__).parent / "materials.csv"


def _load_and_compile_materials():
Expand Down Expand Up @@ -133,13 +133,13 @@ def _write_materials_csv(materials):
materials : dict
Dictionary of materials.
"""
if os.path.exists(FILENAME):
if FILENAME.exists():
warnings.warn(
f"Materials data CSV already exists at {FILENAME} and will be overwritten",
MaterialsWarning,
)
mat_list = sorted(materials.keys())
with open(FILENAME, "w") as f:
with FILENAME.open("w") as f:
print("%name,formula,density,weight fractions,source", file=f)
for name in mat_list:
line = ""
Expand All @@ -158,10 +158,10 @@ def _read_materials_csv():
materials
Dictionary keyed by material names containing the material data.
"""
if not os.path.exists(FILENAME):
if not FILENAME.exists():
raise MaterialsError(f"Materials data CSV does not exist at {FILENAME}")
materials = {}
with open(FILENAME) as f:
with FILENAME.open() as f:
lines = f.readlines()
for tokens in csv.reader(
lines,
Expand Down Expand Up @@ -218,13 +218,13 @@ def fetch_materials(force=False):
materials
Dictionary keyed by material names containing the material data.
"""
if force or not os.path.exists(FILENAME):
if force or not FILENAME.exists():
materials = force_load_and_write_materials_csv()
materials = _read_materials_csv()
return materials


def remove_materials_csv():
"""Remove materials.csv if it exists."""
if os.path.exists(FILENAME):
os.remove(FILENAME)
if FILENAME.exists():
FILENAME.unlink()
8 changes: 4 additions & 4 deletions becquerel/tools/materials_compendium.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
"""

import json
import os
import warnings
from pathlib import Path

import numpy as np
import pandas as pd

from .materials_error import MaterialsError, MaterialsWarning

FNAME = os.path.join(os.path.split(__file__)[0], "MaterialsCompendium.json")
FNAME = Path(__file__).parent / "MaterialsCompendium.json"


def json_elements_to_weight_fractions(elements):
Expand All @@ -47,7 +47,7 @@ def json_elements_to_atom_fractions(elements):
def fetch_compendium_data():
"""Read material data from the Compendium."""
# read the file
if not os.path.exists(FNAME):
if not FNAME.exists():
warnings.warn(
'Material data from the "Compendium of Material Composition Data for '
'Radiation Transport Modeling" cannot be found. If these data are '
Expand All @@ -58,7 +58,7 @@ def fetch_compendium_data():
)
data = []
else:
with open(FNAME) as f:
with FNAME.open() as f:
data = json.load(f)

# extract relevant data
Expand Down
15 changes: 7 additions & 8 deletions examples/autocal.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from pathlib import Path\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
Expand Down Expand Up @@ -217,7 +217,7 @@
],
"source": [
"counts = []\n",
"filename = os.path.join(os.path.dirname(bq.__file__), \"../tests/samples/sim_spec.spe\")\n",
"filename = Path(bq.__file__).parent / \"../tests/samples/sim_spec.spe\"\n",
"spec = bq.Spectrum.from_file(filename)\n",
"spec = spec.combine_bins(4)\n",
"spec.bin_edges_raw *= 4\n",
Expand Down Expand Up @@ -723,8 +723,9 @@
],
"source": [
"# read raw HPGe data\n",
"filename = os.path.join(\n",
" os.path.dirname(bq.__file__), \"../tests/samples/Mendocino_07-10-13_Acq-10-10-13.Spe\"\n",
"filename = (\n",
" Path(bq.__file__).parent /\n",
" \"../tests/samples/Mendocino_07-10-13_Acq-10-10-13.Spe\"\n",
")\n",
"spec = bq.Spectrum.from_file(filename)\n",
"plot_spec(spec)"
Expand Down Expand Up @@ -897,9 +898,7 @@
],
"source": [
"counts = []\n",
"filename = os.path.join(\n",
" os.path.dirname(bq.__file__), \"../tests/samples/nai_detector.spe\"\n",
")\n",
"filename = Path(bq.__file__).parent / \"../tests/samples/nai_detector.spe\"\n",
"spec = bq.Spectrum.from_file(filename)\n",
"plot_spec(spec)"
]
Expand Down Expand Up @@ -1121,7 +1120,7 @@
],
"source": [
"counts = []\n",
"filename = os.path.join(os.path.dirname(bq.__file__), \"../tests/samples/SGM102432.spe\")\n",
"filename = Path(bq.__file__).parent / \"../tests/samples/SGM102432.spe\"\n",
"spec = bq.Spectrum.from_file(filename)\n",
"plot_spec(spec)"
]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ select = [
"TCH", # flake8-type-checking
"INT", # flake8-gettext
# "ARG", # flake8-unused-arguments
# "PTH", # flake8-use-pathlib
"PTH", # flake8-use-pathlib
# "TD", # flake8-todos
# "FIX", # flake8-fixme
# "ERA", # eradicate
Expand Down
9 changes: 5 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import importlib.util
import site
import sys
from pathlib import Path

from setuptools import find_packages, setup

Expand All @@ -21,16 +22,16 @@
site.ENABLE_USER_SITE = "--user" in sys.argv[1:]

# remove package title from description
with open("README.md") as fh:
with Path.open("README.md") as fh:
README = "\n".join(fh.readlines()[2:])

with open("CONTRIBUTING.md") as fh:
with Path.open("CONTRIBUTING.md") as fh:
CONTRIBUTING = fh.read()

with open("requirements.txt") as fh:
with Path.open("requirements.txt") as fh:
REQUIREMENTS = [_line for _line in fh if _line]

with open("requirements-dev.txt") as fh:
with Path.open("requirements-dev.txt") as fh:
REQUIREMENTS_DEV = [line.strip() for line in fh if not line.startswith("-r")]

# make long description from README and CONTRIBUTING
Expand Down
12 changes: 5 additions & 7 deletions tests/autocal_test.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
"""Test PeakFinder and AutoCalibrator classes."""

import os

import matplotlib.pyplot as plt
import numpy as np
import pytest
from parsers_test import SAMPLES_PATH

import becquerel as bq

# read in spectra
SAMPLES_PATH = os.path.join(os.path.dirname(__file__), "samples")
filename1 = os.path.join(SAMPLES_PATH, "sim_spec.spe")
filename2 = os.path.join(SAMPLES_PATH, "Mendocino_07-10-13_Acq-10-10-13.Spe")
filename3 = os.path.join(SAMPLES_PATH, "nai_detector.spe")
filename4 = os.path.join(SAMPLES_PATH, "SGM102432.spe")
filename1 = SAMPLES_PATH / "sim_spec.spe"
filename2 = SAMPLES_PATH / "Mendocino_07-10-13_Acq-10-10-13.Spe"
filename3 = SAMPLES_PATH / "nai_detector.spe"
filename4 = SAMPLES_PATH / "SGM102432.spe"

spec1 = bq.Spectrum.from_file(filename1)
spec2 = bq.Spectrum.from_file(filename2)
Expand Down
12 changes: 5 additions & 7 deletions tests/calibration_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Test Calibration class."""

import os

import matplotlib.pyplot as plt
import numpy as np
import pytest
Expand Down Expand Up @@ -312,7 +310,7 @@ def make_calibration(name, args):
@pytest.mark.parametrize("name, args", name_args)
def test_calibration(name, args):
"""Test the Calibration class."""
fname = os.path.join(TEST_OUTPUTS, f"calibration__init__{name}.h5")
fname = TEST_OUTPUTS / f"calibration__init__{name}.h5"
# test __init__()
cal = make_calibration(name, args)
# test protections on setting parameters
Expand All @@ -338,7 +336,7 @@ def test_calibration(name, args):
@pytest.mark.parametrize("name, args", name_args)
def test_calibration_set_add_points(name, args):
"""Test Calibration.set_points and add_points methods."""
fname = os.path.join(TEST_OUTPUTS, f"calibration__add_points__{name}.h5")
fname = TEST_OUTPUTS / f"calibration__add_points__{name}.h5"
cal = make_calibration(name, args)
# test set_points
cal.set_points()
Expand Down Expand Up @@ -448,7 +446,7 @@ def test_calibration_fit_from_points(name, args):
plt.xlim(0)
plt.ylim(0)
plt.legend()
plt.savefig(os.path.join(TEST_OUTPUTS, f"calibration__fit__{name}.png"))
plt.savefig(TEST_OUTPUTS / f"calibration__fit__{name}.png")

# Test statistics
assert len(cal1.fit_y) > 0
Expand Down Expand Up @@ -503,7 +501,7 @@ def test_calibration_domain_range():

def test_calibration_inverse():
"""Test calibrations with and without inverse expression."""
fname = os.path.join(TEST_OUTPUTS, "calibration__inverse.h5")
fname = TEST_OUTPUTS / "calibration__inverse.h5"

# cal1 has an explicit inverse expression, cal2 does not
cal1 = Calibration(
Expand Down Expand Up @@ -620,7 +618,7 @@ def test_calibration_interpolation():

def test_calibration_read_failures():
"""Test miscellaneous HDF5 reading failures."""
fname = os.path.join(TEST_OUTPUTS, "calibration__read_failures.h5")
fname = TEST_OUTPUTS / "calibration__read_failures.h5"
cal = Calibration.from_linear([2.0, 3.0])
cal.add_points([0, 1000, 2000], [0, 1000, 2000])

Expand Down
9 changes: 3 additions & 6 deletions tests/fitting_test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import glob
import os
from copy import deepcopy

import lmfit
import numpy as np
import pytest
from parsers_test import SAMPLES_PATH

import becquerel as bq

SAMPLES_PATH = os.path.join(os.path.dirname(__file__), "samples")

# TODO: use these for fitting actual data
SAMPLES = {}
for extension in [".spe", ".spc", ".cnf"]:
filenames = glob.glob(os.path.join(SAMPLES_PATH, "*.*"))
filenames = SAMPLES_PATH.glob("*.*")
filenames_filtered = []
for filename in filenames:
fname, ext = os.path.splitext(filename)
ext = filename.suffix
if ext.lower() == extension:
filenames_filtered.append(filename)
SAMPLES[extension] = filenames_filtered
Expand Down
Loading

0 comments on commit 880eaff

Please sign in to comment.