@@ -414,6 +414,46 @@ def _detection_wrapper(positive=True):
414414 _detection_wrapper (positive = True )
415415 _detection_wrapper (positive = False )
416416
417+ def test_mask_cosmic_rays (self ):
418+ """Run detection on a difference image containing a cosmic ray.
419+ """
420+ # Set up the simulated images
421+ noiseLevel = 1.
422+ staticSeed = 1
423+ fluxLevel = 500
424+ xSize = 400
425+ ySize = 400
426+ kwargs = {"seed" : staticSeed , "psfSize" : 2.4 , "fluxLevel" : fluxLevel , "xSize" : xSize , "ySize" : ySize }
427+ science , sources = makeTestImage (noiseLevel = noiseLevel , noiseSeed = 6 , ** kwargs )
428+ matchedTemplate , _ = makeTestImage (noiseLevel = noiseLevel / 4 , noiseSeed = 7 , ** kwargs )
429+ crMask = science .mask .getPlaneBitMask ("CR" )
430+
431+ # Configure the detection Task
432+ detectionTask = self ._setup_detection (doMerge = False , doSkySources = True )
433+
434+ # Test that no CRs are detected
435+ transients , transientSources = makeTestImage (noiseLevel = noiseLevel , noiseSeed = 8 , nSrc = 10 , ** kwargs )
436+ science .maskedImage += transients .maskedImage
437+ difference = science .clone ()
438+ difference .maskedImage -= matchedTemplate .maskedImage
439+ output = detectionTask .run (science , matchedTemplate , difference , sources )
440+ crMaskSet = (output .subtractedMeasuredExposure .mask .array & crMask ) > 0
441+ self .assertTrue (np .all (crMaskSet == 0 ))
442+
443+ crX0 = round (sources .getX ()[0 ] - science .getX0 ())
444+ crY0 = round (sources .getY ()[0 ] - science .getY0 ())
445+ crX1 = round (sources .getX ()[1 ] - science .getX0 ())
446+ crY1 = round (sources .getY ()[1 ] - science .getY0 ())
447+ # Add CR-like shape and check that CR is detected
448+ # Pick two locations on top of sources, since that is what is likely to
449+ # be missed in the first stage of CR rejection.
450+ difference .image .array [crX0 :crX0 + 1 , crY0 :crY0 + 5 ] += 1234 # Arbitrary but large flux for the CRs
451+ difference .image .array [crX1 :crX1 + 5 , crY1 :crY1 + 1 ] += 1234
452+ output = detectionTask .run (science , matchedTemplate , difference )
453+ crMaskSet = (output .subtractedMeasuredExposure .mask .array & crMask ) > 0
454+ self .assertTrue (np .all (crMaskSet [crX0 :crX0 + 1 , crY0 :crY0 + 5 ]))
455+ self .assertTrue (np .all (crMaskSet [crX1 :crX1 + 5 , crY1 :crY1 + 1 ]))
456+
417457 def test_missing_mask_planes (self ):
418458 """Check that detection runs with missing mask planes.
419459 """
0 commit comments