def test_find_degree_centrality(self): # Default test result = self.client.find_degree_centrality(self.stub, self.graph) dict_resp = [] for n, v in {'1': 0.2857142857142857, '2': 0.5714285714285714, '3': 0.5714285714285714, '4': 0.2857142857142857, '5': 0.14285714285714285, '6': 0.14285714285714285, '7': 0.14285714285714285, '8': 0.14285714285714285}.items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) expected = network_analytics_node_importance_pb2.DegreeCentralityResponse(status=True, message='success', output=dict_resp) self.assertEqual([result.status, result.message], [expected.status, expected.message]) self.assertCountEqual(result.output, expected.output) # Non Default test result = self.client.find_degree_centrality(self.stub, self.graph,'in') dict_resp = [] for n, v in {'1': 0.0, '2': 0.14285714285714285, '3': 0.14285714285714285, '4': 0.2857142857142857, '5': 0.14285714285714285, '6': 0.14285714285714285, '7': 0.14285714285714285, '8': 0.14285714285714285}.items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) expected = network_analytics_node_importance_pb2.DegreeCentralityResponse(status=True, message='success', output=dict_resp) self.assertEqual([result.status, result.message], [expected.status, expected.message]) self.assertCountEqual(result.output, expected.output) # # Graph With No Nodes Test result = self.client.find_degree_centrality(self.stub, self.graph_01) self.assertIn('graph should at least contain two nodes', result[1])
def test_find_closeness_centrality(self): # Deafault test result = self.client.find_closeness_centrality(self.stub, self.graph) dict_resp = [] for n, v in {'1': 0.5, '2': 0.7, '3': 0.7, '4': 0.5, '5': 0.4375, '6': 0.4375, '7': 0.4375, '8': 0.4375}.items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) expected = network_analytics_node_importance_pb2.ClosenessCentralityResponse(status=True, message='success', output=dict_resp) self.assertEqual([result.status, result.message], [expected.status, expected.message]) self.assertCountEqual(result.output, expected.output) # Non Deafault test result = self.client.find_closeness_centrality(self.stub, self.graph, distance=True, wf_improved=False, reverse=True, directed=True) dict_resp = [] for n,v in {'1': 0.5, '2': 0.6666666666666666, '3': 1.0, '4': 0.0, '5': 0.0, '6': 0.0, '7': 0.0, '8': 0.0}.items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=n,output=v)) expected = network_analytics_node_importance_pb2.ClosenessCentralityResponse(status=True,message='success', output=dict_resp) self.assertEqual([result.status,result.message],[expected.status,expected.message]) self.assertCountEqual(result.output, expected.output) # # Graph With No Nodes Test result = self.client.find_closeness_centrality(self.stub, self.graph_01) self.assertIn('graph should at least contain two nodes', result[1])
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 test_find_hits(self): # Default Test result = self.client.find_hits(self.stub, self.graph_no_weights) dict_resp_hubs = [] for n, v in {'1': 0.13604957690850644, '2': 0.2015158583139189, '3': 0.2015158583139189, '4': 0.13604957690850644, '5': 0.08121728238878734, '6': 0.08121728238878734, '7': 0.08121728238878734, '8': 0.08121728238878734}.items(): dict_resp_hubs.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) dict_resp_authorities = [] for n, v in {'1': 0.13604957688814256, '2': 0.2015158585243154, '3': 0.2015158585243154, '4': 0.13604957688814256, '5': 0.08121728229377104, '6': 0.08121728229377104, '7': 0.08121728229377104, '8': 0.08121728229377104}.items(): dict_resp_authorities.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) expected = network_analytics_node_importance_pb2.HitsResponse(status=True, message='success', hubs=dict_resp_hubs, authorities=dict_resp_authorities) self.assertEqual(result.status, True) self.assertEqual(result.message, 'success') self.assertCountEqual(result.hubs, expected.hubs) self.assertCountEqual(result.authorities, expected.authorities) # # Non Default Test nstart = [] for k, vv in {'1': 1, '2': 1, '3': 1, '4': 1, '5': 1, '6': 1, '7': 1, '8': 1}.items(): nstart.append(network_analytics_node_importance_pb2.DictIn(node=k, value=vv)) result = self.client.find_hits(self.stub, self.graph_no_weights,max_iter=110,tol=11.0e-7,nstart=nstart,normalized=False,directed=True) dict_resp_hubs = [] for n, v in {'1': 0.6180339887498948, '2': 5.309100041157175e-06, '3': 1.0, '4': 0.0, '5': 0.0, '6': 0.0, '7': 0.0, '8': 0.0}.items(): dict_resp_hubs.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) dict_resp_authorities = [] for n, v in {'1': 0.0, '2': 0.38196601125010515, '3': 3.957169530458124e-06, '4': 1.0, '5': 3.957169530458124e-06, '6': 0.6180339887498948, '7': 3.957169530458124e-06, '8': 0.6180339887498948}.items(): dict_resp_authorities.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) expected = network_analytics_node_importance_pb2.HitsResponse(status=True, message='success', hubs=dict_resp_hubs, authorities=dict_resp_authorities) print(result.hubs) print(expected.hubs) self.assertEqual(result.status, True) self.assertEqual(result.message, 'success') self.assertCountEqual(result.hubs, expected.hubs) self.assertCountEqual(result.authorities, expected.authorities) # # Error raising test nstart = [] for k, vv in {'1': 0.125, '2': 0.125, '31': 0.125, '4': 0.125, '5': 0.125, '6': 0.125, '7': 0.125, '8': 0.125}.items(): nstart.append(network_analytics_node_importance_pb2.DictIn(node=k, value=vv)) result = self.client.find_hits(self.stub, self.graph, nstart=nstart) self.assertIn('nstart parameter contains a node at zero-indexed position 2 that does not exist in the graph',result[1])
def test_find_eigenvector_centrality(self): # Default Test result = self.client.find_eigenvector_centrality(self.stub, self.graph) dict_resp = [] for n, v in {'1': 0.35775018836999806, '2': 0.5298994260311778, '3': 0.5298994260311778, '4': 0.35775018836999806, '5': 0.2135666184274351, '6': 0.2135666184274351, '7': 0.2135666184274351, '8': 0.2135666184274351}.items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) expected = network_analytics_node_importance_pb2.EigenvectorCentralityResponse(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 nstart = [] for k, vv in {'1': 1, '2': 1, '3': 1, '4': 1, '5': 1, '6': 1, '7': 1, '8': 1}.items(): nstart.append(network_analytics_node_importance_pb2.DictIn(node=k, value=vv)) result = self.client.find_eigenvector_centrality(self.stub, self.graph,max_iter=500, tol=1e-05, nstart=nstart, weight=True, directed=True,in_out=True) print(result) dict_resp = [] for n, v in {'1': 1.9935012399077745e-07, '2': 5.183103223760218e-05, '3': 0.0067123180248934745, '4': 0.5773456687445266, '5': 0.0067123180248934745, '6': 0.5772940370624132, '7': 0.0067123180248934745, '8': 0.5772940370624132}.items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) expected = network_analytics_node_importance_pb2.EigenvectorCentralityResponse(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 nstart = [] for k, vv in {'1': 0.125, '2': 0.125, '31': 0.125, '4': 0.125, '5': 0.125, '6': 0.125, '7': 0.125, '8': 0.125}.items(): nstart.append(network_analytics_node_importance_pb2.DictIn(node=k, value=vv)) result = self.client.find_eigenvector_centrality(self.stub, self.graph, nstart=nstart) self.assertIn( 'nstart parameter contains a node at zero-indexed position 2 that does not exist in the graph', result[1])
def ClosenessCentrality(self, request, context): ni = NodeImportance() graph = request.graph distance = request.distance wf_improved = request.wf_improved reverse = request.reverse directed = request.directed 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_closeness_centrality(graph_in, distance = distance, wf_improved = wf_improved, reverse = reverse, directed = directed) resp = network_analytics_node_importance_pb2.ClosenessCentralityResponse(status=ret[0], message=ret[1]) if resp.status: dict_resp = [] for node_ele,val_ele in (ret[2]["closeness_centrality"]).items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=node_ele, output=val_ele)) resp = network_analytics_node_importance_pb2.ClosenessCentralityResponse(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 DegreeCentrality(self, request, context): ni = NodeImportance() graph = request.graph try: edges_list = [] for edges_proto in graph.edges: edges_list.append(list(edges_proto.edge)) nodes_list = list(graph.nodes) graph_in = {"nodes": nodes_list, "edges": edges_list} ret = ni.find_degree_centrality(graph_in, request.in_out) if ret[0]: dict_resp = [] for node_ele, val_ele in (ret[2]["degree_centrality"]).items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=node_ele, output=val_ele)) resp = network_analytics_node_importance_pb2.DegreeCentralityResponse(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 test_find_pagerank(self): # Default Test result = self.client.find_pagerank(self.stub, self.graph) print(result) dict_resp = [] for n, v in {'1': 0.12113884655309373, '2': 0.23955113566709454, '3': 0.23955113566709454, '4': 0.12113884655309375, '5': 0.06965500888990583, '6': 0.06965500888990583, '7': 0.06965500888990583, '8': 0.06965500888990583}.items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) expected = network_analytics_node_importance_pb2.PageRankResponse(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 personalizatoin = [] for k,vv in {'1': 0.125, '2': 0.125, '3': 0.125, '4': 0.125, '5': 0.125, '6': 0.125, '7': 0.125, '8': 0.125}.items(): personalizatoin.append(network_analytics_node_importance_pb2.DictIn(node=k,value=vv)) nstart = [] for k, vv in {'1': 1, '2': 1, '3': 1, '4': 1, '5': 1, '6': 1, '7': 1, '8': 1}.items(): nstart.append(network_analytics_node_importance_pb2.DictIn(node=k, value=vv)) dangling = [] for k, vv in {'1': 0.125, '2': 0.125, '3': 0.125, '4': 0.125, '5': 0.125, '6': 0.125, '7': 0.125, '8': 0.125}.items(): dangling.append(network_analytics_node_importance_pb2.DictIn(node=k, value=vv)) result = self.client.find_pagerank(self.stub, self.graph,alpha=0.95, personalization=personalizatoin, max_iter=100, tol=1e-07, nstart=nstart, weight=True, dangling=dangling,directed=True) print(result) dict_resp = [] for n, v in {'1': 0.08514279383409741, '2': 0.1255854995423924, '3': 0.12491155064890427, '4': 0.16514082203112918, '5': 0.12491155064890427, '6': 0.12469811632283417, '7': 0.12491155064890427, '8': 0.12469811632283417}.items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=n, output=v)) expected = network_analytics_node_importance_pb2.PageRankResponse(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 personalizatoin = [] for k, vv in {'1': 0.125, '2': 0.125, '31': 0.125, '4': 0.125, '5': 0.125, '6': 0.125, '7': 0.125, '8': 0.125}.items(): personalizatoin.append(network_analytics_node_importance_pb2.DictIn(node=k, value=vv)) result = self.client.find_pagerank(self.stub, self.graph, personalization=personalizatoin) self.assertIn('personalization parameter contains a node at zero-indexed position 2 that does not exist in the graph', result[1])
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])
def Hits(self, request, context): ni = NodeImportance() graph = request.graph try: edges_list = [] for edges_proto in graph.edges: edges_list.append(list(edges_proto.edge)) nodes_list = list(graph.nodes) graph_in = {"nodes": nodes_list, "edges": edges_list} nstart_dict = {} for p in request.nstart: nstart_dict[p.node] = p.value if len(nstart_dict) == 0: nstart_dict = None ret = ni.find_hits(graph_in, max_iter=request.max_iter, tol=request.tol, nstart=nstart_dict, normalized=request.normalized, directed=request.directed) if ret[0]: dict_resp_hubs = [] dict_resp_authorities = [] for node_ele, val_ele in (ret[2]["hubs"]).items(): dict_resp_hubs.append(network_analytics_node_importance_pb2.DictOutput(node=node_ele, output=val_ele)) for node_ele, val_ele in (ret[2]["authorities"]).items(): dict_resp_authorities.append(network_analytics_node_importance_pb2.DictOutput(node=node_ele, output=val_ele)) resp = network_analytics_node_importance_pb2.HitsResponse(status=ret[0], message=ret[1], hubs=dict_resp_hubs, authorities=dict_resp_authorities) 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 EigenvectorCentrality(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} nstart_dict = {} for p in request.nstart: nstart_dict[p.node] = p.value if len(nstart_dict) == 0: nstart_dict = None ret = ni.find_eigenvector_centrality(graph_in, max_iter=request.max_iter, tol=request.tol, nstart=nstart_dict, weight=request.weight, directed=request.directed,in_out=request.in_out) if ret[0]: dict_resp = [] for node_ele, val_ele in (ret[2]["eigenvector_centrality"]).items(): dict_resp.append(network_analytics_node_importance_pb2.DictOutput(node=node_ele, output=val_ele)) resp = network_analytics_node_importance_pb2.EigenvectorCentralityResponse(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))