Пример #1
0
def test_you_can_throw_a_java_exception() -> None:
    with pytest.raises(JavaException, match="Illegal initial capacity: -1"):
        context = KeanuContext()
        try:
            context.jvm_view().java.util.HashMap(-1)
        except Py4JJavaError as e:
            raise JavaException(e)
Пример #2
0
def test_its_repr_method_gives_you_the_stack_trace() -> None:
    context = KeanuContext()
    with pytest.raises(Py4JJavaError) as excinfo:
        context.jvm_view().java.util.HashMap(-1)

    java_exception = JavaException(excinfo.value)

    assert str(java_exception).replace(
        "\r", "") == """An error occurred while calling None.java.util.HashMap.
Пример #3
0
def test_you_can_get_info_from_a_java_exception() -> None:
    context = KeanuContext()
    with pytest.raises(Py4JJavaError) as excinfo:
        context.jvm_view().java.util.HashMap(-1)

    java_exception = JavaException(excinfo.value)
    assert java_exception.get_class() == "java.lang.IllegalArgumentException"
    assert java_exception.get_message() == "Illegal initial capacity: -1"
    assert java_exception.unwrap().getCause() == None
Пример #4
0
def test_you_can_create_vertex_array_from_different_types() -> None:
    a = ConstantDouble(np.array([1., 2.]))
    b = ConstantDouble(np.array([3., 4.]))
    c = ConstantDouble(np.array([5., 6.]))

    context = KeanuContext()
    array = context.to_java_vertex_array([a, b, c])
    d = Concatenation(0, [a, b, c])
    assert np.allclose(d.get_value(), [1., 2., 3., 4., 5., 6.])
Пример #5
0
def jvm_view():
    from py4j.java_gateway import java_import
    jvm_view = KeanuContext().jvm_view()
    java_import(
        jvm_view,
        "io.improbable.keanu.vertices.dbl.probabilistic.GaussianVertex")
    return jvm_view
Пример #6
0
def test_you_can_convert_a_numpy_array_to_a_java_array() -> None:
    python_list = [1., 2., 3.]
    java_list = KeanuContext().to_java_array(python_list)
    assert type(java_list) == py4j.java_collections.JavaArray
    assert type(java_list[0]) == float
    assert java_list[0] == 1.
    assert java_list[1] == 2.
    assert java_list[2] == 3.
Пример #7
0
def test_throws_if_not_unwrapped_and_passed_to_java_object(
        java_list_wrapper) -> None:
    with pytest.raises(
            TypeError,
            match=
            "Trying to pass {} to a method that expects a JavaObject - did you forget to call unwrap()?"
            .format(type(java_list_wrapper))):
        lst = KeanuContext()._gateway.jvm.java.util.ArrayList(
            java_list_wrapper)
Пример #8
0
from keanu.base import JavaObjectWrapper
from keanu.context import KeanuContext
from py4j.java_gateway import java_import
from keanu.vertex import Vertex

k = KeanuContext()
java_import(k.jvm_view(), "io.improbable.keanu.util.*")
java_import(k.jvm_view(), "io.improbable.keanu.util.DescriptionCreator")

description_creator = k.jvm_view().io.improbable.keanu.util.DescriptionCreator(
)


def create_description(vertex: Vertex) -> str:
    return description_creator.createDescription(vertex.unwrap())
Пример #9
0
from typing import Set, Iterable, Union

from py4j.java_gateway import java_import

from keanu.base import JavaObjectWrapper
from keanu.context import KeanuContext
from keanu.vertex import Vertex

k = KeanuContext()

java_import(k.jvm_view(), "io.improbable.keanu.algorithms.mcmc.proposal.AcceptanceRateTracker")

proposal_listener_types = Union['AcceptanceRateTracker']


class AcceptanceRateTracker(JavaObjectWrapper):

    def __init__(self) -> None:
        super(AcceptanceRateTracker, self).__init__(k.jvm_view().AcceptanceRateTracker())

    def get_acceptance_rate(self, vertex: Vertex) -> float:
        return self.unwrap().getAcceptanceRate(vertex.unwrap().getId())
Пример #10
0
def test_there_is_only_one_jvm_view() -> None:
    view1 = KeanuContext().jvm_view()
    view2 = KeanuContext().jvm_view()
    assert view1 == view2
Пример #11
0
from numpy import ndenumerate, ndarray
from py4j.java_collections import JavaList
from py4j.java_gateway import java_import, JavaObject

from keanu.algorithm._proposal_distribution import ProposalDistribution
from keanu.context import KeanuContext
from keanu.net import BayesNet, ProbabilisticModel, ProbabilisticModelWithGradient
from keanu.plots import traceplot
from keanu.tensor import Tensor
from keanu.vartypes import sample_types, sample_generator_types, numpy_types, sample_generator_dict_type, tensor_arg_types
from keanu.vertex.base import Vertex

COLUMN_HEADER_FOR_SCALAR = (0, )

k = KeanuContext()

java_import(k.jvm_view(),
            "io.improbable.keanu.algorithms.mcmc.MetropolisHastings")
java_import(k.jvm_view(), "io.improbable.keanu.algorithms.mcmc.nuts.NUTS")
java_import(
    k.jvm_view(),
    "io.improbable.keanu.algorithms.mcmc.RollBackToCachedValuesOnRejection")
java_import(k.jvm_view(), "io.improbable.keanu.algorithms.sampling.Forward")


class PosteriorSamplingAlgorithm:
    def __init__(self, sampler: JavaObject):
        self._sampler = sampler

    def get_sampler(self) -> JavaObject:
Пример #12
0
from py4j.java_gateway import java_import

from keanu.base import JavaObjectWrapper
from keanu.context import KeanuContext

k = KeanuContext()

java_import(k.jvm_view(), "io.improbable.keanu.vertices.VertexLabel")


class _VertexLabel(JavaObjectWrapper):
    """
    This class is intended for private use - as a user you should only need to specify string values.
    It's used behind the scenes e.g. in :class:`keanu.vertex.lambda_model.LambdaModel`
    """

    __separator = "."

    def __init__(self, name: str):
        parts = name.split(_VertexLabel.__separator)

        if len(parts) == 1:
            java_object = k.jvm_view().VertexLabel(name)
        else:
            java_object = k.jvm_view().VertexLabel(
                parts[0], k.to_java_string_array(parts[1:]))
        super(_VertexLabel, self).__init__(java_object)

    def get_name(self) -> str:
        return self.unwrap().getQualifiedName()
Пример #13
0
from py4j.java_gateway import java_import, JavaObject
from keanu.context import KeanuContext
from keanu.net import BayesNet
from keanu.vertex.base import Vertex
from typing import Union, Optional, Tuple

k = KeanuContext()

java_import(
    k.jvm_view(),
    "io.improbable.keanu.algorithms.variational.optimizer.gradient.GradientOptimizer"
)
java_import(
    k.jvm_view(),
    "io.improbable.keanu.algorithms.variational.optimizer.nongradient.NonGradientOptimizer"
)


class Optimizer:
    def __init__(self, optimizer: JavaObject, net: Union[BayesNet,
                                                         Vertex]) -> None:
        self.optimizer = optimizer
        self.net = net

    def max_a_posteriori(self) -> float:
        return self.optimizer.maxAPosteriori()

    def max_likelihood(self) -> float:
        return self.optimizer.maxLikelihood()

    @staticmethod
Пример #14
0
import numpy as np
from numpy import ndarray
from py4j.java_gateway import java_import, JavaObject, JavaMember, is_instance_of
from typing import Any

from keanu.base import JavaObjectWrapper
from keanu.context import KeanuContext
from keanu.functional import Function
from .vartypes import (numpy_types, tensor_arg_types, primitive_types, runtime_int_types, runtime_float_types,
                       runtime_bool_types, runtime_numpy_types, runtime_pandas_types, runtime_primitive_types,
                       primitive_types)

k = KeanuContext()

java_import(k.jvm_view(), "io.improbable.keanu.tensor.dbl.DoubleTensor")
java_import(k.jvm_view(), "io.improbable.keanu.tensor.bool.BooleanTensor")
java_import(k.jvm_view(), "io.improbable.keanu.tensor.intgr.IntegerTensor")
java_import(k.jvm_view(), "io.improbable.keanu.util.Py4jByteArrayConverter")


class Tensor(JavaObjectWrapper):

    def __init__(self, t: tensor_arg_types) -> None:
        if isinstance(t, runtime_numpy_types):
            super(Tensor, self).__init__(Tensor.__get_tensor_from_ndarray(t))
        elif isinstance(t, runtime_pandas_types):
            super(Tensor, self).__init__(Tensor.__get_tensor_from_ndarray(t.values))
        elif isinstance(t, runtime_primitive_types):
            super(Tensor, self).__init__(Tensor.__get_tensor_from_scalar(t))
        else:
            raise NotImplementedError("Generic types in an ndarray are not supported. Was given {}".format(type(t)))
Пример #15
0
from typing import Union

from keanu.context import KeanuContext
from keanu.infer_type import get_type_of_value
from keanu.vartypes import tensor_arg_types
from .base import Vertex, Double, Integer, Boolean
from .generated import cast_to_boolean_vertex, cast_to_double_vertex, cast_to_integer_vertex

context = KeanuContext()


def If(predicate: Union[tensor_arg_types, Vertex],
       thn: Union[tensor_arg_types, Vertex], els: Union[tensor_arg_types,
                                                        Vertex]) -> Vertex:
    then_type = get_type_of_value(thn)
    else_type = get_type_of_value(els)

    if then_type == float or else_type == float:
        return Double(context.jvm_view().WhereVertex, None,
                      cast_to_boolean_vertex(predicate),
                      cast_to_double_vertex(thn), cast_to_double_vertex(els))
    elif then_type == int or else_type == int:
        return Integer(context.jvm_view().WhereVertex, None,
                       cast_to_boolean_vertex(predicate),
                       cast_to_integer_vertex(thn),
                       cast_to_integer_vertex(els))
    elif then_type == bool and else_type == bool:
        return Boolean(context.jvm_view().WhereVertex, None,
                       cast_to_boolean_vertex(predicate),
                       cast_to_boolean_vertex(thn),
                       cast_to_boolean_vertex(els))
Пример #16
0
from py4j.java_gateway import java_import, JavaObject
from py4j.java_collections import JavaList
from keanu.context import KeanuContext
from keanu.tensor import Tensor
from keanu.vertex.base import Vertex
from keanu.net import BayesNet
from typing import Any, Iterable, Dict, List, Tuple, Generator
from keanu.vartypes import sample_types, sample_generator_types

k = KeanuContext()

java_import(k.jvm_view(),
            "io.improbable.keanu.algorithms.mcmc.MetropolisHastings")
java_import(k.jvm_view(), "io.improbable.keanu.algorithms.mcmc.NUTS")
java_import(k.jvm_view(), "io.improbable.keanu.algorithms.mcmc.Hamiltonian")

algorithms = {
    'metropolis': k.jvm_view().MetropolisHastings,
    'NUTS': k.jvm_view().NUTS,
    'hamiltonian': k.jvm_view().Hamiltonian
}


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)
Пример #17
0
from py4j.java_collections import ListConverter

from functools import partial
from collections.abc import Iterable as CollectionsIterable

from keanu import BayesNet
from keanu.base import JavaObjectWrapper
from keanu.context import KeanuContext
from keanu.functional import BiConsumer
from keanu.functional import Consumer
from keanu.functional import JavaIterator
from keanu.vertex import Vertex, cast_to_double_vertex, vertex_constructor_param_types, DoubleProxy, shape_types, \
    IntegerProxy, BooleanProxy
from keanu.vertex.label import _VertexLabel

k = KeanuContext()

java_import(k.jvm_view(), "io.improbable.keanu.templating.SequenceBuilder")
java_import(k.jvm_view(), "io.improbable.keanu.vertices.SimpleVertexDictionary")
java_import(k.jvm_view(), "io.improbable.keanu.templating.SequenceItem")
java_import(k.jvm_view(), "io.improbable.keanu.templating.SequenceLoader")


class SequenceItem(JavaObjectWrapper):

    def add(self, vertex: Vertex, label: Optional[str] = None) -> None:
        if label is None:
            self.unwrap().add(vertex.unwrap())
        else:
            self.unwrap().add(_VertexLabel(label).unwrap(), vertex.unwrap())
Пример #18
0
from typing import Union, Optional, Tuple

from py4j.java_gateway import java_import, JavaObject, JavaClass

from keanu.base import JavaObjectWrapper
from keanu.context import KeanuContext
from keanu.net import BayesNet
from keanu.tensor import Tensor
from keanu.vartypes import numpy_types
from keanu.vertex.base import Vertex

k = KeanuContext()

java_import(
    k.jvm_view(),
    "io.improbable.keanu.algorithms.variational.optimizer.gradient.GradientOptimizer"
)
java_import(
    k.jvm_view(),
    "io.improbable.keanu.algorithms.variational.optimizer.gradient.ConjugateGradient"
)
java_import(
    k.jvm_view(),
    "io.improbable.keanu.algorithms.variational.optimizer.RelativeConvergenceChecker"
)
java_import(
    k.jvm_view(),
    "io.improbable.keanu.algorithms.variational.optimizer.AbsoluteConvergenceChecker"
)
java_import(
    k.jvm_view(),
Пример #19
0
from py4j.java_gateway import java_import, JavaObject, JavaClass
from keanu.context import KeanuContext
from keanu.net import BayesNet
from keanu.vertex.base import Vertex
from typing import Union, Optional, Tuple

k = KeanuContext()

java_import(k.jvm_view(), "io.improbable.keanu.algorithms.variational.optimizer.gradient.GradientOptimizer")
java_import(k.jvm_view(), "io.improbable.keanu.algorithms.variational.optimizer.nongradient.NonGradientOptimizer")
java_import(k.jvm_view(), "io.improbable.keanu.algorithms.variational.optimizer.KeanuOptimizer")


class Optimizer:

    def __init__(self, optimizer: JavaObject, net: Union[BayesNet, Vertex]) -> None:
        self.optimizer = optimizer
        self.net = net

    def max_a_posteriori(self) -> float:
        return self.optimizer.maxAPosteriori()

    def max_likelihood(self) -> float:
        return self.optimizer.maxLikelihood()

    @staticmethod
    def _build_bayes_net(factory_class: JavaClass,
                         net: Union[BayesNet, Vertex]) -> Tuple[JavaObject, Union[BayesNet, Vertex]]:

        if not (isinstance(net, BayesNet) or isinstance(net, Vertex)):
            raise TypeError("net must be a Vertex or a BayesNet. Was given {}".format(type(net)))
Пример #20
0
def test_the_context_is_a_singleton() -> None:
    context1 = KeanuContext()
    context2 = KeanuContext()
    assert context1 == context2
Пример #21
0
import collections
from typing import List, Tuple, Iterator, Union, SupportsRound, Optional
from typing import cast as typing_cast

import numpy as np
from py4j.java_collections import JavaList, JavaArray
from py4j.java_gateway import JavaObject, JavaMember

import keanu as kn
from keanu.base import JavaObjectWrapper
from keanu.context import KeanuContext
from keanu.tensor import Tensor
from keanu.vartypes import (tensor_arg_types, wrapped_java_types, shape_types, numpy_types, runtime_tensor_arg_types,
                            runtime_primitive_types, runtime_wrapped_java_types)

k = KeanuContext()

vertex_operation_param_types = Union['Vertex', tensor_arg_types]
vertex_constructor_param_types = Union['Vertex', tensor_arg_types, wrapped_java_types]


class Vertex(JavaObjectWrapper, SupportsRound['Vertex']):

    def __init__(self, val_or_ctor: Union[JavaMember, JavaObject],
                 *args: Union[vertex_constructor_param_types, shape_types]) -> None:
        val: JavaObject
        if args:
            ctor = val_or_ctor
            val = ctor(*(Vertex.__parse_args(args)))
        else:
            val = typing_cast(JavaObject, val_or_ctor)
Пример #22
0
from typing import Dict, Callable, Optional

from py4j.java_collections import JavaMap
from py4j.java_gateway import java_import

from keanu.context import KeanuContext
from keanu.functional import Consumer, Supplier
from keanu.vertex.base import Vertex
from keanu.vertex.label import _VertexLabel

context = KeanuContext()
java_import(context.jvm_view(),
            "io.improbable.keanu.vertices.model.LambdaModelVertex")


class LambdaModel(Vertex):
    def __init__(self,
                 inputs: Dict[str, Vertex],
                 executor: Callable,
                 update_values: Callable = None,
                 label: Optional[str] = None) -> None:
        self.vertices_wrapped = inputs
        vertex_map = LambdaModel.__to_java_map(inputs)
        self.executor = executor
        self.update_values = update_values or (lambda: self.vertices_wrapped)

        vertex = context.jvm_view().LambdaModelVertex(
            vertex_map, Consumer(self.__execute),
            Supplier(lambda: self.__update_value()))
        super(LambdaModel, self).__init__(vertex, label)
Пример #23
0
)
from .vertex_casting import (
    do_vertex_cast,
    do_inferred_vertex_cast,
    cast_to_double_tensor,
    cast_to_integer_tensor,
    cast_to_boolean_tensor,
    cast_to_double,
    cast_to_integer,
    cast_to_boolean,
    cast_to_long_array,
    cast_to_int_array,
    cast_to_vertex_array,
)

context = KeanuContext()


def cast_to_double_vertex(input: vertex_constructor_param_types) -> Vertex:
    return do_vertex_cast(ConstantDouble, input)


def cast_to_integer_vertex(input: vertex_constructor_param_types) -> Vertex:
    return do_vertex_cast(ConstantInteger, input)


def cast_to_boolean_vertex(input: vertex_constructor_param_types) -> Vertex:
    return do_vertex_cast(ConstantBoolean, input)


def cast_to_vertex(input: vertex_constructor_param_types) -> Vertex:
Пример #24
0
from typing import List, Any, Callable, Dict

from py4j.java_gateway import java_import

from keanu.base import JavaObjectWrapper
from keanu.context import KeanuContext
from keanu.tensor import Tensor
from keanu.vartypes import numpy_types

k = KeanuContext()

java_import(
    k.jvm_view(),
    "io.improbable.keanu.algorithms.mcmc.proposal.GaussianProposalDistribution"
)
java_import(
    k.jvm_view(),
    "io.improbable.keanu.algorithms.mcmc.proposal.PriorProposalDistribution")

proposal_distribution_types: Dict[str, Callable] = {
    "gaussian": k.jvm_view().GaussianProposalDistribution,
    "prior": k.jvm_view().PriorProposalDistribution,
}


class ProposalDistribution(JavaObjectWrapper):
    def __init__(self,
                 type_: str,
                 sigma: numpy_types = None,
                 listeners: List[Any] = []) -> None:
        ctor = proposal_distribution_types[type_]
Пример #25
0
        def __init__(self, numbers):
            lst = KeanuContext()._gateway.jvm.java.util.ArrayList()
            for number in numbers:
                lst.add(number)

            super(JavaListWrapper, self).__init__(lst)