Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mask planes from template and science fake plane into difference #280

Merged
merged 3 commits into from
Oct 18, 2023

Conversation

BrunoSanchez
Copy link
Contributor

Added the needed mask plane into differences. These are two new planes named 'INJECTED' and 'INJECTED_TEMPLATE'

Copy link
Contributor

@isullivan isullivan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the code comments, please add a simple unit test of the new updateMasks.

templateFakeBitMask = template[bbox].mask.getPlaneBitMask('FAKE')

injScienceMaskArray = ((science.mask.array & scienceFakeBitMask) > 0)
injScienceMaskArray *= diffInjectedBitMask
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In testing, I get the following error from this line (and below):
numpy.core._exceptions._UFuncOutputCastingError: Cannot cast ufunc 'multiply' output from dtype('int64') to dtype('bool') with casting rule 'same_kind'
I suggest removing the line injScienceMaskArray *= diffInjectedBitMask and using mask.array[injScienceMaskArray] |= diffInjectedBitMask below. You might want to change the variable name of injScienceMaskArray to reflect the new usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found an alternative solution to this. I have simplified to a single line, and multiplied by the bit mask values in the same variable assignation. It also seems to suit the same name, at least to me.

injTemplateMaskArray = ((template[bbox].mask.array & templateFakeBitMask) > 0)
injTemplateMaskArray *= diffInjectedTemplateBitMask

mask.array |= injScienceMaskArray
Copy link
Contributor

@isullivan isullivan Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my requested modification in the comment above. Looks resolved with the above changes.

@@ -581,6 +576,42 @@ def finalize(self, template, science, difference, kernel,
correctedExposure = difference
return correctedExposure

def updateMasks(self, difference, science, template):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you keep the same order of images as in finalize: template, science, difference

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

maskPlane for maskPlane in badMaskPlanes if maskPlane in mask.getMaskPlaneDict().keys()
]

badPixelMask = mask.getPlaneBitMask(setBadMaskPlanes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for changing this. We need to switch from lsst.afw.image.Mask.getPlaneBitMask to mask.getPlaneBitMask in general (see lsst/meas_algorithms#329 (comment))

@@ -712,7 +743,12 @@ def _checkMask(mask, sources, badMaskPlanes):
kept (True) or rejected (False) based on the value of the
mask plane at its location.
"""
badPixelMask = lsst.afw.image.Mask.getPlaneBitMask(badMaskPlanes)
setBadMaskPlanes = [
maskPlane for maskPlane in badMaskPlanes if maskPlane in mask.getMaskPlaneDict().keys()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor point, but I don't think the .keys() is necessary here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@BrunoSanchez
Copy link
Contributor Author

This is ready for second review!

Copy link
Contributor

@isullivan isullivan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I had a few suggested changes, though the only necessary one is to remove the dangling code comment.

if self.config.doDecorrelation:
self.log.info("Decorrelating image difference.")
# We have cleared the template mask plane, so copy the mask plane of
# the image difference so that we can calculate correct statistics
# during decorrelation
template[science.getBBox()].mask.array[...] = difference.mask.array[...]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also remove the associated code comment above.

science.image.array += science_fake_img.image.array
template.image.array += tmplt_fake_img.image.array

# TODO: update to INJECTED names when source injection gets refactored
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to always reference a ticket number with any TODO. I think that is DM-40796 here.

Comment on lines +780 to +774
inj_masked = (diff_mask.array & diff_mask.getPlaneBitMask("INJECTED")) > 0

# template mask should be now in INJECTED_TEMPLATE
injTmplt_masked = (diff_mask.array & diff_mask.getPlaneBitMask("INJECTED_TEMPLATE")) > 0

self.assertEqual(np.sum(inj_masked.astype(int)-science_fake_masked.astype(int)), 0)
self.assertEqual(np.sum(injTmplt_masked.astype(int)-template_fake_masked.astype(int)), 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the mask plane is changing, this looks like a good way to do the test. For future reference, see https://pipelines.lsst.io/py-api/lsst.utils.tests.TestCase.html for useful built-in asserts, though I don't think any would quite work here.

science.mask.addMaskPlane("FAKE")
template.mask.addMaskPlane("FAKE")

science_mask_planes = science.mask.getMaskPlaneDict()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use science_fake_mask = science.mask.getMaskPlane('FAKE') here, and then just use science_fake_mask instead of science_mask_planes['FAKE'] later. Same for the template below.

Or use science_fake_mask = science.mask.getPlaneBitMask("FAKE") if you use my suggestion below.

Comment on lines 748 to 754
for dy in (-1, 0, 1):
science.mask.setMaskPlaneValues(
science_mask_planes['FAKE'],
int(a_science_source.getX() - x0)-1,
int(a_science_source.getX() - x0)+1,
int(a_science_source.getY() - y0)+dy
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, but I have an alternate suggestion:

bbox = lsst.geom.Box2I(lsst.geom.Point2I(a_science_source.getX(), a_science_source.getY()), lsst.geom.Extent2I(3, 3))
science[bbox].mask.array |= science_fake_mask

using science_fake_mask from my above suggestion.

@BrunoSanchez
Copy link
Contributor Author

Thanks for the comments.

All of them are implemented!

@BrunoSanchez
Copy link
Contributor Author

This is the final Jenkins run that validates this merge: https://rubin-ci.slac.stanford.edu/job/stack-os-matrix/480/

@BrunoSanchez BrunoSanchez reopened this Oct 18, 2023
@BrunoSanchez BrunoSanchez merged commit 21e1ef4 into main Oct 18, 2023
4 checks passed
@BrunoSanchez BrunoSanchez deleted the tickets/DM-32889 branch October 18, 2023 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants