Coverage for metadetect / lsst / tests / test_lsst_photometry.py: 28%

59 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-27 01:48 -0800

1""" 

2test using lsst simple sim 

3""" 

4import sys 

5import numpy as np 

6import pytest 

7 

8import logging 

9from metadetect.lsst import photometry as lsst_phot 

10from metadetect.lsst import util 

11from lsst.utils import getPackageDir 

12 

13try: 

14 getPackageDir('descwl_shear_sims') 

15 getPackageDir('descwl_coadd') 

16 skip_tests_on_simulations = False 

17except LookupError: 

18 skip_tests_on_simulations = True 

19 

20logging.basicConfig( 

21 stream=sys.stdout, 

22 level=logging.INFO, 

23) 

24 

25 

26def make_lsst_sim(seed, mag=20, hlr=0.5, bands=None): 

27 import descwl_shear_sims 

28 

29 rng = np.random.RandomState(seed=seed) 

30 coadd_dim = 251 

31 

32 if bands is None: 

33 bands = ['i'] 

34 

35 galaxy_catalog = descwl_shear_sims.galaxies.FixedGalaxyCatalog( 

36 rng=rng, 

37 coadd_dim=coadd_dim, 

38 buff=20, 

39 layout='grid', 

40 mag=mag, 

41 hlr=hlr, 

42 ) 

43 

44 psf = descwl_shear_sims.psfs.make_fixed_psf(psf_type='gauss') 

45 

46 sim_data = descwl_shear_sims.make_sim( 

47 rng=rng, 

48 galaxy_catalog=galaxy_catalog, 

49 coadd_dim=coadd_dim, 

50 g1=0.02, 

51 g2=0.00, 

52 psf=psf, 

53 bands=bands, 

54 ) 

55 return sim_data 

56 

57 

58def do_coadding(rng, sim_data, nowarp): 

59 from descwl_coadd.coadd import make_coadd 

60 from descwl_coadd.coadd_nowarp import make_coadd_nowarp 

61 

62 bands = list(sim_data['band_data'].keys()) 

63 

64 if nowarp: 

65 coadd_data_list = [ 

66 make_coadd_nowarp( 

67 exp=sim_data['band_data'][band][0], 

68 psf_dims=sim_data['psf_dims'], 

69 rng=rng, 

70 remove_poisson=False, 

71 ) 

72 for band in bands 

73 ] 

74 else: 

75 coadd_data_list = [ 

76 make_coadd( 

77 exps=sim_data['band_data'][band], 

78 psf_dims=sim_data['psf_dims'], 

79 rng=rng, 

80 coadd_wcs=sim_data['coadd_wcs'], 

81 coadd_bbox=sim_data['coadd_bbox'], 

82 remove_poisson=False, 

83 ) 

84 for band in bands 

85 ] 

86 

87 return util.extract_multiband_coadd_data(coadd_data_list) 

88 

89 

90@pytest.mark.skipif( 

91 skip_tests_on_simulations, 

92 reason='descwl_shear_sims and/or descwl_coadd not available' 

93) 

94@pytest.mark.parametrize('meas_type', [None, 'wmom', 'ksigma', 'pgauss']) 

95@pytest.mark.parametrize('subtract_sky', [None, False, True]) 

96@pytest.mark.parametrize('nowarp', [False, True]) 

97def test_lsst_photometry_smoke(meas_type, subtract_sky, nowarp): 

98 print('-'*70) 

99 rng = np.random.RandomState(seed=116) 

100 

101 bands = ['r', 'i'] 

102 sim_data = make_lsst_sim(116, bands=bands) 

103 data = do_coadding(rng=rng, sim_data=sim_data, nowarp=nowarp) 

104 

105 config = {} 

106 

107 if subtract_sky is not None: 

108 config['subtract_sky'] = subtract_sky 

109 

110 if meas_type is not None: 

111 config['meas_type'] = meas_type 

112 

113 res = lsst_phot.run_photometry( 

114 mbexp=data['mbexp'], 

115 mfrac_mbexp=data['mfrac_mbexp'], 

116 ormasks=data['ormasks'], 

117 rng=rng, 

118 config=config, 

119 ) 

120 

121 # 5x5 on the grid 

122 assert res.size == 25 

123 

124 if meas_type is None: 

125 front = 'wmom' 

126 else: 

127 front = meas_type 

128 

129 gname = f'{front}_g' 

130 flux_name = f'{front}_band_flux' 

131 assert gname in res.dtype.names 

132 

133 assert np.any(res[f"{front}_flags"] == 0) 

134 assert np.all(res["mfrac"] == 0) 

135 

136 assert len(res[flux_name].shape) == 2 

137 assert res[flux_name].shape[1] == len(bands) 

138 

139 

140if __name__ == '__main__': 

141 for mt in ['pgauss', 'ksigma']: 

142 for nowarp in [True, False]: 

143 test_lsst_photometry_smoke( 

144 meas_type=mt, subtract_sky=False, nowarp=nowarp 

145 )