@datagrok/curves 1.3.0 → 1.4.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/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@datagrok/curves",
3
3
  "friendlyName": "Curves",
4
- "version": "1.3.0",
4
+ "version": "1.4.0",
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.2.13",
11
+ "@datagrok-libraries/statistics": "^1.3.0",
12
12
  "@datagrok-libraries/utils": "^4.1.45",
13
13
  "@types/wu": "^2.1.42",
14
14
  "cash-dom": "^8.0.0",
@@ -0,0 +1,72 @@
1
+ import {IFitSeries} from '@datagrok-libraries/statistics/src/fit/fit-curve';
2
+
3
+ export class FitConstants {
4
+ static TAG_FIT_CHART_FORMAT = '.fitChartFormat';
5
+ static TAG_FIT_CHART_FORMAT_3DX = '3dx';
6
+ static FIT_SEM_TYPE = 'fit';
7
+ static FIT_CELL_TYPE = 'fit';
8
+ static TAG_FIT = '.fit';
9
+
10
+ static CELL_DEFAULT_WIDTH = 230;
11
+ static CELL_DEFAULT_HEIGHT = 160;
12
+ static MIN_CELL_RENDERER_PX_WIDTH = 20;
13
+ static MIN_CELL_RENDERER_PX_HEIGHT = 10;
14
+ static MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH = 70;
15
+ static MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT = 45;
16
+ static MIN_AXES_CELL_PX_WIDTH = 100;
17
+ static MIN_AXES_CELL_PX_HEIGHT = 55;
18
+ static MIN_TITLE_PX_WIDTH = 275;
19
+ static MIN_TITLE_PX_HEIGHT = 225;
20
+ static MIN_X_AXIS_NAME_VISIBILITY_PX_WIDTH = 180;
21
+ static MIN_Y_AXIS_NAME_VISIBILITY_PX_HEIGHT = 140;
22
+ static MIN_LEGEND_PX_WIDTH = 325;
23
+ static MIN_LEGEND_PX_HEIGHT = 275;
24
+ static MIN_DROPLINES_VISIBILITY_PX_WIDTH = 120;
25
+ static MIN_DROPLINES_VISIBILITY_PX_HEIGHT = 110;
26
+ static AXES_LEFT_PX_MARGIN = 38;
27
+ static AXES_LEFT_PX_MARGIN_WITH_AXES_LABELS = 45;
28
+ static AXES_TOP_PX_MARGIN = 5;
29
+ static AXES_TOP_PX_MARGIN_WITH_TITLE = 25;
30
+ static AXES_RIGHT_PX_MARGIN = 18;
31
+ static AXES_BOTTOM_PX_MARGIN = 15;
32
+ static AXES_BOTTOM_PX_MARGIN_WITH_AXES_LABELS = 30;
33
+ static X_AXIS_LABEL_BOTTOM_PX_MARGIN = 4;
34
+ static LEGEND_TOP_PX_MARGIN = 10;
35
+
36
+ static OUTLIER_PX_SIZE = 12;
37
+ static POINT_PX_SIZE = 4;
38
+ static OUTLIER_HITBOX_RADIUS = 2;
39
+ static CANDLESTICK_BORDER_PX_SIZE = 4;
40
+ static CANDLESTICK_MEDIAN_PX_SIZE = 3.5;
41
+ static CANDLESTICK_OUTLIER_PX_SIZE = 6;
42
+ static INFLATE_SIZE = -12 / 2;
43
+ static LEGEND_RECORD_PX_HEIGHT = 18;
44
+ static LEGEND_RECORD_LINE_PX_WIDTH = 20;
45
+ static LEGEND_RECORD_LINE_RIGHT_PX_MARGIN = 5;
46
+ static LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN = 4;
47
+
48
+ static LINE_STYLES: {[key: string]: number[]} = {
49
+ 'solid': [],
50
+ 'dotted': [1, 1],
51
+ 'dashed': [5, 5],
52
+ 'dashdotted': [5, 5, 2, 5],
53
+ };
54
+
55
+ static CURVE_CONFIDENCE_INTERVAL_BOUNDS = {
56
+ TOP: 'top',
57
+ BOTTOM: 'bottom',
58
+ };
59
+
60
+ static CONFIDENCE_INTERVAL_STROKE_COLOR = 'rgba(255,191,63,0.4)';
61
+ static CONFIDENCE_INTERVAL_FILL_COLOR = 'rgba(255,238,204,0.3)';
62
+
63
+ static CONDITION_MAP: {[key: string]: ((series?: IFitSeries[]) => boolean)} = {
64
+ 'No series to show': (series) => series === undefined || series?.length === 0,
65
+ 'There were series with no points': (series) =>
66
+ series!.some((series) => series.points?.length === 0),
67
+ 'There were series with all x zeroes': (series) =>
68
+ series!.some((series) => series.points?.every((point) => point.x === 0)),
69
+ 'There were series with all y zeroes': (series) =>
70
+ series!.some((series) => series.points?.every((point) => point.y === 0)),
71
+ };
72
+ }
@@ -4,9 +4,10 @@ import * as DG from 'datagrok-api/dg';
4
4
  import * as grok from 'datagrok-api/grok';
5
5
 
6
6
  import {_package} from '../package';
7
- import {IFitChartData, FIT_SEM_TYPE, sigmoid} from '@datagrok-libraries/statistics/src/fit/fit-curve';
7
+ import {IFitChartData, sigmoid} from '@datagrok-libraries/statistics/src/fit/fit-curve';
8
8
 
9
9
  import wu from 'wu';
10
+ import {FitConstants} from "./const";
10
11
 
11
12
  /** Returns random number from the interval */
12
13
  function rnd(min: number, max: number): number {
@@ -26,7 +27,9 @@ export function createSigmoidPoints(length: number, step: number, pointsPerX: nu
26
27
  for (let num = start, i = 0; num <= end; num += step, i++) {
27
28
  for (let j = 0; j < pointsPerX; j++) {
28
29
  x[i * pointsPerX + j] = num - start + 0.1;
29
- y[i * pointsPerX + j] = sigmoid(params, num);
30
+ const floatParams = new Float32Array(4);
31
+ floatParams.set(params);
32
+ y[i * pointsPerX + j] = sigmoid(floatParams, num);
30
33
  }
31
34
  }
32
35
 
@@ -61,7 +64,7 @@ export function createDemoDataFrame(rowCount: number, chartsCount: number, chart
61
64
  const pointsPerX = colIdx === 3 ? 5 : 1;
62
65
 
63
66
  const jsonColumn = df.columns.addNewString(`json chart ${colIdx}`); // charts as json
64
- jsonColumn.semType = FIT_SEM_TYPE;
67
+ jsonColumn.semType = FitConstants.FIT_SEM_TYPE;
65
68
  let charts = colIdx % 2 === 0 ? chartsPerCell : 1;
66
69
 
67
70
  for (let i = 0; i < rowCount; i++) {
@@ -3,17 +3,23 @@ import * as ui from 'datagrok-api/ui';
3
3
 
4
4
  import {
5
5
  fitSeries,
6
- getColumnChartOptions,
7
6
  getSeriesStatistics,
8
7
  getSeriesFitFunction,
9
- getDataFrameChartOptions,
10
8
  LogOptions,
11
9
  getChartBounds,
12
10
  } from '@datagrok-libraries/statistics/src/fit/fit-data';
13
- import {statisticsProperties, fitSeriesProperties, fitChartDataProperties, FIT_CELL_TYPE, TAG_FIT, IFitChartData, IFitSeries, IFitChartOptions, FIT_SEM_TYPE, IFitSeriesOptions, FitStatistics} from '@datagrok-libraries/statistics/src/fit/fit-curve';
14
- import {TAG_FIT_CHART_FORMAT, TAG_FIT_CHART_FORMAT_3DX, getChartData, isColorValid, mergeProperties, substituteZeroes} from './fit-renderer';
11
+ import {statisticsProperties, fitSeriesProperties, fitChartDataProperties, IFitChartData, IFitSeries, IFitChartOptions, IFitSeriesOptions, FitStatistics} from '@datagrok-libraries/statistics/src/fit/fit-curve';
12
+ import {
13
+ getChartData,
14
+ getColumnChartOptions,
15
+ getDataFrameChartOptions,
16
+ isColorValid,
17
+ mergeProperties,
18
+ substituteZeroes
19
+ } from './fit-renderer';
15
20
  import {CellRenderViewer} from './cell-render-viewer';
16
21
  import {convertXMLToIFitChartData} from './fit-parser';
22
+ import {FitConstants} from './const';
17
23
 
18
24
 
19
25
  const CHART_OPTIONS = 'chartOptions';
@@ -50,8 +56,10 @@ export function calculateSeriesStats(series: IFitSeries, chartLogOptions: LogOpt
50
56
  if (series.parameters[2] > 0)
51
57
  series.parameters[2] = Math.log10(series.parameters[2]);
52
58
  }
53
- else
54
- series.parameters = fitSeries(series, fitFunction, chartLogOptions).parameters;
59
+ else {
60
+ const params = fitSeries(series, fitFunction, chartLogOptions).parameters;
61
+ series.parameters = [...params];
62
+ }
55
63
 
56
64
  const seriesStatistics = getSeriesStatistics(series, fitFunction, chartLogOptions);
57
65
  return seriesStatistics;
@@ -67,22 +75,29 @@ export function getChartDataAggrStats(chartData: IFitChartData, aggrType: string
67
75
  const seriesStats = calculateSeriesStats(chartData.series![i], chartLogOptions);
68
76
  rSquaredValues[j] = seriesStats.rSquared!;
69
77
  aucValues[j] = seriesStats.auc!;
70
- interceptXValues[j] = seriesStats.interceptX;
71
- interceptYValues[j] = seriesStats.interceptY;
72
- slopeValues[j] = seriesStats.slope;
78
+ interceptXValues[j] = seriesStats.interceptX!;
79
+ interceptYValues[j] = seriesStats.interceptY!;
80
+ slopeValues[j] = seriesStats.slope!;
73
81
  topValues[j] = seriesStats.top!;
74
82
  bottomValues[j] = seriesStats.bottom!;
75
83
  j++;
76
84
  }
77
85
 
78
86
  return {
79
- rSquared: DG.Stats.fromValues(rSquaredValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number,
80
- auc: DG.Stats.fromValues(aucValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number,
81
- interceptX: DG.Stats.fromValues(interceptXValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number,
82
- interceptY: DG.Stats.fromValues(interceptYValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number,
83
- slope: DG.Stats.fromValues(slopeValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number,
84
- top: DG.Stats.fromValues(topValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number,
85
- bottom: DG.Stats.fromValues(bottomValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number
87
+ rSquared: rSquaredValues.some((elem) => elem === undefined || elem === null) ? undefined:
88
+ DG.Stats.fromValues(rSquaredValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number,
89
+ auc: aucValues.some((elem) => elem === undefined || elem === null) ? undefined :
90
+ DG.Stats.fromValues(aucValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number,
91
+ interceptX: interceptXValues.some((elem) => elem === undefined || elem === null) ? undefined :
92
+ DG.Stats.fromValues(interceptXValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number,
93
+ interceptY: interceptYValues.some((elem) => elem === undefined || elem === null) ? undefined :
94
+ DG.Stats.fromValues(interceptYValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number,
95
+ slope: slopeValues.some((elem) => elem === undefined || elem === null) ? undefined :
96
+ DG.Stats.fromValues(slopeValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number,
97
+ top: topValues.some((elem) => elem === undefined || elem === null) ? undefined :
98
+ DG.Stats.fromValues(topValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number,
99
+ bottom: bottomValues.some((elem) => elem === undefined || elem === null) ? undefined :
100
+ DG.Stats.fromValues(bottomValues)[AGGREGATION_TYPES[aggrType] as keyof DG.Stats] as number
86
101
  };
87
102
  }
88
103
 
@@ -106,11 +121,11 @@ function convertJnJColumnToJSON(column: DG.Column): void {
106
121
  const chartData: IFitChartData = convertXMLToIFitChartData(value);
107
122
  column.set(i, JSON.stringify(chartData));
108
123
  }
109
- column.setTag(TAG_FIT_CHART_FORMAT, '');
124
+ column.setTag(FitConstants.TAG_FIT_CHART_FORMAT, '');
110
125
  }
111
126
 
112
127
  function detectSettings(df: DG.DataFrame): void {
113
- const fitColumns = df.columns.bySemTypeAll(FIT_SEM_TYPE);
128
+ const fitColumns = df.columns.bySemTypeAll(FitConstants.FIT_SEM_TYPE);
114
129
  for (let i = 0; i < fitColumns.length; i++) {
115
130
  fitChartDataProperties.map((prop) => {
116
131
  fitColumns[i].temp[`${CHART_OPTIONS}-custom-${prop.name}`] = false;
@@ -118,7 +133,7 @@ function detectSettings(df: DG.DataFrame): void {
118
133
  fitSeriesProperties.map((prop) => {
119
134
  fitColumns[i].temp[`${SERIES_OPTIONS}-custom-${prop.name}`] = false;
120
135
  });
121
- if (fitColumns[i].getTag(TAG_FIT_CHART_FORMAT) === TAG_FIT_CHART_FORMAT_3DX)
136
+ if (fitColumns[i].getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX)
122
137
  convertJnJColumnToJSON(fitColumns[i]);
123
138
 
124
139
  for (let j = 0; j < fitColumns[i].length; j++) {
@@ -163,11 +178,11 @@ function changeCurvesOptions(gridCell: DG.GridCell, inputBase: DG.InputBase, opt
163
178
  else {
164
179
  let columns: DG.Column[];
165
180
  if (manipulationLevel === MANIPULATION_LEVEL.DATAFRAME) {
166
- gridCell.cell.dataFrame.tags[TAG_FIT] = JSON.stringify(chartOptions);
167
- columns = gridCell.cell.dataFrame.columns.bySemTypeAll(FIT_SEM_TYPE);
181
+ gridCell.cell.dataFrame.tags[FitConstants.TAG_FIT] = JSON.stringify(chartOptions);
182
+ columns = gridCell.cell.dataFrame.columns.bySemTypeAll(FitConstants.FIT_SEM_TYPE);
168
183
  }
169
184
  else {
170
- gridCell.cell.column.tags[TAG_FIT] = JSON.stringify(chartOptions);
185
+ gridCell.cell.column.tags[FitConstants.TAG_FIT] = JSON.stringify(chartOptions);
171
186
  columns = [gridCell.cell.column];
172
187
  }
173
188
 
@@ -176,7 +191,7 @@ function changeCurvesOptions(gridCell: DG.GridCell, inputBase: DG.InputBase, opt
176
191
  const columnChartOptions = getColumnChartOptions(columns[i]);
177
192
  options === CHART_OPTIONS ? delete columnChartOptions.chartOptions![propertyName as keyof IFitChartOptions] :
178
193
  delete columnChartOptions.seriesOptions![propertyName as keyof IFitSeriesOptions];
179
- columns[i].tags[TAG_FIT] = JSON.stringify(columnChartOptions);
194
+ columns[i].tags[FitConstants.TAG_FIT] = JSON.stringify(columnChartOptions);
180
195
  }
181
196
  if (columns[i].temp[`${options}-custom-${propertyName}`] === false) continue;
182
197
 
@@ -217,7 +232,7 @@ export class FitGridCellHandler extends DG.ObjectHandler {
217
232
  }
218
233
 
219
234
  isApplicable(x: any): boolean {
220
- return x instanceof DG.GridCell && x.cellType === FIT_CELL_TYPE;
235
+ return x instanceof DG.GridCell && x.cellType === FitConstants.FIT_CELL_TYPE;
221
236
  }
222
237
 
223
238
  // TODO: add aspect ratio for the cell
@@ -328,7 +343,7 @@ export class FitGridCellHandler extends DG.ObjectHandler {
328
343
  ui.input.form(seriesStatistics, statisticsProperties, {
329
344
  onCreated: (input) => input.root.appendChild(ui.iconFA('plus', async () => {
330
345
  const funcParams = {df: gridCell.cell.dataFrame, colName: gridCell.gridColumn.name, propName: input.property.name, seriesName: series.name, seriesNumber: i};
331
- await DG.Func.find({name: 'addStatisticsColumn'})[0].prepare(funcParams).call(undefined, undefined, {processed: false})
346
+ await DG.Func.find({name: 'addStatisticsColumn'})[0].prepare(funcParams).call(undefined, undefined, {processed: false});
332
347
  }, `Calculate ${input.property.name} for the whole column`))
333
348
  })
334
349
  ]));