Exemplo n.º 1
0
def test_can_pass_scalar_to_vertex(jvm_view: JVMView) -> None:
    gaussian = Vertex(jvm_view.GaussianVertex, 0., 1.)
    sample = gaussian.sample()

    assert type(sample) == numpy_types
    assert sample.shape == ()
    assert sample.dtype == float
Exemplo n.º 2
0
def test_get_vertex_id(jvm_view: JVMView) -> None:
    gaussian = Vertex(jvm_view.GaussianVertex, 0., 1.)

    java_id = gaussian.unwrap().getId().getValue()
    python_id = gaussian.get_id()

    assert all(value in python_id for value in java_id)
Exemplo n.º 3
0
def test_can_pass_vertex_to_vertex(jvm_view: JVMView) -> None:
    mu = Gaussian(0., 1.)
    gaussian = Vertex(jvm_view.GaussianVertex, "gaussian", mu, Const(1.))
    sample = gaussian.sample()

    assert type(sample) == numpy_types
    assert sample.shape == ()
    assert sample.dtype == float
Exemplo n.º 4
0
def test_java_collections_to_generator() -> None:
    gaussian = Gaussian(0., 1.)

    java_collections = gaussian.unwrap().getConnectedGraph()
    python_list = list(Vertex._to_generator(java_collections))

    java_vertex_ids = [
        Vertex._get_python_id(java_vertex) for java_vertex in java_collections
    ]

    assert java_collections.size() == len(python_list)
    assert all(
        type(element) == Double and element.get_id() in java_vertex_ids
        for element in python_list)
Exemplo n.º 5
0
def _samples_generator(sample_iterator: JavaObject,
                       vertices_unwrapped: JavaList, live_plot: bool,
                       refresh_every: int, ax: Any) -> sample_generator_types:
    traces = []
    x0 = 0
    while (True):
        network_sample = sample_iterator.next()
        sample = {
            Vertex._get_python_label(vertex_unwrapped):
            Tensor._to_ndarray(network_sample.get(vertex_unwrapped))
            for vertex_unwrapped in vertices_unwrapped
        }

        if live_plot:
            traces.append(sample)
            if len(traces) % refresh_every == 0:
                joined_trace = {
                    k: [t[k] for t in traces]
                    for k in sample.keys()
                }
                if ax is None:
                    ax = traceplot(joined_trace, x0=x0)
                else:
                    traceplot(joined_trace, ax=ax, x0=x0)
                x0 += refresh_every
                traces = []

        yield sample
Exemplo n.º 6
0
def assert_vertex_value_equals_scalar(vertex: Vertex, expected_type: Type,
                                      scalar: primitive_types) -> None:
    vertex_value = vertex.get_value()
    assert vertex_value == scalar
    assert type(vertex_value) == numpy_types
    assert vertex_value.shape == ()
    assert vertex_value.dtype == expected_type
Exemplo n.º 7
0
def assert_vertex_value_equals_pandas(vertex: Vertex, expected_type: Type,
                                      pandas: pandas_types) -> None:
    get_value = vertex.get_value()
    expected_value = pandas.values.astype(expected_type).reshape(
        get_value.shape)
    assert np.array_equal(get_value, expected_value)
    assert np.issubdtype(get_value.dtype, expected_type)
Exemplo n.º 8
0
def sample(net: BayesNet,
           sample_from: Iterable[Vertex],
           algo: str = 'metropolis',
           proposal_distribution: str = None,
           proposal_distribution_sigma: numpy_types = None,
           proposal_listeners=[],
           draws: int = 500,
           drop: int = 0,
           down_sample_interval: int = 1,
           plot: bool = False,
           ax: Any = None) -> sample_types:

    sampling_algorithm: JavaObject = build_sampling_algorithm(
        algo, proposal_distribution, proposal_distribution_sigma,
        proposal_listeners)

    vertices_unwrapped: JavaList = k.to_java_object_list(sample_from)

    network_samples: JavaObject = sampling_algorithm.getPosteriorSamples(
        net.unwrap(), vertices_unwrapped,
        draws).drop(drop).downSample(down_sample_interval)

    vertex_samples = {
        Vertex._get_python_label(vertex_unwrapped): list(
            map(Tensor._to_ndarray,
                network_samples.get(vertex_unwrapped).asList()))
        for vertex_unwrapped in vertices_unwrapped
    }

    if plot:
        traceplot(vertex_samples, ax=ax)

    return vertex_samples
Exemplo n.º 9
0
def _samples_generator(
        sample_iterator: JavaObject, vertices_unwrapped: JavaList,
        live_plot: bool, refresh_every: int, ax: Any, all_scalar: bool,
        id_to_label: Dict[Tuple[int, ...], str]) -> sample_generator_types:
    traces = []
    x0 = 0
    while (True):
        network_sample = sample_iterator.next()

        if all_scalar:
            sample: sample_generator_dict_type = {
                id_to_label[Vertex._get_python_id(vertex_unwrapped)]:
                Tensor._to_ndarray(
                    network_sample.get(vertex_unwrapped)).item()
                for vertex_unwrapped in vertices_unwrapped
            }
        else:
            sample = __create_multi_indexed_samples_generated(
                vertices_unwrapped, network_sample, id_to_label)

        if live_plot:
            traces.append(sample)
            if len(traces) % refresh_every == 0:
                joined_trace: sample_types = {
                    k: [t[k] for t in traces]
                    for k in sample.keys()
                }
                if ax is None:
                    ax = traceplot(joined_trace, x0=x0)
                else:
                    traceplot(joined_trace, ax=ax, x0=x0)
                x0 += refresh_every
                traces = []

        yield sample
Exemplo n.º 10
0
def __create_single_indexed_samples(
        network_samples: JavaObject, vertices_unwrapped: JavaList,
        id_to_label: Dict[Tuple[int, ...], str]) -> sample_types:
    vertex_samples: sample_types = {}
    for vertex_unwrapped in vertices_unwrapped:
        vertex_label = id_to_label[Vertex._get_python_id(vertex_unwrapped)]
        samples_for_vertex = __get_vertex_samples(network_samples,
                                                  vertex_unwrapped)
        vertex_samples[vertex_label] = samples_for_vertex.tolist()
    return vertex_samples
Exemplo n.º 11
0
def _samples_generator(sample_iterator: JavaObject,
                       vertices_unwrapped: JavaList) -> sample_generator_types:
    while (True):
        network_sample = sample_iterator.next()
        sample = {
            Vertex._get_python_id(vertex_unwrapped):
            Tensor._to_ndarray(network_sample.get(vertex_unwrapped))
            for vertex_unwrapped in vertices_unwrapped
        }
        yield sample
Exemplo n.º 12
0
def test_cannot_pass_generic_to_vertex(jvm_view: JVMView) -> None:
    class GenericExampleClass:
        pass

    with pytest.raises(
            ValueError,
            match=r"Can't parse generic argument. Was given {}".format(
                GenericExampleClass)):
        Vertex(  # type: ignore # this is expected to fail mypy
            jvm_view.GaussianVertex, "gaussian", GenericExampleClass(),
            GenericExampleClass())
Exemplo n.º 13
0
def __create_multi_indexed_samples_generated(
        vertices_unwrapped: JavaList, network_samples: JavaObject,
        id_to_label: Dict[Tuple[int, ...], str]) -> sample_generator_dict_type:
    vertex_samples_multi: Dict = {}
    for vertex in vertices_unwrapped:
        vertex_label = id_to_label[Vertex._get_python_id(vertex)]
        vertex_samples_multi[vertex_label] = defaultdict(list)
        sample = Tensor._to_ndarray(network_samples.get(vertex))
        __add_sample_to_dict(sample, vertex_samples_multi[vertex_label])

    tuple_hierarchy: Dict = {
        (vertex_label, tensor_index): values
        for vertex_label, tensor_index in vertex_samples_multi.items()
        for tensor_index, values in tensor_index.items()
    }

    return tuple_hierarchy
Exemplo n.º 14
0
def test_id_str_of_downstream_vertex_is_higher_than_upstream(
        jvm_view: JVMView) -> None:
    hyper_params = Vertex(jvm_view.GaussianVertex, 0., 1.)
    gaussian = Vertex(jvm_view.GaussianVertex, 0., hyper_params)

    hyper_params_id = hyper_params.get_id()
    gaussian_id = gaussian.get_id()

    assert type(hyper_params_id) == tuple
    assert type(gaussian_id) == tuple

    assert hyper_params_id < gaussian_id
Exemplo n.º 15
0
def __create_multi_indexed_samples(
        vertices_unwrapped: JavaList, network_samples: JavaObject,
        id_to_label: Dict[Tuple[int, ...], str]) -> sample_types:
    vertex_samples_multi: Dict = {}
    for vertex in vertices_unwrapped:
        vertex_label = id_to_label[Vertex._get_python_id(vertex)]
        vertex_samples_multi[vertex_label] = defaultdict(list)
        samples_for_vertex = __get_vertex_samples(network_samples, vertex)
        for sample in samples_for_vertex:
            __add_sample_to_dict(sample, vertex_samples_multi[vertex_label])

    tuple_hierarchy: Dict = {
        (vertex_label, shape_index): values
        for vertex_label, samples in vertex_samples_multi.items()
        for shape_index, values in samples.items()
    }

    return tuple_hierarchy
Exemplo n.º 16
0
def sample(net: BayesNet,
           sample_from: Iterable[Vertex],
           algo: str = 'metropolis',
           draws: int = 500,
           drop: int = 0,
           down_sample_interval: int = 1) -> sample_types:

    vertices_unwrapped = k.to_java_object_list(sample_from)

    network_samples = algorithms[algo].withDefaultConfig().getPosteriorSamples(
        net.unwrap(), vertices_unwrapped,
        draws).drop(drop).downSample(down_sample_interval)
    vertex_samples = {
        Vertex._get_python_id(vertex_unwrapped): list(
            map(Tensor._to_ndarray,
                network_samples.get(vertex_unwrapped).asList()))
        for vertex_unwrapped in vertices_unwrapped
    }

    return vertex_samples
Exemplo n.º 17
0
def test_get_connected_graph(jvm_view: JVMView) -> None:
    gaussian = Vertex(jvm_view.GaussianVertex, 0., 1.)
    connected_graph = set(gaussian.get_connected_graph())

    assert len(connected_graph) == 3
Exemplo n.º 18
0
def test_can_pass_array_to_vertex(jvm_view: JVMView) -> None:
    gaussian = Vertex(jvm_view.GaussianVertex, "gaussian", [3, 3], Const(0.),
                      Const(1.))
    sample = gaussian.sample()

    assert sample.shape == (3, 3)
Exemplo n.º 19
0
def test_java_vertex_to_python_vertex_persists_label() -> None:
    label = "gaussian"
    java_vertex = Gaussian(0., 1., label=label).unwrap()
    python_vertex = Vertex._from_java_vertex(java_vertex)
    assert python_vertex.get_label() == label
Exemplo n.º 20
0
def assert_vertex_value_equals_ndarray(vertex: Vertex, expected_type: Type,
                                       ndarray: numpy_types) -> None:
    vertex_value = vertex.get_value()
    expected_value = ndarray.astype(expected_type)
    assert np.array_equal(vertex_value, expected_value)
    assert np.issubdtype(vertex_value.dtype, expected_type)
Exemplo n.º 21
0
def test_construct_vertex_with_java_vertex(jvm_view: JVMView) -> None:
    java_vertex = Vertex(jvm_view.GaussianVertex, 0., 1.).unwrap()
    python_vertex = Vertex(java_vertex)

    assert tuple(java_vertex.getId().getValue()) == python_vertex.get_id()
Exemplo n.º 22
0
def test_can_pass_ndarray_to_vertex(jvm_view: JVMView) -> None:
    gaussian = Vertex(jvm_view.GaussianVertex, np.array([0.1, 0.4]),
                      np.array([0.4, 0.5]))
    sample = gaussian.sample()

    assert sample.shape == (2, )
Exemplo n.º 23
0
def test_you_can_set_value(vertex: Vertex, expected_type: Type,
                           value: tensor_arg_types,
                           assert_vertex_value_equals: Callable) -> None:
    vertex.set_value(value)
    assert_vertex_value_equals(vertex, expected_type, value)
Exemplo n.º 24
0
def test_can_pass_pandas_dataframe_to_vertex(jvm_view: JVMView) -> None:
    gaussian = Vertex(jvm_view.GaussianVertex, pd.DataFrame(data=[0.1, 0.4]),
                      pd.DataFrame(data=[0.1, 0.4]))
    sample = gaussian.sample()

    assert sample.shape == (2, 1)
Exemplo n.º 25
0
def test_construct_vertex_with_java_vertex() -> None:
    java_vertex = Gaussian(0., 1.).unwrap()
    python_vertex = Vertex._from_java_vertex(java_vertex)

    assert tuple(java_vertex.getId().getValue()) == python_vertex.get_id()
Exemplo n.º 26
0
def test_can_pass_pandas_series_to_vertex(jvm_view):
    gaussian = Vertex(jvm_view.GaussianVertex, pd.Series(data=[0.1, 0.4]),
                      pd.Series(data=[0.1, 0.4]))
    sample = gaussian.sample()

    assert sample.shape == (2, )
Exemplo n.º 27
0
 def __wrap(vertices: JavaMap) -> Dict[str, Vertex]:
     return {
         k.getUnqualifiedName(): Vertex._from_java_vertex(v)
         for k, v in vertices.items()
     }
Exemplo n.º 28
0
def test_can_pass_array_to_vertex(jvm_view: JVMView) -> None:
    gaussian = Vertex(jvm_view.GaussianVertex, [3, 3], 0., 1.)
    sample = gaussian.sample()

    assert sample.shape == (3, 3)
Exemplo n.º 29
0
 def get_double_model_output_vertex(self, label: str) -> Vertex:
     label_unwrapped = _VertexLabel(label).unwrap()
     result = self.unwrap().getDoubleModelOutputVertex(label_unwrapped)
     return Vertex._from_java_vertex(result)
Exemplo n.º 30
0
 def value_for(self, v: Vertex) -> numpy_types:
     return Tensor._to_ndarray(self.unwrap().getValueFor(
         v.unwrap().getReference()))