Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions Model_I/sim_axion.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@

axion_masses = 10**np.random.uniform(-24,-22,num_sim)

failures = 0
for i in range(num_sim):
lens = DeepLens(axion_mass=axion_masses[i])
lens.make_single_halo(1e12)
lens.make_vortex(3e10)
lens.make_source_light()
lens.simple_sim()
File = np.array([lens.image_real,axion_masses[i]])
np.save('/users/mtoomey/scratch/deeplense/Model_I_test/axion/axion_sim_' + str(random.getrandbits(128)),File)
try:
lens = DeepLens(axion_mass=axion_masses[i])
lens.make_single_halo(1e12)
lens.make_vortex(3e10)
lens.make_source_light()
lens.simple_sim()
File = np.array([lens.image_real,axion_masses[i]])
np.save('/users/mtoomey/scratch/deeplense/Model_I_test/axion/axion_sim_' + str(random.getrandbits(128)),File)
except Exception as exc:
failures += 1
print(f"sim_axion: skipping sim {i} after failure: {exc}")
continue

print(f"sim_axion: completed with {failures} failed sim(s) out of {num_sim}")



Expand Down
22 changes: 15 additions & 7 deletions Model_I/sim_cdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@
# Number of sims
num_sim = int(5e3)

failures = 0
for i in range(num_sim):
lens = DeepLens()
lens.make_single_halo(1e12)
lens.make_old_cdm()
lens.make_source_light()
lens.simple_sim()
File = lens.image_real
np.save('/users/mtoomey/scratch/deeplense/Model_I_test/cdm/cdm_sim_' + str(random.getrandbits(128)),File)
try:
lens = DeepLens()
lens.make_single_halo(1e12)
lens.make_old_cdm()
lens.make_source_light()
lens.simple_sim()
File = lens.image_real
np.save('/users/mtoomey/scratch/deeplense/Model_I_test/cdm/cdm_sim_' + str(random.getrandbits(128)),File)
except Exception as exc:
failures += 1
print(f"sim_cdm: skipping sim {i} after failure: {exc}")
continue

print(f"sim_cdm: completed with {failures} failed sim(s) out of {num_sim}")



Expand Down
22 changes: 15 additions & 7 deletions Model_I/sim_no_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@
# Number of sims
num_sim = int(5e3)

failures = 0
for i in range(num_sim):
lens = DeepLens()
lens.make_single_halo(1e12)
lens.make_no_sub()
lens.make_source_light()
lens.simple_sim()
File = lens.image_real
np.save('/users/mtoomey/scratch/deeplense/Model_I_test/no_sub/no_sub_sim_' + str(random.getrandbits(128)),File)
try:
lens = DeepLens()
lens.make_single_halo(1e12)
lens.make_no_sub()
lens.make_source_light()
lens.simple_sim()
File = lens.image_real
np.save('/users/mtoomey/scratch/deeplense/Model_I_test/no_sub/no_sub_sim_' + str(random.getrandbits(128)),File)
except Exception as exc:
failures += 1
print(f"sim_no_sub: skipping sim {i} after failure: {exc}")
continue

print(f"sim_no_sub: completed with {failures} failed sim(s) out of {num_sim}")



Expand Down
24 changes: 16 additions & 8 deletions Model_II/sim_axion.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@

axion_masses = 10**np.random.uniform(-24,-22,num_sim)

failures = 0
for i in range(num_sim):
lens = DeepLens(axion_mass=axion_masses[i])
lens.make_single_halo(1e12)
lens.make_vortex(3e10)
lens.set_instrument('Euclid')
lens.make_source_light_mag()
lens.simple_sim_2()
File = np.array([lens.image_real,axion_masses[i]])
np.save('/users/mtoomey/scratch/deeplense/Model_II_test/axion/axion_sim_' + str(random.getrandbits(128)),File)
try:
lens = DeepLens(axion_mass=axion_masses[i])
lens.make_single_halo(1e12)
lens.make_vortex(3e10)
lens.set_instrument('Euclid')
lens.make_source_light_mag()
lens.simple_sim_2()
File = np.array([lens.image_real,axion_masses[i]])
np.save('/users/mtoomey/scratch/deeplense/Model_II_test/axion/axion_sim_' + str(random.getrandbits(128)),File)
except Exception as exc:
failures += 1
print(f"sim_axion: skipping sim {i} after failure: {exc}")
continue

print(f"sim_axion: completed with {failures} failed sim(s) out of {num_sim}")



Expand Down
24 changes: 16 additions & 8 deletions Model_II/sim_cdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@
# Number of sims
num_sim = int(5e3)

failures = 0
for i in range(num_sim):
lens = DeepLens()
lens.make_single_halo(1e12)
lens.make_old_cdm()
lens.set_instrument('Euclid')
lens.make_source_light_mag()
lens.simple_sim_2()
File = lens.image_real
np.save('/users/mtoomey/scratch/deeplense/Model_II_test/cdm/cdm_sim_' + str(random.getrandbits(128)),File)
try:
lens = DeepLens()
lens.make_single_halo(1e12)
lens.make_old_cdm()
lens.set_instrument('Euclid')
lens.make_source_light_mag()
lens.simple_sim_2()
File = lens.image_real
np.save('/users/mtoomey/scratch/deeplense/Model_II_test/cdm/cdm_sim_' + str(random.getrandbits(128)),File)
except Exception as exc:
failures += 1
print(f"sim_cdm: skipping sim {i} after failure: {exc}")
continue

print(f"sim_cdm: completed with {failures} failed sim(s) out of {num_sim}")



Expand Down
24 changes: 16 additions & 8 deletions Model_II/sim_no_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@
# Number of sims
num_sim = int(5e3)

failures = 0
for i in range(num_sim):
lens = DeepLens()
lens.make_single_halo(1e12)
lens.make_no_sub()
lens.set_instrument('Euclid')
lens.make_source_light_mag()
lens.simple_sim_2()
File = lens.image_real
np.save('/users/mtoomey/scratch/deeplense/Model_II_test/no_sub/no_sub_sim_' + str(random.getrandbits(128)),File)
try:
lens = DeepLens()
lens.make_single_halo(1e12)
lens.make_no_sub()
lens.set_instrument('Euclid')
lens.make_source_light_mag()
lens.simple_sim_2()
File = lens.image_real
np.save('/users/mtoomey/scratch/deeplense/Model_II_test/no_sub/no_sub_sim_' + str(random.getrandbits(128)),File)
except Exception as exc:
failures += 1
print(f"sim_no_sub: skipping sim {i} after failure: {exc}")
continue

print(f"sim_no_sub: completed with {failures} failed sim(s) out of {num_sim}")



Expand Down
24 changes: 16 additions & 8 deletions Model_III/sim_axion.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@

axion_masses = 10**np.random.uniform(-24,-22,num_sim)

failures = 0
for i in range(num_sim):
lens = DeepLens(axion_mass=axion_masses[i])
lens.make_single_halo(1e12)
lens.make_vortex(3e10)
lens.set_instrument('hst')
lens.make_source_light_mag()
lens.simple_sim_2()
File = np.array([lens.image_real,axion_masses[i]])
np.save('/users/mtoomey/scratch/deeplense/Model_III_test/axion/axion_sim_' + str(random.getrandbits(128)),File)
try:
lens = DeepLens(axion_mass=axion_masses[i])
lens.make_single_halo(1e12)
lens.make_vortex(3e10)
lens.set_instrument('hst')
lens.make_source_light_mag()
lens.simple_sim_2()
File = np.array([lens.image_real,axion_masses[i]])
np.save('/users/mtoomey/scratch/deeplense/Model_III_test/axion/axion_sim_' + str(random.getrandbits(128)),File)
except Exception as exc:
failures += 1
print(f"sim_axion: skipping sim {i} after failure: {exc}")
continue

print(f"sim_axion: completed with {failures} failed sim(s) out of {num_sim}")



Expand Down
24 changes: 16 additions & 8 deletions Model_III/sim_cdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@
# Number of sims
num_sim = int(3e5)

failures = 0
for i in range(num_sim):
lens = DeepLens()
lens.make_single_halo(1e12)
lens.make_old_cdm()
lens.set_instrument('hst')
lens.make_source_light_mag()
lens.simple_sim_2()
File = lens.image_real
np.save('/users/mtoomey/scratch/deeplense/Model_III_test/cdm/cdm_sim_' + str(random.getrandbits(128)),File)
try:
lens = DeepLens()
lens.make_single_halo(1e12)
lens.make_old_cdm()
lens.set_instrument('hst')
lens.make_source_light_mag()
lens.simple_sim_2()
File = lens.image_real
np.save('/users/mtoomey/scratch/deeplense/Model_III_test/cdm/cdm_sim_' + str(random.getrandbits(128)),File)
except Exception as exc:
failures += 1
print(f"sim_cdm: skipping sim {i} after failure: {exc}")
continue

print(f"sim_cdm: completed with {failures} failed sim(s) out of {num_sim}")



Expand Down
24 changes: 16 additions & 8 deletions Model_III/sim_no_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@
# Number of sims
num_sim = int(3e5)

failures = 0
for i in range(num_sim):
lens = DeepLens()
lens.make_single_halo(1e12)
lens.make_no_sub()
lens.set_instrument('hst')
lens.make_source_light_mag()
lens.simple_sim_2()
File = lens.image_real
np.save('/users/mtoomey/scratch/deeplense/Model_III_test/no_sub/no_sub_sim_' + str(random.getrandbits(128)),File)
try:
lens = DeepLens()
lens.make_single_halo(1e12)
lens.make_no_sub()
lens.set_instrument('hst')
lens.make_source_light_mag()
lens.simple_sim_2()
File = lens.image_real
np.save('/users/mtoomey/scratch/deeplense/Model_III_test/no_sub/no_sub_sim_' + str(random.getrandbits(128)),File)
except Exception as exc:
failures += 1
print(f"sim_no_sub: skipping sim {i} after failure: {exc}")
continue

print(f"sim_no_sub: completed with {failures} failed sim(s) out of {num_sim}")



Expand Down
8 changes: 6 additions & 2 deletions deeplense/lens.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from lenstronomy.LensModel.lens_model import LensModel
from lenstronomy.SimulationAPI.sim_api import SimAPI

from deeplense.validation import validate_image



#TODO this is only valid for halo z=0.5
Expand Down Expand Up @@ -267,7 +269,8 @@ def simple_sim(self):
bkg = image_util.add_background(image_model, sigma_bkd=background_rms)
image_real = exp_time * (image_model + poisson + bkg)

self.image_real = np.random.poisson(image_real.clip(min=0))#print(np.isnan(image_real).any())
self.image_real = np.random.poisson(image_real.clip(min=0))
validate_image(self.image_real)
self.image_model = image_model
self.poisson = poisson
self.bkg = bkg
Expand Down Expand Up @@ -310,7 +313,8 @@ def simple_sim_2(self):

self.image_model = image
self.poisson = sim.noise_for_model(model=image)
self.image_real = self.image_model + self.image_model
self.image_real = self.image_model + self.poisson
validate_image(self.image_real)

def set_instrument(self,inst_name):
"""
Expand Down
34 changes: 34 additions & 0 deletions deeplense/validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import numpy as np


class SimulationValidationError(ValueError):
"""Raised when a simulated image fails a numerical/physical sanity check."""


def validate_image(image):
"""
Sanity-check a simulated lensing image before it is saved to disk.

Catches the failure modes that otherwise silently corrupt a batch run:
NaN/Inf pixels (unstable lens configuration), empty arrays, and
constant/zero-variance images (a degenerate sim that produced no
signal). Returns the image unchanged so it can be used inline.
"""
image = np.asarray(image)

if image.size == 0:
raise SimulationValidationError("simulated image is empty")

if np.isnan(image).any():
raise SimulationValidationError("simulated image contains NaN values")

if np.isinf(image).any():
raise SimulationValidationError("simulated image contains Inf values")

if np.all(image == image.flat[0]):
raise SimulationValidationError(
"simulated image has zero variance (constant image, likely a "
"failed lens/source configuration)"
)

return image