Source detection on difference images

I am trying to put together tutorial notebooks DP02_14_Injecting_Synthetic_Sources and DP02_05_Introduction_to_Source_Detection. The final idea would be to inject a fake source onto an image, run the image subtraction task, and finally measure the residuals on the difference.

I got the difference image following notebook 14:

from lsst.ip.diffim.subtractImages import AlardLuptonSubtractTask, AlardLuptonSubtractConfig
difftemp = 'goodSeeingDiff_templateExp'
templateExposure = butler.get(difftemp, dataId=dataId_g.required)
config = AlardLuptonSubtractConfig()
alTask = AlardLuptonSubtractTask(config=config)

scienceExposure = injected_exposure

sources = butler.get('src', dataId=dataId_g)
result =, scienceExposure, sources)

Everything is fine when I want to see the resulting image (result.difference.image) with afwDisplay, but I do not understand how to give it as input for the source detection task. The following code snippet (from notebook 05) assumes you have a calexp:

# Image characterization (this cell may take a few seconds)
result =

# Define the pixel coordinates of a point of interest
# (in this case, basically a random point within the image)
x_target, y_target = tar_xy[0], tar_xy[1]
width, height = 400, 400
xmin, ymin = x_target-width//2, y_target-height//2
point = geom.Point2D(x_target, y_target)

# Get the PSF at our point of interest
psf = calexp.getPsf()
sigma = psf.computeShape(point).getDeterminantRadius()
pixelScale = calexp.getWcs().getPixelScale().asArcseconds()

# The factor of 2.355 converts from std to fwhm
print('psf fwhm = {:.2f} arcsec'.format(sigma*pixelScale*2.355))

May someone help me please?

Hi Vincenzo-

I don’t have a full answer for you, because I also don’t know how to run detection and measurement on difference images. However, the guide at Processing DC2 data with the AP Pipelines — LSST Science Pipelines may be helpful in pointing you to the right tasks. (Note that you may not need the APDB portion of the guide, and can just pick out the tasks from that pipeline that do the detection and measurement. In particular, see these lines in the main apPipe.yaml.)

Hopefully somebody with more knowledge of DIA measurement can chime in with more details, though.

1 Like

Could you try using result.difference, which is of type lsst.afw.image._exposure.ExposureF ? The following code seemed to have worked:

from lsst.pipe.tasks.characterizeImage import CharacterizeImageTask
config = CharacterizeImageTask.ConfigClass()
config.psfIterations = 1
charImageTask = CharacterizeImageTask(config=config)

resultChar =

Although I did not do a deep investigation beyond that.

1 Like

Vincenzo, while the above code I provided runs, it might not be the recommended / optimal way of running source detection on difference images. We (CST members) are checking on this and will return to you.

Thank you for the quick reply! It is working for me as well, albeit yesterday I was apparently trying to do the same thing. Anyway, I am glad it works now!

Now I have a new issue with this error RuntimeError: Failed to determine psfex psf: too few good stars.
I guess it is happening because the default configuration is trying to get the psf from the difference image (when there is nothing but a single star I injected). It would be nice to run the source detection task only on a specific list of sources, providing some constraints. I suppose it is possible, but it would requires to study the package a bit.

I will keep working on this a bit in the following days. Maybe we could eventually discuss it during DP0 assemblies.

You don’t want to run characterizeImage on a difference image: it already has a well characterized PSF (which is what characterizeImage is trying to do). DetectandMeasureTask from ip_diffm is the task you want for getting a catalog from a difference image.

1 Like

Hi @Vincenzo , as suggested above by Jeff and confirmed by John, characterizeImage should not be used on a difference image, but rather DetectandMeasureTask from ip_diffm.

I found a nice example of the use of that task on a difference image using DP0.2 in this DESC notebook:


I have cloned the repo at the RSP of and confirmed that it runs using the currently recommended weekly (and a large container): 2024_04. The only caveat is that I had to add the following line in cell 44 to avoid an error:


The task is called in cell 45.

1 Like

Docs for DetectAndMeasureTask are here: DetectAndMeasureTask — LSST Science Pipelines