all these changes

This commit is contained in:
Jake Kasper
2026-04-09 13:19:47 -05:00
parent e83a51a051
commit 65315f36d1
39102 changed files with 7932979 additions and 567 deletions

View File

@@ -0,0 +1,73 @@
require('should');
var DBSCAN = require('../lib/index.js').DBSCAN;
describe('DBSCAN', function() {
describe('run', function() {
it('should return correct clusters for regular density set', function() {
var dbscan = new DBSCAN();
var dataset = [
[1, 1], [0, 1], [1, 0],
[10, 10], [10, 13], [13, 13],
[30, 30], [30, 33], [49, 49], [33, 33], [36, 36]
];
// small radius
dbscan.run(dataset, 5, 2)
.should.be.eql([
[0, 1, 2],
[3, 4, 5],
[6, 7, 9, 10]
]);
dbscan.noise.should.eql([8]);
// big radius
dbscan.run(dataset, 50, 2).should.be.eql([
[0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 8]
]);
dbscan.noise.should.eql([]);
});
});
describe('regionQuery', function() {
it('should return nearest neighborhood of a point', function() {
var dataset = [
[1, 1], [2, 2], [3, 3],
[50, 50], [51, 51]
];
var dbscan = new DBSCAN(dataset);
dbscan.epsilon = 1;
dbscan._regionQuery(1).should.eql([1]);
dbscan.epsilon = 2;
dbscan._regionQuery(1).should.eql([0, 1, 2]);
dbscan._regionQuery(4).should.eql([3, 4]);
dbscan.epsilon = 100;
dbscan._regionQuery(1).should.eql([0, 1, 2, 3, 4]);
});
});
describe('mergeArrays', function() {
it('should merge two arrays', function() {
var dbscan = new DBSCAN();
dbscan._mergeArrays([1, 2, 3], [2, 3, 4, 5])
.should.eql([1, 2, 3, 4, 5]);
dbscan._mergeArrays([2, 3, 4, 5], [1, 2, 3])
.should.eql([2, 3, 4, 5, 1]);
});
});
describe('euclideanDistance', function() {
it('should return distance between two points', function() {
var dbscan = new DBSCAN();
dbscan._euclideanDistance([1, 1], [3, 1]).should.eql(2);
dbscan._euclideanDistance([1, 1], [1, 3]).should.eql(2);
});
});
});

View File

@@ -0,0 +1,70 @@
require('should');
var KMEANS = require('../lib/index.js').KMEANS;
describe('KMEANS', function() {
describe('run', function() {
it('should return correct clusters', function() {
var kmeans = new KMEANS();
var k = 3;
var dataset = [
[1,1],[0,1],[1,0],
[10,10],[10,13],[13,13],
[54,54],[55,55],[89,89],[57,55]
];
var clusters = kmeans.run(dataset, k);
clusters.should.have.lengthOf(k);
clusters.forEach(function(cluster) {
(cluster instanceof Array).should.be.true;
cluster.length.should.be.greaterThan(0);
});
});
it('should return correct clusters for high dimensional data', function() {
var kmeans = new KMEANS();
var k = 4;
var dataset = generateData(k, 10, 10);
var clusters = kmeans.run(dataset, k);
clusters.should.have.lengthOf(k);
clusters.forEach(function(cluster) {
(cluster instanceof Array).should.be.true;
cluster.length.should.be.greaterThan(0);
});
});
});
describe('randomCentroid', function() {
it('should return extremes', function() {
var dataset = [
[-10, -20], [0,0], [30, 20]
];
var kmeans = new KMEANS(dataset);
var centroid = kmeans.randomCentroid();
centroid[0].should.be.within(-10, 30);
centroid[1].should.be.within(-20, 20);
});
});
});
function generateData(clusters, points, dimensions) {
var dataset = [];
for (var i = 0; i < clusters; i++) {
for (var p = 0; p < points; p++) {
var point = new Array(dimensions);
for (var d = 0; d < dimensions; d++) {
point[d] = Math.random() + (i * 100);
}
dataset.push(point);
}
}
return dataset;
}

View File

@@ -0,0 +1,98 @@
require('should');
var clustering = require('../lib/index.js');
var OPTICS = clustering.OPTICS;
describe('OPTICS', function() {
describe('run', function() {
it('should test regular density set', function() {
var dataset = [
[1, 1], [0, 1], [1, 0],
[10, 10], [10, 11], [11, 10],
[50, 50], [51, 50], [50, 51],
[100, 100]
];
var optics = new OPTICS();
var clusters = optics.run(dataset, 2, 2);
clusters.should.be.eql([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9]
]);
});
it('should test various density set', function() {
var dataset = [
[0, 0], [6, 0], [-1, 0], [0, 1], [0, -1],
[45, 45], [45.1, 45.2], [45.1, 45.3], [45.8, 45.5],
[45.2, 45.3],
[50, 50], [56, 50], [50, 52], [50, 55], [50, 51]
];
var optics = new OPTICS();
var clusters = optics.run(dataset, 6, 2);
clusters.should.be.eql([
[0, 2, 3, 4],
[1],
[5, 6, 7, 9, 8],
[10, 14, 12, 13],
[11]
]);
});
});
describe('regionQuery', function() {
it('should return nearest neighborhood of a point', function() {
var optics = new OPTICS();
optics.dataset = [
[1, 1], [2, 2], [3, 3],
[50, 50], [51, 51]
];
optics.epsilon = 2;
optics._regionQuery(1).should.eql([0, 1, 2]);
optics._regionQuery(4).should.eql([3, 4]);
optics.epsilon = 100;
optics._regionQuery(1).should.eql([0, 1, 2, 3, 4]);
});
});
describe('euclideanDistance', function() {
it('should return distance between two points', function() {
var optics = new OPTICS();
optics._euclideanDistance([1, 1], [3, 1]).should.eql(2);
optics._euclideanDistance([1, 1], [1, 3]).should.eql(2);
});
});
describe('getReachabilityPlot', function() {
it('should return reachability plot', function() {
var optics = new OPTICS();
var plot;
var dataset = [
[1, 1], [0, 1], [1, 0],
[10, 10], [10, 11], [11, 10]
];
optics.run(dataset, 2, 2);
plot = optics.getReachabilityPlot();
plot.should.eql([
[0, undefined], [1, 1], [2, 1],
[3, undefined], [4, 1], [5, 1]
]);
// reachability plot should be always the same for different epsilon values
optics.run(dataset, 10, 2);
plot = optics.getReachabilityPlot();
plot.should.eql([
[0, undefined], [1, 1], [2, 1],
[3, undefined], [4, 1], [5, 1]
]);
});
});
});

View File

@@ -0,0 +1,27 @@
require('should');
var PriorityQueue = require('../lib/index.js').PriorityQueue;
describe('PriorityQueue', function() {
describe('insert', function() {
it('should return correct result', function() {
var p = new PriorityQueue();
p.insert(1, 4);
p.insert(2, 1);
p.insert(3, 2);
p.insert(4, 3);
p.getElements().should.eql([1, 4, 3, 2]);
p.getPriorities().should.eql([4, 3, 2, 1]);
});
});
describe('init', function() {
it('should initialize queue correctly', function() {
var p = new PriorityQueue([1, 2, 3, 4], [4, 1, 2, 3]);
p.getElements().should.eql([1, 4, 3, 2]);
p.getPriorities().should.eql([4, 3, 2, 1]);
});
});
});