Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test__image_all_1s__bg_noise_all_1s__exposure_time_all_2s__noise_map_all_sqrt_6s_over_2(
self
):
imaging_data = aa.array.ones(shape_2d=(2,2))
background_noise_map = aa.array.ones(shape_2d=(2,2))
exposure_time_map = aa.array.full(fill_value=2.0, shape_2d=(2,2))
noise_map = al.NoiseMap.from_image_and_background_noise_map(
image=imaging_data,
background_noise_map=background_noise_map,
gain=1.0,
exposure_time_map=exposure_time_map,
)
assert (
noise_map.in_2d
== np.array(
[
[np.sqrt(6.0) / 2.0, np.sqrt(6.0) / 2.0],
[np.sqrt(6.0) / 2.0, np.sqrt(6.0) / 2.0],
]
self
):
imaging_data = al.load_imaging_data_from_fits(
pixel_scales=0.1,
image_path=test_data_dir + "3x3_ones.fits",
psf_path=test_data_dir + "3x3_twos.fits",
noise_map_from_image_and_background_noise_map=True,
background_noise_map_path=test_data_dir + "3x3_fours.fits",
poisson_noise_map_path=test_data_dir + "3x3_fives.fits",
exposure_time_map_path=test_data_dir + "3x3_sixes.fits",
background_sky_map_path=test_data_dir + "3x3_sevens.fits",
convert_from_electrons=True,
renormalize_psf=False,
)
image = aa.array.ones(shape_2d=(3,3))
background_noise_map = aa.array.manual_2d(
array=4.0 * np.ones((3, 3))
)
noise_map_converted = al.NoiseMap.from_image_and_background_noise_map(
image=image,
background_noise_map=background_noise_map,
gain=None,
exposure_time_map=imaging_data.exposure_time_map,
convert_from_electrons=True,
)
noise_map_converted = noise_map_converted / 6.0
assert (imaging_data.image.in_2d == np.ones((3, 3)) / 6.0).all()
assert (imaging_data.psf.in_2d == 2.0 * np.ones((3, 3))).all()
def test__same_as_above_but_different_image_values_in_each_pixel_and_new_background_values(
self
):
# Can use pattern from previous test_autoarray for values
image = aa.array.manual_2d(
array=[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]
)
exposure_time = aa.array.ones(shape_2d=(3,2))
background_noise = aa.array.full(fill_value=12.0, shape_2d=(3,2))
imaging_data = al.ImagingData(
image=image,
pixel_scales=1.0,
psf=aa.kernel.ones(shape_2d=(3,3)),
exposure_time_map=exposure_time,
background_noise_map=background_noise,
)
assert imaging_data.estimated_noise_map.in_2d == pytest.approx(
np.array(
[
[np.sqrt(1.0 + 144.0), np.sqrt(2.0 + 144.0)],
[np.sqrt(3.0 + 144.0), np.sqrt(4.0 + 144.0)],
[np.sqrt(5.0 + 144.0), np.sqrt(6.0 + 144.0)],
def test__image_and_exposure_times_all_1s__background_is_float_sqrt_3__noise_is_all_2s(
self
):
# Imaging (eps) = 1.0
# Background (eps) = sqrt(3.0)
# Exposure times = 1.0 s
# Imaging (counts) = 1.0
# Background (counts) = sqrt(3.0)
# Noise (counts) = sqrt(1.0 + sqrt(3.0)**2) = sqrt(1.0 + 3.0) = 2.0
# Noise (eps) = 2.0 / 1.0 = 2.0
image = aa.array.ones(shape_2d=(3,3))
exposure_time = aa.array.ones(shape_2d=(3,3))
background_noise = aa.array.full(fill_value=3.0 ** 0.5, shape_2d=(3,3))
imaging_data = al.ImagingData(
image=image,
pixel_scales=1.0,
psf=aa.kernel.ones(shape_2d=(3,3)),
exposure_time_map=exposure_time,
background_noise_map=background_noise,
)
assert imaging_data.estimated_noise_map.in_2d == pytest.approx(
2.0 * np.ones((3, 3)), 1e-2
)
def test__image_all_4s__exposure_time_all_1s__no_background__noise_is_all_2s(
self
):
# Imaging (eps) = 4.0
# Background (eps) = 0.0
# Exposure times = 1.0 s
# Imaging (counts) = 4.0
# Background (counts) = 0.0
# Noise (counts) = sqrt(4.0 + 0.0**2) = 2.0
# Noise (eps) = 2.0 / 1.0
image = aa.array.full(fill_value=4.0, shape_2d=(4, 2))
exposure_time = aa.array.ones(shape_2d=(4,2))
background_noise = aa.array.zeros(shape_2d=(4,2))
imaging_data = al.ImagingData(
image=image,
pixel_scales=1.0,
psf=aa.kernel.ones(shape_2d=(3,3)),
exposure_time_map=exposure_time,
background_noise_map=background_noise,
)
assert (
imaging_data.estimated_noise_map.in_2d == 2.0 * np.ones((4, 2))
).all()
def test__all_components_resized__psf_is_not(self):
image = aa.array.manual_2d(array=np.ones((6, 6)), pixel_scales=1.0)
image[21] = 2.0
noise_map_array = aa.array.ones(shape_2d=(6,6))
noise_map_array[21] = 3.0
background_noise_map_array = aa.array.ones(shape_2d=(6,6))
background_noise_map_array[21] = 4.0
exposure_time_map_array = aa.array.ones(shape_2d=(6,6))
exposure_time_map_array[21] = 5.0
background_sky_map_array = aa.array.ones(shape_2d=(6,6))
background_sky_map_array[21] = 6.0
imaging_data = al.ImagingData(
image=image,
pixel_scales=1.0,
psf=aa.kernel.zeros(shape_2d=(3,3)),
noise_map=noise_map_array,
background_noise_map=background_noise_map_array,
exposure_time_map=exposure_time_map_array,
background_sky_map=background_sky_map_array,
def test__convert_from_electrons__image_all_negative_2s__bg_noise_all_1s__exposure_time_all_10s__noise_map_all_1s(
self
):
imaging_data = aa.array.full(fill_value=-2.0, shape_2d=(2,2))
background_noise_map = aa.array.ones(shape_2d=(2,2))
exposure_time_map = aa.array.full(fill_value=10.0, shape_2d=(2,2))
noise_map = al.NoiseMap.from_image_and_background_noise_map(
image=imaging_data,
background_noise_map=background_noise_map,
exposure_time_map=exposure_time_map,
gain=1.0,
convert_from_electrons=True,
)
assert (noise_map.in_2d == np.array([[1.0, 1.0], [1.0, 1.0]])).all()
def test__noise_map_creates_nans_due_to_low_exposure_time__raises_error(self):
image = aa.array.manual_2d(array=np.ones((9, 9)))
psf = aa.kernel.no_blur()
exposure_time_map = aa.array.ones(shape_2d=image.mask.shape
)
background_sky_map = aa.array.ones(shape_2d=image.mask.shape
)
with pytest.raises(exc.DataException):
al.SimulatedImagingData.from_image_and_exposure_arrays(
image=image,
psf=psf,
exposure_time=1.0,
exposure_time_map=exposure_time_map,
background_sky_map=background_sky_map,
add_noise=True,
noise_seed=1,
)
def test__image_all_2s_and_3s__exposure_time_all_1s__noise_map_all_sqrt_2s_and_3s(
self
):
imaging_data = aa.array.manual_2d([[2.0, 2.0], [3.0, 3.0]])
exposure_time_map = aa.array.ones(shape_2d=(2,2))
poisson_noise_map = al.PoissonNoiseMap.from_image_and_exposure_time_map(
image=imaging_data,
exposure_time_map=exposure_time_map,
gain=1.0,
)
assert (
poisson_noise_map.in_2d
== np.array(
[[np.sqrt(2.0), np.sqrt(2.0)], [np.sqrt(3.0), np.sqrt(3.0)]]
)
def test__image_all_1s__exposure_time_all_1s__noise_map_all_1s__gain_is_2__ignores_gain(
self
):
imaging_data = aa.array.ones(shape_2d=(2,2))
exposure_time_map = aa.array.ones(shape_2d=(2,2))
poisson_noise_map = al.PoissonNoiseMap.from_image_and_exposure_time_map(
image=imaging_data,
exposure_time_map=exposure_time_map,
gain=2.0,
)
assert (
poisson_noise_map.in_2d
== np.array(
[[np.sqrt(1.0), np.sqrt(1.0)], [np.sqrt(1.0), np.sqrt(1.0)]]
)