35 """Make a gaussian noise MaskedImageF
38 - dimensions: dimensions of output array (cols, rows)
39 - sigma; sigma of image plane's noise distribution
40 - variance: constant value for variance plane
42 npSize = (dimensions[1], dimensions[0])
43 image = np.random.normal(loc=0.0, scale=sigma,
44 size=npSize).astype(np.float32)
45 mask = np.zeros(npSize, dtype=np.int32)
46 variance = np.zeros(npSize, dtype=np.float32) + variance
48 return makeMaskedImageFromArrays(image, mask, variance)
52 """!Make an image whose values are a linear ramp
54 @param[in] bbox bounding box of image (an lsst.geom.Box2I)
55 @param[in] start starting ramp value, inclusive
56 @param[in] stop ending ramp value, inclusive; if None, increase by integer values
57 @param[in] imageClass type of image (e.g. lsst.afw.image.ImageF)
60 imDim = im.getDimensions()
61 numPix = imDim[0]*imDim[1]
65 stop = start + numPix - 1
66 rampArr = np.linspace(start=start, stop=stop,
67 endpoint=
True, num=numPix, dtype=imArr.dtype)
69 imArr[:] = np.reshape(rampArr, (imDim[1], imDim[0]))
73@lsst.utils.tests.inTestCase
75 rtol=1.0e-05, atol=1e-08, msg="Images differ"):
76 """!Assert that two images are almost equal, including non-finite values
78 @param[in] testCase unittest.TestCase instance the test is part of;
79 an object supporting one method: fail(self, msgStr)
80 @param[in] image0 image 0, an lsst.afw.image.Image, lsst.afw.image.Mask,
81 or transposed numpy array (see warning)
82 @param[in] image1 image 1, an lsst.afw.image.Image, lsst.afw.image.Mask,
83 or transposed numpy array (see warning)
84 @param[in] skipMask mask of pixels to skip, or None to compare all pixels;
85 an lsst.afw.image.Mask, lsst.afw.image.Image, or transposed numpy array (see warning);
86 all non-zero pixels are skipped
87 @param[in] rtol maximum allowed relative tolerance; more info below
88 @param[in] atol maximum allowed absolute tolerance; more info below
89 @param[in] msg exception message prefix; details of the error are appended after ": "
91 The images are nearly equal if all pixels obey:
92 |val1 - val0| <= rtol*|val1| + atol
93 or, for float types, if nan/inf/-inf pixels match.
95 @warning the comparison equation is not symmetric, so in rare cases the assertion
96 may give different results depending on which image comes first.
98 @warning the axes of numpy arrays are transposed with respect to Image and Mask data.
99 Thus for example if image0 and image1 are both lsst.afw.image.ImageD with dimensions (2, 3)
100 and skipMask is a numpy array, then skipMask must have shape (3, 2).
102 @throw self.failureException (usually AssertionError) if any of the following are true
103 for un-skipped pixels:
104 - non-finite values differ in any way (e.g. one is "nan" and another is not)
105 - finite values differ by too much, as defined by atol and rtol
107 @throw TypeError if the dimensions of image0, image1 and skipMask do not match,
108 or any are not of a numeric data type.
111 image0, image1, skipMask=skipMask, rtol=rtol, atol=atol)
113 testCase.fail(f
"{msg}: {errStr}")
116@lsst.utils.tests.inTestCase
157 testCase, maskedImage0, maskedImage1,
158 doImage=True, doMask=True, doVariance=True, skipMask=None,
159 rtol=1.0e-05, atol=1e-08, msg="Masked images differ",
161 """!Assert that two masked images are nearly equal, including non-finite values
163 @param[in] testCase unittest.TestCase instance the test is part of;
164 an object supporting one method: fail(self, msgStr)
165 @param[in] maskedImage0 masked image 0 (an lsst.afw.image.MaskedImage or
166 collection of three transposed numpy arrays: image, mask, variance)
167 @param[in] maskedImage1 masked image 1 (an lsst.afw.image.MaskedImage or
168 collection of three transposed numpy arrays: image, mask, variance)
169 @param[in] doImage compare image planes if True
170 @param[in] doMask compare mask planes if True
171 @param[in] doVariance compare variance planes if True
172 @param[in] skipMask mask of pixels to skip, or None to compare all pixels;
173 an lsst.afw.image.Mask, lsst.afw.image.Image, or transposed numpy array;
174 all non-zero pixels are skipped
175 @param[in] rtol maximum allowed relative tolerance; more info below
176 @param[in] atol maximum allowed absolute tolerance; more info below
177 @param[in] msg exception message prefix; details of the error are appended after ": "
179 The mask planes must match exactly. The image and variance planes are nearly equal if all pixels obey:
180 |val1 - val0| <= rtol*|val1| + atol
181 or, for float types, if nan/inf/-inf pixels match.
183 @warning the comparison equation is not symmetric, so in rare cases the assertion
184 may give different results depending on which masked image comes first.
186 @warning the axes of numpy arrays are transposed with respect to MaskedImage data.
187 Thus for example if maskedImage0 and maskedImage1 are both lsst.afw.image.MaskedImageD
188 with dimensions (2, 3) and skipMask is a numpy array, then skipMask must have shape (3, 2).
190 @throw self.failureException (usually AssertionError) if any of the following are true
191 for un-skipped pixels:
192 - non-finite image or variance values differ in any way (e.g. one is "nan" and another is not)
193 - finite values differ by too much, as defined by atol and rtol
194 - mask pixels differ at all
196 @throw TypeError if the dimensions of maskedImage0, maskedImage1 and skipMask do not match,
197 either image or variance plane is not of a numeric data type,
198 either mask plane is not of an integer type (unsigned or signed),
199 or skipMask is not of a numeric data type.
201 if hasattr(maskedImage0,
"image"):
202 maskedImageArrList0 = (maskedImage0.image.array,
203 maskedImage0.mask.array,
204 maskedImage0.variance.array)
206 maskedImageArrList0 = maskedImage0
207 if hasattr(maskedImage1,
"image"):
208 maskedImageArrList1 = (maskedImage1.image.array,
209 maskedImage1.mask.array,
210 maskedImage1.variance.array)
212 maskedImageArrList1 = maskedImage1
214 for arrList, arg, name
in (
215 (maskedImageArrList0, maskedImage0,
"maskedImage0"),
216 (maskedImageArrList1, maskedImage1,
"maskedImage1"),
219 assert len(arrList) == 3
224 assert arrList[i].shape == arrList[1].shape
225 assert arrList[i].dtype.kind
in (
"b",
"i",
"u",
"f",
"c")
226 assert arrList[1].dtype.kind
in (
"b",
"i",
"u")
228 raise TypeError(f
"{name}={arg!r} is not a supported type")
231 for ind, (doPlane, planeName)
in enumerate(((doImage,
"image"),
233 (doVariance,
"variance"))):
237 if planeName ==
"mask":
238 errStr =
imagesDiffer(maskedImageArrList0[ind], maskedImageArrList1[ind], skipMask=skipMask,
241 errStrList.append(errStr)
243 errStr =
imagesDiffer(maskedImageArrList0[ind], maskedImageArrList1[ind],
244 skipMask=skipMask, rtol=rtol, atol=atol)
246 errStrList.append(f
"{planeName} planes differ: {errStr}")
249 errStr =
"; ".join(errStrList)
250 testCase.fail(f
"{msg}: {errStr}")
253@lsst.utils.tests.inTestCase
263def imagesDiffer(image0, image1, skipMask=None, rtol=1.0e-05, atol=1e-08):
264 """!Compare the pixels of two image or mask arrays; return True if close, False otherwise
266 @param[in] image0 image 0, an lsst.afw.image.Image, lsst.afw.image.Mask,
267 or transposed numpy array (see warning)
268 @param[in] image1 image 1, an lsst.afw.image.Image, lsst.afw.image.Mask,
269 or transposed numpy array (see warning)
270 @param[in] skipMask mask of pixels to skip, or None to compare all pixels;
271 an lsst.afw.image.Mask, lsst.afw.image.Image, or transposed numpy array (see warning);
272 all non-zero pixels are skipped
273 @param[in] rtol maximum allowed relative tolerance; more info below
274 @param[in] atol maximum allowed absolute tolerance; more info below
276 The images are nearly equal if all pixels obey:
277 |val1 - val0| <= rtol*|val1| + atol
278 or, for float types, if nan/inf/-inf pixels match.
280 @warning the comparison equation is not symmetric, so in rare cases the assertion
281 may give different results depending on which image comes first.
283 @warning the axes of numpy arrays are transposed with respect to Image and Mask data.
284 Thus for example if image0 and image1 are both lsst.afw.image.ImageD with dimensions (2, 3)
285 and skipMask is a numpy array, then skipMask must have shape (3, 2).
287 @return a string which is non-empty if the images differ
289 @throw TypeError if the dimensions of image0, image1 and skipMask do not match,
290 or any are not of a numeric data type.
293 imageArr0 = image0.getArray()
if hasattr(image0,
"getArray")
else image0
294 imageArr1 = image1.getArray()
if hasattr(image1,
"getArray")
else image1
295 skipMaskArr = skipMask.getArray()
if hasattr(skipMask,
"getArray")
else skipMask
299 (imageArr0, image0,
"image0"),
300 (imageArr1, image1,
"image1"),
302 if skipMask
is not None:
303 arrArgNameList.append((skipMaskArr, skipMask,
"skipMask"))
304 for i, (arr, arg, name)
in enumerate(arrArgNameList):
306 assert arr.dtype.kind
in (
"b",
"i",
"u",
"f",
"c")
308 raise TypeError(f
"{name!r}={arg!r} is not a supported type")
310 if arr.shape != imageArr0.shape:
311 raise TypeError(f
"{name} shape = {arr.shape} != {imageArr0.shape} = image0 shape")
317 if imageArr0.dtype.kind ==
"u":
318 imageArr0 = imageArr0.astype(
319 np.promote_types(imageArr0.dtype, np.int8))
320 if imageArr1.dtype.kind ==
"u":
321 imageArr1 = imageArr1.astype(
322 np.promote_types(imageArr1.dtype, np.int8))
324 if skipMaskArr
is not None:
325 skipMaskArr = np.array(skipMaskArr, dtype=bool)
326 maskedArr0 = np.ma.array(imageArr0, copy=
False, mask=skipMaskArr)
327 maskedArr1 = np.ma.array(imageArr1, copy=
False, mask=skipMaskArr)
328 filledArr0 = maskedArr0.filled(0.0)
329 filledArr1 = maskedArr1.filled(0.0)
332 filledArr0 = imageArr0
333 filledArr1 = imageArr1
336 np.array([np.nan], dtype=imageArr0.dtype)
337 np.array([np.nan], dtype=imageArr1.dtype)
341 valSkipMaskArr = skipMaskArr
345 nan0 = np.isnan(filledArr0)
346 nan1 = np.isnan(filledArr1)
347 if np.any(nan0 != nan1):
348 errStrList.append(
"NaNs differ")
350 posinf0 = np.isposinf(filledArr0)
351 posinf1 = np.isposinf(filledArr1)
352 if np.any(posinf0 != posinf1):
353 errStrList.append(
"+infs differ")
355 neginf0 = np.isneginf(filledArr0)
356 neginf1 = np.isneginf(filledArr1)
357 if np.any(neginf0 != neginf1):
358 errStrList.append(
"-infs differ")
360 valSkipMaskArr = nan0 | nan1 | posinf0 | posinf1 | neginf0 | neginf1
361 if skipMaskArr
is not None:
362 valSkipMaskArr |= skipMaskArr
365 valMaskedArr1 = np.ma.array(imageArr0, copy=
False, mask=valSkipMaskArr)
366 valMaskedArr2 = np.ma.array(imageArr1, copy=
False, mask=valSkipMaskArr)
367 valFilledArr1 = valMaskedArr1.filled(0.0)
368 valFilledArr2 = valMaskedArr2.filled(0.0)
370 if not np.allclose(valFilledArr1, valFilledArr2, rtol=rtol, atol=atol):
371 errArr = np.abs(valFilledArr1 - valFilledArr2)
372 maxErr = errArr.max()
373 maxAbsInd = np.where(errArr == maxErr)
374 maxAbsTuple = (maxAbsInd[0][0], maxAbsInd[1][0])
377 allcloseLimit = rtol*np.abs(valFilledArr2) + atol
378 failing = np.where(errArr >= allcloseLimit)
380 maxFailing = errArr[failing].
max()
381 maxFailingInd = np.where(errArr == maxFailing)
382 maxFailingTuple = (maxFailingInd[0][0], maxFailingInd[1][0])
383 errStr = (f
"{len(failing[0])} pixels failing np.allclose(), worst is: "
384 f
"|{valFilledArr1[maxFailingTuple]} - {valFilledArr2[maxFailingTuple]}| = "
385 f
"{maxFailing} > {allcloseLimit[maxFailingTuple]} "
386 f
"(rtol*abs(image2)+atol with rtol={rtol}, atol={atol}) "
387 f
"at position {maxFailingTuple}, and maximum absolute error: "
388 f
"|{valFilledArr1[maxAbsInd][0]} - {valFilledArr2[maxAbsInd][0]}| = {maxErr} "
389 f
"at position {maxAbsTuple}.")
390 errStrList.insert(0, errStr)
392 return "; ".join(errStrList)
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.