Coverage for src/galsbi/ucat/plugins/galaxy_mag_noise.py: 95%

21 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-13 03:24 +0000

1# Copyright (C) 2019 ETH Zurich, Institute for Particle Physics and Astrophysics 

2 

3""" 

4Created on Aug 2021 

5author: Tomasz Kacprzak 

6""" 

7 

8import numpy as np 

9from cosmic_toolbox import logger 

10from ivy.plugin.base_plugin import BasePlugin 

11 

12LOGGER = logger.get_logger(__file__) 

13SEED_OFFSET_MAG_NOISE = 14301 

14 

15 

16def mag_to_flux(mag): 

17 return 10 ** ((mag + 48.6) / (-2.5)) 

18 

19 

20def flux_to_mag(flux): 

21 return -2.5 * np.log10(flux) - 48.6 

22 

23 

24class Plugin(BasePlugin): 

25 def __call__(self): 

26 par = self.ctx.parameters 

27 

28 np.random.seed(SEED_OFFSET_MAG_NOISE + par.seed) 

29 

30 if "galaxies" in self.ctx and par.noise_const_abs_mag is not None: 

31 for band, mag in self.ctx.galaxies.abs_magnitude_dict.items(): 

32 flux_noisy = np.random.normal( 

33 loc=mag_to_flux(mag), 

34 scale=par.noise_const_abs_mag, 

35 size=len(mag), 

36 ) 

37 mag_noisy = flux_to_mag(flux_noisy) 

38 mag_noisy[np.isnan(mag_noisy)] = np.inf 

39 self.ctx.galaxies.abs_magnitude_dict[band] = mag_noisy 

40 

41 def __str__(self): 

42 return "add noise to app_mags"