Export to BIF/XBIF Graphs/Bayesian Network
Pablomoreiragarcia opened this issue · 15 comments
Hi, I'm using the lib edu.cmu.tetrad on a project, and I did not find a way/function to export the graphs, is there any way to export them on the lib or I must try to do a function myself?
A couple of questions.
- Do you mean just the graphs, or do you mean the whole parameterized Bayes net?
- Are you working in Python? R? Java?
Im working on Java, and both the graphs and the bayes net
Hmm...oh man I used to have BIF format but I think I deleted it... here's the format I have currently for saving Bayes nets to an XML format--is this useful? Could you transform this into BIF/XBIF?
<?xml version="1.0" encoding="UTF-8"?>
<bayesNet>
<bnVariables>
<discreteVariable name="X1" index="0" x="200" y="60">
<category name="0" index="0"/>
<category name="1" index="1"/>
<category name="2" index="2"/>
</discreteVariable>
<discreteVariable name="X2" index="1" x="271" y="77">
<category name="0" index="0"/>
<category name="1" index="1"/>
<category name="2" index="2"/>
</discreteVariable>
<discreteVariable name="X3" index="2" x="323" y="147">
<category name="0" index="0"/>
<category name="1" index="1"/>
<category name="2" index="2"/>
</discreteVariable>
<discreteVariable name="X4" index="3" x="323" y="232">
<category name="0" index="0"/>
<category name="1" index="1"/>
<category name="2" index="2"/>
</discreteVariable>
<discreteVariable name="X5" index="4" x="272" y="302">
<category name="0" index="0"/>
<category name="1" index="1"/>
<category name="2" index="2"/>
</discreteVariable>
<discreteVariable name="X6" index="5" x="190" y="329">
<category name="0" index="0"/>
<category name="1" index="1"/>
<category name="2" index="2"/>
</discreteVariable>
<discreteVariable name="X7" index="6" x="109" y="303">
<category name="0" index="0"/>
<category name="1" index="1"/>
<category name="2" index="2"/>
</discreteVariable>
<discreteVariable name="X8" index="7" x="58" y="233">
<category name="0" index="0"/>
<category name="1" index="1"/>
<category name="2" index="2"/>
</discreteVariable>
<discreteVariable name="X9" index="8" x="57" y="148">
<category name="0" index="0"/>
<category name="1" index="1"/>
<category name="2" index="2"/>
</discreteVariable>
<discreteVariable name="X10" index="9" x="108" y="78">
<category name="0" index="0"/>
<category name="1" index="1"/>
<category name="2" index="2"/>
</discreteVariable>
</bnVariables>
<parents>
<parentsFor name="X1"/>
<parentsFor name="X2">
<parent name="X1" index="0"/>
</parentsFor>
<parentsFor name="X3">
<parent name="X1" index="0"/>
</parentsFor>
<parentsFor name="X4"/>
<parentsFor name="X5">
<parent name="X3" index="0"/>
</parentsFor>
<parentsFor name="X6"/>
<parentsFor name="X7">
<parent name="X3" index="0"/>
</parentsFor>
<parentsFor name="X8">
<parent name="X1" index="0"/>
<parent name="X3" index="1"/>
<parent name="X6" index="2"/>
</parentsFor>
<parentsFor name="X9">
<parent name="X6" index="0"/>
</parentsFor>
<parentsFor name="X10">
<parent name="X5" index="0"/>
<parent name="X7" index="1"/>
</parentsFor>
</parents>
<cpts rowSumTolerance="0.0001">
<cpt variable="X1" numRows="1" numCols="3">
<row>0.565 0.091 0.344</row>
</cpt>
<cpt variable="X2" numRows="3" numCols="3">
<row>0.284 0.636 0.080</row>
<row>0.134 0.022 0.844</row>
<row>0.191 0.204 0.605</row>
</cpt>
<cpt variable="X3" numRows="3" numCols="3">
<row>0.659 0.197 0.144</row>
<row>0.066 0.573 0.362</row>
<row>0.465 0.031 0.503</row>
</cpt>
<cpt variable="X4" numRows="1" numCols="3">
<row>0.390 0.033 0.577</row>
</cpt>
<cpt variable="X5" numRows="3" numCols="3">
<row>0.049 0.488 0.463</row>
<row>0.034 0.816 0.151</row>
<row>0.138 0.078 0.784</row>
</cpt>
<cpt variable="X6" numRows="1" numCols="3">
<row>0.037 0.759 0.204</row>
</cpt>
<cpt variable="X7" numRows="3" numCols="3">
<row>0.773 0.010 0.218</row>
<row>0.353 0.202 0.445</row>
<row>0.092 0.328 0.580</row>
</cpt>
<cpt variable="X8" numRows="27" numCols="3">
<row>0.008 0.871 0.121</row>
<row>0.660 0.024 0.317</row>
<row>0.102 0.539 0.360</row>
<row>0.222 0.100 0.678</row>
<row>0.696 0.263 0.041</row>
<row>0.151 0.365 0.484</row>
<row>0.089 0.180 0.731</row>
<row>0.327 0.595 0.077</row>
<row>0.458 0.161 0.381</row>
<row>0.028 0.325 0.646</row>
<row>0.822 0.030 0.148</row>
<row>0.270 0.376 0.355</row>
<row>0.567 0.036 0.397</row>
<row>0.308 0.394 0.297</row>
<row>0.152 0.843 0.005</row>
<row>0.496 0.166 0.338</row>
<row>0.085 0.019 0.896</row>
<row>0.495 0.472 0.033</row>
<row>0.011 0.587 0.402</row>
<row>0.788 0.210 0.003</row>
<row>0.213 0.559 0.228</row>
<row>0.707 0.169 0.124</row>
<row>0.429 0.300 0.271</row>
<row>0.490 0.142 0.368</row>
<row>0.505 0.491 0.003</row>
<row>0.106 0.642 0.251</row>
<row>0.289 0.647 0.064</row>
</cpt>
<cpt variable="X9" numRows="3" numCols="3">
<row>0.493 0.027 0.480</row>
<row>0.101 0.787 0.112</row>
<row>0.522 0.352 0.126</row>
</cpt>
<cpt variable="X10" numRows="9" numCols="3">
<row>0.323 0.268 0.409</row>
<row>0.385 0.145 0.471</row>
<row>0.488 0.029 0.483</row>
<row>0.358 0.036 0.606</row>
<row>0.574 0.363 0.062</row>
<row>0.255 0.457 0.288</row>
<row>0.003 0.088 0.909</row>
<row>0.023 0.728 0.249</row>
<row>0.305 0.078 0.617</row>
</cpt>
</cpts>
</bayesNet>
This matches the internal format of Tetrad's Bayes nets. I think the issue was when I was working with BIF before, I found it insufficiently informative for Tetrad, so I got rid of it. Maybe XBIF fixes the issues, though.
Could you give me a reference for the XBIF format? I was having trouble finding it...
Maybe I could add that...
Oh OK, I found the BNLEARN page for BIF examples:
https://www.bnlearn.com/bnrepository/discrete-small.html#asia
This format is somewhat more developed than when I last looked, admittedly some two decades ago. We can support this format. We were planning to release a new version of Tetrad for other reasons early next week, so I don't think I could get it in by then, but soon after that, I think, maybe by early November.
It's not much different from where the current Tetrad format ended up, above. Just basically some relabeling.
I can get back to you on this.
I still can't find anything on XBIF, so if you can forward me information on that, I'll consider it.
Hello and thank you so much, sorry for answering so late, here you can see a paper where they are using XBIF format for the bayesian nets (http://simd.albacete.org/supplements/FastCHC.html)
Hello again, i did something like that
`
public void exportToBIF(String filename) {
try (FileWriter writer = new FileWriter(filename)) {
// File header
writer.write("\n");
writer.write("\n");
// Write the BIF header
writer.write("<BIF VERSION="0.3">\n");
writer.write("\n");
writer.write(" bayesiannetwork\n");
// Write the variables
for (Node node : this.finalGraph.getNodes()) {
writer.write(" <VARIABLE TYPE=\"nature\">\n");
writer.write(" <NAME>" + node.getName() + "</NAME>\n");
//for (Object out : node.getAllAttributes()){
// writer.write(" <OUTCOME>" + out + "</OUTCOME>\n");
//}
writer.write(" </VARIABLE>\n");
writer.write("\n");
}
// Write the variables definition
for (Node node : this.finalGraph.getNodes()) {
writer.write(" <DEFINITION>\n");
writer.write(" <FOR>" + node.getName() +"</FOR>\n");
if (this.finalGraph.getParents(node).isEmpty()){
writer.write(" <TABLE>" + "XXXXXXXXXXXXXXXXXXX" +"</TABLE>\n");
}
else {
for (Node node_father : this.finalGraph.getParents(node)) {
writer.write(" <GIVEN>" + node_father.getName() + "</GIVEN>\n");
}
writer.write(" <TABLE>" + "XXXXXXXXXXXXXXXXXXX" + "</TABLE>\n");
}
writer.write(" </DEFINITION>\n");
writer.write("\n");
}
writer.write("</NETWORK>\n");
writer.write("</BIF>\n");
writer.write("\n");
} catch (IOException e) {
e.printStackTrace();
}
}
`
I have yet to complete the probability distributions, also another question, how can i do the Parameter learning?
No problem, here there are more information about that Xml BIF format.
https://www.cs.cmu.edu/afs/cs/user/fgcozman/www/Research/InterchangeFormat/
No im coding on my own project that use tetrad to generate the BNs.
@Pablomoreiragarcia Sorry for the delay, but I added a parser/renderer for the BIF format to my branch on GitHub. It should be available the next time we publish Tetrad (which should be soon).
@Pablomoreiragarcia I am parsing/rendering discrete Bayes nets in the BIF format given on this page:
https://www.bnlearn.com/bnrepository/
Let me know if that's not sufficient.
This is done!