LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
cleanBadPoints.py
Go to the documentation of this file.
1 from __future__ import absolute_import
2 from builtins import range
3 #
4 # LSST Data Management System
5 # Copyright 2008, 2009, 2010 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 
25 
26 import os
27 import numpy as np
28 
29 import lsst.afw.detection as det
30 
31 from . import sipLib as sip
32 
33 
34 def clean(srcMatch, wcs, order=3, nsigma=3):
35  """Remove bad points from srcMatch
36 
37  Input:
38  srcMatch : std::vector<det::SourceMatch>
39  order: Order of polynomial to use in robust fitting
40  nsigma: Sources more than this far away from the robust best fit
41  polynomial are removed
42 
43  Return:
44  std::vector<det::SourceMatch> of the good data points
45  """
46 
47  N = len(srcMatch)
48  catX = np.zeros(N)
49  #catY = np.zeros(N)
50  for i in range(N):
51  x, y = wcs.skyToPixel(srcMatch[i].first.getCoord())
52  catX[i] = x
53  #catY[i] = y
54 
55  # FIXME -- why does this only use X?
56 
57  x = np.array([s.second.getX() for s in srcMatch])
58  dx = x - catX
59  sigma = np.zeros_like(dx) + 0.1
60 
61  idx = indicesOfGoodPoints(x, dx, sigma, order=order, nsigma=nsigma)
62 
63  clean = []
64  for i in idx:
65  clean.append(srcMatch[i])
66  return clean
67 
68 
69 def indicesOfGoodPoints(x, y, s, order=1, nsigma=3, maxiter=100):
70  """Return a list of indices in the range [0, len(x)]
71  of points that lie less than nsigma away from the robust
72  best fit polynomial
73  """
74 
75  # Indices of elements of x sorted in order of increasing value
76  idx = x.argsort()
77  newidx = []
78  for niter in range(maxiter):
79  rx = chooseRx(x, idx, order)
80  ry = chooseRy(y, idx, order)
81  rs = np.ones((len(rx)))
82 
83  lsf = sip.LeastSqFitter1dPoly(list(rx), list(ry), list(rs), order)
84  fit = [lsf.valueAt(value) for value in rx]
85  f = [lsf.valueAt(value) for value in x]
86 
87  sigma = (y-f).std()
88  deviance = np.fabs((y - f) / sigma)
89  newidx = np.flatnonzero(deviance < nsigma)
90 
91  if False:
92  import matplotlib.pyplot as plt
93  plt.plot(x, y, 'ks')
94  plt.plot(rx, ry, 'b-')
95  plt.plot(rx, ry, 'bs')
96  plt.plot(rx, fit, 'ms')
97  plt.plot(rx, fit, 'm-')
98  #plt.plot(x[newidx], y[newidx], 'rs')
99  plt.show()
100 
101  # If we haven't culled any points we're finished cleaning
102  if len(newidx) == len(idx):
103  break
104 
105  # We get here because we either a) stopped finding bad points
106  # or b) ran out of iterations. Either way, we just return our
107  # list of indices of good points.
108  return newidx
109 
110 
111 def chooseRx(x, idx, order):
112  """Create order+1 values of the ordinate based on the median of groups of elements of x"""
113  rSize = len(idx)/float(order+1) # Note, a floating point number
114  rx = np.zeros((order+1))
115 
116  for i in range(order+1):
117  rng = list(range(int(rSize*i), int(rSize*(i+1))))
118  rx[i] = np.mean(x[idx[rng]])
119  return rx
120 
121 
122 def chooseRy(y, idx, order):
123  """Create order+1 values of the ordinate based on the median of groups of elements of y"""
124  rSize = len(idx)/float(order+1) # Note, a floating point number
125  ry = np.zeros((order+1))
126 
127  for i in range(order+1):
128  rng = list(range(int(rSize*i), int(rSize*(i+1))))
129  ry[i] = np.median(y[idx[rng]])
130  return ry
STL namespace.