Coverage for tests / test_splineMap.py: 20%

54 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-14 23:35 +0000

1import unittest 

2 

3import numpy as np 

4import numpy.testing as npt 

5from scipy.interpolate import RectBivariateSpline 

6 

7import astshim as ast 

8from astshim.test import MappingTestCase 

9 

10 

11class TestSplineMap(MappingTestCase): 

12 

13 def setUp(self): 

14 self.nx = 150 

15 self.ny = 100 

16 self.k = 4 

17 a = 4.0 

18 b = 0.2 

19 c = 3.0 

20 

21 self.xs = np.linspace(1, self.nx, self.nx) 

22 self.ys = np.linspace(1, self.ny, self.ny) 

23 

24 fcx = np.zeros((self.nx, self.ny)) 

25 fcy = np.zeros((self.nx, self.ny)) 

26 for j in range(self.ny): 

27 y = self.ys[j] 

28 for i in range(self.nx): 

29 x = self.xs[i] 

30 fcx[i][j] = x + a * np.sin(b * x) * np.cos(b * y) 

31 fcy[i][j] = y + c * np.cos(b * x) * np.sin(b * y) 

32 

33 self.splinex = RectBivariateSpline(self.xs, self.ys, fcx, s=0) 

34 (self.tx, self.ty) = self.splinex.get_knots() 

35 self.arx = self.splinex.get_coeffs() 

36 

37 self.spliney = RectBivariateSpline(self.xs, self.ys, fcy, s=0) 

38 self.ary = self.spliney.get_coeffs() 

39 

40 self.splineMap = ast.SplineMap( 

41 self.k, self.k, self.nx, self.ny, self.tx, self.ty, self.arx, self.ary 

42 ) 

43 

44 def test_SplineMap(self): 

45 """Test that the forward and inverse transforms match 

46 scipy.interpolate.RectBivariateSpline. 

47 """ 

48 xval = np.array([0.0, 12.5, 12.0, 1.0, 15.0, 1.0, 95.0, 149.5, 151.2, 77.77]) 

49 yval = np.array([-1.0, 8.8, 8.0, 1.0, 15.0, 76.0, 100.0, 99.8, 82.3, 54.3]) 

50 

51 u = self.splinex.ev(xval, yval) 

52 v = self.spliney.ev(xval, yval) 

53 

54 outData = self.splineMap.applyForward(np.array([xval, yval])) 

55 

56 outOfBounds = ( 

57 (xval > self.xs.max()) 

58 | (xval < self.xs.min()) 

59 | (yval < self.ys.min()) 

60 | (yval > self.ys.max()) 

61 ) 

62 

63 npt.assert_equal(outData[:, outOfBounds], np.nan) 

64 npt.assert_almost_equal(outData[0, ~outOfBounds], u[~outOfBounds]) 

65 npt.assert_almost_equal(outData[1, ~outOfBounds], v[~outOfBounds]) 

66 

67 reverseTrip = self.splineMap.applyInverse(outData[:, ~outOfBounds].copy()) 

68 

69 npt.assert_almost_equal(reverseTrip[0], xval[~outOfBounds]) 

70 npt.assert_almost_equal(reverseTrip[1], yval[~outOfBounds]) 

71 

72 def test_splineMapAttributes(self): 

73 """Check that SplineMap attributes can be accessed and return expected 

74 values.""" 

75 self.assertEqual(self.splineMap.invTol, 1e-6) 

76 self.assertEqual(self.splineMap.invNIter, 6) 

77 self.assertEqual(self.splineMap.outUnit, False) 

78 

79 # Initialize SplineMap with other attributes and check they are set 

80 # correctly. 

81 options = "InvNIter=5,InvTol=1e-7,OutUnit=1" 

82 splineMap = ast.SplineMap( 

83 self.k, 

84 self.k, 

85 self.nx, 

86 self.ny, 

87 self.tx, 

88 self.ty, 

89 self.arx, 

90 self.ary, 

91 options=options, 

92 ) 

93 self.assertEqual(splineMap.invTol, 1e-7) 

94 self.assertEqual(splineMap.invNIter, 5) 

95 self.assertEqual(splineMap.outUnit, 1) 

96 

97 

98if __name__ == "__main__": 98 ↛ 99line 98 didn't jump to line 99 because the condition on line 98 was never true

99 unittest.main()