Parameter tuning in Python

# __author__ = 'Bayes Server'
# __version__= '0.1'

from jpype import *  # pip install jpype1==0.7.5

# TODO change path to Bayes Server jar file
classpath = 'C:\\Program Files\\Bayes Server\\Bayes Server 9.2\\API\\Java\\bayesserver-9.2.jar'

startJVM(getDefaultJVMPath(), '-Djava.class.path=%s' % classpath, convertStrings=False)

bayes = JPackage('com.bayesserver')
bayes_data = bayes.data
bayes_inference = bayes.inference
bayes_analysis = bayes.analysis

# // TODO change path to Asia network
network_path = 'C:\\ProgramData\\Bayes Server 8.19\\Sample Networks\\Asia.bayes'

network = bayes.Network()
network.load(network_path)

variables = network.getVariables()

visit_to_asia = variables.get('Visit to Asia', True)
has_lung_cancer = variables.get('Has Lung Cancer', True)
tuberculosis_or_cancer = variables.get('Tuberculosis or Cancer', True)
smoker = variables.get('Smoker', True)
has_tuberculosis = variables.get('Has Tuberculosis', True)
dyspnea = variables.get('Dyspnea', True)
xray_result = variables.get('XRay Result', True)
has_bronchitis = variables.get('Has Bronchitis', True)

xRayResultAbnormal = xray_result.getStates().get('Abnormal', True)
smokerFalse = smoker.getStates().get('False', True)
hasLungCancerFalse = has_lung_cancer.getStates().get('False', True)

evidence = bayes_inference.DefaultEvidence(network)

# TODO set any evidence here if you need to...

sensitivity = bayes_analysis.SensitivityToParameters(network, bayes_inference.RelevanceTreeInferenceFactory())

parameters_to_test = []

parameters_to_test.append(
    bayes_analysis.ParameterReference(has_lung_cancer.getNode(), [smokerFalse, hasLungCancerFalse]))

# TODO add more parameters to test here if necessary

print('Node\tParameter\tMin\tMax')

for parameter in parameters_to_test:

    oneWay = sensitivity.oneWay(
        evidence,
        xRayResultAbnormal,
        parameter)

    try:

        output = bayes_analysis.ParameterTuning.oneWaySimple(
                        oneWay,
                        bayes.Interval(
                            java.lang.Double(0.2),
                            java.lang.Double(0.25),
                            bayes.IntervalEndPoint.CLOSED,
                            bayes.IntervalEndPoint.CLOSED))

        param_states_text = '[' + ','.join([str(s.getVariable().getName()) + ' = ' + str(s.getName()) for s in parameter.getStates()]) + ']'
        print('{}\t{}\t{}\t{}'.format(
            parameter.getNode().getName(),
            param_states_text,
            output.getInterval().getMinimum(),
            output.getInterval().getMaximum()
        ))

    except bayes_analysis.ConstraintNotSatisfiedException:
        print("Ignoring here as solution not found for this parameter.")

# Expected output...

# Node Parameter       Min Max
# Has Lung Cancer[Has Lung Cancer = False, Smoker = False]        0.686390938882659       0.795047852504759