@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.
- package/.eslintrc.json +39 -0
- package/.vscode/launch.json +10 -9
- package/.vscode/tasks.json +14 -0
- package/CHANGELOG.md +33 -0
- package/README.md +36 -8
- package/databases/plates/0000_init.sql +167 -0
- package/detectors.js +2 -2
- package/dist/package-test.js +15930 -1
- package/dist/package-test.js.map +1 -1
- package/dist/package.js +13750 -1
- package/dist/package.js.map +1 -1
- package/img/plate-dose-response-series.gif +0 -0
- package/img/plate-readers.gif +0 -0
- package/package.json +17 -11
- package/queries/plates-crud.sql +148 -0
- package/src/fit/const.ts +60 -59
- package/src/fit/data-to-curves.ts +220 -0
- package/src/fit/fit-demo.ts +6 -6
- package/src/fit/fit-grid-cell-handler.ts +94 -85
- package/src/fit/fit-renderer.ts +162 -98
- package/src/fit/multi-curve-viewer.ts +69 -28
- package/src/fit/render-utils.ts +346 -288
- package/src/package-api.ts +167 -0
- package/src/package-test.ts +7 -1
- package/src/package.g.ts +140 -2
- package/src/package.ts +221 -79
- package/src/plate/excel-plates.ts +112 -0
- package/src/plate/plate-cell-renderer.ts +48 -0
- package/src/plate/plate-drc-analysis.ts +262 -0
- package/src/plate/plate-reader.ts +28 -0
- package/src/plate/plate-well-validators.ts +41 -0
- package/src/plate/plate-widget.ts +242 -0
- package/src/plate/plate.ts +557 -0
- package/src/plate/plates-folder-preview.ts +63 -0
- package/src/plate/readers/bmg-pherastar-96.ts +42 -0
- package/src/plate/readers/delfia-envision-96-max-rlu.ts +16 -0
- package/src/plate/readers/delfia-envision-by-row.ts +18 -0
- package/src/plate/readers/spectramax.ts +42 -0
- package/src/plate/utils.ts +141 -0
- package/src/plates/numeric_matcher.ts +191 -0
- package/src/plates/objects/plate-template-handler.ts +48 -0
- package/src/plates/plates-app.ts +64 -0
- package/src/plates/plates-crud.ts +502 -0
- package/src/plates/plates-demo.ts +107 -0
- package/src/plates/plates.sql +169 -0
- package/src/plates/views/plates-create-view.ts +123 -0
- package/src/plates/views/plates-schema-view.ts +58 -0
- package/src/plates/views/plates-search-view.ts +137 -0
- package/src/plates/views/plates-templates-view.ts +38 -0
- package/src/tests/curves-cell-renderer-tests.ts +95 -75
- package/src/tests/detector-tests.ts +3 -2
- package/src/tests/fit-tests.ts +199 -163
- package/src/tests/plate-tests.ts +153 -0
- package/src/tests/transform-tests.ts +6 -6
- package/test-console-output-1.log +435 -0
- package/test-record-1.mp4 +0 -0
- package/tsconfig.json +1 -1
- package/src/fit/cell-render-viewer.ts +0 -54
package/src/fit/fit-renderer.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable max-len */
|
|
1
2
|
import * as DG from 'datagrok-api/dg';
|
|
2
3
|
import * as grok from 'datagrok-api/grok';
|
|
3
4
|
import * as ui from 'datagrok-api/ui';
|
|
@@ -5,9 +6,9 @@ import * as ui from 'datagrok-api/ui';
|
|
|
5
6
|
import {
|
|
6
7
|
IFitChartData,
|
|
7
8
|
IFitSeries,
|
|
8
|
-
FitStatistics,
|
|
9
9
|
fitChartDataProperties,
|
|
10
|
-
fitSeriesProperties, IFitChartOptions, IFitPoint,
|
|
10
|
+
fitSeriesProperties, IFitChartOptions, IFitPoint, FitCurve, FitFunction,
|
|
11
|
+
FitStatistics,
|
|
11
12
|
} from '@datagrok-libraries/statistics/src/fit/fit-curve';
|
|
12
13
|
import {Viewport} from '@datagrok-libraries/utils/src/transform';
|
|
13
14
|
|
|
@@ -17,29 +18,36 @@ import {
|
|
|
17
18
|
getChartBounds,
|
|
18
19
|
getSeriesFitFunction,
|
|
19
20
|
getCurve,
|
|
20
|
-
LogOptions,
|
|
21
|
+
LogOptions, getDataPoints,
|
|
21
22
|
} from '@datagrok-libraries/statistics/src/fit/fit-data';
|
|
22
23
|
|
|
23
24
|
import {convertXMLToIFitChartData} from './fit-parser';
|
|
24
|
-
import {CellRenderViewer} from './cell-render-viewer';
|
|
25
|
-
import { calculateSeriesStats, getChartDataAggrStats } from './fit-grid-cell-handler';
|
|
26
25
|
import {FitConstants} from './const';
|
|
27
26
|
import {
|
|
28
|
-
|
|
27
|
+
renderAxesLabels,
|
|
29
28
|
renderConfidenceIntervals, renderConnectDots,
|
|
30
29
|
renderDroplines,
|
|
31
30
|
renderFitLine, renderLegend,
|
|
32
31
|
renderPoints, renderStatistics, renderTitle
|
|
33
32
|
} from './render-utils';
|
|
33
|
+
import {merge} from 'rxjs';
|
|
34
|
+
import {calculateSeriesStats, getChartDataAggrStats} from './fit-grid-cell-handler';
|
|
34
35
|
|
|
35
36
|
|
|
37
|
+
export interface FitCellOutlierToggleArgs {
|
|
38
|
+
gridCell: DG.GridCell;
|
|
39
|
+
series: IFitSeries;
|
|
40
|
+
seriesIdx: number;
|
|
41
|
+
pointIdx: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
36
44
|
function clamp(value: number, min: number, max: number): number {
|
|
37
45
|
return Math.min(Math.max(value, min), max);
|
|
38
46
|
}
|
|
39
47
|
|
|
40
48
|
function showIncorrectFitCell(g: CanvasRenderingContext2D, screenBounds: DG.Rect): void {
|
|
41
49
|
DG.Paint.marker(g, DG.MARKER_TYPE.OUTLIER, screenBounds.midX, screenBounds.midY, DG.Color.red,
|
|
42
|
-
clamp(Math.min(screenBounds.width, screenBounds.height) * 0.8,0, 30));
|
|
50
|
+
clamp(Math.min(screenBounds.width, screenBounds.height) * 0.8, 0, 30));
|
|
43
51
|
}
|
|
44
52
|
|
|
45
53
|
/** Merges properties of the two objects by iterating over the specified {@link properties}
|
|
@@ -135,20 +143,30 @@ export function mergeSeries(series: IFitSeries[]): IFitSeries | null {
|
|
|
135
143
|
return mergedSeries;
|
|
136
144
|
}
|
|
137
145
|
|
|
138
|
-
/**
|
|
146
|
+
/** Returns either cached or constructed chart data for the specified table cell. */
|
|
147
|
+
export function getOrCreateParsedChartData(tableCell: DG.Cell, useCache = true): IFitChartData {
|
|
148
|
+
const column = tableCell?.column;
|
|
149
|
+
return (useCache && column && tableCell) ?
|
|
150
|
+
FitChartCellRenderer.parsedCurves.getOrCreate(`tableId: ${column.dataFrame.id} || tableName: ${column.dataFrame.name} || colName: ${column.name} || colVersion: ${column.version} || rowIdx: ${tableCell?.rowIndex}`, () => {
|
|
151
|
+
return getChartData(tableCell);
|
|
152
|
+
}) : getChartData(tableCell);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Constructs {@link IFitChartData} from the table cell, taking into account
|
|
139
156
|
* chart and fit settings potentially defined on the dataframe and column level. */
|
|
140
|
-
|
|
157
|
+
function getChartData(tableCell: DG.Cell): IFitChartData {
|
|
141
158
|
// removing '|' from JSON (how did it get here?)
|
|
142
|
-
let cellValue =
|
|
159
|
+
let cellValue = tableCell.value as string;
|
|
143
160
|
if (cellValue.includes('|'))
|
|
144
161
|
cellValue = cellValue.replaceAll('|', '');
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
162
|
+
const column = tableCell.column;
|
|
163
|
+
const cellChartData: IFitChartData = column ? (column.type === DG.TYPE.STRING ?
|
|
164
|
+
(column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX ?
|
|
165
|
+
convertXMLToIFitChartData(cellValue) : JSON.parse(cellValue ?? '{}') ?? {}) :
|
|
166
|
+
createDefaultChartData()) : JSON.parse(cellValue ?? '{}') ?? {};
|
|
149
167
|
|
|
150
|
-
const columnChartOptions =
|
|
151
|
-
const dfChartOptions =
|
|
168
|
+
const columnChartOptions = tableCell.column ? getColumnChartOptions(tableCell.column) : {};
|
|
169
|
+
const dfChartOptions = tableCell.column ? getDataFrameChartOptions(tableCell.dataFrame) : {};
|
|
152
170
|
|
|
153
171
|
cellChartData.series ??= [];
|
|
154
172
|
cellChartData.chartOptions ??= columnChartOptions.chartOptions;
|
|
@@ -165,6 +183,28 @@ export function getChartData(gridCell: DG.GridCell): IFitChartData {
|
|
|
165
183
|
return cellChartData;
|
|
166
184
|
}
|
|
167
185
|
|
|
186
|
+
/** Returns existing, or fits curve for the specified grid cell and series. */
|
|
187
|
+
export function getOrCreateCachedFitCurve(series: IFitSeries, seriesIdx: number, fitFunc: FitFunction,
|
|
188
|
+
chartLogOptions: LogOptions, tableCell?: DG.Cell, useCache = true): FitCurve {
|
|
189
|
+
const dataPoints = getOrCreateCachedCurvesDataPoints(series, seriesIdx, chartLogOptions, false, tableCell, useCache);
|
|
190
|
+
// don't refit when just rerender - using LruCache with key `cellValue_colName_colVersion`
|
|
191
|
+
const column = tableCell?.column;
|
|
192
|
+
return (useCache && column && tableCell) ?
|
|
193
|
+
FitChartCellRenderer.fittedCurves.getOrCreate(`tableId: ${column.dataFrame.id} || tableName: ${column.dataFrame.name} || colName: ${column.name} || colVersion: ${column.version} || rowIdx: ${tableCell.rowIndex} || idx: ${seriesIdx}`, () => {
|
|
194
|
+
return fitSeries(series, fitFunc, dataPoints, chartLogOptions);
|
|
195
|
+
}) : fitSeries(series, fitFunc, dataPoints, chartLogOptions);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Returns existing, or maps new data points for the specified series. */
|
|
199
|
+
export function getOrCreateCachedCurvesDataPoints(series: IFitSeries, idx: number, logOptions?: LogOptions,
|
|
200
|
+
userParamsFlag?: boolean, tableCell?: DG.Cell, useCache = true): {x: number[], y: number[]} {
|
|
201
|
+
const column = tableCell?.column;
|
|
202
|
+
return (useCache && column && tableCell) ?
|
|
203
|
+
FitChartCellRenderer.curvesDataPoints.getOrCreate(`tableId: ${column.dataFrame.id} || tableName: ${column.dataFrame.name} || colName: ${column.name} || colVersion: ${column.version} || rowIdx: ${tableCell.rowIndex} || idx: ${idx} || userParamsFlag: ${userParamsFlag}`, () => {
|
|
204
|
+
return getDataPoints(series, logOptions, userParamsFlag);
|
|
205
|
+
}) : getDataPoints(series, logOptions, userParamsFlag);
|
|
206
|
+
}
|
|
207
|
+
|
|
168
208
|
/** Returns existing, or creates new dataframe default chart options. */
|
|
169
209
|
export function getDataFrameChartOptions(df: DG.DataFrame): IFitChartData {
|
|
170
210
|
return JSON.parse(df.tags[FitConstants.TAG_FIT] ??= JSON.stringify(createDefaultChartData()));
|
|
@@ -224,6 +264,57 @@ export function substituteZeroes(data: IFitChartData): void {
|
|
|
224
264
|
}
|
|
225
265
|
}
|
|
226
266
|
|
|
267
|
+
export function setOutlier(gridCell: DG.GridCell, p: IFitPoint, seriesIdx: number, pointIdx: number, _data?: IFitChartData) {
|
|
268
|
+
p.outlier = !p.outlier;
|
|
269
|
+
// temporarily works only for JSON structure
|
|
270
|
+
if (gridCell.cell.column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) !== FitConstants.TAG_FIT_CHART_FORMAT_3DX) {
|
|
271
|
+
const gridCellValue = JSON.parse(gridCell.cell.value) as IFitChartData;
|
|
272
|
+
gridCellValue.series![seriesIdx].points[pointIdx].outlier = p.outlier;
|
|
273
|
+
gridCell.cell.column.set(gridCell.cell.rowIndex, JSON.stringify(gridCellValue), true);
|
|
274
|
+
grok.events.fireCustomEvent('fit-cell-outlier-toggle', {
|
|
275
|
+
gridCell: gridCell,
|
|
276
|
+
series: gridCellValue.series![seriesIdx],
|
|
277
|
+
seriesIdx: seriesIdx,
|
|
278
|
+
pointIdx: pointIdx,
|
|
279
|
+
});
|
|
280
|
+
// const g = gridCell.grid.canvas.getContext('2d')!;
|
|
281
|
+
// gridCell.render({context: g, bounds: gridCell.bounds});
|
|
282
|
+
}
|
|
283
|
+
const columns = gridCell.grid.dataFrame.columns.byTags({'.sourceColumn': gridCell.cell.column.name});
|
|
284
|
+
if (columns) {
|
|
285
|
+
_data ??= gridCell.cell.column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX ?
|
|
286
|
+
convertXMLToIFitChartData(gridCell.cell.value) : getOrCreateParsedChartData(gridCell.cell);
|
|
287
|
+
for (const column of columns) {
|
|
288
|
+
const chartLogOptions: LogOptions = {logX: _data.chartOptions?.logX, logY: _data.chartOptions?.logY};
|
|
289
|
+
const stats = column.tags['.seriesAggregation'] !== null ?
|
|
290
|
+
getChartDataAggrStats(_data, column.tags['.seriesAggregation'], gridCell.cell) :
|
|
291
|
+
column.tags['.seriesNumber'] === seriesIdx.toString() ? calculateSeriesStats(_data.series![seriesIdx], seriesIdx, chartLogOptions, gridCell.cell) : null;
|
|
292
|
+
if (stats === null)
|
|
293
|
+
continue;
|
|
294
|
+
column.set(gridCell.cell.rowIndex, stats[column.tags['.statistics'] as unknown as keyof FitStatistics]);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function inspectCurve(gridCell: DG.GridCell, size?: Partial<DG.Size>, rerenderOnChange: boolean = false): void {
|
|
300
|
+
if (!gridCell.cell.value)
|
|
301
|
+
return;
|
|
302
|
+
|
|
303
|
+
const gridCellWidget = DG.GridCellWidget.fromGridCell(gridCell);
|
|
304
|
+
gridCellWidget.root.style.removeProperty('aspectRatio');
|
|
305
|
+
gridCellWidget.root.style.height = '100%';
|
|
306
|
+
gridCellWidget.canvas.style.removeProperty('height');
|
|
307
|
+
gridCellWidget.canvas.style.removeProperty('width');
|
|
308
|
+
gridCellWidget.canvas.style.left = '18px';
|
|
309
|
+
gridCellWidget.canvas.style.top = '18px';
|
|
310
|
+
|
|
311
|
+
const dlg = ui.dialog({title: 'Edit chart'})
|
|
312
|
+
.add(gridCellWidget.root)
|
|
313
|
+
.show({resizable: true, width: size?.width ?? 500, height: size?.height ?? 430});
|
|
314
|
+
if (rerenderOnChange)
|
|
315
|
+
dlg.sub(merge(gridCell.grid.dataFrame.onDataChanged, gridCell.grid.dataFrame.onMetadataChanged).subscribe(() => gridCellWidget.render()));
|
|
316
|
+
}
|
|
317
|
+
|
|
227
318
|
@grok.decorators.cellRenderer({
|
|
228
319
|
name: 'Fit',
|
|
229
320
|
cellType: 'fit',
|
|
@@ -234,16 +325,29 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
234
325
|
|
|
235
326
|
get cellType() { return FitConstants.FIT_CELL_TYPE; }
|
|
236
327
|
|
|
237
|
-
|
|
328
|
+
get defaultHeight() { return FitConstants.CELL_DEFAULT_HEIGHT; }
|
|
329
|
+
|
|
330
|
+
get defaultWidth() { return FitConstants.CELL_DEFAULT_WIDTH; }
|
|
331
|
+
|
|
332
|
+
getDefaultSize(_gridColumn: DG.GridColumn): {width?: number | null, height?: number | null} {
|
|
238
333
|
return {width: FitConstants.CELL_DEFAULT_WIDTH, height: FitConstants.CELL_DEFAULT_HEIGHT};
|
|
239
334
|
}
|
|
240
335
|
|
|
336
|
+
static fittedCurves: DG.LruCache<string, FitCurve> = new DG.LruCache<string, FitCurve>(1000);
|
|
337
|
+
static parsedCurves: DG.LruCache<string, IFitChartData> = new DG.LruCache<string, IFitChartData>(1000);
|
|
338
|
+
static curvesDataPoints: DG.LruCache<string, {x: number[], y: number[]}> = new DG.LruCache<string, {x: number[], y: number[]}>(2000);
|
|
339
|
+
|
|
340
|
+
static inflateScreenBounds(rect: DG.Rect): DG.Rect {
|
|
341
|
+
return rect.inflate(rect.width < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH ? FitConstants.MIN_INFLATE_SIZE : FitConstants.INFLATE_SIZE,
|
|
342
|
+
rect.height < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT ? FitConstants.MIN_INFLATE_SIZE : FitConstants.INFLATE_SIZE);
|
|
343
|
+
}
|
|
344
|
+
|
|
241
345
|
onClick(gridCell: DG.GridCell, e: MouseEvent): void {
|
|
242
346
|
if (!gridCell.cell.value)
|
|
243
347
|
return;
|
|
244
348
|
|
|
245
349
|
const data = gridCell.cell.column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX ?
|
|
246
|
-
convertXMLToIFitChartData(gridCell.cell.value) :
|
|
350
|
+
convertXMLToIFitChartData(gridCell.cell.value) : getOrCreateParsedChartData(gridCell.cell, false);
|
|
247
351
|
|
|
248
352
|
for (const [message, condition] of Object.entries(FitConstants.CONDITION_MAP)) {
|
|
249
353
|
if (condition(data.series)) {
|
|
@@ -254,7 +358,7 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
254
358
|
|
|
255
359
|
grok.shell.o = gridCell;
|
|
256
360
|
|
|
257
|
-
const screenBounds = gridCell.bounds
|
|
361
|
+
const screenBounds = FitChartCellRenderer.inflateScreenBounds(gridCell.bounds);
|
|
258
362
|
const dataBox = layoutChart(screenBounds, this.areAxesLabelsShown(screenBounds, data),
|
|
259
363
|
this.isTitleShown(screenBounds, data))[0];
|
|
260
364
|
const dataBounds = getChartBounds(data);
|
|
@@ -267,56 +371,15 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
267
371
|
for (let j = 0; j < data.series![i].points.length!; j++) {
|
|
268
372
|
const p = data.series![i].points[j];
|
|
269
373
|
if (this.hitTest(e, p, viewport)) {
|
|
270
|
-
p
|
|
271
|
-
const columns = gridCell.grid.dataFrame.columns.byTags({'.sourceColumn': gridCell.cell.column.name});
|
|
272
|
-
if (columns) {
|
|
273
|
-
for (const column of columns) {
|
|
274
|
-
const chartLogOptions: LogOptions = {logX: data.chartOptions?.logX, logY: data.chartOptions?.logY};
|
|
275
|
-
const stats = column.tags['.seriesAggregation'] !== null ?
|
|
276
|
-
getChartDataAggrStats(data, column.tags['.seriesAggregation']) :
|
|
277
|
-
column.tags['.seriesNumber'] === i ? calculateSeriesStats(data.series![i], chartLogOptions) : null;
|
|
278
|
-
if (stats === null)
|
|
279
|
-
continue;
|
|
280
|
-
column.set(gridCell.cell.rowIndex, stats[column.tags['.statistics'] as keyof FitStatistics]);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
// temporarily works only for JSON structure
|
|
285
|
-
if (gridCell.cell.column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) !== FitConstants.TAG_FIT_CHART_FORMAT_3DX) {
|
|
286
|
-
const gridCellValue = JSON.parse(gridCell.cell.value) as IFitChartData;
|
|
287
|
-
gridCellValue.series![i].points[j].outlier = p.outlier;
|
|
288
|
-
gridCell.cell.value = JSON.stringify(gridCellValue);
|
|
289
|
-
}
|
|
374
|
+
setOutlier(gridCell, p, i, j, data);
|
|
290
375
|
return;
|
|
291
376
|
}
|
|
292
377
|
}
|
|
293
378
|
}
|
|
294
379
|
}
|
|
295
380
|
|
|
296
|
-
onDoubleClick(gridCell: DG.GridCell,
|
|
297
|
-
|
|
298
|
-
return;
|
|
299
|
-
|
|
300
|
-
const cellRenderViewer = CellRenderViewer.fromGridCell(gridCell);
|
|
301
|
-
const dlg = ui.dialog({title: 'Edit chart'})
|
|
302
|
-
.add(cellRenderViewer.root)
|
|
303
|
-
.show({resizable: true});
|
|
304
|
-
|
|
305
|
-
// canvas is created as (300, 150), so we change its size to the dialog contents box size
|
|
306
|
-
const dlgContentsBox = dlg.root.getElementsByClassName('d4-dialog-contents dlg-edit-chart')[0].firstChild as HTMLElement;
|
|
307
|
-
cellRenderViewer.canvas.width = dlgContentsBox.clientWidth;
|
|
308
|
-
cellRenderViewer.canvas.height = dlgContentsBox.clientHeight;
|
|
309
|
-
cellRenderViewer.render();
|
|
310
|
-
|
|
311
|
-
// contents ui-box isn't resizable by default
|
|
312
|
-
dlgContentsBox.style.width = '100%';
|
|
313
|
-
dlgContentsBox.style.height = '100%';
|
|
314
|
-
|
|
315
|
-
ui.tools.handleResize(dlgContentsBox, (w: number, h: number) => {
|
|
316
|
-
cellRenderViewer.canvas.width = w;
|
|
317
|
-
cellRenderViewer.canvas.height = h;
|
|
318
|
-
cellRenderViewer.render();
|
|
319
|
-
});
|
|
381
|
+
onDoubleClick(gridCell: DG.GridCell, _e: MouseEvent): void {
|
|
382
|
+
inspectCurve(gridCell);
|
|
320
383
|
}
|
|
321
384
|
|
|
322
385
|
areAxesShown(screenBounds: DG.Rect): boolean {
|
|
@@ -341,12 +404,14 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
341
404
|
return screenBounds.width >= FitConstants.MIN_DROPLINES_VISIBILITY_PX_WIDTH && screenBounds.height >= FitConstants.MIN_DROPLINES_VISIBILITY_PX_HEIGHT;
|
|
342
405
|
}
|
|
343
406
|
|
|
344
|
-
renderCurves(g: CanvasRenderingContext2D, screenBounds: DG.Rect, data: IFitChartData): void {
|
|
407
|
+
renderCurves(g: CanvasRenderingContext2D, screenBounds: DG.Rect, data: IFitChartData, gridCell?: DG.GridCell): void {
|
|
345
408
|
g.save();
|
|
346
409
|
g.beginPath();
|
|
347
410
|
g.rect(screenBounds.x, screenBounds.y, screenBounds.width, screenBounds.height);
|
|
348
411
|
g.clip();
|
|
349
412
|
|
|
413
|
+
const isRenderedOnGrid = gridCell?.grid && gridCell?.grid.dart && g.canvas === gridCell?.grid?.canvas; // only use cache for grid, because there is no guarantee that rowIdx will be correct for other places
|
|
414
|
+
const tableCell = gridCell?.cell;
|
|
350
415
|
if (data.chartOptions?.allowXZeroes && data.chartOptions?.logX &&
|
|
351
416
|
data.series?.some((series) => series.points.some((p) => p.x === 0)))
|
|
352
417
|
substituteZeroes(data);
|
|
@@ -371,14 +436,9 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
371
436
|
|
|
372
437
|
for (let i = 0; i < data.series?.length!; i++) {
|
|
373
438
|
const series = data.series![i];
|
|
439
|
+
const containsParams = series.parameters && series.parameters.length > 0;
|
|
374
440
|
if (series.points.some((point) => point.x === undefined || point.y === undefined))
|
|
375
441
|
continue;
|
|
376
|
-
if (screenBounds.width < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH ||
|
|
377
|
-
screenBounds.height < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT) {
|
|
378
|
-
series.showPoints = '';
|
|
379
|
-
if (data.chartOptions)
|
|
380
|
-
data.chartOptions.showStatistics = [];
|
|
381
|
-
}
|
|
382
442
|
series.points.sort((a, b) => a.x - b.x);
|
|
383
443
|
|
|
384
444
|
let userParamsFlag = true;
|
|
@@ -387,33 +447,34 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
387
447
|
if (!(series.connectDots && !series.showFitLine)) {
|
|
388
448
|
if (series.parameters) {
|
|
389
449
|
if (data.chartOptions?.logX) {
|
|
390
|
-
if (series.parameters[2] > 0)
|
|
450
|
+
if (series.parameters[2] > 0) // check if sigmoid, then log
|
|
391
451
|
series.parameters[2] = Math.log10(series.parameters[2]);
|
|
392
452
|
}
|
|
393
453
|
curve = getCurve(series, fitFunc);
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
// TODO: Stop refitting when we just rerender the same curve - use LruMap
|
|
397
|
-
const fitResult = fitSeries(series, fitFunc, chartLogOptions);
|
|
454
|
+
} else {
|
|
455
|
+
const fitResult = getOrCreateCachedFitCurve(series, i, fitFunc, chartLogOptions, tableCell, isRenderedOnGrid);
|
|
398
456
|
curve = fitResult.fittedCurve;
|
|
399
|
-
const params = [...fitResult.parameters]
|
|
457
|
+
const params = [...fitResult.parameters];
|
|
400
458
|
series.parameters = params;
|
|
401
459
|
userParamsFlag = false;
|
|
402
460
|
}
|
|
403
461
|
}
|
|
404
462
|
|
|
405
|
-
assignSeriesColors(series, i);
|
|
406
|
-
renderConnectDots(g, series, {viewport, ratio});
|
|
407
|
-
renderPoints(g, series, {viewport, ratio});
|
|
408
463
|
renderFitLine(g, series, {viewport, ratio, logOptions: chartLogOptions, showAxes: this.areAxesShown(screenBounds),
|
|
409
|
-
showAxesLabels: this.areAxesLabelsShown(screenBounds, data), screenBounds, curveFunc: curve
|
|
464
|
+
showAxesLabels: this.areAxesLabelsShown(screenBounds, data), screenBounds, curveFunc: curve!, seriesIdx: i});
|
|
465
|
+
renderConnectDots(g, series, {viewport, ratio, seriesIdx: i});
|
|
466
|
+
renderPoints(g, series, {viewport, ratio, screenBounds, seriesIdx: i});
|
|
410
467
|
renderConfidenceIntervals(g, series, {viewport, logOptions: chartLogOptions, showAxes: this.areAxesShown(screenBounds),
|
|
411
|
-
showAxesLabels: this.areAxesLabelsShown(screenBounds, data), screenBounds, fitFunc, userParamsFlag
|
|
412
|
-
|
|
468
|
+
showAxesLabels: this.areAxesLabelsShown(screenBounds, data), screenBounds, fitFunc, userParamsFlag,
|
|
469
|
+
dataPoints: getOrCreateCachedCurvesDataPoints(series, i, chartLogOptions, userParamsFlag, tableCell, isRenderedOnGrid)});
|
|
470
|
+
if (series.parameters) {
|
|
413
471
|
renderDroplines(g, series, {viewport, ratio, showDroplines: this.areDroplinesShown(screenBounds),
|
|
414
472
|
xValue: series.parameters![2], dataBounds, curveFunc: curve!, logOptions: chartLogOptions});
|
|
473
|
+
}
|
|
415
474
|
renderStatistics(g, series, {statistics: data.chartOptions?.showStatistics, fitFunc,
|
|
416
|
-
logOptions: chartLogOptions, dataBox});
|
|
475
|
+
logOptions: chartLogOptions, dataBox, screenBounds, seriesIdx: i});
|
|
476
|
+
if (!containsParams)
|
|
477
|
+
delete series.parameters;
|
|
417
478
|
}
|
|
418
479
|
|
|
419
480
|
renderTitle(g, {showTitle: this.isTitleShown(screenBounds, data), title: data.chartOptions?.title, dataBox, screenBounds});
|
|
@@ -428,29 +489,32 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
428
489
|
// TODO: Curves: make less margins in the right, also in the left
|
|
429
490
|
// TODO: Curves: add a warning or error in top right if some mistakes in data
|
|
430
491
|
render(g: CanvasRenderingContext2D,
|
|
431
|
-
|
|
432
|
-
|
|
492
|
+
x: number, y: number, w: number, h: number,
|
|
493
|
+
gridCell: DG.GridCell, _cellStyle: DG.GridCellStyle): void {
|
|
433
494
|
if (!gridCell.cell.value)
|
|
434
495
|
return;
|
|
435
496
|
if (w < FitConstants.MIN_CELL_RENDERER_PX_WIDTH || h < FitConstants.MIN_CELL_RENDERER_PX_HEIGHT)
|
|
436
497
|
return;
|
|
437
498
|
|
|
499
|
+
const isRenderedOnGrid = gridCell?.grid && gridCell?.grid.dart && g.canvas === gridCell?.grid?.canvas; // only use cache for grid, because there is no guarantee that rowIdx will be correct for other places
|
|
438
500
|
const data = gridCell.cell.column?.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX ?
|
|
439
|
-
convertXMLToIFitChartData(gridCell.cell.value) :
|
|
440
|
-
const screenBounds = new DG.Rect(x, y, w, h)
|
|
501
|
+
convertXMLToIFitChartData(gridCell.cell.value) : getOrCreateParsedChartData(gridCell.cell, isRenderedOnGrid);
|
|
502
|
+
const screenBounds = FitChartCellRenderer.inflateScreenBounds(new DG.Rect(x, y, w, h));
|
|
441
503
|
|
|
442
|
-
for (const [
|
|
504
|
+
for (const [_message, condition] of Object.entries(FitConstants.CONDITION_MAP)) {
|
|
443
505
|
if (condition(data.series)) {
|
|
444
506
|
showIncorrectFitCell(g, screenBounds);
|
|
445
507
|
return;
|
|
446
508
|
}
|
|
447
509
|
}
|
|
448
510
|
|
|
449
|
-
|
|
511
|
+
if (gridCell.cell.column?.name)
|
|
512
|
+
data.series?.forEach((series) => series.columnName = gridCell.cell.column.name);
|
|
450
513
|
if (data.chartOptions?.mergeSeries)
|
|
451
514
|
data.series = [mergeSeries(data.series!)!];
|
|
452
515
|
|
|
453
|
-
|
|
516
|
+
g.clearRect(screenBounds.x, screenBounds.y, screenBounds.width, screenBounds.height);
|
|
517
|
+
this.renderCurves(g, screenBounds, data, gridCell);
|
|
454
518
|
}
|
|
455
519
|
|
|
456
520
|
hitTest(e: MouseEvent, point: IFitPoint, viewport: Viewport): boolean {
|
|
@@ -463,10 +527,10 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
463
527
|
}
|
|
464
528
|
|
|
465
529
|
onMouseMove(gridCell: DG.GridCell, e: MouseEvent): void {
|
|
466
|
-
if (!gridCell
|
|
530
|
+
if (!gridCell?.cell?.value)
|
|
467
531
|
return;
|
|
468
532
|
|
|
469
|
-
const screenBounds = gridCell.bounds
|
|
533
|
+
const screenBounds = FitChartCellRenderer.inflateScreenBounds(gridCell.bounds);
|
|
470
534
|
if (screenBounds.width < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH ||
|
|
471
535
|
screenBounds.height < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT) {
|
|
472
536
|
const canvas = ui.canvas(300, 200);
|
|
@@ -476,9 +540,9 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
476
540
|
}
|
|
477
541
|
|
|
478
542
|
// TODO: add caching
|
|
479
|
-
const data = gridCell.cell.column
|
|
480
|
-
|
|
481
|
-
|
|
543
|
+
const data = gridCell.cell.column?.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX ?
|
|
544
|
+
convertXMLToIFitChartData(gridCell.cell.value) :
|
|
545
|
+
getOrCreateParsedChartData(gridCell?.cell);
|
|
482
546
|
|
|
483
547
|
if (screenBounds.width >= FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH &&
|
|
484
548
|
screenBounds.height >= FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT) {
|
|
@@ -492,8 +556,8 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
492
556
|
for (let j = 0; j < data.series![i].points.length!; j++) {
|
|
493
557
|
const p = data.series![i].points[j];
|
|
494
558
|
if (this.hitTest(e, p, viewport)) {
|
|
495
|
-
ui.tooltip.show(ui.divV([ui.divText(
|
|
496
|
-
ui.divText(
|
|
559
|
+
ui.tooltip.show(ui.divV([ui.divText(`${data.chartOptions?.xAxisName ?? 'x'}: ${DG.format(p.x, !data.chartOptions?.logX ? '#0.000' : 'scientific')}`),
|
|
560
|
+
ui.divText(`${data.chartOptions?.yAxisName ?? 'y'}: ${DG.format(p.y, !data.chartOptions?.logY ? '#0.000' : 'scientific')}`)]), e.x + 16, e.y + 16);
|
|
497
561
|
if (!data.series![i].connectDots && data.series![i].clickToToggle && screenBounds.width >= FitConstants.MIN_AXES_CELL_PX_WIDTH &&
|
|
498
562
|
screenBounds.height >= FitConstants.MIN_AXES_CELL_PX_HEIGHT)
|
|
499
563
|
document.body.style.cursor = 'pointer';
|
|
@@ -507,7 +571,7 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
507
571
|
}
|
|
508
572
|
}
|
|
509
573
|
|
|
510
|
-
const
|
|
574
|
+
const _sample: IFitChartData = {
|
|
511
575
|
// chartOptions could be retrieved either from the column, or from the cell
|
|
512
576
|
'chartOptions': {
|
|
513
577
|
'minX': 0, 'minY': 0, 'maxX': 5, 'maxY': 10,
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
/* eslint-disable max-len */
|
|
1
2
|
import * as DG from 'datagrok-api/dg';
|
|
2
3
|
import * as ui from 'datagrok-api/ui';
|
|
3
4
|
import * as grok from 'datagrok-api/grok';
|
|
4
5
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {getChartData, mergeProperties} from './fit-renderer';
|
|
6
|
+
import {mergeChartOptions, mergeSeries} from './fit-renderer';
|
|
7
|
+
import {getOrCreateParsedChartData, mergeProperties} from './fit-renderer';
|
|
8
8
|
import {FitChartData, fitChartDataProperties, IFitChartData, IFitChartOptions} from '@datagrok-libraries/statistics/src/fit/fit-curve';
|
|
9
9
|
|
|
10
10
|
import {debounce} from 'rxjs/operators';
|
|
@@ -18,9 +18,12 @@ const ERROR_CLASS = 'd4-viewer-error';
|
|
|
18
18
|
name: 'MultiCurveViewer',
|
|
19
19
|
description: 'A viewer that superimposes multiple in-cell curves on one chart',
|
|
20
20
|
icon: 'icons/multi-curve-viewer.png',
|
|
21
|
+
trellisable: true
|
|
21
22
|
})
|
|
22
|
-
export class MultiCurveViewer extends
|
|
23
|
+
export class MultiCurveViewer extends DG.JsViewer {
|
|
23
24
|
[index: string]: any;
|
|
25
|
+
canvas: HTMLCanvasElement;
|
|
26
|
+
gridCellWidget: DG.GridCellWidget;
|
|
24
27
|
curvesColumnNames?: string[] = [];
|
|
25
28
|
showSelectedRowsCurves: boolean = false;
|
|
26
29
|
showCurrentRowCurve: boolean = true;
|
|
@@ -34,12 +37,34 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
|
|
|
34
37
|
mergeCellSeries?: boolean;
|
|
35
38
|
showColumnLabel?: boolean;
|
|
36
39
|
|
|
40
|
+
|
|
41
|
+
private isInTrellis(): boolean {
|
|
42
|
+
let curRoot = this.root;
|
|
43
|
+
let i = 0;
|
|
44
|
+
const maxDepth = 7;
|
|
45
|
+
while (curRoot && i < maxDepth) {
|
|
46
|
+
if (curRoot.classList.contains('d4-trellis-plot-cell'))
|
|
47
|
+
return true;
|
|
48
|
+
curRoot = curRoot.parentElement!;
|
|
49
|
+
i++;
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
37
54
|
constructor() {
|
|
38
|
-
super(
|
|
55
|
+
super();
|
|
56
|
+
this.gridCellWidget = DG.GridCellWidget.fromGridCell(this.createGridCell(''));
|
|
57
|
+
this.canvas = this.gridCellWidget.canvas;
|
|
58
|
+
this.root.append(this.gridCellWidget.canvas);
|
|
59
|
+
ui.tools.handleResize(this.root, (w: number, h: number) => {
|
|
60
|
+
this.canvas.width = w;
|
|
61
|
+
this.canvas.height = h;
|
|
62
|
+
this.render();
|
|
63
|
+
});
|
|
39
64
|
|
|
40
65
|
this.curvesColumnNames = this.addProperty('curvesColumnNames', DG.TYPE.COLUMN_LIST, [], {semType: FitConstants.FIT_SEM_TYPE});
|
|
41
66
|
|
|
42
|
-
this.showSelectedRowsCurves = this.bool('showSelectedRowsCurves', false, {
|
|
67
|
+
this.showSelectedRowsCurves = this.bool('showSelectedRowsCurves', false, {description: 'Adds curves from the selected rows'});
|
|
43
68
|
this.showCurrentRowCurve = this.bool('showCurrentRowCurve', true);
|
|
44
69
|
this.showMouseOverRowCurve = this.bool('showMouseOverRowCurve', true);
|
|
45
70
|
this.mergeColumnSeries = this.bool('mergeColumnSeries', false);
|
|
@@ -49,12 +74,18 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
|
|
|
49
74
|
}
|
|
50
75
|
|
|
51
76
|
static fromChartData(chartData: IFitChartData): MultiCurveViewer {
|
|
52
|
-
|
|
77
|
+
const viewer = new MultiCurveViewer();
|
|
53
78
|
viewer.data = chartData;
|
|
54
79
|
viewer.render();
|
|
55
80
|
return viewer;
|
|
56
81
|
}
|
|
57
82
|
|
|
83
|
+
createGridCell(value: string): DG.GridCell {
|
|
84
|
+
const gridCell = DG.GridCell.fromValue(value);
|
|
85
|
+
gridCell.cellType = FitConstants.FIT_CELL_TYPE;
|
|
86
|
+
return gridCell;
|
|
87
|
+
}
|
|
88
|
+
|
|
58
89
|
applyViewerProperties(): void {
|
|
59
90
|
mergeProperties(fitChartDataProperties, this, this.data.chartOptions);
|
|
60
91
|
}
|
|
@@ -80,27 +111,29 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
|
|
|
80
111
|
return;
|
|
81
112
|
this.rows.length = 0;
|
|
82
113
|
let selectionStart = -1;
|
|
83
|
-
if (this.
|
|
84
|
-
this.
|
|
85
|
-
|
|
86
|
-
this.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
114
|
+
if (this.isInTrellis()) { this.rows.push(...this.dataFrame.filter.getSelectedIndexes()); } else {
|
|
115
|
+
if (this.showCurrentRowCurve && this.dataFrame.currentRowIdx !== -1)
|
|
116
|
+
this.rows.push(this.dataFrame.currentRowIdx);
|
|
117
|
+
if (this.showMouseOverRowCurve && this.dataFrame.mouseOverRowIdx !== -1)
|
|
118
|
+
this.rows.push(this.dataFrame.mouseOverRowIdx);
|
|
119
|
+
if (this.showSelectedRowsCurves) {
|
|
120
|
+
selectionStart = this.rows.length;
|
|
121
|
+
this.rows.push(...this.dataFrame.selection.getSelectedIndexes());
|
|
122
|
+
}
|
|
90
123
|
}
|
|
91
124
|
|
|
92
125
|
this.data = new FitChartData();
|
|
93
|
-
const
|
|
126
|
+
//const _grid = this.isInTrellis() ? grok.shell.tableView(this.dataFrame.name).grid : this.tableView?.grid!;
|
|
94
127
|
const mergeCellSeries = this.props.get('mergeCellSeries') as unknown as boolean;
|
|
95
128
|
const chartOptions: IFitChartOptions[] = [];
|
|
96
129
|
for (const colName of this.curvesColumnNames!) {
|
|
97
130
|
const series = [];
|
|
98
131
|
for (const i of new Set(this.rows)) {
|
|
99
|
-
const
|
|
100
|
-
if (
|
|
132
|
+
const tableCell = this.dataFrame.cell(i, colName);
|
|
133
|
+
if (!tableCell || !tableCell.value)
|
|
101
134
|
continue;
|
|
102
|
-
const cellCurves =
|
|
103
|
-
cellCurves.series?.forEach((series) => series.columnName =
|
|
135
|
+
const cellCurves = getOrCreateParsedChartData(tableCell);
|
|
136
|
+
cellCurves.series?.forEach((series) => series.columnName = tableCell.column.name);
|
|
104
137
|
const currentChartOptions = cellCurves.chartOptions;
|
|
105
138
|
if (currentChartOptions !== undefined && currentChartOptions !== null)
|
|
106
139
|
chartOptions[chartOptions.length] = currentChartOptions;
|
|
@@ -115,7 +148,7 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
|
|
|
115
148
|
if (this.mergeColumnSeries)
|
|
116
149
|
this.data.series?.push(mergeSeries(series)!);
|
|
117
150
|
else
|
|
118
|
-
this.data.series?.push(...series);
|
|
151
|
+
this.data.series?.push(...JSON.parse(JSON.stringify(series)));
|
|
119
152
|
}
|
|
120
153
|
this.data.chartOptions = mergeChartOptions(chartOptions);
|
|
121
154
|
this.mergeViewerChartOptions();
|
|
@@ -153,12 +186,16 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
|
|
|
153
186
|
if (fitCol !== null)
|
|
154
187
|
this.curvesColumnNames = [fitCol.name];
|
|
155
188
|
|
|
156
|
-
merge(this.dataFrame.onCurrentCellChanged, grid.onCellMouseEnter, this.dataFrame.onSelectionChanged)
|
|
157
|
-
.pipe(debounce(_ => interval(50)))
|
|
158
|
-
.subscribe(_ => {
|
|
159
|
-
this.
|
|
160
|
-
|
|
189
|
+
merge(...[this.dataFrame.onCurrentCellChanged, ...(grid ? [grid.onCellMouseEnter] : []), this.dataFrame.onSelectionChanged])
|
|
190
|
+
.pipe(debounce((_) => interval(50)))
|
|
191
|
+
.subscribe((_) => {
|
|
192
|
+
if (this.dataFrame) {
|
|
193
|
+
this.createChartData();
|
|
194
|
+
this.render();
|
|
195
|
+
}
|
|
161
196
|
});
|
|
197
|
+
if (this.isInTrellis())
|
|
198
|
+
this.createChartData();
|
|
162
199
|
}
|
|
163
200
|
|
|
164
201
|
_showErrorMessage(msg: string) {
|
|
@@ -172,14 +209,18 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
|
|
|
172
209
|
}
|
|
173
210
|
|
|
174
211
|
render(): void {
|
|
175
|
-
const g = this.canvas.getContext('2d')
|
|
212
|
+
const g = this.canvas.getContext('2d')!;
|
|
176
213
|
g.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
214
|
+
this._removeErrorMessage();
|
|
177
215
|
|
|
178
216
|
if (this.curvesColumnNames?.length === 0) {
|
|
179
217
|
this._showErrorMessage('The MultiCurveViewer viewer requires a minimum of 1 curves column.');
|
|
180
218
|
return;
|
|
181
219
|
}
|
|
182
|
-
this.
|
|
183
|
-
|
|
220
|
+
if (this.data?.series?.length === 0) {
|
|
221
|
+
this._showErrorMessage('No data to show.');
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
this.gridCellWidget.gridCell = this.createGridCell(JSON.stringify(this.data));
|
|
184
225
|
}
|
|
185
226
|
}
|