Skip to content

Commit

Permalink
Update with latest versions of proxmin and scarlet
Browse files Browse the repository at this point in the history
  • Loading branch information
fred3m committed May 28, 2020
1 parent b08b9d2 commit d526d4e
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 160 deletions.
4 changes: 1 addition & 3 deletions python/lsst/meas/extensions/scarlet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

#from .version import * # Generated by sconsUtils

from .observation import *
from .source import *
from .blend import *
from . import source
from . import deblend
from .version import *
45 changes: 0 additions & 45 deletions python/lsst/meas/extensions/scarlet/blend.py

This file was deleted.

80 changes: 54 additions & 26 deletions python/lsst/meas/extensions/scarlet/deblend.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import numpy as np
import scarlet
from scarlet.psf import PSF, gaussian
from scarlet import PointSource, ExtendedSource, MultiComponentSource
from scarlet import PointSource, ExtendedSource, MultiComponentSource, Blend, Frame, Observation

import lsst.log
import lsst.pex.config as pexConfig
Expand All @@ -39,14 +39,20 @@
import lsst.afw.table as afwTable

from .source import initSource, modelToHeavy
from .blend import LsstBlend, checkBlendConvergence
from .observation import LsstFrame, LsstObservation

__all__ = ["deblend", "ScarletDeblendConfig", "ScarletDeblendTask"]

logger = lsst.log.Log.getLogger("meas.deblender.deblend")


def _checkBlendConvergence(blend, f_rel):
"""Check whether or not a blend has converged
"""
deltaLoss = np.abs(blend.loss[-2] - blend.loss[-1])
convergence = f_rel * np.abs(blend.loss[-1])
return deltaLoss < convergence


def _getPsfFwhm(psf):
"""Calculate the FWHM of the `psf`
"""
Expand Down Expand Up @@ -165,40 +171,54 @@ def deblend(mExposure, footprint, config):
psfShape = (config.modelPsfSize, config.modelPsfSize)
model_psf = PSF(partial(gaussian, sigma=config.modelPsfSigma), shape=(None,)+psfShape)

frame = LsstFrame(images.shape, psfs=model_psf, channels=mExposure.filters)
observation = LsstObservation(images, psfs=psfs, weights=weights, channels=mExposure.filters)
frame = Frame(images.shape, psfs=model_psf, channels=mExposure.filters)
observation = Observation(images, psfs=psfs, weights=weights, channels=mExposure.filters)
observation.match(frame)

assert(config.sourceModel in ["single", "double", "point", "fit"])

# Set the appropriate number of components
if config.sourceModel == "single":
maxComponents = 1
elif config.sourceModel == "double":
maxComponents = 2
elif config.sourceModel == "point":
maxComponents = 0
elif config.sourceModel == "fit":
# It is likely in the future that there will be some heuristic
# used to determine what type of model to use for each source,
# but that has not yet been implemented (see DM-22551)
raise NotImplementedError("sourceModel 'fit' has not been implemented yet")

# Convert the centers to pixel coordinates
xmin = bbox.getMinX()
ymin = bbox.getMinY()
centers = [np.array([peak.getIy()-ymin, peak.getIx()-xmin], dtype=int) for peak in footprint.peaks]

# Only deblend sources that can be initialized
sources = []
skipped = []
for k, center in enumerate(footprint.peaks):
if config.sourceModel == "single":
components = 1
elif config.sourceModel == "double":
components = 2
elif config.sourceModel == "point":
components = 0
elif config.sourceModel == "fit":
# It is likely in the future that there will be some heuristic
# used to determine what type of model to use for each source,
# but that has not yet been implemented (see DM-22551)
raise NotImplementedError("sourceModel 'fit' has not been implemented yet")
else:
raise ValueError("Unrecognized sourceModel")

source = initSource(frame=frame, peak=center, observation=observation, bbox=bbox,
symmetric=config.symmetric, monotonic=config.monotonic,
thresh=config.morphThresh, components=components,
edgeDistance=config.edgeDistance, shifting=False)
for k, center in enumerate(centers):
source = initSource(
frame=frame,
center=center,
observation=observation,
symmetric=config.symmetric,
monotonic=config.monotonic,
thresh=config.morphThresh,
maxComponents=maxComponents,
edgeDistance=config.edgeDistance,
shifting=False,
downgrade=config.downgrade,
fallback=config.fallback,
)
if source is not None:
source.detectedPeak = footprint.peaks[k]
sources.append(source)
else:
skipped.append(k)

blend = LsstBlend(sources, observation)
blend = Blend(sources, observation)
blend.fit(max_iter=config.maxIter, e_rel=config.relativeError)

return blend, skipped
Expand Down Expand Up @@ -258,6 +278,10 @@ class ScarletDeblendConfig(pexConfig.Config):
"- 'point: use a point-source model for all sources\n"
"- 'fit: use a PSF fitting model to determine the number of components (not yet implemented)")
)
downgrade = pexConfig.Field(
dtype=bool, default=False,
doc="Whether or not to downgrade the number of components for sources in small bounding boxes"
)

# Mask-plane restrictions
badMask = pexConfig.ListField(
Expand Down Expand Up @@ -292,6 +316,10 @@ class ScarletDeblendConfig(pexConfig.Config):
"as large; non-positive means no threshold applied"))

# Failure modes
fallback = pexConfig.Field(
dtype=bool, default=True,
doc="Whether or not to fallback to a smaller number of components if a source does not initialize"
)
notDeblendedMask = pexConfig.Field(
dtype=str, default="NOT_DEBLENDED", optional=True,
doc="Mask name for footprints not deblended, or None")
Expand Down Expand Up @@ -528,7 +556,7 @@ def deblend(self, mExposure, sources):
runtime = (tf-t0)*1000
src.set(self.deblendFailedKey, False)
src.set(self.runtimeKey, runtime)
converged = checkBlendConvergence(blend, self.config.relativeError)
converged = _checkBlendConvergence(blend, self.config.relativeError)
src.set(self.blendConvergenceFailedFlagKey, converged)
sources = [src for src in blend.sources]
# Re-insert place holders for skipped sources
Expand Down
32 changes: 0 additions & 32 deletions python/lsst/meas/extensions/scarlet/observation.py

This file was deleted.

0 comments on commit d526d4e

Please sign in to comment.