kde.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.KDE = exports.defined = void 0;
  7. const pdfast_1 = __importDefault(require("pdfast"));
  8. const d3_array_1 = require("d3-array");
  9. function defined(d) {
  10. return d !== undefined && d !== null && !Number.isNaN(d);
  11. }
  12. exports.defined = defined;
  13. /**
  14. * Kernel Density Estimation base on [pdfast](https://www.npmjs.com/package/pdfast),
  15. * generating probability density function (pdf) using triangular kernel,
  16. * optimized to run in O(N + K).
  17. */
  18. const KDE = (options) => {
  19. const { field, groupBy, as = ['y', 'size'], min, max, size = 10, width, } = options;
  20. const [yField, sizeField] = as;
  21. return (data) => {
  22. const gs = Array.from((0, d3_array_1.group)(data, (d) => groupBy.map((gb) => d[gb]).join('-')).values());
  23. return gs.map((g) => {
  24. const pdfResult = pdfast_1.default.create(g.map((i) => i[field]), {
  25. min,
  26. max,
  27. size,
  28. width,
  29. });
  30. const _y = pdfResult.map((result) => result.x);
  31. const _size = pdfResult.map((result) => result.y);
  32. return Object.assign(Object.assign({}, g[0]), { [yField]: _y, [sizeField]: _size });
  33. });
  34. };
  35. };
  36. exports.KDE = KDE;
  37. exports.KDE.props = {};
  38. //# sourceMappingURL=kde.js.map