def test_load_graph_gexf_three_adjacency_matrix(self): # 0 1 2 3 4 5 6 7 8 result = np.matrix([ [0.00, 0.00, 0.88, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00], # 0 [0.00, 0.00, 0.25, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00], # 1 [0.88, 0.25, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00], # 2 [1.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00], # 3 [0.00, 0.00, 1.00, 1.00, 0.00, 0.00, 1.00, 0.00, 1.00], # 4 [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.52, 0.00], # 5 [0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.52], # 6 [0.00, 1.00, 0.00, 0.00, 0.00, 0.52, 0.00, 0.00, 0.52], # 7 [0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.52, 0.52, 0.00] ]) # 8 graph = graphio.load_graph(self.test_three_group_gexf_file, format='GEXF') nodes = list(graph.nodes()) adj_matrix = nx.adjacency_matrix(graph).todense() for i in range(result.shape[0]): for j in range(result.shape[1]): result_i = nodes.index( str(i)) # Order of the nodes is not fixed result_j = nodes.index(str(j)) self.assertEquals(adj_matrix[i, j], result[result_i, result_j])
def experiment(self, extra_params): """ Run the experiment. All config overriding and stuff are performed in the Experiment class. """ self.log.info("Running preprocess xml experiment.") self.log.info("Parsing from config file.") required = ['file', 'out', 'precompute'] if self._are_required_parameters_valid(self.config, required): cfg_params = self._load_parameters(self.params_section) file = cfg_params['file'] format = cfg_params['format'] labels_as_ids = cfg_params['labels_as_ids'] outfile = cfg_params['out'] cfg_precompute = cfg_params['precompute'] path = cfg_params['data_path'] self.log.info("Reading file format") graph = graphio.load_graph(file, format=format) self.log.info("Converting to ProphTools format") converted = graphio.convert_to_graphdataset( graph, precompute=cfg_precompute, labels_as_ids=labels_as_ids) self.log.info("Writing mat file") converted.write(path, outfile) self.log.info("Process performed successfully") return 0 else: self.log.error("Exiting") return -1
def test_convert_to_graphdata_zero_matrices_relations_connections(self): graph = graphio.load_graph( self.test_three_group_two_relations_gexf_file, format='GEXF') converted_graph = graphio.convert_to_graphdataset(graph) connections = converted_graph.connections expected_connections = np.matrix([[-1, 0, -1], [-1, -1, 1], [-1, -1, -1]]) for i in range(connections.shape[0]): for j in range(connections.shape[1]): self.assertEquals(connections[i, j], expected_connections[i, j])
def test_load_graph_gexf_adjacency_matrix(self): result = np.matrix([[0, 0.25, 0, 0.88], [0.25, 0, 0.52, 0], [0, 0.52, 0, 1.00], [0.88, 0, 1.00, 0]]) graph = graphio.load_graph(self.test_file, format='GEXF') nodes = list(graph.nodes()) adj_matrix = nx.adjacency_matrix(graph).todense() for i in range(result.shape[0]): for j in range(result.shape[1]): result_i = nodes.index( str(i)) # Order of the nodes is not fixed result_j = nodes.index(str(j)) self.assertEquals(adj_matrix[i, j], result[result_i, result_j])
def test_convert_to_graphdata_relations_orientation(self): graph = graphio.load_graph(self.test_three_group_gexf_file, format='GEXF') converted_graph = graphio.convert_to_graphdataset(graph) connections = converted_graph.connections for i in range(connections.shape[0]): for j in range(connections.shape[1]): if connections[i, j] != -1: mat_index = connections[i, j] relation = converted_graph.relations[mat_index].matrix src_mat = converted_graph.networks[i].matrix dst_mat = converted_graph.networks[j].matrix self.assertEquals(src_mat.shape[0], relation.shape[0]) self.assertEquals(dst_mat.shape[0], relation.shape[1])
def test_txt_file_with_non_unique_ids_raises_exception(self): with self.assertRaises(ValueError): graph = graphio.load_graph(self.test_repeated_id_file, format="TXT")
def test_file_with_missing_group_attributes_raises_exception(self): with self.assertRaises(ValueError): graph = graphio.load_graph(self.test_missing_attr_file)
def test_file_with_missing_group_attributes_raises_exception_txt(self): with self.assertRaises(ValueError): graph = graphio.load_graph(self.test_txt_missing_group_file, format='TXT')
def test_load_graph_unknown_format_raises_exception(self): with self.assertRaises(TypeError): unknown_format = "unknown" graphio.load_graph(self.test_file, format=unknown_format)
def test_write_mat_file_load(self): graph = graphio.load_graph( self.test_three_group_two_relations_gexf_file, format='GEXF') converted_graph = graphio.convert_to_graphdataset(graph, precompute=True) converted_graph.write(self.test_dir, 'test_out.mat')
def test_convert_to_graphdata_one_network_per_group(self): graph = graphio.load_graph(self.test_three_group_gexf_file, format='GEXF') converted_graph = graphio.convert_to_graphdataset(graph) self.assertEquals(len(converted_graph.networks), 3)
def test_convert_to_graphdata_raises_exception_on_repeated_labels(self): graph = graphio.load_graph(self.test_repeated_label_file, format='GEXF') with self.assertRaises(ValueError): converted_graph = graphio.convert_to_graphdataset( graph, labels_as_ids=True)
def test_load_graph_txt_undirected_symmetric_adj_matrix(self): graph = graphio.load_graph(self.test_txt_file, format='TXT') adj_matrix = nx.adjacency_matrix(graph).todense() for i in range(adj_matrix.shape[0]): for j in range(i + 1, adj_matrix.shape[1]): self.assertEquals(adj_matrix[i, j], adj_matrix[j, i])
def test_load_graph_txt_edge_number(self): graph = graphio.load_graph(self.test_txt_file, format='TXT') self.assertEquals(len(graph.edges()), 11)
def test_load_graph_gexf_edge_number(self): graph = graphio.load_graph(self.test_file, format='GEXF') self.assertEquals(len(graph.edges()), 4)
def test_load_graph_gexf_valid_format_runs(self): graphio.load_graph(self.test_file, format='GEXF')
def test_load_graph_txt_valid_format_runs(self): graphio.load_graph(self.test_txt_file, format='TXT')
def test_convert_to_graphdata_returns_graphdataset_object(self): graph = graphio.load_graph(self.test_three_group_gexf_file, format='GEXF') converted_graph = graphio.convert_to_graphdataset(graph) self.assertTrue(isinstance(converted_graph, GraphDataSet))
def test_node_attributes_txt(self): graph = graphio.load_graph(self.test_txt_file, format='TXT') self.assertEquals(graph.node['0']['group'], '0') self.assertEquals(graph.node['1']['group'], '0') self.assertEquals(graph.node['2']['group'], '0') self.assertEquals(graph.node['3']['group'], '1')
def test_convert_to_graphdata_number_of_relations(self): graph = graphio.load_graph(self.test_three_group_gexf_file, format='GEXF') converted_graph = graphio.convert_to_graphdataset(graph) self.assertEquals(len(converted_graph.relations), 3)
def test_labels_txt_are_loaded(self): graph = graphio.load_graph(self.test_txt_file, format='TXT') self.assertEquals(graph.node['0']['label'], 'node_0') self.assertEquals(graph.node['1']['label'], 'node_1') self.assertEquals(graph.node['2']['label'], 'node_2') self.assertEquals(graph.node['3']['label'], 'node_3')
def test_convert_to_graphdata_zero_matrices_relations_are_not_added(self): graph = graphio.load_graph( self.test_three_group_two_relations_gexf_file, format='GEXF') converted_graph = graphio.convert_to_graphdataset(graph) self.assertEquals(len(converted_graph.relations), 2)
def test_labels_gexf_are_loaded(self): graph = graphio.load_graph(self.test_file, format='GEXF') self.assertEquals(graph.node['0']['label'], 'Mark Johnson') self.assertEquals(graph.node['1']['label'], 'Jane Schwartz') self.assertEquals(graph.node['2']['label'], 'Ed Lopez') self.assertEquals(graph.node['3']['label'], 'Maria Lopez')
def test_convert_to_graphdata_high_number_of_entity_types(self): graph = graphio.load_graph(self.test_ten_group_gexf_file, format='GEXF') with self.assertRaises(ValueError): converted_graph = graphio.convert_to_graphdataset(graph)
def test_gexf_file_with_no_label_attr_loads_empty_strings(self): graph = graphio.load_graph(self.test_no_label_file, format='GEXF') self.assertEquals(graph.node['0']['label'], '') self.assertEquals(graph.node['1']['label'], '') self.assertEquals(graph.node['2']['label'], '') self.assertEquals(graph.node['3']['label'], '')
def test_file_with_no_group_attribute_assigns_same_to_all_txt(self): graph = graphio.load_graph(self.test_txt_no_group_file, format='TXT') self.assertEquals(graph.node['0']['group'], '0') self.assertEquals(graph.node['1']['group'], '0') self.assertEquals(graph.node['2']['group'], '0') self.assertEquals(graph.node['3']['group'], '0')
def test_load_graph_non_existing_file_raises_exception(self): with self.assertRaises(IOError): non_existing_filename = '_' graphio.load_graph(non_existing_filename, format="GEXF")