import pandas as pd
import jpype
import jpype.imports
from jpype.types import *
classpath = "lib/bayesserver-10.8.jar"
jpype.startJVM(classpath=[classpath])
import data_frame_utils as dfu
from com.bayesserver import *
from com.bayesserver.inference import *
from com.bayesserver.learning.parameters import *
from com.bayesserver.data import *
from com.bayesserver.data.sampling import *
from jpype import java, JImplements, JOverride
def nullable_int(x):
""" Helper function to convert an integer to a java nullable integer (java.lang.Integer) """
return java.lang.Integer(x)
def value_as_text(variable, time, evidence):
time_nullable = nullable_int(time)
if evidence.getEvidenceType(variable, time_nullable) == EvidenceType.NONE:
return "(null)"
if variable.getValueType() == VariableValueType.CONTINUOUS:
return f"{evidence.get(variable, time_nullable)}"
else:
return variable.getStates().get(evidence.getState(variable, time_nullable)).getName()
network = Network()
network.load("Tutorial 3 - Time series network.bayes")
x1 = network.getVariables().get("X1", True)
x2 = network.getVariables().get("X2", True)
fixedEvidence = DefaultEvidence(network)
fixedEvidence.set(x2, 3.4, nullable_int(3))
sampler = DataSampler(network, fixedEvidence)
options = DataSamplingOptions()
random = java.util.Random(0)
sample = DefaultEvidence(network)
for i in range(10):
try:
print(f"Sample {i}")
print("Time\tX1\tX2")
print("------------------------------")
sequence_length = 5
options.setSequenceLength(nullable_int(sequence_length))
sampler.takeSample(sample, random, options)
for t in range(sequence_length):
print(f"\t{t}\t{value_as_text(x1, t, sample)}\t{value_as_text(x2, t, sample)}")
except InconsistentEvidenceException:
print("Inconsistent evidence exception was raised.")