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

20 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_Z_NOISE = 14302 

14 

15 

16def add_z_noise(z, delta_z, frac_outliers): 

17 sig = delta_z * (1 + z) 

18 z_noise = np.random.normal(z, sig, size=len(z)) 

19 

20 n_outliers = int(len(z) * frac_outliers) 

21 ind_outliers = np.random.choice(len(z), n_outliers) 

22 z_noise[ind_outliers] = np.random.uniform(low=0, high=z.max(), size=n_outliers) 

23 return z_noise 

24 

25 

26class Plugin(BasePlugin): 

27 def __call__(self): 

28 par = self.ctx.parameters 

29 np.random.seed(SEED_OFFSET_Z_NOISE) 

30 

31 if "galaxies" in self.ctx: 

32 self.ctx.galaxies.z_noisy = add_z_noise( 

33 z=self.ctx.galaxies.z, 

34 delta_z=par.noise_z_sigma, 

35 frac_outliers=par.noise_z_outlier_fraction, 

36 ) 

37 

38 def __str__(self): 

39 return "add noise to redshift"