예제 #1
0
def test_distance_functions():
    vec_length = 5
    v1 = np.random.rand(1, vec_length)
    v2 = np.random.rand(1, vec_length)

    # calculate Euclidean distance using sciope and numpy
    euc_func = euclidean.EuclideanDistance()
    euc_dist = euc_func.compute(v1, v2)
    validation_val = np.linalg.norm(v1 - v2)
    assert euc_dist == validation_val, "EuclideanDistance functional test error, expected value mismatch"

    # now for Manhattan distance
    man_func = manhattan.ManhattanDistance()
    man_dist = man_func.compute(v1, v2)
    validation_val = cityblock(v1, v2)
    assert man_dist == validation_val, "ManhattanDistance functional test error, expected value mismatch"

    # ... and naive squared distance
    ns_func = naive_squared.NaiveSquaredDistance()
    v3 = np.asarray([0, 0, 0])
    v4 = np.asarray([1, 1, 1])
    validation_val = v4
    ns_dist = ns_func.compute(v3, v4)
    assert np.array_equal(ns_dist, validation_val), "NaiveSquaredDistance functional test error, " \
                                                    "expected value mismatch"
예제 #2
0
def test_param_sim_summ():
    lhd = LatinHypercube(dmin, dmax)
    n_points = 10
    lhd.generate_array(n_points)
    summ = lambda x: generate_tsfresh_features(x, MinimalFCParameters())
    graph_dict = core.get_graph_chunked(param_func=lhd.draw,
                                        sim_func=simulator2,
                                        summaries_func=summ,
                                        batch_size=n_points,
                                        chunk_size=2)
    assert len(
        graph_dict["parameters"]) == 5, "Core test failed, dimensions mismatch"
    assert len(graph_dict["trajectories"]
               ) == 5, "Core test failed, dimensions mismatch"
    assert len(
        graph_dict["summarystats"]) == 5, "Core test failed, expected None"

    params, sim, summaries = dask.compute(graph_dict["parameters"],
                                          graph_dict["trajectories"],
                                          graph_dict["summarystats"])

    sim = np.asarray(sim)
    params = np.asarray(params)
    summaries = np.asarray(summaries)

    assert params.shape == (5, 2, 5), "Core test failed, dimensions mismatch"
    assert sim.shape == (5, 2, 1, 2,
                         101), "Core test failed, dimensions mismatch"
    assert summaries.shape == (5, 2, 1,
                               16), "Core test failed, dimensions mismatch"

    fixed_data = np.asarray([simulator2(bound) for p in range(10)])
    print(fixed_data.shape)
    fixed_data = fixed_data.reshape(10, 2, 101)

    fixed_mean = core.get_fixed_mean(fixed_data, summ, chunk_size=2)

    m, = dask.compute(fixed_mean)
    m = np.asarray(m)
    assert m.shape == (1, 16), "Core test failed, dimensions mismatch"

    dist_class = ns.NaiveSquaredDistance()

    dist_func = lambda x: dist_class.compute(x, m)

    dist = core.get_distance(dist_func, graph_dict["summarystats"])

    assert len(dist) == 5, "Core test failed, dimesnion mismatch"

    dist_res, = dask.compute(dist)
    dist_res = np.asarray(dist_res)

    assert dist_res.shape == (5, 2, 1,
                              16), "Core test failed, dimension mismatch"
예제 #3
0
def abc_test_run():
    """
    Perform a test abc run
    :return: ABC mean absolute error
    """
    dmin, dmax = get_bounds()
    uni_prior = uniform_prior.UniformPrior(dmin, dmax)
    fixed_data = get_fixed_data()
    summ_func = auto_tsfresh.SummariesTSFRESH()
    ns = naive_squared.NaiveSquaredDistance()
    abc = ABC(fixed_data,
              sim=simulate,
              prior_function=uni_prior,
              summaries_function=summ_func.compute,
              distance_function=ns,
              use_logger=True)
    abc.compute_fixed_mean(chunk_size=2)
    res = abc.infer(num_samples=100, batch_size=10, chunk_size=2)
    true_params = get_true_param()
    mae_inference = mean_absolute_error(true_params,
                                        abc.results['inferred_parameters'])
    return mae_inference
                       number_of_trajectories=100,
                       seed=None)

# In[9]:

# Reshape the dat to (n_points,n_species,n_timepoints) and remove timepoints array
fixed_data = np.asarray([x.T for x in fixed_data])
fixed_data = fixed_data[:, 1:, :]

# In[10]:

# Function to generate summary statistics
summ_func = auto_tsfresh.SummariesTSFRESH()

# Distance
ns = naive_squared.NaiveSquaredDistance()

# Start abc instance
abc = ABC(fixed_data,
          sim=simulator2,
          prior_function=uni_prior,
          summaries_function=summ_func.compute,
          distance_function=ns)

# In[11]:

# c = Client()
# c

# In[12]:
# Load data
data = np.loadtxt("datasets/vilar_dataset_specieA_100trajs_150time.dat",
                  delimiter=",")

# Set up the prior
dmin = [30, 200, 0, 30, 30, 1, 1, 0, 0, 0, 0.5, 0.5, 1, 30, 80]
dmax = [70, 600, 1, 70, 70, 10, 12, 1, 2, 0.5, 1.5, 1.5, 3, 70, 120]
mm_prior = uniform_prior.UniformPrior(np.asarray(dmin), np.asarray(dmax))

# Set up ABC
abc_instance = abc_inference.ABC(data,
                                 vilar.simulate,
                                 epsilon=0.1,
                                 prior_function=mm_prior,
                                 distance_function=ns.NaiveSquaredDistance(),
                                 summaries_function=se.SummariesEnsemble())

# Perform ABC; require 30 samples
abc_instance.infer(30)

# Results
true_params = [[
    50.0, 100.0, 50.0, 500.0, 0.01, 50.0, 50.0, 5.0, 1.0, 10.0, 0.5, 0.2, 1.0,
    2.0, 1.0
]]
print('Inferred parameters: ', abc_instance.results['inferred_parameters'])
print(
    'Inference error in MAE: ',
    mean_absolute_error(true_params,
                        abc_instance.results['inferred_parameters']))