Enable diagnosis for nodes using pysmile wrapper

The engine.
Post Reply
hemasadhasivam
Posts: 6
Joined: Mon Apr 20, 2020 8:53 am

Enable diagnosis for nodes using pysmile wrapper

Post by hemasadhasivam »

Hello
I am using PySmile wrapper to generate a network.

1. I want to enable diagnosis for the created nodes through code.
I checked the various methods and found the below snippet relevant to what I am looking for.

Code: Select all

net = PySmile.Network()
net.set_node_diag_type(handle, int)
But the input arguments accepted by the "set_node_diag_type" method are not very clear and I couldn't find information related to the diagnostic role of a node in the documentation. What does the second 'int' parameter indicate?
It would be very helpful if you could provide some information.

Additional note: I want some of the nodes in my network to be of target type and the others to be observation type and that is the reason why I am looking to use the set_node_diag_type method.

2. Is it possible to change the font type or size through PySmile wrapper?

3. Are there methods to convert the graph to a bar view or an icon view?

Thanks & Regards,
Hema S
shooltz[BayesFusion]
Site Admin
Posts: 1417
Joined: Mon Nov 26, 2007 5:51 pm

Re: Enable diagnosis for nodes using pysmile wrapper

Post by shooltz[BayesFusion] »

2. Is it possible to change the font type or size through PySmile wrapper?
3. Are there methods to convert the graph to a bar view or an icon view?
Unfortunately, this functionality is not available from PySMILE.

We will write a sample Python program using PySMILE's diagnostic functionality and post it here.
piotr [BayesFusion]
Site Admin
Posts: 60
Joined: Mon Nov 06, 2017 6:41 pm

Re: Enable diagnosis for nodes using pysmile wrapper

Post by piotr [BayesFusion] »

First argument of set_node_diag_type method is node handle or node ID. Second argument represents diagnostic type. By default, all nodes are Auxiliary.

You can change diagnostic type by calling e.g.

Code: Select all

net.set_node_diag_type("Fault Node ID", pysmile.NodeDiagType.FAULT)
net.set_node_diag_type("First Observation Node", pysmile.NodeDiagType.OBSERVATION)
net.set_node_diag_type("Second Observation Node", pysmile.NodeDiagType.OBSERVATION)
Besides of that, at least one state of fault node must be marked as "fault outcome".

Code: Select all

net.set_fault_outcome("Fault Node ID", "Fault Outcome", True)
Ranked observations could be setted with method net.set_ranked:

Code: Select all

net.set_ranked("First Observation Node", True)
Then, you can run diagnosis with creating DiagNetwork object and running update method.

Code: Select all

diag_net = pysmile.DiagNetwork(net)
diag_result = diag_net.update()
hemasadhasivam
Posts: 6
Joined: Mon Apr 20, 2020 8:53 am

Re: Enable diagnosis for nodes using pysmile wrapper

Post by hemasadhasivam »

Thank you for the update. It's very helpful.
katelynmorrison
Posts: 1
Joined: Wed Sep 06, 2023 1:56 pm

Re: Enable diagnosis for nodes using pysmile wrapper

Post by katelynmorrison »

Hello, I am trying to get the diagnostic values from my model using the pysmile wrapper. But currently, I am struggling on figuring out how to do this.

Based on the answers below, I have generated this code so far:

Code: Select all

net = pysmile.Network()
net.read_file("Survival.xdsl")

for i in range(len(current_features)): 
    net.set_evidence(current_features[i], mapped_values[current_features[i]])
    
net.update_beliefs()
beliefs = net.get_node_value("SURVIVAL_12")

# Iterate through the outcomes
for i in range(0, len(beliefs)):
     outcome = net.get_outcome_id("SURVIVAL_12", i)
     probability = beliefs[i]
     print(f"{outcome} = {probability}")

net.set_node_diag_type("SURVIVAL_12", pysmile.NodeDiagType.FAULT)
net.set_fault_outcome("SURVIVAL_12", "Yes", True)

for i in range(len(current_features)):
     net.set_node_diag_type(current_features[i], pysmile.NodeDiagType.OBSERVATION)

diag_net = pysmile.DiagNetwork(net)
diag_result = diag_net.update()

diagnostic_values = diag_result.get_node_value("SURVIVAL_12")

for i, outcome in enumerate(diag_net.get_outcome_ids("SURVIVAL_12")):
     value = diagnostic_values[i]
     print(f"{outcome}: {value}")
Running this code, I get the following output:

Code: Select all

No = 0.0028522731146181338
Yes = 0.9971477268853817
Traceback (most recent call last):
  File "pysmiletest.py", line 151, in <module>
    test()
  File "pysmiletest.py", line 144, in test
    diagnostic_values = diag_result.get_node_value("SURVIVAL_12")
AttributeError: 'pysmile.DiagResults' object has no attribute 'get_node_value'
Any suggestions on how I can access the diag_result and/or if I am doing something wrong in the code here?

The current features in the model I have include several health-related variables such as heart rate, gender, age, blood pressure, etc., and these variables all predict Survival.

Any insight and help would be appreciated!
piotr [BayesFusion]
Site Admin
Posts: 60
Joined: Mon Nov 06, 2017 6:41 pm

Re: Enable diagnosis for nodes using pysmile wrapper

Post by piotr [BayesFusion] »

When you operate on the DiagNetwork object, you should use the instantiate_observation method, instead of set_evidence. After creating a DiagNetwork object based on Network, add a loop similar to the one you pasted at the beginning of the code snippet (changing net.set_evidence to diag_net.instantiate_observation)

Code: Select all

diag_net = pysmile.DiagNetwork(net)
for i in range(len(current_features)): 
    diag_net.instantiate_observation(current_features[i], mapped_values[current_features[i]])
diag_result = diag_net.update()
The DiagResults object consists of two lists - observations (list of objects of type ObservationInfo) and faults (list of objects of type FaultInfo). You can refer to them as follows:

Code: Select all

diag_result.faults
diag_result.observations
You can get more information about these objects using the help command in the Python interpreter e.g.

Code: Select all

help(pysmile.DiagResults) 
help(pysmile.ObservationInfo)
help(pysmile.FaultInfo)
Post Reply