def test_spectral_clustering_with_arpack_amg_solvers(): # Test that spectral_clustering is the same for arpack and amg solver # Based on toy example from plot_segmentation_toy.py # a small two coin image x, y = np.indices((40, 40)) center1, center2 = (14, 12), (20, 25) radius1, radius2 = 8, 7 circle1 = (x - center1[0]) ** 2 + (y - center1[1]) ** 2 < radius1 ** 2 circle2 = (x - center2[0]) ** 2 + (y - center2[1]) ** 2 < radius2 ** 2 circles = circle1 | circle2 mask = circles.copy() img = circles.astype(float) graph = img_to_graph(img, mask=mask) graph.data = np.exp(-graph.data / graph.data.std()) labels_arpack = spectral_clustering( graph, n_clusters=2, eigen_solver="arpack", random_state=0 ) assert len(np.unique(labels_arpack)) == 2 if amg_loaded: labels_amg = spectral_clustering( graph, n_clusters=2, eigen_solver="amg", random_state=0 ) assert adjusted_rand_score(labels_arpack, labels_amg) == 1 else: with pytest.raises(ValueError): spectral_clustering(graph, n_clusters=2, eigen_solver="amg", random_state=0)
def img_to_graph(image): """Converts an image to a graph. This function takes in an image and returns a 4-connected grid graph. The nodes of this graph are labeled as such: every pixel is a node, the label of each node is the corresponding (x, y) coordinates. Args: image (numpy_array): The input image. Returns: A network X graph. """ image = image.astype(np.int16) coo_matrix = feature_extraction.img_to_graph(image) graph = nx.from_scipy_sparse_matrix(coo_matrix) node_labels = graph.nodes() node_labels = np.array(node_labels) node_labels = node_labels.reshape(image.shape) mapping = {} for index, x in np.ndenumerate(node_labels): mapping[x] = index graph = nx.relabel_nodes(graph, mapping) graph.remove_edges_from(graph.selfloop_edges()) return graph
def test_spectral_clustering_with_arpack_amg_solvers(): # Test that spectral_clustering is the same for arpack and amg solver # Based on toy example from plot_segmentation_toy.py # a small two coin image x, y = np.indices((40, 40)) center1, center2 = (14, 12), (20, 25) radius1, radius2 = 8, 7 circle1 = (x - center1[0]) ** 2 + (y - center1[1]) ** 2 < radius1 ** 2 circle2 = (x - center2[0]) ** 2 + (y - center2[1]) ** 2 < radius2 ** 2 circles = circle1 | circle2 mask = circles.copy() img = circles.astype(float) graph = img_to_graph(img, mask=mask) graph.data = np.exp(-graph.data / graph.data.std()) labels_arpack = spectral_clustering( graph, n_clusters=2, eigen_solver='arpack', random_state=0) assert len(np.unique(labels_arpack)) == 2 if amg_loaded: labels_amg = spectral_clustering( graph, n_clusters=2, eigen_solver='amg', random_state=0) assert adjusted_rand_score(labels_arpack, labels_amg) == 1 else: assert_raises( ValueError, spectral_clustering, graph, n_clusters=2, eigen_solver='amg', random_state=0)
def make_graph(self): """Создание графов @return Список графов, причем индекс графа совпадает с индексом изображения """ result = [] for image in self.image_list: graph = img_to_graph(image) graph.data = np.exp(- self.graph_beta * graph.data / graph.data.std()) + self.graph_eps result.append(graph) return result