Skip to content

Commit 843440f

Browse files
authored
refactor: [breaking] change umap, knn, and findClusters API to camelCase (#34)
1 parent 8b129e3 commit 843440f

File tree

9 files changed

+99
-96
lines changed

9 files changed

+99
-96
lines changed

packages/component/src/lib/embedding_view/EmbeddingViewImpl.svelte

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
interface Cluster {
3737
x: number;
3838
y: number;
39-
sum_density: number;
39+
sumDensity: number;
4040
rects: Rectangle[];
4141
bandwidth: number;
4242
label?: string | null;
@@ -548,12 +548,12 @@
548548
viewport: ViewportState,
549549
): Promise<Cluster[]> {
550550
let map = await renderer.densityMap(1000, 1000, bandwidth, viewport);
551-
let cs = await findClusters(map.data, map.width, map.height, { union_threshold: bandwidth });
552-
let collectedClusters = [];
551+
let cs = await findClusters(map.data, map.width, map.height);
552+
let collectedClusters: Cluster[] = [];
553553
for (let idx = 0; idx < cs.length; idx++) {
554554
let c = cs[idx];
555-
let coord = map.coordinateAtPixel(c.mean_x, c.mean_y);
556-
let rects: Rectangle[] = c.boundary_rect_approximation!.map(([x1, y1, x2, y2]) => {
555+
let coord = map.coordinateAtPixel(c.meanX, c.meanY);
556+
let rects: Rectangle[] = c.boundaryRectApproximation!.map(([x1, y1, x2, y2]) => {
557557
let p1 = map.coordinateAtPixel(x1, y1);
558558
let p2 = map.coordinateAtPixel(x2, y2);
559559
return {
@@ -566,14 +566,14 @@
566566
collectedClusters.push({
567567
x: coord.x,
568568
y: coord.y,
569-
sum_density: c.sum_density,
569+
sumDensity: c.sumDensity,
570570
rects: rects,
571571
bandwidth: bandwidth,
572572
});
573573
}
574-
let maxDensity = collectedClusters.reduce((a, b) => Math.max(a, b.sum_density), 0);
574+
let maxDensity = collectedClusters.reduce((a, b) => Math.max(a, b.sumDensity), 0);
575575
let threshold = maxDensity * 0.005;
576-
return collectedClusters.filter((x) => x.sum_density > threshold);
576+
return collectedClusters.filter((x) => x.sumDensity > threshold);
577577
}
578578
579579
async function generateLabels(viewport: ViewportState): Promise<InitialLabel[]> {
@@ -610,7 +610,7 @@
610610
text: x.label!,
611611
x: x.x,
612612
y: x.y,
613-
priority: x.sum_density,
613+
priority: x.sumDensity,
614614
level: x.bandwidth == 10 ? 0 : 1,
615615
}));
616616

packages/density-clustering/density_clustering_wasm/js/index.d.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ export interface Cluster {
33
/** Cluster identifier */
44
identifier: number;
55
/** The total density */
6-
sum_density: number;
6+
sumDensity: number;
77
/** The mean x location (weighted by density) */
8-
mean_x: number;
8+
meanX: number;
99
/** The mean y location (weighted by density) */
10-
mean_y: number;
10+
meanY: number;
1111
/** The maximum density */
12-
max_density: number;
12+
maxDensity: number;
1313
/** The location with the maximum density */
14-
max_density_location: [number, number];
14+
maxDensityLocation: [number, number];
1515
/** The number of pixels in the cluster */
16-
pixel_count: number;
16+
pixelCount: number;
1717
/** The cluster's boundary represented as a list of polygons */
1818
boundary?: [number, number][][];
19-
/** The cluster's boundary approximated with a list of rectangles */
20-
boundary_rect_approximation?: [number, number, number, number][];
19+
/** The cluster's boundary approximated with a list of rectangles, each rectangle is given as an array [x1, y1, x2, y2] */
20+
boundaryRectApproximation?: [number, number, number, number][];
2121
}
2222

2323
/** Options of the find clusters function */
2424
export interface FindClustersOptions {
2525
/** The threshold for unioning two clusters */
26-
union_threshold: number;
26+
unionThreshold: number;
2727
}
2828

2929
/**
@@ -34,9 +34,9 @@ export interface FindClustersOptions {
3434
* @param options algorithm options
3535
* @returns
3636
*/
37-
export async function findClusters(
38-
density_map: Float32Array,
37+
export function findClusters(
38+
densityMap: Float32Array,
3939
width: number,
4040
height: number,
41-
options: Partial<FindClustersOptions> = {},
41+
options?: Partial<FindClustersOptions>,
4242
): Promise<Cluster[]>;

packages/density-clustering/density_clustering_wasm/js/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ import * as cluster from "../pkg/density_clustering_wasm.js";
44

55
/**
66
* Find clusters from a density map
7-
* @param density_map the density map, a `Float32Array` with `width * height` elements
7+
* @param densityMap the density map, a `Float32Array` with `width * height` elements
88
* @param width the width of the density map
99
* @param height the height of the density map
1010
* @param options algorithm options
1111
* @returns
1212
*/
13-
export async function findClusters(density_map, width, height, options = {}) {
13+
export async function findClusters(densityMap, width, height, options = {}) {
1414
await cluster.default();
1515
// console.debug(`find clusters start, size: ${width}x${height}`);
1616
let t0 = new Date().getTime();
17-
let input = new cluster.DensityMap(width, height, density_map);
17+
let input = new cluster.DensityMap(width, height, densityMap);
1818
let result = cluster.find_clusters(input, {
1919
clustering_options: {
2020
use_disjoint_set: true,
2121
truncate_to_max_density: true,
2222
perform_neighbor_map_grouping: false,
23-
union_threshold: 10.0,
23+
union_threshold: options.unionThreshold ?? 10,
2424
density_upperbound_scaler: 0.2,
2525
density_lowerbound_scaler: 0.2,
2626
...options,
@@ -33,14 +33,14 @@ export async function findClusters(density_map, width, height, options = {}) {
3333
for (let [id, summary] of result.summaries) {
3434
clusters.push({
3535
identifier: id,
36-
sum_density: summary.sum_density,
37-
mean_x: summary.sum_x_density / summary.sum_density,
38-
mean_y: summary.sum_y_density / summary.sum_density,
39-
max_density: summary.max_density,
40-
max_density_location: summary.max_density_location,
41-
pixel_count: summary.num_pixels,
36+
sumDensity: summary.sum_density,
37+
meanX: summary.sum_x_density / summary.sum_density,
38+
meanY: summary.sum_y_density / summary.sum_density,
39+
maxDensity: summary.max_density,
40+
maxDensityLocation: summary.max_density_location,
41+
pixelCount: summary.num_pixels,
4242
boundary: result.boundaries.get(id),
43-
boundary_rect_approximation: result.boundary_rects.get(id),
43+
boundaryRectApproximation: result.boundary_rects.get(id),
4444
});
4545
}
4646
clusters = clusters.filter((x) => x.boundary != null);

packages/docs/algorithms.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ To initialize the UMAP algorithm, use `createUMAP`:
1616
import { createUMAP } from "embedding-atlas";
1717

1818
let count = 2000;
19-
let input_dim = 100;
20-
let output_dim = 2;
19+
let inputDim = 100;
20+
let outputDim = 2;
2121

22-
// The data must be a Float32Array with count * input_dim elements.
23-
let data = new Float32Array(count * input_dim);
22+
// The data must be a Float32Array with count * inputDim elements.
23+
let data = new Float32Array(count * inputDim);
2424
// ... fill in the data
2525

2626
let options = {
2727
metric: "cosine",
2828
};
2929

3030
// Use `createUMAP` to initialize the algorithm.
31-
let umap = await createUMAP(count, input_dim, output_dim, data, options);
31+
let umap = await createUMAP(count, inputDim, outputDim, data, options);
3232
```
3333

3434
After initialization, use the `run` method to update the embedding coordinates:
@@ -48,7 +48,7 @@ for (let i = 0; i < 100; i++) {
4848
At any time, you can get the current embedding by calling the `embedding` method.
4949

5050
```js
51-
// The result is a Float32Array with count * output_dim elements.
51+
// The result is a Float32Array with count * outputDim elements.
5252
let embedding = umap.embedding();
5353
```
5454

@@ -66,22 +66,22 @@ In addition, you can also use the `createKNN` function to perform approximate ne
6666
import { createKNN } from "embedding-atlas";
6767

6868
let count = 2000;
69-
let input_dim = 100;
69+
let inputDim = 100;
7070

71-
// The data must be a Float32Array with count * input_dim elements.
72-
let data = new Float32Array(count * input_dim);
71+
// The data must be a Float32Array with count * inputDim elements.
72+
let data = new Float32Array(count * inputDim);
7373
// ... fill in the data
7474

7575
let options = {
7676
metric: "cosine",
7777
};
7878

7979
// Create the KNN instance
80-
let knn = await createKNN(count, input_dim, data, options);
80+
let knn = await createKNN(count, inputDim, data, options);
8181

8282
// Perform queries
83-
let query = new Float32Array(input_dim);
84-
knn.query_by_vector(query, k);
83+
let query = new Float32Array(inputDim);
84+
knn.queryByVector(query, k);
8585

8686
// Destroy the instance
8787
knn.destroy();
@@ -96,7 +96,7 @@ To run the algorithm, use `findClusters`.
9696
import { findClusters } from "embedding-atlas";
9797

9898
// A density map of width * height floating point numbers.
99-
let density_map: Float32Array;
99+
let densityMap: Float32Array;
100100

101-
clusters = await findClusters(density_map, width, height);
101+
clusters = await findClusters(densityMap, width, height);
102102
```

packages/examples/src/svelte/FindClustersExample.svelte

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
amplitude: randomUniform(0.1, 1, rng),
2525
});
2626
}
27-
let density_map = new Float32Array(width * height);
27+
let densityMap = new Float32Array(width * height);
2828
for (let y = 0; y < width; y++) {
2929
for (let x = 0; x < width; x++) {
3030
let d = 0;
@@ -37,20 +37,20 @@
3737
ry /= g.s2;
3838
d += Math.exp(-(rx ** 2 + ry ** 2)) * g.amplitude;
3939
}
40-
density_map[y * width + x] = d * 80;
40+
densityMap[y * width + x] = d * 80;
4141
}
4242
}
43-
return density_map;
43+
return densityMap;
4444
}
4545
4646
async function run() {
47-
const density_map = generateDensity(width, height);
48-
clusters = await findClusters(density_map, width, height);
47+
const densityMap = generateDensity(width, height);
48+
clusters = await findClusters(densityMap, width, height);
4949
5050
let ctx = canvas.getContext("2d")!;
5151
let data = ctx.getImageData(0, 0, width, height);
5252
for (let i = 0; i < width * height; i++) {
53-
let value = density_map[i];
53+
let value = densityMap[i];
5454
data.data[i * 4 + 0] = value;
5555
data.data[i * 4 + 1] = value;
5656
data.data[i * 4 + 2] = value;
@@ -67,7 +67,7 @@
6767
<svg style:position="absolute" width={width} height={height}>
6868
{#each clusters as c}
6969
<g>
70-
{#each c.boundary_rect_approximation ?? [] as [x1, y1, x2, y2]}
70+
{#each c.boundaryRectApproximation ?? [] as [x1, y1, x2, y2]}
7171
<rect x={x1} y={y1} width={x2 - x1} height={y2 - y1} style:stroke="rgba(255,0,0,0.1)" style:fill="none" />
7272
{/each}
7373
{#each c.boundary ?? [] as boundary}
@@ -77,7 +77,7 @@
7777
style:fill="rgba(255,127,14,0.1)"
7878
/>
7979
{/each}
80-
<circle cx={c.mean_x} cy={c.mean_y} r={2} style:fill="rgba(255,127,14,1)" />
80+
<circle cx={c.meanX} cy={c.meanY} r={2} style:fill="rgba(255,127,14,1)" />
8181
</g>
8282
{/each}
8383
</svg>

packages/umap-wasm/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ To initialize the algorithm, use `createUMAP`:
1616
import { createUMAP } from "umap-wasm";
1717

1818
let count = 2000;
19-
let input_dim = 100;
20-
let output_dim = 2;
19+
let inputDim = 100;
20+
let outputDim = 2;
2121

22-
// The data must be a Float32Array with count * input_dim elements.
23-
let data = new Float32Array(count * input_dim);
22+
// The data must be a Float32Array with count * inputDim elements.
23+
let data = new Float32Array(count * inputDim);
2424
// ... fill in the data
2525

2626
let options = {
2727
metric: "cosine",
2828
};
2929

3030
// Use `createUMAP` to initialize the algorithm.
31-
let umap = await createUMAP(count, input_dim, output_dim, data, options);
31+
let umap = await createUMAP(count, inputDim, outputDim, data, options);
3232
```
3333

3434
After initialization, use the `run` method to update the embedding coordinates:
@@ -48,7 +48,7 @@ for (let i = 0; i < 100; i++) {
4848
At any time, you can get the current embedding by calling the `embedding` method.
4949

5050
```js
51-
// The result is a Float32Array with count * output_dim elements.
51+
// The result is a Float32Array with count * outputDim elements.
5252
let embedding = umap.embedding();
5353
```
5454

0 commit comments

Comments
 (0)