示例#1
0
文件: test_core.py 项目: hyzcn/elfi
 def test_simulator_summary_input_dimensions(self):
     np.random.seed(438763)
     for i in range(20):
         # dimensions
         n_samples = np.random.randint(1,5)
         n_in_dims = np.random.randint(1,5)
         in_dims = [np.random.randint(1,5) for i in range(n_in_dims)]
         in_dims[0] = max(2, in_dims[0])
         in_dims = tuple(in_dims)
         n_out_dims = np.random.randint(1,5)
         out_dims = tuple([np.random.randint(1,5) for i in range(n_out_dims)])
         # data
         ret = np.zeros((n_samples, ) + in_dims)
         obs = ret[0]
         # summary
         def mock_summary(x):
             exp_in_dims = in_dims
             if len(exp_in_dims) == 0:
                 exp_in_dims = (1,)
             if x.shape == (n_samples, ) + exp_in_dims:
                 # simulation data
                 return np.zeros((n_samples,) + out_dims)
             elif x.shape == (1,) + exp_in_dims:
                 # observation data
                 return np.zeros((1,) + out_dims)
             assert False
         # model
         mock = MockSimulator(ret)
         si = elfi.Simulator("si", mock, None, observed=obs)
         su = elfi.Summary("su", mock_summary, si)
         res = su.generate(n_samples).compute()
         exp_out_dims = out_dims
         if len(exp_out_dims) == 0:
             exp_out_dims = (1,)
         assert res.shape == (n_samples,) + exp_out_dims
示例#2
0
文件: test_core.py 项目: hyzcn/elfi
 def test_summary_discrepancy_input_dimensions(self):
     np.random.seed(23876123)
     for i in range(20):
         # dimensions
         n_samples = np.random.randint(1,5)
         n_sum = np.random.randint(1,5)
         n_dims = [np.random.randint(1,5) for i in range(n_sum)]
         dims = [tuple([np.random.randint(1,5) for j in range(n_dims[i])]) for i in range(n_sum)]
         # data
         ret = np.zeros((n_samples, 1))
         obs = ret[0]
         # summary
         def mock_summary(i, x):
             return np.zeros((x.shape[0], ) + dims[i])
         # discrepancy
         def mock_discrepancy(x, y):
             assert len(x) == len(y) == n_sum
             for i in range(n_sum):
                 exp_dims = dims[i]
                 if len(exp_dims) == 0:
                     exp_dims = (1,)
                 assert y[i].shape == (1,) + exp_dims
                 assert x[i].shape == (n_samples,) + exp_dims
             return np.zeros((n_samples, 1))
         # model
         mock = MockSimulator(ret)
         si = elfi.Simulator("si", mock, None, observed=obs)
         su = [elfi.Summary("su{}".format(i), partial(mock_summary, i), si) for i in range(n_sum)]
         di = elfi.Discrepancy("di", mock_discrepancy, *su)
         res = di.generate(n_samples).compute()
         assert res.shape == (n_samples, 1)
示例#3
0
文件: test_core.py 项目: hyzcn/elfi
 def test_as_vectorized_simulator(self):
     ret1 = np.array([5])
     ret2 = np.array([6])
     mock_seq = partial(elfi.core.vectorize_simulator, MockSequentialSimulator([ret1, ret2]))
     mock_vec = MockSimulator([ret1, ret2])
     input_data = [np.atleast_2d([[1], [2]]), np.atleast_2d([[3], [4]])]
     output_seq = mock_seq(*input_data, n_sim=2)
     output_vec = mock_vec(*input_data, n_sim=2)
     assert np.array_equal(output_seq, output_vec)
示例#4
0
文件: test_core.py 项目: hyzcn/elfi
 def test_normal_use(self):
     ret1 = np.array([5])
     ret2 = np.array([6])
     mock = MockSimulator([ret1, ret2])
     prng = np.random.RandomState(1234)
     input_dict = {
             "n": 2,
             "data": [np.atleast_2d([[1], [2]]),
                      np.atleast_2d([[3], [4]])],
             "random_state": prng.get_state()
             }
     output_dict = simulator_operation(mock, True, input_dict)
     prng.rand()
     print(output_dict)
     assert mock.n_calls == 1
     assert output_dict["n"] == 2
     assert np.array_equal(output_dict["data"], np.vstack((ret1, ret2)))
     new_state = prng.get_state()
     assert output_dict["random_state"][0] == new_state[0]
     assert np.array_equal(output_dict["random_state"][1], new_state[1])
     assert output_dict["random_state"][2] == new_state[2]
     assert output_dict["random_state"][3] == new_state[3]
     assert output_dict["random_state"][4] == new_state[4]