Skip to content

Commit 8b129e3

Browse files
authored
refactor: move the density clustering code to its own internal package (#33)
1 parent 2de4e6e commit 8b129e3

File tree

19 files changed

+148
-125
lines changed

19 files changed

+148
-125
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ build/
66
dist/
77
distlib/
88
target/
9-
pkg/
109
.svelte-kit/
1110
.env
1211
.env.*

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"packages/viewer",
66
"packages/table",
77
"packages/umap-wasm",
8+
"packages/density-clustering",
89
"packages/embedding-atlas",
910
"packages/examples",
1011
"packages/backend",
-140 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) 2025 Apple Inc. Licensed under MIT License.
22

3-
import { findClusters } from "../../density_clustering/find_clusters.js";
3+
import { findClusters } from "@embedding-atlas/density-clustering";
44
import { dynamicLabelPlacement } from "../../dynamic_label_placement/dynamic_label_placement.js";
55

66
export { dynamicLabelPlacement, findClusters };

packages/component/src/lib/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ export {
88
type EmbeddingViewProps,
99
} from "./embedding_view/api.js";
1010

11-
export { findClusters, type Cluster, type FindClustersOptions } from "./density_clustering/find_clusters.js";
12-
1311
export { defaultCategoryColors } from "./colors.js";
1412

1513
export type { Theme } from "./embedding_view/theme.js";

packages/density-clustering/density_clustering_wasm/build.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/** A resulting cluster from the find clusters function */
2+
export interface Cluster {
3+
/** Cluster identifier */
4+
identifier: number;
5+
/** The total density */
6+
sum_density: number;
7+
/** The mean x location (weighted by density) */
8+
mean_x: number;
9+
/** The mean y location (weighted by density) */
10+
mean_y: number;
11+
/** The maximum density */
12+
max_density: number;
13+
/** The location with the maximum density */
14+
max_density_location: [number, number];
15+
/** The number of pixels in the cluster */
16+
pixel_count: number;
17+
/** The cluster's boundary represented as a list of polygons */
18+
boundary?: [number, number][][];
19+
/** The cluster's boundary approximated with a list of rectangles */
20+
boundary_rect_approximation?: [number, number, number, number][];
21+
}
22+
23+
/** Options of the find clusters function */
24+
export interface FindClustersOptions {
25+
/** The threshold for unioning two clusters */
26+
union_threshold: number;
27+
}
28+
29+
/**
30+
* Find clusters from a density map
31+
* @param density_map the density map, a `Float32Array` with `width * height` elements
32+
* @param width the width of the density map
33+
* @param height the height of the density map
34+
* @param options algorithm options
35+
* @returns
36+
*/
37+
export async function findClusters(
38+
density_map: Float32Array,
39+
width: number,
40+
height: number,
41+
options: Partial<FindClustersOptions> = {},
42+
): Promise<Cluster[]>;

packages/component/src/lib/density_clustering/find_clusters.ts renamed to packages/density-clustering/density_clustering_wasm/js/index.js

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,6 @@
11
// Copyright (c) 2025 Apple Inc. Licensed under MIT License.
22

3-
import * as cluster from "./wasm/density_clustering_wasm.js";
4-
5-
/** A resulting cluster from the find clusters function */
6-
export interface Cluster {
7-
/** Cluster identifier */
8-
identifier: number;
9-
/** The total density */
10-
sum_density: number;
11-
/** The mean x location (weighted by density) */
12-
mean_x: number;
13-
/** The mean y location (weighted by density) */
14-
mean_y: number;
15-
/** The maximum density */
16-
max_density: number;
17-
/** The location with the maximum density */
18-
max_density_location: [number, number];
19-
/** The number of pixels in the cluster */
20-
pixel_count: number;
21-
/** The cluster's boundary represented as a list of polygons */
22-
boundary?: [number, number][][];
23-
/** The cluster's boundary approximated with a list of rectangles */
24-
boundary_rect_approximation?: [number, number, number, number][];
25-
}
26-
27-
/** Options of the find clusters function */
28-
export interface FindClustersOptions {
29-
/** The threshold for unioning two clusters */
30-
union_threshold: number;
31-
}
3+
import * as cluster from "../pkg/density_clustering_wasm.js";
324

335
/**
346
* Find clusters from a density map
@@ -38,12 +10,7 @@ export interface FindClustersOptions {
3810
* @param options algorithm options
3911
* @returns
4012
*/
41-
export async function findClusters(
42-
density_map: Float32Array,
43-
width: number,
44-
height: number,
45-
options: Partial<FindClustersOptions> = {},
46-
): Promise<Cluster[]> {
13+
export async function findClusters(density_map, width, height, options = {}) {
4714
await cluster.default();
4815
// console.debug(`find clusters start, size: ${width}x${height}`);
4916
let t0 = new Date().getTime();
@@ -62,7 +29,7 @@ export async function findClusters(
6229
smooth_boundaries: true,
6330
});
6431
input.free();
65-
let clusters: Cluster[] = [];
32+
let clusters = [];
6633
for (let [id, summary] of result.summaries) {
6734
clusters.push({
6835
identifier: id,
File renamed without changes.

0 commit comments

Comments
 (0)