@tetrascience-npm/ts-lib-viz 0.0.0-stage → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (278) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +2171 -2
  3. package/dist/src/builder/PlotPanel.d.ts +42 -0
  4. package/dist/src/builder/PlotPanel.js +433 -0
  5. package/dist/src/builder/Popover.d.ts +37 -0
  6. package/dist/src/builder/Popover.js +32 -0
  7. package/dist/src/builder/Stepper.d.ts +21 -0
  8. package/dist/src/builder/Stepper.js +41 -0
  9. package/dist/src/builder/index.d.ts +10 -0
  10. package/dist/src/builder/index.js +10 -0
  11. package/dist/src/core/adapters.d.ts +86 -0
  12. package/dist/src/core/adapters.js +219 -0
  13. package/dist/src/core/aggregate.d.ts +74 -0
  14. package/dist/src/core/aggregate.js +102 -0
  15. package/dist/src/core/annotations.d.ts +34 -0
  16. package/dist/src/core/annotations.js +71 -0
  17. package/dist/src/core/band-order.d.ts +86 -0
  18. package/dist/src/core/band-order.js +132 -0
  19. package/dist/src/core/binning.d.ts +123 -0
  20. package/dist/src/core/binning.js +193 -0
  21. package/dist/src/core/bounds.d.ts +40 -0
  22. package/dist/src/core/bounds.js +107 -0
  23. package/dist/src/core/brush.d.ts +73 -0
  24. package/dist/src/core/brush.js +102 -0
  25. package/dist/src/core/build.d.ts +322 -0
  26. package/dist/src/core/build.js +496 -0
  27. package/dist/src/core/camera.d.ts +152 -0
  28. package/dist/src/core/camera.js +303 -0
  29. package/dist/src/core/capabilities.d.ts +24 -0
  30. package/dist/src/core/capabilities.js +307 -0
  31. package/dist/src/core/chord.d.ts +49 -0
  32. package/dist/src/core/chord.js +57 -0
  33. package/dist/src/core/circular.d.ts +83 -0
  34. package/dist/src/core/circular.js +63 -0
  35. package/dist/src/core/cluster.d.ts +136 -0
  36. package/dist/src/core/cluster.js +607 -0
  37. package/dist/src/core/critique.d.ts +92 -0
  38. package/dist/src/core/critique.js +905 -0
  39. package/dist/src/core/density.d.ts +38 -0
  40. package/dist/src/core/density.js +82 -0
  41. package/dist/src/core/figure-set.d.ts +34 -0
  42. package/dist/src/core/figure-set.js +55 -0
  43. package/dist/src/core/finding-codes.d.ts +10 -0
  44. package/dist/src/core/finding-codes.js +1 -0
  45. package/dist/src/core/fit-types.d.ts +46 -0
  46. package/dist/src/core/fit-types.js +7 -0
  47. package/dist/src/core/fitter.d.ts +62 -0
  48. package/dist/src/core/fitter.js +102 -0
  49. package/dist/src/core/frame-geometry.d.ts +407 -0
  50. package/dist/src/core/frame-geometry.js +612 -0
  51. package/dist/src/core/frame.d.ts +329 -0
  52. package/dist/src/core/frame.js +1432 -0
  53. package/dist/src/core/geom-table.d.ts +503 -0
  54. package/dist/src/core/geom-table.js +484 -0
  55. package/dist/src/core/geom-types.d.ts +182 -0
  56. package/dist/src/core/geom-types.js +17 -0
  57. package/dist/src/core/grammar.d.ts +359 -0
  58. package/dist/src/core/grammar.js +397 -0
  59. package/dist/src/core/heat-reductions.d.ts +36 -0
  60. package/dist/src/core/heat-reductions.js +133 -0
  61. package/dist/src/core/hexbin.d.ts +84 -0
  62. package/dist/src/core/hexbin.js +168 -0
  63. package/dist/src/core/hierarchy.d.ts +59 -0
  64. package/dist/src/core/hierarchy.js +248 -0
  65. package/dist/src/core/hit.d.ts +75 -0
  66. package/dist/src/core/hit.js +37 -0
  67. package/dist/src/core/index.d.ts +33 -0
  68. package/dist/src/core/index.js +18 -0
  69. package/dist/src/core/internal.d.ts +58 -0
  70. package/dist/src/core/internal.js +58 -0
  71. package/dist/src/core/labels.d.ts +37 -0
  72. package/dist/src/core/labels.js +54 -0
  73. package/dist/src/core/layers.d.ts +594 -0
  74. package/dist/src/core/layers.js +2892 -0
  75. package/dist/src/core/load.d.ts +17 -0
  76. package/dist/src/core/load.js +356 -0
  77. package/dist/src/core/marginal.d.ts +65 -0
  78. package/dist/src/core/marginal.js +116 -0
  79. package/dist/src/core/mesh.d.ts +69 -0
  80. package/dist/src/core/mesh.js +237 -0
  81. package/dist/src/core/omission.d.ts +41 -0
  82. package/dist/src/core/omission.js +62 -0
  83. package/dist/src/core/palette-sets.d.ts +623 -0
  84. package/dist/src/core/palette-sets.js +1058 -0
  85. package/dist/src/core/palette.d.ts +871 -0
  86. package/dist/src/core/palette.js +511 -0
  87. package/dist/src/core/plot-data.d.ts +71 -0
  88. package/dist/src/core/plot-data.js +82 -0
  89. package/dist/src/core/predicate.d.ts +50 -0
  90. package/dist/src/core/predicate.js +20 -0
  91. package/dist/src/core/profile.d.ts +111 -0
  92. package/dist/src/core/profile.js +221 -0
  93. package/dist/src/core/readout.d.ts +108 -0
  94. package/dist/src/core/readout.js +215 -0
  95. package/dist/src/core/region.d.ts +56 -0
  96. package/dist/src/core/region.js +101 -0
  97. package/dist/src/core/rings.d.ts +96 -0
  98. package/dist/src/core/rings.js +171 -0
  99. package/dist/src/core/rules.d.ts +45 -0
  100. package/dist/src/core/rules.js +69 -0
  101. package/dist/src/core/sankey.d.ts +49 -0
  102. package/dist/src/core/sankey.js +96 -0
  103. package/dist/src/core/select.d.ts +52 -0
  104. package/dist/src/core/select.js +216 -0
  105. package/dist/src/core/side-track.d.ts +98 -0
  106. package/dist/src/core/side-track.js +197 -0
  107. package/dist/src/core/sizing.d.ts +54 -0
  108. package/dist/src/core/sizing.js +37 -0
  109. package/dist/src/core/spec-edit.d.ts +68 -0
  110. package/dist/src/core/spec-edit.js +105 -0
  111. package/dist/src/core/spec.d.ts +1111 -0
  112. package/dist/src/core/spec.js +39 -0
  113. package/dist/src/core/stats.d.ts +161 -0
  114. package/dist/src/core/stats.js +318 -0
  115. package/dist/src/core/tick-format.d.ts +21 -0
  116. package/dist/src/core/tick-format.js +87 -0
  117. package/dist/src/core/tokens.d.ts +47 -0
  118. package/dist/src/core/tokens.js +138 -0
  119. package/dist/src/core/transform.d.ts +21 -0
  120. package/dist/src/core/transform.js +19 -0
  121. package/dist/src/core/voxels.d.ts +53 -0
  122. package/dist/src/core/voxels.js +129 -0
  123. package/dist/src/core/zoom.d.ts +76 -0
  124. package/dist/src/core/zoom.js +103 -0
  125. package/dist/src/graph/GraphSurface.d.ts +127 -0
  126. package/dist/src/graph/GraphSurface.js +423 -0
  127. package/dist/src/graph/data.d.ts +103 -0
  128. package/dist/src/graph/data.js +140 -0
  129. package/dist/src/graph/index.d.ts +7 -0
  130. package/dist/src/graph/index.js +7 -0
  131. package/dist/src/mcp/data.d.ts +28 -0
  132. package/dist/src/mcp/data.js +131 -0
  133. package/dist/src/mcp/index.d.ts +7 -0
  134. package/dist/src/mcp/index.js +7 -0
  135. package/dist/src/mcp/paths.d.ts +12 -0
  136. package/dist/src/mcp/paths.js +30 -0
  137. package/dist/src/mcp/render.d.ts +67 -0
  138. package/dist/src/mcp/render.js +189 -0
  139. package/dist/src/mcp/server.d.ts +7 -0
  140. package/dist/src/mcp/server.js +25 -0
  141. package/dist/src/mcp/tools.d.ts +82 -0
  142. package/dist/src/mcp/tools.js +412 -0
  143. package/dist/src/react/BrushLayer.d.ts +20 -0
  144. package/dist/src/react/BrushLayer.js +98 -0
  145. package/dist/src/react/ColorLegend.d.ts +45 -0
  146. package/dist/src/react/ColorLegend.js +64 -0
  147. package/dist/src/react/Figure.d.ts +19 -0
  148. package/dist/src/react/Figure.js +6 -0
  149. package/dist/src/react/Frame.d.ts +87 -0
  150. package/dist/src/react/Frame.js +192 -0
  151. package/dist/src/react/MarkLegend.d.ts +32 -0
  152. package/dist/src/react/MarkLegend.js +24 -0
  153. package/dist/src/react/SeriesLegend.d.ts +38 -0
  154. package/dist/src/react/SeriesLegend.js +27 -0
  155. package/dist/src/react/SizeLegend.d.ts +24 -0
  156. package/dist/src/react/SizeLegend.js +25 -0
  157. package/dist/src/react/Toolbar.d.ts +109 -0
  158. package/dist/src/react/Toolbar.js +483 -0
  159. package/dist/src/react/Tooltip.d.ts +37 -0
  160. package/dist/src/react/Tooltip.js +59 -0
  161. package/dist/src/react/Viz.d.ts +112 -0
  162. package/dist/src/react/Viz.js +1311 -0
  163. package/dist/src/react/export.d.ts +32 -0
  164. package/dist/src/react/export.js +181 -0
  165. package/dist/src/react/geoms/Area.d.ts +38 -0
  166. package/dist/src/react/geoms/Area.js +48 -0
  167. package/dist/src/react/geoms/Bars.d.ts +130 -0
  168. package/dist/src/react/geoms/Bars.js +164 -0
  169. package/dist/src/react/geoms/Box.d.ts +50 -0
  170. package/dist/src/react/geoms/Box.js +32 -0
  171. package/dist/src/react/geoms/Chord.d.ts +60 -0
  172. package/dist/src/react/geoms/Chord.js +78 -0
  173. package/dist/src/react/geoms/Contour.d.ts +44 -0
  174. package/dist/src/react/geoms/Contour.js +78 -0
  175. package/dist/src/react/geoms/DensityCurve.d.ts +30 -0
  176. package/dist/src/react/geoms/DensityCurve.js +38 -0
  177. package/dist/src/react/geoms/FitCurve.d.ts +26 -0
  178. package/dist/src/react/geoms/FitCurve.js +52 -0
  179. package/dist/src/react/geoms/Freqpoly.d.ts +30 -0
  180. package/dist/src/react/geoms/Freqpoly.js +45 -0
  181. package/dist/src/react/geoms/Gauge.d.ts +42 -0
  182. package/dist/src/react/geoms/Gauge.js +90 -0
  183. package/dist/src/react/geoms/Heatmap.d.ts +79 -0
  184. package/dist/src/react/geoms/Heatmap.js +51 -0
  185. package/dist/src/react/geoms/Hexbin.d.ts +74 -0
  186. package/dist/src/react/geoms/Hexbin.js +89 -0
  187. package/dist/src/react/geoms/Icicle.d.ts +38 -0
  188. package/dist/src/react/geoms/Icicle.js +9 -0
  189. package/dist/src/react/geoms/Ideogram.d.ts +58 -0
  190. package/dist/src/react/geoms/Ideogram.js +77 -0
  191. package/dist/src/react/geoms/Interval.d.ts +30 -0
  192. package/dist/src/react/geoms/Interval.js +39 -0
  193. package/dist/src/react/geoms/Line.d.ts +39 -0
  194. package/dist/src/react/geoms/Line.js +52 -0
  195. package/dist/src/react/geoms/Parallel.d.ts +38 -0
  196. package/dist/src/react/geoms/Parallel.js +131 -0
  197. package/dist/src/react/geoms/Pie.d.ts +62 -0
  198. package/dist/src/react/geoms/Pie.js +59 -0
  199. package/dist/src/react/geoms/Points.d.ts +84 -0
  200. package/dist/src/react/geoms/Points.js +207 -0
  201. package/dist/src/react/geoms/Points3d.d.ts +49 -0
  202. package/dist/src/react/geoms/Points3d.js +58 -0
  203. package/dist/src/react/geoms/Radar.d.ts +39 -0
  204. package/dist/src/react/geoms/Radar.js +59 -0
  205. package/dist/src/react/geoms/Range.d.ts +31 -0
  206. package/dist/src/react/geoms/Range.js +20 -0
  207. package/dist/src/react/geoms/RingStack.d.ts +39 -0
  208. package/dist/src/react/geoms/RingStack.js +65 -0
  209. package/dist/src/react/geoms/Rule.d.ts +29 -0
  210. package/dist/src/react/geoms/Rule.js +51 -0
  211. package/dist/src/react/geoms/Sankey.d.ts +24 -0
  212. package/dist/src/react/geoms/Sankey.js +51 -0
  213. package/dist/src/react/geoms/Segment.d.ts +36 -0
  214. package/dist/src/react/geoms/Segment.js +39 -0
  215. package/dist/src/react/geoms/Sunburst.d.ts +46 -0
  216. package/dist/src/react/geoms/Sunburst.js +64 -0
  217. package/dist/src/react/geoms/Surface.d.ts +32 -0
  218. package/dist/src/react/geoms/Surface.js +49 -0
  219. package/dist/src/react/geoms/Text.d.ts +35 -0
  220. package/dist/src/react/geoms/Text.js +40 -0
  221. package/dist/src/react/geoms/Treemap.d.ts +39 -0
  222. package/dist/src/react/geoms/Treemap.js +16 -0
  223. package/dist/src/react/geoms/Violin.d.ts +56 -0
  224. package/dist/src/react/geoms/Violin.js +60 -0
  225. package/dist/src/react/geoms/Volume.d.ts +37 -0
  226. package/dist/src/react/geoms/Volume.js +40 -0
  227. package/dist/src/react/geoms/Waterfall.d.ts +43 -0
  228. package/dist/src/react/geoms/Waterfall.js +18 -0
  229. package/dist/src/react/geoms/cube-stage.d.ts +39 -0
  230. package/dist/src/react/geoms/cube-stage.js +85 -0
  231. package/dist/src/react/geoms/hierarchy-cells.d.ts +42 -0
  232. package/dist/src/react/geoms/hierarchy-cells.js +59 -0
  233. package/dist/src/react/geoms/sector-axis.d.ts +66 -0
  234. package/dist/src/react/geoms/sector-axis.js +99 -0
  235. package/dist/src/react/gl/GlMesh.d.ts +22 -0
  236. package/dist/src/react/gl/GlMesh.js +78 -0
  237. package/dist/src/react/gl/GlPoints.d.ts +45 -0
  238. package/dist/src/react/gl/GlPoints.js +117 -0
  239. package/dist/src/react/gl/GlVolume.d.ts +28 -0
  240. package/dist/src/react/gl/GlVolume.js +112 -0
  241. package/dist/src/react/gl/GlWalls.d.ts +24 -0
  242. package/dist/src/react/gl/GlWalls.js +55 -0
  243. package/dist/src/react/gl/context.d.ts +71 -0
  244. package/dist/src/react/gl/context.js +98 -0
  245. package/dist/src/react/gl/encode.d.ts +120 -0
  246. package/dist/src/react/gl/encode.js +215 -0
  247. package/dist/src/react/gl/index.d.ts +10 -0
  248. package/dist/src/react/gl/index.js +8 -0
  249. package/dist/src/react/gl/refresh.d.ts +7 -0
  250. package/dist/src/react/gl/refresh.js +19 -0
  251. package/dist/src/react/gl/renderer.d.ts +377 -0
  252. package/dist/src/react/gl/renderer.js +1081 -0
  253. package/dist/src/react/gl/shaders.d.ts +92 -0
  254. package/dist/src/react/gl/shaders.js +405 -0
  255. package/dist/src/react/gl/surface.d.ts +31 -0
  256. package/dist/src/react/gl/surface.js +73 -0
  257. package/dist/src/react/index.d.ts +12 -0
  258. package/dist/src/react/index.js +11 -0
  259. package/dist/src/react/orbit.d.ts +38 -0
  260. package/dist/src/react/orbit.js +81 -0
  261. package/dist/src/react/sides/DendrogramPanel.d.ts +32 -0
  262. package/dist/src/react/sides/DendrogramPanel.js +39 -0
  263. package/dist/src/react/sides/DensityPanel.d.ts +31 -0
  264. package/dist/src/react/sides/DensityPanel.js +37 -0
  265. package/dist/src/react/sides/HistogramPanel.d.ts +30 -0
  266. package/dist/src/react/sides/HistogramPanel.js +18 -0
  267. package/dist/src/react/sides/RugPanel.d.ts +28 -0
  268. package/dist/src/react/sides/RugPanel.js +21 -0
  269. package/dist/src/react/sides/TrackPanel.d.ts +37 -0
  270. package/dist/src/react/sides/TrackPanel.js +27 -0
  271. package/dist/src/react/theme.d.ts +48 -0
  272. package/dist/src/react/theme.js +148 -0
  273. package/dist/src/react/useFits.d.ts +26 -0
  274. package/dist/src/react/useFits.js +62 -0
  275. package/dist/src/react/useZoom.d.ts +50 -0
  276. package/dist/src/react/useZoom.js +141 -0
  277. package/package.json +164 -5
  278. package/theme/default.css +62 -0
@@ -0,0 +1,607 @@
1
+ /**
2
+ * Hierarchical clustering over a grid.
3
+ *
4
+ * Checked against `hclust` and `cutree` on fixed matrices, one check per linkage. The cost floor
5
+ * is quadratic and `kmeans` covers the case above it.
6
+ */
7
+ /**
8
+ * Where a pair falls in the condensed triangle.
9
+ *
10
+ * The upper half without its diagonal, walked by row. R's `dist` uses this order.
11
+ */
12
+ export const condensedIndex = (n, a, b) => {
13
+ const i = a < b ? a : b;
14
+ const j = a < b ? b : a;
15
+ return i * n - (i * (i + 1)) / 2 + j - i - 1;
16
+ };
17
+ /**
18
+ * The cells read into a matrix. An absent cell takes its row mean.
19
+ *
20
+ * The label orders are arguments. Absent, first-seen stands in. The drawn grid keeps its gap.
21
+ */
22
+ export function heatMatrix(cells, rowOrder, columnOrder) {
23
+ const rows = rowOrder ? [...rowOrder] : firstSeen(cells, (cell) => cell.y);
24
+ const columns = columnOrder ? [...columnOrder] : firstSeen(cells, (cell) => cell.x);
25
+ const rowAt = new Map(rows.map((label, at) => [label, at]));
26
+ const columnAt = new Map(columns.map((label, at) => [label, at]));
27
+ const width = columns.length;
28
+ const values = new Float64Array(rows.length * width);
29
+ const seen = new Uint8Array(values.length);
30
+ for (const cell of cells) {
31
+ const row = rowAt.get(cell.y);
32
+ const column = columnAt.get(cell.x);
33
+ if (row == null || column == null)
34
+ continue;
35
+ const at = row * width + column;
36
+ values[at] = cell.value;
37
+ seen[at] = 1;
38
+ }
39
+ for (let row = 0; row < rows.length; row += 1) {
40
+ const base = row * width;
41
+ let sum = 0;
42
+ let count = 0;
43
+ for (let column = 0; column < width; column += 1) {
44
+ if (seen[base + column] === 0)
45
+ continue;
46
+ sum += values[base + column];
47
+ count += 1;
48
+ }
49
+ if (count === width)
50
+ continue;
51
+ const mean = count > 0 ? sum / count : 0;
52
+ for (let column = 0; column < width; column += 1) {
53
+ if (seen[base + column] === 0)
54
+ values[base + column] = mean;
55
+ }
56
+ }
57
+ return { rows, columns, values };
58
+ }
59
+ const firstSeen = (cells, of) => {
60
+ const out = [];
61
+ const seen = new Set();
62
+ for (const cell of cells) {
63
+ const label = of(cell);
64
+ if (seen.has(label))
65
+ continue;
66
+ seen.add(label);
67
+ out.push(label);
68
+ }
69
+ return out;
70
+ };
71
+ /** The matrix on its side, for clustering the columns through the same arithmetic as the rows. */
72
+ export function transposed(matrix) {
73
+ const height = matrix.rows.length;
74
+ const width = matrix.columns.length;
75
+ const values = new Float64Array(height * width);
76
+ for (let row = 0; row < height; row += 1) {
77
+ for (let column = 0; column < width; column += 1) {
78
+ values[column * height + row] = matrix.values[row * width + column];
79
+ }
80
+ }
81
+ return { rows: matrix.columns, columns: matrix.rows, values };
82
+ }
83
+ /**
84
+ * The matrix z-scored along one direction. pheatmap's `scale`.
85
+ *
86
+ * Rows come out on one scale in standard deviations. The divisor is n-1, matching R's `scale`.
87
+ */
88
+ export function zScored(matrix, direction) {
89
+ if (direction === "none")
90
+ return { ...matrix, values: Float64Array.from(matrix.values) };
91
+ const height = matrix.rows.length;
92
+ const width = matrix.columns.length;
93
+ const values = Float64Array.from(matrix.values);
94
+ const along = direction === "row" ? width : height;
95
+ const count = direction === "row" ? height : width;
96
+ if (along < 2)
97
+ return { ...matrix, values: new Float64Array(values.length) };
98
+ for (let line = 0; line < count; line += 1) {
99
+ const at = (step) => (direction === "row" ? line * width + step : step * width + line);
100
+ let sum = 0;
101
+ for (let step = 0; step < along; step += 1)
102
+ sum += values[at(step)];
103
+ const mean = sum / along;
104
+ let squares = 0;
105
+ for (let step = 0; step < along; step += 1) {
106
+ const gap = values[at(step)] - mean;
107
+ squares += gap * gap;
108
+ }
109
+ const spread = Math.sqrt(squares / (along - 1));
110
+ for (let step = 0; step < along; step += 1) {
111
+ values[at(step)] = spread > 0 ? (values[at(step)] - mean) / spread : 0;
112
+ }
113
+ }
114
+ return { ...matrix, values };
115
+ }
116
+ /**
117
+ * The condensed distance triangle over the rows.
118
+ *
119
+ * Float32. Both metrics come out of one pass over a norms vector and the dot product. A flat row
120
+ * comes back at 1.
121
+ */
122
+ export function condensedDistance(matrix, metric) {
123
+ const n = matrix.rows.length;
124
+ const width = matrix.columns.length;
125
+ const out = new Float32Array((n * (n - 1)) / 2);
126
+ if (n < 2)
127
+ return out;
128
+ /* Correlation reads off centered rows of unit length, where the dot product is Pearson's r. */
129
+ const values = metric === "correlation" ? unitRows(matrix) : matrix.values;
130
+ const flat = metric === "correlation" ? flatRows(matrix) : null;
131
+ const norms = new Float64Array(n);
132
+ if (metric === "euclidean") {
133
+ for (let row = 0; row < n; row += 1) {
134
+ let squares = 0;
135
+ for (let column = 0; column < width; column += 1) {
136
+ const value = values[row * width + column];
137
+ squares += value * value;
138
+ }
139
+ norms[row] = squares;
140
+ }
141
+ }
142
+ for (let i = 0; i < n; i += 1) {
143
+ for (let j = i + 1; j < n; j += 1) {
144
+ let dot = 0;
145
+ for (let column = 0; column < width; column += 1) {
146
+ dot += values[i * width + column] * values[j * width + column];
147
+ }
148
+ const at = condensedIndex(n, i, j);
149
+ if (metric === "euclidean") {
150
+ /* Clamped at zero. Two identical rows can differ by a rounding error here. */
151
+ out[at] = Math.sqrt(Math.max(0, norms[i] + norms[j] - 2 * dot));
152
+ continue;
153
+ }
154
+ out[at] = flat[i] === 1 || flat[j] === 1 ? 1 : 1 - dot;
155
+ }
156
+ }
157
+ return out;
158
+ }
159
+ /** Each row centered and scaled to unit length. A dot product then answers Pearson's r. */
160
+ function unitRows(matrix) {
161
+ const n = matrix.rows.length;
162
+ const width = matrix.columns.length;
163
+ const values = Float64Array.from(matrix.values);
164
+ for (let row = 0; row < n; row += 1) {
165
+ const base = row * width;
166
+ let sum = 0;
167
+ for (let column = 0; column < width; column += 1)
168
+ sum += values[base + column];
169
+ const mean = sum / width;
170
+ let squares = 0;
171
+ for (let column = 0; column < width; column += 1) {
172
+ values[base + column] = values[base + column] - mean;
173
+ squares += values[base + column] * values[base + column];
174
+ }
175
+ const length = Math.sqrt(squares);
176
+ if (length === 0)
177
+ continue;
178
+ for (let column = 0; column < width; column += 1)
179
+ values[base + column] = values[base + column] / length;
180
+ }
181
+ return values;
182
+ }
183
+ /** Which rows have no spread. Their correlation with anything is undefined. */
184
+ function flatRows(matrix) {
185
+ const n = matrix.rows.length;
186
+ const width = matrix.columns.length;
187
+ const flat = new Uint8Array(n);
188
+ for (let row = 0; row < n; row += 1) {
189
+ const base = row * width;
190
+ const first = matrix.values[base];
191
+ let same = true;
192
+ for (let column = 1; column < width; column += 1) {
193
+ if (matrix.values[base + column] !== first) {
194
+ same = false;
195
+ break;
196
+ }
197
+ }
198
+ flat[row] = same ? 1 : 0;
199
+ }
200
+ return flat;
201
+ }
202
+ /** The Lance-Williams update. One merge loop serves five linkages. */
203
+ const merged = (method, toX, toY, between, nx, ny, ni) => {
204
+ switch (method) {
205
+ case "single":
206
+ return Math.min(toX, toY);
207
+ case "complete":
208
+ return Math.max(toX, toY);
209
+ case "average":
210
+ return (nx * toX + ny * toY) / (nx + ny);
211
+ case "weighted":
212
+ return (toX + toY) / 2;
213
+ case "ward":
214
+ return Math.sqrt(((ni + nx) * toX * toX + (ni + ny) * toY * toY - ni * between * between) / (ni + nx + ny));
215
+ }
216
+ };
217
+ /**
218
+ * The merge sequence, by nearest-neighbor chain.
219
+ *
220
+ * The chain walks nearest neighbors to a mutual pair, merges it, and keeps the rest. The
221
+ * surviving cluster keeps the higher index.
222
+ */
223
+ export function cluster(condensed, n, method) {
224
+ if (n < 2)
225
+ return { merges: [], heights: [] };
226
+ const distances = Float32Array.from(condensed);
227
+ const size = new Int32Array(n).fill(1);
228
+ const chain = new Int32Array(n);
229
+ let held = 0;
230
+ const found = [];
231
+ for (let round = 0; round < n - 1; round += 1) {
232
+ if (held === 0) {
233
+ held = 1;
234
+ for (let i = 0; i < n; i += 1) {
235
+ if (size[i] > 0) {
236
+ chain[0] = i;
237
+ break;
238
+ }
239
+ }
240
+ }
241
+ let x = 0;
242
+ let y = 0;
243
+ let closest = Infinity;
244
+ for (;;) {
245
+ x = chain[held - 1];
246
+ closest = Infinity;
247
+ /* The previous link wins a tie. The walk stops cycling. */
248
+ if (held > 1) {
249
+ y = chain[held - 2];
250
+ closest = distances[condensedIndex(n, x, y)];
251
+ }
252
+ for (let i = 0; i < n; i += 1) {
253
+ if (size[i] === 0 || i === x)
254
+ continue;
255
+ const distance = distances[condensedIndex(n, x, i)];
256
+ if (distance < closest) {
257
+ closest = distance;
258
+ y = i;
259
+ }
260
+ }
261
+ if (held > 1 && y === chain[held - 2])
262
+ break;
263
+ chain[held] = y;
264
+ held += 1;
265
+ }
266
+ held -= 2;
267
+ const low = Math.min(x, y);
268
+ const high = Math.max(x, y);
269
+ const nx = size[low];
270
+ const ny = size[high];
271
+ found.push({ low, high, height: closest });
272
+ /* The lower index retires and the higher carries the merged cluster. */
273
+ size[low] = 0;
274
+ size[high] = nx + ny;
275
+ for (let i = 0; i < n; i += 1) {
276
+ if (size[i] === 0 || i === high)
277
+ continue;
278
+ const toLow = distances[condensedIndex(n, i, low)];
279
+ const toHigh = distances[condensedIndex(n, i, high)];
280
+ distances[condensedIndex(n, i, high)] = merged(method, toLow, toHigh, closest, nx, ny, size[i]);
281
+ }
282
+ }
283
+ return relabel(found, n);
284
+ }
285
+ /**
286
+ * The found merges sorted by height and renumbered onto node ids.
287
+ *
288
+ * Sorting by height puts the tree in reading order. Union-find rewrites each pair.
289
+ */
290
+ function relabel(found, n) {
291
+ const sorted = [...found].sort((a, b) => a.height - b.height);
292
+ const parent = new Int32Array(2 * n - 1);
293
+ for (let at = 0; at < parent.length; at += 1)
294
+ parent[at] = at;
295
+ const find = (start) => {
296
+ let at = start;
297
+ while (parent[at] !== at)
298
+ at = parent[at];
299
+ let walk = start;
300
+ while (parent[walk] !== at) {
301
+ const next = parent[walk];
302
+ parent[walk] = at;
303
+ walk = next;
304
+ }
305
+ return at;
306
+ };
307
+ const merges = [];
308
+ const heights = [];
309
+ for (let at = 0; at < sorted.length; at += 1) {
310
+ const entry = sorted[at];
311
+ const left = find(entry.low);
312
+ const right = find(entry.high);
313
+ const node = n + at;
314
+ merges.push(left < right ? [left, right] : [right, left]);
315
+ heights.push(entry.height);
316
+ parent[left] = node;
317
+ parent[right] = node;
318
+ }
319
+ return { merges, heights };
320
+ }
321
+ /** The leaves left to right, as row indices. A pre-order walk from the root, left subtree first. */
322
+ function walk(tree, n) {
323
+ if (n === 0)
324
+ return [];
325
+ const out = [];
326
+ const stack = [n + tree.merges.length - 1];
327
+ while (stack.length > 0) {
328
+ const node = stack.pop();
329
+ if (node < n) {
330
+ out.push(node);
331
+ continue;
332
+ }
333
+ const [left, right] = tree.merges[node - n];
334
+ stack.push(right);
335
+ stack.push(left);
336
+ }
337
+ return out;
338
+ }
339
+ /**
340
+ * The rows in the order the tree puts them.
341
+ *
342
+ * A pre-order walk taking the left subtree first. R's own leaf walk does the same.
343
+ */
344
+ export function leafOrder(tree, labels) {
345
+ return walk(tree, labels.length).map((row) => labels[row]);
346
+ }
347
+ /**
348
+ * Each merge's depth, in the order the merges are held.
349
+ *
350
+ * Taken over the whole tree at once. A leaf is at zero and the last merge is at one.
351
+ */
352
+ function depthsOf(heights, scale) {
353
+ if (scale === "rank") {
354
+ /* Distinct steps. Two merges at one distance draw at one depth. */
355
+ const steps = [...new Set(heights)].sort((a, b) => a - b);
356
+ const place = new Map(steps.map((height, at) => [height, (at + 1) / steps.length]));
357
+ return heights.map((height) => place.get(height));
358
+ }
359
+ const tallest = heights.reduce((high, height) => Math.max(high, height), 0);
360
+ if (tallest <= 0)
361
+ return heights.map(() => 0);
362
+ return heights.map((height) => (scale === "sqrt" ? Math.sqrt(height / tallest) : height / tallest));
363
+ }
364
+ /**
365
+ * The tree as brackets, against band positions.
366
+ *
367
+ * `along` holds one position per leaf, indexed by leaf id. A merge draws at the midpoint of what
368
+ * it joins.
369
+ */
370
+ export function dendrogramBrackets(tree, along, scale = "height") {
371
+ const n = along.length;
372
+ if (n < 2 || tree.merges.length !== n - 1)
373
+ return [];
374
+ const depths = depthsOf(tree.heights, scale);
375
+ const center = [];
376
+ const at = (node) => (node < n ? along[node] : center[node - n]);
377
+ const base = (node) => (node < n ? 0 : depths[node - n]);
378
+ const out = [];
379
+ for (let merge = 0; merge < tree.merges.length; merge += 1) {
380
+ const [left, right] = tree.merges[merge];
381
+ const from = [at(left), at(right)];
382
+ center.push((from[0] + from[1]) / 2);
383
+ out.push({ from, base: [base(left), base(right)], depth: depths[merge], at: center[merge] });
384
+ }
385
+ return out;
386
+ }
387
+ /**
388
+ * Which cluster each row falls in, cutting the tree to k. pheatmap's `cutree_rows`.
389
+ *
390
+ * Numbered by first appearance down the rows, matching R's `cutree`.
391
+ */
392
+ export function cutClusters(tree, n, k) {
393
+ const wanted = Math.max(1, Math.min(k, n));
394
+ const applied = n - wanted;
395
+ const parent = new Int32Array(Math.max(1, 2 * n - 1));
396
+ for (let at = 0; at < parent.length; at += 1)
397
+ parent[at] = at;
398
+ const find = (start) => {
399
+ let at = start;
400
+ while (parent[at] !== at)
401
+ at = parent[at];
402
+ return at;
403
+ };
404
+ for (let m = 0; m < applied; m += 1) {
405
+ const [left, right] = tree.merges[m];
406
+ const node = n + m;
407
+ parent[find(left)] = node;
408
+ parent[find(right)] = node;
409
+ }
410
+ const label = new Map();
411
+ const out = [];
412
+ for (let row = 0; row < n; row += 1) {
413
+ const root = find(row);
414
+ if (!label.has(root))
415
+ label.set(root, label.size);
416
+ out.push(label.get(root));
417
+ }
418
+ return out;
419
+ }
420
+ /**
421
+ * Where the cut falls in the leaf order, as the positions a break comes before.
422
+ *
423
+ * A cut gives blocks contiguous in the leaf order. k clusters give k-1 breaks.
424
+ */
425
+ export function cutBoundaries(tree, n, k) {
426
+ const clusters = cutClusters(tree, n, k);
427
+ const order = walk(tree, n);
428
+ const breaks = [];
429
+ for (let at = 1; at < order.length; at += 1) {
430
+ if (clusters[order[at]] !== clusters[order[at - 1]])
431
+ breaks.push(at);
432
+ }
433
+ return breaks;
434
+ }
435
+ /**
436
+ * The cells z-scored along one direction, under pheatmap's `scale`.
437
+ *
438
+ * Only the cells that were there come back. A gap stays a gap.
439
+ */
440
+ export function scaledCells(cells, direction) {
441
+ if (direction === "none" || cells.length === 0)
442
+ return cells;
443
+ const matrix = heatMatrix(cells);
444
+ const z = zScored(matrix, direction);
445
+ const rowAt = new Map(matrix.rows.map((label, at) => [label, at]));
446
+ const columnAt = new Map(matrix.columns.map((label, at) => [label, at]));
447
+ const width = matrix.columns.length;
448
+ return cells.map((cell) => ({
449
+ ...cell,
450
+ value: z.values[rowAt.get(cell.y) * width + columnAt.get(cell.x)],
451
+ }));
452
+ }
453
+ /**
454
+ * Rows aggregated into k centroids by k-means. pheatmap's `kmeans_k`.
455
+ *
456
+ * The escape past the cluster cap, linear in the row count. Seeding is farthest-first.
457
+ */
458
+ export function kmeansRows(matrix, k) {
459
+ const n = matrix.rows.length;
460
+ const width = matrix.columns.length;
461
+ const wanted = Math.max(1, Math.min(k, n));
462
+ const centers = new Float64Array(wanted * width);
463
+ const seeds = farthestFirst(matrix, wanted);
464
+ for (let at = 0; at < wanted; at += 1) {
465
+ for (let column = 0; column < width; column += 1) {
466
+ centers[at * width + column] = matrix.values[seeds[at] * width + column];
467
+ }
468
+ }
469
+ const assignment = new Array(n).fill(0);
470
+ const ROUNDS = 50;
471
+ for (let round = 0; round < ROUNDS; round += 1) {
472
+ let moved = false;
473
+ for (let row = 0; row < n; row += 1) {
474
+ let best = 0;
475
+ let closest = Infinity;
476
+ for (let at = 0; at < wanted; at += 1) {
477
+ let squares = 0;
478
+ for (let column = 0; column < width; column += 1) {
479
+ const gap = matrix.values[row * width + column] - centers[at * width + column];
480
+ squares += gap * gap;
481
+ }
482
+ if (squares < closest) {
483
+ closest = squares;
484
+ best = at;
485
+ }
486
+ }
487
+ if (assignment[row] !== best) {
488
+ assignment[row] = best;
489
+ moved = true;
490
+ }
491
+ }
492
+ /*
493
+ An emptied centroid takes the row furthest from its own. k clusters stay k.
494
+ */
495
+ fillEmpty(matrix, assignment, wanted, centers);
496
+ recenter(matrix, assignment, wanted, centers);
497
+ if (!moved)
498
+ break;
499
+ }
500
+ const held = new Array(wanted).fill(0);
501
+ for (const at of assignment)
502
+ held[at] += 1;
503
+ return {
504
+ assignment,
505
+ centroids: {
506
+ rows: held.map((count, at) => `Cluster ${at + 1} (${count} ${count === 1 ? "row" : "rows"})`),
507
+ columns: [...matrix.columns],
508
+ values: centers,
509
+ },
510
+ };
511
+ }
512
+ /** Seeds spread across the grid: the row furthest from the mean, then furthest from what is chosen. */
513
+ function farthestFirst(matrix, k) {
514
+ const n = matrix.rows.length;
515
+ const width = matrix.columns.length;
516
+ const mean = new Float64Array(width);
517
+ for (let row = 0; row < n; row += 1) {
518
+ for (let column = 0; column < width; column += 1)
519
+ mean[column] += matrix.values[row * width + column] / n;
520
+ }
521
+ const gapTo = (row, point, offset) => {
522
+ let squares = 0;
523
+ for (let column = 0; column < width; column += 1) {
524
+ const gap = matrix.values[row * width + column] - point[offset + column];
525
+ squares += gap * gap;
526
+ }
527
+ return squares;
528
+ };
529
+ const nearest = new Float64Array(n).fill(Infinity);
530
+ const seeds = [];
531
+ let first = 0;
532
+ let furthest = -1;
533
+ for (let row = 0; row < n; row += 1) {
534
+ const distance = gapTo(row, mean, 0);
535
+ if (distance > furthest) {
536
+ furthest = distance;
537
+ first = row;
538
+ }
539
+ }
540
+ seeds.push(first);
541
+ while (seeds.length < k) {
542
+ const last = seeds[seeds.length - 1];
543
+ let pick = 0;
544
+ let best = -1;
545
+ for (let row = 0; row < n; row += 1) {
546
+ nearest[row] = Math.min(nearest[row], gapTo(row, matrix.values, last * width));
547
+ if (nearest[row] > best) {
548
+ best = nearest[row];
549
+ pick = row;
550
+ }
551
+ }
552
+ seeds.push(pick);
553
+ nearest[pick] = -1;
554
+ }
555
+ return seeds;
556
+ }
557
+ /** Each centroid moved onto the mean of what it holds. */
558
+ function recenter(matrix, assignment, k, centers) {
559
+ const width = matrix.columns.length;
560
+ const held = new Int32Array(k);
561
+ centers.fill(0);
562
+ for (let row = 0; row < assignment.length; row += 1) {
563
+ const at = assignment[row];
564
+ held[at] += 1;
565
+ for (let column = 0; column < width; column += 1) {
566
+ centers[at * width + column] += matrix.values[row * width + column];
567
+ }
568
+ }
569
+ for (let at = 0; at < k; at += 1) {
570
+ if (held[at] === 0)
571
+ continue;
572
+ for (let column = 0; column < width; column += 1)
573
+ centers[at * width + column] /= held[at];
574
+ }
575
+ }
576
+ /** An empty centroid claims the row furthest from the one holding it. */
577
+ function fillEmpty(matrix, assignment, k, centers) {
578
+ const width = matrix.columns.length;
579
+ const held = new Int32Array(k);
580
+ for (const at of assignment)
581
+ held[at] += 1;
582
+ for (let at = 0; at < k; at += 1) {
583
+ if (held[at] > 0)
584
+ continue;
585
+ let pick = -1;
586
+ let furthest = -1;
587
+ for (let row = 0; row < assignment.length; row += 1) {
588
+ const owner = assignment[row];
589
+ if (held[owner] < 2)
590
+ continue;
591
+ let squares = 0;
592
+ for (let column = 0; column < width; column += 1) {
593
+ const gap = matrix.values[row * width + column] - centers[owner * width + column];
594
+ squares += gap * gap;
595
+ }
596
+ if (squares > furthest) {
597
+ furthest = squares;
598
+ pick = row;
599
+ }
600
+ }
601
+ if (pick < 0)
602
+ continue;
603
+ held[assignment[pick]] -= 1;
604
+ assignment[pick] = at;
605
+ held[at] += 1;
606
+ }
607
+ }
@@ -0,0 +1,92 @@
1
+ /**
2
+ * What is wrong with a chart, sorted by severity.
3
+ *
4
+ * `wrong` means the figure misstates the data and blocks a save. `bad` means a reader cannot
5
+ * read it. `ugly` is aesthetics.
6
+ */
7
+ import type { FindingCode, UnwaivableCode, WaivableCode } from "./finding-codes.js";
8
+ import type { FigureSet } from "./figure-set.js";
9
+ import { type ReadyFrame, type Substitution } from "./frame.js";
10
+ import type { ChartSpec } from "./spec.js";
11
+ export type Severity = "wrong" | "bad" | "ugly";
12
+ export type { FindingCode, UnwaivableCode, WaivableCode };
13
+ export interface Finding {
14
+ severity: Severity;
15
+ code: FindingCode;
16
+ /** Dotted path into the spec, in the same shape `validate` returns. */
17
+ path: string;
18
+ /** What is wrong, in a sentence an author can act on. */
19
+ message: string;
20
+ }
21
+ /**
22
+ * Blended fragments a frame can shade inside its budget, in CSS pixels of geom area.
23
+ *
24
+ * About 1.4 ms per million blended fragments. Half a 60fps frame is 8 ms.
25
+ */
26
+ export declare const FILL_BUDGET_PX = 1400000;
27
+ /**
28
+ * How many color categories can a reader distinguish?
29
+ * The Okabe-Ito palette and Grammar of Graphics agree the maximum should be 8.
30
+ */
31
+ export declare const COLORS_LEGIBLE = 8;
32
+ /** What a consumer states beside the spec. */
33
+ export interface CritiqueOptions {
34
+ /** Whether the figure leaves the page, which adds the checks on its copy. */
35
+ exported?: boolean;
36
+ }
37
+ /** What a drawing surface reads beside the spec. Each field is absent by default, and the checks reading it skip. */
38
+ export interface CritiqueContext extends CritiqueOptions {
39
+ /** The built frame. Absent runs the spec-only checks. */
40
+ frame?: ReadyFrame;
41
+ /** What a geom drew in place of the one the spec asked for. */
42
+ substituted?: Substitution;
43
+ /** Room the figure was given, where the caller measured it. Absent skips the fit check. */
44
+ container?: number;
45
+ /** The plot area the geoms were given, in pixels. Absent leaves a layout-picked band unchecked. */
46
+ area?: {
47
+ width: number;
48
+ height: number;
49
+ };
50
+ }
51
+ /**
52
+ * What is wrong with a chart, worst first.
53
+ *
54
+ * Hand the frame where there is one. Without it the spec-only checks run.
55
+ */
56
+ export declare function critique(spec: ChartSpec, { frame, substituted, exported, container, area }?: CritiqueContext): Finding[];
57
+ /**
58
+ * The critic as a consumer calls it: the spec's own checks, and the copy checks for an export.
59
+ *
60
+ * `Viz` runs the checks that read the drawn frame and hands their findings to `onCritique`.
61
+ */
62
+ export declare const critiqueSpec: (spec: ChartSpec, options?: CritiqueOptions) => Finding[];
63
+ /**
64
+ * Findings that stop a save.
65
+ *
66
+ * The `wrong` ones absent from `ChartSpec.overrides`, plus the missing claim.
67
+ *
68
+ * An empty result passes the gate. An override waives the other codes.
69
+ */
70
+ export declare const blocking: (spec: ChartSpec, findings: Finding[]) => Finding[];
71
+ /**
72
+ * Overrides the reader is owed, as footer lines.
73
+ *
74
+ * An override is the author saying they know. The admission goes to the footer.
75
+ */
76
+ export declare const overrideNotes: (spec: ChartSpec, findings: Finding[]) => string[];
77
+ /**
78
+ * What is wrong with a set of figures taken together.
79
+ *
80
+ * Both findings are `ugly`. `plots` holds the members by id and a missing member is skipped.
81
+ */
82
+ export declare function critiqueSet(set: FigureSet, plots: Record<string, ChartSpec>): Finding[];
83
+ /**
84
+ * What a graph has to admit, and what it has to claim.
85
+ *
86
+ * A published figure states its claim. A view holding less than its source reports the count.
87
+ */
88
+ export declare function critiqueGraph(graph: {
89
+ headline?: string;
90
+ links: number;
91
+ linksDropped: number;
92
+ }): Finding[];