Skip to content

Commit e4e33c3

Browse files
Jammy2211claude
authored andcommitted
refactor: replace os.path with pathlib
Drop legacy `from os import path` and `os.path.X` usages in favour of `pathlib.Path`. Internal-only refactor; no public API changes. Refs PyAutoLabs/PyAutoFit#1257. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent e29f17c commit e4e33c3

32 files changed

Lines changed: 216 additions & 309 deletions

autoarray/plot/output.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
2-
from os import path
32
import os
3+
from pathlib import Path
44
from typing import Union, List, Optional
55

66
from autoarray.structures.abstract_structure import Structure
@@ -100,7 +100,7 @@ def output_path_from(self, format):
100100
return None
101101

102102
if self.format_folder:
103-
output_path = path.join(self.path, format)
103+
output_path = str(Path(self.path) / format)
104104
else:
105105
output_path = self.path
106106

@@ -156,7 +156,7 @@ def savefig(self, filename: str, output_path: str, format: str):
156156

157157
try:
158158
plt.savefig(
159-
path.join(output_path, f"{filename}.{format}"),
159+
str(Path(output_path) / f"{filename}.{format}"),
160160
bbox_inches=self.bbox_inches,
161161
pad_inches=0.1,
162162
)
@@ -257,9 +257,9 @@ def to_figure_output_mode(self, filename: str):
257257

258258
import sys
259259

260-
script_name = path.split(sys.argv[0])[-1].replace(".py", "")
260+
script_name = Path(sys.argv[0]).name.replace(".py", "")
261261

262-
output_path = path.join(os.getcwd(), "output_mode", script_name)
262+
output_path = str(Path(os.getcwd()) / "output_mode" / script_name)
263263
os.makedirs(output_path, exist_ok=True)
264264

265265
self.savefig(f"{COUNT}_{filename}", output_path, "png")

autoarray/plot/utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
import os
7+
from pathlib import Path
78
from typing import List, Optional, Tuple
89

910
import numpy as np
@@ -310,16 +311,16 @@ def _output_mode_save(fig, filename):
310311

311312
import sys
312313

313-
script_name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
314-
output_path = os.path.join(os.getcwd(), "output_mode", script_name)
314+
script_name = Path(sys.argv[0]).stem
315+
output_path = str(Path(os.getcwd()) / "output_mode" / script_name)
315316
os.makedirs(output_path, exist_ok=True)
316317

317318
count = getattr(_output_mode_save, "_count", -1) + 1
318319
_output_mode_save._count = count
319320

320321
try:
321322
fig.savefig(
322-
os.path.join(output_path, f"{count}_{filename}.png"),
323+
str(Path(output_path) / f"{count}_{filename}.png"),
323324
dpi=150,
324325
bbox_inches="tight",
325326
pad_inches=0.1,
@@ -371,7 +372,7 @@ def subplot_save(fig, output_path, output_filename, output_format=None):
371372
os.makedirs(output_path, exist_ok=True)
372373
try:
373374
fig.savefig(
374-
os.path.join(output_path, f"{output_filename}.{output_format}"),
375+
str(Path(output_path) / f"{output_filename}.{output_format}"),
375376
bbox_inches="tight",
376377
pad_inches=0.1,
377378
)
@@ -552,7 +553,7 @@ def save_figure(
552553
continue
553554
try:
554555
fig.savefig(
555-
os.path.join(path, f"{filename}.{fmt}"),
556+
str(Path(path) / f"{filename}.{fmt}"),
556557
dpi=dpi,
557558
bbox_inches="tight",
558559
pad_inches=0.1,

autoarray/util/dataset_util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import shutil
3+
from pathlib import Path
34

45

56
def should_simulate(dataset_path):
@@ -18,7 +19,7 @@ def should_simulate(dataset_path):
1819
subprocess.run([sys.executable, "scripts/.../simulator.py"], check=True)
1920
"""
2021
if os.environ.get("PYAUTO_SMALL_DATASETS") == "1":
21-
if os.path.exists(dataset_path):
22+
if Path(dataset_path).exists():
2223
shutil.rmtree(dataset_path)
2324

24-
return not os.path.exists(dataset_path)
25+
return not Path(dataset_path).exists()

docs/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
# If extensions (or modules to document with autodoc) are in another directory,
1212
# add these directories to sys.path here. If the directory is relative to the
13-
# documentation root, use os.path.abspath to make it absolute, like shown here.
13+
# documentation root, use Path(...).resolve() to make it absolute, like shown here.
1414
#
15-
# import os
1615
# import sys
17-
# sys.path.insert(0, os.path.abspath('.'))
16+
# from pathlib import Path
17+
# sys.path.insert(0, str(Path(".").resolve()))
1818

1919
# -- Project information -----------------------------------------------------
2020

test_autoarray/conftest.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from os import path
2+
from pathlib import Path
33
import pytest
44
from matplotlib import pyplot
55

@@ -12,7 +12,7 @@ def __init__(self):
1212
self.paths = []
1313

1414
def __call__(self, path, *args, **kwargs):
15-
self.paths.append(path)
15+
self.paths.append(str(path))
1616

1717

1818
@pytest.fixture(name="plot_patch")
@@ -25,7 +25,7 @@ def make_plot_patch(monkeypatch):
2525
return plot_patch
2626

2727

28-
directory = path.dirname(path.realpath(__file__))
28+
directory = Path(__file__).resolve().parent
2929

3030

3131
@pytest.fixture(autouse=True, scope="session")
@@ -34,16 +34,16 @@ def remove_logs():
3434
for d, _, files in os.walk(directory):
3535
for file in files:
3636
if file.endswith(".log"):
37-
os.remove(path.join(d, file))
37+
os.remove(Path(d) / file)
3838

3939

4040
@pytest.fixture(autouse=True)
4141
def set_config_path(request):
4242
# if dirname(realpath(__file__)) in str(request.module):
4343

4444
conf.instance.push(
45-
new_path=path.join(directory, "config"),
46-
output_path=path.join(directory, "output"),
45+
new_path=str(directory / "config"),
46+
output_path=str(directory / "output"),
4747
)
4848

4949

test_autoarray/dataset/imaging/test_dataset.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import copy
22
import os
3-
from os import path
43

54
import numpy as np
65
import pytest
@@ -9,23 +8,16 @@
98
import autoarray as aa
109

1110
from autoarray import exc
11+
from pathlib import Path
1212

13-
test_data_path = path.join(
14-
"{}".format(path.dirname(path.realpath(__file__))),
15-
"files",
16-
)
13+
test_data_path = Path(Path(__file__).resolve().parent) / "files"
1714

1815

1916
@pytest.fixture(name="test_data_path")
2017
def make_test_data_path():
21-
test_data_path = path.join(
22-
"{}".format(os.path.dirname(os.path.realpath(__file__))),
23-
"files",
24-
"array",
25-
"output_test",
26-
)
18+
test_data_path = Path(__file__).resolve().parent / "files" / "array" / "output_test"
2719

28-
if os.path.exists(test_data_path):
20+
if test_data_path.exists():
2921
shutil.rmtree(test_data_path)
3022

3123
os.makedirs(test_data_path)
@@ -132,9 +124,9 @@ def test__no_noise_map__raises_exception():
132124
def test__from_fits__separate_fits_files__loads_data_psf_noise_map_correctly():
133125
dataset = aa.Imaging.from_fits(
134126
pixel_scales=0.1,
135-
data_path=path.join(test_data_path, "3x3_ones.fits"),
136-
psf_path=path.join(test_data_path, "3x3_twos.fits"),
137-
noise_map_path=path.join(test_data_path, "3x3_threes.fits"),
127+
data_path=Path(test_data_path) / "3x3_ones.fits",
128+
psf_path=Path(test_data_path) / "3x3_twos.fits",
129+
noise_map_path=Path(test_data_path) / "3x3_threes.fits",
138130
)
139131

140132
assert (dataset.data.native == np.ones((3, 3))).all()
@@ -151,11 +143,11 @@ def test__from_fits__separate_fits_files__loads_data_psf_noise_map_correctly():
151143
def test__from_fits__all_data_in_one_fits_file_multiple_hdus__loads_data_psf_noise_map_correctly():
152144
dataset = aa.Imaging.from_fits(
153145
pixel_scales=0.1,
154-
data_path=path.join(test_data_path, "3x3_multiple_hdu.fits"),
146+
data_path=Path(test_data_path) / "3x3_multiple_hdu.fits",
155147
data_hdu=0,
156-
psf_path=path.join(test_data_path, "3x3_multiple_hdu.fits"),
148+
psf_path=Path(test_data_path) / "3x3_multiple_hdu.fits",
157149
psf_hdu=1,
158-
noise_map_path=path.join(test_data_path, "3x3_multiple_hdu.fits"),
150+
noise_map_path=Path(test_data_path) / "3x3_multiple_hdu.fits",
159151
noise_map_hdu=2,
160152
)
161153

@@ -178,17 +170,17 @@ def test__output_to_fits__round_trips_data_psf_noise_map_correctly(
178170

179171
fits_imaging(
180172
dataset=imaging_7x7,
181-
data_path=path.join(test_data_path, "data.fits"),
182-
psf_path=path.join(test_data_path, "psf.fits"),
183-
noise_map_path=path.join(test_data_path, "noise_map.fits"),
173+
data_path=Path(test_data_path) / "data.fits",
174+
psf_path=Path(test_data_path) / "psf.fits",
175+
noise_map_path=Path(test_data_path) / "noise_map.fits",
184176
overwrite=True,
185177
)
186178

187179
dataset = aa.Imaging.from_fits(
188180
pixel_scales=0.1,
189-
data_path=path.join(test_data_path, "data.fits"),
190-
psf_path=path.join(test_data_path, "psf.fits"),
191-
noise_map_path=path.join(test_data_path, "noise_map.fits"),
181+
data_path=Path(test_data_path) / "data.fits",
182+
psf_path=Path(test_data_path) / "psf.fits",
183+
noise_map_path=Path(test_data_path) / "noise_map.fits",
192184
)
193185

194186
assert (dataset.data.native == np.ones((7, 7))).all()

test_autoarray/dataset/interferometer/test_dataset.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import numpy as np
22
import os
3-
from os import path
43
import shutil
54

65
import autoarray as aa
76
import pytest
87

98
from autoarray.operators import transformer
9+
from pathlib import Path
1010

11-
test_data_path = path.join(
12-
"{}".format(path.dirname(path.realpath(__file__))),
13-
"files",
14-
)
11+
test_data_path = Path(Path(__file__).resolve().parent) / "files"
1512

1613

1714
def test__dirty_image__shape_native_matches_real_space_mask(
@@ -70,11 +67,11 @@ def test__dirty_signal_to_noise_map__shape_native_matches_real_space_mask(
7067
def test__from_fits__all_files_in_one_fits__load_using_different_hdus(mask_2d_7x7):
7168
dataset = aa.Interferometer.from_fits(
7269
real_space_mask=mask_2d_7x7,
73-
data_path=path.join(test_data_path, "3x2_multiple_hdu.fits"),
70+
data_path=Path(test_data_path) / "3x2_multiple_hdu.fits",
7471
visibilities_hdu=0,
75-
noise_map_path=path.join(test_data_path, "3x2_multiple_hdu.fits"),
72+
noise_map_path=Path(test_data_path) / "3x2_multiple_hdu.fits",
7673
noise_map_hdu=1,
77-
uv_wavelengths_path=path.join(test_data_path, "3x2_multiple_hdu.fits"),
74+
uv_wavelengths_path=Path(test_data_path) / "3x2_multiple_hdu.fits",
7875
uv_wavelengths_hdu=2,
7976
)
8077

@@ -85,26 +82,18 @@ def test__from_fits__all_files_in_one_fits__load_using_different_hdus(mask_2d_7x
8582

8683

8784
def test__output_all_arrays(mask_2d_7x7):
88-
test_data_path = path.join(
89-
"{}".format(path.dirname(path.realpath(__file__))),
90-
"files",
91-
)
85+
test_data_path = Path(Path(__file__).resolve().parent) / "files"
9286

9387
dataset = aa.Interferometer.from_fits(
9488
real_space_mask=mask_2d_7x7,
95-
data_path=path.join(test_data_path, "3x2_ones_twos.fits"),
96-
noise_map_path=path.join(test_data_path, "3x2_threes_fours.fits"),
97-
uv_wavelengths_path=path.join(test_data_path, "3x2_fives_sixes.fits"),
89+
data_path=Path(test_data_path) / "3x2_ones_twos.fits",
90+
noise_map_path=Path(test_data_path) / "3x2_threes_fours.fits",
91+
uv_wavelengths_path=Path(test_data_path) / "3x2_fives_sixes.fits",
9892
)
9993

100-
test_data_path = path.join(
101-
"{}".format(path.dirname(path.realpath(__file__))),
102-
"files",
103-
"array",
104-
"output_test",
105-
)
94+
test_data_path = Path(Path(__file__).resolve().parent) / "files" / "array" / "output_test"
10695

107-
if path.exists(test_data_path):
96+
if Path(test_data_path).exists():
10897
shutil.rmtree(test_data_path)
10998

11099
os.makedirs(test_data_path)
@@ -113,17 +102,17 @@ def test__output_all_arrays(mask_2d_7x7):
113102

114103
fits_interferometer(
115104
dataset=dataset,
116-
data_path=path.join(test_data_path, "data.fits"),
117-
noise_map_path=path.join(test_data_path, "noise_map.fits"),
118-
uv_wavelengths_path=path.join(test_data_path, "uv_wavelengths.fits"),
105+
data_path=Path(test_data_path) / "data.fits",
106+
noise_map_path=Path(test_data_path) / "noise_map.fits",
107+
uv_wavelengths_path=Path(test_data_path) / "uv_wavelengths.fits",
119108
overwrite=True,
120109
)
121110

122111
dataset = aa.Interferometer.from_fits(
123112
real_space_mask=mask_2d_7x7,
124-
data_path=path.join(test_data_path, "data.fits"),
125-
noise_map_path=path.join(test_data_path, "noise_map.fits"),
126-
uv_wavelengths_path=path.join(test_data_path, "uv_wavelengths.fits"),
113+
data_path=Path(test_data_path) / "data.fits",
114+
noise_map_path=Path(test_data_path) / "noise_map.fits",
115+
uv_wavelengths_path=Path(test_data_path) / "uv_wavelengths.fits",
127116
)
128117

129118
assert (dataset.data == np.array([1.0 + 2.0j, 1.0 + 2.0j, 1.0 + 2.0j])).all()

0 commit comments

Comments
 (0)