def test_pole_node(self): d = util.geographical_distance(90, 30, 40, 90) self.assertGreater(d, 0)
def parse_topology_zoo(path): """ Parse a topology from the Topology Zoo dataset. Parameters ---------- path : str The path to the Topology Zoo file Returns ------- topology : Topology or DirectedTopology The parsed topology. Notes ----- If the parsed topology contains bundled links, i.e. multiple links between the same pair or nodes, the topology is parsed correctly but each bundle of links is represented as a single link whose capacity is the sum of the capacities of the links of the bundle (if capacity values were provided). The returned topology has a boolean attribute named *link_bundling* which is True if the topology contains at list one bundled link or False otherwise. If the topology contains bundled links, then each link has an additional boolean attribute named *bundle* which is True if that specific link was bundled in the original topology or False otherwise. """ def try_convert_int(value): """ Try to convert a string to an int. If not possible, returns the given value unchanged """ if type(value) != int: try: value = int(value) except ValueError: pass return value if path.endswith('.gml'): topo_zoo_graph = nx.read_gml(path) elif path.endswith('.graphml'): topo_zoo_graph = nx.read_graphml(path) else: raise ValueError('Invalid input file format. It must either be a GML '\ 'or GraphML file (with extensions .gml or .graphml)') topology = DirectedTopology() if topo_zoo_graph.is_directed() \ else Topology() topology.graph['type'] = 'topology_zoo' topology.graph['distance_unit'] = 'Km' topology.graph['link_bundling'] = True if topo_zoo_graph.is_multigraph() \ else False for tv in topo_zoo_graph.nodes_iter(): v = try_convert_int(tv) topology.add_node(v) if 'label' in topo_zoo_graph.node[tv]: topology.node[v]['label'] = topo_zoo_graph.node[tv]['label'] try: longitude = topo_zoo_graph.node[tv]['Longitude'] latitude = topo_zoo_graph.node[tv]['Latitude'] topology.node[v]['longitude'] = longitude topology.node[v]['latitude'] = latitude except KeyError: pass for tv, tu in topo_zoo_graph.edges_iter(): v = try_convert_int(tv) u = try_convert_int(tu) if u == v: continue topology.add_edge(v, u) if 'Latitude' in topo_zoo_graph.node[tv] and \ 'Longitude' in topo_zoo_graph.node[tv] and \ 'Latitude' in topo_zoo_graph.node[tu] and \ 'Longitude' in topo_zoo_graph.node[tu]: lat_v = topo_zoo_graph.node[tv]['Latitude'] lon_v = topo_zoo_graph.node[tv]['Longitude'] lat_u = topo_zoo_graph.node[tu]['Latitude'] lon_u = topo_zoo_graph.node[tu]['Longitude'] length = geographical_distance(lat_v, lon_v, lat_u, lon_u) topology.edge[v][u]['length'] = length if topo_zoo_graph.is_multigraph(): edge = topo_zoo_graph.edge[tv][tu] topology.edge[v][u]['bundle'] = True if len(edge) > 1 else False capacity = 0 for edge_attr in list(edge.values()): if 'LinkSpeedRaw' in edge_attr: capacity += edge_attr['LinkSpeedRaw'] if capacity > 0: topology.edge[v][u]['capacity'] = capacity else: if 'LinkSpeedRaw' in topo_zoo_graph.edge[tv][tu]: topology.edge[v][u]['capacity'] = \ topo_zoo_graph.edge[tv][tu]['LinkSpeedRaw'] if len(nx.get_edge_attributes(topology, 'capacity')) > 0: topology.graph['capacity_unit'] = 'bps' return topology
def test_normal_case(self): d = util.geographical_distance(-30, -30, 30, 30) self.assertGreater(d, 0)
def parse_abilene(topology_path, links_path=None): """ Parse the Abilene topology. Parameters ---------- topology_path : str The path of the Abilene topology file links_path : str, optional The path of the Abilene links file Returns ------- topology : DirectedTopology """ topology = DirectedTopology(type='abilene', capacity_unit='kbps', distance_unit='Km') comment_char = '#' link_type_dict = {0: 'internal', 1: 'inbound', 2: 'outbound'} line_type = None for line in open(topology_path, "r").readlines(): if comment_char in line: # split on comment char, keep only the part before line, _ = line.split(comment_char, 1) line = line.strip() if len(line) > 0: if line == 'router' or line == 'link': line_type = line continue if line_type == 'router': node_entry = line.split('\t') try: name = node_entry[0] city = node_entry[1] latitude = float(node_entry[2]) longitude = float(node_entry[3]) except (ValueError, IndexError): raise ValueError('Invalid input file. Parsing failed '\ 'while trying to parse a router') topology.add_node(name, city=city, latitude=latitude, longitude=longitude) elif line_type == 'link': sep = re.compile('[\s\t]') link_entry = sep.split(line) try: u = link_entry[0] v = link_entry[1] capacity = int(link_entry[2]) lon_u = topology.node[u]['longitude'] lat_u = topology.node[u]['latitude'] lon_v = topology.node[v]['longitude'] lat_v = topology.node[v]['latitude'] length = geographical_distance(lat_v, lon_v, lat_u, lon_u) weight = int(link_entry[3]) except (ValueError, IndexError): raise ValueError('Invalid input file. Parsing failed '\ 'while trying to parse a link') topology.add_edge(u, v, capacity=capacity, weight=weight, length=length) else: raise ValueError('Invalid input file. Found a line that '\ 'I cannot interpret') if links_path: for line in open(links_path, "r").readlines(): if comment_char in line: # split on comment char, keep only the part before line, _ = line.split(comment_char, 1) line = line.strip() if len(line) > 0: sep = re.compile('[\s\t]') link_entry = sep.split(line) try: u, v = link_entry[0].split(',', 1) if u == '*' or v == '*': # ignore external links continue link_index = int(link_entry[1]) link_type = link_type_dict[int(link_entry[2])] except (ValueError, IndexError): raise ValueError('Invalid input file. '\ 'Parsing failed while trying to '\ 'parse a link from links_file') topology.edge[u][v]['link_index'] = link_index topology.edge[u][v]['link_type'] = link_type return topology
def parse_topology_zoo(path): """ Parse a topology from the Topology Zoo dataset. Parameters ---------- path : str The path to the Topology Zoo file Returns ------- topology : Topology or DirectedTopology The parsed topology. Notes ----- If the parsed topology contains bundled links, i.e. multiple links between the same pair or nodes, the topology is parsed correctly but each bundle of links is represented as a single link whose capacity is the sum of the capacities of the links of the bundle (if capacity values were provided). The returned topology has a boolean attribute named *link_bundling* which is True if the topology contains at list one bundled link or False otherwise. If the topology contains bundled links, then each link has an additional boolean attribute named *bundle* which is True if that specific link was bundled in the original topology or False otherwise. """ def try_convert_int(value): """ Try to convert a string to an int. If not possible, returns the given value unchanged """ if type(value) != int: try: value = int(value) except ValueError: pass return value if path.endswith('.gml'): topo_zoo_graph = nx.read_gml(path) elif path.endswith('.graphml'): topo_zoo_graph = nx.read_graphml(path) else: raise ValueError('Invalid input file format. It must either be a GML '\ 'or GraphML file (with extensions .gml or .graphml)') topology = DirectedTopology() if topo_zoo_graph.is_directed() \ else Topology() topology.graph['type'] = 'topology_zoo' topology.graph['distance_unit'] = 'Km' topology.graph['link_bundling'] = True if topo_zoo_graph.is_multigraph() \ else False for tv in topo_zoo_graph.nodes_iter(): v = try_convert_int(tv) topology.add_node(v) if 'label' in topo_zoo_graph.node[tv]: topology.node[v]['label'] = topo_zoo_graph.node[tv]['label'] try: longitude = topo_zoo_graph.node[tv]['Longitude'] latitude = topo_zoo_graph.node[tv]['Latitude'] topology.node[v]['longitude'] = longitude topology.node[v]['latitude'] = latitude except KeyError: pass for tv, tu in topo_zoo_graph.edges_iter(): v = try_convert_int(tv) u = try_convert_int(tu) if u == v: continue topology.add_edge(v, u) if 'Latitude' in topo_zoo_graph.node[tv] and \ 'Longitude' in topo_zoo_graph.node[tv] and \ 'Latitude' in topo_zoo_graph.node[tu] and \ 'Longitude' in topo_zoo_graph.node[tu]: lat_v = topo_zoo_graph.node[tv]['Latitude'] lon_v = topo_zoo_graph.node[tv]['Longitude'] lat_u = topo_zoo_graph.node[tu]['Latitude'] lon_u = topo_zoo_graph.node[tu]['Longitude'] length = geographical_distance(lat_v, lon_v, lat_u, lon_u) topology.edge[v][u]['length'] = length if topo_zoo_graph.is_multigraph(): edge = topo_zoo_graph.edge[tv][tu] if len(edge) == 1: topology.edge[v][u]['bundle'] = False if 'LinkSpeedRaw' in edge[0]: topology.edge[v][u]['capacity'] = \ edge[0]['LinkSpeedRaw'] else: topology.edge[v][u]['bundle'] = True capacity = 0 for edge_attr in list(edge.values()): if 'LinkSpeedRaw' in edge_attr: capacity += edge_attr['LinkSpeedRaw'] if capacity > 0: topology.edge[v][u]['capacity'] = capacity else: if 'LinkSpeedRaw' in topo_zoo_graph.edge[tv][tu]: topology.edge[v][u]['capacity'] = \ topo_zoo_graph.edge[tv][tu]['LinkSpeedRaw'] if len(nx.get_edge_attributes(topology, 'capacity')) > 0: topology.graph['capacity_unit'] = 'bps' return topology