Skip to content

Commit fd7d866

Browse files
Jammy2211claude
authored andcommitted
fix: restore lost setup scaffolding in chapter 2 tutorial_2_practicalities
The imports, dataset load, mask and model setup were accidentally deleted from this tutorial. Git history pinpoints the cause: the autolens_workspace commit ce1bf623 "Add __Contents__ sections to all scripts" overwrote the region spanning the old Contents list down through the __Search__ prose, collaterally removing ~119 lines of setup code. The file dropped 421 -> 302 lines there. The later HowToLens bootstrap (62a5f3a) faithfully copied the already-broken file, so the first runnable line was `search = af.Nautilus(...)` referencing undefined `dataset_name`, `dataset` and `model`. This restores the verbatim scaffolding from the last-complete version (autolens_workspace 241cc8c0): imports, __PyAutoFit__, __Initial Setup__ (with auto-simulation), __Mask__ and __Model__. The dataset is `simple__no_lens_light__mass_sis` (IsothermalSph mass + linear ExponentialCoreSph source), the same fit as tutorial_1, matching the tutorial's own prose ("the same dataset we fitted in the previous tutorial"). Modernized to current-repo conventions: setup_notebook header, `scripts/simulator/...` subprocess path, and the PyAutoLabs/PyAutoFit URL. Verified runnable end-to-end in fast mode (PYAUTO_TEST_MODE=2 + skip flags): model composes (6 free params), dataset loads, search and likelihood evaluation complete (exit 0). Scanned both HowTo repos and the source commit's deletion footprint: tutorial_2 was the only tutorial damaged this way; all other candidates were verified false positives. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 0e4209e commit fd7d866

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

scripts/chapter_2_lens_modeling/tutorial_2_practicalities.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,104 @@
7474
is outputting results, try increasing this value to ensure the model-fit runs efficiently.**
7575
"""
7676

77+
from autoconf import jax_wrapper # Sets JAX environment before other imports
78+
79+
# from autoconf import setup_notebook; setup_notebook()
80+
81+
from pathlib import Path
82+
import autolens as al
83+
import autolens.plot as aplt
84+
85+
"""
86+
__PyAutoFit__
87+
88+
Modeling uses the probabilistic programming language
89+
[PyAutoFit](https://github.com/PyAutoLabs/PyAutoFit), an open-source project that allows complex model
90+
fitting techniques to be straightforwardly integrated into scientific modeling software.
91+
92+
The majority of tools that make model-fitting practical are provided by PyAutoFit, for example it handles
93+
all output of the non-linear search to hard-disk, the visualization of results and the estimation of run-times.
94+
"""
95+
import autofit as af
96+
97+
"""
98+
__Initial Setup__
99+
100+
Lets first load the `Imaging` dataset we'll fit a model with using a non-linear search.
101+
102+
This is the same dataset we fitted in the previous tutorial, and we'll repeat the same fit, as we simply want
103+
to illustrate the practicalities of model-fitting in this tutorial.
104+
"""
105+
dataset_name = "simple__no_lens_light__mass_sis"
106+
dataset_path = Path("dataset") / "imaging" / dataset_name
107+
108+
"""
109+
__Dataset Auto-Simulation__
110+
111+
If the dataset does not already exist on your system, it will be created by running the corresponding
112+
simulator script. This ensures that all example scripts can be run without manually simulating data first.
113+
"""
114+
if not dataset_path.exists():
115+
import subprocess
116+
import sys
117+
118+
subprocess.run(
119+
[sys.executable, "scripts/simulator/no_lens_light__mass_sis.py"],
120+
check=True,
121+
)
122+
123+
dataset = al.Imaging.from_fits(
124+
data_path=dataset_path / "data.fits",
125+
noise_map_path=dataset_path / "noise_map.fits",
126+
psf_path=dataset_path / "psf.fits",
127+
pixel_scales=0.1,
128+
)
129+
130+
aplt.subplot_imaging_dataset(dataset=dataset)
131+
132+
"""
133+
__Mask__
134+
135+
The non-linear fit also needs a `Mask2D`, lets use a 3.0" circle.
136+
"""
137+
mask_radius = 3.0
138+
139+
mask = al.Mask2D.circular(
140+
shape_native=dataset.shape_native,
141+
pixel_scales=dataset.pixel_scales,
142+
radius=mask_radius,
143+
)
144+
145+
dataset = dataset.apply_mask(mask=mask)
146+
147+
aplt.subplot_imaging_dataset(dataset=dataset)
148+
149+
"""
150+
__Model__
151+
152+
We compose the model using the same API as the previous tutorial.
153+
154+
This model is the same as the previous tutorial, an `IsothermalSph` spherical mass profile representing the lens
155+
galaxy and a `ExponentialCoreSph` light profile representing the source galaxy.
156+
"""
157+
# Lens:
158+
159+
mass = af.Model(al.mp.IsothermalSph)
160+
161+
lens = af.Model(al.Galaxy, redshift=0.5, mass=mass)
162+
163+
# Source:
164+
165+
bulge = af.Model(al.lp_linear.ExponentialCoreSph)
166+
167+
source = af.Model(al.Galaxy, redshift=1.0, bulge=bulge)
168+
169+
# Overall Lens Model:
170+
171+
model = af.Collection(galaxies=af.Collection(lens=lens, source=source))
172+
173+
print(model.info)
174+
77175
search = af.Nautilus(
78176
path_prefix=Path("howtolens") / "chapter_2",
79177
name="tutorial_2_practicalities",

0 commit comments

Comments
 (0)