Skip to content

Commit

Permalink
Add str/repr for Image, Mask, MaskedImage
Browse files Browse the repository at this point in the history
Delegate to the numpy string output, to let it do the hard work of collapsing
big arrays.

Don't delegate maskedImage printing to the components, so we're not printing
the bbox three times.
  • Loading branch information
parejkoj committed Oct 22, 2018
1 parent 5e12c3f commit fe524df
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions python/lsst/afw/image/image/imageContinued.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def __reduce__(self):
from lsst.afw.fits import reduceToFits
return reduceToFits(self)

def __str__(self):
return "{}, bbox={}".format(self.array, self.getBBox())

def __repr__(self):
return "{}.{}={}".format(self.__module__, self.__class__.__name__, str(self))

readFitsWithOptions = classmethod(imageReadFitsWithOptions)

writeFitsWithOptions = imageWriteFitsWithOptions
Expand Down
6 changes: 6 additions & 0 deletions python/lsst/afw/image/image/maskContinued.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def __reduce__(self):
from lsst.afw.fits import reduceToFits
return reduceToFits(self)

def __str__(self):
return "{}, bbox={}, maskPlaneDict={}".format(self.array, self.getBBox(), self.getMaskPlaneDict())

def __repr__(self):
return "{}.{}={}".format(self.__module__, self.__class__.__name__, str(self))

readFitsWithOptions = classmethod(imageReadFitsWithOptions)

writeFitsWithOptions = imageWriteFitsWithOptions
Expand Down
11 changes: 11 additions & 0 deletions python/lsst/afw/image/maskedImage/maskedImageContinued.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ def __reduce__(self):
from lsst.afw.fits import reduceToFits
return reduceToFits(self)

def __str__(self):
string = "image={},\nmask={}, maskPlaneDict={}\nvariance={}, bbox={}"
return string.format(self.image.array,
self.mask.array,
self.mask.getMaskPlaneDict(),
self.variance.array,
self.getBBox())

def __repr__(self):
return "{}.{}=({})".format(self.__module__, self.__class__.__name__, str(self))

readFitsWithOptions = classmethod(imageReadFitsWithOptions)

writeFitsWithOptions = exposureWriteFitsWithOptions
Expand Down
17 changes: 17 additions & 0 deletions tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,23 @@ def testClone(self):
im2[0, 0] += 10
self.assertNotEqual(float(im[0, 0]), float(im2[0, 0]))

def testString(self):
imageF = afwImage.ImageF(100, 100)
imageDSmall = afwImage.ImageD(2, 2)
imageISmall = afwImage.ImageI(2, 2)
imageU = afwImage.ImageU(100, 100)

# numpy defaults to threshold=1000 for collapsing array output
self.assertIn("...", str(imageF))
self.assertIn("bbox=%s" % str(imageF.getBBox()), str(imageF))

# for small arrays, numpy's representation prints the whole array
self.assertIn("[[0. 0.]\n [0. 0.]]", str(imageDSmall))
self.assertIn("[[0 0]\n [0 0]]", str(imageISmall))

self.assertIn("ImageF=", repr(imageF))
self.assertIn("ImageU=", repr(imageU))


class DecoratedImageTestCase(lsst.utils.tests.TestCase):
"""A test case for DecoratedImage"""
Expand Down
13 changes: 13 additions & 0 deletions tests/test_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,19 @@ def testInterpret(self):
self.assertEqual(self.Mask.interpret(allBits),
",".join(sorted(planes.keys())))

def testString(self):
mask = afwImage.Mask(100, 100)
# numpy defaults to threshold=1000 for collapsing array output
self.assertIn("...", str(mask))
self.assertIn("bbox=%s"%str(mask.getBBox()), str(mask))
self.assertIn("maskPlaneDict=%s"%str(mask.getMaskPlaneDict()), str(mask))

# for small arrays, numpy's representation prints the whole array
smallMask = afwImage.Mask(2, 2)
self.assertIn("[[0 0]\n [0 0]]", str(smallMask))

self.assertIn("MaskX=", repr(mask))


class OldMaskTestCase(unittest.TestCase):
"""A test case for Mask (based on Mask_1.cc); these are taken over from the DC2 fw tests
Expand Down
11 changes: 11 additions & 0 deletions tests/test_maskedImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,17 @@ def testConversionToScalar(self):
# actually, can't convert (img, msk, var) to scalar
self.assertRaises(TypeError, float, im[0, 0])

def testString(self):
image = afwImage.MaskedImageF(100, 100)
self.assertIn("image=", str(image))
self.assertIn("mask=", str(image))
self.assertIn("variance=", str(image))
self.assertIn("...", str(image))
self.assertIn("bbox=%s"%str(image.getBBox()), str(image))
self.assertIn("maskPlaneDict=%s"%str(image.mask.getMaskPlaneDict()), str(image))

self.assertIn("MaskedImageF=(", repr(image))


def printImg(img):
print("%4s " % "", end=' ')
Expand Down

0 comments on commit fe524df

Please sign in to comment.