def test_sampling_returns_multi_indexed_dict_of_list_of_scalars_for_mixed_net( algo: Callable[[BayesNet], PosteriorSamplingAlgorithm]) -> None: exp = Exponential(1.) add_rank_2 = exp + np.array([1., 2., 3., 4.]).reshape((2, 2)) add_rank_3 = exp + np.array([1., 2., 3., 4., 1., 2., 3., 4.]).reshape( (2, 2, 2)) gaussian_rank_2 = Gaussian(add_rank_2, 2.) gaussian_rank_3 = Gaussian(add_rank_3, 1.) exp.set_label("exp") gaussian_rank_2.set_label("gaussian") gaussian_rank_3.set_label("gaussian2") mixed_net = BayesNet(exp.iter_connected_graph()) draws = 5 sample_from = list(mixed_net.iter_latent_vertices()) vertex_labels = [vertex.get_label() for vertex in sample_from] samples = sample(net=mixed_net, sample_from=sample_from, sampling_algorithm=algo(mixed_net), draws=draws) assert type(samples) == dict __assert_valid_samples(draws, samples) assert ('exp', (0, )) in samples for i in (0, 1): for j in (0, 1): assert (('gaussian', (i, j)) in samples) df = pd.DataFrame(samples) expected_num_columns = {"exp": 1, "gaussian": 4, "gaussian2": 8} expected_tuple_size = {"exp": 1, "gaussian": 2, "gaussian2": 3} assert len(df.columns.levels[0]) == 3 for parent_column in df.columns.levels[0]: assert parent_column in vertex_labels assert len( df[parent_column].columns) == expected_num_columns[parent_column] for child_column in df[parent_column].columns: assert type(child_column) == tuple assert len(child_column) == expected_tuple_size[parent_column] assert len(df[parent_column][child_column]) == 5 assert type(df[parent_column][child_column][0]) == np.float64
def test_cannot_set_none_label() -> None: vertex = Gaussian(0., 1., label="gaussian") with pytest.raises(ValueError, match=r"label cannot be None"): vertex.set_label(None)
def test_set_label() -> None: label = "gaussian_vertex" vertex = Gaussian(0., 1.) vertex.set_label(label) assert vertex.get_label() == label