def main(size=None, depth=3): graphcommons = GraphCommons('sk_IDfGvifqeRuvQ7HnCcAEsg') if size is None: G = read_generated_grid_edgelist() else: G = generate_grid_edgelist(size, depth) signals = [ Signal(action="node_create", name=node, type=data['type']) for (node, data) in G.nodes(data=True) ] signals.extend([ Signal(action="edge_create", from_name=source, from_type=G.node[source]['type'], to_name=target, to_type=G.node[target]['type'], name=data['type'], weight=1) for source, target, data in G.edges(data=True) ]) created_graph = new_graph(graphcommons, signals=signals, name="NPM Dependency Graph", description="NPM Dependency Graph") print('Created Graph URL:') print('https://graphcommons.com/graphs/%s' % created_graph.id)
def main(access_token, package_name, max_depth): graph = DiGraph() graphcommons = GraphCommons(access_token) import_package_dependencies(graph, package_name, max_depth=max_depth) signals = [] for (node, data) in graph.nodes(data=True): if data['type'] == 'PACKAGE': reference = "https://www.npmjs.com/package/%s" % node else: reference = 'https://www.npmjs.com/~%s' % node signals.append(Signal( action="node_create", name=node, type=data['type'], reference=reference )) for source, target, data in graph.edges(data=True): signals.append(Signal( action="edge_create", from_name=source, from_type=graph.node[source]['type'], to_name=target, to_type=graph.node[target]['type'], name=data['type'], weight=1 )) created_graph = graphcommons.new_graph( name="Dependency Network of %s" % package_name, description="Dependency Network of %s Package" % package_name, signals=signals ) print 'Created Graph URL:' print 'https://graphcommons.com/graphs/%s' % created_graph.id
def main(access_token, package_name, max_depth): graph = DiGraph() graphcommons = GraphCommons(access_token) import_package_dependencies(graph, package_name, max_depth=max_depth) signals = [] for (node, data) in graph.nodes(data=True): if data['type'] == 'PACKAGE': reference = "https://www.npmjs.com/package/%s" % node else: reference = 'https://www.npmjs.com/~%s' % node signals.append( Signal(action="node_create", name=node, type=data['type'], reference=reference)) for source, target, data in graph.edges(data=True): signals.append( Signal(action="edge_create", from_name=source, from_type=graph.node[source]['type'], to_name=target, to_type=graph.node[target]['type'], name=data['type'], weight=1)) created_graph = graphcommons.new_graph( name="Dependency Network of %s" % package_name, description="Dependency Network of %s Package" % package_name, signals=signals) print 'Created Graph URL:' print 'https://graphcommons.com/graphs/%s' % created_graph.id
def main(): data = 'dependencies_data.json' with open(data, 'r') as data_file: json_data = json.load(data_file) for package, dependencies in json_data.items(): print(package, dependencies) # build dependency graph graph = DiGraph() build_graph_from_dict(graph, json_data) # create graph on graph commons graphcommons_client = GraphCommons(access_token) graph_id = create_graph_visualization(graphcommons_client, graph, "300 Starts") print('graph id:', graph_id)
def test(): package_name = "/package/web3" dependency_tree_depth = 5 # import package dependencies dependencies_dict = {} import_package_dependencies(package_name, dependencies_dict, dependency_tree_depth, 0) # print out dependency dictionary for k, v in dependencies_dict.items(): print(k, v) # build graph from dependency dict graph = DiGraph() build_graph_from_dict(graph, dependencies_dict) # create graph on graph commons graphcommons_client = GraphCommons(access_token) package_name = package_name.replace('/package/', '') graph_id = create_graph_visualization(graphcommons_client, graph, package_name) print('graph id:', graph_id)
for element in chain(journalists, newspapers, reasons): node = create_node(**dict(element)) signals.append(Signal(**node)) total_num_node += 1 print "Total # of node: %d" % total_num_node update_graph(api, graph_id, signals) signals = [] for element in edges: edge = create_edge(**dict(element)) signals.append(Signal(**edge)) update_graph(api, graph_id, signals) return graph_id if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--developer-key', required=True) parser.add_argument('--input-file', required=True) parser.add_argument('--graph-id') args = parser.parse_args() api = GraphCommons(args.developer_key) print create_from_txt(api, args.input_file) print api.status() # to run this script: python run.py --developer-key <YOUR_DEV_KEY> --input-file <INPUT_FILE (tab separated file)>
def __init__(self, api_key, graph_id): if graph_id: self.graph_id = graph_id self.api = GraphCommons(api_key) self.graph = None
class GraphApi(object): def __init__(self, api_key, graph_id): if graph_id: self.graph_id = graph_id self.api = GraphCommons(api_key) self.graph = None def create_new_graph(self, graph_name, subtitle, description): graph = self.api.new_graph( name=graph_name, subtitle=subtitle, description=description, signals=[] ) print("Created a graph with id {}".format(graph['id'])) self.graph_id = graph['id'] return self.graph_id @staticmethod def create_signals(input_filename): result = [] f = open(input_filename, "r") line = f.readline() while line: line = line.strip() obj = json.loads(line) result.append(Signal(**obj)) line = f.readline() f.close() return result def get_nodes_by_node_type(self, node_type, query=None): graph = self.get_graph() def filter_function(node): if node.type != node_type: return False if query: if node.name.find(query) >= 0: return True else: return False else: return True return filter(filter_function, graph.nodes) @staticmethod def create_nodetype_dicts(nodes): names = [node.name for node in nodes] node_dict = dict(zip(names, nodes)) return node_dict def get_graph(self, refresh=False): if self.graph is None or refresh: graph = self.api.graphs(self.graph_id) self.graph = graph return self.graph def get_all_node_types(self): graph = self.get_graph() node_types = [t.name for t in graph._node_types.values()] return dict((t, self.get_nodes_by_node_type(t)) for t in node_types) def get_node_ids_types_names(self): types = self.get_all_node_types() for node_type, nodes in types.iteritems(): for node in nodes: yield node.name, node.id, node_type def get_paths(self, from_id, to_id, **kwargs): d = {"from": from_id, "to": to_id} d.update(kwargs) # add other key-value pairs. paths = self.api.paths(self.graph_id, d) to_return = [] for p in paths: speech = p['nodes'][1] to_return.append(dict(reference=speech.reference, path_string=p['path_string'])) return to_return def upsert_edges(self, signals, chunk_size=100): for i in range(0, len(signals) / chunk_size + 1): # print signals[i*chunk_size:(i+1)*chunk_size] self.api.update_graph( id=self.graph_id, signals=signals[i * chunk_size:(i + 1) * chunk_size] ) print("Updated graph... {} of {}".format(min((i + 1) * chunk_size, len(signals)), len(signals)))
from graphcommons import GraphCommons, Signal graphcommons = GraphCommons('sk_GhRtjThEbbq5zlxQuWkBGg') graphcommons.update_graph( id='7bbf3204-647d-4cbc-81c5-94b5d802710c', signals=[ Signal( action='node_create', name='ETI', type='Company', description='Lovely' ) ] )