def test_can_save_and_load(tmpdir) -> None: PROTO_FILE_NAME = str(tmpdir.join("test.proto")) JSON_FILE_NAME = str(tmpdir.join("test.json")) DOT_FILE_NAME = str(tmpdir.join("test.dot")) gamma = Gamma(1.0, 1.0) gamma.set_value(2.5) # %%SNIPPET_START%% PythonSaveSnippet net = BayesNet(gamma.iter_connected_graph()) metadata = {"Author": "Documentation Team"} protobuf_saver = ProtobufSaver(net) protobuf_saver.save(PROTO_FILE_NAME, True, metadata) json_saver = JsonSaver(net) json_saver.save(JSON_FILE_NAME, True, metadata) dot_saver = DotSaver(net) dot_saver.save(DOT_FILE_NAME, True, metadata) # %%SNIPPET_END%% PythonSaveSnippet # %%SNIPPET_START%% PythonLoadSnippet protobuf_loader = ProtobufLoader() new_net_from_proto = protobuf_loader.load(PROTO_FILE_NAME) json_loader = JsonLoader() new_net_from_json = json_loader.load(JSON_FILE_NAME)
def test_sample_throws_if_vertices_in_sample_from_are_missing_labels() -> None: sigma = Gamma(1., 1) vertex = Gaussian(0., sigma, label="gaussian") assert sigma.get_label() is None net = BayesNet([sigma, vertex]) with pytest.raises(ValueError, match=r"Vertices in sample_from must be labelled."): samples = sample(net=net, sample_from=net.iter_latent_vertices())
def test_probe_for_non_zero_probability_from_bayes_net() -> None: gamma = Gamma(1., 1.) poisson = Poisson(gamma) net = BayesNet([poisson, gamma]) assert not gamma.has_value() assert not poisson.has_value() net.probe_for_non_zero_probability(100, KeanuRandom()) assert gamma.has_value() assert poisson.has_value()
def net() -> BayesNet: with Model() as m: m.gamma = Gamma(1., 1.) m.exp = Exponential(1.) m.cauchy = Cauchy(m.gamma, m.exp) return m.to_bayes_net()
def tensor_net() -> BayesNet: with Model() as m: m.gamma = Gamma( np.array([1., 1., 1., 1.]).reshape((2, 2)), np.array([2., 2., 2., 2.]).reshape((2, 2))) m.exp = Exponential(np.array([1., 1., 1., 1.]).reshape((2, 2))) m.add = m.gamma + m.exp return m.to_bayes_net()
def test_can_get_vertices_from_bayes_net(get_method: str, latent: bool, observed: bool, continuous: bool, discrete: bool) -> None: gamma = Gamma(1., 1.) gamma.observe(0.5) poisson = Poisson(gamma) cauchy = Cauchy(gamma, 1.) assert gamma.is_observed() assert not poisson.is_observed() assert not cauchy.is_observed() net = BayesNet([gamma, poisson, cauchy]) vertex_ids = [vertex.get_id() for vertex in getattr(net, get_method)()] if observed and continuous: assert gamma.get_id() in vertex_ids if latent and discrete: assert poisson.get_id() in vertex_ids if latent and continuous: assert cauchy.get_id() in vertex_ids assert len(vertex_ids) == (observed and continuous) + ( latent and discrete) + (latent and continuous)
def test_to_bayes_net_excludes_non_vertices() -> None: with Model() as m: m.not_a_vertex = 1 m.vertex = Gamma(0.5, 0.1) net = m.to_bayes_net() assert isinstance(net, BayesNet) net_vertex_ids = [vertex.get_id() for vertex in net.iter_latent_or_observed_vertices()] assert len(net_vertex_ids) == 1 assert m.vertex.get_id() in net_vertex_ids
def test_can_save_and_load(tmpdir) -> None: PROTO_FILE = str(tmpdir.join("test.proto")) JSON_FILE = str(tmpdir.join("test.json")) DOT_FILE = str(tmpdir.join("test.dot")) gamma = Gamma(1.0, 1.0) gamma.set_value(2.5) net = BayesNet(gamma.get_connected_graph()) metadata = {"Team": "GraphOS"} protobuf_saver = ProtobufSaver(net) protobuf_saver.save(PROTO_FILE, True, metadata) json_saver = JsonSaver(net) json_saver.save(JSON_FILE, True, metadata) dot_saver = DotSaver(net) dot_saver.save(DOT_FILE, True, metadata) check_dot_file(DOT_FILE) protobuf_loader = ProtobufLoader() json_loader = JsonLoader() new_net_from_proto = protobuf_loader.load(PROTO_FILE) check_loaded_net(new_net_from_proto) new_net_from_json = json_loader.load(JSON_FILE) check_loaded_net(new_net_from_json)
def test_to_bayes_net() -> None: with Model() as m: m.mu = Exponential(1.) m.sigma = Gamma(0.5, 0.1) m.gaussian = Gaussian(m.mu, m.sigma) net = m.to_bayes_net() assert isinstance(net, BayesNet) net_vertex_ids = [ vertex.get_id() for vertex in net.get_latent_or_observed_vertices() ] assert len(net_vertex_ids) == 3 assert m.mu.get_id() in net_vertex_ids assert m.sigma.get_id() in net_vertex_ids assert m.gaussian.get_id() in net_vertex_ids
def net() -> BayesNet: with Model() as m: m.gamma = Gamma(1., 1.) m.gaussian = Gaussian(0., m.gamma) return m.to_bayes_net()
def net() -> BayesNet: gamma = Gamma(1., 1.) exp = Exponential(1.) cauchy = Cauchy(gamma, exp) return BayesNet(cauchy.get_connected_graph())