@datagrok/curves 1.6.0 → 1.9.0

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 (58) hide show
  1. package/.eslintrc.json +39 -0
  2. package/.vscode/launch.json +10 -9
  3. package/.vscode/tasks.json +14 -0
  4. package/CHANGELOG.md +33 -0
  5. package/README.md +36 -8
  6. package/databases/plates/0000_init.sql +167 -0
  7. package/detectors.js +2 -2
  8. package/dist/package-test.js +15930 -1
  9. package/dist/package-test.js.map +1 -1
  10. package/dist/package.js +13750 -1
  11. package/dist/package.js.map +1 -1
  12. package/img/plate-dose-response-series.gif +0 -0
  13. package/img/plate-readers.gif +0 -0
  14. package/package.json +17 -11
  15. package/queries/plates-crud.sql +148 -0
  16. package/src/fit/const.ts +60 -59
  17. package/src/fit/data-to-curves.ts +220 -0
  18. package/src/fit/fit-demo.ts +6 -6
  19. package/src/fit/fit-grid-cell-handler.ts +94 -85
  20. package/src/fit/fit-renderer.ts +162 -98
  21. package/src/fit/multi-curve-viewer.ts +69 -28
  22. package/src/fit/render-utils.ts +346 -288
  23. package/src/package-api.ts +167 -0
  24. package/src/package-test.ts +7 -1
  25. package/src/package.g.ts +140 -2
  26. package/src/package.ts +221 -79
  27. package/src/plate/excel-plates.ts +112 -0
  28. package/src/plate/plate-cell-renderer.ts +48 -0
  29. package/src/plate/plate-drc-analysis.ts +262 -0
  30. package/src/plate/plate-reader.ts +28 -0
  31. package/src/plate/plate-well-validators.ts +41 -0
  32. package/src/plate/plate-widget.ts +242 -0
  33. package/src/plate/plate.ts +557 -0
  34. package/src/plate/plates-folder-preview.ts +63 -0
  35. package/src/plate/readers/bmg-pherastar-96.ts +42 -0
  36. package/src/plate/readers/delfia-envision-96-max-rlu.ts +16 -0
  37. package/src/plate/readers/delfia-envision-by-row.ts +18 -0
  38. package/src/plate/readers/spectramax.ts +42 -0
  39. package/src/plate/utils.ts +141 -0
  40. package/src/plates/numeric_matcher.ts +191 -0
  41. package/src/plates/objects/plate-template-handler.ts +48 -0
  42. package/src/plates/plates-app.ts +64 -0
  43. package/src/plates/plates-crud.ts +502 -0
  44. package/src/plates/plates-demo.ts +107 -0
  45. package/src/plates/plates.sql +169 -0
  46. package/src/plates/views/plates-create-view.ts +123 -0
  47. package/src/plates/views/plates-schema-view.ts +58 -0
  48. package/src/plates/views/plates-search-view.ts +137 -0
  49. package/src/plates/views/plates-templates-view.ts +38 -0
  50. package/src/tests/curves-cell-renderer-tests.ts +95 -75
  51. package/src/tests/detector-tests.ts +3 -2
  52. package/src/tests/fit-tests.ts +199 -163
  53. package/src/tests/plate-tests.ts +153 -0
  54. package/src/tests/transform-tests.ts +6 -6
  55. package/test-console-output-1.log +435 -0
  56. package/test-record-1.mp4 +0 -0
  57. package/tsconfig.json +1 -1
  58. package/src/fit/cell-render-viewer.ts +0 -54
@@ -1,25 +1,36 @@
1
+ /* eslint-disable max-len */
1
2
  import * as DG from 'datagrok-api/dg';
2
3
  import * as ui from 'datagrok-api/ui';
4
+ import * as grok from 'datagrok-api/grok';
3
5
 
4
6
  import {
5
- fitSeries,
6
7
  getSeriesStatistics,
7
8
  getSeriesFitFunction,
8
9
  LogOptions,
9
10
  getChartBounds,
10
11
  } from '@datagrok-libraries/statistics/src/fit/fit-data';
11
- import {statisticsProperties, fitSeriesProperties, fitChartDataProperties, IFitChartData, IFitSeries, IFitChartOptions, IFitSeriesOptions, FitStatistics} from '@datagrok-libraries/statistics/src/fit/fit-curve';
12
12
  import {
13
- getChartData,
13
+ statisticsProperties,
14
+ fitSeriesProperties,
15
+ fitChartDataProperties,
16
+ IFitChartData,
17
+ IFitSeries,
18
+ IFitChartOptions,
19
+ IFitSeriesOptions,
20
+ FitStatistics,
21
+ FIT_FUNCTION_4PL_REGRESSION, FIT_FUNCTION_SIGMOID,
22
+ } from '@datagrok-libraries/statistics/src/fit/fit-curve';
23
+ import {
24
+ getOrCreateParsedChartData,
14
25
  getColumnChartOptions,
15
26
  getDataFrameChartOptions,
16
27
  isColorValid,
17
28
  mergeProperties,
18
- substituteZeroes
29
+ substituteZeroes, getOrCreateCachedFitCurve, getOrCreateCachedCurvesDataPoints, FitChartCellRenderer
19
30
  } from './fit-renderer';
20
- import {CellRenderViewer} from './cell-render-viewer';
21
31
  import {convertXMLToIFitChartData} from './fit-parser';
22
32
  import {FitConstants} from './const';
33
+ import {ColorType, getSeriesColor} from './render-utils';
23
34
 
24
35
 
25
36
  const CHART_OPTIONS = 'chartOptions';
@@ -49,30 +60,34 @@ const AGGREGATION_TYPES: {[key: string]: string} = {
49
60
  };
50
61
 
51
62
 
52
- export function calculateSeriesStats(series: IFitSeries, chartLogOptions: LogOptions): FitStatistics {
63
+ export function calculateSeriesStats(series: IFitSeries, seriesIdx: number, chartLogOptions: LogOptions,
64
+ tableCell: DG.Cell, useCache: boolean = true): FitStatistics {
53
65
  const fitFunction = getSeriesFitFunction(series);
54
66
  if (series.parameters) {
55
- if (chartLogOptions.logX)
67
+ if (chartLogOptions.logX) {
56
68
  if (series.parameters[2] > 0)
57
69
  series.parameters[2] = Math.log10(series.parameters[2]);
58
- }
59
- else {
60
- const params = fitSeries(series, fitFunction, chartLogOptions).parameters;
70
+ }
71
+ } else {
72
+ const params = getOrCreateCachedFitCurve(series, seriesIdx, fitFunction, chartLogOptions, tableCell, useCache).parameters;
61
73
  series.parameters = [...params];
62
74
  }
63
75
 
64
- const seriesStatistics = getSeriesStatistics(series, fitFunction, chartLogOptions);
76
+ const seriesStatistics = getSeriesStatistics(series, fitFunction,
77
+ getOrCreateCachedCurvesDataPoints(series, seriesIdx, chartLogOptions, false, tableCell), chartLogOptions);
78
+ if ([FIT_FUNCTION_4PL_REGRESSION, FIT_FUNCTION_SIGMOID].includes(fitFunction.name) && chartLogOptions.logX && seriesStatistics.interceptX !== undefined && seriesStatistics.interceptX < 0)
79
+ seriesStatistics.interceptX = Math.pow(10, seriesStatistics.interceptX);
65
80
  return seriesStatistics;
66
81
  }
67
82
 
68
- export function getChartDataAggrStats(chartData: IFitChartData, aggrType: string): FitStatistics {
83
+ export function getChartDataAggrStats(chartData: IFitChartData, aggrType: string, tableCell: DG.Cell): FitStatistics {
69
84
  const chartLogOptions: LogOptions = {logX: chartData.chartOptions?.logX, logY: chartData.chartOptions?.logY};
70
- const rSquaredValues: number[] = [], aucValues: number[] = [], interceptXValues: number[] = [], interceptYValues: number[] = [],
71
- slopeValues: number[] = [], topValues: number[] = [], bottomValues: number[] = [];
85
+ const rSquaredValues: number[] = []; const aucValues: number[] = []; const interceptXValues: number[] = []; const interceptYValues: number[] = [];
86
+ const slopeValues: number[] = []; const topValues: number[] = []; const bottomValues: number[] = [];
72
87
  for (let i = 0, j = 0; i < chartData.series?.length!; i++) {
73
88
  if (chartData.series![i].points.every((p) => p.outlier))
74
89
  continue;
75
- const seriesStats = calculateSeriesStats(chartData.series![i], chartLogOptions);
90
+ const seriesStats = calculateSeriesStats(chartData.series![i], i, chartLogOptions, tableCell);
76
91
  rSquaredValues[j] = seriesStats.rSquared!;
77
92
  aucValues[j] = seriesStats.auc!;
78
93
  interceptXValues[j] = seriesStats.interceptX!;
@@ -106,8 +121,7 @@ function changePlotOptions(chartData: IFitChartData, inputBase: DG.InputBase, op
106
121
  if (options === CHART_OPTIONS) {
107
122
  if (chartData.chartOptions === undefined) return;
108
123
  (chartData.chartOptions[propertyName as keyof IFitChartOptions] as any) = inputBase.value;
109
- }
110
- else if (options === SERIES_OPTIONS) {
124
+ } else if (options === SERIES_OPTIONS) {
111
125
  if (chartData.series === undefined) return;
112
126
  for (let i = 0; i < chartData.series.length; i++)
113
127
  (chartData.series[i][propertyName as keyof IFitSeries] as any) = inputBase.value;
@@ -174,18 +188,16 @@ function changeCurvesOptions(gridCell: DG.GridCell, inputBase: DG.InputBase, opt
174
188
  const chartData: IFitChartData = JSON.parse(value ?? '{}') ?? {};
175
189
  changePlotOptions(chartData, inputBase, options);
176
190
  gridCell.cell.value = JSON.stringify(chartData);
177
- }
178
- else {
191
+ } else {
179
192
  let columns: DG.Column[];
180
193
  if (manipulationLevel === MANIPULATION_LEVEL.DATAFRAME) {
181
194
  gridCell.cell.dataFrame.tags[FitConstants.TAG_FIT] = JSON.stringify(chartOptions);
182
195
  columns = gridCell.cell.dataFrame.columns.bySemTypeAll(FitConstants.FIT_SEM_TYPE);
183
- }
184
- else {
196
+ } else {
185
197
  gridCell.cell.column.tags[FitConstants.TAG_FIT] = JSON.stringify(chartOptions);
186
198
  columns = [gridCell.cell.column];
187
199
  }
188
-
200
+
189
201
  for (let i = 0; i < columns.length; i++) {
190
202
  if (manipulationLevel === MANIPULATION_LEVEL.DATAFRAME) {
191
203
  const columnChartOptions = getColumnChartOptions(columns[i]);
@@ -204,15 +216,15 @@ function changeCurvesOptions(gridCell: DG.GridCell, inputBase: DG.InputBase, opt
204
216
  if (chartData.chartOptions[propertyName as keyof IFitChartOptions] === undefined)
205
217
  return value;
206
218
  delete chartData.chartOptions[propertyName as keyof IFitChartOptions];
207
- }
208
- else {
219
+ } else {
209
220
  if (chartData.series === undefined) return value;
210
221
  let isSeriesChanged = false;
211
- for (const series of chartData.series)
222
+ for (const series of chartData.series) {
212
223
  if (series[propertyName as keyof IFitSeriesOptions] !== undefined) {
213
224
  delete series[propertyName as keyof IFitSeriesOptions];
214
225
  isSeriesChanged = true;
215
226
  }
227
+ }
216
228
  if (chartData.seriesOptions)
217
229
  delete chartData.seriesOptions[propertyName as keyof IFitSeriesOptions];
218
230
  if (!isSeriesChanged) return value;
@@ -234,13 +246,13 @@ export class FitGridCellHandler extends DG.ObjectHandler {
234
246
  isApplicable(x: any): boolean {
235
247
  return x instanceof DG.GridCell && x.cellType === FitConstants.FIT_CELL_TYPE;
236
248
  }
237
-
249
+
238
250
  // TODO: add aspect ratio for the cell
239
251
  // TODO: add legend
240
252
  // TODO: add the table for the values on the cell or don't render it at all
241
253
  // TODO: fix the curves demo app
242
254
 
243
- renderProperties(gridCell: DG.GridCell, context: any = null): HTMLElement {
255
+ renderProperties(gridCell: DG.GridCell, _context: any = null): HTMLElement {
244
256
  const acc = ui.accordion('Curves property panel');
245
257
  // TODO: make just the base ui.input.choice after nullable option is added
246
258
  const switchProperty = DG.Property.js('level', DG.TYPE.STRING, {description: 'Controls the level at which properties will be switched',
@@ -250,47 +262,35 @@ export class FitGridCellHandler extends DG.ObjectHandler {
250
262
  // temporarily because input doesn't show the tooltip
251
263
  ui.tooltip.bind(switchLevelInput.captionLabel, 'Controls the level at which properties will be switched');
252
264
 
253
- const chartData = getChartData(gridCell);
265
+ const chartData = getOrCreateParsedChartData(gridCell.cell);
254
266
  const columnChartOptions = getColumnChartOptions(gridCell.cell.column);
255
267
  const dfChartOptions = getDataFrameChartOptions(gridCell.cell.dataFrame);
256
268
 
257
- const seriesOptionsRefresh = {onValueChanged: (v: any, inputBase: DG.InputBase) =>
269
+ const seriesOptionsRefresh = {onValueChanged: (v: any, inputBase: DG.InputBase) =>
258
270
  changeCurvesOptions(gridCell, inputBase, SERIES_OPTIONS, switchLevelInput.value)};
259
271
  const chartOptionsRefresh = {onValueChanged: (v: any, inputBase: DG.InputBase) =>
260
272
  changeCurvesOptions(gridCell, inputBase, CHART_OPTIONS, switchLevelInput.value)};
261
273
 
262
- if (!isColorValid(dfChartOptions.seriesOptions?.pointColor) && !isColorValid(columnChartOptions.seriesOptions?.pointColor)) {
263
- if (chartData.seriesOptions) {
264
- if (!isColorValid(chartData.seriesOptions.pointColor))
265
- chartData.seriesOptions.pointColor = DG.Color.toHtml(DG.Color.getCategoricalColor(0));
266
- }
267
- else {
268
- if (!isColorValid(chartData.series ? chartData.series![0].pointColor : ''))
269
- chartData.series![0].pointColor = DG.Color.toHtml(DG.Color.getCategoricalColor(0));
270
- }
271
- }
272
-
273
- if (!isColorValid(dfChartOptions.seriesOptions?.fitLineColor) && !isColorValid(columnChartOptions.seriesOptions?.fitLineColor)) {
274
- if (chartData.seriesOptions) {
275
- if (!isColorValid(chartData.seriesOptions.fitLineColor))
276
- chartData.seriesOptions.fitLineColor = DG.Color.toHtml(DG.Color.getCategoricalColor(0));
277
- }
278
- else {
279
- if (!isColorValid(chartData.series ? chartData.series![0].fitLineColor : ''))
280
- chartData.series![0].fitLineColor = DG.Color.toHtml(DG.Color.getCategoricalColor(0));
281
- }
282
- }
283
-
284
- if (!isColorValid(dfChartOptions.seriesOptions?.outlierColor) && !isColorValid(columnChartOptions.seriesOptions?.outlierColor)) {
285
- if (chartData.seriesOptions) {
286
- if (!isColorValid(chartData.seriesOptions.outlierColor))
287
- chartData.seriesOptions.outlierColor = DG.Color.toHtml(DG.Color.red);
288
- }
289
- else {
290
- if (!isColorValid(chartData.series ? chartData.series![0].outlierColor : ''))
291
- chartData.series![0].outlierColor = DG.Color.toHtml(DG.Color.red);
274
+ const setValidColors = (colorFieldName: string) => {
275
+ if (dfChartOptions.seriesOptions && !isColorValid(dfChartOptions.seriesOptions[colorFieldName]) &&
276
+ columnChartOptions.seriesOptions && !isColorValid(columnChartOptions.seriesOptions[colorFieldName])) {
277
+ if (chartData.seriesOptions) {
278
+ if (!isColorValid(chartData.seriesOptions[colorFieldName])) {
279
+ chartData.seriesOptions[colorFieldName] = DG.Color.toHtml(colorFieldName === 'outlierColor' ?
280
+ DG.Color.red : DG.Color.getCategoricalColor(0));
281
+ }
282
+ } else {
283
+ if (!isColorValid(chartData.series ? chartData.series[0][colorFieldName] : '')) {
284
+ chartData.series![0][colorFieldName] = DG.Color.toHtml(colorFieldName === 'outlierColor' ?
285
+ DG.Color.red : DG.Color.getCategoricalColor(0));
286
+ }
287
+ }
292
288
  }
293
- }
289
+ };
290
+ const tableCell = gridCell.cell;
291
+ setValidColors('pointColor');
292
+ setValidColors('fitLineColor');
293
+ setValidColors('outlierColor');
294
294
 
295
295
  mergeProperties(fitSeriesProperties, columnChartOptions.seriesOptions, chartData.seriesOptions ? chartData.seriesOptions :
296
296
  chartData.series ? chartData.series[0] ?? {} : {});
@@ -311,9 +311,10 @@ export class FitGridCellHandler extends DG.ObjectHandler {
311
311
  ui.forms.addGroup(form, 'Chart options', fitChartDataProperties.map((p) => ui.input.forProperty(p, chartData.chartOptions, chartOptionsRefresh)));
312
312
  acc.addPane('Options', () => form);
313
313
 
314
+ const choices = (chartData.series?.length ?? 0) > 1 ? ['all', 'aggregated'] : ['all'];
314
315
  const seriesStatsProperty = DG.Property.js('series', DG.TYPE.STRING,
315
316
  {description: 'Controls whether to show series statistics or aggregated statistics',
316
- defaultValue: 'All', choices: ['all', 'aggregated'], nullable: false});
317
+ defaultValue: 'all', choices: choices, nullable: false});
317
318
  const seriesStatsInput = ui.input.forProperty(seriesStatsProperty, null, {onValueChanged: () => {
318
319
  acc.getPane('Fit').root.lastElementChild!.replaceChildren(createFitPane());
319
320
  }});
@@ -325,7 +326,9 @@ export class FitGridCellHandler extends DG.ObjectHandler {
325
326
  }});
326
327
 
327
328
  function createFitPane(): HTMLElement {
328
- const host = ui.divV(seriesStatsInput.stringValue === 'aggregated' ? [seriesStatsInput.root, aggrTypeInput.root] : [seriesStatsInput.root]);
329
+ const hostItems = (chartData.series?.length ?? 0) > 1 ? seriesStatsInput.stringValue === 'aggregated' ?
330
+ [seriesStatsInput.root, aggrTypeInput.root] : [seriesStatsInput.root] : null;
331
+ const host = ui.divV(hostItems!);
329
332
  const dataBounds = getChartBounds(chartData);
330
333
  if (dataBounds.x <= 0 && chartData.chartOptions) chartData.chartOptions.logX = false;
331
334
  if (dataBounds.y <= 0 && chartData.chartOptions) chartData.chartOptions.logY = false;
@@ -334,42 +337,48 @@ export class FitGridCellHandler extends DG.ObjectHandler {
334
337
  const chartLogOptions: LogOptions = {logX: chartData.chartOptions?.logX, logY: chartData.chartOptions?.logY};
335
338
  for (let i = 0; i < chartData.series!.length; i++) {
336
339
  const series = chartData.series![i];
337
- const seriesStatistics = calculateSeriesStats(series, chartLogOptions);
338
-
339
- const color = series.fitLineColor ? DG.Color.fromHtml(series.fitLineColor) ?
340
- series.fitLineColor : DG.Color.toHtml(DG.Color.getCategoricalColor(i)) : DG.Color.toHtml(DG.Color.getCategoricalColor(i));
340
+ const seriesStatistics = calculateSeriesStats(series, i, chartLogOptions, tableCell);
341
+
342
+ const color = getSeriesColor(series, i, ColorType.FIT_LINE);
343
+ const seriesName = series.name ?? 'series ' + i;
341
344
  host.appendChild(ui.panel([
342
- ui.h1(series.name ?? 'series ' + i, {style: {color: color}}),
345
+ ui.h1(seriesName, {style: {color: color}}),
343
346
  ui.input.form(seriesStatistics, statisticsProperties, {
344
347
  onCreated: (input) => input.root.appendChild(ui.iconFA('plus', async () => {
345
- const funcParams = {df: gridCell.cell.dataFrame, colName: gridCell.gridColumn.name, propName: input.property.name, seriesName: series.name, seriesNumber: i};
346
- await DG.Func.find({name: 'addStatisticsColumn'})[0].prepare(funcParams).call(undefined, undefined, {processed: false});
347
- }, `Calculate ${input.property.name} for the whole column`))
348
+ const funcParams = {table: gridCell.cell.dataFrame, colName: gridCell.gridColumn.name, propName: input.property.name, seriesNumber: i};
349
+ await DG.Func.find({name: 'addStatisticsColumn'})[0].prepare(funcParams).call(undefined, undefined, {processed: false});
350
+ }, `Calculate ${input.property.name} for the whole column`))
351
+
352
+ // TODO: Replace with this one after dima merges his branch
353
+ // const newName = gridCell.cell.dataFrame.columns.getUnusedName(`${gridCell.gridColumn.name} ${seriesName} ${input.property.name}`);
354
+ // await gridCell.cell.dataFrame.columns.addNewCalculated(newName, `Curves:addStatisticsColumn("table", \$\{${gridCell.gridColumn.name}\}, "${input.property.name}", ${i})`, undefined, undefined, false);
355
+ // }, `Calculate ${input.property.name} for the whole column`))
348
356
  })
349
357
  ]));
350
358
  }
351
- }
352
- else {
353
- const seriesStatistics = getChartDataAggrStats(chartData, aggrTypeInput.stringValue);
359
+ } else {
360
+ const seriesStatistics = getChartDataAggrStats(chartData, aggrTypeInput.stringValue, tableCell);
354
361
  host.appendChild(ui.panel([
355
- ui.h1(`series ${aggrTypeInput.stringValue}`),
356
- ui.input.form(seriesStatistics, statisticsProperties, {
357
- onCreated: (input) => input.root.appendChild(ui.iconFA('plus', async () => {
358
- const funcParams = {df: gridCell.cell.dataFrame, colName: gridCell.gridColumn.name, propName: input.property.name, aggrType: aggrTypeInput.stringValue};
359
- await DG.Func.find({name: 'addAggrStatisticsColumn'})[0].prepare(funcParams).call(undefined, undefined, {processed: false});
360
- }, `Calculate ${input.property.name} ${aggrTypeInput.stringValue} for the whole column`))
361
- })
362
- ]));
362
+ ui.h1(`series ${aggrTypeInput.stringValue}`),
363
+ ui.input.form(seriesStatistics, statisticsProperties, {
364
+ onCreated: (input) => input.root.appendChild(ui.iconFA('plus', async () => {
365
+ const funcParams = {table: gridCell.cell.dataFrame, colName: gridCell.gridColumn.name, propName: input.property.name, aggrType: aggrTypeInput.stringValue};
366
+ await DG.Func.find({name: 'addAggrStatisticsColumn'})[0].prepare(funcParams).call(undefined, undefined, {processed: false});
367
+ }, `Calculate ${input.property.name} ${aggrTypeInput.stringValue} for the whole column`))
368
+ })
369
+ ]));
363
370
  }
364
371
 
365
372
  return host;
366
373
  }
367
374
 
368
- acc.addPane('Fit', () => {
369
- return createFitPane();
370
- });
375
+ const chartPane = acc.addPane('Chart', () => DG.GridCellWidget.fromGridCell(gridCell).root);
376
+ const screenBounds = FitChartCellRenderer.inflateScreenBounds(gridCell.bounds);
377
+ if (screenBounds.width < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH ||
378
+ screenBounds.height < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT)
379
+ chartPane.expanded = true;
371
380
 
372
- acc.addPane('Chart', () => CellRenderViewer.fromGridCell(gridCell).root);
381
+ acc.addPane('Fit', () => createFitPane());
373
382
 
374
383
  return acc.root;
375
384
  }