/
likelihood_gobnilp.py
36 lines (30 loc) · 1.16 KB
/
likelihood_gobnilp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/python
# Read a dataset file, a gobnilp adjacency matrix file and print the likelihood of the dataset.
import sys
import numpy
import networkx
from subprocess import call
from Dataset import Dataset
from data_likelihood import compute_likelihood
def GetGraphFromGobnilpFile(gobnilpResult):
matrix = "";
with open(gobnilpResult, "r") as gobnilpResultFile:
for line in gobnilpResultFile:
matrix += line.strip('\n') + "; ";
matrix = matrix[:-2];
return networkx.from_numpy_matrix(numpy.matrix(matrix), create_using=networkx.DiGraph());
def GetListOfParents(gobnilpResult):
gobnilpGraph = GetGraphFromGobnilpFile(gobnilpResult);
return [gobnilpGraph.predecessors(node) for node in gobnilpGraph.nodes()];
def Main(datasetFile, matrixFile, ess):
dataset = Dataset();
dataset.ParseFile(datasetFile);
print GetListOfParents(matrixFile);
return compute_likelihood(dataset.data, dataset.variablesCardinality, GetListOfParents(matrixFile), ess);
def Error():
print "Usage:", sys.argv[0], "data_filename", "matrix_file", "ess";
exit(0);
if __name__ == "__main__":
if len(sys.argv) < 4:
Error();
print "Likelihood:", Main(sys.argv[1], sys.argv[2], float(sys.argv[3]));