-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
also add hdbscan as an alternative approach
- Loading branch information
1 parent
de1ba04
commit 1024e79
Showing
11 changed files
with
121 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/depiction_targeted_preproc/workflow/proc/cluster_hdbscan.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from pathlib import Path | ||
from typing import Annotated | ||
|
||
import math | ||
import numpy as np | ||
import typer | ||
import xarray | ||
from hdbscan.flat import (HDBSCAN_flat) | ||
from sklearn.preprocessing import StandardScaler | ||
from typer import Option | ||
|
||
from depiction.image.multi_channel_image import MultiChannelImage | ||
from depiction_targeted_preproc.workflow.proc.cluster_kmeans import retain_strongest_signals | ||
|
||
|
||
def cluster_dbscan(input_netcdf_path: Annotated[Path, Option()], output_netcdf_path: Annotated[Path, Option()]) -> None: | ||
image = MultiChannelImage.read_hdf5(input_netcdf_path) | ||
# TODO make configurable | ||
n_clusters = 10 | ||
n_features = 50 | ||
|
||
reduced_data = retain_strongest_signals(image.data_flat.transpose("i", "c"), n_features) | ||
|
||
scaler = StandardScaler() | ||
scaler.fit(reduced_data.values) | ||
|
||
# kmeans = BisectingKMeans(n_clusters=n_clusters) | ||
# dbscan = (eps=0.3, min_samples=10) | ||
data_scaled = scaler.transform(reduced_data.values) | ||
|
||
# clusterer = hdbscan.HDBSCAN() | ||
# clusterer.fit(data_scaled) | ||
# clusters = clusterer.labels_ | ||
|
||
clusterer = HDBSCAN_flat(data_scaled, | ||
cluster_selection_method='eom', | ||
n_clusters=10, | ||
min_cluster_size=math.ceil(0.02*data_scaled.shape[0])) | ||
clusters = clusterer.labels_ | ||
|
||
cluster_data = xarray.DataArray(clusters, dims=("i",), coords={"i": image.data_flat.coords["i"]}).expand_dims("c") | ||
cluster_data.coords["c"] = ["cluster"] | ||
cluster_data.attrs["bg_value"] = np.nan | ||
cluster_image = MultiChannelImage(cluster_data.unstack("i")) | ||
cluster_image.write_hdf5(output_netcdf_path) | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(cluster_dbscan) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters