Skip to content

Commit 28126cf

Browse files
authored
Merge pull request #15 from Jammy2211/feature/use-pathlib
refactor: replace os.path with pathlib in workspace scripts
2 parents 0cbe8f0 + e09ad81 commit 28126cf

6 files changed

Lines changed: 28 additions & 28 deletions

File tree

projects/cosmology/example_1_intro.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
import matplotlib.pyplot as plt
8080
import numpy as np
8181
from scipy import signal
82-
from os import path
82+
from pathlib import Path
8383

8484
"""
8585
__Plot__
@@ -124,12 +124,12 @@ def plot_grid(grid, title=None):
124124
In the strong lens image and noise map below, you can see this has already been performed, with the edge regions
125125
blank.
126126
"""
127-
dataset_path = path.join("projects", "cosmology", "dataset")
127+
dataset_path = Path("projects") / "cosmology" / "dataset"
128128

129-
data = np.load(file=path.join(dataset_path, "data.npy"))
129+
data = np.load(file=Path(dataset_path) / "data.npy")
130130
plot_array(array=data, title="Image of Strong Lens SDSSJ2303+1422")
131131

132-
noise_map = np.load(file=path.join(dataset_path, "noise_map.npy"))
132+
noise_map = np.load(file=Path(dataset_path) / "noise_map.npy")
133133
plot_array(array=noise_map, title="Noise Map of Strong Lens SDSSJ2303+1422")
134134

135135
"""
@@ -150,7 +150,7 @@ def plot_grid(grid, title=None):
150150
model data. This is an example of how an `Analysis` class may be extended to include additional steps in the model
151151
fitting procedure.
152152
"""
153-
psf = np.load(file=path.join(dataset_path, "psf.npy"))
153+
psf = np.load(file=Path(dataset_path) / "psf.npy")
154154
plot_array(array=psf, title="Point Spread Function of Strong Lens SDSSJ2303+1422")
155155

156156

@@ -166,7 +166,7 @@ def plot_grid(grid, title=None):
166166
This grid only contains (y,x) coordinates within the cricular mask that was applied to the data, as we only need to
167167
perform ray-tracing within this region.
168168
"""
169-
grid = np.load(file=path.join(dataset_path, "grid.npy"))
169+
grid = np.load(file=Path(dataset_path) / "grid.npy")
170170

171171
plot_grid(
172172
grid=grid,

projects/cosmology/example_2_multi_level_model.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
"""
1818

1919
import os
20-
from os import path
2120
from autoconf import conf
2221

2322
cwd = os.getcwd()
24-
config_path = path.join(cwd, "projects", "cosmology", "config")
23+
config_path = Path(cwd) / "projects" / "cosmology" / "config"
2524
conf.instance.push(new_path=config_path)
2625

2726
# %matplotlib inline
@@ -34,6 +33,7 @@
3433
import src as cosmo
3534
import matplotlib.pyplot as plt
3635
import numpy as np
36+
from pathlib import Path
3737

3838
"""
3939
__Plot__
@@ -62,18 +62,18 @@ def plot_grid(grid, title=None):
6262
6363
Now lets load and plot Hubble Space Telescope imaging data of the strong gravitational lens SDSSJ2303+1422.
6464
"""
65-
dataset_path = path.join("projects", "cosmology", "dataset")
65+
dataset_path = Path("projects") / "cosmology" / "dataset"
6666

67-
data = np.load(file=path.join(dataset_path, "data.npy"))
67+
data = np.load(file=Path(dataset_path) / "data.npy")
6868
plot_array(array=data, title="Image of Strong Lens SDSSJ2303+1422")
6969

70-
noise_map = np.load(file=path.join(dataset_path, "noise_map.npy"))
70+
noise_map = np.load(file=Path(dataset_path) / "noise_map.npy")
7171
plot_array(array=noise_map, title="Noise Map of Strong Lens SDSSJ2303+1422")
7272

73-
psf = np.load(file=path.join(dataset_path, "psf.npy"))
73+
psf = np.load(file=Path(dataset_path) / "psf.npy")
7474
plot_array(array=psf, title="Point Spread Function of Strong Lens SDSSJ2303+1422")
7575

76-
grid = np.load(file=path.join(dataset_path, "grid.npy"))
76+
grid = np.load(file=Path(dataset_path) / "grid.npy")
7777

7878
plot_grid(
7979
grid=grid,
@@ -257,7 +257,7 @@ def plot_grid(grid, title=None):
257257
"""
258258

259259
search = af.DynestyStatic(
260-
path_prefix=path.join("projects", "cosmology"),
260+
path_prefix=Path("projects") / "cosmology",
261261
name="multi_level",
262262
nlive=50,
263263
iterations_per_full_update=2500,

scripts/howtofit/chapter_1_introduction/tutorial_8_astronomy_example.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@
6161
- **Chapter Wrap Up**: Summarize the completion of Chapter 1 and its applications to real astronomy.
6262
"""
6363

64-
# from autoconf import setup_notebook; setup_notebook()
64+
# from autoconf import setup_notebook; setup_notebook()
6565

66-
from os import path
6766
import numpy as np
6867
import matplotlib.pyplot as plt
6968
from scipy import signal
@@ -109,15 +108,15 @@ def plot_grid(grid, title=None):
109108
The noise-map has a few strange off-centre features which are an artefact of the telescope. Don't worry about these
110109
features.
111110
"""
112-
dataset_path = path.join("dataset", "howtofit", "chapter_1", "astro", "simple")
111+
dataset_path = Path("dataset") / "howtofit" / "chapter_1" / "astro" / "simple"
113112

114113
"""
115114
__Dataset Auto-Simulation__
116115
117116
If the dataset does not already exist on your system, it will be created by running the corresponding
118117
simulator script. This ensures that all example scripts can be run without manually simulating data first.
119118
"""
120-
if not path.exists(dataset_path):
119+
if not Path(dataset_path).exists():
121120
import subprocess
122121
import sys
123122

@@ -126,10 +125,10 @@ def plot_grid(grid, title=None):
126125
check=True,
127126
)
128127

129-
data = np.load(file=path.join(dataset_path, "data.npy"))
128+
data = np.load(file=Path(dataset_path) / "data.npy")
130129
plot_array(array=data, title="Image of Galaxy")
131130

132-
noise_map = np.load(file=path.join(dataset_path, "noise_map.npy"))
131+
noise_map = np.load(file=Path(dataset_path) / "noise_map.npy")
133132
plot_array(array=noise_map, title="Noise Map of Galaxy")
134133

135134
"""
@@ -143,7 +142,7 @@ def plot_grid(grid, title=None):
143142
We load and plot the mask below to show you how it is applied to the data, and we will use it in
144143
the `log_likelihood_function` below to ensure these regions are not fitted.
145144
"""
146-
mask = np.load(file=path.join(dataset_path, "mask.npy"))
145+
mask = np.load(file=Path(dataset_path) / "mask.npy")
147146
plot_array(array=mask, title="Mask of Galaxy")
148147

149148
"""
@@ -160,7 +159,7 @@ def plot_grid(grid, title=None):
160159
When fitting the data and in the `log_likelihood_function` below, the PSF is used to create the model data. This
161160
demonstrates how an `Analysis` class can be extended to include additional steps in the model fitting process.
162161
"""
163-
psf = np.load(file=path.join(dataset_path, "psf.npy"))
162+
psf = np.load(file=Path(dataset_path) / "psf.npy")
164163
plot_array(array=psf, title="Point Spread Function of Galaxy ?")
165164

166165
"""
@@ -174,7 +173,7 @@ def plot_grid(grid, title=None):
174173
This grid includes only (y,x) coordinates within the circular mask applied to the data, as we only need to perform
175174
calculations within this masked region.
176175
"""
177-
grid = np.load(file=path.join(dataset_path, "grid.npy"))
176+
grid = np.load(file=Path(dataset_path) / "grid.npy")
178177

179178
plot_grid(
180179
grid=grid,
@@ -779,6 +778,7 @@ def model_data_from_instance(self, instance):
779778
780779
This illustrates why we perform model-fitting, we can take complex data and infer simple, interpretable properties
781780
from it which provide insight into the physical processes generating the data. This is the core goal of the scientific
781+
from pathlib import Path
782782
method and the use of models to explain observations.
783783
"""
784784
print(result.info)

searches/pyswarms/example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
autofit's base classes and can be used as drop-in replacements.
1313
"""
1414
import numpy as np
15-
from os import path
1615

1716
from autoconf import conf
1817

1918
# Register the config directory shipped with this repo so that
2019
# PySwarmsGlobal/Local can find their YAML defaults.
2120
workspace_path = path.dirname(path.dirname(path.dirname(path.abspath(__file__))))
22-
conf.instance.push(new_path=path.join(workspace_path, "config"))
21+
conf.instance.push(new_path=Path(workspace_path) / "config")
2322

2423
from searches.pyswarms.globe import PySwarmsGlobal
2524

2625
import autofit as af
26+
from pathlib import Path
2727

2828
# --- Define a simple 1D Gaussian model ---
2929

searches/ultranest/example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
classes and can be used as a drop-in replacement.
1818
"""
1919
import numpy as np
20-
from os import path
2120

2221
from autoconf import conf
2322

2423
# Register the config directory shipped with this repo so that
2524
# UltraNest can find its YAML defaults.
2625
workspace_path = path.dirname(path.dirname(path.dirname(path.abspath(__file__))))
27-
conf.instance.push(new_path=path.join(workspace_path, "config"))
26+
conf.instance.push(new_path=Path(workspace_path) / "config")
2827

2928
from searches.ultranest.search import UltraNest
3029

3130
import autofit as af
31+
from pathlib import Path
3232

3333
# --- Define a simple 1D Gaussian model ---
3434

searches/ultranest/search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def prior_transform(cube):
238238
log_dir = self.paths.search_internal_path
239239

240240
try:
241-
checkpoint_exists = os.path.exists(log_dir / "chains")
241+
checkpoint_exists = Path(log_dir / "chains").exists()
242242
except TypeError:
243243
checkpoint_exists = False
244244

0 commit comments

Comments
 (0)