OPTICS.test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. require('should');
  2. var clustering = require('../lib/index.js');
  3. var OPTICS = clustering.OPTICS;
  4. describe('OPTICS', function() {
  5. describe('run', function() {
  6. it('should test regular density set', function() {
  7. var dataset = [
  8. [1, 1], [0, 1], [1, 0],
  9. [10, 10], [10, 11], [11, 10],
  10. [50, 50], [51, 50], [50, 51],
  11. [100, 100]
  12. ];
  13. var optics = new OPTICS();
  14. var clusters = optics.run(dataset, 2, 2);
  15. clusters.should.be.eql([
  16. [0, 1, 2],
  17. [3, 4, 5],
  18. [6, 7, 8],
  19. [9]
  20. ]);
  21. });
  22. it('should test various density set', function() {
  23. var dataset = [
  24. [0, 0], [6, 0], [-1, 0], [0, 1], [0, -1],
  25. [45, 45], [45.1, 45.2], [45.1, 45.3], [45.8, 45.5],
  26. [45.2, 45.3],
  27. [50, 50], [56, 50], [50, 52], [50, 55], [50, 51]
  28. ];
  29. var optics = new OPTICS();
  30. var clusters = optics.run(dataset, 6, 2);
  31. clusters.should.be.eql([
  32. [0, 2, 3, 4],
  33. [1],
  34. [5, 6, 7, 9, 8],
  35. [10, 14, 12, 13],
  36. [11]
  37. ]);
  38. });
  39. });
  40. describe('regionQuery', function() {
  41. it('should return nearest neighborhood of a point', function() {
  42. var optics = new OPTICS();
  43. optics.dataset = [
  44. [1, 1], [2, 2], [3, 3],
  45. [50, 50], [51, 51]
  46. ];
  47. optics.epsilon = 2;
  48. optics._regionQuery(1).should.eql([0, 1, 2]);
  49. optics._regionQuery(4).should.eql([3, 4]);
  50. optics.epsilon = 100;
  51. optics._regionQuery(1).should.eql([0, 1, 2, 3, 4]);
  52. });
  53. });
  54. describe('euclideanDistance', function() {
  55. it('should return distance between two points', function() {
  56. var optics = new OPTICS();
  57. optics._euclideanDistance([1, 1], [3, 1]).should.eql(2);
  58. optics._euclideanDistance([1, 1], [1, 3]).should.eql(2);
  59. });
  60. });
  61. describe('getReachabilityPlot', function() {
  62. it('should return reachability plot', function() {
  63. var optics = new OPTICS();
  64. var plot;
  65. var dataset = [
  66. [1, 1], [0, 1], [1, 0],
  67. [10, 10], [10, 11], [11, 10]
  68. ];
  69. optics.run(dataset, 2, 2);
  70. plot = optics.getReachabilityPlot();
  71. plot.should.eql([
  72. [0, undefined], [1, 1], [2, 1],
  73. [3, undefined], [4, 1], [5, 1]
  74. ]);
  75. // reachability plot should be always the same for different epsilon values
  76. optics.run(dataset, 10, 2);
  77. plot = optics.getReachabilityPlot();
  78. plot.should.eql([
  79. [0, undefined], [1, 1], [2, 1],
  80. [3, undefined], [4, 1], [5, 1]
  81. ]);
  82. });
  83. });
  84. });