def get_similarity_network(self): plot = None try: cwd = os.path.abspath(os.path.dirname(__file__)) query_path = os.path.join(cwd, self.queries_file) project_cypher = query_utils.read_queries(query_path) query = query_utils.get_query(project_cypher, query_id="projects_subgraph") list_projects = [] driver = connector.getGraphDatabaseConnectionConfiguration() if "other_id" in self.similar_projects: list_projects = self.similar_projects[ "other_id"].values.tolist() list_projects.append(self.identifier) list_projects = ",".join(['"{}"'.format(i) for i in list_projects]) query = query.replace("LIST_PROJECTS", list_projects) path = connector.sendQuery(driver, query, parameters={}).data() G = acore_utils.neo4j_path_to_networkx(path, key='path') args = {} style, layout = self.get_similarity_network_style() args['stylesheet'] = style args['layout'] = layout args['title'] = "Projects subgraph" net, mouseover = acore_utils.networkx_to_cytoscape(G) plot = viz.get_cytoscape_network(net, "projects_subgraph", args) except Exception as err: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] logger.error( "Error: {}. Reading queries from file {}: {}, file: {},line: {}" .format(err, query_path, sys.exc_info(), fname, exc_tb.tb_lineno)) return plot
def get_knowledge_graph_plot(self): if self.graph is None: self.generate_knowledge_graph() title = 'Project {} Knowledge Graph'.format(self.identifier) if self.data is not None: if 'name' in self.data: title = 'Project {} Knowledge Graph'.format(self.data['name']) args = {'title': title, 'node_properties': {}, 'width': 2600, 'height': 2600, 'maxLinkWidth': 7, 'maxRadius': 20} color_selector = "{'selector': '[name = \"KEY\"]', 'style': {'font-size': 10, 'background-color':'VALUE','width': 50,'height': 50,'background-image':'/assets/graph_icons/ENTITY.png','background-fit': 'cover','opacity':OPACITY}}" stylesheet = [{'selector': 'node', 'style': {'label': 'data(name)', 'z-index': 9999}}, {'selector': 'edge', 'style': {'label': 'data(type)', 'curve-style': 'unbundled-bezier', 'control-point-distance': '20px', 'control-point-weight': '0.7', 'z-index': 5000, 'line-color': '#bdbdbd', 'opacity': 0.2, 'font-size': '7px'}}] layout = {'name': 'circle'} #stylesheet.extend([{'selector':'[weight < 0]', 'style':{'line-color':'#3288bd'}},{'selector':'[width > 0]', 'style':{'line-color':'#d73027'}}]) for n in self.nodes: color = self.nodes[n]['color'] image = self.nodes[n]['type'] opacity = 0.3 if image == 'Module' or image == 'Group' else 1 stylesheet.append(ast.literal_eval(color_selector.replace("KEY", n.replace("'", "")).replace("VALUE", color).replace("ENTITY", image).replace("OPACITY", str(opacity)))) stylesheet.extend([{'selector': '[weight < 0]', 'style': {'line-color': '#4add1'}}, {'selector': '[weight > 0]', 'style': {'line-color': '#d6604d'}}]) args['stylesheet'] = stylesheet args['layout'] = layout nodes_table, edges_table = viz.network_to_tables(self.graph) nodes_fig_table = viz.get_table(nodes_table, identifier=self.identifier+"_nodes_table", title="Nodes table") edges_fig_table = viz.get_table(edges_table, identifier=self.identifier+"_edges_table", title="Edges table") cy_elements, mouseover_node = utils.networkx_to_cytoscape(self.graph) #args['mouseover_node'] = mouseover_node net = {"notebook": [cy_elements, stylesheet, layout], "app": viz.get_cytoscape_network(cy_elements, self.identifier, args), "net_tables": (nodes_fig_table, edges_fig_table), "net_json": json_graph.node_link_data(self.graph)} return net
def get_knowledge_graph_plot(self, summarize=True): if self.graph is None: self.generate_knowledge_graph() selected_nodes = [] if summarize and len(self.graph.nodes()) > 1: centrality = nx.betweenness_centrality(self.graph, k=None, weight='weight', normalized=False) #centrality = nx.pagerank(G, alpha=0.95, weight='weight') nx.set_node_attributes(self.graph, centrality, 'centrality') sorted_centrality = sorted(centrality.items(), key=itemgetter(1), reverse=True) for node_type in self.entities: nodes = [ x for x, y in self.graph.nodes(data=True) if 'type' in y and y['type'] == node_type and x not in self.keep_nodes ] selected_nodes.extend( [n for n, c in sorted_centrality if n in nodes][15:]) if len(selected_nodes) > 0: self.graph.remove_nodes_from(selected_nodes) title = 'Project {} Knowledge Graph'.format(self.identifier) if self.data is not None: if 'name' in self.data: title = 'Project {} Knowledge Graph'.format(self.data['name']) args = { 'title': title, 'node_properties': {}, 'width': 2000, 'height': 2000, 'maxLinkWidth': 7, 'maxRadius': 20 } color_selector = "{'selector': '[name = \"KEY\"]', 'style': {'font-size': '7px', 'text-opacity': 0.8, 'background-color':'VALUE','width': 50,'height': 50,'background-image':'/assets/graph_icons/ENTITY.png','background-fit': 'cover','opacity':OPACITY}}" stylesheet = [{ 'selector': 'node', 'style': { 'label': 'data(name)', 'opacity': 0.7 } }, { 'selector': 'edge', 'style': { 'label': 'data(type)', 'curve-style': 'unbundled-bezier', 'control-point-distance': '30px', 'control-point-weight': '0.7', 'z-index': 5000, 'line-color': '#bdbdbd', 'opacity': 0.2, 'font-size': '2.5px', 'text-opacity': 1, 'font-style': "normal", 'font-weight': "normal" } }] layout = { 'name': 'cose', 'idealEdgeLength': 100, 'nodeOverlap': 20, 'refresh': 20, 'randomize': False, 'componentSpacing': 100, 'nodeRepulsion': 400000, 'edgeElasticity': 100, 'nestingFactor': 5, 'gravity': 80, 'numIter': 1000, 'initialTemp': 200, 'coolingFactor': 0.95, 'minTemp': 1.0 } stylesheet.extend([{ 'selector': '[weight < 0]', 'style': { 'line-color': '#3288bd' } }, { 'selector': '[weight > 0]', 'style': { 'line-color': '#d73027' } }]) for n, attr in self.graph.nodes(data=True): color = self.default_color image = '' if 'color' in attr: color = attr['color'] if 'type' in attr: image = attr['type'] opacity = 0.3 if image == 'Module' or image == 'Group' else 1 stylesheet.append( ast.literal_eval( color_selector.replace("KEY", n.replace("'", "")).replace( "VALUE", color).replace("ENTITY", image).replace("OPACITY", str(opacity)))) stylesheet.extend([{ 'selector': 'node', 'style': { 'width': 'mapData(centrality, 0, 1, 15, 30)', 'height': 'mapData(centrality, 0, 1, 15, 30)' } }]) args['stylesheet'] = stylesheet args['layout'] = layout G = self.graph.copy() if G.has_node('Regulated'): G.remove_node('Regulated') nodes_table, edges_table = viz.network_to_tables(G, source='node1', target='node2') nodes_fig_table = viz.get_table(nodes_table, identifier=self.identifier + "_nodes_table", args={'title': "Nodes table"}) edges_fig_table = viz.get_table(edges_table, identifier=self.identifier + "_edges_table", args={'title': "Edges table"}) cy_elements, mouseover_node = utils.networkx_to_cytoscape(G) #args['mouseover_node'] = mouseover_node net = { "notebook": [cy_elements, stylesheet, layout], "app": viz.get_cytoscape_network(cy_elements, self.identifier, args), "net_tables": (nodes_table, edges_table), "net_tables_viz": (nodes_fig_table, edges_fig_table), "net_json": json_graph.node_link_data(G) } return net
def get_db_schema(): """ Retrieves the database schema :return: network with all the database nodes and how they are related """ style = [{ 'selector': 'node', 'style': { 'label': 'data(name)', 'background-color': 'data(color)', 'text-valign': 'center', 'text-halign': 'center', 'border-color': 'gray', 'border-width': '1px', 'width': 55, 'height': 55, 'opacity': 0.8, 'font-size': '14' } }, { 'selector': 'edge', 'style': { 'label': 'data(label)', 'curve-style': 'bezier', 'opacity': 0.7, 'width': 0.4, 'font-size': '5' } }] layout = { 'name': 'cose', 'idealEdgeLength': 100, 'nodeOverlap': 20, 'refresh': 20, 'randomize': False, 'componentSpacing': 100, 'nodeRepulsion': 400000, 'edgeElasticity': 100, 'nestingFactor': 5, 'gravity': 80, 'numIter': 1000, 'initialTemp': 200, 'coolingFactor': 0.95, 'minTemp': 1.0 } query_name = 'db_schema' cypher = get_query() if query_name in cypher: if 'query' in cypher[query_name]: query = cypher[query_name]['query'] path = connector.sendQuery(driver, query, parameters={}).data() G = utils.neo4j_schema_to_networkx(path) args = {'height': '1000px'} args['stylesheet'] = style args['layout'] = layout args['title'] = "Database Schema" net, mouseover = utils.networkx_to_cytoscape(G) plot = viz.get_cytoscape_network(net, "db_schema", args) return plot