Skip to content

Commit

Permalink
Make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
fred3m committed Dec 23, 2024
1 parent f6d2505 commit 1594e56
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
37 changes: 32 additions & 5 deletions python/lsst/meas/extensions/scarlet/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
# The name of the band in an monochome blend.
# This is used as a placeholder since the band is not used in the
# monochromatic model.
monochromeBand = "dummmy"
monochromeBand = "dummy"
monochromeBands = (monochromeBand,)

def monochromaticDataToScarlet(

Check failure on line 47 in python/lsst/meas/extensions/scarlet/io.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

E302

expected 2 blank lines, found 1
Expand Down Expand Up @@ -128,6 +128,7 @@ def updateCatalogFootprints(
imageForRedistribution: MaskedImage | Exposure | None = None,
removeScarletData: bool = True,
updateFluxColumns: bool = True,
bbox: scl.Box | None = None,
) -> dict[int, scl.Blend]:
"""Use the scarlet models to set HeavyFootprints for modeled sources
Expand Down Expand Up @@ -163,6 +164,7 @@ def updateCatalogFootprints(
imageForRedistribution=imageForRedistribution,
bandIndex=bandIndex,
removeScarletData=removeScarletData,
bbox=bbox,
)

for blendId, blend in blends.items():
Expand Down Expand Up @@ -244,6 +246,35 @@ def extraxctMonochrmaticBlends(
removeScarletData: bool = True,
bbox: scl.Box | None = None,
) -> dict[int, scl.Blend]:
"""Extract the monochromatic blends from the scarlet model data
Parameters
----------
modelData:
The scarlet model data.
catalog:
The catalog that is being updated.
modelPsf:
The 2D model of the PSF.
observedPsf:
The observed PSF model for the catalog.
imageForRedistribution:
The image that is the source for flux re-distribution.
If `imageForRedistribution` is `None` then flux re-distribution is
not performed.
bandIndex:
The number of the band to extract.
removeScarletData:
Whether or not to remove `ScarletBlendData` for each blend
in order to save memory.
bbox:
The bounding box of the image to create the weight image for.
Returns
-------
blends :
A dictionary of blends extracted from the model data.
"""
blends = {}
# Create an observation for the entire image
observation = buildMonochromeObservation(
Expand Down Expand Up @@ -395,7 +426,6 @@ def updateBlendRecords(
)

if updateFluxColumns:
assert imageForRedistribution is not None
if heavy.getArea() == 0:
# The source has no flux after being weighted with the PSF
# in this particular band (it might have flux in others).
Expand All @@ -412,9 +442,6 @@ def updateBlendRecords(
else:
sourceRecord.set("deblend_zeroFlux", False)
sourceRecord.setFootprint(heavy)
calculateFootprintCoverage(
sourceRecord.getFootprint(), imageForRedistribution.mask
)

if useFlux:
# Set the fraction of pixels with valid data.
Expand Down
30 changes: 20 additions & 10 deletions tests/test_deblend.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import unittest

import lsst.afw.image as afwImage
import lsst.meas.extensions.scarlet as mes
import lsst.scarlet.lite as scl
import lsst.utils.tests
import numpy as np
Expand All @@ -30,12 +31,8 @@
from lsst.afw.table import SourceCatalog
from lsst.geom import Point2D, Point2I
from lsst.meas.algorithms import SourceDetectionTask
from lsst.meas.extensions.scarlet.io import (
monochromaticDataToScarlet,
updateCatalogFootprints,
)
from lsst.meas.extensions.scarlet.scarletDeblendTask import ScarletDeblendTask
from lsst.meas.extensions.scarlet.utils import bboxToScarletBox, scarletBoxToBBox
from lsst.meas.extensions.scarlet.deconvolveExposureTask import DeconvolveExposureTask
from numpy.testing import assert_almost_equal
from utils import initData

Expand Down Expand Up @@ -151,6 +148,14 @@ def _deblend(self, version):
detectionResult = detectionTask.run(table, self.coadds["r"])
catalog = detectionResult.sources

# Deconvolve the coadds
deconvolvedCoadds = []
deconvolveTask = DeconvolveExposureTask()
for coadd in self.coadds:
deconvolvedCoadd = deconvolveTask.run(coadd, catalog).deconvolved
deconvolvedCoadds.append(deconvolvedCoadd)
mDeconvolved = afwImage.MultibandExposure.fromExposures(self.bands, deconvolvedCoadds)

# Add a footprint that is too large
src = catalog.addNew()
halfLength = int(np.ceil(np.sqrt(config.maxFootprintArea) + 1))
Expand All @@ -168,7 +173,7 @@ def _deblend(self, version):
src.setFootprint(denseFoot)

# Run the deblender
catalog, modelData = deblendTask.run(self.coadds, catalog)
catalog, modelData = deblendTask.run(self.coadds, mDeconvolved, catalog)
return catalog, modelData, config

def test_deblend_task(self):
Expand All @@ -187,15 +192,19 @@ def test_deblend_task(self):

if useFlux:
imageForRedistribution = coadd
bbox = None
else:
imageForRedistribution = None
bbox = mes.utils.bboxToScarletBox(coadd.getBBox())

updateCatalogFootprints(
mes.io.updateCatalogFootprints(
modelData,
catalog,
band=band,
imageForRedistribution=imageForRedistribution,
removeScarletData=False,
updateFluxColumns=True,
bbox = bbox,
)

# Check that the number of deblended children is consistent
Expand Down Expand Up @@ -262,7 +271,7 @@ def test_deblend_task(self):
bbox=modelBox,
dtype=np.float32,
)
blend = monochromaticDataToScarlet(
blend = mes.io.monochromaticDataToScarlet(
blendData=blendData,
bandIndex=bandIndex,
observation=observation,
Expand All @@ -278,6 +287,7 @@ def test_deblend_task(self):
self.assertEqual(source.center[0], py)

if useFlux:
assert imageForRedistribution is not None
# Get the flux re-weighted model and test against
# the HeavyFootprint.
# The HeavyFootprint needs to be projected onto
Expand All @@ -301,15 +311,15 @@ def test_deblend_task(self):
)
blend.conserve_flux()
model = source.flux_weighted_image.data[0]
bbox = scarletBoxToBBox(source.flux_weighted_image.bbox)
bbox = mes.utils.scarletBoxToBBox(source.flux_weighted_image.bbox)
image = afwImage.ImageF(model, xy0=bbox.getMin())
fp.insert(image)
np.testing.assert_almost_equal(image.array, model)
else:
# Get the model for the source and test
# against the HeavyFootprint
bbox = fp.getBBox()
bbox = bboxToScarletBox(bbox)
bbox = mes.utils.bboxToScarletBox(bbox)
model = blend.observation.convolve(
source.get_model().project(bbox=bbox), mode="real"
).data[0]
Expand Down

0 comments on commit 1594e56

Please sign in to comment.