Use a fixture for jvm_view
import keanu as kn
import numpy as np
import pytest
-from py4j.java_gateway import java_import
+
+@pytest.fixture
+def jvm_view():
+ from py4j.java_gateway import java_import
jvm_view = kn.KeanuContext().jvm_view()
java_import(jvm_view, "io.improbable.keanu.vertices.dbl.probabilistic.GaussianVertex")
+ return jvm_view
-def test_can_pass_scalar_to_vertex():
+def test_can_pass_scalar_to_vertex(jvm_view):
gaussian = kn.Vertex(jvm_view.GaussianVertex, (0., 1.))
sample = gaussian.sample()
assert sample.isScalar()
-def test_can_pass_ndarray_to_vertex():
+def test_can_pass_ndarray_to_vertex(jvm_view):
gaussian = kn.Vertex(jvm_view.GaussianVertex, (np.array([[0.1, 0.4]]), np.array([[0.4, 0.5]])))
sample = gaussian.sample()
@@ -24,7 +28,7 @@ def test_can_pass_ndarray_to_vertex():
assert shape[1] == 2
-def test_can_pass_java_object_to_vertex():
+def test_can_pass_java_object_to_vertex(jvm_view):
mu = kn.Vertex(jvm_view.GaussianVertex, (0., 1.))
gaussian = kn.Vertex(jvm_view.GaussianVertex, (mu, 1.))
sample = gaussian.sample()
@@ -32,7 +36,7 @@ def test_can_pass_java_object_to_vertex():
assert sample.isScalar()
-def test_can_pass_array_to_vertex():
+def test_can_pass_array_to_vertex(jvm_view):
gaussian = kn.Vertex(jvm_view.GaussianVertex, ([3, 3], 0., 1.))
sample = gaussian.sample()
@@ -42,7 +46,7 @@ def test_can_pass_array_to_vertex():
assert shape[1] == 3
-def test_cannot_pass_generic_to_vertex():
+def test_cannot_pass_generic_to_vertex(jvm_view):
class GenericExampleClass:
pass
@@ -52,14 +56,14 @@ def test_cannot_pass_generic_to_vertex():
assert str(excinfo.value) == "Can't parse generic argument. Was given {}".format(GenericExampleClass)
-def test_vertex_can_observe_scalar():
+def test_vertex_can_observe_scalar(jvm_view):
gaussian = kn.Vertex(jvm_view.GaussianVertex, (0., 1.))
gaussian.observe(4.)
assert gaussian.getValue().scalar() == 4.
-def test_vertex_can_observe_ndarray():
+def test_vertex_can_observe_ndarray(jvm_view):
gaussian = kn.Vertex(jvm_view.GaussianVertex, (0., 1.))
ndarray = np.array([[1.,2.]])
@@ -70,7 +74,7 @@ def test_vertex_can_observe_ndarray():
assert nd4j_tensor_flat[1] == 2.
-def test_vertex_can_overload_gt():
+def test_vertex_can_overload_gt(jvm_view):
gaussian = kn.Vertex(jvm_view.GaussianVertex, (0., 1.))
sample = gaussian.sample()
assert sample.isScalar()
