From 210cd827ae4cc0022bad1a38f4c115223b348b34 Mon Sep 17 00:00:00 2001 From: Luca Pagano Date: Thu, 21 Nov 2024 10:35:59 +0100 Subject: [PATCH] Change the default behavior of Simulation.add_noise() (#349) * Simulation.add_noise uses self.random as default * CHANGELOG updated --- CHANGELOG.md | 4 +++- docs/source/map_scanning.rst | 2 +- docs/source/noise.rst | 2 +- litebird_sim/simulations.py | 11 +++++++---- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1356e5c..4b2ce304 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,12 @@ - Make the code compatible with Python 3.12 [#332](https://github.com/litebird/litebird_sim/pull/332) -- plot_fp.py which visualizes focal plane and `DetectorInfo` is implemented. +- plot_fp.py which visualizes focal plane and `DetectorInfo` is implemented. Also it can generate a dector list file by clicking visualized detectors. The function is executable by: `python -m litebird_sim.plot_fp` [#345](https://github.com/litebird/litebird_sim/pull/345) +- Simulation.add_noise() uses self.random as default random number generator [#349](https://github.com/litebird/litebird_sim/pull/349) + # Version 0.13.0 - **Breaking change**: new API for pointing computation [#319](https://github.com/litebird/litebird_sim/pull/319). Here is a in-depth list of all the breaking changes in this PR: diff --git a/docs/source/map_scanning.rst b/docs/source/map_scanning.rst index 8f7424ab..d82cdb58 100644 --- a/docs/source/map_scanning.rst +++ b/docs/source/map_scanning.rst @@ -174,7 +174,7 @@ transparent: sim.fill_tods(sky_signal) - sim.add_noise(noise_type='white', random=sim.random) + sim.add_noise(noise_type='white') for i in range(5): print(f"{sim.observations[0].tod[0][i]:.5e}") diff --git a/docs/source/noise.rst b/docs/source/noise.rst index 5cc86214..0b7c376f 100644 --- a/docs/source/noise.rst +++ b/docs/source/noise.rst @@ -241,7 +241,7 @@ the interface is simplified. sim.create_observations(detectors=det) - sim.add_noise(noise_type='one_over_f', random=sim.random) + sim.add_noise(noise_type='one_over_f') for i in range(5): print(f"{sim.observations[0].tod[0][i]:.5e}") diff --git a/litebird_sim/simulations.py b/litebird_sim/simulations.py index 5085cac1..883c50c2 100644 --- a/litebird_sim/simulations.py +++ b/litebird_sim/simulations.py @@ -1548,7 +1548,7 @@ def apply_quadratic_nonlin( @_profile def add_noise( self, - random: np.random.Generator, + random: Union[np.random.Generator, None] = None, noise_type: str = "one_over_f", component: str = "tod", append_to_report: bool = True, @@ -1558,11 +1558,14 @@ def add_noise( This method must be called after having set the instrument, the list of detectors to simulate through calls to :meth:`.set_instrument` and :meth:`.add_detector`. - The parameter `random` must be specified and must be a random number - generator thatimplements the ``normal`` method. You should typically - use the `random` field of a :class:`.Simulation` object for this. + The parameter `random` can be specified as a random number + generator that implements the ``normal`` method. As default it uses + the `random` field of a :class:`.Simulation` object for this. """ + if random is None: + random = self.random + add_noise_to_observations( observations=self.observations, noise_type=noise_type,