Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import operator 

2 

3import astropy.units as u 

4import numpy as np 

5import treecorr 

6 

7from lsst.faro.utils.coord_util import averageRaFromCat, averageDecFromCat 

8 

9__all__ = ("correlation_function_ellipticity_from_matches", "correlation_function_ellipticity", 

10 "select_bin_from_corr", "medianEllipticity1ResidualsFromCat", 

11 "medianEllipticity2ResidualsFromCat") 

12 

13 

14def correlation_function_ellipticity_from_matches(matches, **kwargs): 

15 """Compute shear-shear correlation function for ellipticity residual from a 'MatchedMultiVisitDataset' object. 

16 Convenience function for calling correlation_function_ellipticity. 

17 Parameters 

18 ---------- 

19 matches : `lsst.verify.Blob` 

20 - The matched catalogs to analyze. 

21 Returns 

22 ------- 

23 r, xip, xip_err : each a np.array(dtype=float) 

24 - The bin centers, two-point correlation, and uncertainty. 

25 """ 

26 ra = matches.aggregate(averageRaFromCat) * u.radian 

27 dec = matches.aggregate(averageDecFromCat) * u.radian 

28 

29 e1_res = matches.aggregate(medianEllipticity1ResidualsFromCat) 

30 e2_res = matches.aggregate(medianEllipticity2ResidualsFromCat) 

31 

32 return correlation_function_ellipticity(ra, dec, e1_res, e2_res, **kwargs) 

33 

34 

35def correlation_function_ellipticity(ra, dec, e1_res, e2_res, 

36 nbins=20, min_sep=0.25, max_sep=20, 

37 sep_units='arcmin', verbose=False, 

38 bin_slop=None): 

39 """Compute shear-shear correlation function from ra, dec, g1, g2. 

40 Default parameters for nbins, min_sep, max_sep chosen to cover 

41 an appropriate range to calculate TE1 (<=1 arcmin) and TE2 (>=5 arcmin). 

42 Parameters 

43 ---------- 

44 ra : numpy.array 

45 Right ascension of points [radians] 

46 dec : numpy.array 

47 Declination of points [radians] 

48 e1_res : numpy.array 

49 Residual ellipticity 1st component 

50 e2_res : numpy.array 

51 Residual ellipticity 2nd component 

52 nbins : float, optional 

53 Number of bins over which to analyze the two-point correlation 

54 min_sep : float, optional 

55 Minimum separation over which to analyze the two-point correlation 

56 max_sep : float, optional 

57 Maximum separation over which to analyze the two-point correlation 

58 sep_units : str, optional 

59 Specify the units of min_sep and max_sep 

60 verbose : bool 

61 Request verbose output from `treecorr`. 

62 verbose=True will use verbose=2 for `treecorr.GGCorrelation`. 

63 bin_slop : float 

64 If specified, this allows tuning of how `treecorr` chooses bins. 

65 The value should be between 0 and 1. Higher values allow more 

66 slop, but are faster. If not specified, treecorr will compute 

67 an appropriate value. For non-zero values, details of results 

68 may not agree between runs on different architectures. 

69 Returns 

70 ------- 

71 r, xip, xip_err : each a np.array(dtype=float) 

72 - The bin centers, two-point correlation, and uncertainty. 

73 """ 

74 # Translate to 'verbose_level' here to refer to the integer levels in TreeCorr 

75 # While 'verbose' is more generically what is being passed around 

76 # for verbosity within 'validate_drp' 

77 if verbose: 

78 verbose_level = 2 

79 else: 

80 verbose_level = 0 

81 

82 catTree = treecorr.Catalog(ra=ra, dec=dec, g1=e1_res, g2=e2_res, 

83 dec_units='radian', ra_units='radian') 

84 if bin_slop is not None: 

85 gg = treecorr.GGCorrelation(nbins=nbins, min_sep=min_sep, max_sep=max_sep, 

86 sep_units=sep_units, verbose=verbose_level, 

87 bin_slop=bin_slop) 

88 else: 

89 gg = treecorr.GGCorrelation(nbins=nbins, min_sep=min_sep, max_sep=max_sep, 

90 sep_units=sep_units, 

91 verbose=verbose_level) 

92 gg.process(catTree) 

93 r = np.exp(gg.meanlogr) * u.arcmin 

94 xip = gg.xip * u.Unit('') 

95 # FIXME: Remove treecorr < 4 support 

96 try: 

97 # treecorr > 4 

98 xip_err = np.sqrt(gg.varxip) * u.Unit('') 

99 except AttributeError: 

100 # treecorr < 4 

101 xip_err = np.sqrt(gg.varxi) * u.Unit('') 

102 

103 return (r, xip, xip_err) 

104 

105 

106def select_bin_from_corr(r, xip, xip_err, radius=1*u.arcmin, operator=operator.le): 

107 """Aggregate measurements for r less than (or greater than) radius. 

108 Returns aggregate measurement for all entries where operator(r, radius). 

109 E.g., 

110 * Passing radius=5, operator=operator.le will return averages for r<=5 

111 * Passing radius=2, operator=operator.gt will return averages for r >2 

112 Written with the use of correlation functions in mind, thus the naming 

113 but generically just returns averages of the arrays xip and xip_err 

114 where the condition is satsified 

115 Parameters 

116 ---------- 

117 r : numpy.array 

118 radius 

119 xip : numpy.array 

120 correlation 

121 xip_err : numpy.array 

122 correlation uncertainty 

123 operator : Operation in the 'operator' module: le, ge, lt, gt 

124 Returns 

125 ------- 

126 avg_xip, avg_xip_err : (float, float) 

127 """ 

128 w, = np.where(operator(r, radius)) 

129 

130 avg_xip = np.average(xip[w]) 

131 avg_xip_err = np.average(xip_err[w]) 

132 

133 return avg_xip, avg_xip_err 

134 

135 

136def medianEllipticity1ResidualsFromCat(cat): 

137 """Compute the median real ellipticty residuals from a catalog of measurements. 

138 Parameters 

139 ---------- 

140 cat : collection 

141 Object with .get method for 'e1', 'psf_e1' that returns radians. 

142 Returns 

143 ------- 

144 e1_median : `float` 

145 Median imaginary ellipticity residual. 

146 """ 

147 e1_median = np.median(cat.get('e1') - cat.get('psf_e1')) 

148 return e1_median 

149 

150 

151def medianEllipticity2ResidualsFromCat(cat): 

152 """Compute the median imaginary ellipticty residuals from a catalog of measurements. 

153 Parameters 

154 ---------- 

155 cat : collection 

156 Object with .get method for 'e2', 'psf_e2' that returns radians. 

157 Returns 

158 ------- 

159 e2_median : `float` 

160 Median imaginary ellipticity residual. 

161 """ 

162 e2_median = np.median(cat.get('e2') - cat.get('psf_e2')) 

163 return e2_median