From e09ad813c9dc2f2a3e31e2ea309d6ada935d0e5a Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 7 May 2026 10:35:52 +0100 Subject: [PATCH] refactor: replace os.path with pathlib Drop legacy `from os import path` and `os.path.X` usages in workspace scripts in favour of `pathlib.Path`. Follow-up to library refactor in PyAutoLabs/PyAutoFit#1257. Co-Authored-By: Claude Opus 4.7 (1M context) --- projects/cosmology/example_1_intro.py | 12 ++++++------ .../cosmology/example_2_multi_level_model.py | 16 ++++++++-------- .../tutorial_8_astronomy_example.py | 18 +++++++++--------- searches/pyswarms/example.py | 4 ++-- searches/ultranest/example.py | 4 ++-- searches/ultranest/search.py | 2 +- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/projects/cosmology/example_1_intro.py b/projects/cosmology/example_1_intro.py index f72f1de..ca63ef2 100644 --- a/projects/cosmology/example_1_intro.py +++ b/projects/cosmology/example_1_intro.py @@ -79,7 +79,7 @@ import matplotlib.pyplot as plt import numpy as np from scipy import signal -from os import path +from pathlib import Path """ __Plot__ @@ -124,12 +124,12 @@ def plot_grid(grid, title=None): In the strong lens image and noise map below, you can see this has already been performed, with the edge regions blank. """ -dataset_path = path.join("projects", "cosmology", "dataset") +dataset_path = Path("projects") / "cosmology" / "dataset" -data = np.load(file=path.join(dataset_path, "data.npy")) +data = np.load(file=Path(dataset_path) / "data.npy") plot_array(array=data, title="Image of Strong Lens SDSSJ2303+1422") -noise_map = np.load(file=path.join(dataset_path, "noise_map.npy")) +noise_map = np.load(file=Path(dataset_path) / "noise_map.npy") plot_array(array=noise_map, title="Noise Map of Strong Lens SDSSJ2303+1422") """ @@ -150,7 +150,7 @@ def plot_grid(grid, title=None): model data. This is an example of how an `Analysis` class may be extended to include additional steps in the model fitting procedure. """ -psf = np.load(file=path.join(dataset_path, "psf.npy")) +psf = np.load(file=Path(dataset_path) / "psf.npy") plot_array(array=psf, title="Point Spread Function of Strong Lens SDSSJ2303+1422") @@ -166,7 +166,7 @@ def plot_grid(grid, title=None): This grid only contains (y,x) coordinates within the cricular mask that was applied to the data, as we only need to perform ray-tracing within this region. """ -grid = np.load(file=path.join(dataset_path, "grid.npy")) +grid = np.load(file=Path(dataset_path) / "grid.npy") plot_grid( grid=grid, diff --git a/projects/cosmology/example_2_multi_level_model.py b/projects/cosmology/example_2_multi_level_model.py index e19f75f..51215d8 100644 --- a/projects/cosmology/example_2_multi_level_model.py +++ b/projects/cosmology/example_2_multi_level_model.py @@ -17,11 +17,10 @@ """ import os -from os import path from autoconf import conf cwd = os.getcwd() -config_path = path.join(cwd, "projects", "cosmology", "config") +config_path = Path(cwd) / "projects" / "cosmology" / "config" conf.instance.push(new_path=config_path) # %matplotlib inline @@ -34,6 +33,7 @@ import src as cosmo import matplotlib.pyplot as plt import numpy as np +from pathlib import Path """ __Plot__ @@ -62,18 +62,18 @@ def plot_grid(grid, title=None): Now lets load and plot Hubble Space Telescope imaging data of the strong gravitational lens SDSSJ2303+1422. """ -dataset_path = path.join("projects", "cosmology", "dataset") +dataset_path = Path("projects") / "cosmology" / "dataset" -data = np.load(file=path.join(dataset_path, "data.npy")) +data = np.load(file=Path(dataset_path) / "data.npy") plot_array(array=data, title="Image of Strong Lens SDSSJ2303+1422") -noise_map = np.load(file=path.join(dataset_path, "noise_map.npy")) +noise_map = np.load(file=Path(dataset_path) / "noise_map.npy") plot_array(array=noise_map, title="Noise Map of Strong Lens SDSSJ2303+1422") -psf = np.load(file=path.join(dataset_path, "psf.npy")) +psf = np.load(file=Path(dataset_path) / "psf.npy") plot_array(array=psf, title="Point Spread Function of Strong Lens SDSSJ2303+1422") -grid = np.load(file=path.join(dataset_path, "grid.npy")) +grid = np.load(file=Path(dataset_path) / "grid.npy") plot_grid( grid=grid, @@ -257,7 +257,7 @@ def plot_grid(grid, title=None): """ search = af.DynestyStatic( - path_prefix=path.join("projects", "cosmology"), + path_prefix=Path("projects") / "cosmology", name="multi_level", nlive=50, iterations_per_full_update=2500, diff --git a/scripts/howtofit/chapter_1_introduction/tutorial_8_astronomy_example.py b/scripts/howtofit/chapter_1_introduction/tutorial_8_astronomy_example.py index bb50b7c..7e09682 100644 --- a/scripts/howtofit/chapter_1_introduction/tutorial_8_astronomy_example.py +++ b/scripts/howtofit/chapter_1_introduction/tutorial_8_astronomy_example.py @@ -61,9 +61,8 @@ - **Chapter Wrap Up**: Summarize the completion of Chapter 1 and its applications to real astronomy. """ -# from autoconf import setup_notebook; setup_notebook() +# from autoconf import setup_notebook; setup_notebook() -from os import path import numpy as np import matplotlib.pyplot as plt from scipy import signal @@ -109,7 +108,7 @@ def plot_grid(grid, title=None): The noise-map has a few strange off-centre features which are an artefact of the telescope. Don't worry about these features. """ -dataset_path = path.join("dataset", "howtofit", "chapter_1", "astro", "simple") +dataset_path = Path("dataset") / "howtofit" / "chapter_1" / "astro" / "simple" """ __Dataset Auto-Simulation__ @@ -117,7 +116,7 @@ def plot_grid(grid, title=None): If the dataset does not already exist on your system, it will be created by running the corresponding simulator script. This ensures that all example scripts can be run without manually simulating data first. """ -if not path.exists(dataset_path): +if not Path(dataset_path).exists(): import subprocess import sys @@ -126,10 +125,10 @@ def plot_grid(grid, title=None): check=True, ) -data = np.load(file=path.join(dataset_path, "data.npy")) +data = np.load(file=Path(dataset_path) / "data.npy") plot_array(array=data, title="Image of Galaxy") -noise_map = np.load(file=path.join(dataset_path, "noise_map.npy")) +noise_map = np.load(file=Path(dataset_path) / "noise_map.npy") plot_array(array=noise_map, title="Noise Map of Galaxy") """ @@ -143,7 +142,7 @@ def plot_grid(grid, title=None): We load and plot the mask below to show you how it is applied to the data, and we will use it in the `log_likelihood_function` below to ensure these regions are not fitted. """ -mask = np.load(file=path.join(dataset_path, "mask.npy")) +mask = np.load(file=Path(dataset_path) / "mask.npy") plot_array(array=mask, title="Mask of Galaxy") """ @@ -160,7 +159,7 @@ def plot_grid(grid, title=None): When fitting the data and in the `log_likelihood_function` below, the PSF is used to create the model data. This demonstrates how an `Analysis` class can be extended to include additional steps in the model fitting process. """ -psf = np.load(file=path.join(dataset_path, "psf.npy")) +psf = np.load(file=Path(dataset_path) / "psf.npy") plot_array(array=psf, title="Point Spread Function of Galaxy ?") """ @@ -174,7 +173,7 @@ def plot_grid(grid, title=None): This grid includes only (y,x) coordinates within the circular mask applied to the data, as we only need to perform calculations within this masked region. """ -grid = np.load(file=path.join(dataset_path, "grid.npy")) +grid = np.load(file=Path(dataset_path) / "grid.npy") plot_grid( grid=grid, @@ -779,6 +778,7 @@ def model_data_from_instance(self, instance): This illustrates why we perform model-fitting, we can take complex data and infer simple, interpretable properties from it which provide insight into the physical processes generating the data. This is the core goal of the scientific +from pathlib import Path method and the use of models to explain observations. """ print(result.info) diff --git a/searches/pyswarms/example.py b/searches/pyswarms/example.py index 09f1a68..412d118 100644 --- a/searches/pyswarms/example.py +++ b/searches/pyswarms/example.py @@ -12,18 +12,18 @@ autofit's base classes and can be used as drop-in replacements. """ import numpy as np -from os import path from autoconf import conf # Register the config directory shipped with this repo so that # PySwarmsGlobal/Local can find their YAML defaults. workspace_path = path.dirname(path.dirname(path.dirname(path.abspath(__file__)))) -conf.instance.push(new_path=path.join(workspace_path, "config")) +conf.instance.push(new_path=Path(workspace_path) / "config") from searches.pyswarms.globe import PySwarmsGlobal import autofit as af +from pathlib import Path # --- Define a simple 1D Gaussian model --- diff --git a/searches/ultranest/example.py b/searches/ultranest/example.py index 113c898..b308515 100644 --- a/searches/ultranest/example.py +++ b/searches/ultranest/example.py @@ -17,18 +17,18 @@ classes and can be used as a drop-in replacement. """ import numpy as np -from os import path from autoconf import conf # Register the config directory shipped with this repo so that # UltraNest can find its YAML defaults. workspace_path = path.dirname(path.dirname(path.dirname(path.abspath(__file__)))) -conf.instance.push(new_path=path.join(workspace_path, "config")) +conf.instance.push(new_path=Path(workspace_path) / "config") from searches.ultranest.search import UltraNest import autofit as af +from pathlib import Path # --- Define a simple 1D Gaussian model --- diff --git a/searches/ultranest/search.py b/searches/ultranest/search.py index 107772a..1d25c66 100755 --- a/searches/ultranest/search.py +++ b/searches/ultranest/search.py @@ -238,7 +238,7 @@ def prior_transform(cube): log_dir = self.paths.search_internal_path try: - checkpoint_exists = os.path.exists(log_dir / "chains") + checkpoint_exists = Path(log_dir / "chains").exists() except TypeError: checkpoint_exists = False