Coverage for metadetect / tests / test_shearpos.py: 11%

56 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-04 17:37 +0000

1""" 

2test with super simple sim. The purpose here is not 

3to make sure it gets the right answer or anything, just 

4to test all the moving parts 

5""" 

6import numpy as np 

7import ngmix 

8import galsim 

9from metadetect import shearpos 

10 

11 

12def test_shear_pos(show=False): 

13 """ 

14 test shearing and unshearing positions 

15 """ 

16 

17 step = 0.10 

18 

19 dims = 100, 100 

20 im = np.zeros(dims) 

21 cen = (np.array(dims) - 1)/2 

22 jacobian = ngmix.Jacobian( 

23 row=cen[0], 

24 col=cen[1], 

25 dudrow=0.263, 

26 dudcol=-0.01, 

27 dvdrow=+0.01, 

28 dvdcol=0.263, 

29 ) 

30 

31 obs = ngmix.Observation( 

32 im, 

33 jacobian=jacobian, 

34 ) 

35 

36 shears = ['noshear', '1p', '1m', '2p', '2m'] 

37 

38 rows, cols = np.mgrid[ 

39 :dims[0]:5, 

40 :dims[1]:5, 

41 ] 

42 rows = rows.ravel() 

43 cols = cols.ravel() 

44 

45 for sstr in shears: 

46 srows, scols = shearpos.shear_positions_obs( 

47 rows, 

48 cols, 

49 sstr, 

50 obs, 

51 step=step, 

52 ) 

53 

54 if show: 

55 _show_pos(rows, cols, srows, scols, title=sstr) 

56 

57 crows, ccols = shearpos.unshear_positions_obs( 

58 srows, 

59 scols, 

60 sstr, 

61 obs, 

62 step=step, 

63 ) 

64 

65 if show: 

66 _show_pos(rows, cols, crows, ccols, title=sstr+' unsheared') 

67 

68 assert np.allclose(rows, crows), 'checking rows inverse works' 

69 assert np.allclose(cols, ccols), 'checking cols inverse works' 

70 

71 

72def test_shear_pos_image(show=False): 

73 """ 

74 test shearing and unshearing positions against the galsim 

75 shearing of an image 

76 """ 

77 

78 step = 0.10 

79 

80 dims = 100, 100 

81 im = np.zeros(dims) 

82 cen = (np.array(dims) - 1)/2 

83 jacobian = ngmix.Jacobian( 

84 row=cen[0], 

85 col=cen[1], 

86 dudrow=0.263, 

87 dudcol=-0.01, 

88 dvdrow=+0.01, 

89 dvdcol=0.263, 

90 ) 

91 gs_wcs = jacobian.get_galsim_wcs() 

92 

93 obs = ngmix.Observation( 

94 im, 

95 jacobian=jacobian, 

96 ) 

97 

98 # the single pixel with a non-zero value 

99 row, col = 35, 15 

100 with obs.writeable(): 

101 obs.image[row, col] = 1 

102 

103 gsim = galsim.Image(obs.image, wcs=gs_wcs) 

104 ii = galsim.InterpolatedImage(gsim, x_interpolant='lanczos15') 

105 

106 shears = ['1p', '1m', '2p', '2m'] 

107 for sstr in shears: 

108 gs = shearpos.get_galsim_shear(sstr, step) 

109 

110 ii_sheared = ii.shear(g1=gs.g1, g2=gs.g2) 

111 gsim_sheared = gsim.copy() 

112 ii_sheared.drawImage(image=gsim_sheared) 

113 

114 srow, scol = shearpos.shear_positions_obs( 

115 row, 

116 col, 

117 sstr, 

118 obs, 

119 step=step, 

120 ) 

121 

122 if show: 

123 import images 

124 images.view_mosaic([obs.image, gsim_sheared.array]) 

125 

126 irow = np.rint(srow[0]).astype('i4') 

127 icol = np.rint(scol[0]).astype('i4') 

128 

129 maxval = gsim_sheared.array.max() 

130 smaxval = gsim_sheared.array[irow, icol] 

131 

132 assert smaxval == maxval, 'checking sheared position against image' 

133 

134 

135def _show_pos(rows, cols, srows, scols, **kw): 

136 import biggles 

137 

138 plt = biggles.plot( 

139 rows, 

140 cols, 

141 visible=False, 

142 **kw 

143 ) 

144 biggles.plot( 

145 srows, 

146 scols, 

147 color='red', 

148 plt=plt, 

149 )