OPTICS.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * @requires ./PriorityQueue.js
  3. */
  4. if (typeof module !== 'undefined' && module.exports) {
  5. var PriorityQueue = require('./PriorityQueue.js');
  6. }
  7. /**
  8. * OPTICS - Ordering points to identify the clustering structure
  9. *
  10. * @author Lukasz Krawczyk <contact@lukaszkrawczyk.eu>
  11. * @copyright MIT
  12. */
  13. /**
  14. * OPTICS class constructor
  15. * @constructor
  16. *
  17. * @param {Array} dataset
  18. * @param {number} epsilon
  19. * @param {number} minPts
  20. * @param {function} distanceFunction
  21. * @returns {OPTICS}
  22. */
  23. function OPTICS(dataset, epsilon, minPts, distanceFunction) {
  24. /** @type {number} */
  25. this.epsilon = 1;
  26. /** @type {number} */
  27. this.minPts = 1;
  28. /** @type {function} */
  29. this.distance = this._euclideanDistance;
  30. // temporary variables used during computation
  31. /** @type {Array} */
  32. this._reachability = [];
  33. /** @type {Array} */
  34. this._processed = [];
  35. /** @type {number} */
  36. this._coreDistance = 0;
  37. /** @type {Array} */
  38. this._orderedList = [];
  39. this._init(dataset, epsilon, minPts, distanceFunction);
  40. }
  41. /******************************************************************************/
  42. // pulic functions
  43. /**
  44. * Start clustering
  45. *
  46. * @param {Array} dataset
  47. * @returns {undefined}
  48. * @access public
  49. */
  50. OPTICS.prototype.run = function(dataset, epsilon, minPts, distanceFunction) {
  51. this._init(dataset, epsilon, minPts, distanceFunction);
  52. for (var pointId = 0, l = this.dataset.length; pointId < l; pointId++) {
  53. if (this._processed[pointId] !== 1) {
  54. this._processed[pointId] = 1;
  55. this.clusters.push([pointId]);
  56. var clusterId = this.clusters.length - 1;
  57. this._orderedList.push(pointId);
  58. var priorityQueue = new PriorityQueue(null, null, 'asc');
  59. var neighbors = this._regionQuery(pointId);
  60. // using priority queue assign elements to new cluster
  61. if (this._distanceToCore(pointId) !== undefined) {
  62. this._updateQueue(pointId, neighbors, priorityQueue);
  63. this._expandCluster(clusterId, priorityQueue);
  64. }
  65. }
  66. }
  67. return this.clusters;
  68. };
  69. /**
  70. * Generate reachability plot for all points
  71. *
  72. * @returns {array}
  73. * @access public
  74. */
  75. OPTICS.prototype.getReachabilityPlot = function() {
  76. var reachabilityPlot = [];
  77. for (var i = 0, l = this._orderedList.length; i < l; i++) {
  78. var pointId = this._orderedList[i];
  79. var distance = this._reachability[pointId];
  80. reachabilityPlot.push([pointId, distance]);
  81. }
  82. return reachabilityPlot;
  83. };
  84. /******************************************************************************/
  85. // protected functions
  86. /**
  87. * Set object properties
  88. *
  89. * @param {Array} dataset
  90. * @param {number} epsilon
  91. * @param {number} minPts
  92. * @param {function} distance
  93. * @returns {undefined}
  94. * @access protected
  95. */
  96. OPTICS.prototype._init = function(dataset, epsilon, minPts, distance) {
  97. if (dataset) {
  98. if (!(dataset instanceof Array)) {
  99. throw Error('Dataset must be of type array, ' +
  100. typeof dataset + ' given');
  101. }
  102. this.dataset = dataset;
  103. this.clusters = [];
  104. this._reachability = new Array(this.dataset.length);
  105. this._processed = new Array(this.dataset.length);
  106. this._coreDistance = 0;
  107. this._orderedList = [];
  108. }
  109. if (epsilon) {
  110. this.epsilon = epsilon;
  111. }
  112. if (minPts) {
  113. this.minPts = minPts;
  114. }
  115. if (distance) {
  116. this.distance = distance;
  117. }
  118. };
  119. /**
  120. * Update information in queue
  121. *
  122. * @param {number} pointId
  123. * @param {Array} neighbors
  124. * @param {PriorityQueue} queue
  125. * @returns {undefined}
  126. * @access protected
  127. */
  128. OPTICS.prototype._updateQueue = function(pointId, neighbors, queue) {
  129. var self = this;
  130. this._coreDistance = this._distanceToCore(pointId);
  131. neighbors.forEach(function(pointId2) {
  132. if (self._processed[pointId2] === undefined) {
  133. var dist = self.distance(self.dataset[pointId], self.dataset[pointId2]);
  134. var newReachableDistance = Math.max(self._coreDistance, dist);
  135. if (self._reachability[pointId2] === undefined) {
  136. self._reachability[pointId2] = newReachableDistance;
  137. queue.insert(pointId2, newReachableDistance);
  138. } else {
  139. if (newReachableDistance < self._reachability[pointId2]) {
  140. self._reachability[pointId2] = newReachableDistance;
  141. queue.remove(pointId2);
  142. queue.insert(pointId2, newReachableDistance);
  143. }
  144. }
  145. }
  146. });
  147. };
  148. /**
  149. * Expand cluster
  150. *
  151. * @param {number} clusterId
  152. * @param {PriorityQueue} queue
  153. * @returns {undefined}
  154. * @access protected
  155. */
  156. OPTICS.prototype._expandCluster = function(clusterId, queue) {
  157. var queueElements = queue.getElements();
  158. for (var p = 0, l = queueElements.length; p < l; p++) {
  159. var pointId = queueElements[p];
  160. if (this._processed[pointId] === undefined) {
  161. var neighbors = this._regionQuery(pointId);
  162. this._processed[pointId] = 1;
  163. this.clusters[clusterId].push(pointId);
  164. this._orderedList.push(pointId);
  165. if (this._distanceToCore(pointId) !== undefined) {
  166. this._updateQueue(pointId, neighbors, queue);
  167. this._expandCluster(clusterId, queue);
  168. }
  169. }
  170. }
  171. };
  172. /**
  173. * Calculating distance to cluster core
  174. *
  175. * @param {number} pointId
  176. * @returns {number}
  177. * @access protected
  178. */
  179. OPTICS.prototype._distanceToCore = function(pointId) {
  180. var l = this.epsilon;
  181. for (var coreDistCand = 0; coreDistCand < l; coreDistCand++) {
  182. var neighbors = this._regionQuery(pointId, coreDistCand);
  183. if (neighbors.length >= this.minPts) {
  184. return coreDistCand;
  185. }
  186. }
  187. return;
  188. };
  189. /**
  190. * Find all neighbors around given point
  191. *
  192. * @param {number} pointId
  193. * @param {number} epsilon
  194. * @returns {Array}
  195. * @access protected
  196. */
  197. OPTICS.prototype._regionQuery = function(pointId, epsilon) {
  198. epsilon = epsilon || this.epsilon;
  199. var neighbors = [];
  200. for (var id = 0, l = this.dataset.length; id < l; id++) {
  201. if (this.distance(this.dataset[pointId], this.dataset[id]) < epsilon) {
  202. neighbors.push(id);
  203. }
  204. }
  205. return neighbors;
  206. };
  207. /******************************************************************************/
  208. // helpers
  209. /**
  210. * Calculate euclidean distance in multidimensional space
  211. *
  212. * @param {Array} p
  213. * @param {Array} q
  214. * @returns {number}
  215. * @access protected
  216. */
  217. OPTICS.prototype._euclideanDistance = function(p, q) {
  218. var sum = 0;
  219. var i = Math.min(p.length, q.length);
  220. while (i--) {
  221. sum += (p[i] - q[i]) * (p[i] - q[i]);
  222. }
  223. return Math.sqrt(sum);
  224. };
  225. if (typeof module !== 'undefined' && module.exports) {
  226. module.exports = OPTICS;
  227. }