Esempio n. 1
0
def graph_generation():
    graph = nx.MultiDiGraph()
    data = load_data()

    # 导入对象属性边
    for i in data['object_attribute']:
        label, successor, predecessors = i
        graph.add_edge(successor,
                       predecessors,
                       label,
                       label=label,
                       type='object')
    # 导入值属性边
    for i in data['valued_attribute']:
        label, successor, value_type = i
        predecessors = '%s:%s' % (value_type, label)
        graph.add_edge(successor,
                       predecessors,
                       label,
                       label=label,
                       type='value')

    # 为节点导入属性
    for n in graph.nodes:
        if ':' in n:
            graph.node[n]['label'] = 'literal'
        else:
            graph.node[n]['label'] = 'concept'
    nx.freeze(graph)
    return graph
Esempio n. 2
0
def graph_generation(ontology_dir):
    temp_graph = nx.MultiDiGraph()
    data = load_data(ontology_dir)

    # 导入对象属性边
    for i in data['object_attribute']:
        relation_name, label, successor, predecessors = i
        temp_graph.add_edge(successor,
                            predecessors,
                            label,
                            label=label,
                            type='object',
                            relation_name=relation_name)
    # 导入值属性边
    for i in data['valued_attribute']:
        label, successor, value_type = i
        predecessors = '%s:%s' % (value_type, label)
        temp_graph.add_edge(successor,
                            predecessors,
                            label,
                            label=label,
                            type='value')

    # 为节点导入属性
    for n in temp_graph.nodes:
        if ':' in n:
            temp_graph.node[n]['label'] = 'literal'
        else:
            temp_graph.node[n]['label'] = 'concept'
    # 使graph不能再被修改
    nx.freeze(temp_graph)
    # self.graph = temp_graph
    return temp_graph
Esempio n. 3
0
    def __init__(
        self,
        nodes: List[AssemblyNode],
        container: AlignmentContainer,
        full_assembly_graph: nx.DiGraph,
        query_key: str,
        query: SeqRecord,
        seqdb,
        do_raise: bool = True,
    ):
        self.logger = logger(self)
        self._nodes = tuple(nodes)
        self._reactions = tuple()
        self.validate_input_nodes()
        self.seqdb = seqdb
        self.query_key = query_key
        self.query = query
        self._full_graph = full_assembly_graph

        self.container = container
        self.groups = container.groups()
        if len(self.groups) == 0:
            raise DasiDesignException("No groups were found in container.")
        self.graph = self._subgraph(self._full_graph, nodes, do_raise=do_raise)
        nx.freeze(self.graph)

        if do_raise:
            self.post_validate(do_raise)
def freeze(graphs):
    '''freeze graphs
    :param graphs: List<Graph>
    :return: void
    '''
    for graph in graphs:
        nx.freeze(graph)
Esempio n. 5
0
def createGraphFromTXT(txtFile):
    f = open(txtFile)  #Open file with read only permits
    line = f.readline()  #read first line

    G = nx.Graph()  #Create Graph object G from JSON data, G is symmetrical

    # If the file is not empty keep reading line one at a time
    # till the file is empty
    while line != '\n' and line != '':
        link = line.strip('\n').split(' ')  #Turn to usable link format

        if len(link) == 3:
            weight = float(link[0])
            source = int(link[1])
            target = int(link[2])

            dist = 1 - weight

            #print('Weight: %.12f, From: %d, To: %d' % (weight,source,target))

            #TODO: INDEX TO TRACK ID?????
            if weight > 0 and dist > 0:
                G.add_edge(source, target, similarity=weight, distance=dist)
        else:
            print(link)

        line = f.readline()

    f.close()  #Clean up
    nx.freeze(G)  #Turn Immutable

    return G
def MakeAdjacencyGraph(n, type, freq=0.5, wexc=0.1):
  '''Temporary implementation that only generates complete graph.'''
  G = nx.complete_graph(n, nx.DiGraph())
  # Should profile this.
  for edge in G.edges_iter():
    nx.set_edge_attributes(G, 'weight', {edge: wexc})
  nx.freeze(G) # To prevent unintended modification of G's structure
  return G
Esempio n. 7
0
 def setUp(self):
     self.G_basic = tg.node_generate(
         [[10, 11], [20], [30, 31], [40, 41], [50]],
         itertools.count(step=100))
     self.G_basic.add_path([10, 20, 30, 40, 50])
     self.G_basic.add_path([11, 20, 31])
     self.G_basic.add_path([30, 41, 50])
     nx.freeze(self.G_basic)
Esempio n. 8
0
def asia_graph():
    """Return the 'Asia' PGM graph."""
    G = nx.DiGraph(name="asia")
    G.add_edges_from([('asia', 'tuberculosis'), ('smoking', 'cancer'),
                      ('smoking', 'bronchitis'), ('tuberculosis', 'either'),
                      ('cancer', 'either'), ('either', 'xray'),
                      ('either', 'dyspnea'), ('bronchitis', 'dyspnea')])
    nx.freeze(G)
    return G
Esempio n. 9
0
 def complete(self):
     nx.freeze(self.DAG)
     self.__order = self.DAG.order()
     self.__roots = self.DAG.roots()
     self.__rv_roots = find_rv_roots(self)
     self.__leaves = self.DAG.leaves()
     self.__exo = find_exo(self)
     self.json = form_js(self)
     self.script = form_script(self)
Esempio n. 10
0
def get_nodal_graph(geometry):
    # Mathematical graph of all nodal lines connected by finite strips
    nodal_graph = nx.DiGraph()

    # Add nodal lines
    for node_id, (x, z) in geometry['nodal_lines'].items():
        nodal_graph.add_node(node_id, x=x, z=z)

    # Add finite strips
    for strip_id, (node1_id, node2_id, material_id) in enumerate(geometry['finite_strips'], start=1):
        dx = nodal_graph.node[node2_id]['x'] - nodal_graph.node[node1_id]['x']
        dz = nodal_graph.node[node2_id]['z'] - nodal_graph.node[node1_id]['z']
        b = np.sqrt(dx**2 + dz**2) # [mm] strip width

        R = get_transformation_matrix(dx, dz, b) # global<->local coordinates transformation matrix

        # Deduced from Fortran block 83:94
        astiff_blocks = (node1_id-1, node2_id-1) # Python counts from 0
        astiff_fill_indices = []
        for row in xrange(2):
            for col in xrange(2):
                astiff_row_start = ASTIFF_BLOCK_SIZE * astiff_blocks[row]
                astiff_row_end   = ASTIFF_BLOCK_SIZE + astiff_row_start
                astiff_col_start = ASTIFF_BLOCK_SIZE * astiff_blocks[col]
                astiff_col_end   = ASTIFF_BLOCK_SIZE + astiff_col_start
                astiff_indices = (
                    slice(astiff_row_start, astiff_row_end),
                    slice(astiff_col_start, astiff_col_end)
                )

                segment_row_start = ASTIFF_BLOCK_SIZE * row
                segment_row_end   = ASTIFF_BLOCK_SIZE + segment_row_start
                segment_col_start = ASTIFF_BLOCK_SIZE * col
                segment_col_end   = ASTIFF_BLOCK_SIZE + segment_col_start
                segment_indices = (
                    slice(segment_row_start, segment_row_end),
                    slice(segment_col_start, segment_col_end)
                )

                astiff_fill_indices.append((astiff_indices, segment_indices))

        label = "(%d)" % strip_id

        edge_data_keys = 'material_id, b, R, astiff_fill_indices, label'.split(', ')
        nodal_graph.add_edge(node1_id, node2_id, dict(zip(
            edge_data_keys,
            (material_id, b, R, astiff_fill_indices, label)
        )))

    # Disallow further changes to the nodal_graph
    nx.freeze(nodal_graph)

    # Cache the traversal through strips for performance
    strip_data = nodal_graph.edges(data=True)

    return nodal_graph, strip_data
 def _generate_graph(self):
     '''
     Generate both the unpacked and the packed graph and
     set default attributes to the nodes in the packed graph
     '''
     edges = self._generate_edges()
     self._unpacked_graph = nx.DiGraph()
     self._unpacked_graph.add_edges_from(edges)
     nx.freeze(self._unpacked_graph)
     self.graph = nx.DiGraph(self._unpacked_graph)
     self._pack_graph()
     self._set_nodes_attributes()
    def setUp(self):
        graph_a = nx.Graph(id=1)
        graph_a.add_node(1, label=0)
        graph_a.add_node(2, label=0)
        graph_a.add_edge(1, 2, label=0)
        self.tiny_graph = nx.freeze(graph_a)

        graph_b = nx.Graph(graph_a)
        graph_b.add_node(3, label=3)
        graph_b.add_edge(1, 3, label=13)
        graph_b.add_edge(2, 3, label=23)
        self.small_graph = nx.freeze(graph_b)
Esempio n. 13
0
    def make_site_map(self) -> None:
        """Sycncronous function to make the site map using async run_crawler().

        Returns
        -------
        None

        """
        asyncio.run(self._run_crawler())
        self.site_graph.remove_node(0)
        networkx.freeze(self.site_graph)
        self.logger.info("Site map building done!")
def build_meetups_graph(groups, topics, members, events):
    graph = nx.Graph()
    for group in groups:
        key = group['id']
        graph.add_node(key, name=group['name'], type=Type.Groups, members=group['members'])
        link_members(graph, group,members)
        link_topics(graph, group, topics)
    link_members_to_topics(graph, members, topics)
    link_members_to_events(graph, members, events)
    nx.freeze(graph)

    logging.info('graph built')
    return graph
Esempio n. 15
0
 def setup_class(cls):
     G = nx.Graph(name="test")
     e = [('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f'), ('a', 'f')]
     G.add_edges_from(e, width=10)
     G.add_node('g', color='green')
     G.graph['number'] = 1
     DG = nx.DiGraph(G)
     MG = nx.MultiGraph(G)
     MG.add_edge('a', 'a')
     MDG = nx.MultiDiGraph(G)
     MDG.add_edge('a', 'a')
     fG = G.copy()
     fDG = DG.copy()
     fMG = MG.copy()
     fMDG = MDG.copy()
     nx.freeze(fG)
     nx.freeze(fDG)
     nx.freeze(fMG)
     nx.freeze(fMDG)
     cls.G = G
     cls.DG = DG
     cls.MG = MG
     cls.MDG = MDG
     cls.fG = fG
     cls.fDG = fDG
     cls.fMG = fMG
     cls.fMDG = fMDG
Esempio n. 16
0
 def setup_class(cls):
     G = nx.Graph(name="test")
     e = [("a", "b"), ("b", "c"), ("c", "d"), ("d", "e"), ("e", "f"), ("a", "f")]
     G.add_edges_from(e, width=10)
     G.add_node("g", color="green")
     G.graph["number"] = 1
     DG = nx.DiGraph(G)
     MG = nx.MultiGraph(G)
     MG.add_edge("a", "a")
     MDG = nx.MultiDiGraph(G)
     MDG.add_edge("a", "a")
     fG = G.copy()
     fDG = DG.copy()
     fMG = MG.copy()
     fMDG = MDG.copy()
     nx.freeze(fG)
     nx.freeze(fDG)
     nx.freeze(fMG)
     nx.freeze(fMDG)
     cls.G = G
     cls.DG = DG
     cls.MG = MG
     cls.MDG = MDG
     cls.fG = fG
     cls.fDG = fDG
     cls.fMG = fMG
     cls.fMDG = fMDG
Esempio n. 17
0
 def setUp(self):
     G=nx.Graph(name="test")
     e=[('a','b'),('b','c'),('c','d'),('d','e'),('e','f'),('a','f')]
     G.add_edges_from(e,width=10)
     G.add_node('g',color='green')
     G.graph['number']=1
     DG=nx.DiGraph(G)
     MG=nx.MultiGraph(G)
     MG.add_edge('a', 'a')
     MDG=nx.MultiDiGraph(G)
     MDG.add_edge('a', 'a')
     fG = G.copy()
     fDG = DG.copy()
     fMG = MG.copy()
     fMDG = MDG.copy()
     nx.freeze(fG)
     nx.freeze(fDG)
     nx.freeze(fMG)
     nx.freeze(fMDG)
     self.G=G
     self.DG=DG
     self.MG=MG
     self.MDG=MDG
     self.fG=fG
     self.fDG=fDG
     self.fMG=fMG
     self.fMDG=fMDG
Esempio n. 18
0
def asia_graph():
    """Return the 'Asia' PGM graph."""
    G = nx.DiGraph(name="asia")
    G.add_edges_from([
        ("asia", "tuberculosis"),
        ("smoking", "cancer"),
        ("smoking", "bronchitis"),
        ("tuberculosis", "either"),
        ("cancer", "either"),
        ("either", "xray"),
        ("either", "dyspnea"),
        ("bronchitis", "dyspnea"),
    ])
    nx.freeze(G)
    return G
Esempio n. 19
0
def generic_graph_view(G, create_using=None):
    if create_using is None:
        newG = G.__class__()
    else:
        newG = nx.empty_graph(0, create_using)
    if G.is_multigraph() != newG.is_multigraph():
        raise NetworkXError("Multigraph for G must agree with create_using")
    newG = nx.freeze(newG)

    # create view by assigning attributes from G
    newG._graph = G
    newG.graph = G.graph

    newG._node = G._node
    if newG.is_directed():
        if G.is_directed():
            newG._succ = G._succ
            newG._pred = G._pred
            newG._adj = G._succ
        else:
            newG._succ = G._adj
            newG._pred = G._adj
            newG._adj = G._adj
    elif G.is_directed():
        if G.is_multigraph():
            newG._adj = UnionMultiAdjacency(G._succ, G._pred)
        else:
            newG._adj = UnionAdjacency(G._succ, G._pred)
    else:
        newG._adj = G._adj
    return newG
Esempio n. 20
0
def flatten(item, freeze=True):
    """Flattens a item (a task or flow) into a single execution graph."""
    graph = _post_flatten(_flatten(item, set()))
    if freeze:
        # Frozen graph can't be modified...
        return nx.freeze(graph)
    return graph
Esempio n. 21
0
 def _swap(self, replacement_graph):
     """Validates the replacement graph and then swaps the underlying graph
     with a frozen version of the replacement graph (this maintains the
     invariant that the underlying graph is immutable).
     """
     self._validate(replacement_graph)
     self._graph = nx.freeze(replacement_graph)
Esempio n. 22
0
def create_clusters(positions, *, feature_size, coordinate_system):
    """
    Create clusters from the given positions, by joining all positions which are
    less than twice the ``feature_size`` apart.

    Arguments
    ---------
    positions : list(list(float))
        The list of positions to cluster.
    feature_size : float
        Distance between two nodes where they are considered to belong to different clusters.
    coordinate_system : CoordinateSystem
        Coordinate system used to calculate distances between positions.

    Returns
    -------
    list(nx.Graph) :
        A list of connected graphs, each representing one cluster.
    """
    graph = _create_graph(positions,
                          feature_size=feature_size,
                          coordinate_system=coordinate_system)
    return [
        nx.freeze(subgraph)
        for subgraph in nx.algorithms.connected_component_subgraphs(graph)
    ]
Esempio n. 23
0
def subgraph_view(G, filter_node=no_filter, filter_edge=no_filter):
    newG = nx.freeze(G.__class__())
    newG._NODE_OK = filter_node
    newG._EDGE_OK = filter_edge

    # create view by assigning attributes from G
    newG._graph = G
    newG.graph = G.graph

    newG._node = FilterAtlas(G._node, filter_node)
    if G.is_multigraph():
        Adj = FilterMultiAdjacency

        def reverse_edge(u, v, k):
            return filter_edge(v, u, k)
    else:
        Adj = FilterAdjacency

        def reverse_edge(u, v):
            return filter_edge(v, u)

    if G.is_directed():
        newG._succ = Adj(G._succ, filter_node, filter_edge)
        newG._pred = Adj(G._pred, filter_node, reverse_edge)
        newG._adj = newG._succ
    else:
        newG._adj = Adj(G._adj, filter_node, filter_edge)
    return newG
Esempio n. 24
0
    def from_mininet(cls, mininet_topo):
        """Create a VirtualNetwork from a mininet Topo."""

        from mininet.topo import Topo

        assert isinstance(mininet_topo, Topo), "Invalid Network Format"
        g = nx.Graph()

        for u in mininet_topo.nodes():
            cpu = mininet_topo.nodeInfo(u).get("cpu", 0)
            memory = mininet_topo.nodeInfo(u).get("memory", 0)
            if type(memory) == str:
                if memory.endswith("MB"):
                    memory = int(float(memory[:-2]))
                elif memory.endswith("GB"):
                    memory = int(float(memory[:-2]) * 1000)
            g.add_node(
                u,
                cores=cpu,
                memory=memory,
            )

        for (u, v) in mininet_topo.iterLinks(withInfo=False):
            g.add_edge(u, v, rate=mininet_topo.linkInfo(u, v).get("bw", 0))

        return cls(nx.freeze(g))
Esempio n. 25
0
def main():
  seed(0) #set seed
  #get graph info
  G = nx.read_gpickle("input/graphMTC_CentroidsLength5.gpickle") #noCentroidsLength15.gpickle") #does not have centroidal links
  print '|V| = ', len(G.nodes())
  print '|E| = ', len(G.edges())
  G = nx.freeze(G) #prevents edges or nodes to be added or deleted
  #get od info. This is in format of a dict keyed by od, like demand[sd1][sd2] = 200000.
  demand = bd.build_demand('input/BATS2000_34SuperD_TripTableData.csv', 'input/superdistricts_centroids.csv') #bd.build_demand('input/BATS2000_34SuperD_TripTableData.csv', 'input/superdistricts_centroids.csv')
  #get earthquake info
  q = QuakeMaps('input/20130210_mtc_total_lnsas3.pkl', 'input/20130210_mtc_magnitudes3.pkl', 'input/20130210_mtc_faults3.pkl', 'input/20130210_mtc_weights3.pkl', 'input/20130210_mtc_scenarios3.pkl') #(input/20130107_mtc_total_lnsas1.pkl', 'input/20130107_mtc_magnitudes1.pkl', 'input/20130107_mtc_faults1.pkl', 'input/20130107_mtc_weights1.pkl', 'input/20130107_mtc_scenarios1.pkl') #totalfilename=None, magfilename=None, faultfilename=None, weightsfilename=None, scenariofilename=None): 'input/20130210_mtc_total_lnsas3.pkl', 'input/20130210_mtc_magnitudes3.pkl', 'input/20130210_mtc_faults3.pkl', 'input/20130210_mtc_weights3.pkl', 'input/20130210_mtc_scenarios3.pkl') #(


  q.num_sites = len(q.lnsas[0])
  #determine which scenarios you want to run
  good_indices = pick_scenarios(q.lnsas, q.weights)
  
  travel_index_times = []
  index = 0
  #loop over scenarios
  for scenario in q.lnsas: #each 'scenario' has 1557 values of lnsa, i.e. one per site
    if index in good_indices:
      print 'index: ', index
      (travel_time, vmt) = run_iteration(G, scenario, demand)
      travel_index_times.append((index, travel_time, vmt))
#      print 'new travel times: ', travel_index_times
      if index%100 ==0:
        util.write_2dlist(time.strftime("%Y%m%d")+'_travel_time.txt',travel_index_times)
    index += 1 #IMPORTANT
  util.write_2dlist(time.strftime("%Y%m%d")+'_travel_time.txt',travel_index_times)
Esempio n. 26
0
def flatten(item, freeze=True):
    """Flattens a item (a task or flow) into a single execution graph."""
    graph = _post_flatten(_flatten(item, set()))
    if freeze:
        # Frozen graph can't be modified...
        return nx.freeze(graph)
    return graph
Esempio n. 27
0
    def from_file(cls, filename):
        """Create a VirtualNetwork from json files."""

        g = nx.MultiGraph()

        # filename can be the path to a file or the name of a local topology
        if os.path.isabs(filename):
            filepath = filename
        else:
            raise ValueError("Wrong file path")

        with open(filepath) as f:

            data = json.load(f)

            for node in data["nodes"]:
                g.add_node(
                    node,
                    cores=data["nodes"][node].get("cores", 0),
                    memory=data["nodes"][node].get("memory", 0),
                )

            for link in data["links"]:
                u, v = link.split(" ")
                rate = data["links"][link]["rate"]

                g.add_edge(u, v, rate=rate)

        return cls(nx.freeze(g))
Esempio n. 28
0
    def __init__(self, graph:str or networkx.Graph):
        if isinstance(graph, networkx.Graph):
            nxgraph = networkx.Graph(graph)
        elif isinstance(graph, str):
            nxgraph = phasme.build_graph.graph_from_file(graph)
        else:
            raise ValueError("Unexpected {}".format(graph))
        # internal graph representation
        self.__edges = map(frozenset, nxgraph.edges)
        if const.TEST_INTEGRITY:
            self.__edges = tuple(self.__edges)
            for args in self.__edges:
                if len(args) > 2:
                    print('WARNING: Weird edge: {}. It will be filtered.'.format(args))
                else:
                    assert len(args) == 2, args
        self.__edges = set(edge for edge in self.__edges if len(edge) == 2)

        # data
        self.__nodes = set(nxgraph.nodes)
        self.__uid = str(min(self.__nodes, key=str))
        self.__nb_node = len(self.__nodes)
        self.__nb_cc = networkx.number_connected_components(nxgraph)
        self._nxgraph = nxgraph if constants.KEEP_NX_GRAPH else networkx.freeze(nxgraph)
        self.__initial_number_of_edge = len(self.__edges)
        self.__active_recipe = None

        self.__hierarchy = set()  # inclusions between powernodes
        self.__powernodes = defaultdict(set)  # (step, set) -> {node in powernode}
        self.__poweredges = defaultdict(set)  # (step, set) -> (step, set)
Esempio n. 29
0
 def _swap(self, replacement_graph):
     """Validates the replacement graph and then swaps the underlying graph
     with a frozen version of the replacement graph (this maintains the
     invariant that the underlying graph is immutable).
     """
     self._validate(replacement_graph)
     self._graph = nx.freeze(replacement_graph)
Esempio n. 30
0
    def __init__(self, graph: str or networkx.Graph):
        if isinstance(graph, networkx.Graph):
            nxgraph = networkx.Graph(graph)
        elif isinstance(graph, str):
            nxgraph = phasme.build_graph.graph_from_file(graph)
        else:
            raise ValueError("Unexpected {}".format(graph))
        # internal graph representation
        self.__edges = map(frozenset, nxgraph.edges)
        if const.TEST_INTEGRITY:
            self.__edges = tuple(self.__edges)
            for args in self.__edges:
                if len(args) > 2:
                    print(
                        'WARNING: Weird edge: {}. It will be filtered.'.format(
                            args))
                else:
                    assert len(args) == 2, args
        self.__edges = set(edge for edge in self.__edges if len(edge) == 2)

        # data
        self.__nodes = set(nxgraph.nodes)
        self.__uid = str(min(self.__nodes, key=str))
        self.__nb_node = len(self.__nodes)
        self.__nb_cc = networkx.number_connected_components(nxgraph)
        self._nxgraph = nxgraph if constants.KEEP_NX_GRAPH else networkx.freeze(
            nxgraph)
        self.__initial_number_of_edge = len(self.__edges)
        self.__active_recipe = None

        self.__hierarchy = set()  # inclusions between powernodes
        self.__powernodes = defaultdict(
            set)  # (step, set) -> {node in powernode}
        self.__poweredges = defaultdict(set)  # (step, set) -> (step, set)
Esempio n. 31
0
    def plan(self):
        """ Plan execution order of functions, along with initial conditions to check

        Chainable.
        """
        if len(self._graph) == 0:
            logger.warning('Planning an empty graph!')
        if self._is_planned:
            logger.info('Graph is already planned. Skipping...')
            return

        sorted_graph = dag.topological_sort(self._graph)

        initial_nodes = set()
        execution_plan = []

        for nm in sorted_graph:
            node = self[nm]
            if not node.is_complete:
                initial_nodes.add(node.name)
            else:
                execution_plan.append(node)

        self.initial_nodes = initial_nodes
        self.execution_plan = execution_plan
        self._graph = nx.freeze(self._graph)
        self._is_planned = True
        return self
Esempio n. 32
0
def subgraph_view(G, filter_node=no_filter, filter_edge=no_filter):
    newG = nx.freeze(G.__class__())
    newG._NODE_OK = filter_node
    newG._EDGE_OK = filter_edge

    # create view by assigning attributes from G
    newG._graph = G
    newG.graph = G.graph

    newG._node = FilterAtlas(G._node, filter_node)
    if G.is_multigraph():
        Adj = FilterMultiAdjacency

        def reverse_edge(u, v, k): return filter_edge(v, u, k)
    else:
        Adj = FilterAdjacency

        def reverse_edge(u, v): return filter_edge(v, u)
    if G.is_directed():
        newG._succ = Adj(G._succ, filter_node, filter_edge)
        newG._pred = Adj(G._pred, filter_node, reverse_edge)
        newG._adj = newG._succ
    else:
        newG._adj = Adj(G._adj, filter_node, filter_edge)
    return newG
Esempio n. 33
0
def generic_graph_view(G, create_using=None):
    if create_using is None:
        newG = G.__class__()
    else:
        newG = nx.empty_graph(0, create_using)
    if G.is_multigraph() != newG.is_multigraph():
        raise NetworkXError("Multigraph for G must agree with create_using")
    newG = nx.freeze(newG)

    # create view by assigning attributes from G
    newG._graph = G
    newG.graph = G.graph

    newG._node = G._node
    if newG.is_directed():
        if G.is_directed():
            newG._succ = G._succ
            newG._pred = G._pred
            newG._adj = G._succ
        else:
            newG._succ = G._adj
            newG._pred = G._adj
            newG._adj = G._adj
    elif G.is_directed():
        if G.is_multigraph():
            newG._adj = UnionMultiAdjacency(G._succ, G._pred)
        else:
            newG._adj = UnionAdjacency(G._succ, G._pred)
    else:
        newG._adj = G._adj
    return newG
Esempio n. 34
0
    def clean_up(self):
        if nx.is_frozen(self.G):
            return

        if self.use_virtual_nodes:
            if self.verbose > 0:
                print("Remove virtual nodes")

                print("\nCurrent graph:")
                print("Nodes: {}".format(self.G.number_of_nodes()))
                print("Edges: {}".format(self.G.number_of_edges()))

            self.G.remove_nodes_from(self.virtual_nodes)
            self.assignments = np.delete(self.assignments, self.virtual_nodes)
            self.fixed = np.delete(self.fixed, self.virtual_nodes)

            if self.verbose > 0:
                print("\nVirtual nodes removed:")
                print("Nodes: {}".format(self.G.number_of_nodes()))
                print("Edges: {}".format(self.G.number_of_edges()))

        # Add partition attribute to nodes
        for i in range(0, len(self.assignments)):
            self.G.add_nodes_from([i], partition=str(self.assignments[i]))

        # Remove original node/edge weights
        for node in self.G.nodes_iter(data=True):
            if 'weight_orig' in node[1]:
                del node[1]['weight_orig']
        for edge in self.G.edges_iter(data=True):
            if 'weight_orig' in edge[2]:
                del edge[2]['weight_orig']

        # Freeze Graph from further modification
        self.G = nx.freeze(self.G)
Esempio n. 35
0
def main():
  seed(0) #set seed
  #get graph info
  G = nx.read_gpickle("input/graphMTC_CentroidsLength6.gpickle") #noCentroidsLength15.gpickle") #does not have centroidal links. There is also the choice of a proper multidigraph: nx.read_gpickle("input/graphMTC_CentroidsLength5.gpickle")
  G = nx.freeze(G) #prevents edges or nodes to be added or deleted
  #get od info. This is in format of a dict keyed by od, like demand[sd1][sd2] = 200000.
  demand = bd.build_demand('input/BATS2000_34SuperD_TripTableData.csv', 'input/superdistricts_centroids.csv')
  #get earthquake info
  q = QuakeMaps('input/20130210_mtc_total_lnsas3.pkl', 'input/20130210_mtc_magnitudes3.pkl', 'input/20130210_mtc_faults3.pkl', 'input/20130210_mtc_weights3.pkl', 'input/20130210_mtc_scenarios3.pkl') #input/20130107_mtc_total_lnsas1.pkl', 'input/20130107_mtc_magnitudes1.pkl','input/20130107_mtc_faults1.pkl', 'input/20130107_mtc_weights1.pkl', 'input/20130107_mtc_scenarios1.pkl') #'input/20130210_mtc_total_lnsas3.pkl', 'input/20130210_mtc_magnitudes3.pkl', 'input/20130210_mtc_faults3.pkl', 'input/20130210_mtc_weights3.pkl', 'input/20130210_mtc_scenarios3.pkl') #('input/20130107_mtc_total_lnsas1.pkl', 'input/20130107_mtc_magnitudes1.pkl',  #totalfilename=None, magfilename=None, faultfilename=None, weightsfilename=None, scenariofilename=None):
  print 'weights: ', q.weights
  q.num_sites = len(q.lnsas[0])
  #determine which scenarios you want to run
  good_indices = pick_scenarios(q.lnsas, q.weights)
  
  travel_index_times = []
  index = 0
  #loop over scenarios
  print 'size of lnsas: ', len(q.lnsas)
  for scenario in q.lnsas: #each 'scenario' has 1557 values of lnsa, i.e. one per site
    if index in good_indices:
      print 'index: ', index
      (bridges, flow, path, path2) = run_simple_iteration(G, scenario, demand, False)
      travel_index_times.append((index, bridges, flow, path, path2))
#      print 'new travel times: ', travel_index_times
      if index%1000 ==0:
        util.write_2dlist(time.strftime("%Y%m%d")+'_bridges_flow_paths4.txt',travel_index_times)
    index += 1 #IMPORTANT
  util.write_2dlist(time.strftime("%Y%m%d")+'_bridges_flow_paths4.txt',travel_index_times)
  print 'the number of scenarios I considered doing: ', index
  print 'the number of scenarios I actually did: ', len(travel_index_times)
Esempio n. 36
0
    def __init__(self, nodes, segments, points):
        graph = nx.Graph()

        nodes = self.parse_nodes_file(nodes)
        for nid, row in nodes.iterrows():
            coord = (row["Z Coord"], row["Y Coord"], row["X Coord"])
            graph.add_node(nid, coord=coord)

        segments = self.parse_segments_file(segments)
        points = self.parse_points_file(points)
        for _, row in segments.iterrows():
            point_coords = []
            for pid in row["Point IDs"]:
                point_coords.append((
                    points["Z Coord"][pid],
                    points["Y Coord"][pid],
                    points["X Coord"][pid],
                ))
            graph.add_edge(row["Node ID #1"],
                           row["Node ID #2"],
                           points=point_coords)

        self.graph = nx.freeze(graph)

        self._rebuild_mito_relationship()
Esempio n. 37
0
def spec2graph(spec: ConcreteSpec, qdd=False, dprob=None) -> nx.DiGraph:
    dfa = spec._as_dfa(qdd=qdd)

    if dprob is None:
        dprob = fn.constantly(None)

    def is_sink(state) -> bool:
        return state.node.var is None

    def is_decision(state) -> bool:
        lvl = state.node.level
        if qdd:
            lvl -= state.debt
        return spec.order.is_decision(lvl)

    def key(state):
        return (state.ref, state.debt) if qdd else state.ref

    sinks = set()
    g = nx.DiGraph()

    @fn.memoize
    def _node(state):
        decision = is_decision(state) and not is_sink(state)
        lvl = state.node.level
        var = state.label() if state.node.var is None else state.node.var
        node = key(state)
        g.add_node(node, lvl=lvl, var=var, decision=decision)
        return node

    stack = [dfa.start]
    while len(stack) > 0:
        state = stack.pop()
        action2succ = {a: dfa._transition(state, a) for a in dfa.inputs}

        for action, succ in action2succ.items():
            if key(succ) not in g.nodes:
                stack.append(succ)

            if succ == state:  # Sink
                sinks.add(_node(succ))
                continue

            if qdd and state.debt > 0:
                g.add_edge(_node(state), _node(succ), action=None, prob=1)
            else:
                if is_decision(state):
                    prob = dprob(key(state), action)
                else:
                    prob = 1/2

                g.add_edge(_node(state), _node(succ), action=action, prob=prob)

    g.add_node("DUMMY", lvl=None, var=None, decision=False)
    for sink in sinks:
        g.add_edge(sink, "DUMMY", action=None, prob=1)

    g = nx.freeze(g)
    return g, _node(dfa.start), list(sinks)
Esempio n. 38
0
def get_graph():
	import networkx
	'''loads full mtc highway graph with dummy links and then adds a few fake centroidal nodes for max flow and traffic assignment'''
	G = networkx.read_gpickle("input/graphMTC_CentroidsLength3int.gpickle")
	G = add_superdistrict_centroids(G)
	assert not G.is_multigraph() # Directed! only one edge between nodes
	G = networkx.freeze(G) #prevents edges or nodes to be added or deleted
	return G
Esempio n. 39
0
    def __init__(self, G, theta, eps, max_comms=100):
        self._G = nx.freeze(G)
        self._theta = theta
        self._max_comms = max_comms
        self._log = []
        self._fitness_list = []

        self._eps = eps
Esempio n. 40
0
 def __init__(self, d):
     super(Spec, self).__init__()
     self.graph = nx.Graph()
     self._load_switches(d)
     self._load_hosts(d)
     self._load_links(d)
     self.graph = nx.freeze(self.graph)
     self._check_sanity()
Esempio n. 41
0
def get_graph():
  import networkx
  '''loads full mtc highway graph with dummy links and then adds a few fake centroidal nodes for max flow and traffic assignment'''
  G = networkx.read_gpickle("input/graphMTC_CentroidsLength3int.gpickle")
  G = add_superdistrict_centroids(G)
   # Directed! only one edge between nodes
  G = nx.freeze(G) #prevents edges or nodes to be added or deleted
  return G
Esempio n. 42
0
    def create_test_nw(cls, req_cores=3, req_memory=3000, req_rate=15000):
        """create a random network."""
        g = nx.Graph()
        g.add_node("Node_0", cores=req_cores, memory=req_memory)
        g.add_node("Node_1", cores=req_cores, memory=req_memory)
        g.add_edge("Node_0", "Node_1", rate=req_rate)

        return cls(nx.freeze(g))
Esempio n. 43
0
 def graph(self):
     if self._subgraph is not None:
         return self._subgraph
     if self._target is None:
         return self._graph
     nodes = [self._target]
     nodes.extend(dst for _src, dst in
                  traversal.dfs_edges(self._graph.reverse(), self._target))
     self._subgraph = nx.freeze(self._graph.subgraph(nodes))
     return self._subgraph
Esempio n. 44
0
 def flatten(self):
     """Flattens a item (a task or flow) into a single execution graph."""
     if self._graph is not None:
         return self._graph
     self._pre_flatten()
     graph = self._flatten(self._root)
     self._post_flatten(graph)
     if self._freeze:
         self._graph = nx.freeze(graph)
     else:
         self._graph = graph
     return self._graph
Esempio n. 45
0
 def test_freeze(self):
     G=networkx.freeze(self.G)
     assert_equal(G.frozen,True)
     assert_raises(networkx.NetworkXError, G.add_node, 1)
     assert_raises(networkx.NetworkXError, G.add_nodes_from, [1])
     assert_raises(networkx.NetworkXError, G.remove_node, 1)
     assert_raises(networkx.NetworkXError, G.remove_nodes_from, [1])
     assert_raises(networkx.NetworkXError, G.add_edge, 1,2)
     assert_raises(networkx.NetworkXError, G.add_edges_from, [(1,2)])
     assert_raises(networkx.NetworkXError, G.remove_edge, 1,2)
     assert_raises(networkx.NetworkXError, G.remove_edges_from, [(1,2)])
     assert_raises(networkx.NetworkXError, G.clear)
Esempio n. 46
0
def main():
  '''can change the number of epsilons below'''
  seed(0) #set seed
  simple = False  #simple is just %bridges out, which is computationally efficient
  number_of_highway_bridges = 1743
  numeps = 3 #the number of epsilons
  tol = 0.00001 #the minimum annual rate that you care about in the original event set (the weight now is the original annual rate / number of epsilons per event)
  demand = bd.build_demand('input/BATS2000_34SuperD_TripTableData.csv', 'input/superdistricts_centroids.csv') #we just take a percentage in ita.py, namely  #to get morning flows, take 5.3% of daily driver values. 11.5/(4.5*6+11.5*10+14*4+4.5*4) from Figure S10 of http://www.nature.com/srep/2012/121220/srep01001/extref/srep01001-s1.pdf
  #figure out ground motions
  lnsas, weights = ground_motions(numeps, tol, '/Users/mahalia/Documents/matlab/Research/Herbst2011/output_data/SF2_mtc_total_3909scenarios_1743bridgesPlusBART_3eps.txt')
  bart_dict = transit_to_damage.make_bart_dict()
  muni_dict = transit_to_damage.make_muni_dict()
  set_main_path('/Users/mahaliamiller/Desktop/trn/transit_lines/', None) #TODO: need to change THREE file paths (these plus bart)

  print 'the number of ground motion events we are considering: ', len(lnsas)
  index = 0
  bridge_array = []
  travel_index_times = []

  # G = nx.read_gpickle("input/graphMTC_noCentroidsLength15.gpickle")
  G = nx.read_gpickle("input/graphMTC_CentroidsLength6.gpickle")
   # Directed! only one edge between nodes
  G = nx.freeze(G) #prevents edges or nodes to be added or deleted
  print 'am I a multi graph? ', G.is_multigraph()
  no_damage_travel_time, no_damage_vmt = compute_tt_vmt(G, demand)
  if not os.path.isdir(time.strftime("%Y%m%d")+'_filesForCube/'):
    os.mkdir(time.strftime("%Y%m%d")+'_filesForCube/')
  if not os.path.isdir(time.strftime("%Y%m%d")+'_filesForCube/transit/'):
    os.mkdir(time.strftime("%Y%m%d")+'_filesForCube/transit/')
  if not os.path.isdir(time.strftime("%Y%m%d")+'_filesForCube/modCapacities/'):
    os.mkdir(time.strftime("%Y%m%d")+'_filesForCube/modCapacities/')

  for scenario in lnsas:
    print index
    #figure out bridge damage for each scenario
    damaged_bridges, num_bridges_out = damage_bridges(scenario) #e.g., [1, 89, 598] #num_bridges_out is highway bridges only
    bridge_array.append(damaged_bridges)

    #figure out network damage and output Cube files to this effect
    G = damage_network(damaged_bridges, G, time.strftime("%Y%m%d")+'_filesForCube/', index)

    #figure out impact (performance metrics)
    flow, shortest_paths, travel_time, vmt = measure_performance(G, damaged_bridges, demand, no_damage_travel_time, no_damage_vmt)
    travel_index_times.append((index, num_bridges_out, flow, shortest_paths, travel_time, vmt, num_bridges_out/float(number_of_highway_bridges)))
    G = util.clean_up_graph(G)
    index +=1

    # if index%3909 == 0:
    if index%100 == 0:
      save_results(bridge_array, travel_index_times, int(index/float(3909)))

  test(numeps, lnsas, damaged_bridges, damaged_graph, num_bridges_out, flow, shortest_paths, travel_time, vmt)
Esempio n. 47
0
def flatten(item, freeze=True):
    graph = _flatten(item, set())
    if freeze:
        # Frozen graph can't be modified...
        return nx.freeze(graph)
    return graph
Esempio n. 48
0
 def _freeze(self):
     self._g = nx.freeze(self._g)
     return self
Esempio n. 49
0
def make_graph(loglines,
               tag_map,
               id_map,
               time_weighting,
               adjacent_logline_edge_weight=1.0,
               logline_id_edge_weight=1.0,
               logline_tag_edge_weight=1.0):
    """
    Creates a digraph from the argument data.

    The graph that is created has the following structure:
      o LogLine nodes have one edge to their previous LogLine
        and one to its next Logline. This is defined by their position
        in the loglines array argument.
      o LogLine nodes have one DATA_TO_META edge to every id node, based upon
        the entry in the id_map argument.
      o LogLine nodes have one DATA_TO_META edge to each tag node.

      o Id nodes have one META_TO_DATA edge to each of their LogLine nodes.

      o Tag nodes have one META_TO_DATA edge to each of their LogLine nodes.
      o Tag nodes each have two META_TO_META edges -- one to the 'previous'
        tag node and one to the 'next' tag node. 'next' and 'previous' are
        defined by the TagVertex.time field.

      o The graph is directed, but every directed edge has a corresponding
        directed edge -- if an edge (u,v) exists then (v,u) (of some type)
        exists.

    TODO(trevor) once we're further along and we like this graph take a few
    minutes and make a asciigraph example.

    Args:
        loglines see vinge.parser.parse_log return value
        tag_map see vinge.parser.parse_log return value
        id_map see vinge.parser.parse_log return value
        time_weighting (fun (datetime.datetime, datetime.datetime) -> float)
            function defining the weight between tag nodes
        adjacent_logline_edge_weight (float)
        logline_id_edge_weight (float)
        logline_tag_edge_weight (float)

    Returns:
        networkx.DiGraph
    """

    g = nx.DiGraph()
    g.add_nodes_from(loglines)

    oldll = None
    for ll in loglines:
        if oldll is not None:
            g.add_edge(oldll, ll, weight=adjacent_logline_edge_weight,
                       edge_type=EdgeType.ADJACENT_NEXT)
            g.add_edge(ll, oldll, weight=adjacent_logline_edge_weight,
                       edge_type=EdgeType.ADJACENT_PREV)
        oldll = ll

    for id in id_map:
        v = UniqueIDVertex(id)
        g.add_node(v)
        for ll in id_map[id]:
            g.add_edge(ll, v, weight=logline_id_edge_weight,
                       edge_type=EdgeType.DATA_TO_META)
            g.add_edge(v, ll, weight=logline_id_edge_weight,
                       edge_type=EdgeType.META_TO_DATA)


    for tag in tag_map:
        v, oldv = None, None

        the_tag_occurrences = sorted(list(tag_map[tag]))
        for ll in the_tag_occurrences:
            oldv = v
            v = TagVertex(tag, ll.time)
            g.add_node(v)

            # add edges between adjacent tag vertices, with
            # weight based on how far apart the times are
            if oldv is not None:
                wt = time_weighting(v.time, oldv.time)
                g.add_edge(v, oldv, weight=wt, edge_type=EdgeType.META_TO_META)
                g.add_edge(oldv, v, weight=wt, edge_type=EdgeType.META_TO_META)

            g.add_edge(ll, v, weight=logline_tag_edge_weight,
                       edge_type=EdgeType.DATA_TO_META)
            g.add_edge(v, ll, weight=logline_tag_edge_weight,
                       edge_type=EdgeType.META_TO_DATA)
                
    # Normalize edge weights
    normalize_graph(g)

    # Add the vertex index to all nodes
    for i, node in enumerate(g.nodes_iter()):
        node._set_idx(i)
        
    g = nx.freeze(g)
    return g
Esempio n. 50
0
            howdeep(each_item,h)
    else:
        pass

reader=csv.reader(file('C:/nx/networkx/sing.csv','rb'))
#构图
for row in reader:
        if reader.line_num ==1:
                continue
        G.add_edge(row[0].decode('utf-8'),row[1].decode('utf-8'))

 #返回G中节点权重,并按排序输出结果
weight_g = sorted(G.degree().values())
for each_node in G.nodes():
         #找到邻居数大于设定值的节点i
        if nx.degree(G)[each_node] >= weight_g[-rang]:
                 #print G.neighbors(each_item)
                 howdeep(each_node,deep)
                 '''
                 #生成与i直接连接的图
                for each_one in G.neighbors(each_node):
                        DG.add_edge(each_one,each_node)
                        for each_neighbor in G.neighbors(each_one):
                                DG.add_edge(each_neighbor,each_one)
'''

pos = nx.spring_layout(G)
nx.draw_networkx(DG,pos,node_size=1000,font_size=8)
nx.freeze(G)
plt.show()
Esempio n. 51
0
 def __init__(self, name):
     super(Flow, self).__init__(name)
     self._graph = nx.freeze(nx.DiGraph())
Esempio n. 52
0
	def _make_left_corner(self):
		g = nx.DiGraph()
		for r in self.grammar:
			g.add_edge(r.lhs,r.rhs[0])
		return nx.freeze(g)
Esempio n. 53
0
def main():
  '''can change the number of epsilons below'''
  seed(0) #set seed
  simple = False #False #simple is just %bridges out, which is computationally efficient
  #get graph info
  # G = nx.read_gpickle("input/graphMTC_CentroidsLength6.gpickle") #noCentroidsLength15.gpickle") #does not have centroidal links. There is also the choice of a proper multidigraph: nx.read_gpickle("input/graphMTC_CentroidsLength5.gpickle")
  G = nx.read_gpickle("input/graphMTC_CentroidsLength6highways.gpickle") #noCentroidsLength15.gpickle") #does not have centroidal links. Directed! only one edge between nodes
  # G1 = nx.read_gpickle("input/graphMTC_CentroidsLength5.gpickle") #undirected, multiple edges. It is a little funky because it has two links between A and B and two between B and A so is that double-counting?
  # '''a multigraph: An undirected graph class that can store multiedges.
  #   Multiedges are multiple edges between two nodes.  Each edge
  #   can hold optional data or attributes.
  #   A MultiGraph holds undirected edges.  Self loops are allowed.'''
  print 'nodes: ', len(G.nodes())
  G = nx.freeze(G) #prevents edges or nodes to be added or deleted
  # G1 = nx.freeze(G1)
  #get od info. This is in format of a dict keyed by od, like demand[sd1][sd2] = 200000.
  demand = bd.build_demand('input/BATS2000_34SuperD_TripTableData.csv', 'input/superdistricts_centroids.csv') #we just take a percentage in ita.py, namely  #to get morning flows, take 5.3% of daily driver values. 11.5/(4.5*6+11.5*10+14*4+4.5*4) from Figure S10 of http://www.nature.com/srep/2012/121220/srep01001/extref/srep01001-s1.pdf
          #get path
  #get earthquake info #UPDATED May 23, 2013
  #TODO
  q = QuakeMaps('input/20130612_mtc_total_lnsas5.pkl', 'input/20130612_mtc_magnitudes5.pkl', 'input/20130612_mtc_faults5.pkl', 'input/20130612_mtc_weights5.pkl', 'input/20130612_mtc_scenarios5.pkl') #input/20130107_mtc_total_lnsas1.pkl', 'input/20130107_mtc_magnitudes1.pkl','input/20130107_mtc_faults1.pkl', 'input/20130107_mtc_weights1.pkl', 'input/20130107_mtc_scenarios1.pkl') #'input/20130210_mtc_total_lnsas3.pkl', 'input/20130210_mtc_magnitudes3.pkl', 'input/20130210_mtc_faults3.pkl', 'input/20130210_mtc_weights3.pkl', 'input/20130210_mtc_scenarios3.pkl') #('input/20130107_mtc_total_lnsas1.pkl', 'input/20130107_mtc_magnitudes1.pkl',  #totalfilename=None, magfilename=None, faultfilename=None, weightsfilename=None, scenariofilename=None):
  q.num_sites = len(q.lnsas[0])
  numeps = 5 #CAHNGE THIS CHANGE THIS!!!!!!!!
  #determine which scenarios you want to run
  good_indices = pick_scenarios(q.lnsas, q.weights,True, numeps)
  targets = good_indices #[12, 35, 55, 71, 75, 82, 86, 87, 88, 106, 108, 115, 121, 231, 241, 247, 256, 258, 260, 261, 676, 730, 733, 1231, 1548] #indices between 0 and 2110. the scenarios for which you want to save the damaged bridge data
  print 'the number of scenarios for which I want to save bridge info: ', len(targets)

  travel_index_times = []
  index = 0
  good_index = 0
  # pdb.set_trace()
  #figure out what the travel time and vmt are if no damage to any bridges
  no_damage_travel_time = -1
  no_damage_vmt = -1
  found_no_damage = False
  for scenario in q.lnsas: #each 'scenario' has 1xxx values of lnsa, i.e. one per site
    while found_no_damage == False:
      (bridges, flow, path, path2, newG) = run_simple_iteration(G, scenario, demand, False, good_index, targets, True) #since looking for no damage case, it is ok to clean up
      if bridges == 0:
        found_no_damage = True
        print 'found case with no damage so I will save those and save you work later on'
        (no_damage_travel_time, no_damage_vmt) = run_iteration(G, scenario, demand, newG)

  #loop over scenarios
  print 'size of lnsas: ', len(q.lnsas)
  for scenario in q.lnsas: #each 'scenario' has 1xxx values of lnsa, i.e. one per site
    if index in good_indices:
      print 'index: ', index
      if simple == True:
        (bridges, flow, path, path2, newG) = run_simple_iteration(G, scenario, demand, False, good_index, targets)
        travel_index_times.append((index, bridges, flow, path, path2, -1, -1, bridges/float(q.num_sites), -1))
      else:
        (bridges, flow, path, path2, newG) = run_simple_iteration(G, scenario, demand, False, good_index, targets, False) #doesn't clean up the damage
        print 'what i found for bridges: ', bridges
        if bridges == 0:
          travel_time = no_damage_travel_time; 
          vmt = no_damage_vmt; 
        else:
          print 'attempting new'
          (travel_time, vmt) = run_iteration(G, scenario, demand, newG, True)
        print 'what i have for (tt, vmt): ', (travel_time, vmt)
        travel_index_times.append((index, bridges, flow, path, path2, travel_time, vmt, bridges/float(q.num_sites), -1))
      good_index += 1
        # travel_index_times.append((index, travel_time, vmt))
#      print 'new travel times: ', travel_index_times
    if index%1000 ==0:
      print 'index: ', index
      util.write_2dlist(time.strftime("%Y%m%d")+'_bridges_flow_paths_5eps_extensive.txt',travel_index_times)
    index += 1 #IMPORTANT
  util.write_2dlist(time.strftime("%Y%m%d")+'_bridges_flow_paths_5eps_extensive.txt',travel_index_times)
  print 'the number of scenarios I considered doing: ', index
  print 'the number of scenarios I actually did: ', len(travel_index_times)
  print 'i.e.: ', good_index
  print 'and now, I will save a dataset of damaged bridges in each scenario'
  util.write_2dlist(time.strftime("%Y%m%d")+'_damaged_bridges_5eps_extensive.txt',BRIDGE_DAMAGE_DATASET)
  with open(time.strftime("%Y%m%d")+'_damaged_bridges_5eps_extensive.pkl', 'wb') as f:
    pickle.dump(BRIDGE_DAMAGE_DATASET, f)
Esempio n. 54
0
 def freeze(self):
     """Freezes the graph so that no more mutations can occur."""
     if not self.frozen:
         nx.freeze(self)
     return self
    assignments = np.delete(assignments, virtual_nodes)
    fixed = np.delete(fixed, virtual_nodes)

    print("\nVirtual nodes removed:")
    print("Nodes: {}".format(G.number_of_nodes()))
    print("Edges: {}".format(G.number_of_edges()))


# In[10]:

# Add partition attribute to nodes
for i in range(0, len(assignments)):
    G.add_nodes_from([i], partition=str(assignments[i]))

# Freeze Graph from further modification
G = nx.freeze(G)


# In[11]:

import os
import datetime

timestamp = datetime.datetime.now().strftime('%H%M%S')
data_filename,_ = os.path.splitext(os.path.basename(DATA_FILENAME))
data_filename += "-" + timestamp

graph_metrics = {
    "file": timestamp,
    "num_partitions": num_partitions,
    "num_iterations": num_iterations,
Esempio n. 56
0
 def freeze(self):
     nx.freeze(self)
Esempio n. 57
0
def contour(m, levels, nest=False, degrees=False, simplify=True):
    import pkg_resources
    try:
        pkg_resources.require('healpy >= 1.9.0')
    except:
        raise RuntimeError('This function requires healpy >= 1.9.0.')
    try:
        import networkx as nx
    except:
        raise RuntimeError('This function requires the networkx package.')

    # Determine HEALPix resolution
    npix = len(m)
    nside = hp.npix2nside(npix)
    min_area = 0.4 * hp.nside2pixarea(nside)

    # Compute faces, vertices, and neighbors.
    # vertices is an N X 3 array of the distinct vertices of the HEALPix faces.
    # faces is an npix X 4 array mapping HEALPix faces to their vertices.
    # neighbors is an npix X 4 array mapping faces to their nearest neighbors.
    faces = np.ascontiguousarray(
            np.rollaxis(hp.boundaries(nside, np.arange(npix), nest=nest), 2, 1))
    dtype = faces.dtype
    faces = faces.view(np.dtype((np.void, dtype.itemsize * 3)))
    vertices, faces = np.unique(faces.ravel(), return_inverse=True)
    faces = faces.reshape(-1, 4)
    vertices = vertices.view(dtype).reshape(-1, 3)
    neighbors = hp.get_all_neighbours(nside, np.arange(npix), nest=nest)[::2].T

    # Loop over the requested contours.
    paths = []
    for level in levels:

        # Find credible region
        indicator = (m >= level)

        # Construct a graph of the eges of the contour.
        graph = nx.Graph()
        face_pairs = set()
        for ipix1, ipix2 in enumerate(neighbors):
            for ipix2 in ipix2:
                # Determine if we have already considered this pair of faces.
                new_face_pair = frozenset((ipix1, ipix2))
                if new_face_pair in face_pairs: continue
                face_pairs.add(new_face_pair)

                # Determine if this pair of faces are on a boundary of the
                # credible level.
                if indicator[ipix1] == indicator[ipix2]: continue

                # Add all common edges of this pair of faces.
                i1 = np.concatenate((faces[ipix1], [faces[ipix1][0]]))
                i2 = np.concatenate((faces[ipix2], [faces[ipix2][0]]))
                edges1 = frozenset(frozenset(_) for _ in zip(i1[:-1], i1[1:]))
                edges2 = frozenset(frozenset(_) for _ in zip(i2[:-1], i2[1:]))
                for edge in edges1 & edges2:
                    graph.add_edge(*edge)
        graph = nx.freeze(graph)

        # Record a closed path for each cycle in the graph.
        cycles = [
            np.take(vertices, cycle, axis=0) for cycle in nx.cycle_basis(graph)]

        # Simplify paths if requested
        if simplify:
            cycles = [_simplify(cycle, min_area) for cycle in cycles]
            cycles = [cycle for cycle in cycles if len(cycle) > 2]

        # Convert to lists
        cycles = [
            _vec2radec(cycle, degrees=degrees).tolist() for cycle in cycles]

        # Add to output paths
        paths.append([cycle + [cycle[0]] for cycle in cycles])

    return paths
Esempio n. 58
0
 def test_is_frozen(self):
     assert_equal(networkx.is_frozen(self.G), False)
     G=networkx.freeze(self.G)
     assert_equal(G.frozen, networkx.is_frozen(self.G))
     assert_equal(G.frozen,True)