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

DM-15682: Add str() for afw::Image, afw::Mask, and afw::MaskedImage #407

Merged
merged 3 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 4 additions & 8 deletions include/lsst/afw/image/ImageBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,20 +299,16 @@ class ImageBase : public lsst::daf::base::Citizen {
//@}

/// Return a reference to a single pixel (with no bounds check).
PixelReference get(lsst::geom::Point2I const & index, ImageOrigin origin);
PixelReference get(lsst::geom::Point2I const& index, ImageOrigin origin);

/// Return a const reference to a single pixel (with no bounds check).
PixelConstReference get(lsst::geom::Point2I const & index, ImageOrigin origin) const;
PixelConstReference get(lsst::geom::Point2I const& index, ImageOrigin origin) const;

/// Return a reference to a single pixel in PARENT coordinates (with no bounds check).
PixelReference operator[](lsst::geom::Point2I const & index) {
return get(index, PARENT);
}
PixelReference operator[](lsst::geom::Point2I const& index) { return get(index, PARENT); }

/// Return a reference to a single pixel in PARENT coordinates (with no bounds check).
PixelConstReference operator[](lsst::geom::Point2I const & index) const {
return get(index, PARENT);
}
PixelConstReference operator[](lsst::geom::Point2I const& index) const { return get(index, PARENT); }

/// Return the number of columns in the %image
int getWidth() const { return _gilView.width(); }
Expand Down
200 changes: 101 additions & 99 deletions python/lsst/afw/image/image/image.cc

Large diffs are not rendered by default.

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