def _compute_partition(self): if self.partition is None: self.setStatusMessage('Detecting communities...') self.setBlocking(True) louvain = Louvain(resolution=self.resolution) self.partition = louvain.fit_predict(self.graph)
def run_on_graph(graph, resolution, state): # type: (nx.Graph, float, TaskState) -> Results """ Run the louvain clustering on `graph`. """ state = state # type: TaskState res = Results(resolution=resolution) louvain = Louvain(resolution=resolution, random_state=0) state.set_status("Detecting communities...") if state.is_interuption_requested(): return res partition = louvain.fit_predict(graph) res.partition = partition state.set_partial_results(("partition", res.partition)) return res
def run_on_data(data, pca_components, k_neighbors, metric, resolution, state): # type: (Table, Optional[int], int, str, float, TaskState) -> Results """ Run the louvain clustering on `data`. state is used to report progress and partial results. Returns early if `task.is_interuption_requested()` returns true. Parameters ---------- data : Table Data table pca_components : Optional[int] If not `None` then the data is first projected onto first `pca_components` principal components. k_neighbors : int Passed to `table_to_knn_graph` metric : str Passed to `table_to_knn_graph` resolution : float Passed to `Louvain` state : TaskState Returns ------- res : Results """ state = state # type: TaskState res = Results( pca_components=pca_components, k_neighbors=k_neighbors, metric=metric, resolution=resolution, ) step = 0 if state.is_interuption_requested(): return res if pca_components is not None: steps = 3 state.set_status("Computing PCA...") pca = PCA(n_components=pca_components, random_state=0) data = res.pca_projection = pca(data)(data) assert isinstance(data, Table) state.set_partial_results(("pca_projection", res.pca_projection)) step += 1 else: steps = 2 if state.is_interuption_requested(): return res state.set_progress_value(100. * step / steps) state.set_status("Building graph...") def pcallback(val): state.set_progress_value((100. * step + 100 * val) / steps) if state.is_interuption_requested(): raise InteruptRequested() try: res.graph = graph = table_to_knn_graph(data, k_neighbors=k_neighbors, metric=metric, progress_callback=pcallback) except InteruptRequested: return res state.set_partial_results(("graph", res.graph)) step += 1 state.set_progress_value(100 * step / steps) state.set_status("Detecting communities...") if state.is_interuption_requested(): return res louvain = Louvain(resolution=resolution, random_state=0) res.partition = louvain.fit_predict(graph) state.set_partial_results(("partition", res.partition)) return res
def run_on_data(data, pca_components, k_neighbors, metric, resolution, state): # type: (Table, Optional[int], int, str, float, TaskState) -> Results """ Run the louvain clustering on `data`. state is used to report progress and partial results. Returns early if `task.is_interuption_requested()` returns true. Parameters ---------- data : Table Data table pca_components : Optional[int] If not `None` then the data is first projected onto first `pca_components` principal components. k_neighbors : int Passed to `table_to_knn_graph` metric : str Passed to `table_to_knn_graph` resolution : float Passed to `Louvain` state : TaskState Returns ------- res : Results """ state = state # type: TaskState res = Results( pca_components=pca_components, k_neighbors=k_neighbors, metric=metric, resolution=resolution, ) step = 0 if state.is_interuption_requested(): return res if pca_components is not None: steps = 3 state.set_status("Computing PCA...") pca = PCA(n_components=pca_components, random_state=0) data = res.pca_projection = pca(data)(data) assert isinstance(data, Table) state.set_partial_results(("pca_projection", res.pca_projection)) step += 1 else: steps = 2 if state.is_interuption_requested(): return res state.set_progress_value(100. * step / steps) state.set_status("Building graph...") def pcallback(val): state.set_progress_value((100. * step + 100 * val) / steps) if state.is_interuption_requested(): raise InteruptRequested() try: res.graph = graph = table_to_knn_graph( data, k_neighbors=k_neighbors, metric=metric, progress_callback=pcallback ) except InteruptRequested: return res state.set_partial_results(("graph", res.graph)) step += 1 state.set_progress_value(100 * step / steps) state.set_status("Detecting communities...") if state.is_interuption_requested(): return res louvain = Louvain(resolution=resolution, random_state=0) res.partition = louvain.fit_predict(graph) state.set_partial_results(("partition", res.partition)) return res