Hide keyboard shortcuts

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

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 TestPermMap(MappingTestCase): 

11 

12 def test_PermMapMatched(self): 

13 """Test a PermMap whose inverse is the inverse of its forward 

14 """ 

15 permmap = ast.PermMap([2, 3, 1], [3, 1, 2]) 

16 self.assertEqual(permmap.className, "PermMap") 

17 self.assertEqual(permmap.nIn, 3) 

18 self.assertEqual(permmap.nOut, 3) 

19 

20 self.checkBasicSimplify(permmap) 

21 self.checkCopy(permmap) 

22 

23 indata = np.array([ 

24 [1.1, -43.5], 

25 [2.2, 1309.31], 

26 [3.3, 0.005], 

27 ]) 

28 outdata = permmap.applyForward(indata) 

29 pred_outdata = np.array([ 

30 [3.3, 0.005], 

31 [1.1, -43.5], 

32 [2.2, 1309.31], 

33 ]) 

34 assert_allclose(outdata, pred_outdata) 

35 

36 self.checkRoundTrip(permmap, indata) 

37 self.checkMappingPersistence(permmap, indata) 

38 

39 def test_PermMapUnmatched(self): 

40 """Test PermMap with different number of inputs and outputs 

41 """ 

42 permmap = ast.PermMap([2, 1, 3], [3, 1]) 

43 self.assertEqual(permmap.className, "PermMap") 

44 self.assertEqual(permmap.nIn, 3) 

45 self.assertEqual(permmap.nOut, 2) 

46 

47 indata = np.array([1.1, 2.2, -3.3]) 

48 outdata = permmap.applyForward(indata) 

49 assert_allclose(outdata, [-3.3, 1.1]) 

50 

51 indata2 = np.array([1.1, 2.2]) 

52 outdata2 = permmap.applyInverse(indata2) 

53 assert_allclose(outdata2, [2.2, 1.1, np.nan], equal_nan=True) 

54 

55 self.checkMappingPersistence(permmap, indata) 

56 

57 def test_PermMapWithConstants(self): 

58 """Test a PermMap with constant values 

59 """ 

60 permmap = ast.PermMap([-2, 1, 3], [2, 1, -1], [75.3, -126.5]) 

61 self.assertEqual(permmap.className, "PermMap") 

62 self.assertEqual(permmap.nIn, 3) 

63 self.assertEqual(permmap.nOut, 3) 

64 

65 indata = np.array([1.1, 2.2, 3.3]) 

66 outdata = permmap.applyForward(indata) 

67 assert_allclose(outdata, [2.2, 1.1, 75.3]) 

68 

69 outdata2 = permmap.applyInverse(indata) 

70 assert_allclose(outdata2, [-126.5, 1.1, 3.3]) 

71 

72 self.checkMappingPersistence(permmap, indata) 

73 

74 

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

76 unittest.main()