Skip to content

Commit

Permalink
Separate noises (#254)
Browse files Browse the repository at this point in the history
* Separate the two noises

* Updated tests

* Possibility to choose which noise is drawn

* Update doc and notebook

* Changed lists to tuples

* Fix test

* Fix all tests

* Updated notebooks

* Revert change

* Notebooks
  • Loading branch information
thuiop authored and ismael-mendoza committed Feb 25, 2022
1 parent 6afd759 commit a3de7a6
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 118 deletions.
24 changes: 18 additions & 6 deletions btk/draw_blends.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def __init__(
stamp_size=24,
cpus=1,
verbose=False,
add_noise=True,
add_noise="all",
shifts=None,
indexes=None,
channels_last=False,
Expand All @@ -146,7 +146,11 @@ def __init__(
stamp_size (float): Size of the stamps, in arcseconds
cpus (int): Number of cpus to use; defines the number of minibatches
verbose (bool): Indicates whether additionnal information should be printed
add_noise (bool): Indicates if the blends should be generated with noise
add_noise (str): Indicates if the blends should be generated with noise.
"all" indicates that all the noise should be applied,
"background" adds only the background noise,
"galaxy" only the galaxy noise, and "none" gives noiseless
images.
shifts (list): Contains arbitrary shifts to be applied instead of
random shifts. Must be of length batch_size. Must be used
with indexes. Used mostly for internal testing purposes.
Expand Down Expand Up @@ -416,12 +420,20 @@ def render_blend(self, blend_catalog, psf, filt, survey, seedseq_blend, extra_da
_blend_image += single_image

# add noise.
if self.add_noise:
if self.add_noise in ("galaxy", "all"):
if self.verbose:
print("Noise added to blend image")
print("Galaxy noise added to blend image")
generator = galsim.random.BaseDeviate(seed=seedseq_blend.generate_state(1))
noise = galsim.PoissonNoise(rng=generator, sky_level=mean_sky_level)
_blend_image.addNoise(noise)
galaxy_noise = galsim.PoissonNoise(rng=generator, sky_level=0.0)
_blend_image.addNoise(galaxy_noise)
if self.add_noise in ("background", "all"):
if self.verbose:
print("Background noise added to blend image")
generator = galsim.random.BaseDeviate(seed=seedseq_blend.generate_state(1))
background_noise = galsim.PoissonNoise(rng=generator, sky_level=mean_sky_level)
noise_image = galsim.Image(np.zeros((pix_stamp_size, pix_stamp_size)))
noise_image.addNoise(background_noise)
_blend_image += noise_image

blend_image = _blend_image.array
return blend_image, iso_image
Expand Down
6 changes: 3 additions & 3 deletions docs/source/tutorials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ Now that we have all the objects at our disposal, we can create the DrawBlendsGe
batch_size=8,
stamp_size=stamp_size,
cpus=1,
add_noise=True,
add_noise="all",
seed=seed
)

Expand Down Expand Up @@ -452,7 +452,7 @@ Saving the results can be automatically achieved by providing the save_path argu
batch_size=100,
stamp_size=stamp_size,
cpus=1,
add_noise=True,
add_noise="all",
save_path=save_path
)
meas_generator = btk.measure.MeasureGenerator(btk.measure.sep_measure,draw_generator,save_path=save_path)
Expand Down Expand Up @@ -500,7 +500,7 @@ We can now create the corresponding instance of ``DrawBlendsGenerator``. There i
batch_size=batch_size,
stamp_size=stamp_size,
cpus=1,
add_noise=True,
add_noise="all",
verbose=False,
)

Expand Down
33 changes: 17 additions & 16 deletions notebooks/00-intro.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion notebooks/01a-cosmos_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
" batch_size=batch_size,\n",
" stamp_size=stamp_size,\n",
" cpus=1,\n",
" add_noise=True,\n",
" add_noise=\"all\",\n",
" verbose=False,\n",
" )"
]
Expand Down
85 changes: 42 additions & 43 deletions notebooks/02a-multi-tutorial.ipynb

Large diffs are not rendered by default.

62 changes: 25 additions & 37 deletions notebooks/02b-custom-tutorial.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/test_cosmos.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_cosmos_galaxies():
batch_size=batch_size,
stamp_size=stamp_size,
cpus=1,
add_noise=True,
add_noise="all",
verbose=True,
)

Expand All @@ -51,7 +51,7 @@ def test_cosmos_ext_galaxies():
batch_size=batch_size,
stamp_size=stamp_size,
cpus=1,
add_noise=True,
add_noise="all",
verbose=True,
)

Expand Down
10 changes: 5 additions & 5 deletions tests/test_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def get_draw_generator(
batch_size=8,
cpus=1,
add_noise=True,
add_noise="all",
fixed_parameters=False,
sampling_function=None,
):
Expand Down Expand Up @@ -107,9 +107,9 @@ def match_blend_images_default(blend_images):
the mean and std values in the batch. This is compared to the values
measured a priori for the default input settings.
"""
test_batch_max = np.array([175.012, 1601.121, 7951.862, 11260.612, 9006.189, 5224.317])
test_batch_mean = 6.116279698020711
test_batch_std = 403.68242694018846
test_batch_max = np.array([172.012, 1372.121, 7881.862, 10346.612, 9120.189, 4965.317])
test_batch_mean = 6.123635022094785
test_batch_std = 403.7842461889957
batch_max = blend_images.max(axis=(0, 2, 3))
batch_mean = blend_images.mean()
batch_std = blend_images.std()
Expand Down Expand Up @@ -138,7 +138,7 @@ def match_background_noise(blend_images):
the r band. This is compared to the values measured a priori for the
default input settings.
"""
test_batch_noise = 128667.07421875
test_batch_noise = 128664.3287115097
batch_noise = np.var(blend_images[1, 2, 0:32, 0:32])
np.testing.assert_almost_equal(
batch_noise,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_group_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ def test_group_sampling():
rel_diff1 = (test_batch_max - batch_max) / test_batch_max
rel_diff2 = (batch_mean - test_batch_mean) / test_batch_mean
rel_diff3 = (batch_std - test_batch_std) / test_batch_std
assert np.all(rel_diff1 <= 0.12)
assert np.all(rel_diff2 <= 0.12)
assert np.all(rel_diff3 <= 0.12)
assert np.all(rel_diff1 <= 0.15)
assert np.all(rel_diff2 <= 0.15)
assert np.all(rel_diff3 <= 0.15)
2 changes: 1 addition & 1 deletion tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_measure_kwargs(mock_show):
)
_, _, results = next(metrics_generator)
average_precision = auc(results, "sep_measure", 2, plot=True)
assert average_precision == 0.375
assert average_precision == 0.4375


def test_detection_eff_matrix():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mr.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_multiresolution(mock_show):
stamp_size = 24.0
batch_size = 8
cpus = 1
add_noise = True
add_noise = "all"
surveys = get_surveys(["Rubin", "HSC"])

catalog = CatsimCatalog.from_file(catalog_name)
Expand Down

0 comments on commit a3de7a6

Please sign in to comment.