def testChebyshev1Function1D(self): errMsg = "{}: {} != {} for x={}, xMin={}, xMax={}, xNorm={}, params={}; {}" maxOrder = 6 deltaParam = 0.3 ranges = ((-1, 1), (-1, 0), (0, 1), (-17, -2), (-65.3, 2.132)) rangeIter = itertools.cycle(ranges) nPoints = 9 for order in range(maxOrder + 1): xMin, xMax = next(rangeIter) xMean = (xMin + xMax) / 2.0 xDelta = (xMax - xMin) / float(nPoints - 1) f = afwMath.Chebyshev1Function1D(order, xMin, xMax) numParams = f.getNParameters() params = np.arange(deltaParam, deltaParam * numParams + (deltaParam / 2.0), deltaParam) f.setParameters(params) g = afwMath.Chebyshev1Function1D(params, xMin, xMax) h = f.clone() self.assertEqual(f.getNParameters(), g.getNParameters()) self.assertEqual(f.getMinX(), xMin) self.assertEqual(f.getMaxX(), xMax) self.assertEqual(f.getOrder(), order) self.assertEqual(g.getMinX(), xMin) self.assertEqual(g.getMaxX(), xMax) self.assertEqual(g.getOrder(), order) minXNorm = None maxXNorm = None for x in np.arange(xMin, xMax + xDelta/2.0, xDelta): xNorm = 2.0 * (x - xMean) / float(xMax - xMin) if minXNorm is None or xNorm < minXNorm: minXNorm = xNorm if maxXNorm is None or xNorm > maxXNorm: maxXNorm = xNorm predVal = referenceChebyshev1Polynomial1(xNorm, params) msg = errMsg.format(type(f).__name__, f(x), predVal, x, xMin, xMax, xNorm, params, "order constructor") self.assertFloatsAlmostEqual( f(x), predVal, msg=msg, atol=self.atol) msg = errMsg.format(type(g).__name__, g(x), predVal, x, xMin, xMax, xNorm, params, "params constructor") self.assertFloatsAlmostEqual( g(x), predVal, msg=msg, atol=self.atol) msg = errMsg.format(type(h).__name__, h(x), predVal, x, xMin, xMax, xNorm, params, "clone") self.assertFloatsAlmostEqual( h(x), predVal, msg=msg, atol=self.atol) msg = self.normErr.format("x", xMin, xMax, minXNorm, maxXNorm) self.assertFloatsAlmostEqual( minXNorm, -1., msg=msg, atol=self.atol) self.assertFloatsAlmostEqual(maxXNorm, 1., msg=msg, atol=self.atol)
def testCast(self): for instance in (afwMath.Chebyshev1Function1F(2), afwMath.GaussianFunction1F(1.0), afwMath.LanczosFunction1F(3), afwMath.NullFunction1F(), afwMath.PolynomialFunction1F(2)): Class = type(instance) base = instance.clone() self.assertEqual(type(base), afwMath.Function1F) derived = Class.cast(base) self.assertEqual(type(derived), Class) for instance in (afwMath.Chebyshev1Function1D(2), afwMath.GaussianFunction1D(1.0), afwMath.LanczosFunction1D(3), afwMath.NullFunction1D(), afwMath.PolynomialFunction1D(2)): Class = type(instance) base = instance.clone() self.assertEqual(type(base), afwMath.Function1D) derived = Class.cast(base) self.assertEqual(type(derived), Class) for instance in (afwMath.Chebyshev1Function2F(2), afwMath.GaussianFunction2F(1.0, 1.0), afwMath.DoubleGaussianFunction2F(1.0), afwMath.LanczosFunction2F(3), afwMath.NullFunction2F(), afwMath.PolynomialFunction2F(2)): Class = type(instance) base = instance.clone() self.assertEqual(type(base), afwMath.Function2F) derived = Class.cast(base) self.assertEqual(type(derived), Class) for instance in (afwMath.Chebyshev1Function2D(2), afwMath.GaussianFunction2D(1.0, 1.0), afwMath.DoubleGaussianFunction2D(1.0), afwMath.LanczosFunction2D(3), afwMath.NullFunction2D(), afwMath.PolynomialFunction2D(2)): Class = type(instance) base = instance.clone() self.assertEqual(type(base), afwMath.Function2D) derived = Class.cast(base) self.assertEqual(type(derived), Class)
def testChebyshev1Function1D(self): """A test for Chebyshev1Function1D""" maxOrder = 6 deltaParam = 0.3 allParams = numpy.arange( deltaParam, deltaParam * (maxOrder + 1) + (deltaParam / 2.0), deltaParam) ranges = ((-1, 1), (-1, 0), (0, 1), (-17, -2), (-65.3, 2.132)) rangeIter = itertools.cycle(ranges) nPoints = 9 for order in range(maxOrder + 1): xMin, xMax = rangeIter.next() xMean = (xMin + xMax) / 2.0 xDelta = (xMax - xMin) / float(nPoints - 1) f = afwMath.Chebyshev1Function1D(order, xMin, xMax) numParams = f.getNParameters() params = numpy.arange(deltaParam, deltaParam * numParams + (deltaParam / 2.0), deltaParam) f.setParameters(params) g = afwMath.Chebyshev1Function1D(params, xMin, xMax) h = f.clone() self.assertEqual(f.getNParameters(), g.getNParameters()) self.assertEqual(f.getMinX(), xMin) self.assertEqual(f.getMaxX(), xMax) self.assertEqual(f.getOrder(), order) self.assertEqual(g.getMinX(), xMin) self.assertEqual(g.getMaxX(), xMax) self.assertEqual(g.getOrder(), order) # self.assertEqual(h.getMinX(), xMin) # self.assertEqual(h.getMaxX(), xMax) minXNorm = None maxXNorm = None for x in numpy.arange(xMin, xMax + xDelta / 2.0, xDelta): xNorm = 2.0 * (x - xMean) / float(xMax - xMin) if minXNorm == None or xNorm < minXNorm: minXNorm = xNorm if maxXNorm == None or xNorm > maxXNorm: maxXNorm = xNorm predVal = referenceChebyshev1Polynomial1(xNorm, params) if not numpy.allclose(predVal, f(x)): self.fail( "%s = %s != %s for x=%s, xMin=%s, xMax=%s, xNorm=%s, params=%s; order constructor" % \ (type(f).__name__, f(x), predVal, x, xMin, xMax, xNorm, params)) if not numpy.allclose(predVal, g(x)): self.fail( "%s = %s != %s for x=%s, xMin=%s, xMax=%s, xNorm=%s, params=%s; params constructor" %\ (type(f).__name__, g(x), predVal, x, xMin, xMax, xNorm, params)) if not numpy.allclose(predVal, h(x)): self.fail( "%s = %s != %s for x=%s, xMin=%s, xMax=%s, xNorm=%s, params=%s; clone" %\ (type(f).__name__, h(x), predVal, x, xMin, xMax, xNorm, params)) if not numpy.allclose((minXNorm, maxXNorm), (-1.0, 1.0)): raise RuntimeError( "Invalid x normalization: xMin=%s, xMax=%s, min/max xNorm=(%s, %s) != (-1, 1)" % (xMin, xMax, minXNorm, maxXNorm))