Skip to content

Commit

Permalink
add min distance for umap parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
lazigu committed Mar 29, 2023
1 parent f52080c commit a0f792f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
32 changes: 29 additions & 3 deletions napari_clusters_plotter/_dimensionality_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -434,6 +452,7 @@ def run(
pca_components,
n_components,
umap_multithreading,
min_dist,
mds_metric,
mds_n_init,
mds_max_iter,
Expand Down Expand Up @@ -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,
)
Expand All @@ -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,
)

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion napari_clusters_plotter/_tests/test_dimension_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a0f792f

Please sign in to comment.