Below is “white-board level” pseudocode that maps the conceptual plan into a step-by-step workflow.
The syntax is intentionally language-agnostic; indentation and ALL-CAPS keywords (FUNCTION, IF, FOR, etc.) clarify control flow, while comments (# …) describe key PyAutoLens calls or astrophysical reasoning that would replace the placeholders in real code.
###############################################################################
# 0. GLOBAL PARAMETERS & INPUTS
###############################################################################
SET sigma_threshold = 3.0 # residual significance for seeds
SET log_evidence_threshold = 10.0 # Δlog E required to claim detection
SET subhalo_mass_prior = [1e7, 1e10] # M⊙ (flat-in-log prior)
SET subhalo_profile_type = "NFW" # or "Burkert", "SIS", …
SET grid_spacing_arcsec = 0.10 # for blind grid search
SET max_cpu_cores = N # use all available cores
###############################################################################
# 1. LOAD & PRE-PROCESS DATA
###############################################################################
FUNCTION load_imaging_data(filepath):
imaging = PyAutoLens.Imaging.from_fits(filepath) # data, noise, psf
mask = PyAutoLens.Mask.circular_annulus(...) # user-defined mask
imaging.apply_mask(mask)
RETURN imaging
imaging_data = load_imaging_data("lensed_system.fits")
###############################################################################
# 2. FIT THE “MACRO” (MAIN) LENS MODEL
###############################################################################
FUNCTION fit_macro_model(imaging):
# Build light + mass + source models (no subhalos yet)
lens_galaxy = Galaxy(mass=EllNFW, light=Sersic, ...) # example
source_galaxy= Galaxy(light=Sersic, ...)
model = Collection(galaxies=[lens_galaxy, source_galaxy])
result_macro = PyAutoLens.FitImaging(
imaging = imaging,
model = model,
search_algo = "nested_sampling",
settings = {n_live: 200, ...}
).run()
RETURN result_macro
macro_result = fit_macro_model(imaging_data)
store(macro_result, "macro_result.pkl")
###############################################################################
# 3. COMPUTE RESIDUAL MAPS
###############################################################################
FUNCTION compute_residuals(imaging, macro_result):
macro_model_image = macro_result.tracer.image_2d_from(imaging.grid)
residual_map = imaging.data - macro_model_image
chi_map = residual_map / imaging.noise_map
RETURN residual_map, chi_map
residual_map, chi_map = compute_residuals(imaging_data, macro_result)
save_fits(residual_map, "residual_map.fits")
###############################################################################
# 4. SEED CANDIDATE SUBHALO POSITIONS
###############################################################################
FUNCTION find_candidate_seeds(chi_map, sigma_threshold):
thresholded_pixels = WHERE(ABS(chi_map) > sigma_threshold)
clusters = CLUSTER_CONNECTED_PIXELS(thresholded_pixels)
seed_list = []
FOR cluster IN clusters:
centroid = COMPUTE_CENTROID(cluster)
seed_list.APPEND(centroid)
RETURN seed_list
seed_positions = find_candidate_seeds(chi_map, sigma_threshold)
###############################################################################
# 5. OPTIONAL BLIND GRID SEARCH (fills gaps where seeds may miss)
###############################################################################
FUNCTION generate_grid(imaging, spacing):
bounds = imaging.mask.bounding_box
grid = []
FOR x FROM bounds.xmin TO xmax STEP spacing:
FOR y FROM bounds.ymin TO ymax STEP spacing:
IF (x, y) IS NOT MASKED:
grid.APPEND((x, y))
RETURN grid
grid_positions = generate_grid(imaging_data, grid_spacing_arcsec)
###############################################################################
# 6. BUILD MASTER CANDIDATE LIST
###############################################################################
candidate_positions = MERGE_UNIQUE(seed_positions, grid_positions)
###############################################################################
# 7. FIT EACH CANDIDATE WITH A SUBHALO MODEL
###############################################################################
FUNCTION fit_subhalo_at_position(imaging, macro_result, pos):
# Clone macro model; add *ONE* subhalo with priors
subhalo = Galaxy(
mass = subhalo_profile_type,
redshift = macro_result.lens_redshift,
priors = {
"centre" : GaussianPrior(mean=pos, sigma=0.05"),
"mass" : LogUniformPrior(subhalo_mass_prior)
}
)
model_plus = macro_result.model + subhalo # combine collections
result_plus = PyAutoLens.FitImaging(
imaging = imaging,
model = model_plus,
search_algo = "nested_sampling",
settings = {n_live: 100, ...}
).run()
ΔlogE = result_plus.log_evidence - macro_result.log_evidence
RETURN result_plus, ΔlogE
# Parallel loop over all candidate positions
DETECT_LIST = PARALLEL_MAP(
fit_subhalo_at_position,
args_list = [(imaging_data, macro_result, pos) FOR pos IN candidate_positions],
n_cores = max_cpu_cores
)
###############################################################################
# 8. FILTER SIGNIFICANT DETECTIONS
###############################################################################
significant_detections = []
FOR result_plus, ΔlogE IN DETECT_LIST:
IF ΔlogE > log_evidence_threshold:
significant_detections.APPEND({result: result_plus, delta_logE: ΔlogE})
###############################################################################
# 9. VALIDATION SUITE
###############################################################################
FUNCTION validate_detection(detection):
# A: Noise realization test --------------------------
synth_imaging = SIMULATE_MACRO_PLUS_NOISE(macro_result)
re_residuals = compute_residuals(synth_imaging, macro_result)
REPEAT seed search → no false positives?
# B: PSF perturbation test ---------------------------
perturbed_psf_imaging = imaging_data.with_psf(JITTER_PSF(imaging_data.psf))
re_result, re_ΔlogE = fit_subhalo_at_position(perturbed_psf_imaging, macro_result,
detection.result.subhalo.centre)
# C: Degeneracy test --------------------------------
macro_variant = tweak_macro_priors(macro_result)
no_subhalo_fit= fit_macro_model(imaging_data, initial_model=macro_variant)
RECOMPUTE ΔlogE′
RETURN PASSED_ALL_TESTS(…logic…)
validated_detections = []
FOR det IN significant_detections:
IF validate_detection(det):
validated_detections.APPEND(det)
###############################################################################
# 10. OUTPUT & VISUALIZATION
###############################################################################
FUNCTION summarize_detections(detections):
PRINT "---- Subhalo Candidates ----"
FOR det IN detections:
pos = det.result.subhalo.centre.median
mass = det.result.subhalo.mass.median
logE = det.delta_logE
PRINT f"Pos(arcsec)={pos}, M(⊙)={mass}, ΔlogE={logE}"
EXPORT_CORNER_PLOT(det.result, filename=f"corner_{pos}.png")
SAVE_MODEL(det.result, f"model_with_subhalo_{pos}.pkl")
summarize_detections(validated_detections)
###############################################################################
# 11. OPTIONAL POPULATION-LEVEL INFERENCE
###############################################################################
# Combine posteriors of all validated detections to constrain the subhalo mass
# function or compare with ΛCDM predictions.
########################################################################
# END OF PSEUDOCODE
########################################################################
Explanatory Notes (not part of the pseudocode itself):
• Steps 1–3: Establish a robust macro model so that residuals truly capture subhalo‐induced anomalies.
• Steps 4–6: Two-track seeding (σ-threshold + blind grid) ensures both “obvious” and “hidden” subhalos are considered.
• Step 7: Each candidate is re-fitted with a single subhalo; Δlog E quickly tells whether the data justify the extra complexity.
• Step 9: Validation stresses the detection against noise, PSF uncertainties, and macro-model degeneracies—key to avoiding false positives.
• The entire pipeline is parallelizable and modular; each block can be swapped with domain-specific alternatives (e.g., a CNN in Step 4, different priors in Step 7).