예제 #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
예제 #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)
예제 #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
예제 #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)
예제 #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
예제 #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
예제 #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)
예제 #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
예제 #9
0
파일: sampling.py 프로젝트: shazbots/keanu
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
예제 #10
0
파일: sampling.py 프로젝트: shazbots/keanu
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
예제 #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
예제 #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())
예제 #13
0
파일: sampling.py 프로젝트: shazbots/keanu
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
예제 #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
예제 #15
0
파일: sampling.py 프로젝트: shazbots/keanu
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
예제 #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
예제 #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
예제 #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)
예제 #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
예제 #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)
예제 #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()
예제 #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, )
예제 #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)
예제 #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)
예제 #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()
예제 #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, )
예제 #27
0
 def __wrap(vertices: JavaMap) -> Dict[str, Vertex]:
     return {
         k.getUnqualifiedName(): Vertex._from_java_vertex(v)
         for k, v in vertices.items()
     }
예제 #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)
예제 #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)
예제 #30
0
 def value_for(self, v: Vertex) -> numpy_types:
     return Tensor._to_ndarray(self.unwrap().getValueFor(
         v.unwrap().getReference()))