LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Public Member Functions | Public Attributes | List of all members
lsst.pipe.tasks.maskStreaks.LineProfile Class Reference

Public Member Functions

def __init__ (self, data, weights, line=None)
 
def setLineMask (self, line)
 
def makeProfile (self, line, fitFlux=True)
 
def fit (self, dChi2Tol=0.1, maxIter=100)
 

Public Attributes

 data
 
 weights
 
 mask
 
 lineMask
 
 lineMaskSize
 

Detailed Description

Construct and/or fit a model for a linear streak.

This assumes a simple model for a streak, in which the streak
follows a straight line in pixels space, with a Moffat-shaped profile. The
model is fit to data using a Newton-Raphson style minimization algorithm.
The initial guess for the line parameters is assumed to be fairly accurate,
so only a narrow band of pixels around the initial line estimate is used in
fitting the model, which provides a significant speed-up over using all the
data. The class can also be used just to construct a model for the data with
a line following the given coordinates.

Parameters
----------
data : np.ndarray
    2d array of data
weights : np.ndarray
    2d array of weights
line : `Line` (optional)
    Guess for position of line. Data far from line guess is masked out.
    Defaults to None, in which case only data with `weights` = 0 is masked
    out.

Definition at line 189 of file maskStreaks.py.

Constructor & Destructor Documentation

◆ __init__()

def lsst.pipe.tasks.maskStreaks.LineProfile.__init__ (   self,
  data,
  weights,
  line = None 
)

Definition at line 213 of file maskStreaks.py.

213  def __init__(self, data, weights, line=None):
214  self.data = data
215  self.weights = weights
216  self._ymax, self._xmax = data.shape
217  self._dtype = data.dtype
218  xrange = np.arange(self._xmax) - self._xmax / 2.
219  yrange = np.arange(self._ymax) - self._ymax / 2.
220  self._rhoMax = ((0.5 * self._ymax)**2 + (0.5 * self._xmax)**2)**0.5
221  self._xmesh, self._ymesh = np.meshgrid(xrange, yrange)
222  self.mask = (weights != 0)
223 
224  self._initLine = line
225  self.setLineMask(line)
226 

Member Function Documentation

◆ fit()

def lsst.pipe.tasks.maskStreaks.LineProfile.fit (   self,
  dChi2Tol = 0.1,
  maxIter = 100 
)
Perform Newton-Raphson minimization to find line parameters

This method takes advantage of having known derivative and Hessian of
the multivariate function to quickly and efficiently find the minimum.
This is more efficient than the scipy implementation of the Newton-
Raphson method, which doesn't take advantage of the Hessian matrix. The
method here also performs a line search in the direction of the steepest
derivative at each iteration, which reduces the number of iterations
needed.

Parameters
----------
dChi2Tol : float (optional)
    Change in Chi2 tolerated for fit convergence
maxIter : int (optional)
    Maximum number of fit iterations allowed. The fit should converge in
    ~10 iterations, depending on the value of dChi2Tol, but this
    maximum provides a backup.

Returns
-------
outline : np.ndarray
    Coordinates and inverse width of fit line
chi2 : float
    Reduced Chi2 of model fit to data
fitFailure : bool
    Boolean where `False` corresponds to a successful  fit

Definition at line 362 of file maskStreaks.py.

362  def fit(self, dChi2Tol=0.1, maxIter=100):
363  """Perform Newton-Raphson minimization to find line parameters
364 
365  This method takes advantage of having known derivative and Hessian of
366  the multivariate function to quickly and efficiently find the minimum.
367  This is more efficient than the scipy implementation of the Newton-
368  Raphson method, which doesn't take advantage of the Hessian matrix. The
369  method here also performs a line search in the direction of the steepest
370  derivative at each iteration, which reduces the number of iterations
371  needed.
372 
373  Parameters
374  ----------
375  dChi2Tol : float (optional)
376  Change in Chi2 tolerated for fit convergence
377  maxIter : int (optional)
378  Maximum number of fit iterations allowed. The fit should converge in
379  ~10 iterations, depending on the value of dChi2Tol, but this
380  maximum provides a backup.
381 
382  Returns
383  -------
384  outline : np.ndarray
385  Coordinates and inverse width of fit line
386  chi2 : float
387  Reduced Chi2 of model fit to data
388  fitFailure : bool
389  Boolean where `False` corresponds to a successful fit
390  """
391  # Do minimization on inverse of sigma to simplify derivatives:
392  x = np.array([self._initLine.rho, self._initLine.theta, self._initLine.sigma**-1])
393 
394  dChi2 = 1
395  iter = 0
396  oldChi2 = 0
397  fitFailure = False
398 
399  def line_search(c, dx):
400  testx = x - c * dx
401  testLine = Line(testx[0], testx[1], testx[2]**-1)
402  return self._lineChi2(testLine, grad=False)
403 
404  while abs(dChi2) > dChi2Tol:
405  line = Line(x[0], x[1], x[2]**-1)
406  chi2, b, A = self._lineChi2(line)
407  if chi2 == 0:
408  break
409  if not np.isfinite(A).all():
410  # TODO: DM-30797 Add warning here.
411  fitFailure = True
412  break
413  dChi2 = oldChi2 - chi2
414  cholesky = scipy.linalg.cho_factor(A)
415  dx = scipy.linalg.cho_solve(cholesky, b)
416 
417  factor, fmin, _, _ = scipy.optimize.brent(line_search, args=(dx,), full_output=True, tol=0.05)
418  x -= factor * dx
419  if (abs(x[0]) > 1.5 * self._rhoMax) or (iter > maxIter):
420  fitFailure = True
421  break
422  oldChi2 = chi2
423  iter += 1
424 
425  outline = Line(x[0], x[1], abs(x[2])**-1)
426 
427  return outline, chi2, fitFailure
428 
429 
bool all(CoordinateExpr< N > const &expr) noexcept
Return true if all elements are true.
Angle abs(Angle const &a)
Definition: Angle.h:106

◆ makeProfile()

def lsst.pipe.tasks.maskStreaks.LineProfile.makeProfile (   self,
  line,
  fitFlux = True 
)
Construct the line profile model

Parameters
----------
line : `Line`
    Parameters of the line profile to model
fitFlux : bool (optional)
    Fit the amplitude of the line profile to the data

Returns
-------
finalModel : np.ndarray
    Model for line profile

Definition at line 308 of file maskStreaks.py.

308  def makeProfile(self, line, fitFlux=True):
309  """Construct the line profile model
310 
311  Parameters
312  ----------
313  line : `Line`
314  Parameters of the line profile to model
315  fitFlux : bool (optional)
316  Fit the amplitude of the line profile to the data
317 
318  Returns
319  -------
320  finalModel : np.ndarray
321  Model for line profile
322  """
323  model, _ = self._makeMaskedProfile(line, fitFlux=fitFlux)
324  finalModel = np.zeros((self._ymax, self._xmax), dtype=self._dtype)
325  finalModel[self.lineMask] = model
326  return finalModel
327 

◆ setLineMask()

def lsst.pipe.tasks.maskStreaks.LineProfile.setLineMask (   self,
  line 
)
Set mask around the image region near the line

Parameters
----------
line : `Line`
    Parameters of line in the image

Definition at line 227 of file maskStreaks.py.

227  def setLineMask(self, line):
228  """Set mask around the image region near the line
229 
230  Parameters
231  ----------
232  line : `Line`
233  Parameters of line in the image
234  """
235  if line:
236  # Only fit pixels within 5 sigma of the estimated line
237  radtheta = np.deg2rad(line.theta)
238  distance = (np.cos(radtheta) * self._xmesh + np.sin(radtheta) * self._ymesh - line.rho)
239  m = (abs(distance) < 5 * line.sigma)
240  self.lineMask = self.mask & m
241  else:
242  self.lineMask = np.copy(self.mask)
243 
244  self.lineMaskSize = self.lineMask.sum()
245  self._maskData = self.data[self.lineMask]
246  self._maskWeights = self.weights[self.lineMask]
247  self._mxmesh = self._xmesh[self.lineMask]
248  self._mymesh = self._ymesh[self.lineMask]
249 

Member Data Documentation

◆ data

lsst.pipe.tasks.maskStreaks.LineProfile.data

Definition at line 214 of file maskStreaks.py.

◆ lineMask

lsst.pipe.tasks.maskStreaks.LineProfile.lineMask

Definition at line 240 of file maskStreaks.py.

◆ lineMaskSize

lsst.pipe.tasks.maskStreaks.LineProfile.lineMaskSize

Definition at line 244 of file maskStreaks.py.

◆ mask

lsst.pipe.tasks.maskStreaks.LineProfile.mask

Definition at line 222 of file maskStreaks.py.

◆ weights

lsst.pipe.tasks.maskStreaks.LineProfile.weights

Definition at line 215 of file maskStreaks.py.


The documentation for this class was generated from the following file: