Changeset 13574
 Timestamp:
 Sep 23, 2019 11:13:28 AM (3 months ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/dev5819/Python/src/tests_jmodelica/test_core.py
r10318 r13574 2 2 # * coding: utf8 * 3 3 4 # Copyright (C) 201 0Modelon AB4 # Copyright (C) 2019 Modelon AB 5 5 # 6 6 # This program is free software: you can redistribute it and/or modify … … 22 22 import os, os.path 23 23 import sys 24 25 import nose 26 import nose.tools 24 import numpy as np 27 25 28 26 from tests_jmodelica import testattr, get_files_path … … 32 30 class Test_TrajectoryConstantInterpolationExtrapolation: 33 31 32 def _assert(self, result, expected, assert_index, n_asserts): 33 err_msg = "Failed in assert {} of {}, got {} instead of {}".format(result, expected, assert_index, n_asserts) 34 assert result == expected, err_msg 35 34 36 @testattr(stddist_full = True) 35 37 def test_one_dimensional(self): 36 t = N.array([1,2,3,4,5])37 x = N.array([[2,2.5,4,5,9]]).transpose()38 t = np.array([1,2,3,4,5]) 39 x = np.array([[2,2.5,4,5,9]]).transpose() 38 40 39 41 interp = TrajectoryConstantInterpolationExtrapolation(t,x) 40 42 41 assert interp.eval(0)[:,0] == 2.0 42 assert interp.eval(6)[:,0] == 9.0 43 assert interp.eval(1.5)[:,0] == 2.0 44 assert interp.eval(2.6)[:,0] == 2.5 43 n_tests = 16 44 self._assert(interp.eval(0)[:,0], 2.0, 1, n_tests) 45 self._assert(interp.eval(6)[:,0], 9.0, 2, n_tests) 46 self._assert(interp.eval(1.5)[:,0], 2.0, 3, n_tests) 47 self._assert(interp.eval(2.6)[:,0], 2.5, 4, n_tests) 45 48 46 assert interp.eval([0, 1.5, 2.6, 6])[0,0] == 2.047 assert interp.eval([0, 1.5, 2.6, 6])[1,0] == 2.048 assert interp.eval([0, 1.5, 2.6, 6])[2,0] == 2.549 assert interp.eval([0, 1.5, 2.6, 6])[3,0] == 9.049 self._assert(interp.eval([0, 1.5, 2.6, 6])[0,0], 2.0, 5, n_tests) 50 self._assert(interp.eval([0, 1.5, 2.6, 6])[1,0], 2.0, 6, n_tests) 51 self._assert(interp.eval([0, 1.5, 2.6, 6])[2,0], 2.5, 7, n_tests) 52 self._assert(interp.eval([0, 1.5, 2.6, 6])[3,0], 9.0, 8, n_tests) 50 53 51 54 #Change mode 52 55 interp.set_mode("Backward") 53 56 54 assert interp.eval(0)[:,0] == 2.055 assert interp.eval(6)[:,0] == 9.056 assert interp.eval(1.5)[:,0] == 2.557 assert interp.eval(2.6)[:,0] == 4.057 self._assert(interp.eval(0)[:,0], 2.0, 9, n_tests) 58 self._assert(interp.eval(6)[:,0], 9.0, 10, n_tests) 59 self._assert(interp.eval(1.5)[:,0], 2.5, 11, n_tests) 60 self._assert(interp.eval(2.6)[:,0], 4.0, 12, n_tests) 58 61 59 assert interp.eval([0, 1.5, 2.6, 6])[0,0] == 2.060 assert interp.eval([0, 1.5, 2.6, 6])[1,0] == 2.561 assert interp.eval([0, 1.5, 2.6, 6])[2,0] == 4.062 assert interp.eval([0, 1.5, 2.6, 6])[3,0] == 9.062 self._assert(interp.eval([0, 1.5, 2.6, 6])[0,0], 2.0, 13, n_tests) 63 self._assert(interp.eval([0, 1.5, 2.6, 6])[1,0], 2.5, 14, n_tests) 64 self._assert(interp.eval([0, 1.5, 2.6, 6])[2,0], 4.0, 15, n_tests) 65 self._assert(interp.eval([0, 1.5, 2.6, 6])[3,0], 9.0, 16, n_tests) 63 66 64 67 @testattr(stddist_full = True) 65 68 def test_two_dimensional(self): 66 t = N.array([1,2,3,4,5])67 x = N.array([[2,2.5,4,5,9], [1,1.5,3,4,8]]).transpose()69 t = np.array([1,2,3,4,5]) 70 x = np.array([[2,2.5,4,5,9], [1,1.5,3,4,8]]).transpose() 68 71 69 72 interp = TrajectoryConstantInterpolationExtrapolation(t,x) 70 73 71 assert interp.eval(0)[0,0] == 2.0 72 assert interp.eval(6)[0,0] == 9.0 73 assert interp.eval(1.5)[0,0] == 2.0 74 assert interp.eval(2.6)[0,0] == 2.5 75 assert interp.eval(0)[0,1] == 1.0 76 assert interp.eval(6)[0,1] == 8.0 77 assert interp.eval(1.5)[0,1] == 1.0 78 assert interp.eval(2.6)[0,1] == 1.5 74 n_tests = 31 75 self._assert(interp.eval(0)[0,0], 2.0, 1, n_tests) 76 self._assert(interp.eval(6)[0,0], 9.0, 2, n_tests) 77 self._assert(interp.eval(1.5)[0,0], 2.0, 3, n_tests) 78 self._assert(interp.eval(2.6)[0,0], 2.5, 3, n_tests) 79 self._assert(interp.eval(0)[0,1], 1.0, 4, n_tests) 80 self._assert(interp.eval(6)[0,1], 8.0, 5, n_tests) 81 self._assert(interp.eval(1.5)[0,1], 1.0, 6, n_tests) 82 self._assert(interp.eval(2.6)[0,1], 1.5, 7, n_tests) 79 83 80 assert interp.eval([0, 1.5, 2.6, 6])[0,0] == 2.081 assert interp.eval([0, 1.5, 2.6, 6])[1,0] == 2.082 assert interp.eval([0, 1.5, 2.6, 6])[2,0] == 2.583 assert interp.eval([0, 1.5, 2.6, 6])[3,0] == 9.084 assert interp.eval([0, 1.5, 2.6, 6])[0,1] == 1.085 assert interp.eval([0, 1.5, 2.6, 6])[1,1] == 1.086 assert interp.eval([0, 1.5, 2.6, 6])[2,1] == 1.587 assert interp.eval([0, 1.5, 2.6, 6])[3,1] == 8.084 self._assert(interp.eval([0, 1.5, 2.6, 6])[0,0], 2.0, 8, n_tests) 85 self._assert(interp.eval([0, 1.5, 2.6, 6])[1,0], 2.0, 9, n_tests) 86 self._assert(interp.eval([0, 1.5, 2.6, 6])[2,0], 2.5, 10, n_tests) 87 self._assert(interp.eval([0, 1.5, 2.6, 6])[3,0], 9.0, 11, n_tests) 88 self._assert(interp.eval([0, 1.5, 2.6, 6])[0,1], 1.0, 12, n_tests) 89 self._assert(interp.eval([0, 1.5, 2.6, 6])[1,1], 1.0, 13, n_tests) 90 self._assert(interp.eval([0, 1.5, 2.6, 6])[2,1], 1.5, 14, n_tests) 91 self._assert(interp.eval([0, 1.5, 2.6, 6])[3,1], 8.0, 15, n_tests) 88 92 89 93 #Change mode 90 94 interp.set_mode("Backward") 91 95 92 assert interp.eval(0)[0,0] == 2.093 assert interp.eval(6)[0,0] == 9.094 assert interp.eval(1.5)[0,0] == 2.595 assert interp.eval(2.6)[0,0] == 4.096 assert interp.eval(0)[0,1] == 1.097 assert interp.eval(6)[0,1] == 8.098 assert interp.eval(1.5)[0,1] == 1.599 assert interp.eval(2.6)[0,1] == 3.096 self._assert(interp.eval(0)[0,0], 2.0, 16, n_tests) 97 self._assert(interp.eval(6)[0,0], 9.0, 17, n_tests) 98 self._assert(interp.eval(1.5)[0,0], 2.5, 18, n_tests) 99 self._assert(interp.eval(2.6)[0,0], 4.0, 19, n_tests) 100 self._assert(interp.eval(0)[0,1], 1.0, 20, n_tests) 101 self._assert(interp.eval(6)[0,1], 8.0, 21, n_tests) 102 self._assert(interp.eval(1.5)[0,1], 1.5, 22, n_tests) 103 self._assert(interp.eval(2.6)[0,1], 3.0, 23, n_tests) 100 104 101 assert interp.eval([0, 1.5, 2.6, 6])[0,0] == 2.0102 assert interp.eval([0, 1.5, 2.6, 6])[1,0] == 2.5103 assert interp.eval([0, 1.5, 2.6, 6])[2,0] == 4.0104 assert interp.eval([0, 1.5, 2.6, 6])[3,0] == 9.0105 assert interp.eval([0, 1.5, 2.6, 6])[0,1] == 1.0106 assert interp.eval([0, 1.5, 2.6, 6])[1,1] == 1.5107 assert interp.eval([0, 1.5, 2.6, 6])[2,1] == 3.0108 assert interp.eval([0, 1.5, 2.6, 6])[3,1] == 8.0105 self._assert(interp.eval([0, 1.5, 2.6, 6])[0,0], 2.0, 24, n_tests) 106 self._assert(interp.eval([0, 1.5, 2.6, 6])[1,0], 2.5, 25, n_tests) 107 self._assert(interp.eval([0, 1.5, 2.6, 6])[2,0], 4.0, 26, n_tests) 108 self._assert(interp.eval([0, 1.5, 2.6, 6])[3,0], 9.0, 27, n_tests) 109 self._assert(interp.eval([0, 1.5, 2.6, 6])[0,1], 1.0, 28, n_tests) 110 self._assert(interp.eval([0, 1.5, 2.6, 6])[1,1], 1.5, 29, n_tests) 111 self._assert(interp.eval([0, 1.5, 2.6, 6])[2,1], 3.0, 30, n_tests) 112 self._assert(interp.eval([0, 1.5, 2.6, 6])[3,1], 8.0, 31, n_tests) 109 113 110 114 111 115 class Test_TrajectoryLinearInterpolationExtrapolation: 112 116 117 def _assert(self, result, expected, assert_index, n_asserts): 118 err_msg = "Failed in assert {} of {}, got {} instead of {}".format(result, expected, assert_index, n_asserts) 119 assert result == expected, err_msg 120 113 121 @testattr(stddist_full = True) 114 122 def test_one_dimensional(self): 115 t = N.array([1,2,3,4,5]) 116 x = N.array([[2,2.5,4,5,9]]).transpose() 123 124 t = np.array([1,2,3,4,5]) 125 x = np.array([[2,2.5,4,5,9]]).transpose() 126 127 interp = TrajectoryLinearInterpolationExtrapolation(t,x) 128 n_tests = 8 129 self._assert(interp.eval(0)[:,0], 1.5, 1, n_tests) 130 self._assert(interp.eval(6)[:,0], 13.0, 2, n_tests) 131 self._assert(interp.eval(1.5)[:,0], 2.25, 3, n_tests) 132 np.testing.assert_almost_equal(interp.eval(2.6)[:,0], 3.4) 133 134 self._assert(interp.eval([0, 1.5, 2.6, 6])[0,0], 1.5, 5, n_tests) 135 self._assert(interp.eval([0, 1.5, 2.6, 6])[1,0], 2.25, 6, n_tests) 136 np.testing.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[2,0], 3.4) 137 np.testing.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[3,0],13.0) 138 139 @testattr(stddist_full = True) 140 def test_two_dimensional(self): 141 t = np.array([1,2,3,4,5]) 142 x = np.array([[2,2.5,4,5,9], [1,1.5,3,4,8]]).transpose() 117 143 118 144 interp = TrajectoryLinearInterpolationExtrapolation(t,x) 119 145 120 assert interp.eval(0)[:,0] == 1.5 121 assert interp.eval(6)[:,0] == 13.0 122 assert interp.eval(1.5)[:,0] == 2.25 123 nose.tools.assert_almost_equal(interp.eval(2.6)[:,0],3.4) 146 n_tests = 16 147 self._assert(interp.eval(0)[:,0], 1.5, 1, n_tests) 148 self._assert(interp.eval(6)[:,0], 13.0, 1, n_tests) 149 self._assert(interp.eval(1.5)[:,0], 2.25, 1, n_tests) 150 np.testing.assert_almost_equal(interp.eval(2.6)[:,0],3.4) 151 self._assert(interp.eval(0)[:,1], 0.5, 1, n_tests) 152 self._assert(interp.eval(6)[:,1], 12.0, 1, n_tests) 153 self._assert(interp.eval(1.5)[:,1], 1.25, 1, n_tests) 154 np.testing.assert_almost_equal(interp.eval(2.6)[:,1],2.4) 124 155 125 assert interp.eval([0, 1.5, 2.6, 6])[0,0] == 1.5 126 assert interp.eval([0, 1.5, 2.6, 6])[1,0] == 2.25 127 nose.tools.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[2,0],3.4) 128 nose.tools.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[3,0],13.0) 129 130 @testattr(stddist_full = True) 131 def test_two_dimensional(self): 132 t = N.array([1,2,3,4,5]) 133 x = N.array([[2,2.5,4,5,9], [1,1.5,3,4,8]]).transpose() 134 135 interp = TrajectoryLinearInterpolationExtrapolation(t,x) 136 137 assert interp.eval(0)[:,0] == 1.5 138 assert interp.eval(6)[:,0] == 13.0 139 assert interp.eval(1.5)[:,0] == 2.25 140 nose.tools.assert_almost_equal(interp.eval(2.6)[:,0],3.4) 141 assert interp.eval(0)[:,1] == 0.5 142 assert interp.eval(6)[:,1] == 12.0 143 assert interp.eval(1.5)[:,1] == 1.25 144 nose.tools.assert_almost_equal(interp.eval(2.6)[:,1],2.4) 145 146 assert interp.eval([0, 1.5, 2.6, 6])[0,0] == 1.5 147 assert interp.eval([0, 1.5, 2.6, 6])[1,0] == 2.25 148 nose.tools.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[2,0],3.4) 149 nose.tools.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[3,0],13.0) 150 assert interp.eval([0, 1.5, 2.6, 6])[0,1] == 0.5 151 assert interp.eval([0, 1.5, 2.6, 6])[1,1] == 1.25 152 nose.tools.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[2,1],2.4) 153 nose.tools.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[3,1],12.0) 156 self._assert(interp.eval([0, 1.5, 2.6, 6])[0,0], 1.5, 1, n_tests) 157 self._assert(interp.eval([0, 1.5, 2.6, 6])[1,0], 2.25, 1, n_tests) 158 np.testing.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[2,0],3.4) 159 np.testing.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[3,0],13.0) 160 self._assert(interp.eval([0, 1.5, 2.6, 6])[0,1], 0.5, 1, n_tests) 161 self._assert(interp.eval([0, 1.5, 2.6, 6])[1,1], 1.25, 1, n_tests) 162 np.testing.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[2,1],2.4) 163 np.testing.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[3,1],12.0)
Note: See TracChangeset
for help on using the changeset viewer.