Commit da2a0637 authored by Jan Pöppel's avatar Jan Pöppel
Browse files

dbn specs can now specify networks either relative or absolute

parent b0b2f2a1
......@@ -180,9 +180,16 @@ class DBNSpec(object):
with open(filename) as json_data:
spec = json.load(json_data)
basepath = os.path.dirname(os.path.abspath(filename))
b0 = XMLBIFParser.parse(basepath + os.path.sep + spec['B0'])
two_tbn = XMLBIFParser.parse(basepath + os.path.sep + spec['TBN'])
if os.path.isabs(spec['B0']):
b0 = XMLBIFParser.parse(spec['B0'])
else:
b0 = XMLBIFParser.parse(basepath + os.path.sep + spec['B0'])
if os.path.isabs(spec['TBN']):
two_tbn = XMLBIFParser.parse(spec['TBN'])
else:
two_tbn = XMLBIFParser.parse(basepath + os.path.sep + spec['TBN'])
dbn = networks.DynamicBayesianNetwork(b0, two_tbn)
for transition in spec['transitions']:
dbn.add_transition(transition[0], transition[1])
......
......@@ -203,6 +203,26 @@ class DBNSpecTest(unittest.TestCase):
self.assertEqual(dbn._b0.name, "Test_DBN_B0")
self.assertEqual(dbn._two_tbn.name, "Test_DBN_2TBN")
os.chdir("../..")
def test_parseDBN_absolute_path(self):
import shutil
if os.path.exists("/tmp"):
shutil.copyfile("primo2/tests/dbn-test-b0.xbif", "/tmp/dbn-test-b0.xbif")
shutil.copyfile("primo2/tests/dbn-test-2tbn.xbif", "/tmp/dbn-test-2tbn.xbif")
dbn = DBNSpec.parse("primo2/tests/dbn-test-abs.conf")
self.assertEqual(dbn._b0.name, "Test_DBN_B0")
self.assertEqual(dbn._two_tbn.name, "Test_DBN_2TBN")
os.remove("/tmp/dbn-test-b0.xbif")
os.remove("/tmp/dbn-test-2tbn.xbif")
def test_parseDBN_mixed_path(self):
import shutil
if os.path.exists("/tmp"):
shutil.copyfile("primo2/tests/dbn-test-b0.xbif", "/tmp/dbn-test-b0.xbif")
dbn = DBNSpec.parse("primo2/tests/dbn-test-mixed.conf")
self.assertEqual(dbn._b0.name, "Test_DBN_B0")
self.assertEqual(dbn._two_tbn.name, "Test_DBN_2TBN")
os.remove("/tmp/dbn-test-b0.xbif")
if __name__ == "__main__":
#Workaround so that this script also finds the resource files when run directly
......
<?xml version="1.0" encoding="US-ASCII"?>
<!--
Bayesian network in XMLBIF v0.3 (BayesNet Interchange Format)
Produced by SamIam http://reasoning.cs.ucla.edu/samiam
Output created Feb 13, 2017 4:29:38 PM
-->
<BIF VERSION="0.3">
<NETWORK>
<NAME>Test_DBN_2TBN</NAME>
<VARIABLE TYPE="nature">
<NAME>A</NAME>
<OUTCOME>True</OUTCOME>
<OUTCOME>False</OUTCOME>
<PROPERTY>position = (300, 0)</PROPERTY>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>B</NAME>
<OUTCOME>True</OUTCOME>
<OUTCOME>False</OUTCOME>
<PROPERTY>position = (400, 0)</PROPERTY>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>B_t0</NAME>
<OUTCOME>True</OUTCOME>
<OUTCOME>False</OUTCOME>
<PROPERTY>position = (400, 0)</PROPERTY>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>C</NAME>
<OUTCOME>True</OUTCOME>
<OUTCOME>False</OUTCOME>
<PROPERTY>position = (300, 0)</PROPERTY>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>C_t0</NAME>
<OUTCOME>True</OUTCOME>
<OUTCOME>False</OUTCOME>
<PROPERTY>position = (300, 0)</PROPERTY>
</VARIABLE>
<DEFINITION>
<FOR>A</FOR>
<GIVEN>B_t0</GIVEN>
<GIVEN>C_t0</GIVEN>
<TABLE>0.8 0.2 0.6 0.4 0.65 0.25 0.5 0.5 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>B</FOR>
<GIVEN>B_t0</GIVEN>
<GIVEN>A</GIVEN>
<TABLE>0.2 0.8 0.3 0.7 0.4 0.6 0.75 0.25</TABLE>
</DEFINITION>
<DEFINITION>
<FOR>B_t0</FOR>
<TABLE>0.5 0.5 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>C</FOR>
<GIVEN>C_t0</GIVEN>
<TABLE>0.7 0.3 0.45 0.55</TABLE>
</DEFINITION>
<DEFINITION>
<FOR>C_t0</FOR>
<TABLE>0.1 0.9 </TABLE>
</DEFINITION>
</NETWORK>
</BIF>
{
"B0": "/tmp/dbn-test-b0.xbif",
"TBN": "/tmp/dbn-test-2tbn.xbif",
"transitions": [
["C_t0", "C"],
["B_t0", "B"]
]
}
<?xml version="1.0" encoding="US-ASCII"?>
<!--
Bayesian network in XMLBIF v0.3 (BayesNet Interchange Format)
Produced by SamIam http://reasoning.cs.ucla.edu/samiam
Output created Feb 13, 2017 4:29:38 PM
-->
<BIF VERSION="0.3">
<NETWORK>
<NAME>Test_DBN_B0</NAME>
<VARIABLE TYPE="nature">
<NAME>A</NAME>
<OUTCOME>True</OUTCOME>
<OUTCOME>False</OUTCOME>
<PROPERTY>position = (300, 0)</PROPERTY>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>B</NAME>
<OUTCOME>True</OUTCOME>
<OUTCOME>False</OUTCOME>
<PROPERTY>position = (400, 0)</PROPERTY>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>C</NAME>
<OUTCOME>True</OUTCOME>
<OUTCOME>False</OUTCOME>
<PROPERTY>position = (300, 0)</PROPERTY>
</VARIABLE>
<DEFINITION>
<FOR>B</FOR>
<TABLE>0.5 0.5 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>C</FOR>
<TABLE>0.2 0.8 </TABLE>
</DEFINITION>
</NETWORK>
</BIF>
{
"B0": "/tmp/dbn-test-b0.xbif",
"TBN": "dbn-test-2tbn.xbif",
"transitions": [
["C_t0", "C"],
["B_t0", "B"]
]
}
{
"B0": "dbn-test-b0.xbif",
"TBN": "dbn-test-2tbn.xbif",
"transitions": [
["C_t0", "C"],
["B_t0", "B"]
]
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment