Skip to content

Commit 7c019c8

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 d32c696 commit 7c019c8

29 files changed

Lines changed: 149 additions & 195 deletions

autolens/analysis/positions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"""
1919
import numpy as np
2020
from typing import Optional
21-
from os import path
21+
from pathlib import Path
2222

2323
import autoarray as aa
2424
import autofit as af
@@ -124,7 +124,7 @@ def output_positions_info(
124124

125125
flag = "w+" if overwrite_file else "a+"
126126

127-
with open_(path.join(output_path, "positions.info"), flag) as f:
127+
with open_(Path(output_path) / "positions.info", flag) as f:
128128

129129
positions_fit = SourceMaxSeparation(
130130
data=self.positions,

docs/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
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
#
1515

16-
import os
1716
import sys
17+
from pathlib import Path
1818

19-
sys.path.insert(0, os.path.abspath("."))
19+
sys.path.insert(0, str(Path(".").resolve()))
2020

2121
import autolens
2222

test_autolens/analysis/analysis/test_analysis_dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from os import path
1+
from pathlib import Path
22
import os
33
import pytest
44

@@ -9,7 +9,7 @@
99
import autolens as al
1010
from autolens import exc
1111

12-
directory = path.dirname(path.realpath(__file__))
12+
directory = Path(__file__).resolve().parent
1313

1414

1515
def test__modify_before_fit__inversion_no_positions_likelihood__raises_exception(

test_autolens/analysis/analysis/test_analysis_lens.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from os import path
1+
from pathlib import Path
22
import pytest
33

44
import autofit as af
55
import autolens as al
66

7-
directory = path.dirname(path.realpath(__file__))
7+
directory = Path(__file__).resolve().parent
88

99

1010
def test__tracer_for_instance(analysis_imaging_7x7):

test_autolens/analysis/test_analysis.py

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

@@ -10,7 +10,7 @@
1010
import autolens as al
1111
from autolens import exc
1212

13-
directory = path.dirname(path.realpath(__file__))
13+
directory = Path(__file__).resolve().parent
1414

1515

1616
def test__tracer_for_instance(analysis_imaging_7x7):
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import os
21
import shutil
3-
from os import path
2+
from pathlib import Path
43

54
import pytest
65
import autolens as al
76
from autolens.analysis import plotter as vis
87

9-
directory = path.dirname(path.realpath(__file__))
8+
directory = Path(__file__).resolve().parent
109

1110

1211
@pytest.fixture(name="plot_path")
1312
def make_plotter_plotter_setup():
14-
return path.join("{}".format(directory), "files")
13+
return directory / "files"
1514

1615

1716
def test__tracer(masked_imaging_7x7, tracer_x2_plane_7x7, plot_path, plot_patch):
18-
if os.path.exists(plot_path):
17+
if plot_path.exists():
1918
shutil.rmtree(plot_path)
2019

2120
plotter = vis.Plotter(image_path=plot_path)
@@ -25,21 +24,21 @@ def test__tracer(masked_imaging_7x7, tracer_x2_plane_7x7, plot_path, plot_patch)
2524
grid=masked_imaging_7x7.grids.lp,
2625
)
2726

28-
assert path.join(plot_path, "galaxies_images.png") in plot_patch.paths
27+
assert str(plot_path / "galaxies_images.png") in plot_patch.paths
2928

3029
image = al.ndarray_via_fits_from(
31-
file_path=path.join(plot_path, "tracer.fits"), hdu=0
30+
file_path=plot_path / "tracer.fits", hdu=0
3231
)
3332

3433
assert image.shape == (5, 5)
3534

3635

3736
def test__image_with_positions(image_7x7, positions_x2, plot_path, plot_patch):
38-
if os.path.exists(plot_path):
37+
if plot_path.exists():
3938
shutil.rmtree(plot_path)
4039

4140
plotter = vis.Plotter(image_path=plot_path)
4241

4342
plotter.image_with_positions(image=image_7x7, positions=positions_x2)
4443

45-
assert path.join(plot_path, "image_with_positions.png") in plot_patch.paths
44+
assert str(plot_path / "image_with_positions.png") in plot_patch.paths

test_autolens/analysis/test_positions.py

Lines changed: 3 additions & 5 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

55
from autoconf.dictable import output_to_json, from_json, from_dict
@@ -21,9 +21,7 @@ def test__check_positions_on_instantiation():
2121

2222

2323
def test__output_positions_info():
24-
output_path = path.join(
25-
"{}".format(os.path.dirname(os.path.realpath(__file__))), "files"
26-
)
24+
output_path = Path(__file__).resolve().parent / "files"
2725

2826
positions_likelihood = al.PositionsLH(
2927
positions=al.Grid2DIrregular([(1.0, 2.0), (3.0, 4.0)]), threshold=0.1
@@ -35,7 +33,7 @@ def test__output_positions_info():
3533

3634
positions_likelihood.output_positions_info(output_path=output_path, tracer=tracer)
3735

38-
positions_file = path.join(output_path, "positions.info")
36+
positions_file = output_path / "positions.info"
3937

4038
with open_(positions_file, "r") as f:
4139
output = f.readlines()

test_autolens/analysis/test_result.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os
1+
from pathlib import Path
22
import numpy as np
33
import pytest
44

@@ -9,7 +9,7 @@
99
from autolens.analysis import result as res
1010
from autolens.imaging.model.result import ResultImaging
1111

12-
directory = os.path.dirname(os.path.realpath(__file__))
12+
directory = Path(__file__).resolve().parent
1313

1414

1515
def test__max_log_likelihood_tracer(

test_autolens/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
from unittest.mock import MagicMock
44

55
import pytest
@@ -21,7 +21,7 @@ def __init__(self):
2121
self.paths = []
2222

2323
def __call__(self, path, *args, **kwargs):
24-
self.paths.append(path)
24+
self.paths.append(str(path))
2525

2626

2727
@pytest.fixture(name="plot_patch")
@@ -34,14 +34,14 @@ def make_plot_patch(monkeypatch):
3434
return plot_patch
3535

3636

37-
directory = path.dirname(path.realpath(__file__))
37+
directory = Path(__file__).resolve().parent
3838

3939

4040
@pytest.fixture(autouse=True)
4141
def set_config_path(request):
4242
conf.instance.push(
43-
new_path=path.join(directory, "config"),
44-
output_path=path.join(directory, "output"),
43+
new_path=directory / "config",
44+
output_path=directory / "output",
4545
)
4646

4747

@@ -51,7 +51,7 @@ def remove_logs():
5151
for d, _, files in os.walk(directory):
5252
for file in files:
5353
if file.endswith(".log"):
54-
os.remove(path.join(d, file))
54+
os.remove(Path(d) / file)
5555

5656

5757
############

test_autolens/imaging/model/test_analysis_imaging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from os import path
1+
from pathlib import Path
22
import pytest
33

44
import autofit as af
@@ -10,7 +10,7 @@
1010
from autolens import exc
1111

1212

13-
directory = path.dirname(path.realpath(__file__))
13+
directory = Path(__file__).resolve().parent
1414

1515

1616
def test__make_result__result_imaging_is_returned(masked_imaging_7x7):

0 commit comments

Comments
 (0)