Skip to content

Commit

Permalink
Add index.tsx to dbscan
Browse files Browse the repository at this point in the history
  • Loading branch information
spcfox committed Jun 16, 2023
1 parent b33e100 commit 7a574af
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/visualizers/dbscan/dbscan.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bind, here } from "../../lib";
import { bind, here } from "./";

export const dbscan = async (points: number[][], eps: number, min_samples: number) => {
let labels = new Array(points.length).fill(0);
Expand All @@ -12,8 +12,8 @@ export const dbscan = async (points: number[][], eps: number, min_samples: numbe
let neighbors = findNeighbors(points, eps);
for (let i = 0; i < points.length; i++) {
if (await expandCluster(labels, neighbors, i, cluster, min_samples)) {
await here("new_cluster", cluster.value);
cluster.value++;
await here("new_cluster", cluster.value);
}
}
await here("done");
Expand All @@ -31,13 +31,13 @@ const expandCluster = async (
}

if (neighbors[i].length < min_samples) {
await here("noise", i, -1, neighbors[i].length);
labels[i] = -1;
await here("noise", i, neighbors[i].length);
return false;
}

await here("expand_cluster", i, cluster.value, neighbors[i].length);
labels[i] = cluster.value;
await here("expand_cluster", i, neighbors[i].length);
for (let neighbor of neighbors[i]) {
if (labels[neighbor] === 0) {
await expandCluster(labels, neighbors, neighbor, cluster, min_samples);
Expand Down Expand Up @@ -65,9 +65,7 @@ const distance = (p1: number[], p2: number[]): number =>

// TODO: move to lib?
class Mutable<T> {
constructor(public value: T) {
this.value = value;
}
constructor(public value: T) {}
}

export type DBScanState = {
Expand All @@ -78,21 +76,26 @@ export type DBScanState = {

export type DBScanEvent = Start | Done | NewCluster | ExpandCluster | Noise;

type Start = { name: "start"; }
type Done = { name: "done";}
// TODO: move Start and Done to lib?
type Start = {
name: "start",
args: [],
}
type Done = {
name: "done",
args: [],
}
type NewCluster = {
name: "new_cluster";
cluster: number;
args: [cluster: number];
}
type ExpandCluster = {
name: "expand_cluster";
point: number;
neighbors_count: number;
args: [point: number, neighbors_count: number];
}
type Noise = {
name: "noise";
point: number;
neighbors_count: number;
args: [point: number, neighbors_count: number];
}

export type DBScanArguments = [number[][], number, number];
21 changes: 21 additions & 0 deletions src/visualizers/dbscan/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AlgorithmManifest } from "../../lib/manifest";
import { RuntimeStore } from "../../lib/store";
import { DBScanArguments, DBScanEvent, DBScanState, dbscan } from "./dbscan";
import { DBScanRender } from "./render";
import { DBScanStarter } from "./start";

export const manifest: AlgorithmManifest<DBScanState, DBScanEvent, DBScanArguments> = {
algo: dbscan,
startComponent: DBScanStarter,
renderComponent: DBScanRender
}

export const globalStore = new RuntimeStore<DBScanState, DBScanEvent, DBScanArguments>(dbscan);

export const bind = (name: keyof DBScanState, value: DBScanState[keyof DBScanState]) => {
globalStore.bind(name, value);
}

export const here = async (name: DBScanEvent["name"], ...args: DBScanEvent["args"]): Promise<void> => {
return globalStore.here(name, ...args);
}

0 comments on commit 7a574af

Please sign in to comment.