Coverage for tests/test_transformBoundedField.py : 21%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
|
# This file is part of afw. # # Developed for the LSST Data Management System. # This product includes software developed by the LSST Project # (https://www.lsst.org). # See the COPYRIGHT file at the top-level directory of this distribution # for details of code ownership. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>.
Tests for math.TransformBoundedField
Run with: python test_transformBoundedField.py or pytest test_transformBoundedField.py """
lambda x: x**0, lambda x: x, lambda x: 2*x**2 - 1, lambda x: (4*x**2 - 3)*x, lambda x: (8*x**2 - 8)*x**2 + 1, lambda x: ((16*x**2 - 20)*x**2 + 5)*x, ]
self.longMessage = True
# an arbitrary bounding box (not that this kind of field cares) self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(-3, 4), lsst.geom.Extent2I(5, 30))
# a list of points contained in the bbox self.pointList = lsst.geom.Box2D(self.bbox).getCorners() self.pointList.append(lsst.geom.Box2D(self.bbox).getCenter()) self.xList = np.array([p[0] for p in self.pointList]) self.yList = np.array([p[1] for p in self.pointList])
# a simple polynomial mapping coeff_f = np.array([ [1.5, 1, 0, 0], [-0.5, 1, 1, 0], [1.0, 1, 0, 1], ]) polyMap = astshim.PolyMap(coeff_f, 1) self.transform = lsst.afw.geom.TransformPoint2ToGeneric(polyMap) self.boundedField = TransformBoundedField(self.bbox, self.transform)
del self.transform
"""Test the various overloads of `evaluate` """ for point in self.pointList: # applylForward returns a vector with one entry per axis # and in this case there is just one axis predRes = self.transform.applyForward(point)[0]
res = self.boundedField.evaluate(point) self.assertFloatsAlmostEqual(res, predRes)
x, y = point res2 = self.boundedField.evaluate(x, y) self.assertFloatsAlmostEqual(res2, predRes)
resArr = self.boundedField.evaluate(self.xList, self.yList) # applylForward returns an array with one row of values per axis # and in this case there is just one axis predResArr = self.transform.applyForward(self.pointList)[0] assert_allclose(resArr, predResArr)
"""Test operator* """ maxVal = np.max(np.abs(self.transform.applyForward(self.pointList)[0])) for multFactor in (-9e99, -1.5e-7, 3.6e-7, 1.5, 9.23e99): atol = abs(maxVal*multFactor*1e-15) predResult = self.transform.applyForward(self.pointList)[0]*multFactor
scaledField1 = self.boundedField*multFactor assert_allclose(scaledField1.evaluate(self.xList, self.yList), predResult, atol=atol)
scaledField2 = multFactor*self.boundedField assert_allclose(scaledField2.evaluate(self.xList, self.yList), predResult, atol=atol)
"""The BBox should have no effect on the kind of transform being tested
Use an empty bbox as an extreme test of this """ self.assertEqual(self.boundedField.getBBox(), self.bbox)
emptyBBox = lsst.geom.Box2I() noBBoxField = TransformBoundedField(emptyBBox, self.transform) self.assertEqual(noBBoxField.getBBox(), emptyBBox)
resArr = self.boundedField.evaluate(self.xList, self.yList) resArrNoBBox = noBBoxField.evaluate(self.xList, self.yList) assert_allclose(resArr, resArrNoBBox)
"""Test persistence using writeFits and readFits
Also test operator== """ with lsst.utils.tests.getTempFilePath(".fits") as filename: self.boundedField.writeFits(filename) readField = TransformBoundedField.readFits(filename)
self.assertTrue(self.boundedField == readField) self.assertFalse(self.boundedField != readField) self.assertEqual(self.boundedField, readField)
resArr = self.boundedField.evaluate(self.xList, self.yList) readResArr = readField.evaluate(self.xList, self.yList) assert_allclose(resArr, readResArr) self.assertEqual(readField.getBBox(), self.bbox)
"""Test persistence of a TransformBoundedField whose string representation is huge """ # DM-11964 shows that CFITSIO cannot handle string fields # in binary tables that have more than 28799 characters # make sure the test has plenty of margin minChars = 10*28799 degree = 100 # make large enough that len(transform.writeString()) > minChars n_coeffs = (degree + 1)*(degree + 2)//2 coeffs = np.zeros((n_coeffs, 4), dtype=float) k = 0 for j in range(degree + 1): for i in range(degree - j + 1): coeffs[k][0] = np.random.random() coeffs[k][1] = 1 coeffs[k][2] = i coeffs[k][3] = j k += 1 chebyMap = astshim.PolyMap(coeffs, 1) transform = lsst.afw.geom.TransformPoint2ToGeneric(chebyMap) lenTransformStr = len(transform.writeString()) print(f"len transform str={lenTransformStr}; minChars={minChars}") self.assertGreater(lenTransformStr, minChars) complexBoundedField = TransformBoundedField(self.bbox, transform) with lsst.utils.tests.getTempFilePath(".fits") as filename: complexBoundedField.writeFits(filename) readField = TransformBoundedField.readFits(filename)
self.assertTrue(complexBoundedField == readField) self.assertFalse(complexBoundedField != readField) self.assertEqual(complexBoundedField, readField)
resArr = complexBoundedField.evaluate(self.xList, self.yList) readResArr = readField.evaluate(self.xList, self.yList) assert_allclose(resArr, readResArr) self.assertEqual(readField.getBBox(), self.bbox)
lsst.utils.tests.init()
lsst.utils.tests.init() unittest.main() |