From a0f792fabcc6fe74554c3039881000daa35934a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20=C5=BDigutyt=C4=97?= Date: Wed, 29 Mar 2023 21:14:52 +0200 Subject: [PATCH] add min distance for umap parameter --- .../_dimensionality_reduction.py | 32 +++++++++++++++++-- .../_tests/test_dimension_reduction.py | 5 ++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/napari_clusters_plotter/_dimensionality_reduction.py b/napari_clusters_plotter/_dimensionality_reduction.py index a9067f59..7c357060 100644 --- a/napari_clusters_plotter/_dimensionality_reduction.py +++ b/napari_clusters_plotter/_dimensionality_reduction.py @@ -51,6 +51,7 @@ # therefore by default the following value is False. # See more: https://github.com/BiAPoL/napari-clusters-plotter/issues/169 "umap_separate_thread": False, + "min_distance_umap": 0.1, } EXCLUDE = [ID_NAME, POINTER, "UMAP", "t-SNE", "PCA"] @@ -196,7 +197,7 @@ def __init__(self, napari_viewer): False ) # hide this container until umap is selected - self.settings_container_multithreading, self.multithreading = checkbox( + self.settings_container_multithreading, self.umap_separate_thread = checkbox( name="Enable Multi-threading", value=DEFAULTS["umap_separate_thread"], visible=True, @@ -207,6 +208,20 @@ def __init__(self, napari_viewer): self.settings_container_multithreading ) + ( + self.min_distance_umap_container, + self.min_distance_umap, + ) = float_sbox_containter_and_selection( + name="Minimum Distance", + label="Minimum Distance", + value=DEFAULTS["min_distance_umap"], + step=0.01, + min=0, + visible=False, + tool_tip="The minimum distance apart that points are allowed to be in the low dimensional representation.", + tool_link="https://umap-learn.readthedocs.io/en/latest/parameters.html#min-dist", + ) + # additional options for MDS (self.mds_metric_container, self.mds_metric) = checkbox( "Metric", @@ -286,7 +301,8 @@ def run_clicked(): self.explained_variance.value, self.pca_components.value, self.n_components.value, - self.multithreading.value, + self.umap_separate_thread.value, + self.min_distance_umap.value, self.mds_metric.value, self.mds_n_init.value, self.mds_max_iter.value, @@ -318,6 +334,7 @@ def run_clicked(): self.layout().addWidget(self.n_neighbors_container) self.layout().addWidget(self.pca_components_container) self.layout().addWidget(self.n_components_container) + self.layout().addWidget(self.min_distance_umap_container) self.layout().addWidget(self.explained_variance_container) self.layout().addWidget(self.settings_container_scaler) self.layout().addWidget(self.mds_metric_container) @@ -372,6 +389,7 @@ def change_settings_visibility(self): """ widgets_active( self.n_neighbors_container, + self.min_distance_umap_container, self.advanced_options_container, active=self.algorithm_choice_list.current_choice == self.Options.UMAP.value, ) @@ -434,6 +452,7 @@ def run( pca_components, n_components, umap_multithreading, + min_dist, mds_metric, mds_n_init, mds_max_iter, @@ -544,6 +563,7 @@ def return_func_dim_reduction(result): properties_to_reduce, n_neighbors=n_neighbours, n_components=n_components, + min_dist=min_dist, verbose=True, _progress=True, ) @@ -562,6 +582,7 @@ def return_func_dim_reduction(result): properties_to_reduce, n_neighbors=n_neighbours, n_components=n_components, + min_dist=min_dist, verbose=False, ) @@ -626,7 +647,11 @@ def return_func_dim_reduction(result): @catch_NaNs def umap( - reg_props: pd.DataFrame, n_neighbors: int, n_components: int, verbose: bool = False + reg_props: pd.DataFrame, + n_neighbors: int, + n_components: int, + min_dist: float, + verbose: bool = False, ) -> Tuple[str, np.ndarray]: """ Performs dimensionality reduction using the Uniform Manifold Approximation Projection (UMAP) on the given data. @@ -664,6 +689,7 @@ def umap( n_components=n_components, n_neighbors=n_neighbors, verbose=verbose, + min_dist=min_dist, tqdm_kwds={"desc": "Dimensionality reduction progress"}, ) return "UMAP", reducer.fit_transform(reg_props) diff --git a/napari_clusters_plotter/_tests/test_dimension_reduction.py b/napari_clusters_plotter/_tests/test_dimension_reduction.py index 32a55075..a321e05a 100644 --- a/napari_clusters_plotter/_tests/test_dimension_reduction.py +++ b/napari_clusters_plotter/_tests/test_dimension_reduction.py @@ -73,6 +73,7 @@ def test_bad_measurements(qtbot, make_napari_viewer): mds_max_iter=300, mds_eps=0.001, umap_multithreading=True, + min_dist=0.1, ) blocker = qtbot.waitSignal(widget.worker.finished, timeout=1000000) @@ -181,7 +182,9 @@ def test_umap(): nr_components = 2 # umap returns (str, np.ndarray), where the first item is algorithm name - result = umap(pd.DataFrame(X), n_neighbors=2, n_components=nr_components) + result = umap( + pd.DataFrame(X), n_neighbors=2, n_components=nr_components, min_dist=0.1 + ) assert result[1].shape[-1] == nr_components