Skip to content
Merged
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
8 changes: 5 additions & 3 deletions autoarray/dataset/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,13 @@ def poisson_noise_via_data_eps_from(data_eps, exposure_time_map, seed=-1):
"""
setup_random_seed(seed)

image_counts = np.multiply(data_eps.array, exposure_time_map.array)
return data_eps - np.divide(
np.random.poisson(image_counts, data_eps.shape), exposure_time_map.array
image_counts = data_eps.array * exposure_time_map.array
noisy_eps_array = (
np.random.poisson(image_counts, data_eps.shape) / exposure_time_map.array
)

return data_eps.with_new_array(noisy_eps_array) - data_eps


def data_eps_with_poisson_noise_added(data_eps, exposure_time_map, seed=-1):
"""
Expand Down
14 changes: 8 additions & 6 deletions test_autoarray/dataset/imaging/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ def test__via_image_from__psf_off__include_poisson_noise_in_noise_map(
dataset = simulator.via_image_from(image=image)

assert dataset.data.native == pytest.approx(
np.array([[1.05, 1.3, 1.25], [1.05, 2.1, 1.2], [1.05, 1.3, 1.15]]), 1e-2
np.array([[0.95, 0.7, 0.75], [0.95, 1.9, 0.8], [0.95, 0.7, 0.85]]), 1e-2
)

assert dataset.noise_map.native == pytest.approx(
np.array([[0.229, 0.255, 0.25], [0.229, 0.324, 0.245], [0.229, 0.255, 0.240]]),
np.array(
[[0.218, 0.187, 0.194], [0.218, 0.308, 0.2], [0.218, 0.187, 0.206]]
),
1e-2,
)

Expand Down Expand Up @@ -85,10 +87,10 @@ def test__via_image_from__psf_off__background_sky_on(image_central_delta_3x3):

assert (
dataset.data.native
== np.array([[1.0, 5.0, 4.0], [1.0, 2.0, 1.0], [5.0, 2.0, 7.0]])
== np.array([[-1.0, -5.0, -4.0], [-1.0, 0.0, -1.0], [-5.0, -2.0, -7.0]])
).all()

assert dataset.noise_map.native[0, 0] == pytest.approx(4.12310, 1.0e-4)
assert dataset.noise_map.native[0, 0] == pytest.approx(3.87298, 1.0e-4)


def test__via_image_from__psf_on__psf_blurs_image_with_edge_trimming(
Expand Down Expand Up @@ -143,7 +145,7 @@ def test__via_image_from__psf_on__disable_poisson_noise_in_data(

assert dataset.noise_map.native == pytest.approx(
np.array(
[[0.0, 0.22912, 0.0], [0.25495, 0.34278, 0.22912], [0.0, 0.229128, 0.0]]
[[0.0, 0.21794, 0.0], [0.18708, 0.28723, 0.21794], [0.0, 0.21794, 0.0]]
),
1e-2,
)
Expand All @@ -169,5 +171,5 @@ def test__via_image_from__psf_on__psf_and_noise_both_on(image_central_delta_3x3)
dataset = simulator.via_image_from(image=image)

assert dataset.data.native == pytest.approx(
np.array([[4.1, 6.65, 4.45], [6.15, 8.15, 6.5], [4.1, 6.7, 4.25]]), 1e-2
np.array([[3.9, 5.35, 3.55], [5.85, 7.85, 5.5], [3.9, 5.3, 3.75]]), 1e-2
)
12 changes: 6 additions & 6 deletions test_autoarray/dataset/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def test__poisson_noise_from_data():
)

assert (
poisson_noise.native == np.array([[(10.0 - 9.0), 0], [0, (10.0 - 6.0)]])
poisson_noise.native == np.array([[(9.0 - 10.0), 0], [0, (6.0 - 10.0)]])
).all()

data = aa.Array2D.full(fill_value=10.0, shape_native=(2, 2), pixel_scales=1.0)
Expand All @@ -623,7 +623,7 @@ def test__poisson_noise_from_data():
)

# Use known noise_map_1d map for given seed.
assert (poisson_noise.native == np.array([[1, 4], [3, 1]])).all()
assert (poisson_noise.native == np.array([[-1, -4], [-3, -1]])).all()

data = aa.Array2D.no_mask(
values=[[10000000.0, 0.0], [0.0, 10000000.0]], pixel_scales=1.0
Expand All @@ -634,7 +634,7 @@ def test__poisson_noise_from_data():
data_eps=data, exposure_time_map=exposure_time_map, seed=1
)

assert (poisson_noise.native == np.array([[743, 0], [0, 3783]])).all()
assert (poisson_noise.native == np.array([[-743, 0], [0, -3783]])).all()


def test__data_with_poisson_noised_added():
Expand All @@ -654,7 +654,7 @@ def test__data_with_poisson_noised_added():
data_eps=data, exposure_time_map=exposure_time_map, seed=1
)

assert (data_with_poisson_noise.native == np.array([[11, 0], [0, 14]])).all()
assert (data_with_poisson_noise.native == np.array([[9, 0], [0, 6]])).all()

data = aa.Array2D.full(fill_value=10.0, shape_native=(2, 2), pixel_scales=1.0)

Expand All @@ -663,7 +663,7 @@ def test__data_with_poisson_noised_added():
data_eps=data, exposure_time_map=exposure_time_map, seed=1
)

assert (data_with_poisson_noise.native == np.array([[11, 14], [13, 11]])).all()
assert (data_with_poisson_noise.native == np.array([[9, 6], [7, 9]])).all()

data = aa.Array2D.no_mask(
values=[[10000000.0, 0.0], [0.0, 10000000.0]], pixel_scales=1.0
Expand All @@ -676,7 +676,7 @@ def test__data_with_poisson_noised_added():
)

assert (
data_with_poisson_noise.native == np.array([[10000743, 0.0], [0.0, 10003783.0]])
data_with_poisson_noise.native == np.array([[9999257, 0.0], [0.0, 9996217.0]])
).all()


Expand Down
Loading