Coverage for tests/test_matrixMap.py: 24%

44 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-12 01:17 -0800

1import unittest 

2 

3import numpy as np 

4from numpy.testing import assert_allclose 

5 

6import astshim as ast 

7from astshim.test import MappingTestCase 

8 

9 

10class TestMatrixMap(MappingTestCase): 

11 

12 def test_MatrixMapDiagonal(self): 

13 """Test MatrixMap constructed with a diagonal vector""" 

14 

15 mm = ast.MatrixMap([-1.0, 2.0]) 

16 self.assertEqual(mm.className, "MatrixMap") 

17 self.assertEqual(mm.nIn, 2) 

18 self.assertEqual(mm.nOut, 2) 

19 self.assertTrue(mm.hasForward) 

20 self.assertTrue(mm.hasInverse) 

21 

22 self.checkBasicSimplify(mm) 

23 self.checkCopy(mm) 

24 

25 indata = np.array([ 

26 [1.0, 2.0, 3.0], 

27 [0.0, 1.0, 2.0], 

28 ], dtype=float) 

29 outdata = mm.applyForward(indata) 

30 pred_outdata = np.array([ 

31 [-1.0, -2.0, -3.0], 

32 [0.0, 2.0, 4.0], 

33 ], dtype=float) 

34 assert_allclose(outdata, pred_outdata) 

35 

36 self.checkRoundTrip(mm, indata) 

37 self.checkMappingPersistence(mm, indata) 

38 

39 def test_MatrixMapMatrix(self): 

40 """Test MatrixMap constructed with a 2-d matrix 

41 

42 matrix inputs expected outputs 

43 0, 1 (1, 0) (0, 2, -1) 

44 2, 3 (2, 1) (1, 7, -4) 

45 -1, -2 (3, 2) (2, 12, -7) 

46 """ 

47 matrix = np.array([ 

48 [0.0, 1.0], 

49 [2.0, 3.0], 

50 [-1.0, -2.0] 

51 ], dtype=float) 

52 mm = ast.MatrixMap(matrix) 

53 self.assertEqual(mm.nIn, 2) 

54 self.assertEqual(mm.nOut, 3) 

55 self.assertTrue(mm.hasForward) 

56 self.assertFalse(mm.hasInverse) 

57 

58 indata = np.array([ 

59 [1.0, 2.0, 3.0], 

60 [0.0, 1.0, 2.0], 

61 ], dtype=float) 

62 outdata = mm.applyForward(indata) 

63 pred_outdata = np.array([ 

64 [0.0, 1.0, 2.0], 

65 [2.0, 7.0, 12.0], 

66 [-1.0, -4.0, -7.0], 

67 ], dtype=float) 

68 assert_allclose(outdata, pred_outdata) 

69 

70 self.checkMappingPersistence(mm, indata) 

71 

72 def test_MatrixMapWithZeros(self): 

73 """Test that a MatrixMap all coefficients 0 can be simplified 

74 

75 This is ticket DM-10942 

76 """ 

77 mm = ast.MatrixMap([0.0, 0.0]) 

78 

79 indata = np.array([ 

80 [1.0, 2.0, 3.0], 

81 [0.0, 1.0, 2.0], 

82 ], dtype=float) 

83 outdata = mm.applyForward(indata) 

84 pred_outdata = np.array([ 

85 [0.0, 0.0, 0.0], 

86 [0.0, 0.0, 0.0], 

87 ], dtype=float) 

88 assert_allclose(outdata, pred_outdata) 

89 

90 simplifiedMM = mm.simplified() 

91 outdata2 = simplifiedMM.applyForward(indata) 

92 assert_allclose(outdata2, pred_outdata) 

93 

94 

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

96 unittest.main()