Variable generator (Add nodes from data) in Python

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

import pandas as pd
from jpype import *  # pip install jpype1   (for Java 7, pip install jpype1 0.6.3, and remove convertStrings=False)
import data_frame_utils as dfu


# This example uses a Pandas DataFrame as the data source for adding variables to a Bayesian network
# You can also connect to databases using DatabaseDataReaderCommand

# NOTE that this can be used to define variables from data,
# but does not learn the structure or parameters of the network (see other examples for learning)

classpath = "C:\\Program Files\\Bayes Server\\Bayes Server 8.12\\API\\Java\\bayesserver-8.12.jar"

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

bayes = JPackage("com.bayesserver")
bayes_data = bayes.data
bayes_data_discovery = bayes.data.discovery

d = {'A': [3.0, 10.0, 14.3, 12.2], 'B': [16, 12, 3, 6], 'C': ['A', 'B', 'A', 'B'], 'D': ['X', 'Y', 'X', 'X']}
df = pd.DataFrame(data=d)

dt = dfu.to_data_table(df)

network = bayes.Network()

data_reader_command = bayes_data.DataTableDataReaderCommand(dt)

options = bayes_data_discovery.VariableGeneratorOptions()

variable_defs = [
    bayes_data_discovery.VariableDefinition('A', 'A', bayes.VariableValueType.CONTINUOUS),
    bayes_data_discovery.VariableDefinition('B', 'B', bayes.VariableValueType.CONTINUOUS),
    bayes_data_discovery.VariableDefinition('C', 'C', bayes.VariableValueType.DISCRETE),
    bayes_data_discovery.VariableDefinition('D', 'D', bayes.VariableValueType.DISCRETE)
  ]


variable_infos = bayes_data_discovery.VariableGenerator.generate(
    data_reader_command,
    java.util.Arrays.asList(variable_defs),
    options)

# Note that no variables have yet been added to the network

print(network.getVariables().size())


for vi in variable_infos:
    variable = vi.getVariable()
    network.getNodes().add(bayes.Node(variable))

print(network.getVariables().size())


shutdownJVM()