LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 130 of file objectSizeStarSelector.py.

131 def _assignClusters(yvec, centers):
132  """Return a vector of centerIds based on their distance to the centers"""
133  assert len(centers) > 0
134 
135  minDist = numpy.nan*numpy.ones_like(yvec)
136  clusterId = numpy.empty_like(yvec)
137  clusterId.dtype = int # zeros_like(..., dtype=int) isn't in numpy 1.5
138 
139  for i, mean in enumerate(centers):
140  dist = abs(yvec - mean)
141  if i == 0:
142  update = dist == dist # True for all points
143  else:
144  update = dist < minDist
145 
146  minDist[update] = dist[update]
147  clusterId[update] = i
148 
149  return clusterId
def lsst.meas.algorithms.objectSizeStarSelector._improveCluster (   yvec,
  centers,
  clusterId,
  nsigma = 2.0,
  nIteration = 10,
  clusterNum = 0 
)
private
Improve our estimate of one of the clusters (clusterNum) by sigma-clipping around its median

Definition at line 185 of file objectSizeStarSelector.py.

186 def _improveCluster(yvec, centers, clusterId, nsigma=2.0, nIteration=10, clusterNum=0):
187  """Improve our estimate of one of the clusters (clusterNum) by sigma-clipping around its median"""
188 
189  nMember = sum(clusterId == clusterNum)
190  if nMember < 5: # can't compute meaningful interquartile range, so no chance of improvement
191  return clusterId
192  for iter in range(nIteration):
193  old_nMember = nMember
194 
195  inCluster0 = clusterId == clusterNum
196  yv = yvec[inCluster0]
197 
198  centers[clusterNum] = numpy.median(yv)
199  stdev = numpy.std(yv)
200 
201  syv = sorted(yv)
202  stdev_iqr = 0.741*(syv[int(0.75*nMember)] - syv[int(0.25*nMember)])
203 
204  sd = stdev if stdev < stdev_iqr else stdev_iqr
205 
206  if False:
207  print "sigma(iqr) = %.3f, sigma = %.3f" % (stdev_iqr, numpy.std(yv))
208  newCluster0 = abs(yvec - centers[clusterNum]) < nsigma*sd
209  clusterId[numpy.logical_and(inCluster0, newCluster0)] = clusterNum
210  clusterId[numpy.logical_and(inCluster0, numpy.logical_not(newCluster0))] = -1
211 
212  nMember = sum(clusterId == clusterNum)
213  if nMember == old_nMember:
214  break
215 
216  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 
)
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 150 of file objectSizeStarSelector.py.

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

Definition at line 218 of file objectSizeStarSelector.py.

219  clear=True):
220 
221  global fig
222  if not fig:
223  fig = pyplot.figure()
224  newFig = True
225  else:
226  newFig = False
227  if clear:
228  fig.clf()
229 
230  axes = fig.add_axes((0.1, 0.1, 0.85, 0.80));
231 
232  xmin = sorted(mag)[int(0.05*len(mag))]
233  xmax = sorted(mag)[int(0.95*len(mag))]
234 
235  axes.set_xlim(-17.5, -13)
236  axes.set_xlim(xmin - 0.1*(xmax - xmin), xmax + 0.1*(xmax - xmin))
237  axes.set_ylim(0, 10)
238 
239  colors = ["r", "g", "b", "c", "m", "k",]
240  for k, mean in enumerate(centers):
241  if k == 0:
242  axes.plot(axes.get_xlim(), (mean, mean,), "k%s" % ltype)
243 
244  l = (clusterId == k)
245  axes.plot(mag[l], width[l], marker, markersize=markersize, markeredgewidth=markeredgewidth,
246  color=colors[k%len(colors)])
247 
248  l = (clusterId == -1)
249  axes.plot(mag[l], width[l], marker, markersize=markersize, markeredgewidth=markeredgewidth,
250  color='k')
251 
252  if newFig:
253  axes.set_xlabel("model")
254  axes.set_ylabel(r"$\sqrt{I_{xx} + I_{yy}}$")
255 
256  return fig
257 
258 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

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.