LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Classes | Functions | Variables
lsst.meas.algorithms.objectSizeStarSelector Namespace Reference

Classes

class  ObjectSizeStarSelectorConfig
 
class  EventHandler
 
class  ObjectSizeStarSelector
 A measurePsfTask star selector. More...
 

Functions

def _assignClusters
 
def _kcenters
 
def _improveCluster
 
def plot
 

Variables

 fig = None
 
 pyplot = None
 

Function Documentation

def lsst.meas.algorithms.objectSizeStarSelector._assignClusters (   yvec,
  centers 
)
private
Return a vector of centerIds based on their distance to the centers

Definition at line 143 of file objectSizeStarSelector.py.

144 def _assignClusters(yvec, centers):
145  """Return a vector of centerIds based on their distance to the centers"""
146  assert len(centers) > 0
147 
148  minDist = numpy.nan*numpy.ones_like(yvec)
149  clusterId = numpy.empty_like(yvec)
150  clusterId.dtype = int # zeros_like(..., dtype=int) isn't in numpy 1.5
151 
152  for i, mean in enumerate(centers):
153  dist = abs(yvec - mean)
154  if i == 0:
155  update = dist == dist # True for all points
156  else:
157  update = dist < minDist
158 
159  minDist[update] = dist[update]
160  clusterId[update] = i
161 
162  return clusterId
def lsst.meas.algorithms.objectSizeStarSelector._improveCluster (   yvec,
  centers,
  clusterId,
  nsigma = 2.0,
  nIteration = 10,
  clusterNum = 0,
  widthStdAllowed = 0.15 
)
private
Improve our estimate of one of the clusters (clusterNum) by sigma-clipping around its median

Definition at line 199 of file objectSizeStarSelector.py.

200 def _improveCluster(yvec, centers, clusterId, nsigma=2.0, nIteration=10, clusterNum=0, widthStdAllowed=0.15):
201  """Improve our estimate of one of the clusters (clusterNum) by sigma-clipping around its median"""
202 
203  nMember = sum(clusterId == clusterNum)
204  if nMember < 5: # can't compute meaningful interquartile range, so no chance of improvement
205  return clusterId
206  for iter in range(nIteration):
207  old_nMember = nMember
208 
209  inCluster0 = clusterId == clusterNum
210  yv = yvec[inCluster0]
211 
212  centers[clusterNum] = numpy.median(yv)
213  stdev = numpy.std(yv)
214 
215  syv = sorted(yv)
216  stdev_iqr = 0.741*(syv[int(0.75*nMember)] - syv[int(0.25*nMember)])
217  median = syv[int(0.5*nMember)]
218 
219  sd = stdev if stdev < stdev_iqr else stdev_iqr
220 
221  if False:
222  print "sigma(iqr) = %.3f, sigma = %.3f" % (stdev_iqr, numpy.std(yv))
223  newCluster0 = abs(yvec - centers[clusterNum]) < nsigma*sd
224  clusterId[numpy.logical_and(inCluster0, newCluster0)] = clusterNum
225  clusterId[numpy.logical_and(inCluster0, numpy.logical_not(newCluster0))] = -1
226 
227  nMember = sum(clusterId == clusterNum)
228  # 'sd < widthStdAllowed * median' prevents too much rejections
229  if nMember == old_nMember or sd < widthStdAllowed * median:
230  break
231 
232  return clusterId
boost::enable_if< typename ExpressionTraits< Scalar >::IsScalar, Scalar >::type sum(Scalar const &scalar)
Definition: operators.h:1250
def lsst.meas.algorithms.objectSizeStarSelector._kcenters (   yvec,
  nCluster,
  useMedian = False,
  widthStdAllowed = 0.15 
)
private
A classic k-means algorithm, clustering yvec into nCluster clusters

Return the set of centres, and the cluster ID for each of the points

If useMedian is true, use the median of the cluster as its centre, rather than
the traditional mean

Serge Monkewitz points out that there other (maybe smarter) ways of seeding the means:
   "e.g. why not use the Forgy or random partition initialization methods"
however, the approach adopted here seems to work well for the particular sorts of things
we're clustering in this application

Definition at line 163 of file objectSizeStarSelector.py.

164 def _kcenters(yvec, nCluster, useMedian=False, widthStdAllowed=0.15):
165  """A classic k-means algorithm, clustering yvec into nCluster clusters
166 
167  Return the set of centres, and the cluster ID for each of the points
168 
169  If useMedian is true, use the median of the cluster as its centre, rather than
170  the traditional mean
171 
172  Serge Monkewitz points out that there other (maybe smarter) ways of seeding the means:
173  "e.g. why not use the Forgy or random partition initialization methods"
174  however, the approach adopted here seems to work well for the particular sorts of things
175  we're clustering in this application
176  """
177 
178  assert nCluster > 0
179 
180  mean0 = sorted(yvec)[len(yvec)//10] # guess
181  delta = mean0 * widthStdAllowed * 2.0
182  centers = mean0 + delta * numpy.arange(nCluster)
183 
184  func = numpy.median if useMedian else numpy.mean
185 
186  clusterId = numpy.zeros_like(yvec) - 1 # which cluster the points are assigned to
187  clusterId.dtype = int # zeros_like(..., dtype=int) isn't in numpy 1.5
188  while True:
189  oclusterId = clusterId
190  clusterId = _assignClusters(yvec, centers)
191 
192  if numpy.all(clusterId == oclusterId):
193  break
194 
195  for i in range(nCluster):
196  centers[i] = func(yvec[clusterId == i])
197 
198  return centers, clusterId
def lsst.meas.algorithms.objectSizeStarSelector.plot (   mag,
  width,
  centers,
  clusterId,
  marker = "o",
  markersize = 2,
  markeredgewidth = 0,
  ltype = '-',
  magType = "model",
  clear = True 
)

Definition at line 234 of file objectSizeStarSelector.py.

235  magType="model", clear=True):
236 
237  global fig
238  if not fig:
239  fig = pyplot.figure()
240  else:
241  if clear:
242  fig.clf()
243 
244  axes = fig.add_axes((0.1, 0.1, 0.85, 0.80));
245 
246  xmin = sorted(mag)[int(0.05*len(mag))]
247  xmax = sorted(mag)[int(0.95*len(mag))]
248 
249  axes.set_xlim(-17.5, -13)
250  axes.set_xlim(xmin - 0.1*(xmax - xmin), xmax + 0.1*(xmax - xmin))
251  axes.set_ylim(0, 10)
252 
253  colors = ["r", "g", "b", "c", "m", "k",]
254  for k, mean in enumerate(centers):
255  if k == 0:
256  axes.plot(axes.get_xlim(), (mean, mean,), "k%s" % ltype)
257 
258  l = (clusterId == k)
259  axes.plot(mag[l], width[l], marker, markersize=markersize, markeredgewidth=markeredgewidth,
260  color=colors[k%len(colors)])
261 
262  l = (clusterId == -1)
263  axes.plot(mag[l], width[l], marker, markersize=markersize, markeredgewidth=markeredgewidth,
264  color='k')
265 
266  if clear:
267  axes.set_xlabel("Instrumental %s mag" % magType)
268  axes.set_ylabel(r"$\sqrt{(I_{xx} + I_{yy})/2}$")
269 
270  return fig
271 
272 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Variable Documentation

lsst.meas.algorithms.objectSizeStarSelector.fig = None

Definition at line 27 of file objectSizeStarSelector.py.

lsst.meas.algorithms.objectSizeStarSelector.pyplot = None

Definition at line 29 of file objectSizeStarSelector.py.