@datagrok/curves 1.13.0 → 1.13.2
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/.turbo/turbo-build.log +24 -0
- package/CHANGELOG.md +8 -0
- package/CLAUDE.md +4 -2
- package/LICENSE.md +16 -0
- package/dist/package-test.js +30 -16967
- package/dist/package-test.js.map +1 -1
- package/dist/package.js +1 -12779
- package/dist/package.js.map +1 -1
- package/package.json +17 -43
- package/src/fit/data-to-curves.ts +1 -1
- package/src/fit/fit-chart-data.ts +19 -12
- package/src/fit/fit-grid-cell-handler.ts +4 -4
- package/src/fit/fit-options.ts +4 -4
- package/src/fit/multi-curve-viewer.ts +2 -2
- package/src/package-test.ts +1 -0
- package/src/tests/fit-tests.ts +34 -1
- package/src/tests/panel-renderer-tests.ts +10 -3
- package/src/tests/project-tests.ts +24 -0
- package/src/tests/statistics-tests.ts +127 -0
- package/test-console-output-1.log +351 -285
- package/test-record-1.mp4 +0 -0
- package/tsconfig.json +4 -69
- package/.eslintrc.json +0 -39
- package/webpack.config.js +0 -62
package/package.json
CHANGED
|
@@ -1,55 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@datagrok/curves",
|
|
3
3
|
"friendlyName": "Curves",
|
|
4
|
-
"version": "1.13.
|
|
4
|
+
"version": "1.13.2",
|
|
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/
|
|
12
|
-
"@
|
|
13
|
-
"
|
|
14
|
-
"@types/wu": "^2.1.42",
|
|
15
|
-
"@typescript-eslint/eslint-plugin": "^5.62.0",
|
|
16
|
-
"@typescript-eslint/parser": "^5.62.0",
|
|
17
|
-
"cash-dom": "^8.0.0",
|
|
11
|
+
"@datagrok-libraries/test": "1.4.1",
|
|
12
|
+
"@types/wu": "^2.1.44",
|
|
13
|
+
"cash-dom": "8.1.5",
|
|
18
14
|
"datagrok-api": "^1.27.0",
|
|
19
|
-
"dayjs": "
|
|
20
|
-
"
|
|
21
|
-
"eslint-config-google": "^0.14.0",
|
|
22
|
-
"exceljs": "^4.3.0",
|
|
15
|
+
"dayjs": "1.11.23",
|
|
16
|
+
"exceljs": "4.4.0",
|
|
23
17
|
"jstat": "^1.9.6",
|
|
24
|
-
"rxjs": "
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"css-loader": "^5.0.0",
|
|
28
|
-
"datagrok-tools": "^6.1.6",
|
|
29
|
-
"source-map-loader": "^4.0.1",
|
|
30
|
-
"style-loader": "^2.0.0",
|
|
31
|
-
"ts-loader": "^9.5.1",
|
|
32
|
-
"typescript": "^5.6.3",
|
|
33
|
-
"webpack": "^5.95.0",
|
|
34
|
-
"webpack-cli": "^5.1.4"
|
|
35
|
-
},
|
|
36
|
-
"overrides": {
|
|
37
|
-
"datagrok-api": "$datagrok-api",
|
|
38
|
-
"uuid": "^11.1.1"
|
|
39
|
-
},
|
|
40
|
-
"scripts": {
|
|
41
|
-
"link-all": "npm link datagrok-api @datagrok-libraries/utils @datagrok-libraries/statistics",
|
|
42
|
-
"debug-curves": "webpack && grok publish",
|
|
43
|
-
"release-curves": "webpack && grok publish --release",
|
|
44
|
-
"build-curves": "webpack",
|
|
45
|
-
"build": "grok api && grok check --soft && webpack",
|
|
46
|
-
"debug-curves-dev": "webpack && grok publish dev",
|
|
47
|
-
"release-curves-dev": "webpack && grok publish dev --release",
|
|
48
|
-
"debug-curves-public": "webpack && grok publish public",
|
|
49
|
-
"release-curves-public": "webpack && grok publish public --release",
|
|
50
|
-
"debug-curves-local": "webpack && grok publish local",
|
|
51
|
-
"test": "grok test",
|
|
52
|
-
"build-all": "npm --prefix ./../../js-api run build && npm --prefix ./../../libraries/utils run build && npm --prefix ./../../libraries/statistics run build && npm run build"
|
|
18
|
+
"rxjs": "6.6.7",
|
|
19
|
+
"@datagrok-libraries/statistics": "^1.12.14",
|
|
20
|
+
"@datagrok-libraries/utils": "^4.7.9"
|
|
53
21
|
},
|
|
54
22
|
"canEdit": [
|
|
55
23
|
"Administrators"
|
|
@@ -62,5 +30,11 @@
|
|
|
62
30
|
"url": "https://github.com/datagrok-ai/public.git",
|
|
63
31
|
"directory": "packages/Curves"
|
|
64
32
|
},
|
|
65
|
-
"category": "Visualizations"
|
|
66
|
-
|
|
33
|
+
"category": "Visualizations",
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "grok build",
|
|
36
|
+
"typecheck": "grok tsc --noEmit -p tsconfig.json",
|
|
37
|
+
"lint": "eslint --ext .ts,.tsx src",
|
|
38
|
+
"test": "grok test"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -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
|
|
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
|
|
|
@@ -272,16 +272,16 @@ export function getOrCreateCachedCurvesDataPoints(series: IFitSeries, idx: numbe
|
|
|
272
272
|
/** Reads a level's options, migrating a value stored under the pre-`.%` tag name so that only one of
|
|
273
273
|
* the two ever holds the options. */
|
|
274
274
|
function readChartOptions(tags: any): IFitChartData {
|
|
275
|
-
//
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
275
|
+
// TODO: once Curves requires platform 1.28, move '.fit' onto '.%fit' on read again, so an old
|
|
276
|
+
// table's options travel with its layouts without waiting for the next option change
|
|
277
|
+
return JSON.parse(tags[FitConstants.TAG_FIT] || tags[FitConstants.TAG_FIT_LEGACY] ||
|
|
278
|
+
JSON.stringify(createDefaultChartData()));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Stores a level's options, dropping the legacy copy so only one of the two tags ever holds them. */
|
|
282
|
+
export function storeChartOptions(tags: any, chartOptions: IFitChartData): void {
|
|
283
|
+
tags[FitConstants.TAG_FIT] = JSON.stringify(chartOptions);
|
|
284
|
+
delete tags[FitConstants.TAG_FIT_LEGACY];
|
|
285
285
|
}
|
|
286
286
|
|
|
287
287
|
/** Returns existing, or creates new dataframe default chart options. */
|
|
@@ -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
|
-
|
|
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
|
|
129
|
-
chartData.series
|
|
130
|
-
|
|
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
|
}
|
package/src/fit/fit-options.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
} from '@datagrok-libraries/statistics/src/fit/fit-curve';
|
|
12
12
|
import {FitConstants} from '@datagrok-libraries/statistics/src/fit/const';
|
|
13
13
|
import {fitFunctions, fitFunctionDescriptions, fitSeriesProperties, getStatisticProperty, DEFAULT_FIT_FUNCTION} from '@datagrok-libraries/statistics/src/fit/fit-engine';
|
|
14
|
-
import {getColumnChartOptions, getDataFrameChartOptions, CHART_OPTIONS, SERIES_OPTIONS} from './fit-chart-data';
|
|
14
|
+
import {getColumnChartOptions, getDataFrameChartOptions, storeChartOptions, CHART_OPTIONS, SERIES_OPTIONS} from './fit-chart-data';
|
|
15
15
|
import {isNativeFormat} from './curve-converter';
|
|
16
16
|
|
|
17
17
|
// Options a statistic can depend on; everything else only repaints. errorModel weights the objective
|
|
@@ -172,10 +172,10 @@ export function changeCurvesOptions(gridCell: DG.GridCell, inputBase: DG.InputBa
|
|
|
172
172
|
|
|
173
173
|
let columns: DG.Column[];
|
|
174
174
|
if (manipulationLevel === MANIPULATION_LEVEL.DATAFRAME) {
|
|
175
|
-
gridCell.cell.dataFrame.tags
|
|
175
|
+
storeChartOptions(gridCell.cell.dataFrame.tags, chartOptions);
|
|
176
176
|
columns = gridCell.cell.dataFrame.columns.bySemTypeAll(FitConstants.FIT_SEM_TYPE);
|
|
177
177
|
} else {
|
|
178
|
-
gridCell.cell.column.tags
|
|
178
|
+
storeChartOptions(gridCell.cell.column.tags, chartOptions);
|
|
179
179
|
columns = [gridCell.cell.column];
|
|
180
180
|
}
|
|
181
181
|
|
|
@@ -186,7 +186,7 @@ export function changeCurvesOptions(gridCell: DG.GridCell, inputBase: DG.InputBa
|
|
|
186
186
|
if (section)
|
|
187
187
|
delete (section as any)[propertyName];
|
|
188
188
|
unclaim(columnChartOptions, options, propertyName);
|
|
189
|
-
column.tags
|
|
189
|
+
storeChartOptions(column.tags, columnChartOptions);
|
|
190
190
|
}
|
|
191
191
|
if (!isNativeFormat(column))
|
|
192
192
|
continue;
|
|
@@ -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();
|
package/src/package-test.ts
CHANGED
|
@@ -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';
|
package/src/tests/fit-tests.ts
CHANGED
|
@@ -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
|
-
|
|
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 () => {
|
|
@@ -246,7 +246,7 @@ category('panel and renderer', () => {
|
|
|
246
246
|
}
|
|
247
247
|
});
|
|
248
248
|
|
|
249
|
-
test('options stored under the legacy tag are read and
|
|
249
|
+
test('options stored under the legacy tag are read, and move onto the layout-carried one when written', async () => {
|
|
250
250
|
// only '.%' tags reach a layout, so options in '.fit' never travelled to a fresh table
|
|
251
251
|
expect(FitConstants.TAG_FIT.startsWith('.%'), true, 'the fit tag is no longer carried by layouts');
|
|
252
252
|
|
|
@@ -255,8 +255,15 @@ category('panel and renderer', () => {
|
|
|
255
255
|
col.setTag(FitConstants.TAG_FIT_LEGACY, JSON.stringify({chartOptions: {logX: true}}));
|
|
256
256
|
|
|
257
257
|
expect(getColumnChartOptions(col).chartOptions!.logX, true, 'options under the legacy tag were ignored');
|
|
258
|
-
expect(
|
|
259
|
-
|
|
258
|
+
expect(col.getTag(FitConstants.TAG_FIT) == null, true, 'reading the options wrote a tag');
|
|
259
|
+
|
|
260
|
+
changeCurvesOptions(DG.Viewer.grid(df).cell('curve', 0),
|
|
261
|
+
{property: {name: 'showCurveConfidenceInterval'}, value: false} as unknown as DG.InputBase,
|
|
262
|
+
'seriesOptions', 'Column');
|
|
263
|
+
const stored = JSON.parse(col.getTag(FitConstants.TAG_FIT)) as IFitChartData;
|
|
264
|
+
expect(stored.chartOptions!.logX, true, 'the legacy options were lost when the column was written');
|
|
265
|
+
expect(stored.seriesOptions!.showCurveConfidenceInterval, false, 'the change was not stored');
|
|
266
|
+
expect(col.getTag(FitConstants.TAG_FIT_LEGACY) == null, true, 'both tags now hold the options');
|
|
260
267
|
});
|
|
261
268
|
|
|
262
269
|
test('the panel offers the label names the cell carries', async () => {
|
|
@@ -2,6 +2,8 @@ import * as DG from 'datagrok-api/dg';
|
|
|
2
2
|
import * as grok from 'datagrok-api/grok';
|
|
3
3
|
|
|
4
4
|
import {awaitCheck, category, delay, expect, test} from '@datagrok-libraries/test/src/test';
|
|
5
|
+
import {FitConstants} from '@datagrok-libraries/statistics/src/fit/const';
|
|
6
|
+
import {createDefaultChartData} from '@datagrok-libraries/statistics/src/fit/fit-data';
|
|
5
7
|
import {changeCurvesOptions} from '../fit/fit-options';
|
|
6
8
|
import {getColumnChartOptions, getOrCreateParsedChartData} from '../fit/fit-chart-data';
|
|
7
9
|
|
|
@@ -90,6 +92,28 @@ category('projects', () => {
|
|
|
90
92
|
await delay(100);
|
|
91
93
|
}, {timeout: 120000});
|
|
92
94
|
|
|
95
|
+
test('painting a table leaves its dataframe tags alone, so its layout saves', async () => {
|
|
96
|
+
// platforms up to 1.27.x fail to encode a layout whose table carries a dataframe '.%' tag
|
|
97
|
+
const df = await grok.dapi.files.readCsv(`System:AppData/Curves/${DEMO_FILE}`);
|
|
98
|
+
df.name = 'curvesPaintedLayout';
|
|
99
|
+
// what Curves before 1.13 left on every table it painted
|
|
100
|
+
df.setTag(FitConstants.TAG_FIT_LEGACY, JSON.stringify(createDefaultChartData()));
|
|
101
|
+
await grok.data.detectSemanticTypes(df);
|
|
102
|
+
const tv = grok.shell.addTableView(df);
|
|
103
|
+
let layout: DG.ViewLayout | null = null;
|
|
104
|
+
try {
|
|
105
|
+
for (const col of df.columns.bySemTypeAll(FitConstants.FIT_SEM_TYPE))
|
|
106
|
+
getOrCreateParsedChartData(df.cell(0, col.name), false);
|
|
107
|
+
await delay(500);
|
|
108
|
+
expect(df.getTag(FitConstants.TAG_FIT) == null, true, 'reading the options tagged the dataframe');
|
|
109
|
+
layout = await grok.dapi.layouts.save(tv.saveLayout());
|
|
110
|
+
} finally {
|
|
111
|
+
if (layout)
|
|
112
|
+
await grok.dapi.layouts.delete(layout);
|
|
113
|
+
tv.close();
|
|
114
|
+
}
|
|
115
|
+
}, {timeout: 60000});
|
|
116
|
+
|
|
93
117
|
test('column-level option survives a datasync project', async () => {
|
|
94
118
|
// the table is rebuilt from its creation script, so the cells come back declaring the interval
|
|
95
119
|
// and only the tag the layout carries can turn it off
|
|
@@ -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
|
+
});
|