def test_precision(self): weight = np.array([[2, 8], [3, 5]]) delay = 1.5 delays = np.array([[delay, delay], [delay, delay]]) init_value = [[0.9, 0.0], [0.9, 0.0]] resolution_simulation = 0.1 synchronization_time = resolution_simulation * 10.0 nb_init = (int(delay / resolution_simulation)) + 1 initial_condition = np.array(init_value * nb_init).reshape( (nb_init, 2, weight.shape[0], 1)) proxy_id = [0] no_proxy = [1] # simulation with one proxy np.random.seed(42) sim = TvbSim(weight, delays, proxy_id, resolution_simulation, synchronization_time, initial_condition=initial_condition) time, s, result = sim(synchronization_time, rate=True) # full simulation np.random.seed(42) sim_ref = TvbSim(weight, delays, [], resolution_simulation, synchronization_time, initial_condition=initial_condition) time_ref, s_ref, result_ref = sim_ref(synchronization_time, rate=True) # compare with the Cosim Monitor RawCosim np.testing.assert_array_equal(result_ref[:, no_proxy, :], result[0][:, no_proxy, :]) np.testing.assert_array_equal(s_ref[:, no_proxy, :], s[0][:, no_proxy, :]) for i in range(0, 1000): time, s, result = sim( synchronization_time, rate_data=[time_ref, result_ref[:, proxy_id][:, :, 0]], rate=True) # compare with monitor delayed by synchronization_time np.testing.assert_array_equal(result_ref[:, no_proxy, :], result[1][:, no_proxy, :]) np.testing.assert_array_equal(result_ref[:, proxy_id, :] * np.NAN, result[1][:, proxy_id, :]) np.testing.assert_array_equal(s_ref, s[1]) time_ref, s_ref, result_ref = sim_ref(synchronization_time, rate=True) # compare with the Cosim Monitor RawCosim np.testing.assert_array_equal(result_ref[:, no_proxy, :], result[0][:, no_proxy, :]) np.testing.assert_array_equal(s_ref[:, no_proxy, :], s[0][:, no_proxy, :])
def test_precision_delay(self): weight = np.array([[2, 8, 10], [0.2, 0.5, 0.1], [3, 0.6, 1]]) delay = np.array([[0.6, 0.5, 1.0], [0.7, 0.8, 3.0], [1.0, 0.5, 0.7]]) max = np.int(np.max(delay) * 10 + 1) init_value = np.array([[[0.1, 0.0], [0.1, 0.0], [0.2, 0.0]]] * max) initial_condition = init_value.reshape((max, 2, weight.shape[0], 1)) resolution_simulation = 0.1 synchronization_time = 0.1 * 4 proxy_id = [0] no_proxy = [1, 2] # simulation with one proxy np.random.seed(42) sim = TvbSim(weight, delay, proxy_id, resolution_simulation, synchronization_time, initial_condition=initial_condition) time, s, result = sim(synchronization_time, rate=True) # full simulation np.random.seed(42) sim_ref = TvbSim(weight, delay, [], resolution_simulation, synchronization_time, initial_condition=initial_condition) time, s_ref, result_ref = sim_ref(synchronization_time, rate=True) # compare with the CosimMonitor RawCosim np.testing.assert_array_equal( np.squeeze(result_ref[:, no_proxy, :], axis=2), np.squeeze(result[0][:, no_proxy, :], axis=2)) np.testing.assert_array_equal( np.squeeze(s_ref[:, no_proxy, :], axis=2), np.squeeze(s[0][:, no_proxy, :], axis=2)) for i in range(0, 1000): time, s, result = sim( synchronization_time, rate_data=[time, result_ref[:, proxy_id][:, :, 0]], rate=True) # compare with RawDelayed monitor, delayed by synchronization_time np.testing.assert_array_equal(result_ref[:, no_proxy, :], result[1][:, no_proxy, :]) np.testing.assert_array_equal(result_ref[:, proxy_id, :] * np.NAN, result[1][:, proxy_id, :]) np.testing.assert_array_equal(s_ref, s[1]) time, s_ref, result_ref = sim_ref(synchronization_time, rate=True) # compare with the CosimMonitor RawCosim np.testing.assert_array_equal(result_ref[:, no_proxy, :], result[0][:, no_proxy, :]) np.testing.assert_array_equal(s_ref[:, no_proxy, :], s[0][:, no_proxy, :])
def test_double_precision_complex(self): weight = np.array([[5, 2, 4, 0], [8, 5, 4, 1], [6, 1, 7, 9], [10, 0, 5, 6]]) delay = np.array([[7, 8, 5, 1], [10, 3, 7, 9], [4, 3, 2, 8], [9, 10, 11, 5]]) max = np.int(np.max(delay)*10+1) init_value = np.array([[[0.1,0.0], [0.1,0.0], [0.2,0.0], [0.9,0.0]]] * max) initial_condition = init_value.reshape((max, 2, weight.shape[0], 1)) resolution_simulation = 0.1 synchronization_time = 0.1 * 10 proxy_id_1 = [1] proxy_id_2 = [0, 2] # simulation with one proxy np.random.seed(42) sim_1 = TvbSim(weight, delay, proxy_id_1, resolution_simulation, synchronization_time, initial_condition=initial_condition) time, result_1 = sim_1(synchronization_time) # simulation_2 with one proxy np.random.seed(42) sim_2 = TvbSim(weight, delay, proxy_id_2, resolution_simulation, synchronization_time, initial_condition=initial_condition) time, result_2 = sim_2(synchronization_time) # full simulation np.random.seed(42) sim_ref = TvbSim(weight, delay, [], resolution_simulation, synchronization_time, initial_condition=initial_condition) time_ref, result_ref = sim_ref(synchronization_time) # COMPARE PROXY 1 np.testing.assert_array_equal(np.squeeze(result_ref[:, proxy_id_2, :], axis=2)[0], np.squeeze(result_1[0][:, proxy_id_2, :], axis=2)[0]) # COMPARE PROXY 2 np.testing.assert_array_equal(np.squeeze(result_ref[:, proxy_id_1, :], axis=2)[0], np.squeeze(result_2[0][:, proxy_id_1, :], axis=2)[0]) for i in range(0, 1000): time, result_2 = sim_2(synchronization_time, [time, result_1[0][:, proxy_id_2][:, :, 0]]) # compare with raw monitor delayed of synchronization_time np.testing.assert_array_equal(result_ref, result_2[1]) time, result_1 = sim_1(synchronization_time, [time_ref, result_ref[:, proxy_id_1][:, :, 0]]) # compare with raw monitor delayed of synchronization_time np.testing.assert_array_equal(result_ref, result_1[1]) time_ref, result_ref = sim_ref(synchronization_time) # COMPARE PROXY 1 np.testing.assert_array_equal(np.squeeze(result_ref[:, proxy_id_2, :], axis=2)[0], np.squeeze(result_1[0][:, proxy_id_2, :], axis=2)[0]) # COMPARE PROXY 2 np.testing.assert_array_equal(np.squeeze(result_ref[:, proxy_id_1, :], axis=2)[0], np.squeeze(result_2[0][:, proxy_id_1, :], axis=2)[0])
def test_double_precision_complex_delay_update(self): weight = np.array([[5, 2, 4, 0], [8, 5, 4, 1], [6, 1, 7, 9], [10, 0, 5, 6]],dtype=np.float) delay = np.array([[7, 8, 5, 1], [9, 3, 7, 9], [4, 3, 2, 8], [9, 10, 11, 5]],dtype=np.float) max = np.int(np.max(delay)*10+1) resolution_simulation = 0.1 time_synchronize = np.min(delay) proxy_id_1 = [1] proxy_id_2 = [0, 2] # simulation with one proxy np.random.seed(42) sim_1 = TvbSim(weight, delay, proxy_id_1, resolution_simulation, time_synchronize) time, result_1 = sim_1(time_synchronize) # simulation_2 with one proxy np.random.seed(42) sim_2 = TvbSim(weight, delay, proxy_id_2, resolution_simulation, time_synchronize) time, result_2 = sim_2(time_synchronize) # full simulation np.random.seed(42) sim_ref = TvbSim(weight, delay, [], resolution_simulation, time_synchronize) time_ref, result_ref = sim_ref(time_synchronize) # COMPARE PROXY 1 np.testing.assert_array_equal(np.squeeze(result_ref[:, proxy_id_2, :], axis=2)[0], np.squeeze(result_1[0][:, proxy_id_2, :], axis=2)[0]) # COMPARE PROXY 2 np.testing.assert_array_equal(np.squeeze(result_ref[:, proxy_id_1, :], axis=2)[0], np.squeeze(result_2[0][:, proxy_id_1, :], axis=2)[0]) for i in range(0, 1000): time, result_2 = sim_2(time_synchronize, [time, result_1[0][:, proxy_id_2][:, :, 0]]) # compare with raw monitor delayed of time_synchronize np.testing.assert_array_equal(result_ref, result_2[1]) time, result_1 = sim_1(time_synchronize, [time_ref, result_ref[:, proxy_id_1][:, :, 0]]) # compare with raw monitor delayed of time_synchronize np.testing.assert_array_equal(result_ref, result_1[1]) time_ref, result_ref = sim_ref(time_synchronize) # COMPARE PROXY 1 np.testing.assert_array_equal(np.squeeze(result_ref[:, proxy_id_2, :], axis=2)[0], np.squeeze(result_1[0][:, proxy_id_2, :], axis=2)[0]) # COMPARE PROXY 2 np.testing.assert_array_equal(np.squeeze(result_ref[:, proxy_id_1,:], axis=2)[0], np.squeeze(result_2[0][:, proxy_id_1,:], axis=2)[0])
def test_precision_multiple(self): weight = np.array([[5, 2, 4, 0], [8, 5, 4, 1], [6, 1, 7, 9], [10, 0, 5, 6]]) delay = np.array([[0.1, 0.1, 0.1, 0.1], [0.1, 0.1, 0.1, 0.1], [0.1, 0.1, 0.1, 0.1], [0.1, 0.1, 0.1, 0.1]]) * 10 max = np.int(np.max(delay) * 10 + 1) init_value = np.array( [[[0.1, 0.0], [0.1, 0.0], [0.2, 0.0], [0.2, 0.0]]] * max) initial_condition = init_value.reshape((max, 2, weight.shape[0], 1)) resolution_simulation = 0.1 synchronization_time = 0.1 * 5 proxy_id = [0, 1, 2] no_proxy = [3] # simulation with one or more proxy np.random.seed(42) sim = TvbSim(weight, delay, proxy_id, resolution_simulation, synchronization_time, initial_condition=initial_condition) time, result = sim(synchronization_time) # full simulation np.random.seed(42) sim_ref = TvbSim(weight, delay, [], resolution_simulation, synchronization_time, initial_condition=initial_condition) time, result_ref = sim_ref(synchronization_time) # compare with the CosimMonitor RawCosim np.testing.assert_array_equal( np.squeeze(result_ref[:, no_proxy, :], axis=1)[0], np.squeeze(result[0][:, no_proxy, :], axis=1)[0]) for i in range(0, 1000): time, result = sim(synchronization_time, [time, result_ref[:, proxy_id][:, :, 0]]) # compare with Raw monitor delayed by synchronization_time np.testing.assert_array_equal(result_ref, result[1]) time, result_ref = sim_ref(synchronization_time) # compare with the CosimMonitor RawCosim np.testing.assert_array_equal(result_ref[:, no_proxy, :], result[0][:, no_proxy, :])
def test_update_model(self): weight = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) delay = np.array([[1.5, 1.5, 1.5, 1.5], [1.5, 1.5, 1.5, 1.5], [1.5, 1.5, 1.5, 1.5], [1.5, 1.5, 1.5, 1.5]]) resolution_simulation = 0.1 resolution_monitor = 1.0 synchronization_time = 1.0 proxy_id = [0, 1] firing_rate = np.array([[ 20.0, 10.0 ]]) * 10**-3 # time units in tvb is ms so the rate is in KHz sim = TvbSim(weight, delay, proxy_id, resolution_simulation, synchronization_time) time, result = sim(resolution_monitor, [np.array([resolution_simulation]), firing_rate]) for i in range(0, 100): time, result = sim(synchronization_time, [ time + resolution_monitor, np.repeat(firing_rate.reshape(1, 2), int(resolution_monitor / resolution_simulation), axis=0) ]) assert True
def test_precision_delay_update(self): weight = np.array([[2, 8, 0], [0, 0, 0], [3, 0, 1]]) delay = np.array([[0.6, 0.5, 1.0], [0.7, 0.8, 3.0], [1.0, 0.5, 0.7]]) max = np.int(np.max(delay) * 10 + 1) init_value = np.array([[[0.1, 0.0], [0.1, 0.0], [0.2, 0.0]]] * max) initial_condition = init_value.reshape((max, 2, weight.shape[0], 1)) resolution_simulation = 0.1 synchronization_time = np.min(delay) proxy_id = [0] no_proxy = [1, 2] # simulation with one proxy np.random.seed(42) sim = TvbSim(weight, delay, proxy_id, resolution_simulation, synchronization_time, initial_condition=initial_condition) time, result = sim(synchronization_time) # full simulation np.random.seed(42) sim_ref = TvbSim(weight, delay, [], resolution_simulation, synchronization_time, initial_condition=initial_condition) time, result_ref = sim_ref(synchronization_time) # compare with TVB Raw monitor delayed by synchronization_time np.testing.assert_array_equal( np.squeeze(result_ref[:, no_proxy, :], axis=2)[0], np.squeeze(result[0][:, no_proxy, :], axis=2)[0]) for i in range(0, 1000): delay_input = [time, result_ref[:, proxy_id][:, :, 0]] time, result = sim(synchronization_time, delay_input) # compare with Raw monitor delayed by synchronization_time np.testing.assert_array_equal(result_ref, result[1]) time, result_ref = sim_ref(synchronization_time) # compare with TVB Raw monitor delayed by synchronization_time np.testing.assert_array_equal(result_ref[:, no_proxy, :], result[0][:, no_proxy, :])
def test_update_model(self): weight = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) delay = np.array([[1.5, 1.5, 1.5, 1.5], [1.5, 1.5, 1.5, 1.5], [1.5, 1.5, 1.5, 1.5], [1.5, 1.5, 1.5, 1.5]]) resolution_simulation = 0.1 synchronization_time = 1.0 proxy_id = [0, 1] firing_rate = np.array([[ 20.0, 10.0 ]]) * 10**-3 # units time in tvb is ms so the rate is in KHz # Test the the update function sim = TvbSim(weight, delay, proxy_id, resolution_simulation, synchronization_time) time, result = sim(resolution_simulation, [np.array([resolution_simulation]), firing_rate]) for i in range(0, 100): time, result = sim(synchronization_time, [ np.arange(i * synchronization_time, (i + 1) * synchronization_time, resolution_simulation), np.repeat(firing_rate.reshape(1, 2), int(synchronization_time / resolution_simulation), axis=0) ]) assert True # Test a fail function due to the time of simulation too long with pytest.raises(ValueError): sim(synchronization_time, [ np.arange(100 * synchronization_time, 102 * synchronization_time, resolution_simulation), np.repeat( firing_rate.reshape(1, 2), int(synchronization_time / resolution_simulation) * 2, axis=0) ]) # Test a fail function due to the resoulation time is not good with pytest.raises(ValueError): sim(synchronization_time, [ np.arange(100 * synchronization_time, 101 * synchronization_time, resolution_simulation * 2), np.repeat( firing_rate.reshape(1, 2), int(synchronization_time / resolution_simulation) * 2, axis=0) ])
def test_double_proxy_precision_simple(self): weight = np.array([[1, 1], [1, 1]]) delay = np.array([[10.0, 10.0], [10.0, 10.0]]) max = np.int(np.max(delay) * 10 + 1) init_value = np.array([[[0.1, 0.0], [0.1, 0.0]]] * max) initial_condition = init_value.reshape((max, 2, weight.shape[0], 1)) resolution_simulation = 0.1 synchronization_time = 0.1 * 4 proxy_id_1 = [0] proxy_id_2 = [1] # simulation_2 with one proxy np.random.seed(42) sim_2 = TvbSim(weight, delay, proxy_id_2, resolution_simulation, synchronization_time, initial_condition=initial_condition) time, s_2, result_2 = sim_2(synchronization_time, rate=True) # simulation with one proxy np.random.seed(42) sim_1 = TvbSim(weight, delay, proxy_id_1, resolution_simulation, synchronization_time, initial_condition=initial_condition) time, s_1, result_1 = sim_1(synchronization_time, rate=True) # full simulation np.random.seed(42) sim_ref = TvbSim(weight, delay, [], resolution_simulation, synchronization_time, initial_condition=initial_condition) time_ref, s_ref, result_ref = sim_ref(synchronization_time, rate=True) # COMPARE PROXY 1 np.testing.assert_array_equal( np.squeeze(result_ref[:, proxy_id_2, :], axis=2), np.squeeze(result_1[0][:, proxy_id_2, :], axis=2)) np.testing.assert_array_equal( np.squeeze(s_ref[:, proxy_id_2, :], axis=2), np.squeeze(s_1[0][:, proxy_id_2, :], axis=2)) # COMPARE PROXY 2 np.testing.assert_array_equal( np.squeeze(result_ref[:, proxy_id_1, :], axis=2), np.squeeze(result_2[0][:, proxy_id_1, :], axis=2)) np.testing.assert_array_equal( np.squeeze(s_ref[:, proxy_id_1, :], axis=2), np.squeeze(s_2[0][:, proxy_id_1, :], axis=2)) for i in range(0, 1000): time, s_2, result_2 = sim_2( synchronization_time, rate_data=[time, result_1[0][:, proxy_id_2][:, :, 0]], rate=True) # compare with Raw monitor delayed by synchronization_time np.testing.assert_array_equal(result_ref[:, proxy_id_1, :], result_2[1][:, proxy_id_1, :]) np.testing.assert_array_equal( result_ref[:, proxy_id_2, :] * np.NAN, result_2[1][:, proxy_id_2, :]) np.testing.assert_array_equal(s_ref, s_2[1]) time, s_1, result_1 = sim_1( synchronization_time, rate_data=[time_ref, result_ref[:, proxy_id_1][:, :, 0]], rate=True) # compare with Raw monitor delayed by synchronization_time np.testing.assert_array_equal(result_ref[:, proxy_id_2, :], result_1[1][:, proxy_id_2, :]) np.testing.assert_array_equal( result_ref[:, proxy_id_1, :] * np.NAN, result_1[1][:, proxy_id_1, :]) np.testing.assert_array_equal(s_ref, s_1[1]) time_ref, s_ref, result_ref = sim_ref(synchronization_time, rate=True) # COMPARE PROXY 1 np.testing.assert_array_equal( np.squeeze(result_ref[:, proxy_id_2, :], axis=2), np.squeeze(result_1[0][:, proxy_id_2, :], axis=2)) np.testing.assert_array_equal( np.squeeze(s_ref[:, proxy_id_2, :], axis=2), np.squeeze(s_1[0][:, proxy_id_2, :], axis=2)) # COMPARE PROXY 2 np.testing.assert_array_equal( np.squeeze(result_ref[:, proxy_id_1, :], axis=2), np.squeeze(result_2[0][:, proxy_id_1, :], axis=2)) np.testing.assert_array_equal( np.squeeze(s_ref[:, proxy_id_1, :], axis=2), np.squeeze(s_2[0][:, proxy_id_1, :], axis=2))
def test_precision_bad(self): weight = np.array([[2, 8], [3, 5]]) delay = 100.0 delays = np.array([[delay, delay], [delay, delay]]) max = np.int(np.max(delay)*10+1) init_value = np.array([[0.9,0.0], [0.9,0.0]]*max) resolution_simulation = 0.1 synchronization_time = 0.1 * 10.0 nb_init = (int(delay / resolution_simulation)) + 1 initial_condition = np.array(init_value * nb_init).reshape((nb_init, 2, weight.shape[0], 1)) proxy_id = [0] no_proxy = [1] # simulation with one proxy np.random.seed(42) sim = TvbSim(weight, delays, proxy_id, resolution_simulation, synchronization_time, initial_condition=initial_condition) time, result = sim(synchronization_time) # full simulation np.random.seed(42) sim_ref = TvbSim(weight, delays, [], resolution_simulation, synchronization_time, initial_condition=initial_condition) time, result_ref = sim_ref(synchronization_time) # the results are different because the data of the proxy is wrong np.testing.assert_array_equal(result_ref[:, no_proxy, :], result[0][:, no_proxy, :]) # the first part of the result are correct because the wrong result are delayed for i in range(0, 99): time, result = sim(synchronization_time, [time, np.zeros_like(result_ref[:, proxy_id][:, :, 0])]) # compare with Raw monitor delayed by synchronization_time np.testing.assert_array_equal(result_ref[:, no_proxy, :], result[1][:, no_proxy, :]) np.testing.assert_array_compare(operator.__ne__, result_ref[:, proxy_id, :], result[1][:, proxy_id, :]) time, result_ref = sim_ref(synchronization_time) # compare with the CosimMonitor RawCosim np.testing.assert_array_equal(result_ref[:, no_proxy, :], result[0][:, no_proxy, :]) # the result become of different value when the delayed result is computed for i in range(100, 1000): time, result = sim(synchronization_time, [time, np.zeros_like(result_ref[:, proxy_id][:, :, 0])]) # compare with Raw monitor delayed by synchronization_time if i == 100: # as precedent np.testing.assert_array_equal(result_ref[:, no_proxy, :], result[1][:, no_proxy, :]) np.testing.assert_array_compare(operator.__ne__, result_ref[:, proxy_id, :], result[1][:, proxy_id, :]) elif i == 101: # the first one is the same np.testing.assert_array_equal(result_ref[:1, no_proxy, :], result[1][:1, no_proxy, :]) np.testing.assert_array_compare(operator.__ne__, result_ref[1:, proxy_id, :], result[1][1:, proxy_id, :]) np.testing.assert_array_compare(operator.__ne__, result_ref[1:, :, :], result[1][1:, :, :]) else: np.testing.assert_array_compare(operator.__ne__,result_ref, result[1]) time, result_ref = sim_ref(synchronization_time) # compare with the CosimMonitor RawCosim if i == 100: # the first one is the same np.testing.assert_array_equal(result_ref[:1, no_proxy, :], result[0][:1, no_proxy, :]) np.testing.assert_array_compare(operator.__ne__,result_ref[1:, no_proxy, :], result[0][1:, no_proxy, :]) else: np.testing.assert_array_compare(operator.__ne__,result_ref[:, no_proxy, :], result[0][:, no_proxy, :])