Coverage for src/galsbi/ucat/plugins/sample_galaxies_morph.py: 97%

38 statements  

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

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

2 

3""" 

4Created 2021 

5author: Tomasz Kacprzak 

6""" 

7 

8import numpy as np 

9import PyCosmo 

10from cosmic_toolbox import logger 

11from ivy.plugin.base_plugin import BasePlugin 

12from galsbi.ucat.galaxy_population_models.galaxy_light_profile import ( 

13 sample_sersic_for_galaxy_type, 

14) 

15from galsbi.ucat.galaxy_population_models.galaxy_shape import ( 

16 sample_ellipticities_for_galaxy_type, 

17) 

18from galsbi.ucat.galaxy_population_models.galaxy_size import sample_r50_for_galaxy_type 

19 

20LOGGER = logger.get_logger(__file__) 

21SEED_OFFSET_LUMFUN = 123491 

22 

23 

24class Plugin(BasePlugin): 

25 """ 

26 Generate a random catalog of galaxies with magnitudes in multiple bands. 

27 """ 

28 

29 def __call__(self): 

30 par = self.ctx.parameters 

31 

32 # cosmology 

33 cosmo = PyCosmo.build() 

34 cosmo.set(h=par.h, omega_m=par.omega_m) 

35 

36 # Initialize galaxy catalog 

37 assert hasattr( 

38 self.ctx, "galaxies" 

39 ), "galaxy catalog not initialized, perhaps run sample_galaxies_photo first" 

40 assert hasattr( 

41 self.ctx, "pixels" 

42 ), "pixels not initialized, perhaps run sample_galaxies_photo first" 

43 

44 # add new columns 

45 morph_columns = ["sersic_n", "int_r50", "int_e1", "int_e2"] 

46 self.ctx.galaxies.columns += morph_columns 

47 n_gal_total = len(self.ctx.galaxies.z) 

48 for c in morph_columns: 

49 setattr( 

50 self.ctx.galaxies, c, np.zeros(n_gal_total, dtype=par.catalog_precision) 

51 ) 

52 

53 # loop over luminosity functions 

54 for j, g in enumerate(par.galaxy_types): 

55 for i in range(len(self.ctx.pixels)): 

56 # get info about the sample 

57 select_type = self.ctx.galaxies.galaxy_type == j 

58 n_gal_ = np.count_nonzero(select_type) 

59 

60 if n_gal_ == 0: 

61 continue 

62 

63 # sersic index 

64 np.random.seed( 

65 par.seed + self.ctx.pixels[i] + par.gal_sersic_seed_offset 

66 ) 

67 sersic_n = sample_sersic_for_galaxy_type( 

68 n_gal=n_gal_, 

69 galaxy_type=g, 

70 app_mag=self.ctx.galaxies.int_magnitude_dict[par.reference_band][ 

71 select_type 

72 ], 

73 par=par, 

74 z=self.ctx.galaxies.z[select_type], 

75 ) 

76 

77 # intrinsic size 

78 int_r50 = sample_r50_for_galaxy_type( 

79 z=self.ctx.galaxies.z[select_type], 

80 abs_mag=self.ctx.galaxies.abs_magnitude_dict[par.reference_band][ 

81 select_type 

82 ], 

83 cosmo=cosmo, 

84 par=par, 

85 galaxy_type=g, 

86 ) 

87 

88 # intrinsic ellipticity 

89 np.random.seed( 

90 par.seed + self.ctx.pixels[i] + par.gal_ellipticities_seed_offset 

91 ) 

92 ( 

93 int_e1, 

94 int_e2, 

95 ) = sample_ellipticities_for_galaxy_type( 

96 n_gal=n_gal_, galaxy_type=g, par=par 

97 ) 

98 

99 # append lists 

100 self.ctx.galaxies.int_r50[select_type] = int_r50 

101 self.ctx.galaxies.sersic_n[select_type] = sersic_n 

102 self.ctx.galaxies.int_e1[select_type] = int_e1 

103 self.ctx.galaxies.int_e2[select_type] = int_e2 

104 

105 def __str__(self): 

106 return "sample gal morph "