25 __all__ = [
"assertImagesAlmostEqual",
"assertImagesEqual",
"assertMasksEqual",
26 "assertMaskedImagesAlmostEqual",
"assertMaskedImagesEqual",
27 "assertImagesNearlyEqual",
"assertMaskedImagesNearlyEqual"]
34 from .image
import ImageF
35 from .basicUtils
import makeMaskedImageFromArrays
39 """Make a gaussian noise MaskedImageF 42 - dimensions: dimensions of output array (cols, rows) 43 - sigma; sigma of image plane's noise distribution 44 - variance: constant value for variance plane 46 npSize = (dimensions[1], dimensions[0])
47 image = np.random.normal(loc=0.0, scale=sigma,
48 size=npSize).astype(np.float32)
49 mask = np.zeros(npSize, dtype=np.int32)
50 variance = np.zeros(npSize, dtype=np.float32) + variance
56 """!Make an image whose values are a linear ramp 58 @param[in] bbox bounding box of image (an lsst.afw.geom.Box2I) 59 @param[in] start starting ramp value, inclusive 60 @param[in] stop ending ramp value, inclusive; if None, increase by integer values 61 @param[in] imageClass type of image (e.g. lsst.afw.image.ImageF) 64 imDim = im.getDimensions()
65 numPix = imDim[0]*imDim[1]
69 stop = start + numPix - 1
70 rampArr = np.linspace(start=start, stop=stop,
71 endpoint=
True, num=numPix, dtype=imArr.dtype)
73 imArr[:] = np.reshape(rampArr, (imDim[1], imDim[0]))
77 @lsst.utils.tests.inTestCase
79 rtol=1.0e-05, atol=1e-08, msg="Images differ"):
80 """!Assert that two images are almost equal, including non-finite values 82 @param[in] testCase unittest.TestCase instance the test is part of; 83 an object supporting one method: fail(self, msgStr) 84 @param[in] image0 image 0, an lsst.afw.image.Image, lsst.afw.image.Mask, 85 or transposed numpy array (see warning) 86 @param[in] image1 image 1, an lsst.afw.image.Image, lsst.afw.image.Mask, 87 or transposed numpy array (see warning) 88 @param[in] skipMask mask of pixels to skip, or None to compare all pixels; 89 an lsst.afw.image.Mask, lsst.afw.image.Image, or transposed numpy array (see warning); 90 all non-zero pixels are skipped 91 @param[in] rtol maximum allowed relative tolerance; more info below 92 @param[in] atol maximum allowed absolute tolerance; more info below 93 @param[in] msg exception message prefix; details of the error are appended after ": " 95 The images are nearly equal if all pixels obey: 96 |val1 - val0| <= rtol*|val1| + atol 97 or, for float types, if nan/inf/-inf pixels match. 99 @warning the comparison equation is not symmetric, so in rare cases the assertion 100 may give different results depending on which image comes first. 102 @warning the axes of numpy arrays are transposed with respect to Image and Mask data. 103 Thus for example if image0 and image1 are both lsst.afw.image.ImageD with dimensions (2, 3) 104 and skipMask is a numpy array, then skipMask must have shape (3, 2). 106 @throw self.failureException (usually AssertionError) if any of the following are true 107 for un-skipped pixels: 108 - non-finite values differ in any way (e.g. one is "nan" and another is not) 109 - finite values differ by too much, as defined by atol and rtol 111 @throw TypeError if the dimensions of image0, image1 and skipMask do not match, 112 or any are not of a numeric data type. 115 image0, image1, skipMask=skipMask, rtol=rtol, atol=atol)
117 testCase.fail(
"%s: %s" % (msg, errStr))
120 @lsst.utils.tests.inTestCase
122 """!Assert that two images are exactly equal, including non-finite values. 124 All arguments are forwarded to assertAnglesAlmostEqual aside from atol and rtol, 125 which are set to zero. 130 @lsst.utils.tests.inTestCase
132 """!Assert that two masks are equal 134 @param[in] testCase unittest.TestCase instance the test is part of; 135 an object supporting one method: fail(self, msgStr) 136 @param[in] mask0 mask 0, an lsst.afw.image.Mask, lsst.afw.image.Image, 137 or transposed numpy array (see warning) 138 @param[in] mask1 mask 1, an lsst.afw.image.Mask, lsst.afw.image.Image, 139 or transposed numpy array (see warning) 140 @param[in] skipMask mask of pixels to skip, or None to compare all pixels; 141 an lsst.afw.image.Mask, lsst.afw.image.Image, or transposed numpy array (see warning); 142 all non-zero pixels are skipped 143 @param[in] msg exception message prefix; details of the error are appended after ": " 145 @warning the axes of numpy arrays are transposed with respect to Mask and Image. 146 Thus for example if mask0 and mask1 are both lsst.afw.image.Mask with dimensions (2, 3) 147 and skipMask is a numpy array, then skipMask must have shape (3, 2). 149 @throw self.failureException (usually AssertionError) if any any un-skipped pixels differ 151 @throw TypeError if the dimensions of mask0, mask1 and skipMask do not match, 152 or any are not of a numeric data type. 154 errStr =
imagesDiffer(mask0, mask1, skipMask=skipMask, rtol=0, atol=0)
156 testCase.fail(
"%s: %s" % (msg, errStr))
159 @lsst.utils.tests.inTestCase
161 testCase, maskedImage0, maskedImage1,
162 doImage=True, doMask=True, doVariance=True, skipMask=None,
163 rtol=1.0e-05, atol=1e-08, msg="Masked images differ",
165 """!Assert that two masked images are nearly equal, including non-finite values 167 @param[in] testCase unittest.TestCase instance the test is part of; 168 an object supporting one method: fail(self, msgStr) 169 @param[in] maskedImage0 masked image 0 (an lsst.afw.image.MaskedImage or 170 collection of three transposed numpy arrays: image, mask, variance) 171 @param[in] maskedImage1 masked image 1 (an lsst.afw.image.MaskedImage or 172 collection of three transposed numpy arrays: image, mask, variance) 173 @param[in] doImage compare image planes if True 174 @param[in] doMask compare mask planes if True 175 @param[in] doVariance compare variance planes if True 176 @param[in] skipMask mask of pixels to skip, or None to compare all pixels; 177 an lsst.afw.image.Mask, lsst.afw.image.Image, or transposed numpy array; 178 all non-zero pixels are skipped 179 @param[in] rtol maximum allowed relative tolerance; more info below 180 @param[in] atol maximum allowed absolute tolerance; more info below 181 @param[in] msg exception message prefix; details of the error are appended after ": " 183 The mask planes must match exactly. The image and variance planes are nearly equal if all pixels obey: 184 |val1 - val0| <= rtol*|val1| + atol 185 or, for float types, if nan/inf/-inf pixels match. 187 @warning the comparison equation is not symmetric, so in rare cases the assertion 188 may give different results depending on which masked image comes first. 190 @warning the axes of numpy arrays are transposed with respect to MaskedImage data. 191 Thus for example if maskedImage0 and maskedImage1 are both lsst.afw.image.MaskedImageD 192 with dimensions (2, 3) and skipMask is a numpy array, then skipMask must have shape (3, 2). 194 @throw self.failureException (usually AssertionError) if any of the following are true 195 for un-skipped pixels: 196 - non-finite image or variance values differ in any way (e.g. one is "nan" and another is not) 197 - finite values differ by too much, as defined by atol and rtol 198 - mask pixels differ at all 200 @throw TypeError if the dimensions of maskedImage0, maskedImage1 and skipMask do not match, 201 either image or variance plane is not of a numeric data type, 202 either mask plane is not of an integer type (unsigned or signed), 203 or skipMask is not of a numeric data type. 205 maskedImageArrList0 = maskedImage0.getArrays()
if hasattr(
206 maskedImage0,
"getArrays")
else maskedImage0
207 maskedImageArrList1 = maskedImage1.getArrays()
if hasattr(
208 maskedImage1,
"getArrays")
else maskedImage1
210 for arrList, arg, name
in (
211 (maskedImageArrList0, maskedImage0,
"maskedImage0"),
212 (maskedImageArrList1, maskedImage1,
"maskedImage1"),
215 assert len(arrList) == 3
220 assert arrList[i].shape == arrList[1].shape
221 assert arrList[i].dtype.kind
in (
"b",
"i",
"u", "f", "c")
222 assert arrList[1].dtype.kind
in (
"b",
"i",
"u") 224 raise TypeError(
"%s=%r is not a supported type" % (name, arg))
227 for ind, (doPlane, planeName)
in enumerate(((doImage,
"image"),
229 (doVariance,
"variance"))):
233 if planeName ==
"mask":
234 errStr =
imagesDiffer(maskedImageArrList0[ind], maskedImageArrList1[ind], skipMask=skipMask,
237 errStrList.append(errStr)
239 errStr =
imagesDiffer(maskedImageArrList0[ind], maskedImageArrList1[ind],
240 skipMask=skipMask, rtol=rtol, atol=atol)
242 errStrList.append(
"%s planes differ: %s" % (planeName, errStr))
245 testCase.fail(
"%s: %s" % (msg,
"; ".join(errStrList)))
248 @lsst.utils.tests.inTestCase
250 """!Assert that two masked images are exactly equal, including non-finite values. 252 All arguments are forwarded to assertMaskedImagesAlmostEqual aside from atol and rtol, 253 which are set to zero. 258 def imagesDiffer(image0, image1, skipMask=None, rtol=1.0e-05, atol=1e-08):
259 """!Compare the pixels of two image or mask arrays; return True if close, False otherwise 261 @param[in] image0 image 0, an lsst.afw.image.Image, lsst.afw.image.Mask, 262 or transposed numpy array (see warning) 263 @param[in] image1 image 1, an lsst.afw.image.Image, lsst.afw.image.Mask, 264 or transposed numpy array (see warning) 265 @param[in] skipMask mask of pixels to skip, or None to compare all pixels; 266 an lsst.afw.image.Mask, lsst.afw.image.Image, or transposed numpy array (see warning); 267 all non-zero pixels are skipped 268 @param[in] rtol maximum allowed relative tolerance; more info below 269 @param[in] atol maximum allowed absolute tolerance; more info below 271 The images are nearly equal if all pixels obey: 272 |val1 - val0| <= rtol*|val1| + atol 273 or, for float types, if nan/inf/-inf pixels match. 275 @warning the comparison equation is not symmetric, so in rare cases the assertion 276 may give different results depending on which image comes first. 278 @warning the axes of numpy arrays are transposed with respect to Image and Mask data. 279 Thus for example if image0 and image1 are both lsst.afw.image.ImageD with dimensions (2, 3) 280 and skipMask is a numpy array, then skipMask must have shape (3, 2). 282 @return a string which is non-empty if the images differ 284 @throw TypeError if the dimensions of image0, image1 and skipMask do not match, 285 or any are not of a numeric data type. 288 imageArr0 = image0.getArray()
if hasattr(image0,
"getArray")
else image0
289 imageArr1 = image1.getArray()
if hasattr(image1,
"getArray")
else image1
290 skipMaskArr = skipMask.getArray()
if hasattr(skipMask,
"getArray")
else skipMask
294 (imageArr0, image0,
"image0"),
295 (imageArr1, image1,
"image1"),
297 if skipMask
is not None:
298 arrArgNameList.append((skipMaskArr, skipMask,
"skipMask"))
299 for i, (arr, arg, name)
in enumerate(arrArgNameList):
301 assert arr.dtype.kind
in (
"b",
"i",
"u", "f", "c")
303 raise TypeError(
"%r=%r is not a supported type" % (name, arg))
305 if arr.shape != imageArr0.shape:
306 raise TypeError(
"%s shape = %s != %s = image0 shape" %
307 (name, arr.shape, imageArr0.shape))
313 if imageArr0.dtype.kind ==
"u": 314 imageArr0 = imageArr0.astype( 315 np.promote_types(imageArr0.dtype, np.int8)) 316 if imageArr1.dtype.kind ==
"u": 317 imageArr1 = imageArr1.astype( 318 np.promote_types(imageArr1.dtype, np.int8)) 320 if skipMaskArr
is not None:
321 skipMaskArr = np.array(skipMaskArr, dtype=bool)
322 maskedArr0 = np.ma.array(imageArr0, copy=
False, mask=skipMaskArr)
323 maskedArr1 = np.ma.array(imageArr1, copy=
False, mask=skipMaskArr)
324 filledArr0 = maskedArr0.filled(0.0)
325 filledArr1 = maskedArr1.filled(0.0)
328 filledArr0 = imageArr0
329 filledArr1 = imageArr1
332 np.array([np.nan], dtype=imageArr0.dtype)
333 np.array([np.nan], dtype=imageArr1.dtype)
337 valSkipMaskArr = skipMaskArr
341 nan0 = np.isnan(filledArr0)
342 nan1 = np.isnan(filledArr1)
343 if np.any(nan0 != nan1):
344 errStrList.append(
"NaNs differ")
346 posinf0 = np.isposinf(filledArr0)
347 posinf1 = np.isposinf(filledArr1)
348 if np.any(posinf0 != posinf1):
349 errStrList.append(
"+infs differ")
351 neginf0 = np.isneginf(filledArr0)
352 neginf1 = np.isneginf(filledArr1)
353 if np.any(neginf0 != neginf1):
354 errStrList.append(
"-infs differ")
356 valSkipMaskArr = nan0 | nan1 | posinf0 | posinf1 | neginf0 | neginf1
357 if skipMaskArr
is not None:
358 valSkipMaskArr |= skipMaskArr
361 valMaskedArr1 = np.ma.array(imageArr0, copy=
False, mask=valSkipMaskArr)
362 valMaskedArr2 = np.ma.array(imageArr1, copy=
False, mask=valSkipMaskArr)
363 valFilledArr1 = valMaskedArr1.filled(0.0)
364 valFilledArr2 = valMaskedArr2.filled(0.0)
366 if not np.allclose(valFilledArr1, valFilledArr2, rtol=rtol, atol=atol):
367 errArr = np.abs(valFilledArr1 - valFilledArr2)
368 maxErr = errArr.max()
369 maxPosInd = np.where(errArr == maxErr)
370 maxPosTuple = (maxPosInd[1][0], maxPosInd[0][0])
371 errStr =
"maxDiff=%s at position %s; value=%s vs. %s" % \
372 (maxErr, maxPosTuple,
373 valFilledArr1[maxPosInd][0], valFilledArr2[maxPosInd][0])
374 errStrList.insert(0, errStr)
376 return "; ".join(errStrList)
379 @lsst.utils.tests.inTestCase
381 warnings.warn(
"Deprecated. Use assertImagesAlmostEqual",
382 DeprecationWarning, 2)
386 @lsst.utils.tests.inTestCase
388 warnings.warn(
"Deprecated. Use assertMaskedImagesAlmostEqual",
389 DeprecationWarning, 2)
def assertImagesAlmostEqual(testCase, image0, image1, skipMask=None, rtol=1.0e-05, atol=1e-08, msg="Images differ")
Assert that two images are almost equal, including non-finite values.
def assertMaskedImagesNearlyEqual(args, kwargs)
def assertMaskedImagesAlmostEqual(testCase, maskedImage0, maskedImage1, doImage=True, doMask=True, doVariance=True, skipMask=None, rtol=1.0e-05, atol=1e-08, msg="Masked images differ")
Assert that two masked images are nearly equal, including non-finite values.
def assertMaskedImagesEqual(args, kwds)
Assert that two masked images are exactly equal, including non-finite values.
def makeGaussianNoiseMaskedImage(dimensions, sigma, variance=1.0)
def imagesDiffer(image0, image1, skipMask=None, rtol=1.0e-05, atol=1e-08)
Compare the pixels of two image or mask arrays; return True if close, False otherwise.
def assertImagesEqual(args, kwds)
Assert that two images are exactly equal, including non-finite values.
def makeMaskedImageFromArrays(image, mask=None, variance=None)
def assertMasksEqual(testCase, mask0, mask1, skipMask=None, msg="Masks differ")
Assert that two masks are equal.
def assertImagesNearlyEqual(args, kwargs)
def makeRampImage(bbox, start=0, stop=None, imageClass=ImageF)
Make an image whose values are a linear ramp.