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

40 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-03 17:18 +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 

12 

13from galsbi.ucat.galaxy_population_models.galaxy_light_profile import ( 

14 sample_sersic_for_galaxy_type, 

15) 

16from galsbi.ucat.galaxy_population_models.galaxy_shape import ( 

17 sample_ellipticities_for_galaxy_type, 

18) 

19from galsbi.ucat.galaxy_population_models.galaxy_size import sample_r50_for_galaxy_type 

20 

21LOGGER = logger.get_logger(__file__) 

22SEED_OFFSET_LUMFUN = 123491 

23 

24 

25class Plugin(BasePlugin): 

26 """ 

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

28 """ 

29 

30 def __call__(self): 

31 par = self.ctx.parameters 

32 

33 # cosmology 

34 cosmo = PyCosmo.build() 

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

36 

37 # Initialize galaxy catalog 

38 assert hasattr( 

39 self.ctx, "galaxies" 

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

41 assert hasattr( 

42 self.ctx, "pixels" 

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

44 

45 # add new columns 

46 morph_columns = [ 

47 "sersic_n", 

48 "int_r50", 

49 "int_r50_arcsec", 

50 "int_r50_phys", 

51 "int_e1", 

52 "int_e2", 

53 ] 

54 self.ctx.galaxies.columns += morph_columns 

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

56 for c in morph_columns: 

57 setattr( 

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

59 ) 

60 

61 # loop over luminosity functions 

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

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

64 # get info about the sample 

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

66 n_gal_ = np.count_nonzero(select_type) 

67 

68 if n_gal_ == 0: 

69 continue 

70 

71 # sersic index 

72 np.random.seed( 

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

74 ) 

75 sersic_n = sample_sersic_for_galaxy_type( 

76 n_gal=n_gal_, 

77 galaxy_type=g, 

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

79 select_type 

80 ], 

81 par=par, 

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

83 ) 

84 

85 # intrinsic size 

86 int_r50, int_r50_arcsec, int_r50_phys = sample_r50_for_galaxy_type( 

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

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

89 select_type 

90 ], 

91 cosmo=cosmo, 

92 par=par, 

93 galaxy_type=g, 

94 ) 

95 

96 # intrinsic ellipticity 

97 np.random.seed( 

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

99 ) 

100 ( 

101 int_e1, 

102 int_e2, 

103 ) = sample_ellipticities_for_galaxy_type( 

104 n_gal=n_gal_, galaxy_type=g, par=par 

105 ) 

106 

107 # append lists 

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

109 self.ctx.galaxies.int_r50_arcsec[select_type] = int_r50_arcsec 

110 self.ctx.galaxies.int_r50_phys[select_type] = int_r50_phys 

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

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

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

114 

115 def __str__(self): 

116 return "sample gal morph "