Skip to content

Commit

Permalink
Merge pull request #249 from lsst/tickets/DM-39934
Browse files Browse the repository at this point in the history
DM-39934: Deprecate afw MaskedImage.getArrays()
  • Loading branch information
parejkoj committed Jul 24, 2023
2 parents 90805c0 + c19305e commit a9f583a
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 43 deletions.
19 changes: 9 additions & 10 deletions python/lsst/meas/base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def __exit__(self, type_, value, tb):
self.owner._installFootprint(self.parentRecord, self.parentImage)
# Create perfect HeavyFootprints for all children; these will need to
# be modified later to account for the noise we'll add to the image.
deblend = lsst.afw.image.MaskedImageF(self.owner.exposure.getMaskedImage(), True)
deblend = lsst.afw.image.MaskedImageF(self.owner.exposure.maskedImage, True)
for record, image in self.children:
deblend.getImage().getArray()[:, :] = image.getArray()
deblend.image.array[:, :] = image.array
heavyFootprint = lsst.afw.detection.HeavyFootprintF(self.parentRecord.getFootprint(), deblend)
record.setFootprint(heavyFootprint)

Expand Down Expand Up @@ -377,7 +377,7 @@ def drawGaussian(bbox, instFlux, ellipse):
xt = t[t.XX] * x + t[t.XY] * y + t[t.X]
yt = t[t.YX] * x + t[t.YY] * y + t[t.Y]
image = lsst.afw.image.ImageF(bbox)
image.getArray()[:, :] = np.exp(-0.5*(xt**2 + yt**2))*instFlux/(2.0*ellipse.getCore().getArea())
image.array[:, :] = np.exp(-0.5*(xt**2 + yt**2))*instFlux/(2.0*ellipse.getCore().getArea())
return image

def __init__(self, bbox, threshold=10.0, exposure=None, **kwds):
Expand Down Expand Up @@ -406,7 +406,7 @@ def _installFootprint(self, record, image, setPeakSignificance=True):
for footprint in fpSet.getFootprints():
footprint.updatePeakSignificance(self.threshold.getValue())
# Update the full exposure's mask plane to indicate the detection
fpSet.setMask(self.exposure.getMaskedImage().getMask(), "DETECTED")
fpSet.setMask(self.exposure.mask, "DETECTED")
# Attach the new footprint to the exposure
if len(fpSet.getFootprints()) > 1:
raise RuntimeError("Threshold value results in multiple Footprints for a single object")
Expand Down Expand Up @@ -459,7 +459,7 @@ def addSource(self, instFlux, centroid, shape=None, setPeakSignificance=True):
# Generate a footprint for this source
self._installFootprint(record, image, setPeakSignificance)
# Actually add the source to the full exposure
self.exposure.getMaskedImage().getImage().getArray()[:, :] += image.getArray()
self.exposure.image.array[:, :] += image.array
return record, image

def addBlend(self):
Expand Down Expand Up @@ -563,9 +563,8 @@ def realize(self, noise, schema, randomSeed=1):
mapper = lsst.afw.table.SchemaMapper(self.schema)
mapper.addMinimalSchema(self.schema, True)
exposure = self.exposure.clone()
exposure.getMaskedImage().getVariance().getArray()[:, :] = noise**2
exposure.getMaskedImage().getImage().getArray()[:, :] \
+= random_state.randn(exposure.getHeight(), exposure.getWidth())*noise
exposure.variance.array[:, :] = noise**2
exposure.image.array[:, :] += random_state.randn(exposure.getHeight(), exposure.getWidth())*noise
catalog = lsst.afw.table.SourceCatalog(schema)
catalog.extend(self.catalog, mapper=mapper)
# Loop over sources and generate new HeavyFootprints that divide up
Expand All @@ -581,11 +580,11 @@ def realize(self, noise, schema, randomSeed=1):
footprint = parent.getFootprint()
parentFluxArrayNoNoise = np.zeros(footprint.getArea(), dtype=np.float32)
footprint.spans.flatten(parentFluxArrayNoNoise,
self.exposure.getMaskedImage().getImage().getArray(),
self.exposure.image.array,
self.exposure.getXY0())
parentFluxArrayNoisy = np.zeros(footprint.getArea(), dtype=np.float32)
footprint.spans.flatten(parentFluxArrayNoisy,
exposure.getMaskedImage().getImage().getArray(),
exposure.image.array,
exposure.getXY0())
oldHeavy = record.getFootprint()
fraction = (oldHeavy.getImageArray() / parentFluxArrayNoNoise)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_FlagHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def measure(self, measRecord, exposure):
raise MeasurementError(self.EDGE.doc, self.EDGE.number)

# Sum the pixels inside the bounding box
instFlux = lsst.afw.image.ImageF(exposure.getMaskedImage().getImage(), bbox).getArray().sum()
instFlux = lsst.afw.image.ImageF(exposure.image, bbox).array.sum()
measRecord.set(self.instFluxKey, instFlux)

# If there was a NaN inside the bounding box, the instFlux will still
Expand Down Expand Up @@ -289,7 +289,7 @@ def testPluginContainsNan(self):
task = lsst.meas.base.SingleFrameMeasurementTask(schema=schema, config=self.config)
exposure, cat = self.dataset.realize(noise=100.0, schema=schema, randomSeed=2)
source = cat[0]
exposure.getMaskedImage().getImage().getArray()[int(source.getY()), int(source.getX())] = np.nan
exposure.image.array[int(source.getY()), int(source.getX())] = np.nan
task.run(cat, exposure)
self.assertTrue(source.get(self.algName + "_flag"))
self.assertTrue(source.get(self.algName + "_flag_containsNan"))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ImportCentroidAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def testMeasureCentroid(self):

im = afwImage.MaskedImageF(lsst.geom.ExtentI(512, 512))
im.set(0)
arr = im.getImage().getArray()
arr = im.image.array
arr[y, x] = 1000
exp = afwImage.makeExposure(im)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_InputCount.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def ccdVennDiagram(exp, showImage=True, legendLocation='best'):
# If showImage is true, plot the data contained in exp as well as the
# boundaries
if showImage:
plt.imshow(exp.getMaskedImage().getArrays()[0], cmap='Greys', origin='lower')
plt.imshow(exp.image.array, cmap='Greys', origin='lower')
plt.colorbar()
# Adjust plot parameters and plot
plt.gca().relim()
Expand Down
16 changes: 8 additions & 8 deletions tests/test_MeasureSources.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ def testPeakLikelihoodFlux(self):
psf = afwDetection.GaussianPsf(kernelWidth, kernelWidth, sigma)
psfKernel = psf.getLocalKernel(psf.getAveragePosition())
psfImage = psf.computeKernelImage(psf.getAveragePosition())
sumPsfSq = np.sum(psfImage.getArray()**2)
psfSqArr = psfImage.getArray()**2
sumPsfSq = np.sum(psfImage.array**2)
psfSqArr = psfImage.array**2

for instFlux in (1000, 10000):
ctrInd = lsst.geom.Point2I(50, 51)
Expand Down Expand Up @@ -142,8 +142,8 @@ def testPeakLikelihoodFlux(self):
# = sqrt(sum(unfiltered variance * PSF^2)) / sum(PSF^2)
# and compare to that derived from filtered pixels;
# again, this is a test of the algorithm
varView = afwImage.ImageF(unshMImage.getVariance(), kernelBBox)
varArr = varView.getArray()
varView = afwImage.ImageF(unshMImage.variance, kernelBBox)
varArr = varView.array
unfiltPredFluxErr = math.sqrt(np.sum(varArr*psfSqArr)) / sumPsfSq
self.assertLess(abs(unfiltPredFluxErr - predFluxErr), predFluxErr * 0.01)

Expand Down Expand Up @@ -213,8 +213,8 @@ def testPixelFlags(self):
width, height = 100, 100
mi = afwImage.MaskedImageF(width, height)
exp = afwImage.makeExposure(mi)
mi.getImage().set(0)
mask = mi.getMask()
mi.image.set(0)
mask = mi.mask
sat = mask.getPlaneBitMask('SAT')
interp = mask.getPlaneBitMask('INTRP')
edge = mask.getPlaneBitMask('EDGE')
Expand Down Expand Up @@ -378,10 +378,10 @@ def makeFakeImage(bbox, centerList, instFluxList, fwhm, var):
if len(centerList) != len(instFluxList):
raise RuntimeError("len(centerList) != len(instFluxList)")
maskedImage = afwImage.MaskedImageF(bbox)
image = maskedImage.getImage()
image = maskedImage.image
for center, instFlux in zip(centerList, instFluxList):
addStar(image, center=center, instFlux=instFlux, fwhm=fwhm)
variance = maskedImage.getVariance()
variance = maskedImage.variance
variance[:] = image
variance += var
return maskedImage
Expand Down
4 changes: 2 additions & 2 deletions tests/test_NoiseReplacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, config, name, schema, metadata):

def measure(self, measRecord, exposure):
footprint = measRecord.getFootprint()
fullArray = exposure.getMaskedImage().getImage().getArray()
fullArray = exposure.image.array
insideArray = np.zeros(footprint.getArea(), dtype=fullArray.dtype)
footprint.spans.flatten(insideArray, fullArray, exposure.getXY0())
insideFlux = float(insideArray.sum())
Expand Down Expand Up @@ -94,7 +94,7 @@ def testNoiseReplacer(self, noiseSource, variance):
md['BGMEAN'] = variance
exposure.setMetadata(md)
task.run(catalog, exposure)
sumVariance = exposure.getMaskedImage().getVariance().getArray().sum()
sumVariance = exposure.variance.array.sum()
for record in catalog:
self.assertFloatsAlmostEqual(record.get("test_NoiseReplacer_inside"),
record.get("truth_instFlux"), rtol=1E-3)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_PluginLogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def measure(self, measRecord, exposure):
# Sum the pixels inside the bounding box
centerPoint = lsst.geom.Point2I(int(measRecord.getX()), int(measRecord.getY()))
bbox = lsst.geom.Box2I(centerPoint, lsst.geom.Extent2I(1, 1))
instFlux = lsst.afw.image.ImageF(exposure.getMaskedImage().getImage(), bbox).getArray().sum()
instFlux = lsst.afw.image.ImageF(exposure.image, bbox).array.sum()
measRecord.set(self.instFluxKey, instFlux)

# If there was a NaN inside the bounding box, the instFlux will still
Expand Down
6 changes: 3 additions & 3 deletions tests/test_PsfFlux.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def testMasking(self):
exposure, catalog = self.dataset.realize(10.0, schema, randomSeed=0)
record = catalog[0]
badPoint = lsst.geom.Point2I(self.center) + lsst.geom.Extent2I(3, 4)
imageArray = exposure.getMaskedImage().getImage().getArray()
maskArray = exposure.getMaskedImage().getMask().getArray()
badMask = exposure.getMaskedImage().getMask().getPlaneBitMask("BAD")
imageArray = exposure.image.array
maskArray = exposure.mask.array
badMask = exposure.mask.getPlaneBitMask("BAD")
imageArray[badPoint.getY() - exposure.getY0(), badPoint.getX() - exposure.getX0()] = np.inf
maskArray[badPoint.getY() - exposure.getY0(), badPoint.getX() - exposure.getX0()] |= badMask
# Should get an infinite value exception, because we didn't mask that
Expand Down
4 changes: 2 additions & 2 deletions tests/test_SdssCentroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def testNo2ndDerivative(self):
bbox.grow(20)
subImage = lsst.afw.image.ExposureF(exposure, bbox)
# A completely flat image will trigger the no 2nd derivative error
subImage.getMaskedImage().getImage().getArray()[:] = 0
subImage.image.array[:] = 0
task.measure(catalog, subImage)
self.assertTrue(catalog[0].get("base_SdssCentroid_flag"))
self.assertTrue(catalog[0].get("base_SdssCentroid_flag_noSecondDerivative"))
Expand All @@ -182,7 +182,7 @@ def testNotAtMaximum(self):
bbox.grow(20)
subImage = lsst.afw.image.ExposureF(exposure, bbox)
# zero out the central region, which will destroy the maximum
subImage.getMaskedImage().getImage().getArray()[18:22, 18:22] = 0
subImage.image.array[18:22, 18:22] = 0
task.measure(catalog, subImage)
self.assertTrue(catalog[0].get("base_SdssCentroid_flag"))
self.assertTrue(catalog[0].get("base_SdssCentroid_flag_notAtMaximum"))
Expand Down
26 changes: 13 additions & 13 deletions tests/test_Variance.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ def setUp(self):

# Initial setup of an image
exp = afwImage.ExposureF(size, size)
image = exp.getMaskedImage().getImage()
mask = exp.getMaskedImage().getMask()
var = exp.getMaskedImage().getVariance()
image = exp.image
mask = exp.mask
var = exp.variance
image.set(0.0)
mask.set(0)
var.getArray()[:, :] = variancePlane
var.array[:, :] = variancePlane

# Put down a PSF
psfSize = int(6*width + 1) # Size of PSF image; must be odd
Expand All @@ -74,17 +74,17 @@ def setUp(self):
# Put in some bad pixels to ensure they're ignored
for i in range(-5, 6):
bad = size//2 + i*width
var.getArray()[bad, :] = float("nan")
mask.getArray()[bad, :] = mask.getPlaneBitMask("BAD")
var.getArray()[:, bad] = float("nan")
mask.getArray()[:, bad] = mask.getPlaneBitMask("BAD")
var.array[bad, :] = float("nan")
mask.array[bad, :] = mask.getPlaneBitMask("BAD")
var.array[:, bad] = float("nan")
mask.array[:, bad] = mask.getPlaneBitMask("BAD")

# Put in some unmasked bad pixels outside the expected aperture, to
# ensure the aperture is working
var.getArray()[0, 0] = float("nan")
var.getArray()[0, -1] = float("nan")
var.getArray()[-1, 0] = float("nan")
var.getArray()[-1, -1] = float("nan")
var.array[0, 0] = float("nan")
var.array[0, -1] = float("nan")
var.array[-1, 0] = float("nan")
var.array[-1, -1] = float("nan")

if display:
import lsst.afw.display as afwDisplay
Expand Down Expand Up @@ -149,7 +149,7 @@ def testVariance(self):

def testEmptyFootprint(self):
# Set the pixel mask for all pixels to ``BAD`` and remeasure.
self.mask.getArray()[:, :] = self.mask.getPlaneBitMask("BAD")
self.mask.array[:, :] = self.mask.getPlaneBitMask("BAD")
self.task.run(self.catalog, self.exp)

# The computed variance should be NaN and flag_emptyFootprint should
Expand Down

0 comments on commit a9f583a

Please sign in to comment.