Package: pyagena
Table of ContentsDescriptionPrerequisites and InstallationStructure of pyagena ClassesClass and Instance MethodsImporting a Model from .cmpxCreating and Modifying a Model in pythonAgena.ai Cloud with pyagenaLocal agena.ai API with pyagenaImporting and Exporting Datasets for Batch Calculationpyagena Use Case Examples1. Descriptionpyagena is a python environment for creating, modifying, and parsing Bayesian network models, and sending the models to agena.ai Cloud or to local agena.ai developer API to execute calculation requests. The environment allows users to read and modify Bayesian networks from .cmpx model files, create new Bayesian networks in python and export to .cmpx and .json files locally, as well as authenticate with agena.ai Cloud or use local agena.ai developer API for model calculations and sensitivity analyses. In the rest of this document, the python environment for agena.ai is referred to as pyagena. pyagena is developed for Python 3.8+.2. Prerequisites and InstallationYou can install pyagena using pip:pipinstallpyagenapyagena requires the following python packages to be installed:requests,pandas,networkx, andmatplotlibTo install the dependencies, you can usepipwith the commands:pipinstallrequests
pipinstallpandas
pipinstallmatplotlib
pipinstallnetworkxOnce installed, you can import the package in the python script (or a jupyter notebook file) to use. For example:importpyagenaasagorfrompyagenaimport*3. Structure of pyagena ClassesThe Bayesian networks (BNs) in pyagena are represented with several objects:Model,Network,Node, andDataset. These python objects generally follow their equivalents defined in agena.ai models.3.1ModelobjectsThese represent the overall BN. A single .cmpx file corresponds to a singeModel. A BN model can have multiple networks with their own nodes, links between these networks, and datasets.3.1.1idId of the Model, optional.3.1.2networksA list of all theNetworkobjects in the model. This field will be populated with thecreate_network()method. See below for more information aboutNetworks.3.1.3datasetsA list of all theDatasetobjects in the model. When a newModelis created, it will have the default first dataset "Case 1", and it is possible to add new datasets to the model afterwards with thecreate_dataset()method. See below for more information aboutDatasets.3.1.4network_linksIf theModelhas multiple networks, it is possible to have links between these networks, following the agena.ai model network_links format.To see how to create these links, seeadd_network_link()function later in this document.3.1.5settingsModelsettings for calculations. It includes the following fields (the values in parantheses are the defaults if settings are not specified for a model):discreteTails (False)sampleSizeRanked (5)convergence (0.01)iterations (50)tolerance (1)Model settings can be provided when creating a new model, if not provided the model will come with the default settings. Default settings can be changed later on (with the methodchange_settings()), or model settings can be reset back to default values (with the methoddefault_settings()). See the correct input parameter format for these functions in the following section. Individual fields in model setting can be adjusted withchange_settings()too. Individual setting value can be accessed withsyntax model.settings['convergence'].3.2NetworkobjectsThese represent each network in a BN model. Networks consist of nodes and in a BN model there might be more than one network. These networks can also be linked to each other with the use of input and output nodes. For such links, seeModel.network_linksfield.The fields that define aNetworkobject are as follows:3.2.1idId of theNetwork. Mandatory field to create a new network.3.2.2nameName of the network, optional. If not specified,idof the network is passed ontonamefield as well.3.2.3descriptionDescription, optional. If not specified, the string "New Network" is assigned todescriptionfield by default.3.2.4nodesA list ofNodeobjects which are in the network. See below for more information aboutNodes.Note thatNetworkobjects do have acreate_edge()method but do not have alinksfield unlike the agena.ai models. As explained inNode.parentssection below, this information is stored inNodeobjects in the python environment. When importing a .cmpx model, the information inlinksfield is used to populateNode.parentsfields for each node. Similarly, when exporting to a .cmpx/.json file, the parent-child information inNode.parentsfield is used to create thelinksfield of theNetworkfield of the .cmpx/.json.3.3NodeobjectsThese represent the nodes in a BN. The fields that define aNodeobject are as follows:3.3.1idMandatory field to create a newNodeobject. This is the unique identifier of agena.ai model nodes.3.3.2nameName of the node, optional. If not defined,idof the node will be passed onto thenamefield too.3.3.3descriptionDescription of the node, optional. If not defined, "New Node" will be assigned to thedescriptionfield.3.3.4typeNode type, it can be:BooleanLabelledRankedDiscreteRealContinuousIntervalIntegerIntervalIf it's not specified when creating a new node, the new node is "Boolean" by default if it's not a simulation node; and it is "ContinuousInterval" by default if it's a simulation node.3.3.5parentsOtherNodeobjects can be pointed as parents of aNodeobject. To add parents to a node, see the node methodadd_parent()or network methodcreate_edge().Something to keep in mind: the parent-child relationship information is stored atNodelevel in the python environment thanks to this field, as opposed to the separatelinksfield of a .cmpx/.json file for the agena.ai models. When importing or exporting .cmpx files you do not need to think about this difference, as the file import and export functions handle the correct formats. This difference allows adding and removingNodeobjects as parents.3.3.6simulatedA boolean field to indicate whether the node is a simulation node or not.3.3.7distr_typeThe table type of the node, it can be:ManualExpressionPartitionedTo set the table (distribution) type of a node, see the node methodset_distr_type()or the network methodset_node_distr_type().3.3.8statesStates of the node (if not simulated). If states are not specified, depending on thetype, sensible default states are assigned. Default states for different node types are:"Boolean" or "Labelled" node: "False", "True""Ranked" node: "Low", "Medium", "High""DiscreteReal" node: "0", "1""IntegerInterval" node (if not simulated): "(-Infinity, -1]", "[0, 4]", "[5, Infinity)""ContinuousInterval" node (if not simulated): "(-Infinity, -1)", "[-1, 1)", "[1, Infinity)"And for a node with the table type (distr_type) "Expression", the default expression is: "Normal(0,1000000)".To set new states to an existing nodes, see the node methodset_states()or the network methodset_node_states().3.3.9probabilitiesIf the table type (distr_type) of the node is "Manual", the node will have state probabilities, values in its NPT. This field is a list of lists containing these values. The length of the list depends on the node states and the number of its parents. To see how to set probability values for a node, see the node methodset_probabilities()or network methodset_node_probabilities().3.3.10expressionsIf the table type (distr_type) of the node is "Expression" or "Partitioned", the node will have expression(s) instead of the manually defined NPT values.If the node's table type is "Expression", theexpressionsfield will be a list with a single expression.If the node's table type is "Partitioned", theexpressionsfield will be a list of as many expressions as the number of parent node states on which the expression is partitioned.To see how to set the expressions for a node, see the node methodset_expressions()or the network methodset_node_expressions().Possible expressions for the node types are listed below:Boolean and Labelled:ComparativeIntegerIntervalBinomialExponentialGeometricHypergeometricNegative BinomialNormalPoissonTNormalTriangularUniformContinuousIntervalArithmeticBetaBetaPertChiSquaredExponentialExtremeValueGammaLogisticLogNormalNormalStudentTNormalTriangleUniformWeibullRankedTNormalFor further information on expressions, please refer toagena.ai modeller manual, Sections 22 (Statistical distributions) and 23 (Expressions).3.3.11partitionsIf the table type (distr_type) of the node is "Partitioned", in addition to the expressions, the node will have thepartitionsfield. This field is a list of strings, which areids of the parent nodes on which the node expression is partitioned.3.3.12variablesThe node variables are called constants on agena.ai Modeller. This field, if specified, sets the constant value for the node observations. For the correct syntax of defining a variable, seeset_variable()function.3.4DatasetobjectsThese represent the set of observations in a BN model (cases). AModelcan have multipleDatasetobjects in itsdatasetsfield.  When a newModelis created, it always comes with a defaultDatasetobject with theid"Case 1" and with blank observations. It is possible to add more datasets (cases) with theirids. EachDatasetobject under aModelcan be called a new "case".3.4.1idId of the dataset (case).3.4.2observationsUnder each dataset (case), observations for all the observed nodes in all the networks of the model (in terms of their states or values) are listed. If it's hard evidence, observation for a node will have a single value with the weight of 1. If a node in the model has a value in itsvariablefield, this value will be passed onto the dataset (case) with the weight of 1.3.4.3resultsThis field is defined only for when a .cmpx model with calculations is imported or after the model is calculated with the agena.ai Cloud or local API. When creating a new BN in the python environment, this field is not filled in. Theresultsfield stores the posterior probability and inference results upon model calculation on agena.ai Cloud or local agena.ai developer API.4. Class and Instance MethodsTheModel,Network,Node, andDatasetobjects have their own respective methods to help their definition and manipulate their fields. The python instance methods are used with the.sign following an instance of the class. For example,example_node.add_parent(example_parent_node)orexample_network.remove_node(example_node)orexample_model.create_dataset(example_case)Before going into the details of methods, a note on displaying success messages and logging info as you go on with the pyagena usage: If you'd like to see the success messages and logging info, you can set the following once at the beginning of the work:set_verbose(True)This is for success messages and logging info. The warning and error messages are displayed by default.To also display timestamps of the messages:include_timestamps(True)4.1ModelmethodsAModelobject consists of networks, network links, datasets, and settings. Once a newModelis created, it is possible to createNetworks andDatasets in it. By default, a newModelobject comes with a single empty dataset (case) called "Case 1". Following methods can be used to modifyModelobjects:4.1.1create_network(id, name=None, description=None)A method to create a newNetworkobject in aModeland add it to thenetworksfield of theModelobject. The inputidis mandatory. The newNetworkobject is added to theModelif theModeldoes not already have aNetworkwith the given id.4.1.2remove_network(id)A method to remove an existingNetworkobject from the model using itsid. Note that removing aNetworkfrom aModeldoesn't automatically remove its possible network links to otherNetworks in the model.network-linksfield of aModelshould be adjusted accordingly if needed.4.1.3get_network(network_id)A method to access aNetworkobject in aModel. TheNetworkcan be assigned to a variable and used.4.1.4add_network_link(source_network_id, source_node_id, target_network_id, target_node_id, link_type, pass_state = optional)This is the method to add links to a model between its networks. These links start from a "source node" in a network and go to a "target node" in another network. To create the link, the source and target nodes in the networks need to be specified together with the network they belong to (by theNodeandNetworkids). The input parameters are as follows:source_network_id=Network.idof the network the source node belongs tosource_node_id=Node.idof the source nodetarget_network_id=Network.idof the network the target node belongs totarget_node_id=Node.idof the target nodelink_type= a string of the link type name. If not specified, it isMarginals. It can be one of the following:MarginalsMeanMedianVarianceStandardDeviationLowerPercentileUpperPercentileStatepass_state= one of theNode.statesof the source node. It has to be specified only if thelink_typeof the link is"State", otherwise is left blank.Note that links between networks are allowed only when the source and target nodes fit certain criteria. Network links are allowed if:Both nodes are the same type and either of them is simulatedBoth nodes are the same type and neither is simulated and both have the same number of statesSource node is not numeric interval or discrete real and target node is simulated4.1.5remove_network_link(source_network_id, source_node_id,target_network_id, target_node_id)A method to remove network links, given theids of the source and target nodes (and the networks they belong to).4.1.6remove_all_network_links()A method to remove all existing network links in a model.4.1.7create_dataset(dataset_id)It is possible to add multiple cases to a model. These cases are newDatasetobjects added to thedatasetsfield of a model. Initially these cases have no observations and are only defined by theirids. The cases are populated with theenter_observation()function. Thecreate_dataset()function takes theidof the new dataset to be added as input.4.1.8remove_dataset(dataset_id)A method to remove an existing scenario from the model. Input parameterdataset_idis the string which is theidof a dataset (case).4.1.9get_dataset(dataset_id)A method to access aDatasetobject in aModel. TheDatasetcan be assigned to a variable and used.4.1.10enter_observation(network_id, node_id, value, dataset_id = optional, variable_input = False)A method to enter observation to a dataset in the model. Calls the dataset methodenter_observationin the background.To enter the observation to a specific dataset (case), the dataset id must be given as the input parameterdataset. Ifdatasetis left blank, the entered observation will by default go to the first dataset (case) of the model (called "Case 1" by default). This means that if there is no extra datasets created for a model (which by default comes with "Case 1"), any observation entered will be set for this dataset (mimicking the behaviour of entering observation to agena.ai Modeller).The observation is defined with the mandatory input parameters:network_id=Network.idof the network the observed node belongs tonode_id=Node.idof the observed nodevalue= this parameter can be:the value or state of the observation for the observed node (if it is hard evidence)the array of multiple values and their weights (if it is soft evidence)the value of the observation for a variable of the observed nodedataset_id= optional,Dataset.idof the dataset which will have the observationvariable_input= a boolean parameter, set toTrueif the entered observation is a variable (constant) id for the node instead of an observed value.A node can have only one observation. This observation can be hard evidence (single state or a single value) or soft evidence (multiple states or multiple values with their weights). If the node has variable(s), the variable value(s) can also be observed. Each variable of the node can have an observation. If the node with variables has observations for its variables and an observation for itself, the variable observations will be ignored and only the node observation will be used in calculations.4.1.11remove_observation(network_id, node_id, dataset_id = optional)A method to remove a specific observation from the model. Calls the dataset methodremove_observationin the background. It requires the id of the node which has the observation to be removed and the id of the network the node belongs to.4.1.12clear_dataset_observations(dataset_id)A method to clear all observations in a specific dataset (case) in the model. Calls the dataset methodclear_all_observations()in the background.4.1.13clear_all_observations()A method to clear all observations defined in a model. This function removes all observations from all datasets (cases).4.1.14change_settings(setting arguments)A method to change model settings. The input parameters can be some or all of thesettingsfields. For example:example_model.change_settings(convergence=0.001,iterations=75)4.1.15default_settings()A method to reset model settings back to default values. The default values for model settings are:discreteTails = FalsesampleSizeRanked = 5convergence = 0.01iterations = 50tolerance = 14.1.16save_to_file(filename)A method to export theModelto a .cmpx or a .json file. This method passes on all the information about the model, its datasets, its networks, their nodes, and model settings to a file in the correct format readable by agena.ai.Input parameterfilenamemust have a file extension of '.cmpx' or '.json'.4.1.17import_data(filename)A method to import dataset observations to the model using either a .csv or a .json input file. See examples below for the correct format of the input .csv and .json for data import.4.1.18export_data(filename, dataset_ids = optional, include_inputs = False, include_outputs = True)A method to export dataset results or observations from the model to an either .csv or .json output file. See examples below for the format of the output .csv and .json files.4.1.19from_cmpx(filepath = "/path/to/model/file.cmpx")This is the class method to create aModelobject from a .cmpx file. The method parses the .cmpx file and creates the python objects based on the model in the file. To see its use, see examples below.4.1.20create_csv_template()This method creates an empty CSV file for the model with the correct format so that it can be filled in and used forimport_data(filename). Note that this template includes every single node in the model, not all of which might be observable - you can delete the columns of the nodes which will not be observed. See examples below for its format and use.4.1.21create_sensitivity_config(...)A method to create a sensitivity configuration object if a sensitivity analysis request will be sent to agena.ai Cloud servers or the local API. Its parameters are:targetNode= target node ID for the analysissensitivityNodes= a list of sensitivity node IDs(optional)network= ID of the network to perform analysis on. If missing, the first network in the model is used(optional)dataSet= ID of the dataSet (scenario) to use for analysis(optional)report_settings= a dictionary for settings for the sensitivity analysis report. The elements of the dictionary are:"summaryStats"(a list with some/all of the following fields)meanmedianvariancestandardDeviationupperPercentilelowerPercentile"sumsLowerPercentileValue"(set the reported lower percentile value.
Default is 25)"sumsUpperPercentileValue"(set the reported upper percentile value.
Default is 75)"sensLowerPercentileValue"(lower percentile value to limit sensitivity node data by. Default is 0)"sensUpperPercentileValue"(upper percentile value to limit sensitivity node data by. Default is 100)For the use of the function, see Sections8and9.4.2NetworkmethodsANetworkobject is created in aModelobject. Once a network of a model is created, the following methods can be used to modify and manipulate the object and its nodes.4.2.1create_node(id, name=optional, description=optional, type=optional, simulated=optional, states=optional)A method to create a newNodeobject in aNetworkand add the new node to thenodesfield of theNetworkobject. Theidof the new node is mandatory whereasname,description,type,simulated, andstatesare optional arguments. A newNodeobject is created with the arguments and added to the network if the network does not already have a node with the given id.4.2.2remove_node(node_id)A method to remove an existingNodeobject from the network. Note that removing a Node from a network doesn't automatically remove it from its previous parent-child relationships in the network. You need to adjust such relationships separately onNodelevel.4.2.3get_node(node_id)A method to access aNodeobject in aNetwork. TheNodecan be assigned to a variable and used.4.2.4create_edge(child_id, parent_id)A method to add a parentNodeto anotherNodein aNetwork. It takes node ids as parameters and runs theadd_parent()method of a node.4.2.5remove_edge(child_id, parent_id)A method to remove a parentNodefrom anotherNodein aNetwork. It takes node ids as parameters and runs theremove_parent()method of a node.4.2.6set_node_probabilities(node_id, new_probs, by_row=False)A method to modifyNodes in aNetwork. On the definedNodewith thenode_idparameter, it calls the node methodset_probabilities().4.2.7set_node_statesA method to modifyNodes in aNetwork. On the definedNodewith thenode_idparameter, it calls the node methodset_states().4.2.8set_node_expressionsA method to modifyNodes in aNetwork. On the definedNodewith thenode_idparameter, it calls the node methodset_expressions().4.2.9set_node_variableA method to modifyNodes in aNetwork. On the definedNodewith thenode_idparameter, it calls the node methodset_variable().4.2.10set_node_distr_typeA method to modifyNodes in aNetwork. On the definedNodewith thenode_idparameter, it calls the node methodset_distr_type().4.2.11plot()A method to plot the graphical structure of a BN network.4.3NodemethodsNodeobjects are created in aNetworkobject (see above). Once the nodes of a network are created, it is possible to modify them.SomeNodefields can be modified with a direct access to the field. For example, to update the name or a description information of aNode, simply use:example_node.name="new node name"orexample_node.description="new node description"Because changing the name or description of aNodedoes not cause any compatibility issues. However, some fields such as table type or parents will have implications for other fields. Changing the node parents will change the size of its NPT, changing the node's table type from "Manual" to "Expression" will mean the state probabilities are now defined in a different way. Therefore, to modify such fields of aNode, use the corresponding method described below. These methods will ensure all the sensible adjustments are made when a field of aNodehas been changed. To modify the nodes, you can either use the node methods described in this section, or the network methods described in the next section which allow you to modify nodes in the network. Once a network and its nodes are created, it is recommended to use the network methods to modify its nodes.If you are directly working withNodeobjects, these are the methodsNodeobjects can call for various purposes with their input parameters shown in parantheses:4.3.1set_states(states)The method to update the states of theNodeobject. The node states can be defined upon creation if the node is a discrete node. If states are not specified during creation, sensible defaults will be assigned based on the node type. Withset_states()it is possible to update node states later on. If the number of new states given with this method is the same as the previous number of node states, state names will be updated. Ifset_states()changes the number of node states, node probability table size will be adjusted accordingly and probability values will reset to uniform.4.3.2add_parent(new_parent)The method to add a new parent to a node. Equivalent of adding an arc between two nodes on agena.ai Modeller. The input parameternew_parentis anotherNodeobject. Ifnew_parentis already a parent for the node, the function does not update theparentsfield of the node.When a new parent is added to a node, its NPT values and expressions are reset/resized accordingly.4.3.3remove_parent(old_parent)The method to remove one of the existing parents of a node. Equivalent of removing the arc between two nodes on agena.ai Modeller. The input parameterold_parentis aNodeobject which has already been added to theparentsfield of the node.When an existing parent is removed from a node, its NPT values and expressions are reset/resized accordingly.4.3.4set_distr_type(new_distr_type)A method to set the table type (distr_type) of a node. If aNodeissimulated, its table type can be "Expression" or "Partitioned" - the latter is only if the node has discrete parent nodes. If aNodeisnot simulated, its table type can be "Manual", "Expression", or "Partitioned (if the node has discrete parent nodes)".Changing the node's distribution type (table type) adjusts itsstates/probabilities/`expressions`` parameters accordingly.4.3.5set_probabilities(new_probs, by_rows = False)The method to set the probability values if the table type (distr_type) of aNodeis "Manual".new_probsis a list of lists containing numerical values, and the length of the input list depends on the number of the states of the node and of its parents.You can format the input list in two different orders. If the parameterby_rowsis set toTrue, the method will read the input list to fill in the NPT row by row; if set toFalse(it isFalseby default), the method will read the input list to fill in the NPT column by columnn. This behaviour is illustrated with use case examples later in this document.4.3.6set_expressions(new_expr, partition_parents = optional)The method to set the probability values if the table type (distr_type) of aNodeis "Expression" or "Partitioned". If the table type is "Expression",new_expris a list of size one andpartition_parentsis left untouched. If the table type is "Partitioned",new_expris a list of expressions for each parent state, andpartition_parentsis a list of strings for each partitioned parent node'sid. See the following sections for examples.4.3.7set_variable(variable_name, variable_value)A method to set variables (constants) for a node. Takes thevariable_nameandvariable_valueinputs which define a new variable (constant) for the node.4.3.8remove_variable(variable_name)A method to remove one of the existing variables (constants) from a node, using thevariable_name.4.4DatasetMethodsTheDatasetobjects in the models hold the observations and calculation results. The datasets have the following methods.4.4.1enter_observation(network_id, node_id, value, variable_name=optional)A method to enter an observation to a dataset. See Section4.1.10for details. Section 4.1.10 is the model method which calls this dataset method after using its parameterdataset_idto determine to which dataset the observation is entered. It is possible to use the dataset method directly to enter observations to the corresponding dataset.4.4.2remove_observation(network_id, node_id)A method to remove an observation from a dataset. See Section4.1.11for details. Section 4.1.11 is the model method which calls this dataset method after using its parameterdataset_idto determine from which dataset is the observation is removed.4.4.3clear_all_observations()A method to remove all observations from a dataset. If the model methodclear_dataset_observations(dataset_id)is used, it calls this dataset method. Not to be confused with the model methodclear_all_observations()which removes all observations from all datasets in the model.4.4.4get_result(network_id, node_id)If the dataset contains calculation results, you can access the results of a specific node in the model with this method.4.5 agena.ai Cloud Related Functionspyagena allows users to send their models to agena.ai Cloud servers for calculation. The functions around the server capabilities (including authentication) are described inSection 8.4.6 agena.ai Local API Related Functionspyagena allows users to connect to the local agena.ai developer API for calculation. The functions about the local developer API communication are descibed inSection 9.Local API related functions come with the parameterverbose(set to False by default). If you'd like to see detailed console output, you can call the functions explained inSection 9withverbose = True. Note that, for this to work, you also need to have enabled globalset_verbose(True)setting.5. Importing a Model from .cmpxTo import an existing agena.ai model (from a .cmpx file), create a newModelobject using thefrom_cmpx()option:new_model=Model.from_cmpx("/path/to/model/file.cmpx")This creates a pythonModelobject with all the information taken from the .cmpx file. All fields and sub-fields of theModelobject are accessible now. For example, you can see the networks in this model with:new_model.networksEach network in the model is aNetworkobject, therefore you can access its fields with the same logic, for example to see the id of the first network and all the nodes in the first network in the BN, use respectively:new_model.networks[0].idnew_model.networks[0].nodesAlternatively you can assign the networks to variables with the get method for further use:net=new_model.get_network(network_id)#assign desired network to a variablenet.nodes#display fields of the selected networkSimilarly, each node in a network itself is aNodeobject. You can display all the fields of a node. Example uses for the second node in the first network of a model:new_model.networks[0].nodes[0].idnew_model.networks[0].nodes[1].statesLike the networks, you can assign nodes to variables with the get method for further use:node_one=net.get_node(node_id)#assign desired node to a variablenode_one.states#display fields of the selected nodeOnce the python model is created from the imported .cmpx file, theModelobject as well as all of itsNetwork,Dataset, andNodeobjects can be manipulated using python methods.6. Creating and Modifying a Model in pythonIt is possible to create an agena.ai model entirely in python, without a .cmpx file to begin with. Once all the networks and nodes of a model are created and defined in python, you can export the model to a .cmpx or .json file to be used with agena.ai calculations and inference, locally or on agena.ai Cloud. In this section, creating a model is shown step by step.6.1 Creating a ModelYou can create an empty model and modify its networks and nodes as described below. To create a new model object:example_model=Model()which will initiate a new model object with no networks and one default empty dataset (Case 1).6.2 Creating NetworksYou can create network(s) in the model with thecreate_network()method of a model. For example:example_net=example_model.create_network(id="example_net")which will create a new network in the model with no nodes. Note that if you assign the creation method to a variable, the network variable is usable directly in addition to the model now including the new network.6.3 Creating NodesYou can create nodes in a network with thecreate_node()method of a network.It is recommended to assign the network in a model to a new variable for the ease of future operations. If not assigned upon creation, you can useget_network(), for example:example_net=example_model.get_network("example_net")Now we can use the variablenetto create and modify nodes in it:node_one=net.create_node(id="node_one")Similar no network creation, you can assign the newly created node to its own variable at the time of creation.If the optional fields are not specified, the nodes will be created with the defaults. The default values for the fields, if they are not specified, are:name = node.iddescription = "New Node"simulated = Falsetype =if simulated: "ContinuousInterval"if not simulated: "Boolean"states =if Boolean or Labelled: ["False", "True"]if Ranked: ["Low", "Medium", "High"]if DiscreteReal: ["0.0", "1.0"]Once a new node is created, depending on the type and number of states, other fields are given sensible default values too. These fields are distr_type (table type), probabilities or expressions. To specify values in these fields, you need to use the relevant set functions (described above and explained later). The default values for these fields are:distr_type =if simulated: "Expression"if not simulated: "Manual"probabilities =if distr_type is Manual: discrete uniform distribution, each state has the probability of (1/number of states)expressions =if distr_type is Expression: "Normal(0,1000000)"Look at the following new node creation examples:net.create_node(id="node_one")net.create_node(id="node_two",name="Second Node")net.create_node(id="node_three",type="Ranked")net.create_node(id="node_four",type="Ranked",states=["Very low","Low","Medium","High","Very high"])Looking up some example values in the fields that define these nodes:node_one.id = "node_one"node_one.name = "node_one"node_one.description = "New Node"node_one.type = "Boolean"node_one.states = ["False", "True"]node_two.id = "node_two"node_two.name = "Second Node"node_three.type = "Ranked"node_three.states =  ["Low", "Medium", "High"]node_four.states =  ["Very low", "Low", "Medium", "High", "Very high"]node_one.distr_type = "Manual"node_one.probabilities = [[0.5, 0.5]]node_three.probabilities = [[0.3333, 0.3333, 0.3333]]node_four.probabilities = [[0.2, 0.2, 0.2, 0.2, 0.2]]Note that probabilities will be a list (of size one) of lists even when there is no parent node.6.4 Modifying NodesTo modify nodes they can either be obtained with the network methodget_node()and assigned to a new variable, or preferably the network methods which allow the node modification can be used. For the former, as an example:node_one=net.get_node("node_one")and now you can use the variablenode_oneto modify the node in the network.To update node information, some fields can be simply overwritten with direct access to the field if it does not affect other fields. These fields are nodenameanddescription. For example:node_one.description="first node we have created"Other fields can be specified with the relevant set functions. For these, the node methods can directly be used, or preferably the network methods which allow the node modification can be used. When working on a network, it is recommended to use the network methods to modify its nodes, but below both alternatives are shown with examples.To update the node states, you can useset_states()on a node:node_one.set_states(["Negative","Positive"])orset_node_states()on a network:net.set_node_states("node_one",states=["Negative","Positive"])Note that the input parameter forset_states()orset_node_states()is a list of node names. If this method changes the number of node states, the NPT will be adjusted accordingly and state probabilities will reset to uniform.To set probability values for a node with a manual table (distr_type), you can useset_probabilities()on a node:node_one.set_probabilities([[0.2,0.8]])orset_node_probabilities()on a network:net.set_node_probabilities("node_one",probabilities=[[0.2,0.8]])Note that theset_probabilities()orset_node_probabilities()takea list of listsas input, even when the node has no parents and its NPT has only one row of probabilities. If the node has parents, the NPT will have multiple rows which should be in the input list.Assume thatnode_oneandnode_twoare the parents ofnode_three(how to add parent nodes is illustrated later in this section). Now assume that you wantnode_threeto have the following NPT:node_oneNegativePositivenode_twoFalseTrueFalseTrueLow0.10.20.30.4Medium0.40.450.60.55High0.50.350.10.05There are two ways to order the values in this table for theset_probabilities()method, using the booleanby_rowsparameter (which is set toFalseby default). If you want to enter the values following the rows in agena.ai Modeller NPT rather than ordering them by the combination of parent states (columns), you can useby_rows = Truewhere each element of the list is a row of the agena.ai Modeller NPT:net.set_node_probabilities("node_three",probabilities=[[0.1,0.2,0.3,0.4],[0.4,0.45,0.6,0.55],[0.5,0.35,0.1,0.05]],by_rows=True)If, instead, you want to define the NPT with the probabilities that add up to 1 (conditioned on the each possible combination of parent states), you can setby_rows = Falseas the following example:net.set_node_probabilities("node_three",probabilities=[[0.1,0.4,0.5],[0.2,0.45,0.35],[0.3,0.6,0.1],[0.4,0.55,0.05]],by_rows=False)Similarly, you can use the node methodset_expressions()or the network methodset_node_expressions()to define and update expressions for the nodes without Manual NPT tables. If the node has no parents, you can add a single expression:net.set_node_expressions("example_node",expressions=["TNormal(4,1,-10,10)"])orexample_node.set_expressions(["TNormal(4,1,-10,10)"])If the node has parents and the expression is partitioned on the parents:net.set_node_expressions("example_node",expressions=["Normal(90,10)","Normal(110,15)","Normal(120,30)"],partition_parents=["parent_node_id"])In the former example the expression is a list of size one, and in the latter the expression is a list with three elements and the second parameter (partition_parameters) is a list which contains the ids of the parent nodes. In the second example, expression input has three elements based on the number of states of the parent node(s) on which the expression is partitioned.6.5 Creating/Removing Edges in a Network (Adding and Removing Parent Nodes)When working with a network with nodes in it, you can add edges between nodes withcreate_edge:net.create_edge(child_id="node_three",parent_id="node_one")This method calls the node methodadd_parent()to add the parent node information to the child node. To do this directly with nodes, you can alternatively use:node_three.add_parent(node_one)This addsnode_oneto the parents list ofnode_three, and resizes the NPT ofnode_three(and resets the values to a discrete uniform distribution).To remove an already existing edge (parent-child relation) in the network, you can use:net.remove_edge(child_id="node_three",parent_id="node_one")This method calls the node methodremove_parent()on the child node. To use it directly:node_three.remove_parent(node_one)This removesnode_onefrom the parents list ofnode_three, and resizes the NPT ofnode_three(and resets the values to a discrete uniform distribution).Below we follow the steps from creation of node_three to the parent modifications and see how the NPT of node_three changes after each step.Creating node_tree with only type specified:net.create_node(id="node_three",type="Ranked")node_three=net.get_node("node_three")node_three.parents:[]node_three.probabilities:[[0.3333333333333333,0.3333333333333333,0.3333333333333333]]#discrete uniform with three states (default of Ranked node)Changing the probabilities:node_three.set_probabilities([[0.7,0.2,0.1]])node_three.probabilities:[[0.7,0.2,0.1]]Adding a parent:node_three.add_parent(node_one)#or net.create_edge("node_three", "node_one")node_three.parents:[Node:node_one(Boolean)]# node_one has been added to the parents list of node_threenode_three.probabilities:[[0.3333333333333333,0.3333333333333333,0.3333333333333333],[0.3333333333333333,0.3333333333333333,0.3333333333333333]]#  NPT of node_three has been resized based on the number of parent node_one states# NPT values for node_three are reset to discrete uniformAdding another parent:node_three.add_parent(node_two)# or net.create_edge("node_three", "node_two")node_three.parents:[Node:node_one(Boolean),Node:node_two(Boolean)]# node_two has been added to the parents list of node_threenode_three.probabilities:[[0.3333333333333333,0.3333333333333333,0.3333333333333333],[0.3333333333333333,0.3333333333333333,0.3333333333333333],[0.3333333333333333,0.3333333333333333,0.3333333333333333],[0.3333333333333333,0.3333333333333333,0.3333333333333333]]#  NPT of node_three has been resized based on the number of parent node_one and node_two states# NPT values for node_three are reset to discrete uniform6.6 Modifying the ModelSo far we initially created an "empty" model and populated it with a network and its nodes. It is possible to add multiple networks to the model:example_model.create_network(id="network_one")example_model.create_network(id="network_two")It is also possible to modify a model object regarding its network_links, datasets, and settings.Network links between networks of the model can be added with theadd_network_link()method. For example:example_model.add_network_link(source_network_id="network_one",source_node_id="node_three",target_network_id="network_two",target_node_id="node_three",link_type="Marginals")For link_type options and allowed network link rules, seeadd_network_link()in the previous section.You can remove an existing network link withremove_network_link()method:example_model.remove_network_link(source_network_id="network_one",source_node_id="node_three",target_network_id="network_two",target_node_id="node_three")or you can remove all existing network links in a model withexample_model.remove_all_network_links()When a new model is created, it comes with a single dataset (case) by default. See next section to see how to add observations to this dataset (case) or add new datasets (cases).6.7 Creating Datasets (Cases) and Entering ObservationTo enter observations to a Model (which by default has one single case), use theenter_observation()function. You need to specify the node (and the network it belongs to) and give the value (one of the states if it's a discrete node, a sensible numerical value if it's a continuous node):example_model.enter_observation(network_id=network_one,node_id=node_three,value="High")Note that this function did not specify any dataset (case). If this is the case, observation is always entered to the first (default) case.You may choose to add more datasets (cases) to the model with thecreate_dataset()function:case_two=example_model.create_dataset("Case 2")Once added, you can enter observation to the new dataset (case) if you specify thedataset_idparameter in theenter_observation()function:example_model.enter_observation(dataset_id="Case 2",network_id=network_one,node_id=node_three,value="Medium")6.8 Exporting a Model to .cmpx or .jsonOnce a python model is defined fully and it is ready for calculations, you can export it to a .cmpx or a .json file:example_model.save_to_file("example_model.cmpx")orexample_model.save_to_file("example_model.json")7. agena.ai Cloud with pyagenaOnce the Bayesian network model is prepared and modified and is ready for calculations, you can connect to either agena.ai Cloud or a local agena.ai developer API environment for calculations.pyagena allows you to authenticate with agena.ai Cloud (using your existing account) and send your model files to Cloud for calculations. The connection between your local python environment and agena.ai Cloud servers uses the python packagerequests.7.1 AuthenticationThe cloud operations use a python object calledloginto authenticate the user account, and allows user to run further calculations.To create an account, visithttps://portal.agena.ai. Once created, you can use your credentials in pyagena to access the servers.example_user=login()If you give no parameters, you will be prompted to enter your username and password for authentication. Alternatively you can pass them as parameters to login constructor:example_user=login(username,password)This will send a POST request to authentication server, and will create a logged in user instance (including access and refresh tokens) which will be used to authenticate further operations.You can choose to see either basic operation result messages or detailed debugging messages when you use the model calculation and sensitivity analysis functions after authentication. You can set the debug message option for the logged in cloud user with:example_user.set_debug(True)You can also choose to change the root of the API server URL for operations for the logged in user instance. The default for this is "https://api.agena.ai". To change this, for example:example_user.set_server_url("http://localhost:8080")If you'd like to reset the server URL to the default, you can run:example_user.reset_server_url()7.2 Model CalculationUsing the login instance created, you can do further operations such as calculations and sensitivity analysis.calculate()function is used to send a python model object to agena.ai Cloud servers for calculation. This is a method of the logged in user instance. The function takes the following parameters:modelis the python Model object(optional)dataset_idis the id of the dataset that contains the set of observations (.idof one of the.datasetsobjects) if any. If the model has only one dataset (case) with observations, dataset needs not be specified (it is also possible to send a model without any observations).Currently servers accept a single dataset (a single set of observations) for each calculation, if the python model has multiple datasets (cases), you need to specify which dataset is to be used.For example,example_user.calculate(example_model)orexample_user.calculate(example_model,dataset_id)If the calculation is successful, this function will update the python model (the relevant.resultsfield in the model's.dataset) with results of the calculation obtained from agena.ai Cloud.The model calculation computation supports asynchronous request (polling) if the computation job takes longer than 10 seconds. The python client will periodically recheck the servers and obtain the results once the computation is finished (or timed out, whichever comes first).If you would like to see the calculation results in a .csv format, you can use the Model methodexport_data()to generate the output file. SeeSection 9for examples.7.3 Sensitivity AnalysisSensitivity analysis is another cloud server operation for the logged in user instance. For the sensitivity analysis, first you need to crate a sensivity configuration object, using the.create_sensitivity_config(...)method of a Model. For example,example_sens_config=example_model.create_sensitivity_config(targetNode="node_one",sensitivityNodes=["node_two","node_three"],report_settings={"summaryStats":["mean","variance"]},dataSet="dataset_id",network="network_one")Here,targetNodeandsensitivityNodesparameters are mandatory for a sensitivity configuration and the rest is optional with sensible defaults assigned on the cloud servers.Using the defined configuration object, now you can use thesensitivity_analysis()method of the logged in user instance to send the request to the server. For example,sa_results=example_user.sensitivity_analysis(example_model,example_sens_config)If successful, this will return adictwith some information about the process and the results. Unlikecalculate(), this method is advised to assign to a variable for future analysis and use. The returneddictincludes the following fields:lastUpdatedversionloguuiddebugdurationmessagesresultsmemoryThe results contains raw results data for all analysis report options defined, such as tables, tornado graphs, and curve graphs. To access the results:sa_results.resultsYou can see the cloud API documentation for further information on the sensitivity analysis responsehere (Manual/Tools: Sensitivity Analysis/Response).The sensitivity analysis computation supports asynchronous request (polling) if the computation job takes longer than 10 seconds. The python client will periodically recheck the servers and obtain the results once the computation is finished (or timed out, whichever comes first).8. Local agena.ai API with pyagenaAgena.ai has aJava based APIto be used with agena.ai developer license. If you have the developer license, you can use the local API for calculations in addition to agena.ai modeller or cloud. The local API has Java and maven dependencies, which you can see on its github page in full detail. pyagena allows communications with the local agena developer API.Local API functions, except forlocal_api_clone, come with the optional parameterverbose, which is by default set to False. If you choose to display full maven output as a result of the functions, you can setverbose = Truein the function calls.8.1 Setting up the local API directoryTo manually set up the local agena developer API, follow the instructions on the github page for the API:https://github.com/AgenaRisk/api.Or, for the API setup you can use the python environment:local_api_clone()to clone the git repository of the API in your working directory.Once the API is cloned, you can compile maven environment with:local_api_compile()Note that for this to work you need to stay in your current working directory, you don't need to navigate into the cloned api folder. The python function will compile the api directory as long as it's a sub-directory of the current working directory (default behaviour iflocal_api_clone()is used to clone the repository.)And if needed, activate your agena.ai developer license withlocal_api_activate_license("1234-ABCD-5678-EFGH")passing on your developer license key as the input parameter.If you need to deactivate your license,local_api_deactivate_license()And if you would like to see the license information:local_api_show_license()!! Note that when there is a new version of the agena developer API, you need to re-runlocal_api_compile()function to update the local repository. Remember that this is done in the working directory which contains the /api/ folder, not in the /api/ folder !!8.2 Model calculation with the local APIOnce the local API is compiled and developer license is activated, you can use the local API directly with your models defined in python. To use the local API for calculations of a model created in python:local_api_calculate(model,dataset_ids=optional,cache_path=optional)where the parametermodelis a python Model object,dataset_idsis a list of the dataset ids in the Model that you want to calculate, andcache_pathis an optional parameter for when you want to use cache for batch case calculations (see below). If nodataset_idsis specified, the calculation will be completed for all existing datasets in the model. Even in the case where one dataset is desired to be calculated, the parameter is a list (of size one).For example,local_api_calculate(model=example_model,dataset_ids=["example_dataset_id"])This function will temporarily create the .cmpx file for the model and the separate .json file required for the dataset, and send them to the local API (cloned and compiled within the working directory), obtain the calculation result values and update the python Model object with the calculation results.To calculate multiple datasets in the same model:local_api_calculate(model=example_model,dataset_ids=["case_one","case_two"])or to calculate all the datasets in the model, simply:local_api_calculate(model=exmple_model)Local API calculation also supports the use of a cached result file. This option is useful when you want to be able to resume an interrupted batch calculation, as every dataset will be written to file as soon as it is calculated. This requires an existing output json file to be specified in thecache_pathparameter, if calculation results for certain datasets already are in this file, they will not be recalculated, and the datasets given indataset_idswithout results in the existing output file will be calculated.To use a cached output file:local_api_calculate(model=example_model,dataset_ids=["case_three","case_four"],cache_path="C:/path/to/file/model_output.json")If you would like to see the calculation results in a .csv format, you can use the Model methodexport_data()to generate the output file as described inSection 9.8.4 Sensitivity Analysis with the local APIYou can also run a sensitivity analysis in the local API, usinglocal_api_sensitivity_analysis(model,sens_config)Here the sens_config is created by the use of.create_sensitivity_config(...)method of a Model. For example:example_sens_config=example_model.create_sensitivity_config(targetNode="node_one",sensitivityNodes=["node_two","node_three"],report_settings={"summaryStats":["mean","variance"]},dataSet="dataset_id",network="network_one")Note that, unlikelocal_api_calculate(), this function does not update the existing Model object but returns a newdictof results. So it's a good practice to assign the function to a variable for future analysis and use.sa_results=local_api_sensitivity_analysis(model=example_model,sens_config=example_sens_config)This function will temporarily create the .cmpx file for the model and the separate .json files required for the dataset and sensitivity analysis configuration file, and send them to the local API (cloned and compiled within the working directory), obtain the sensitivity analysis result values and create the resultsdict. The returneddictincludes the result values for displays such as sensitivity tables, tornado graphs, and curves.local_api_sensitivity_analysis()looks at thedataSetfield ofsens_configto determine which dataset to use, if the field doesn't exist, the default behaviour is to create a new dataset without any observations for the sensitivity analysis.9 Importing and Exporting Datasets for Batch CalculationIt is possible to use pyagena for batch import/export of data and to use the local agena.ai API for batch operations.9.1 Importing DataSection 8.2already explained that you can calculate multiple datasets at once with the local API. The python model can have multiple datasets created in pyagena or parsed from an input .cmpx model with the datasets. Additionally, for an existing python model, you can useimport_data(filename)to import multiple datasets and observations in batch from an input file. The input file can be either a .csv or a .json.9.1.1 From a .csv FileIf the input file is a .csv, it should consist of columns representing the nodes in the networks of the model and each row in the file is a new dataset.In this file, each row of the data is a single case (dataset) with a set of observed values for nodes in the model. First column of the CSV file is the dataset (case) ids which will be used to create a new dataset for each data row. All other columns are possible evidence variables whose headers follow the "network_id.node_id" format. Thus, each column represents a node in the BN and is defined by the node id and the id of the network to which it belongs. The column header for the first one (dataset names) can be anything, 'Case' is the header name whencreate_csv_template()is used.An example CSV format is as below:Casenetwork_one.node_onenetwork_one.node_twonetwork_one.cont_nodenetwork_two.node_onenetwork_two.node_twoCase 1NegativeTrue20NegativeFalseCase 2PositiveTrueNegativeTrueNew CasePositiveFalse18PositiveOnce the model is defined in pyagena and the .csv file with the observations is prepared, you can useimport_data(filename)to generate scenarios for the BN:example_model.import_data(filename="./example_data.csv")This will look up all the rows in the .csv file and create new datasets (cases) in the model for each row whose id does not already exist in the model datasets. If the row id already is the id of an existing dataset, the existing dataset will be updated with the information in the input file. Once the model has a dataset for each row of the input file, all the values of the nodes will be entered as observations to their datasets.Missing values must be blank cells in the dataset. If there are missing values in the dataset, it will not fill in any observation for that specific node in that specific dataset (case).You can usecreate_csv_template()function on a Model object to create an empty .csv file with the correct format for all the nodes and networks in the model. You can then delete the columns for the nodes that are not observed.9.1.2 From a .json FileThe methodimport_data(filename)also accepts .json file inputs. In this case the .json file should have a list of datasets to be added to the model in the correct format regarding the field names and values. For example:[{"id":"dataset_one","observations":[{"network":"net_one","node":"node_one","entries":[{"weight":1,"value":"Low"}]},{"network":"net_one","node":"node_two","entries":[{"weight":1,"value":5.0}]}]}]The input file can be used with the import method:example_model.import_data(filename="./example_data.json")This will look up the ids of all the datasets in the .json input, create new datasets in the model for those whose id does not already exist in the model. If the id of any dataset in the input file already exists in the model, that dataset will be updated with the information from the input file. The observations for all the datasets will be imported to the model. If the .json file contains results for any dataset, the results are imported too.9.2 Exporting Data to a .csv FileIt is also possible to export model data to either a .csv or a .json file. The methodexport_data(...)allows the export of (calculated) results as well as observations in the datasets.9.2.1 To a .csv FileIf the output of theexport_data(...)is a .csv file, it can either export observations (inputs) or the results (outputs) of a model dataset.To export calculation results of all the datasets in a model to a .csv file:example_model.export_data(filename="./example_output.csv",include_inputs=False,include_outputs=True)This will generate a .csv file with the following format:CaseNetworkNodeStateProbabilityCase 1Network 1Node 1State 10.2Case 1Network 1Node 1State 20.3Case 1Network 1Node 1State 30.5Case 1Network 1Node 2State 10.3Case 1Network 1Node 2State 20.7Case 1Network 1Node 3State 10.1Case 1Network 1Node 3State 20.8Case 1Network 1Node 3State 30.1You can see the posterior probabilities for all the nodes calculated in the model. If you choose to export certain datasets, you can specify:example_model.export_data(filename="./example_output.csv",dataset_ids=["Case 1","Case 2"],include_inputs=False,include_outputs=True)If you choose to export only observations (inputs) and not the state probabilities of the calculated nodes:example_model.export_data(filename="./example_output.csv",include_inputs=True,include_outputs=False)Let's assume the Case 1 has the following three observations:Node 1: observed State 2Node 2: observed State 1Node 3: soft evidence p(State 2)=0.5, p(State 3)=0.5The export method will generate a .csv with the following format:CaseNetworkNodeStateProbabilityCase 1Network 1Node 1State 21Case 1Network 1Node 2State 11Case 1Network 1Node 3State 20.5Case 1Network 1Node 3State 30.5If you're planning to use Microsoft Excel to display and work with the generated .csv file, it is recommended to set the parameterexcel_compatibilitytoTruein theexport_data()function. This makes sure the state names are displayed correctly as text in the Excel environment: if for some nodes in the networks the state name includes a "-" before or between values (for example, "-Infinity - 7.0" or "40.0 - 45.0"), some spreadsheet apps like MS Excel misinterpret this as a formula instead of text,excel_compatible = Truesends the state names in quotes for MS Excel to interpret as text (this behaviour might add extra characters to state names if  the resulting csv is opened with other spreadsheet/text editing apps, in that case setting theexcel_compatibility = False, which is the default behaviour, is required).9.2.2 To a .json FileIf the export output is a .json file, then it will contain a list of datasets with all their fields (id, observations, results). .json output can have either inputs, outputs, or both at the same time.For example,example_model.export_data(filename="./example_output.json",include_inputs=True,include_outputs=True)will list all the datasets in the model in the exported .json file such as:[{"id":"dataset_one","observations":[{"network":"net_one","node":"node_one","entries":[{"weight":1,"value":"Low"}]},{"network":"net_one","node":"node_two","entries":[{"weight":1,"value":5.0}]}],"results":[{"network":"net_one","node":"node_four","resultValues":[{"label":"Low","value":0.2},{"label":"High","value":0.8}],"summaryStatistics":{"confidenceInterval":50,"entropy":0,"percentile":0.5,}}]}]If inputs are not included,observationsfield is blank. If outputs are not included,resultsfield is blank.10. pyagena Use Case ExamplesIn this section, some use case examples of pyagena environment are shown.10.1 Asia BN ModelThis is a BN which calculates the risk of certain medical conditions such as tuberculosis, lung cancer, and bronchitis from two casual factors - smoking and whether the patient has been to Asia recently. Additionally two other pieces of evidence are available: whether the patient is suffering from dyspnoea (shortness of breath) and whether a positive or negative X-ray test result is available.We can start with creating the model:asia=Model()Now create a network in this model (and assign it to a variable for ease of further operations):asia.create_network(id="asia_net")net=asia.get_network("asia_net")Now we can add the nodes to the network:net.create_node(id="A",name="Visit to Asia?")net.create_node(id="S",name="Smoker?")net.create_node(id="T",name="Has tuberculosis")net.create_node(id="L",name="Has lung cancer")net.create_node(id="B",name="Has bronchitis")net.create_node(id="TBoC",name="Tuberculosis or cancer")net.create_node(id="X",name="Positive X-ray?")net.create_node(id="D",name="Dyspnoea?")All the nodes are binary so we do not need to specify the type or states. Then we can add the edges to the network between nodes (adding relevant nodes as parents to the child nodes):net.create_edge(child_id="T",parent_id="A")net.create_edge(child_id="L",parent_id="S")net.create_edge(child_id="B",parent_id="S")net.create_edge(child_id="TBoC",parent_id="T")net.create_edge(child_id="TBoC",parent_id="L")net.create_edge(child_id="X",parent_id="TBoC")net.create_edge(child_id="D",parent_id="TBoC")net.create_edge(child_id="D",parent_id="B")At the moment all nodes have default uniform NPTs, we can define the NPT values:net.set_node_probabilities(node_id="A",new_probs=[[0.99,0.01]])net.set_node_probabilities(node_id="T",new_probs=[[0.99,0.01],[0.95,0.05]])net.set_node_probabilities(node_id="L",new_probs=[[0.9,0.1],[0.99,0.01]])net.set_node_probabilities(node_id="B",new_probs=[[0.7,0.3],[0.4,0.6]])net.set_node_probabilities(node_id="TBoC",new_probs=[[1,0],[0,1],[0,1],[0,1]])net.set_node_probabilities(node_id="X",new_probs=[[0.95,0.05],[0.02,0.98]])net.set_node_probabilities(node_id="D",new_probs=[[0.9,0.1],[0.2,0.8],[0.3,0.7],[0.1,0.9]])Now we can choose to use the model in any possible way: exporting to a .cmpx file for agena.ai modeller, sending it to agena.ai cloud, or sending it to the local agena.ai developer API for calculations. For example:asia.save_to_file("asia_bn.json")10.2 Diet Experiment ModelThis is a BN which uses experiment observations to estimate the parameters of a distribution. In the model structure, there are nodes for the parameters which are the underlying parameters for all the experiments and the observed values inform us about the values for these parameters. The model in agena.ai Modeller is given below:In this section we will create this model entirely in pyagena. We can start with creating an empty model and create the network in it:diet=Model()diet.create_network("Hierarchical_Normal_Model_1")net=diet.get_network("Hierarchical_Normal_Model_1")Now we can add nodes to the network. Let's start with mean and variance nodes:#Creating mean and variance nodesnet.create_node(id="mean",simulated=True)net.set_node_expressions(node_id="mean",expressions=["Normal(0.0,100000.0)"])net.create_node(id="variance",simulated=True)net.set_node_expressions(node_id="variance",expressions=["Uniform(0.0,50.0)"])Common variance and tau nodes:#Now we create the "common variance" and its "tau" parameter nodesnet.create_node(id="tau",simulated=True)net.set_node_expressions(node_id="tau",expressions=["Gamma(0.001,1000.0)"])net.create_node(id="common_var",name="common variance",simulated=True)net.create_edge(child_id="common_var",parent_id="tau")net.set_node_expressions(node_id="common_var",expressions=["Arithmetic(1.0/tau)"])Now we can create the four mean nodes, using a for loop and list ofNodes:#Creating a list of four mean nodes, "mean A", "mean B", "mean C", and "mean D"mean_names=["A","B","C","D"]means_list=[]formninmean_names:this_id="mean"+mnthis_name="mean "+mnnet.create_node(id=this_id,name=this_name)net.create_edge(child_id=this_id,parent_id="mean")net.create_edge(child_id=this_id,parent_id="variance")net.set_node_expressions(node_id=this_id,expressions=["Normal(mean,variance)"])means_list.append(this_id)Now we can create the experiment nodes, based on the number of observations which will be entered:# Defining the list of observations for the experiment nodes# and creating the experiment nodes y11, y12, ..., y47, y48observations=[[62,60,63,59],[63,67,71,64,65,66],[68,66,71,67,68,68],[56,62,60,61,63,64,63,59]]fori,(obs,mn)inenumerate(zip(observations,means_list)):forj,obinenumerate(obs):this_id="y"+str(i)+str(j)net.create_node(id=this_id,simulated=True)net.create_edge(child_id=this_id,parent_id="common_var")net.create_edge(child_id=this_id,parent_id=mn)net.set_node_expressions(node_id=this_id,expressions=["Normal("+mn+",common_var)"])We enter all the observation values to the nodes:# Entering all the observationsfori,obsinenumerate(observations):forj,obinenumerate(obs):this_node_id="y"+str(i)+str(j)diet.enter_observation(network_id=net.id,node_id=this_node_id,value=ob)Now the model is ready with all the information, we can export it to either a .json or a .cmpx file for agena.ai Modeller calculations, send it to agena.ai Cloud or to local agena.ai developer API.# To create a local .cmpx filediet_model.save_to_file("./diet_model_example.cmpx")# Or sending it to local agena.ai developer API for calculationlocal_api_calculate(diet_model,"Case 1")# Or sending it to agena.ai Cloud for calculationuser=login()user.calculate(diet_model,"Case 1")