def main(inname='submission.yaml'): try: submission = nx.read_yaml(inname) except ScannerError as se: print( 'Unable to read file: {} Please ensure file has properly formatted YAML.' .format(inname)) print(se) sys.exit(125) # No submission, case of an update to deprecated IDs if submission is None: indications = nx.read_yaml('indication_paths.yaml') indications = filter_deprecated_ids(indications) nx.write_yaml(indications, 'indication_paths.yaml', indent=4) print('Paths writen successfully with deprecated IDs removed') sys.exit(0) # Prep the submission fixing simple common errors and throwing excptions when not simple fixes submission = test_and_fix(submission) # Determine if Update or new submission has_identifier = ['_id' in rec['graph'].keys() for rec in submission] if all(has_identifier): update_existing_records(submission) elif any(has_identifier): print('Mixed submissions and updates are not allowed. Please ensure that either all records ' +\ 'contain an `_id` feild in the `graph` feild if updating, or none contain the `_id` ' +\ 'field if submitting new records') sys.exit(400) else: add_new_submission(submission)
def __init__(self): subs = rospy.Service('/pose2D', CreateMap, self.graph_map) goaltag = rospy.Service('/goalpose', GoalTag, self.getposegoal) sonar_subs = rospy.Subscriber('/sonar_distance', Float64, self.take_sonar_distance) self.G = nx.Graph() self.flag = 0 self.n_node = 0 self.past_node = 0 self.map_completed = rospy.get_param('/explore_map/map_completed', False) self.current_node = -1 self.chasing_goal = False self.goal_pose = -1 self.sonar_distance = 0 self.save_edge_value = [] self.path_saved_map = rospy.get_param('/explore_map/path_from_saved_map', "/home/"+getpass.getuser()+"/Desktop/mapa.yaml") self.load_saved_map = rospy.get_param('/explore_map/load_saved_map', False) erase_last_node = rospy.get_param('/explore_map/erase_last_node', False) if(self.load_saved_map): if(os.path.isfile(self.path_saved_map)): self.G = nx.read_yaml(self.path_saved_map) rospy.loginfo("Loaded map") if(erase_last_node): self.n_node = self.G.number_of_nodes() - 1 self.G.remove_node(self.n_node) else: self.n_node = self.G.number_of_nodes() self.past_node = self.n_node - 1 else: rospy.logerr("File cannot be load or not exist") sys.exit(0)
def _read_cell_graph(self, filename, format): """Load the cell-to-cell connectivity graph from a file. Returns None if any error happens. """ cell_graph = None if filename: try: start = datetime.now() if format == "gml": cell_graph = nx.read_gml(filename) elif format == "pickle": cell_graph = nx.read_gpickle(filename) elif format == "edgelist": cell_graph = nx.read_edgelist(filename) elif format == "yaml": cell_graph = nx.read_yaml(filename) elif format == "graphml": cell_graph = cell_graph = nx.read_graphml(filename) else: print "Unrecognized format:", format end = datetime.now() delta = end - start config.BENCHMARK_LOGGER.info( "Read cell_graph from file %s of format %s in %g s" % (filename, format, delta.seconds + 1e-6 * delta.microseconds) ) except Exception, e: print e
def test_update_graph(self): graph_name = self.generator.generate_cpt_graph(SMELL_TEST_PATH) graph = nx.read_yaml(f"{self.config.OUTPUT_DIR}/{graph_name}.yaml") self.smell.update_graph(graph) self.assertTrue(graph.nodes["Step:Self referencing concept"]["smell"]) self.assertEqual(graph.nodes["Step:Self referencing concept"]["smell_names"], ["Self referencing concept"])
def _read_celltype_graph(self, celltypes_file, format="gml"): """ Read celltype-celltype connectivity graph from file. celltypes_file -- the path of the file containing the graph. format -- format of the file. allowed values: gml, graphml, edgelist, pickle, yaml. """ start = datetime.now() celltype_graph = None try: if format == "gml": celltype_graph = nx.read_gml(celltypes_file) elif format == "edgelist": celltype_graph = nx.read_edgelist(celltypes_file) elif format == "graphml": celltype_graph = nx.read_graphml(celltypes_file) elif format == "pickle": celltype_graph = nx.read_gpickle(celltypes_file) elif format == "yaml": celltype_graph = nx.read_yaml(celltypes_file) else: print "Unrecognized format %s" % (format) except Exception, e: print e
def read_lib(lib_dir_path): """ read all library graphs """ library_dir_path = lib_dir_path lib_files = os.listdir(library_dir_path) if os.path.isfile("dont_use_cells.txt"): logger.debug("Reading Dont Use cells: dont_use_cells.txt") with open('dont_use_cells.txt') as dont_use_file: dont_use_library = dont_use_file.read().splitlines() else: dont_use_library = [] logger.debug("no dont use list defined") library = [] for sub_block_name in lib_files: graph = nx.read_yaml(library_dir_path + sub_block_name) if sub_block_name[:-5] not in dont_use_library: subgraph_ports = [] for node, attr in graph.nodes(data=True): if 'net' in attr['inst_type']: if 'external' in attr['net_type']: subgraph_ports.append(node) library.append({ "name": sub_block_name[:-5], "graph": graph, "ports": subgraph_ports, "conn": max_connectivity(graph) }) logger.debug(f"Read lib: {sub_block_name}, {subgraph_ports}") return sorted(library, key=lambda k: k['conn'], reverse=True)
def get_hindi_scanner(count=None): hindi_dg = nx.read_yaml('data/hindi_meter.yaml') hindi_scanner = CustomMeterGraph(count=count) hindi_scanner.DG = hindi_dg print hindi_scanner return hindi_scanner
def read_graph(filename, file_type=None, create_using=networkx.DiGraph): if not file_type: file_type = get_graph_type(filename) if not file_type: raise RuntimeError("Unable to determine graph file type.") if file_type == "adjlist": return networkx.read_adjlist(filename, nodetype=int, create_using=create_using()) elif file_type == "edgelist": return networkx.read_edgelist(filename, nodetype=int, create_using=create_using()) elif file_type == "gexf": return networkx.read_gexf(filename, nodetype=int) elif file_type == "gml": return networkx.read_gml(filename) elif file_type == "gpickle": return networkx.read_gpickle(filename) elif file_type == "graphml": return networkx.read_graphml(filename, nodetype=int) elif file_type == "yaml": return networkx.read_yaml(filename) elif file_type == "pajek": return networkx.read_pajek(filename) elif file_type == "adjmat": matrix = numpy.loadtxt(filename, delimiter=",") return networkx.from_numpy_matrix(matrix) else: raise RuntimeError("Unrecognized input graph file type (" + str(file_type) + ").")
def test_generate_spec_graph(self): output_file = self.generator.generate_spec_graph(TEST_SPEC_PATH) output_graph = nx.read_yaml(f"{OUTPUT_DIR}/{output_file}.yaml") nodes = list(output_graph.nodes()) self.assertEqual(len(nodes), 6) expected_nodes = [ 'Scenario:Scenario 1', 'Scenario:Scenario 2', 'Step:Step 1', 'Step:Step 2', 'Step:Step 3', 'Step:Step with $Variable$' ] expected_nodes.sort() nodes.sort() self.assertEqual(nodes, expected_nodes) edges = list(output_graph.edges()) expected_edges = [("Scenario:Scenario 1", "Step:Step 1"), ("Scenario:Scenario 1", "Step:Step 2"), ("Scenario:Scenario 1", "Step:Step 3"), ("Scenario:Scenario 2", "Step:Step with $Variable$"), ("Scenario:Scenario 2", "Step:Step 2"), ("Scenario:Scenario 2", "Step:Step 3")] edges.sort(key=lambda edge: edge[0] + edge[1]) expected_edges.sort(key=lambda edge: edge[0] + edge[1]) self.assertEqual(edges, expected_edges)
def __init__(self, prob_util, directed_graph_yaml, verbose=True): self.prob_util = prob_util self.verbose = verbose self.graph = nx.Graph() directed_graph = nx.read_yaml(directed_graph_yaml) max_fac = 0 cpds = [] self.rvs = [] for rv in directed_graph.nodes(): self.graph.add_node(rv, bipartite=0) dependencies = [e[0] for e in directed_graph.in_edges(rv)] cpd = CPD(rv, dependencies) cpds.append(cpd) max_fac = max(max_fac, len(cpd.allVars())) self.rvs.append(RandomVariable(rv)) if self.verbose: print('Creating %d factors (max factor size is %d)...' % \ (len(cpds), max_fac)) self.factors = [Factor(cpd, self.rvs, self.prob_util) for cpd in cpds] sorter = lambda factor: len(factor.rvs) self.factors = list(sorted(self.factors, key=sorter)) for factor in self.factors: self.graph.add_node(factor.key, bipartite=1) for rv in factor.rvs: self.graph.add_edge(factor.key, rv.key) rv.addNeighboringFactor(factor) if self.verbose: print('All %d factors initialized.' % len(self.factors))
def temporal(in_folder, big_folder, persThresh, consistThresh): #find mappers with persistence higher than 1 #use that list to go through each time slice #if more than 50% of the time slices where for the same part of the day - output the mapper and time slices B = nx.read_yaml(big_folder) users = B.nodes(data=True) pers_users = [u[0] for u in users if u[1]['persistence'] > persThresh] for u in pers_users: night = 0 day = 0 evenining = 0 persistence = 0 for file in os.listdir(in_folder): if file != '.DS_Store': #weird MAC thing path = in_folder + file G = getJsonNet(path) G = nx.DiGraph(G) if G.has_node(u): persistence += 1 period = str(file)[11:13] if period == '00': #03 for the old, wrong time night += 1 elif period == '08': #11 for the old, wrong time day += 1 else: evenining += 1 if night / float(persistence) > consistThresh: print u, 'night', night / float(persistence) elif day / float(persistence) > consistThresh: print u, 'day', day / float(persistence) elif evenining / float(persistence) > consistThresh: print u, 'evenining', evenining / float(persistence)
def run_comparison_large(): configuration.arc_weight_key = 'weight' configuration.max_iter = 1 configuration.pulse_primal_bound_factor = 1 file_path_prefix = path.join(path.dirname(__file__), '..', 'data/mdeo_large') graph = nx.read_yaml(f'{file_path_prefix}.yml') infrastructures = { 'cost_factors': [0.9, 0.5, 0.4], 'construction_cost_factors': [1, 4, 8], } demand = { ('14880', '87'): 500, ('6933', '16825'): 50, ('6988', '11437'): 100, ('16108', '6987'): 350, } budget = 15000 logging.info(f"""\n Running params: demand: {demand} budget: {budget} graph_file: {file_path_prefix} """) with open(f'{file_path_prefix}.dat', 'w') as data_file: export(data_file, graph, infrastructures, demand, budget) solution = solve(graph, infrastructures, demand, budget) solution.print()
def supervisor(big_folder): with open('/Users/Ish/Dropbox/OSM/results/Haiti/new_users', 'rb') as f: newList = pickle.load(f) B = nx.read_yaml(big_folder) initList = [] for e in B.edges_iter(): if (e[1] in newList) & (e[0] not in newList): initList.append( e[0]) #save the experienced user who's edited after a new user print e initList = list(set(initList)) #get rid of the duplicates highNewProp = [] for m in initList: #for each mapper in the initial list succ = B.successors(m) new_succ = 0 new_succList = [] for s in succ: if s in newList: new_succ += 1 new_succList.append(s) new_prop = new_succ / float(len(succ)) if new_prop > 0.5: highNewProp.append(m) highNewProp.append(new_succList) print highNewProp
def test_uptdate_graph(self): graph_name = self.generator.generate_spec_graph(SMELL_TEST_SPEC_PATH) graph = nx.read_yaml(f"{self.config.OUTPUT_DIR}/{graph_name}.yaml") self.smell.update_graph(graph) self.assertTrue(graph.nodes["Scenario:Long Test"]["smell"]) self.assertEqual(graph.nodes["Scenario:Long Test"]["smell_names"], ["Long Test: > 10 steps."])
def test_generate_concept_graph(self): output_file = self.generator.generate_cpt_graph(TEST_CPT_PATH) output_graph = nx.read_yaml(f"{OUTPUT_DIR}/{output_file}.yaml") nodes = list(output_graph.nodes()) self.assertEqual(len(nodes), 4) expected_nodes = [ "Step:Test Concept", "Step:Step 1", "Step:Step 2", "Step:Step 3" ] expected_nodes.sort() nodes.sort() self.assertEqual(nodes, expected_nodes) edges = list(output_graph.edges()) expected_edges = [("Step:Test Concept", "Step:Step 1"), ("Step:Test Concept", "Step:Step 2"), ("Step:Test Concept", "Step:Step 3")] edges.sort(key=lambda edge: edge[0] + edge[1]) expected_edges.sort(key=lambda edge: edge[0] + edge[1]) self.assertEqual(edges, expected_edges)
def _read_cell_graph(self, filename, format): """Load the cell-to-cell connectivity graph from a file. Returns None if any error happens. """ cell_graph = None if filename: try: start = datetime.now() if format == 'gml': cell_graph = nx.read_gml(filename) elif format == 'pickle': cell_graph = nx.read_gpickle(filename) elif format == 'edgelist': cell_graph = nx.read_edgelist(filename) elif format == 'yaml': cell_graph = nx.read_yaml(filename) elif format == 'graphml': cell_graph = cell_graph = nx.read_graphml(filename) else: print 'Unrecognized format:', format end = datetime.now() delta = end - start config.BENCHMARK_LOGGER.info( 'Read cell_graph from file %s of format %s in %g s' % (filename, format, delta.seconds + 1e-6 * delta.microseconds)) except Exception, e: print e
def _read_celltype_graph(self, celltypes_file, format='gml'): """ Read celltype-celltype connectivity graph from file. celltypes_file -- the path of the file containing the graph. format -- format of the file. allowed values: gml, graphml, edgelist, pickle, yaml. """ start = datetime.now() celltype_graph = None try: if format == 'gml': celltype_graph = nx.read_gml(celltypes_file) elif format == 'edgelist': celltype_graph = nx.read_edgelist(celltypes_file) elif format == 'graphml': celltype_graph = nx.read_graphml(celltypes_file) elif format == 'pickle': celltype_graph = nx.read_gpickle(celltypes_file) elif format == 'yaml': celltype_graph = nx.read_yaml(celltypes_file) else: print 'Unrecognized format %s' % (format) except Exception, e: print e
def read_graph_file(self): ''' Read a graph file. ''' self.graph = networkx.read_yaml(GRAPH_FILE) if self.graph is None: self.graph = networkx.DiGraph() self.write_graph_file()
def combine_graphs(self, filenames): output = self.config.OUTPUT_DIR graphs = [] for filename in filenames: g = nx.read_yaml(f"{output}/{filename}.yaml") graphs.append(g) combined = nx.compose_all(graphs) return combined
def read(self, filename=None): if filename is None: filename = self._filename try: # self._digraph = nx.read_gml(filename) self._digraph = nx.read_yaml(filename) except Exception as e: print("read failed", repr(e)) self._digraph = nx.DiGraph()
def update_existing_records(submission, outname='indication_paths.yaml'): if submission is not None: print('Updating Indications...') indications = nx.read_yaml('indication_paths.yaml') current_ids = [ind['graph']['_id'] for ind in indications] submission_prepped = {rec['graph']['_id']: rec for rec in submission} # Make sure new recrods have unique IDs if len(submission_prepped) != len(submission): submitted_id_counts = Counter( [rec['graph']['_id'] for rec in submission]) duplicated = [k for k, v in submitted_id_counts.items() if v > 1] print( 'Error, the folling identifiers were used for multiple paths:\n{}' .format(', '.join(duplicated))) sys.exit(400) # Make sure the Updated records exist diff = set(submission_prepped.keys()) - set(current_ids) if diff: print( "Error, the following path id's submitted for update do not exist:\n{}" .format(', '.join(diff))) sys.exit(400) out = [] errors = [] for ind in indications: _id = ind['graph']['_id'] if _id in submission_prepped.keys(): # Previous checks will have ensured that the `graph` data matches the `nodes` and `links` data # To ensure that we're updating the right path we can simply check that the identifier # is correct for the info found in `graph` try: validate_path_id(_id, submission_prepped[_id]) out.append(submission_prepped[_id]) except AssertionError as ae: errors.append(ae) else: out.append(ind) if errors: print('Update Unsuccessful') print('There were {} paths that produced errors'.format( len(errors))) print('Please see error messages below\n') for error in errors: print(error, end='\n\n') sys.exit(400) else: out = filter_deprecated_ids(out) print('Update Successful') nx.write_yaml(out, outname, indent=4)
def test_scenarios_have_source_files(self): output_file = self.generator.generate_spec_graph(TEST_SPEC_PATH) output_graph = nx.read_yaml(f"{OUTPUT_DIR}/{output_file}.yaml") self.assertEqual( output_graph.nodes["Scenario:Scenario 1"]["source_file"], "test_specification.spec") self.assertEqual( output_graph.nodes["Scenario:Scenario 2"]["source_file"], "test_specification.spec")
def supervised(big_folder, weightThresh): with open('/Users/Ish/Dropbox/OSM/results/Haiti/new_users', 'rb') as f: newList = pickle.load(f) B = nx.read_yaml(big_folder) initList = [] for e in B.edges_iter(): if (e[1] in newList) & (e[0] not in newList): if B[e[0]][e[1]]['weight'] > weightThresh: if ~B.has_edge(e[1], e[0]): #new user doesn't respond back print e
def assert_equal(self, G, data=False): (fd, fname) = tempfile.mkstemp() nx.write_yaml(G, fname) Gin = nx.read_yaml(fname) assert_nodes_equal(list(G), list(Gin)) assert_edges_equal(G.edges(data=data), Gin.edges(data=data)) os.close(fd) os.unlink(fname)
def assert_equal(self, G, data=False): (fd, fname) = tempfile.mkstemp() nx.write_yaml(G, fname) Gin = nx.read_yaml(fname); assert_equal(sorted(G.nodes()),sorted(Gin.nodes())) assert_equal(G.edges(data=data),Gin.edges(data=data)) os.close(fd) os.unlink(fname)
def loadNGdata(self): filefloder = r'../data/' filename = filefloder + 'whutgraph.yaml' # filename = 'whutgraph.yaml' try: self.DG = nx.DiGraph() self.DG = nx.read_yaml(filename) except Exception: print('Error, the graph data isnot exist!!!') exit()
def __init__(self, training=False): random.seed(datetime.datetime.now()) self.ADOLF_ARTICLE_URL = 'https://ru.wikipedia.org/wiki/%D0%93%D0%B8%D1%82%D0%BB%D0%B5%D1%80,_%D0%90%D0%B4%D0%BE%D0%BB%D1%8C%D1%84' self.training = training self.map_file = 'map.yaml' if os.path.exists(self.map_file): self.map = nx.read_yaml(self.map_file) else: self.map = nx.DiGraph() self.print_map()
def main(): print_own_info(__file__) yaml_file = get_graph_file() if exists(yaml_file): print "Graph (YAML) file exists: ",yaml_file print "Reading it ..." g = nx.read_yaml(yaml_file) d3_js.export_d3_js(g) print "Web files exported: ", else: create_graph()
def test_uptdate_graph(self): graph_name = self.generator.generate_spec_graph(SMELL_TEST_SPEC_PATH) graph = nx.read_yaml(f"{self.config.OUTPUT_DIR}/{graph_name}.yaml") self.smell.update_graph(graph) self.assertTrue(graph.nodes[ "Scenario:This is a scenario with an overly long name, an overly long name is defined as at least 120 characters blah blah blah blah"] ["smell"]) self.assertEqual( graph.nodes[ "Scenario:This is a scenario with an overly long name, an overly long name is defined as at least 120 characters blah blah blah blah"] ["smell_names"], ["Long Step name > 120 characters"])
def graph_from_file(cls, path, file_format=GraphFileFormat.GraphMl): if file_format == GraphFileFormat.GraphMl: graph = net.read_graphml(path) elif file_format == GraphFileFormat.AdjList: graph = net.read_adjlist(path) elif file_format == GraphFileFormat.Gml: graph = net.read_gml(path) elif file_format == GraphFileFormat.Yaml: graph = net.read_yaml(path) else: graph = net.read_graphml(path) return cls(graph=graph)
def parse_config_file(file_path): """ config file is a json file with the following data: { graph: 'path/to/graph.yml', infrastructures: { cost_factors: [...], construction_cost_factors: [...], costs: [ { source: ..., target: ..., value: ... }, ... ], construction_costs: [ { source: ..., target: ..., value: ... }, ... ], }, budget: .., demand: [ { source: ..., target: ..., value: ... }, ... ], config: { ...configs key value } } """ with open(file_path, 'r') as f: data = json.loads(f.read()) graph = nx.read_yaml(data['graph']) infrastructures = data['infrastructures'] for key in ['costs', 'construction_costs']: if key in infrastructures: infrastructures[key] = as_tuples_dict(infrastructures[key]) budget = data['budget'] demand = as_tuples_dict(data['demand']) config = data.get('config', {}) return Context( graph=graph, demand=demand, infrastructures=infrastructures, budget=budget, config=config )
def reciprocity(big_folder, weightThresh): B = nx.read_yaml(big_folder) reciprocity = [] edges = [] for e in B.edges_iter(): if B.has_edge(e[1], e[0]): if B[e[0]][e[1]][ 'weight'] > weightThresh: # get the constant from a distribution? if B[e[0]][e[1]]['weight'] <= B[e[1]][e[0]]['weight']: reciprocity.append(B[e[0]][e[1]]['weight'] / float(B[e[1]][e[0]]['weight'])) edges.append((e[0], e[1])) return zip(edges, reciprocity)
def set_network(self): network_spec = self.spec.get('network', False) node_spec = network_spec.get('nodes', False) edge_spec = network_spec.get('edges', False) if network_spec: loadfile = network_spec.get('load', False) if loadfile: # If filepath presented to load network, do so. Otherwise, create network. self.network = nx.read_yaml(loadfile) else: self.network = TSP_Network.create_network(node_spec, edge_spec) return self.network
def onkey(event): global G, wehren, color, fig if event.key == 'r': xmin = event.inaxes.axes.viewLim.xmin xmax = event.inaxes.axes.viewLim.xmax ymin = event.inaxes.axes.viewLim.ymin ymax = event.inaxes.axes.viewLim.ymax fig.set_facecolor((1.0, 0, 0)) fig.canvas.draw() fig.canvas.flush_events() wehren = convert.convert() G = nx.read_yaml("test.yml") color = [] for key in G._node: if G._node[key]["tlf"] < minimum and type == "tlf": color.append('red') elif G._node[key]["elw1"] < minimum and type == "elw1": color.append('red') elif G._node[key]["dlk"] < minimum and type == "dlk": color.append('red') elif G._node[key]["rw"] < minimum and type == "rw": color.append('red') elif G._node[key]["hlf"] < minimum and type == "hlf": color.append('red') elif G._node[key]["gwOel"] < minimum and type == "gwOel": color.append('red') elif G._node[key]["gwA"] < minimum and type == "gwA": color.append('red') elif G._node[key]["gwS"] < minimum and type == "gwS": color.append('red') elif G._node[key]["hoeh"] < minimum and type == "hoeh": color.append('red') elif G._node[key]["mess"] < minimum and type == "mess": color.append('red') elif G._node[key]["gwG"] < minimum and type == "gwG": color.append('red') elif G._node[key]["elw2"] < minimum and type == "elw2": color.append('red') elif G._node[key]["dekonP"] < minimum and type == "dekonP": color.append('red') elif G._node[key]["fwk"] < minimum and type == "fwk": color.append('red') else: color.append('green') fig.clf() nx.draw(G, pos, with_labels=True, node_color=color) fig.canvas.toolbar.push_current() plt.axis([xmin, xmax, ymin, ymax]) plt.show()
def readAllModels(folder): print folder allMpaths = getAllModels(folder) actualModels = {} for name, pa in allMpaths.iteritems(): print name, try: gTmp = nx.read_yaml(pa) actualModels[name] = gTmp except: print ' FAILED', print '' return actualModels
def test_graph_conversion_to_yaml(self): """ convert graph to yaml format :return: """ yt_script.convert_graph_to_yaml(self.MOCK_GRAPH, self.MOCK_FILE_OUTPUT) result_graph = nx.read_yaml(self.MOCK_FILE_OUTPUT) for node in self.MOCK_GRAPH.nodes(): self.assertIn(node, result_graph.nodes()) for edge in self.MOCK_GRAPH.edges(): try: self.assertIn(edge, result_graph.edges()) except AssertionError: edge = (edge[1], edge[0]) self.assertIn(edge, result_graph.edges()) continue
def load_graph(self, filename): """Read graph from GML or Adjacency list or YML format Args: filename : File or filename to read """ try: file_extention = list(filename.split("."))[-1] if file_extention == "gml": self.graph = nx.read_gml(filename) if file_extention == "adjlist": self.graph = nx.read_adjlist(filename) if file_extention == "yaml": self.graph = nx.read_yaml(filename) except Exception as e: print("Error in loading Graph file: The error is", e)
def __init__(self,starturl): # self.starturl = starturl self.DBP = dbp.MySqlBase() self.SH = shttp.SpiderHttp() self.todo_queue = squeue.Queue() self.todo_queue.enqueue(starturl) self.DG = nx.DiGraph() filefloder = r'../data/' filename = filefloder + 'whutgraph.yaml' # self.DG=nx.read_yaml('whutgraph.gml') if os.path.exists(filename): self.DG=nx.read_yaml(filename) self.visited_set = set(self.DBP.getOriginalUrllist()) print('visited_set',self.visited_set) self.wideSpiderPre() self.createGraph()
def read_graph(self, subgraph_file=None): if subgraph_file is None: subraph_file = self.context_graph_file logging.info("Writing graph.") # write the graph out file_format = subgraph_file.split(".")[-1] if file_format == "graphml": return nx.read_graphml(subgraph_file) elif file_format == "gml": return nx.read_gml(subgraph_file) elif file_format == "gexf": return nx.read_gexf(subgraph_file) elif file_format == "net": return nx.read_pajek(subgraph_file) elif file_format == "yaml": return nx.read_yaml(subgraph_file) elif file_format == "gpickle": return nx.read_gpickle(subgraph_file) else: logging.warning("File format not found, returning empty graph.") return nx.MultiDiGraph()
def read_graph(filename): """Read graph from file, raise IOError if cannot do it.""" graph = None if filename.endswith('.yaml'): try: graph = nx.read_yaml(filename) except ImportError: print('E: cannot read graph from file in YAML format.') print('Please install PyYAML or other similar package ' 'to use this functionality.') raise IOError else: print('Read graph from {0} in YAML format.'.format(filename)) elif filename.endswith('.gml'): try: graph = nx.read_gml(filename) except ImportError: print('E: cannot read graph from file in GML format.') print('Please install GML or other similar package ' 'to use this functionality.') raise IOError else: print('Read graph from {0} in GML format.'.format(filename)) elif filename.endswith('.net'): graph = nx.read_pajek(filename) print('Read graph from {0} in PAJECK format.'.format(filename)) elif filename.endswith('.gexf'): graph = nx.read_gexf(filename) print('Read graph from {0} in GEXF format.'.format(filename)) elif filename.endswith('.graphml'): graph = nx.read_graphml(filename) print('Read graph from {0} in GraphML format.'.format(filename)) else: with open(filename, 'rb') as f: graph = pickle.load(f) print('Read graph from {0} in pickle format.'.format(filename)) return graph
def load_graph(load_path, encoding='UTF-8'): logging.info('Loading graph from file:%s' % load_path) G=nx.read_yaml(load_path) logging.info('Loaded %d nodes and %d edges' % (G.number_of_nodes(), G.number_of_edges())) return G
#!/usr/bin/env python try: import matplotlib.pyplot as plt except: raise import networkx as nx import sys,os if len(sys.argv)==2: fn = sys.argv[1] print "Reading in %s" % fn g = nx.read_yaml(fn) print "Drawing a simple graph" plt.figure(figsize=(15,15)) nx.draw_circular(g) plt.savefig("%s.spectral.png" % fn) # save as png plt.show() # display
import libs.api_setup import libs.crawlers import networkx import tweepy USER_HANDLE_IS_FOUND = True api = libs.api_setup.create_tweepy_api(rate_limit_wait=False) graph = networkx.read_yaml('test.yaml') crawler = libs.crawlers.RecursiveCrawler(api) def get_user_by_handle(user_handle): ''' Calculate statistics for a user, looked up by their Twitter handle. :param user_handle: String of user handle EXCLUDING the '@' character :return: dictionary of the given user's information ''' # Fetch user information try: user = api.get_user(user_handle) except tweepy.error.TweepError as err: if err.response.status_code == 404: raise KeyError('User name does not exist') crawler.graph.add_node(user.screen_name.lower()) try: path = networkx.algorithms.shortest_path(crawler.graph, user.screen_name.lower(), '@')
import copy import networkx as nx import custom_meter_graph as cmg import pandas as pd import re def get_hindi_scanner(count=None): hindi_scanner = copy.copy(get_hindi_scanner.orig_hindi_scanner) hindi_scanner.count = count return hindi_scanner hindi_dg = nx.read_yaml('data/hindi_meter.yaml') hindi_scanner = cmg.CustomMeterGraph() hindi_scanner.DG = hindi_dg get_hindi_scanner.orig_hindi_scanner = hindi_scanner #scanner = cmg.CustomMeterGraph() def get_all_matra_counts(x, separator='\n'): return separator.join([str(cmg.get_matra_count(z)) for z in x.split(separator)]) def get_poss_scans(x,scanner=None): def splitup(x): return ','.join(sorted(set(x.split('\n')))) return splitup(get_all_matra_counts(scanner.get_all_scans_as_string(x))) def get_line_scans(m, lines,scanner): # also needs scanner ''' takes a multiline match and extracts line-level scans''' tokens = [] for line_id, line in enumerate(lines):
def read_graph(self, infile): self.graph = nx.read_yaml(infile)
import networkx as nx import pickle import random try: graph = pickle.load(open('R_infomap.bin','rb')) print('[i] loaded graph') except: # load any graph version, let's take the simple one for the moment graph = nx.read_yaml('R_infomap.yaml') with open('R_infomap.bin', 'wb') as f: pickle.dump(graph, f) # assemble subgraphs subs = {} for n,data in graph.nodes(data=True): col = data['color'] try: subs[col] += [n] except KeyError: subs[col] = [n] ccol = { 'red' : 'ə', 'green' : 'a', 'blue' : 'u', 'gray' : 'e', 'yellow' : 'i', 'cyan' : 'o'
LOG_FORMAT = "[%(asctime)s: %(levelname)s] %(name)s %(funcName)s: %(message)s" def search(graph, node, seen, depth=1000): if depth == 0: return seen.add(node) for _, n in graph.edges([node]): if n not in seen: search(graph, n, seen, depth - 1) CURR_DIR = P.dirname(P.abspath(__file__)) G = nx.read_yaml(P.join(CURR_DIR, 'graph.yml')) with gzip.GzipFile(P.join(CURR_DIR, 'heisig-data.txt.gz')) as fin: fin.readline() reader = csv.DictReader( io.StringIO(fin.read().decode('utf-8')), delimiter=':' ) heisig = list(reader) heisig_dict = {k['kanji']: k for k in heisig} from_keyword = {k['keyword5th-ed']: k['kanji'] for k in heisig} def get_keyword(kanji): try:
def main(): g = nx.read_yaml('data/simple_yaml.yaml') print(g['adj'])
def readGraphFromYAMLFile(self, filename): """Loads a graph from an YAML file. """ self.G = nx.read_yaml(filename)
def open(self): self.graph = nx.read_yaml("graph.yaml")
def open(self): self.graph = nx.read_yaml('graph.yaml')
import networkx as nx import pickle from sys import argv import html import igraph from matplotlib import pyplot as plt import numpy as np import matplotlib as mpl from lingpy import * # try to read the file try: G = pickle.load(open('bin/infomap.bin','rb')) except Exception: print(Exception) G = nx.read_yaml('yaml/infomap.yaml') with open('bin/infomap.bin', 'wb') as f: pickle.dump(G, f) def get_infomap(graph): g = igraph.Graph() for node, data in graph.nodes(data=True): g.add_vertex(name=node, **data) for nodeA, nodeB, data in graph.edges(data=True): g.add_edge(nodeA, nodeB, **data) C = g.community_infomap(edge_weights='occurrence', vertex_weights='occurrence') for community,name in zip(C.membership, g.vs['name']):