Coverage for python/lsst/faro/utils/tex.py : 28%

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
3import astropy.units as u
4import numpy as np
5import treecorr
7from lsst.faro.utils.coord_util import averageRaFromCat, averageDecFromCat
9__all__ = ("correlation_function_ellipticity_from_matches", "correlation_function_ellipticity",
10 "select_bin_from_corr", "medianEllipticity1ResidualsFromCat",
11 "medianEllipticity2ResidualsFromCat")
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
29 e1_res = matches.aggregate(medianEllipticity1ResidualsFromCat)
30 e2_res = matches.aggregate(medianEllipticity2ResidualsFromCat)
32 return correlation_function_ellipticity(ra, dec, e1_res, e2_res, **kwargs)
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 brute=False):
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 brute : bool
64 Use burte force mechanism. This is very slow, but will prevent
65 cross platform inconsistencies due to numerical/digital noise
66 Returns
67 -------
68 r, xip, xip_err : each a np.array(dtype=float)
69 - The bin centers, two-point correlation, and uncertainty.
70 """
71 # Translate to 'verbose_level' here to refer to the integer levels in TreeCorr
72 # While 'verbose' is more generically what is being passed around
73 # for verbosity within 'validate_drp'
74 if verbose:
75 verbose_level = 2
76 else:
77 verbose_level = 0
79 catTree = treecorr.Catalog(ra=ra, dec=dec, g1=e1_res, g2=e2_res,
80 dec_units='radian', ra_units='radian')
81 gg = treecorr.GGCorrelation(nbins=nbins, min_sep=min_sep, max_sep=max_sep,
82 sep_units=sep_units,
83 verbose=verbose_level,
84 brute=brute)
85 gg.process(catTree)
86 r = np.exp(gg.meanlogr) * u.arcmin
87 xip = gg.xip * u.Unit('')
88 # FIXME: Remove treecorr < 4 support
89 try:
90 # treecorr > 4
91 xip_err = np.sqrt(gg.varxip) * u.Unit('')
92 except AttributeError:
93 # treecorr < 4
94 xip_err = np.sqrt(gg.varxi) * u.Unit('')
96 return (r, xip, xip_err)
99def select_bin_from_corr(r, xip, xip_err, radius=1*u.arcmin, operator=operator.le):
100 """Aggregate measurements for r less than (or greater than) radius.
101 Returns aggregate measurement for all entries where operator(r, radius).
102 E.g.,
103 * Passing radius=5, operator=operator.le will return averages for r<=5
104 * Passing radius=2, operator=operator.gt will return averages for r >2
105 Written with the use of correlation functions in mind, thus the naming
106 but generically just returns averages of the arrays xip and xip_err
107 where the condition is satsified
108 Parameters
109 ----------
110 r : numpy.array
111 radius
112 xip : numpy.array
113 correlation
114 xip_err : numpy.array
115 correlation uncertainty
116 operator : Operation in the 'operator' module: le, ge, lt, gt
117 Returns
118 -------
119 avg_xip, avg_xip_err : (float, float)
120 """
121 w, = np.where(operator(r, radius))
123 avg_xip = np.average(xip[w])
124 avg_xip_err = np.average(xip_err[w])
126 return avg_xip, avg_xip_err
129def medianEllipticity1ResidualsFromCat(cat):
130 """Compute the median real ellipticty residuals from a catalog of measurements.
131 Parameters
132 ----------
133 cat : collection
134 Object with .get method for 'e1', 'psf_e1' that returns radians.
135 Returns
136 -------
137 e1_median : `float`
138 Median imaginary ellipticity residual.
139 """
140 e1_median = np.median(cat.get('e1') - cat.get('psf_e1'))
141 return e1_median
144def medianEllipticity2ResidualsFromCat(cat):
145 """Compute the median imaginary ellipticty residuals from a catalog of measurements.
146 Parameters
147 ----------
148 cat : collection
149 Object with .get method for 'e2', 'psf_e2' that returns radians.
150 Returns
151 -------
152 e2_median : `float`
153 Median imaginary ellipticity residual.
154 """
155 e2_median = np.median(cat.get('e2') - cat.get('psf_e2'))
156 return e2_median