1 from __future__
import absolute_import, division
31 """Make a gaussian noise MaskedImageF
34 - dimensions: dimensions of output array (cols, rows)
35 - sigma; sigma of image plane's noise distribution
36 - variance: constant value for variance plane
38 npSize = (dimensions[1], dimensions[0])
39 image = numpy.random.normal(loc=0.0, scale=sigma, size=npSize).astype(numpy.float32)
40 mask = numpy.zeros(npSize, dtype=numpy.uint16)
41 variance = numpy.zeros(npSize, dtype=numpy.float32) + variance
43 return afwImage.makeMaskedImageFromArrays(image, mask, variance)
45 def imagesDiffer(imageArr1, imageArr2, skipMaskArr=None, rtol=1.0e-05, atol=1e-08):
46 """Compare the pixels of two image arrays; return True if close, False otherwise
49 - image1: first image to compare
50 - image2: second image to compare
51 - skipMaskArr: pixels to ignore; nonzero values are skipped
52 - rtol: relative tolerance (see below)
53 - atol: absolute tolerance (see below)
55 rtol and atol are positive, typically very small numbers.
56 The relative difference (rtol * abs(b)) and the absolute difference "atol" are added together
57 to compare against the absolute difference between "a" and "b".
59 Return a string describing the error if the images differ significantly, an empty string otherwise
62 if skipMaskArr
is not None:
63 maskedArr1 = numpy.ma.array(imageArr1, copy=
False, mask = skipMaskArr)
64 maskedArr2 = numpy.ma.array(imageArr2, copy=
False, mask = skipMaskArr)
65 filledArr1 = maskedArr1.filled(0.0)
66 filledArr2 = maskedArr2.filled(0.0)
68 filledArr1 = imageArr1
69 filledArr2 = imageArr2
71 nan1 = numpy.isnan(filledArr1)
72 nan2 = numpy.isnan(filledArr2)
73 if numpy.any(nan1 != nan2):
74 retStrs.append(
"NaNs differ")
76 posinf1 = numpy.isposinf(filledArr1)
77 posinf2 = numpy.isposinf(filledArr2)
78 if numpy.any(posinf1 != posinf2):
79 retStrs.append(
"+infs differ")
81 neginf1 = numpy.isneginf(filledArr1)
82 neginf2 = numpy.isneginf(filledArr2)
83 if numpy.any(neginf1 != neginf2):
84 retStrs.append(
"-infs differ")
87 valSkipMaskArr = nan1 | nan2 | posinf1 | posinf2 | neginf1 | neginf2
88 if skipMaskArr
is not None:
89 valSkipMaskArr |= skipMaskArr
90 valMaskedArr1 = numpy.ma.array(imageArr1, copy=
False, mask = valSkipMaskArr)
91 valMaskedArr2 = numpy.ma.array(imageArr2, copy=
False, mask = valSkipMaskArr)
92 valFilledArr1 = valMaskedArr1.filled(0.0)
93 valFilledArr2 = valMaskedArr2.filled(0.0)
95 if not numpy.allclose(valFilledArr1, valFilledArr2, rtol=rtol, atol=atol):
96 errArr = numpy.abs(valFilledArr1 - valFilledArr2)
98 maxPosInd = numpy.where(errArr==maxErr)
99 maxPosTuple = (maxPosInd[1][0], maxPosInd[0][0])
100 errStr =
"maxDiff=%s at position %s; value=%s vs. %s" % \
101 (maxErr, maxPosTuple, valFilledArr1[maxPosInd][0], valFilledArr2[maxPosInd][0])
102 retStrs.insert(0, errStr)
103 return "; ".join(retStrs)
106 """Compare the pixels of two mask arrays; return True if they match, False otherwise
109 - mask1: first image to compare
110 - mask2: second image to compare
111 - skipMaskArr: pixels to ignore; nonzero values are skipped
113 Return a string describing the error if the images differ significantly, an empty string otherwise
116 if skipMaskArr
is not None:
117 maskedArr1 = numpy.ma.array(maskArr1, copy=
False, mask = skipMaskArr)
118 maskedArr2 = numpy.ma.array(maskArr2, copy=
False, mask = skipMaskArr)
119 filledArr1 = maskedArr1.filled(0.0)
120 filledArr2 = maskedArr2.filled(0.0)
122 filledArr1 = maskArr1
123 filledArr2 = maskArr2
125 if numpy.any(filledArr1 != filledArr2):
126 errArr = numpy.abs(filledArr1 - filledArr2)
127 maxErr = errArr.max()
128 maxPosInd = numpy.where(errArr==maxErr)
129 maxPosTuple = (maxPosInd[1][0], maxPosInd[0][0])
130 retStr =
"maxDiff=%s at position %s; value=%s vs. %s" % \
131 (maxErr, maxPosTuple, filledArr1[maxPosInd][0], filledArr2[maxPosInd][0])
132 retStr =
"masks differ"
136 doImage=
True, doMask=
True, doVariance=
True, skipMaskArr=
None, rtol=1.0e-05, atol=1e-08):
137 """Compare pixels from two masked images
140 - maskedImageArrSet1: first masked image to compare as (image, mask, variance) arrays
141 - maskedImageArrSet2: second masked image to compare as (image, mask, variance) arrays
142 - doImage: compare image planes if True
143 - doMask: compare mask planes if True
144 - doVariance: compare variance planes if True
145 - skipMaskArr: pixels to ingore on the image, mask and variance arrays; nonzero values are skipped
146 - rtol: relative tolerance (see below)
147 - atol: absolute tolerance (see below)
149 rtol and atol are positive, typically very small numbers.
150 The relative difference (rtol * abs(b)) and the absolute difference "atol" are added together
151 to compare against the absolute difference between "a" and "b".
153 Return a string describing the error if the images differ significantly, an empty string otherwise
156 for ind, (doPlane, planeName)
in enumerate(((doImage,
"image"),
158 (doVariance,
"variance"))):
162 if planeName ==
"mask":
163 errStr =
masksDiffer(maskedImageArrSet1[ind], maskedImageArrSet2[ind], skipMaskArr=skipMaskArr)
165 retStrs.append(errStr)
167 errStr =
imagesDiffer(maskedImageArrSet1[ind], maskedImageArrSet2[ind],
168 skipMaskArr=skipMaskArr, rtol=rtol, atol=atol)
170 retStrs.append(
"%s planes differ: %s" % (planeName, errStr))
171 return " | ".join(retStrs)
def makeGaussianNoiseMaskedImage