예제 #1
0
    def __init__(self,  graph_dict, marginal, CPD):
        '''
        initialize function in pearls ALG, whcih includes initialization of
        BN in DAG class, lamda value, lamda message of all nodes, initialization
        of pi value of all root nodes.
        '''

        DAG.__init__(self,  graph_dict)
        self.E, self.e = [], []
        self.value = {}
        for key in list(marginal.keys()):
            self.value[key] = list(marginal[key].keys())  # this is value set each node can take
        self.lamda = dict.fromkeys(graph_dict.keys())
        self.pi = dict.fromkeys(graph_dict.keys())
        self.lamda_msg = dict.fromkeys(graph_dict.keys())
        self.pi_msg = dict.fromkeys(graph_dict.keys())

        self.cpd = dict.fromkeys(graph_dict.keys())

        for x in self.vertices:
            self.lamda[x], self.lamda_msg[x], self.pi[x], self.pi_msg[x], self.cpd[x] = {}, {}, {}, {}, {}

#         given info of the graph
        self.marginal = marginal
        self.CPD = CPD

        self.get_root()
#         initialize lamda
        for x in self.vertices:
            for x_value in self.value[x]:
                # data strucutre for lamuda(X = x_value) is self.lamda[X][x_value]
                self.lamda[x][x_value] = 1

#         initialize lamda_msg
            for z in self.parents[x]:
                self.lamda_msg[z][x] = {}
                for z_value in self.value[z]:
                    # data strucutre for lamuda_X(Z = z_value) is self.lamda_msg[Z][X][z_value]
                    self.lamda_msg[z][x][z_value] = 1

#          initialize pi_msg
            for y in self.graph_dict[x]:
                self.pi_msg[x][y] = {}
                for x_value in self.value[x]:
                    # data strucutre for pi_Y(Z = z_value) is self.lamda_msg[Z][Y][z_value]
                    self.pi_msg[x][y][x_value] = 1


#         send pi_message
        for R in self.root:
            for r_value in self.value[R]:
                # data strucutre for pi(X = x_value) is self.pi[X][x_value]
                self.pi[R][r_value] = self.marginal[R][r_value]
                # data strucutre for P(X = x|e) is self.cpd[X][x]
                self.cpd[R][r_value] = self.marginal[R][r_value]

            for W in self.graph_dict[R]:
                self.send_pi_msg(R, W)
예제 #2
0
    def __init__(self,  graph_dict, marginal, CPD):
        '''
        initialize function in pearls ALG, whcih includes initialization of
        BN in DAG class, lamda value, lamda message of all nodes, initialization
        of pi value of all root nodes.
        '''

        DAG.__init__(
            self,  graph_dict)
        self.E, self.e = [], []
        self.value = [0, 1]  # this is value set each node can take
        self.lamda = dict.fromkeys(graph_dict.keys())
        self.pi = dict.fromkeys(graph_dict.keys())
        self.lamda_msg = dict.fromkeys(graph_dict.keys())
        self.pi_msg = dict.fromkeys(graph_dict.keys())

        self.cpd = dict.fromkeys(graph_dict.keys())

        for x in self.vertices:
            self.lamda[x], self.lamda_msg[x], self.pi[x], self.pi_msg[x], self.cpd[x] = {}, {}, {}, {}, {}

#         given info of the graph
        self.marginal = marginal
        self.CPD = CPD

        self.get_root()
#         initialize lamda
        for x in self.vertices:
            for x_value in self.value:
                # data strucutre for lamuda(X = x_value) is self.lamda[X][x_value]
                self.lamda[x][x_value] = 1

#         initialize lamda_msg
            for z in self.parents[x]:
                self.lamda_msg[z][x] = {}
                for z_value in self.value:
                    # data strucutre for lamuda_X(Z = z_value) is self.lamda_msg[Z][X][z_value]
                    self.lamda_msg[z][x][z_value] = 1

#          initialize pi_msg
            for y in self.graph_dict[x]:
                self.pi_msg[x][y] = {}
                for x_value in self.value:
                    # data strucutre for pi_Y(Z = z_value) is self.lamda_msg[Z][Y][z_value]
                    self.pi_msg[x][y][x_value] = 1


#         send pi_message
        for R in self.root:
            for r_value in self.value:
                # data strucutre for pi(X = x_value) is self.pi[X][x_value]
                self.pi[R][r_value] = self.marginal[R][r_value]
                # data strucutre for P(X = x|e) is self.cpd[X][x]
                self.cpd[R][r_value] = self.marginal[R][r_value]

            for W in self.graph_dict[R]:
                self.send_pi_msg(R, W)
예제 #3
0
class DependencyResolver(object):
    def __init__(self, path, options):
        logging.debug("Creating DependencyResolver")
        self.graph  = DAG()
        self.root   = path
        self.opts   = options

    def scan(self):
        for root, dirs, files in os.walk(self.root):
            for name in files:
                (base, ext) = os.path.splitext(name)
                if ext in ['.css', '.js']:
                    data = self.scanfile(os.path.join(root, name))
                    for module in data[ 'module' ]:
                        self.graph.addNode(
                            Module(
                                module=module,
                                filename=data['filename'],
                                package=data['package'],
                                requires=data['requires']
                            )
                        )
        for module in self.graph.nodes():
            self.graph.addEdgesFromNode(module.id(), module.requirements())

        

    def scanfile(self, path):
        logging.debug( "Scanning file: %s" % path )
        metadata    = {
            "filename": path,
            "package":  [],
            "module":   [],
            "requires": []
        }

        with open( path, 'r' ) as f:
            in_comment  = False
            for line in f:
                if in_comment:
                    if re.search( "\*\/", line ):
                        in_comment = False
                    else:
                        tags = re.search( "@(?P<name>\w+)\s+(?P<value>.+)\s*$", line )
                        if tags:
                            (name, value) = (tags.group( 'name' ), re.split( '\s*,\s*', tags.group( 'value' )))
                            if metadata.has_key( name ):
                                metadata[ name ].extend(value)
                else:
                    if re.match( "\s*\/\*\*", line ):
                        in_comment = True
        logging.debug( "   - Result: %s" % metadata )
        return metadata
예제 #4
0
 def __init__(self, path, options):
     logging.debug("Creating DependencyResolver")
     self.graph  = DAG()
     self.root   = path
     self.opts   = options
예제 #5
0
 def setUp(self):
     self.graph = DAG()
예제 #6
0
class TestDAG(unittest.TestCase):
    def setUp(self):
        self.graph = DAG()
#
#   Insertion
#
    def test_insertion(self):
        self.assertEqual( len(self.graph.nodes()), 0 )
        self.graph.addNode(MockNode('id1'))
        self.assertTrue( self.graph.contains( 'id1' ) )
        self.assertRaises(DuplicateInsertionError, lambda: self.graph.addNode(MockNode('id1')))

    def test_edgeinsertion(self):
        self.graph.addNode(MockNode('id1'))
        self.graph.addNode(MockNode('id2'))
        self.assertRaises(NodeNotExists, lambda: self.graph.addEdge('id1', 'id3'))
        self.assertRaises(NodeNotExists, lambda: self.graph.addEdge('id3', 'id1'))

        self.graph.addEdge('id1', 'id2')
        self.assertEquals(self.graph.neighbors('id1'), [ 'id2' ])
        self.assertEquals(self.graph.neighbors('id2'), [])
#
#   Cycle Detection
#
    def test_cycledetection_simple(self):
        self.graph.addNode(MockNode('id1'))
        self.graph.addNode(MockNode('id2'))
        self.graph.addEdge('id1','id2')
        self.assertRaises(CyclicInsertionError, lambda: self.graph.addEdge('id2','id1'))
    
    def test_cycledetection_complex(self):
        prev = None
        for x in range( 100 ):
            self.graph.addNode(MockNode(x))
            if prev is not None:
                self.graph.addEdge(prev, x)
            prev = x
        self.assertRaises(CyclicInsertionError, lambda: self.graph.addEdge(99, 0))
#
#   Topological Sort
#
    def test_topologicalsort_empty(self):
        self.assertEquals( [], self.graph.topologicalSort(self.graph.nodes()))

    def test_topologicalsort_one(self):
        self.graph.addNode(MockNode('id1'))
        self.assertEquals( ['id1'], self.graph.topologicalSort(self.graph.nodes()))

    def test_topologicalsort_two(self):
        self.graph.addNode(MockNode('id1'))
        self.graph.addNode(MockNode('id2'))
        self.graph.addEdge('id1','id2')
        self.assertEquals( ['id2', 'id1'], self.graph.topologicalSort(self.graph.nodes()))

    def test_topologicalsort_many(self):
        prev = None
        list = []
        for x in range( 100 ):
            self.graph.addNode(MockNode(x))
            if prev is not None:
                self.graph.addEdge(prev, x)
            prev = x
            list.append( x )
        list.reverse()
        self.assertEquals( list, self.graph.topologicalSort(self.graph.nodes()))