def find_central_nodes(output): try: # channel = grpc.insecure_channel('tz-services-1.snet.sh:2234') channel = grpc.insecure_channel('localhost:5001') stub = network_analytics_node_importance_pb2_grpc.NetworkAnalyticsNodeImportanceStub( channel) graph = { "nodes": ['1', '2', '3', '4', '5', '6', '7', '8'], "edges": [['1', '2'], ['1', '4'], ['2', '3'], ['2', '5'], ['3', '4'], ['3', '6'], ['2', '7'], ['3', '8']], "weights": [3, 4, 5, 6, 7, 8, 9, 10] } edges_req = [] for e in graph["edges"]: edges_req.append( network_analytics_node_importance_pb2.Edge(edge=e)) graph_in = network_analytics_node_importance_pb2.Graph( nodes=graph["nodes"], edges=edges_req) center_req = network_analytics_node_importance_pb2.CentralNodeRequest( graph=graph_in) resp = stub.CentralNodes(center_req) # print('done') print(resp) output.put((1, resp.message)) except Exception as e: # print('done') output.put((0, str(e)))
def BetweennessCentrality(self, request, context): ni = NodeImportance() graph = request.graph try: edges_list = [] for edges_proto in graph.edges: edges_list.append(list(edges_proto.edge)) weights_list = list(graph.weights) nodes_list = list(graph.nodes) if len(weights_list) > 0: graph_in = {"nodes": nodes_list, "edges": edges_list, "weights": weights_list} else: graph_in = {"nodes": nodes_list, "edges": edges_list} ret = ni.find_betweenness_centrality(graph_in, k=request.k, normalized=request.normalized, weight=request.weight, endpoints=request.endpoints, type=request.type, seed=request.seed, directed=request.directed) if ret[0]: dict_resp = [] if ret[2]['type'] == 'node': for node_ele, val_ele in (ret[2]["betweenness_centrality"]).items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=node_ele, output=val_ele)) else: for edge_ele, val_ele in (ret[2]["betweenness_centrality"]).items(): edges_resp = network_analytics_node_importance_pb2.Edge(edge=list(edge_ele)) dict_resp.append(network_analytics_node_importance_pb2.DictOutput(edge=edges_resp, output=val_ele)) resp = network_analytics_node_importance_pb2.BetweennessCentralityResponse(status=ret[0], message=ret[1], output=dict_resp) else: print(time.strftime("%c")) print('Waiting for next call on port 5001.') raise grpc.RpcError(grpc.StatusCode.UNKNOWN, ret[1]) print('status:', resp.status) print('message:', resp.message) print(time.strftime("%c")) print('Waiting for next call on port 5001.') return resp except Exception as e: logging.exception("message") print(time.strftime("%c")) print('Waiting for next call on port 5001.') raise grpc.RpcError(grpc.StatusCode.UNKNOWN, str(e))
def get_graph(self, graph): try_weight = False try_edge = False edges_req = [] try: for e in graph["edges"]: edges_req.append(network_analytics_node_importance_pb2.Edge(edge=e)) try_edge = True except Exception as e: pass try: weights_req = graph['weights'] try_weight = True except Exception as e: pass try: if not try_weight and not try_edge: graph_in = network_analytics_node_importance_pb2.Graph(nodes=graph["nodes"]) elif not try_weight and try_edge: graph_in = network_analytics_node_importance_pb2.Graph(nodes=graph["nodes"], edges=edges_req) elif try_weight and not try_edge: graph_in = network_analytics_node_importance_pb2.Graph(nodes=graph["nodes"], weights=weights_req) else: graph_in = network_analytics_node_importance_pb2.Graph(nodes=graph["nodes"], edges=edges_req, weights=weights_req) except: if not try_weight and not try_edge: graph_in = network_analytics_node_importance_pb2.Graph() elif not try_weight and try_edge: graph_in = network_analytics_node_importance_pb2.Graph(edges=edges_req) elif try_weight and not try_edge: graph_in = network_analytics_node_importance_pb2.Graph(weights=weights_req) else: graph_in = network_analytics_node_importance_pb2.Graph(edges=edges_req, weights=weights_req) return graph_in
def test_find_betweenness_centrality(self): # non Default Test with weight for node betweeness result = self.client.find_betweenness_centrality(self.stub, self.graph, k=1, normalized=False, weight=True, endpoints=True, seed=1, directed=True) dict_resp = [] for n, v in {'1': 0.0, '2': 0.0, '3': 3.0, '4': 1.0, '5': 0.0, '6': 1.0, '7': 0.0, '8': 1.0}.items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) expected = network_analytics_node_importance_pb2.BetweennessCentralityResponse(status=True, message='success', output=dict_resp) self.assertEqual(result.status, True) self.assertEqual(result.message, 'success') self.assertCountEqual(result.output, expected.output) # non Default Test with weight for node betweeness and with type parameter specified result = self.client.find_betweenness_centrality(self.stub, self.graph, k=1, normalized=False, weight=True, endpoints=True, seed=1, directed=True, type='node') dict_resp = [] for n, v in {'1': 0.0, '2': 0.0, '3': 3.0, '4': 1.0, '5': 0.0, '6': 1.0, '7': 0.0, '8': 1.0}.items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) expected = network_analytics_node_importance_pb2.BetweennessCentralityResponse(status=True, message='success', output=dict_resp) self.assertEqual(result.status, True) self.assertEqual(result.message, 'success') self.assertCountEqual(result.output, expected.output) # non Default Test without weight for node betweeness result = self.client.find_betweenness_centrality(self.stub, self.graph, k=1, normalized=False, endpoints=True, seed=1, directed=True) dict_resp = [] for n, v in {'1': 0.0, '2': 0.0, '3': 3.0, '4': 1.0, '5': 0.0, '6': 1.0, '7': 0.0, '8': 1.0}.items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) expected = network_analytics_node_importance_pb2.BetweennessCentralityResponse(status=True, message='success', output=dict_resp) self.assertEqual(result.status, True) self.assertEqual(result.message, 'success') self.assertCountEqual(result.output, expected.output) # non Default Test with weight for edge betweeness result = self.client.find_betweenness_centrality(self.stub, self.graph, k=1, normalized=False, seed=1, directed=True, type='edge', weight=True) dict_resp = [] for e, v in {('1', '2'): 0.0, ('1', '4'): 0.0, ('2', '3'): 0.0, ('2', '5'): 0.0, ('2', '7'): 0.0, ('3', '4'): 1.0, ('3', '6'): 1.0, ('3', '8'): 1.0}.items(): edges_resp = network_analytics_node_importance_pb2.Edge(edge=list(e)) dict_resp.append(network_analytics_node_importance_pb2.DictOutput(edge=edges_resp, output=v)) expected = network_analytics_node_importance_pb2.BetweennessCentralityResponse(status=True, message='success', output=dict_resp) self.assertEqual(result.status, True) self.assertEqual(result.message, 'success') self.assertCountEqual(result.output, expected.output) # error raising test result = self.client.find_betweenness_centrality(self.stub, self.graph, k=15, normalized=False, weight=True, endpoints=True, seed=1, directed=True) self.assertIn('parameter k is larger than the number of nodes in the graph',result[1])