LSST Applications g0fba68d861+539c3c8198,g1ec0fe41b4+f0b7a83aa5,g1fd858c14a+78b1f303e5,g22210bfa2a+8e34e7f00e,g2440f9efcc+8c5ae1fdc5,g3533f9d6cb+f4db8b1b3b,g35bb328faa+8c5ae1fdc5,g3fc7d4f5b9+a4d3a2d8bb,g40d01f57dd+ad4ae80abb,g4178042926+abd409f097,g53246c7159+8c5ae1fdc5,g548d740b8c+47e4a7beb3,g60b5630c4e+f4db8b1b3b,g663da51e9b+e0f2ae43b2,g67b6fd64d1+c104138150,g78460c75b0+7e33a9eb6d,g786e29fd12+668abc6043,g8352419a5c+8c5ae1fdc5,g8852436030+d435302cbd,g89139ef638+c104138150,g8b40312ef5+5b6744d273,g90aefe88b0+c7f866738c,g989de1cb63+c104138150,g9f33ca652e+0b88989aa6,ga2f891cd6c+f4db8b1b3b,gabe3b4be73+8856018cbb,gabf8522325+5cb9e9d408,gb1101e3267+78d08b86ce,gb89ab40317+c104138150,gcf25f946ba+d435302cbd,gd6cbbdb0b4+be834e5da7,gde0f65d7ad+a7114e3dba,ge278dab8ac+fa35eb453c,ge410e46f29+c104138150,gf35d7ec915+97dd712d81,gf5e32f922b+8c5ae1fdc5,gf67bdafdda+c104138150,gffe7e49bb4+f4db8b1b3b,v29.1.0.rc2
LSST Data Management Base Package
Loading...
Searching...
No Matches
lsst.meas.algorithms.objectSizeStarSelector Namespace Reference

Classes

class  EventHandler
 
class  ObjectSizeNoGoodSourcesError
 
class  ObjectSizeNoSourcesError
 
class  ObjectSizeStarSelectorConfig
 
class  ObjectSizeStarSelectorTask
 

Functions

 _assignClusters (yvec, centers)
 
 _kcenters (yvec, nCluster, useMedian=False, widthStdAllowed=0.15)
 
 _improveCluster (yvec, centers, clusterId, nsigma=2.0, nIteration=10, clusterNum=0, widthStdAllowed=0.15)
 
 plot (mag, width, centers, clusterId, marker="o", markersize=2, markeredgewidth=0, ltype='-', magType="model", clear=True)
 

Variables

 _LOG = getLogger(__name__)
 

Function Documentation

◆ _assignClusters()

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

Definition at line 197 of file objectSizeStarSelector.py.

197def _assignClusters(yvec, centers):
198 """Return a vector of centerIds based on their distance to the centers.
199 """
200 assert len(centers) > 0
201
202 minDist = numpy.nan*numpy.ones_like(yvec)
203 clusterId = numpy.empty_like(yvec)
204 clusterId.dtype = int # zeros_like(..., dtype=int) isn't in numpy 1.5
205 dbl = _LOG.getChild("_assignClusters")
206 dbl.setLevel(dbl.INFO)
207
208 # Make sure we are logging aall numpy warnings...
209 oldSettings = numpy.seterr(all="warn")
210 with warnings.catch_warnings(record=True) as w:
211 warnings.simplefilter("always")
212 for i, mean in enumerate(centers):
213 dist = abs(yvec - mean)
214 if i == 0:
215 update = dist == dist # True for all points
216 else:
217 update = dist < minDist
218 if w: # Only do if w is not empty i.e. contains a warning message
219 dbl.trace(str(w[-1]))
220
221 minDist[update] = dist[update]
222 clusterId[update] = i
223 numpy.seterr(**oldSettings)
224
225 return clusterId
226
227

◆ _improveCluster()

lsst.meas.algorithms.objectSizeStarSelector._improveCluster ( yvec,
centers,
clusterId,
nsigma = 2.0,
nIteration = 10,
clusterNum = 0,
widthStdAllowed = 0.15 )
protected
Improve our estimate of one of the clusters (clusterNum) by sigma-clipping around its median.

Definition at line 270 of file objectSizeStarSelector.py.

270def _improveCluster(yvec, centers, clusterId, nsigma=2.0, nIteration=10, clusterNum=0, widthStdAllowed=0.15):
271 """Improve our estimate of one of the clusters (clusterNum) by sigma-clipping around its median.
272 """
273
274 nMember = sum(clusterId == clusterNum)
275 if nMember < 5: # can't compute meaningful interquartile range, so no chance of improvement
276 return clusterId
277 for iter in range(nIteration):
278 old_nMember = nMember
279
280 inCluster0 = clusterId == clusterNum
281 yv = yvec[inCluster0]
282
283 centers[clusterNum] = numpy.median(yv)
284 stdev = numpy.std(yv)
285
286 syv = sorted(yv)
287 stdev_iqr = 0.741*(syv[int(0.75*nMember)] - syv[int(0.25*nMember)])
288 median = syv[int(0.5*nMember)]
289
290 sd = stdev if stdev < stdev_iqr else stdev_iqr
291
292 if False:
293 print("sigma(iqr) = %.3f, sigma = %.3f" % (stdev_iqr, numpy.std(yv)))
294 newCluster0 = abs(yvec - centers[clusterNum]) < nsigma*sd
295 clusterId[numpy.logical_and(inCluster0, newCluster0)] = clusterNum
296 clusterId[numpy.logical_and(inCluster0, numpy.logical_not(newCluster0))] = -1
297
298 nMember = sum(clusterId == clusterNum)
299 # 'sd < widthStdAllowed * median' prevents too much rejections
300 if nMember == old_nMember or sd < widthStdAllowed * median:
301 break
302
303 return clusterId
304
305

◆ _kcenters()

lsst.meas.algorithms.objectSizeStarSelector._kcenters ( yvec,
nCluster,
useMedian = False,
widthStdAllowed = 0.15 )
protected
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 228 of file objectSizeStarSelector.py.

228def _kcenters(yvec, nCluster, useMedian=False, widthStdAllowed=0.15):
229 """A classic k-means algorithm, clustering yvec into nCluster clusters
230
231 Return the set of centres, and the cluster ID for each of the points
232
233 If useMedian is true, use the median of the cluster as its centre, rather than
234 the traditional mean
235
236 Serge Monkewitz points out that there other (maybe smarter) ways of seeding the means:
237 "e.g. why not use the Forgy or random partition initialization methods"
238 however, the approach adopted here seems to work well for the particular sorts of things
239 we're clustering in this application
240 """
241
242 assert nCluster > 0
243
244 mean0 = sorted(yvec)[len(yvec)//10] # guess
245 delta = mean0 * widthStdAllowed * 2.0
246 centers = mean0 + delta * numpy.arange(nCluster)
247
248 func = numpy.median if useMedian else numpy.mean
249
250 clusterId = numpy.zeros_like(yvec) - 1 # which cluster the points are assigned to
251 clusterId.dtype = int # zeros_like(..., dtype=int) isn't in numpy 1.5
252 while True:
253 oclusterId = clusterId
254 clusterId = _assignClusters(yvec, centers)
255
256 if numpy.all(clusterId == oclusterId):
257 break
258
259 for i in range(nCluster):
260 # Only compute func if some points are available; otherwise, default to NaN.
261 pointsInCluster = (clusterId == i)
262 if numpy.any(pointsInCluster):
263 centers[i] = func(yvec[pointsInCluster])
264 else:
265 centers[i] = numpy.nan
266
267 return centers, clusterId
268
269

◆ plot()

lsst.meas.algorithms.objectSizeStarSelector.plot ( mag,
width,
centers,
clusterId,
marker = "o",
markersize = 2,
markeredgewidth = 0,
ltype = '-',
magType = "model",
clear = True )

Definition at line 306 of file objectSizeStarSelector.py.

307 magType="model", clear=True):
308
309 log = _LOG.getChild("plot")
310 try:
311 import matplotlib.pyplot as plt
312 except ImportError as e:
313 log.warning("Unable to import matplotlib: %s", e)
314 return
315
316 try:
317 fig
318 except NameError:
319 fig = plt.figure()
320 else:
321 if clear:
322 fig.clf()
323
324 axes = fig.add_axes((0.1, 0.1, 0.85, 0.80))
325
326 xmin = sorted(mag)[int(0.05*len(mag))]
327 xmax = sorted(mag)[int(0.95*len(mag))]
328
329 axes.set_xlim(-17.5, -13)
330 axes.set_xlim(xmin - 0.1*(xmax - xmin), xmax + 0.1*(xmax - xmin))
331 axes.set_ylim(0, 10)
332
333 colors = ["r", "g", "b", "c", "m", "k", ]
334 for k, mean in enumerate(centers):
335 if k == 0:
336 axes.plot(axes.get_xlim(), (mean, mean,), "k%s" % ltype)
337
338 li = (clusterId == k)
339 axes.plot(mag[li], width[li], marker, markersize=markersize, markeredgewidth=markeredgewidth,
340 color=colors[k % len(colors)])
341
342 li = (clusterId == -1)
343 axes.plot(mag[li], width[li], marker, markersize=markersize, markeredgewidth=markeredgewidth,
344 color='k')
345
346 if clear:
347 axes.set_xlabel("Instrumental %s mag" % magType)
348 axes.set_ylabel(r"$\sqrt{(I_{xx} + I_{yy})/2}$")
349
350 return fig
351
352
353@pexConfig.registerConfigurable("objectSize", sourceSelectorRegistry)

Variable Documentation

◆ _LOG

lsst.meas.algorithms.objectSizeStarSelector._LOG = getLogger(__name__)
protected

Definition at line 42 of file objectSizeStarSelector.py.