@datagrok/curves 1.13.0 → 1.13.1

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.
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@datagrok/curves",
3
3
  "friendlyName": "Curves",
4
- "version": "1.13.0",
4
+ "version": "1.13.1",
5
5
  "description": "Curves provides support for fitted curves (such as dose-response curves), including in-grid rendering, storing charts in cells, interactivity, and automatic fitting.",
6
6
  "author": {
7
7
  "name": "Dmytro Kovalyov",
8
8
  "email": "dkovalyov@datagrok.ai"
9
9
  },
10
10
  "dependencies": {
11
- "@datagrok-libraries/statistics": "^1.12.10",
11
+ "@datagrok-libraries/statistics": "^1.12.13",
12
12
  "@datagrok-libraries/test": "^1.2.0",
13
13
  "@datagrok-libraries/utils": "^4.6.5",
14
14
  "@types/wu": "^2.1.42",
@@ -563,7 +563,7 @@ export async function convertDataToCurves(df: DG.DataFrame,
563
563
  curve.x.push(concentration);
564
564
  curve.y.push(readout);
565
565
  curve.outliers.push(outlier);
566
- if (!outlier && readout != null || readout != DG.FLOAT_NULL && readout != DG.INT_NULL)
566
+ if (!outlier && readout != null && readout != DG.FLOAT_NULL && readout != DG.INT_NULL)
567
567
  curve.info['Max Percent Inhibition'] = Math.max(curve.info['Max Percent Inhibition'] ?? -Infinity, readout);
568
568
  }
569
569
 
@@ -309,12 +309,19 @@ export function substituteZeroes(data: IFitChartData): void {
309
309
  minNonZeroX = series.points[j].x;
310
310
  if (series.points[j].x > maxNonZeroX && series.points[j].x !== 0)
311
311
  maxNonZeroX = series.points[j].x;
312
- if (!uniqueArr.includes(series.points[j].x)) {
312
+ if (series.points[j].x !== 0 && !uniqueArr.includes(series.points[j].x)) {
313
313
  uniqueArr[uniqueArr.length] = series.points[j].x;
314
314
  countOfDistNonZeroX++;
315
315
  }
316
316
  }
317
- const zeroSubstitute = Math.pow(10, Math.log10(minNonZeroX) - (Math.log10(maxNonZeroX) - Math.log10(minNonZeroX) / (countOfDistNonZeroX - 1)));
317
+ if (countOfDistNonZeroX === 0)
318
+ continue;
319
+
320
+ // one dose step below the smallest tested dose, measured on the log scale the axis uses
321
+ const logMin = Math.log10(minNonZeroX);
322
+ const logStep = countOfDistNonZeroX > 1 ?
323
+ (Math.log10(maxNonZeroX) - logMin) / (countOfDistNonZeroX - 1) : 1;
324
+ const zeroSubstitute = Math.pow(10, logMin - logStep);
318
325
  for (let j = 0; j < series.points.length; j++) {
319
326
  if (series.points[j].x === 0)
320
327
  series.points[j].x = zeroSubstitute;
@@ -124,10 +124,10 @@ export class FitGridCellHandler extends DG.ObjectHandler {
124
124
  chartData.seriesOptions[colorFieldName] = DG.Color.toHtml(colorFieldName === 'outlierColor' ?
125
125
  DG.Color.red : DG.Color.getCategoricalColor(0));
126
126
  }
127
- } else {
128
- if (!isColorValid(chartData.series ? chartData.series[0][colorFieldName] : '')) {
129
- chartData.series![0][colorFieldName] = DG.Color.toHtml(colorFieldName === 'outlierColor' ?
130
- DG.Color.red : DG.Color.getCategoricalColor(0));
127
+ } else if (chartData.series?.length) {
128
+ if (!isColorValid(chartData.series[0][colorFieldName])) {
129
+ chartData.series[0][colorFieldName] = DG.Color.toHtml(colorFieldName === 'outlierColor' ?
130
+ DG.Color.red : DG.Color.getCategoricalColor(0));
131
131
  }
132
132
  }
133
133
  }
@@ -223,14 +223,14 @@ export class MultiCurveViewer extends DG.JsViewer {
223
223
  if (fitCol !== null)
224
224
  this.curvesColumnNames = [fitCol.name];
225
225
 
226
- merge(...[this.dataFrame.onCurrentCellChanged, ...(grid ? [grid.onCellMouseEnter] : []), this.dataFrame.onSelectionChanged])
226
+ this.subs.push(merge(...[this.dataFrame.onCurrentCellChanged, ...(grid ? [grid.onCellMouseEnter] : []), this.dataFrame.onSelectionChanged])
227
227
  .pipe(debounce((_) => interval(50)))
228
228
  .subscribe((_) => {
229
229
  if (this.dataFrame) {
230
230
  this.createChartData();
231
231
  this.render();
232
232
  }
233
- });
233
+ }));
234
234
  this.createChartData();
235
235
  if (!this.isInTrellis())
236
236
  this.render();
@@ -7,6 +7,7 @@ import './tests/converter-tests';
7
7
  import './tests/curves-cell-renderer-tests';
8
8
  import './tests/transform-tests';
9
9
  import './tests/fit-tests';
10
+ import './tests/statistics-tests';
10
11
  import './tests/pzfx-tests';
11
12
  import './tests/calculated-columns-tests';
12
13
  import './tests/panel-renderer-tests';
@@ -425,7 +425,40 @@ category('fit', () => {
425
425
  expect(boxPlotStats.q2, 0.8257747292518616);
426
426
  expect(boxPlotStats.q3, 0.9558155536651611);
427
427
  expect(boxPlotStats.lowerAdjacentValue, 0.7654603719711304);
428
- expect(boxPlotStats.upperAdjacentValue, 0.9558155536651611);
428
+ // the whisker is the largest value within q3 + 1.5*IQR, which here is every value
429
+ expect(boxPlotStats.upperAdjacentValue, 0.9596694707870483);
430
+ });
431
+
432
+ test('calculateBoxPlotStatistics sorts numerically', async () => {
433
+ // every value here is in [0, 1), so a lexicographic sort happens to agree with a numeric one;
434
+ // these cases are the ones that tell the two apart
435
+ const spread = calculateBoxPlotStatistics([2, 10, 1.5]);
436
+ expect(spread.q1, 1.5);
437
+ expect(spread.q2, 2);
438
+ expect(spread.q3, 10);
439
+
440
+ const unordered = calculateBoxPlotStatistics([5, 4, 3, 2, 1]);
441
+ const ordered = calculateBoxPlotStatistics([1, 2, 3, 4, 5]);
442
+ expect(unordered.q1, ordered.q1);
443
+ expect(unordered.q3, ordered.q3);
444
+ expect(ordered.upperAdjacentValue, 5);
445
+ expect(ordered.lowerAdjacentValue, 1);
446
+ });
447
+
448
+ test('calculateBoxPlotStatistics excludes an outlier from the whisker', async () => {
449
+ const stats = calculateBoxPlotStatistics([1, 2, 3, 4, 100]);
450
+ expect(stats.q1, 2);
451
+ expect(stats.q3, 4);
452
+ // 100 is beyond q3 + 1.5*IQR, so the whisker stops at 4 and 100 is drawn as an outlier
453
+ expect(stats.upperAdjacentValue, 4);
454
+ });
455
+
456
+ test('calculateBoxPlotStatistics leaves the caller array alone', async () => {
457
+ const values = [3, 1, 2];
458
+ calculateBoxPlotStatistics(values);
459
+ expect(values[0], 3);
460
+ expect(values[1], 1);
461
+ expect(values[2], 2);
429
462
  });
430
463
 
431
464
  test('ICxx is the x at that fraction of the curve', async () => {
@@ -0,0 +1,127 @@
1
+ import {tTest, uTest} from '@datagrok-libraries/statistics/src/tests';
2
+ import {fdrcorrection} from '@datagrok-libraries/statistics/src/multiple-tests';
3
+ import {category, test, expect, expectArray, expectFloat,
4
+ expectExceptionAsync} from '@datagrok-libraries/test/src/test';
5
+
6
+ // References are computed from the textbook formulae, not read back off this library: sample (n-1)
7
+ // variances, Welch-Satterthwaite dof, and t = (m1 - m2) / sqrt(v1/n1 + v2/n2).
8
+ const A = [1, 2, 3, 4, 5];
9
+ const B = [2, 4, 6, 8, 10];
10
+ const EPS = 1e-12;
11
+
12
+ // R's p.adjust(p, method = 'BH'): the running minimum, taken from the largest p downwards, of
13
+ // n/(i+1) * p_i. Independent of alpha - only `reject` depends on it.
14
+ const P = [0.001, 0.008, 0.039, 0.041, 0.042, 0.06, 0.074, 0.205, 0.212, 0.216];
15
+ const ADJUSTED = [0.01, 0.04, 0.084, 0.084, 0.084, 0.1, 0.10571428, 0.216, 0.216, 0.216];
16
+
17
+ category('statistics', () => {
18
+ test('tTest: the Welch branch uses the sample variance', async () => {
19
+ const r = tTest(A, B);
20
+ expectFloat(r['p-value'], 0.10753119493044197, EPS);
21
+ expectFloat(r['p-value less'], 0.053765597465220985, EPS);
22
+ expectFloat(r['Mean difference']!, -3, EPS);
23
+ // the population variance, which is what jStat returns without the flag, puts this at 0.0790
24
+ expect(r['p-value'] > 0.1);
25
+ });
26
+
27
+ test('tTest: the equal-variance branch divides by the pooled standard deviation', async () => {
28
+ const r = tTest(A, B, false, true);
29
+ expectFloat(r['p-value'], 0.09434977284244564, EPS);
30
+ expectFloat(r['p-value less'], 0.04717488642122282, EPS);
31
+ });
32
+
33
+ test('tTest: the known-deviation branch reports a tail probability', async () => {
34
+ const r = tTest(A, B, true);
35
+ expectFloat(r['p-value'], 0.05777957112359733, EPS);
36
+ expectFloat(r['p-value less'], 0.028889785561798664, EPS);
37
+ // a density is bounded by 0.3989 and is blind to the direction; a cdf is neither
38
+ expectFloat(tTest(B, A, true)['p-value less'], 1 - r['p-value less'], EPS);
39
+ });
40
+
41
+ test('tTest: the tails complement each other and the two-tailed p is twice the smaller', async () => {
42
+ const results = [tTest(A, B), tTest(A, B, false, true), tTest(A, B, true)];
43
+ for (const r of results) {
44
+ expectFloat(r['p-value less'] + r['p-value more'], 1, EPS);
45
+ expectFloat(r['p-value'], 2 * Math.min(r['p-value less'], r['p-value more']), EPS);
46
+ }
47
+ });
48
+
49
+ test('tTest: a sample of fewer than two values is refused', async () => {
50
+ await expectExceptionAsync(async () => void tTest([1], B), (e) => `${e}`.includes('Wrong sample size'));
51
+ await expectExceptionAsync(async () => void tTest(A, []), (e) => `${e}`.includes('Wrong sample size'));
52
+ });
53
+
54
+ test('uTest: no ties, hand-computable', async () => {
55
+ // ranks 1..10 with x on the odd ones: R1 = 25, U1 = 10, mu = 12.5, s = sqrt(25/12 * 11)
56
+ const r = uTest([1, 3, 5, 7, 9], [2, 4, 6, 8, 10]);
57
+ expectFloat(r['p-value'], 0.6761033140231467, EPS);
58
+ expectFloat(r['p-value less'], 0.33805165701157336, EPS);
59
+ expectFloat(r['p-value more'], 0.6619483429884266, EPS);
60
+ expectFloat(r['Median difference']!, -1, EPS);
61
+ });
62
+
63
+ test('uTest: ties are corrected with the sum of t^3 - t', async () => {
64
+ // xy ranks [1,3,3,5,8,3,6.5,6.5,9,10]: one group of 3 and one of 2, so the term is 24 + 6 = 30.
65
+ // Summing the group sizes instead gives 10 - the sample size - and a p-value of 0.1416.
66
+ const r = uTest([1, 2, 2, 3, 5], [2, 4, 4, 6, 7]);
67
+ expectFloat(r['p-value'], 0.1375638939099033, EPS);
68
+ expectFloat(r['p-value less'], 0.06878194695495166, EPS);
69
+ });
70
+
71
+ test('uTest: the one-tailed fields are one-tailed', async () => {
72
+ const r = uTest([1, 3, 5, 7, 9], [2, 4, 6, 8, 10]);
73
+ expect(r['p-value less'] !== r['p-value more']);
74
+ expectFloat(r['p-value less'] + r['p-value more'], 1, EPS);
75
+ expectFloat(r['p-value'], 2 * Math.min(r['p-value less'], r['p-value more']), EPS);
76
+
77
+ const swapped = uTest([2, 4, 6, 8, 10], [1, 3, 5, 7, 9]);
78
+ expectFloat(swapped['p-value less'], r['p-value more'], EPS);
79
+ expectFloat(swapped['p-value more'], r['p-value less'], EPS);
80
+ expectFloat(swapped['p-value'], r['p-value'], EPS);
81
+ });
82
+
83
+ test('uTest: the two-tailed p never exceeds 1', async () => {
84
+ // the continuity correction used to push U past the mean of its own null distribution
85
+ const r = uTest([1, 2, 3, 4], [1, 2, 3, 4]);
86
+ expectFloat(r['p-value'], 1, EPS);
87
+ expectFloat(r['p-value less'], 0.5, EPS);
88
+ });
89
+
90
+ test('uTest: a sample of fewer than two values is refused', async () => {
91
+ await expectExceptionAsync(async () => void uTest([1], [1, 2, 3]), (e) => `${e}`.includes('Wrong sample size'));
92
+ await expectExceptionAsync(async () => void uTest([1, 2, 3], []), (e) => `${e}`.includes('Wrong sample size'));
93
+ });
94
+
95
+ test('fdrcorrection: adjusted p-values match Benjamini-Hochberg', async () => {
96
+ const [, adjusted] = fdrcorrection(new Float32Array(P), 0.05, 'i');
97
+ for (let i = 0; i < P.length; i++)
98
+ expectFloat(adjusted[i], ADJUSTED[i], 1e-6);
99
+ });
100
+
101
+ test('fdrcorrection: every hypothesis at or below the line is rejected', async () => {
102
+ const alpha = 0.05;
103
+ const [reject, adjusted] = fdrcorrection(new Float32Array(P), alpha, 'i');
104
+ expectArray(reject, [true, true, false, false, false, false, false, false, false, false]);
105
+ // the largest rejected hypothesis used to be dropped, leaving reject out of step with adjusted
106
+ for (let i = 0; i < P.length; i++)
107
+ expect(reject[i], adjusted[i] <= alpha, `hypothesis ${i}`);
108
+ });
109
+
110
+ test('fdrcorrection: a lone passing hypothesis is rejected', async () => {
111
+ const [reject] = fdrcorrection(new Float32Array([0.001, 0.9, 0.9, 0.9]), 0.05, 'i');
112
+ expectArray(reject, [true, false, false, false]);
113
+ });
114
+
115
+ test('fdrcorrection: rejections come back in the caller order', async () => {
116
+ const shuffled = [0.216, 0.008, 0.212, 0.001, 0.042, 0.041, 0.074, 0.039, 0.205, 0.06];
117
+ const [reject, adjusted] = fdrcorrection(new Float32Array(shuffled), 0.05, 'i');
118
+ expectArray(reject, [false, true, false, true, false, false, false, false, false, false]);
119
+ for (let i = 0; i < shuffled.length; i++)
120
+ expectFloat(adjusted[i], ADJUSTED[P.indexOf(shuffled[i])], 1e-6);
121
+ });
122
+
123
+ test('fdrcorrection: nothing is rejected when nothing passes', async () => {
124
+ const [reject] = fdrcorrection(new Float32Array([0.4, 0.5, 0.6]), 0.05, 'i');
125
+ expectArray(reject, [false, false, false]);
126
+ });
127
+ });