Exemplo n.º 1
0
def create_graph(doc):
    G = Graph()

    for node in doc['network']['networkStructure']['nodes']['node']:
        G.add_node(node['@id'], lat=node['coordinates']['x'], lon=node['coordinates']['y'])

    for edge in doc['network']['networkStructure']['links']['link']:
        modules = []
        try: 
            for _,v in edge['additionalModules'].iteritems():
                if isinstance(v,list):
                    for e in v:
                        m = {'capacity': e['capacity'], 'cost': e['cost']}
                        modules.append(m)
                else:
                    m = {'capacity': v['capacity'], 'cost': v['cost']}
                    modules.append(m)
        except KeyError:
            modules = []
        try:
            preInstalled=edge['preInstalledModule']
        except KeyError:
            preInstalled=''

        G.add_edge(edge['source'], edge['target'], preInstalledModule=preInstalled, additionalModules=modules)
    return G
Exemplo n.º 2
0
def test_create2():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test2', Node('test3')))
    assert [node.name for node in G.dfs()] == ['test1', 'test2', 'test3']
    assert not G.verify_edges()
Exemplo n.º 3
0
    def test_topological_sort_for_strings(self):
        g = Graph()
        g.add_edge('donor', 'specimen')
        g.add_edge('specimen', 'cell_suspension')

        sorted_list = g.topological_sort()
        self.assertEqual(['donor', 'specimen', 'cell_suspension'], sorted_list)
Exemplo n.º 4
0
 def _read_graphs(self):
     self.graphs = dict()
     with codecs.open(self._database_file_name, 'r', 'utf-8') as f:
         lines = [line.strip() for line in f.readlines()]
         tgraph, graph_cnt = None, 0
         for i, line in enumerate(lines):
             cols = line.split(' ')
             if cols[0] == 't':
                 if tgraph is not None:
                     self.graphs[graph_cnt] = tgraph
                     graph_cnt += 1
                     tgraph = None
                 if cols[-1] == '-1' or graph_cnt >= self._max_ngraphs:
                     break
                 tgraph = Graph(graph_cnt,
                                is_undirected=self._is_undirected,
                                eid_auto_increment=True)
             elif cols[0] == 'v':
                 tgraph.add_vertex(cols[1], cols[2])
             elif cols[0] == 'e':
                 tgraph.add_edge(AUTO_EDGE_ID, cols[1], cols[2], cols[3])
         # adapt to input files that do not end with 't # -1'
         if tgraph is not None:
             self.graphs[graph_cnt] = tgraph
     return self
Exemplo n.º 5
0
    def test_direct_cyclic_dependency_error_for_strings(self):
        g = Graph()
        g.add_edge('donor', 'specimen')

        with self.assertRaises(CyclicDependencyError) as context:
            g.add_edge('specimen', 'donor')

        self.assertEqual(['specimen', 'donor'], context.exception.cycle)
Exemplo n.º 6
0
def create_graph(filepath):
    g=Graph()
    with open(filepath) as csvfile:
        reader  = csv.DictReader(csvfile)
        #print reader.fieldnames
        for row in reader:
            g.add_edge(row['fromNode'],row['toNode'])
    return g
Exemplo n.º 7
0
    def _get_samples(self):
        samples_map = {}
        derived_from_graph = Graph()

        project = self.manifest.get_project()
        for biomaterial in self.manifest.get_biomaterials():
            archive_entity = ArchiveEntity()
            archive_entity.manifest_id = self.manifest.manifest_id
            archive_type = "sample"
            archive_entity.archive_entity_type = archive_type
            archive_entity.id = self.generate_archive_entity_id(
                archive_type, biomaterial.data)

            archive_entity.data = {
                'biomaterial': biomaterial.data,
                'project': project
            }

            archive_entity.metadata_uuids = [
                biomaterial.data['uuid']['uuid'], project['uuid']['uuid']
            ]
            archive_entity.accessioned_metadata_uuids = [
                biomaterial.data['uuid']['uuid']
            ]

            if biomaterial.derived_by_process:
                # TODO protocols will be needed for samples conversion
                # archive_entity.data.update(biomaterial.derived_with_protocols)

                sample_links = []
                for derived_from in biomaterial.derived_from_biomaterials:
                    derived_from_alias = self.generate_archive_entity_id(
                        'sample', derived_from)
                    derived_from_graph.add_edge(derived_from_alias,
                                                archive_entity.id)
                    sample_links.append({
                        'alias': derived_from_alias,
                        'relationshipNature': 'derived from'
                    })

                links = {'sampleRelationships': sample_links}
                archive_entity.links = links

            samples_map[archive_entity.id] = archive_entity

        sorted_samples = derived_from_graph.topological_sort()
        priority_samples = [
            samples_map.get(sample) for sample in sorted_samples
            if samples_map.get(sample)
        ]
        orphan_samples = [
            samples_map.get(sample) for sample in samples_map.keys()
            if sample not in priority_samples
        ]

        return priority_samples + orphan_samples
Exemplo n.º 8
0
def test_reverse_dfs1():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    expect = ['test1', 'test2', 'test3']
    assert [node.name for node in G.dfs()] == expect
    expect.reverse()
    assert [node.name for node in G.dfs(reverse=True)] == expect
Exemplo n.º 9
0
 def to_graph(self, gid=VACANT_GRAPH_ID, is_undirected=True):
     """Construct a graph according to the dfs code."""
     g = Graph(gid, is_undirected=is_undirected, eid_auto_increment=True)
     for dfsedge in self:
         frm, to, (vlb1, elb, vlb2) = dfsedge.frm, dfsedge.to, dfsedge.vevlb
         if vlb1 != VACANT_VERTEX_LABEL:
             g.add_vertex(frm, vlb1)
         if vlb2 != VACANT_VERTEX_LABEL:
             g.add_vertex(to, vlb2)
         g.add_edge(AUTO_EDGE_ID, frm, to, elb)
     return g
Exemplo n.º 10
0
def create_graph(filepath):
    g = Graph()
    with open(filepath) as csvfile:
        for line in csvfile:
            line = line.split()
            if line == []:
                continue
            if line[0] == '%':
                continue
            else:
                g.add_edge(line[0], line[1])
    return g
Exemplo n.º 11
0
def test_create4():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', Node('test3')))
    assert not G.verify_edges()
    G.replace_node('test2', Node('test4'))
    expect = ['test1', 'test4', 'test3']
    assert [node.name for node in G.dfs()] == expect
    expect.reverse()
    assert [node.name for node in G.dfs(reverse=True)] == expect
    assert not G.verify_edges()
Exemplo n.º 12
0
def test_create1():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', Node('test3')))
    expect = ['test1', 'test2', 'test3']
    assert [node.name for node in G.dfs()] == expect
    expect.reverse()
    assert [node.name for node in G.dfs(reverse=True)] == expect
    assert G.num_in_edges('test1') == 0
    assert G.num_out_edges('test1') == 1
    assert not G.verify_edges()
Exemplo n.º 13
0
    def test_indirect_cyclic_dependency_error_3_levels(self):
        g = Graph()
        g.add_edge(5, 2)
        g.add_edge(5, 0)
        g.add_edge(4, 0)
        g.add_edge(4, 1)
        g.add_edge(2, 3)
        g.add_edge(3, 1)
        g.add_edge(1, 0)

        with self.assertRaises(CyclicDependencyError) as context:
            g.add_edge(0, 2)

        self.assertEqual([0, 2, 3, 1], context.exception.cycle)
Exemplo n.º 14
0
Arquivo: mrd.py Projeto: zywan/Kariz
 def dag_from_string2(self, raw_execplan):
     raw_dag = ast.literal_eval(raw_execplan)
     nodes = raw_dag['nodes']
     n_nodes = len(nodes)
     g = Graph(n_nodes)
     g.dag_id = raw_dag['id']
     g.nodes = nodes
     for src in raw_dag['edges']:
         dests = raw_dag['edges'][src]
         for dst in dests:
             g.add_edge(src, dst, 0)
     g.inputs = raw_dag['inputs']
     g.inputSize = raw_dag['size']
     g.timeValue = raw_dag['original_runtime']
     g.cachedtimeValue = raw_dag['cached_runtime']
     self.initialize_mrdtable(g)
     self.graphs[g.dag_id] = g
Exemplo n.º 15
0
    def _get_samples(self):
        samples_map = {}
        derived_from_graph = Graph()

        for biomaterial in self.manifest.get_biomaterials():
            archive_entity = ArchiveEntity()
            archive_entity.manifest_id = self.manifest.manifest_id
            archive_type = "sample"
            archive_entity.archive_entity_type = archive_type
            archive_entity.id = self.generate_archive_entity_id(
                archive_type, biomaterial.data)

            archive_entity.data = {'biomaterial': biomaterial.data}

            if biomaterial.derived_by_process:
                # TODO protocols will be needed for samples conversion
                # archive_entity.data.update(biomaterial.derived_with_protocols)

                derived_from_alias = self.generate_archive_entity_id(
                    'sample', biomaterial.derived_from)
                derived_from_graph.add_edge(derived_from_alias,
                                            archive_entity.id)
                links = {
                    'sampleRelationships': [{
                        'alias':
                        derived_from_alias,
                        'relationshipNature':
                        'derived from'
                    }]
                }
                archive_entity.links = links

            samples_map[archive_entity.id] = archive_entity

        sorted_samples = derived_from_graph.topological_sort()
        priority_samples = [
            samples_map.get(sample) for sample in sorted_samples
            if samples_map.get(sample)
        ]
        orphan_samples = [
            samples_map.get(sample) for sample in samples_map.keys()
            if sample not in priority_samples
        ]

        return priority_samples + orphan_samples
Exemplo n.º 16
0
    def test_indirect_cyclic_dependency_error(self):
        g = Graph()
        g.add_edge(5, 2)
        g.add_edge(5, 0)
        g.add_edge(4, 0)
        g.add_edge(4, 1)
        g.add_edge(2, 3)
        g.add_edge(3, 1)

        with self.assertRaises(CyclicDependencyError):
            g.add_edge(1, 2)
Exemplo n.º 17
0
def test_match3():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test2', 'test4'))
    # not the same - look here

    fragment = Graph()
    fragment.add_node(MatchNameNode("test1"))
    fragment.add_node(MatchNameNode('test2'))
    fragment.add_edge(Edge('test1', 'test2'))
    fragment.add_edge(Edge('test1', MatchNameNode('test3')))

    res = G.match_fragment(fragment)
    assert len(res) == 1
    assert res[0].num_nodes() == 3
    assert res[0].num_edges() == 2
Exemplo n.º 18
0
def test_remove_edge():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', Node('test3')))
    assert [node.name for node in G.dfs()] == ['test1', 'test2', 'test3']
    assert G.num_edges() == 2
    edge12 = G.edge('test1', 'test2')
    assert edge12.from_node.name == 'test1' and edge12.from_idx == 0
    assert edge12.to_node.name == 'test2' and edge12.to_idx == 0
    assert not G.verify_edges()
    G.remove_edge(edge12)
    assert G.num_edges() == 1
    assert G.num_in_edges('test1') == 0
    assert G.num_out_edges('test1') == 0
    assert G.num_in_edges('test2') == 0
    assert G.num_out_edges('test2') == 1
    assert G.num_in_edges('test3') == 1
    assert G.num_out_edges('test3') == 0
    assert not G.verify_edges()
Exemplo n.º 19
0
def test_match4alt():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_node(Node('test3'))
    G.add_node(Node('test4'))
    G.add_node(Node('test5'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4', to_idx=1))
    G.add_edge(Edge('test4', 'test5'))

    fragment = GraphMatcher()
    m2 = MatchNodeByName("test2")
    m3 = MatchNodeByName('test3')
    m4 = MatchNodeByName('test4')
    e1 = MatchEdgeInputsGroupFactory()

    fragment.add_edge(e1.get_edge(from_node=m2, to_node=m4))
    fragment.add_edge(e1.get_edge(from_node=m3, to_node=m4))

    res = fragment.match_graph(G)
    assert len(res) == 1
    assert res[0].num_nodes() == 3
    assert res[0].num_edges() == 2
Exemplo n.º 20
0
def test_match_fail():
    class MatchSuffNode(MatchNode):
        def __init__(self, name, suff):
            super().__init__(name)
            self.suff = suff

        def _match(self, G, node, edge):
            return node.name.endswith(self.suff)

    G = Graph()
    G.add_node(Node("test1a"))
    G.add_edge(Edge('test1a', Node('test2a')))
    G.add_edge(Edge('test2a', Node('test3b')))
    G.add_edge(Edge('test3b', Node('test4a')))

    fragment = Graph()
    fragment.add_node(MatchSuffNode('match1', 'a'))
    fragment.add_node(MatchSuffNode('match2', 'b'))
    fragment.add_edge(Edge('match1', 'match2'))

    res = G.match_fragment(fragment)
    assert len(res) == 1
    assert res[0].num_nodes() == 2
    assert res[0].num_edges() == 1
    assert [node.name for node in res[0].nodes()] == ['test2a', 'test3b']
Exemplo n.º 21
0
def test_match_fail(caplog):
    caplog.set_level(logging.DEBUG)

    class MatchSuffNode(NodeMatch):
        def __init__(self, suff):
            self._suff = suff
            self._has_matched = False

        def match(self, G, node, state):
            if self._has_matched:
                return False
            if node.name.endswith(self._suff):
                self._has_matched = True
                return True
            return False

        def commit_match(self, G, node, state):
            pass

        def reset_match(self, G, state, node=None, init=False):
            self._has_matched = False

    G = Graph()
    G.add_node(Node("test1a"))
    G.add_edge(Edge('test1a', Node('test2a')))
    G.add_edge(Edge('test2a', Node('test3b')))
    G.add_edge(Edge('test3b', Node('test4a')))

    fragment = GraphMatcher()
    fragment.add_edge(
        MatchEdgeByIdx(from_node=MatchSuffNode('a'),
                       to_node=MatchSuffNode('b')))

    res = fragment.match_graph(G)
    assert len(res) == 1
    assert res[0].num_nodes() == 2
    assert res[0].num_edges() == 1
    assert set([node.name
                for node in res[0].nodes()]) == set(['test2a', 'test3b'])
Exemplo n.º 22
0
def test_match4():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_node(Node('test3'))
    G.add_node(Node('test4'))
    G.add_node(Node('test5'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4', to_idx=1))
    G.add_edge(Edge('test4', 'test5'))

    fragment = Graph()
    fragment.add_node(MatchNameNode("test2"))
    fragment.add_node(MatchNameNode('test3'))
    fragment.add_node(MatchNameNode('test4'))
    fragment.add_edge(Edge('test2', 'test4'))
    fragment.add_edge(Edge('test3', 'test4', to_idx=1))

    res = G.match_fragment(fragment)
    assert len(res) == 1
    assert res[0].num_nodes() == 3
    assert res[0].num_edges() == 2
Exemplo n.º 23
0
def test_match_and_replace():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4'))

    fragment = Graph()
    fragment.add_node(MatchNameNode("test1"))
    fragment.add_node(MatchNameNode('test2'))
    fragment.add_edge(Edge('test1', 'test2'))
    fragment.add_edge(Edge('test1', MatchNameNode('test3')))

    res = G.match_fragment(fragment)
    assert len(res) == 1
    assert res[0].num_nodes() == 3
    assert res[0].num_edges() == 2
    G.replace_fragment(res[0], Node('test5'))
    res = list(node.name for node in G.dfs())
    assert res == ['test5', 'test4']
    assert not G.verify_edges()
Exemplo n.º 24
0
 def test_topological_sort(self):
     g = Graph()
     g.add_edge(5, 2)
     g.add_edge(5, 0)
     g.add_edge(4, 0)
     g.add_edge(4, 1)
     g.add_edge(2, 3)
     g.add_edge(3, 1)
     sorted_list = g.topological_sort()
     self.assertEqual([4, 5, 0, 2, 3, 1], sorted_list)
Exemplo n.º 25
0
def test_all_predecessors():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4', to_idx=1))
    preds = set()
    for node in G.all_predecessors('test4'):
        assert node.name not in preds
        preds.add(node.name)
    test_fn = lambda name: set(node.name for node in G.all_predecessors(name))
    assert set(['test1', 'test2', 'test3']) == test_fn('test4')
    assert set(['test1']) == test_fn('test2')
    assert set(['test1']) == test_fn('test3')
    assert not test_fn('test1')
Exemplo n.º 26
0
def test_insert():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4', to_idx=2))

    G.insert_node(Node('test5'), 'test2', 'test4')

    nodes = [node.name for node in G.dfs()]
    assert nodes == ['test1', 'test2', 'test5', 'test3', 'test4']
    suc = G.successor_names('test5')
    pred = G.predecessor_names('test5')
    assert len(suc) == 1 and 'test4' in suc
    assert len(pred) == 1 and 'test2' in pred
    assert not G.verify_edges()
Exemplo n.º 27
0
def test_match1():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test3', 'test4'))
    assert not G.verify_edges()

    fragment = Graph()
    fragment.add_node(MatchNameNode("test1"))
    fragment.add_node(MatchNameNode('test2'))
    fragment.add_edge(Edge('test1', 'test2'))
    assert not fragment.verify_edges()

    res = G.match_fragment(fragment)
    assert len(res) == 1
    assert len(res[0]) == 2
    assert res[0].num_edges() == 1
Exemplo n.º 28
0
def test_create5():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4'))
    assert G.num_in_edges('test4') == 2
    assert G.num_out_edges('test1') == 2
    assert [node.name
            for node in G.dfs()] == ['test1', 'test2', 'test3', 'test4']
    assert not G.verify_edges()
Exemplo n.º 29
0
def test_match1():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test3', 'test4'))
    assert not G.verify_edges()

    fragment = GraphMatcher()
    fragment.add_edge(
        MatchEdgeByIdx(from_node=MatchNodeByName("test1"),
                       to_node=MatchNodeByName('test2')))

    res = fragment.match_graph(G)
    assert len(res) == 1
    assert len(res[0]) == 2
    assert res[0].num_edges() == 1
Exemplo n.º 30
0
def test_match3(caplog):
    caplog.set_level(logging.DEBUG)
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    G.add_node(Node('test4'))
    G.add_edge(Edge('test2', 'test4'))
    # not the same - look here

    fragment = GraphMatcher()
    n1 = MatchNodeByName('test1')
    fragment.add_edge(
        MatchEdgeByIdx(from_node=n1, to_node=MatchNodeByName('test2')))
    fragment.add_edge(
        MatchEdgeByIdx(from_node=n1, to_node=MatchNodeByName('test3')))

    res = fragment.match_graph(G)
    assert len(res) == 1
    assert res[0].num_nodes() == 3
    assert res[0].num_edges() == 2
Exemplo n.º 31
0
def test_match4():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_node(Node('test3'))
    G.add_node(Node('test4'))
    G.add_node(Node('test5'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test2', 'test4'))
    G.add_edge(Edge('test3', 'test4', to_idx=1))
    G.add_edge(Edge('test4', 'test5'))

    fragment = GraphMatcher()
    m2 = MatchNodeByName("test2")
    m3 = MatchNodeByName('test3')
    m4 = MatchNodeByName('test4')
    fragment.add_edge(MatchEdgeByIdx(from_node=m2, to_node=m4))
    fragment.add_edge(MatchEdgeByIdx(from_node=m3, to_node=m4, to_idx=1))

    res = fragment.match_graph(G)
    assert len(res) == 1
    assert res[0].num_nodes() == 3
    assert res[0].num_edges() == 2