@datagrok/curves 1.2.15 → 1.3.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.
@@ -2,33 +2,37 @@ import * as DG from 'datagrok-api/dg';
2
2
  import * as ui from 'datagrok-api/ui';
3
3
 
4
4
  import {CellRenderViewer} from './cell-render-viewer';
5
- import {FitChartCellRenderer} from './fit-renderer';
6
- import {getChartData, mergeProperties} from '../fit/fit-renderer';
5
+ import {FitChartCellRenderer, mergeSeries} from './fit-renderer';
6
+ import {getChartData, mergeProperties} from './fit-renderer';
7
7
  import {FIT_SEM_TYPE, FitChartData, fitChartDataProperties, IFitChartData} from '@datagrok-libraries/statistics/src/fit/fit-curve';
8
8
 
9
9
  import {debounce} from 'rxjs/operators';
10
10
  import {interval, merge} from 'rxjs';
11
11
 
12
12
 
13
+ const ERROR_CLASS = 'd4-viewer-error';
14
+
13
15
  export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
14
- curvesColumnName?: string;
16
+ curvesColumnNames?: string[] = [];
15
17
  showSelectedRowsCurves: boolean = false;
16
18
  showCurrentRowCurve: boolean = true;
17
19
  showMouseOverRowCurve: boolean = true;
20
+ mergeColumnSeries: boolean = false;
18
21
  rows: number[] = [];
19
22
  data: IFitChartData = new FitChartData();
20
23
 
21
24
  constructor() {
22
25
  super(new FitChartCellRenderer());
23
26
 
24
- this.curvesColumnName = this.column('curves');
27
+ this.curvesColumnNames = this.addProperty('curvesColumnNames', DG.TYPE.COLUMN_LIST);
25
28
 
26
29
  this.showSelectedRowsCurves = this.bool('showSelectedRowsCurves', false, { description: 'Adds curves from the selected rows'});
27
30
  this.showCurrentRowCurve = this.bool('showCurrentRowCurve', true);
28
31
  this.showMouseOverRowCurve = this.bool('showMouseOverRowCurve', true);
32
+ this.mergeColumnSeries = this.bool('mergeColumnSeries', false);
29
33
 
30
34
  for (const p of fitChartDataProperties)
31
- this.addProperty(p.name, p.propertyType, p.defaultValue, p.options);
35
+ this.addProperty(p.name === 'mergeSeries' ? 'mergeCellSeries' : p.name, p.propertyType, p.defaultValue, p.options);
32
36
  }
33
37
 
34
38
  static fromChartData(chartData: IFitChartData): MultiCurveViewer {
@@ -43,7 +47,7 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
43
47
  }
44
48
 
45
49
  createChartData(): void {
46
- if (!this.curvesColumnName)
50
+ if (this.curvesColumnNames?.length === 0)
47
51
  return;
48
52
  this.rows.length = 0;
49
53
  if (this.showCurrentRowCurve && this.dataFrame.currentRowIdx !== -1)
@@ -55,20 +59,48 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
55
59
 
56
60
  this.data = new FitChartData();
57
61
  const grid = this.tableView?.grid!;
58
- for (let i of this.rows) {
59
- const cellCurves = getChartData(grid.cell(this.curvesColumnName!, grid.tableRowToGrid(i)));
60
- this.data.series?.push(...cellCurves.series!);
62
+ this.data.chartOptions!.showColumnLabel = this.props.get('showColumnLabel') as unknown as boolean;
63
+ const mergeCellSeries = this.props.get('mergeCellSeries') as unknown as boolean;
64
+ for (const colName of this.curvesColumnNames!) {
65
+ const series = [];
66
+ for (const i of new Set(this.rows)) {
67
+ const gridCell = grid.cell(colName, grid.tableRowToGrid(i));
68
+ const cellCurves = getChartData(gridCell);
69
+ cellCurves.series?.forEach((series) => series.columnName = gridCell.cell.column.name);
70
+ if (mergeCellSeries)
71
+ cellCurves.series = [mergeSeries(cellCurves.series!)!];
72
+ series.push(...cellCurves.series!);
73
+ }
74
+ if (this.mergeColumnSeries)
75
+ this.data.series?.push(mergeSeries(series)!);
76
+ else
77
+ this.data.series?.push(...series);
61
78
  }
79
+ this.data.series?.forEach((series, i) => {
80
+ series.pointColor = DG.Color.toHtml(DG.Color.getCategoricalColor(this.data.series?.length! > 20 ? 0 : i));
81
+ series.fitLineColor = DG.Color.toHtml(DG.Color.getCategoricalColor(this.data.series?.length! > 20 ? 0 : i));
82
+ series.showCurveConfidenceInterval = false;
83
+ series.droplines = [];
84
+ if (this.data.series?.length! > 20) {
85
+ series.showPoints = '';
86
+ series.lineStyle = 'solid';
87
+ }
88
+ });
62
89
  }
63
90
 
64
91
  onPropertyChanged(property: DG.Property | null): void {
92
+ if (property?.name === 'curvesColumnNames')
93
+ this.props.set('showColumnLabel', this.curvesColumnNames?.length! > 1 as unknown as object);
65
94
  this.applyViewerProperties();
95
+ this.createChartData();
66
96
  this.render();
67
97
  }
68
98
 
69
99
  onTableAttached(): void {
70
100
  const grid = this.tableView?.grid!;
71
- this.curvesColumnName ??= this.dataFrame.columns.bySemType(FIT_SEM_TYPE)?.name;
101
+ const fitCol = this.dataFrame.columns.bySemType(FIT_SEM_TYPE);
102
+ if (fitCol !== null)
103
+ this.curvesColumnNames = [fitCol.name];
72
104
 
73
105
  merge(this.dataFrame.onCurrentCellChanged, grid.onCellMouseEnter, this.dataFrame.onSelectionChanged)
74
106
  .pipe(debounce(_ => interval(50)))
@@ -82,14 +114,21 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
82
114
  this.root.appendChild(ui.divText(msg, 'd4-viewer-error'));
83
115
  }
84
116
 
117
+ _removeErrorMessage() {
118
+ const divTextElement = this.root.getElementsByClassName(ERROR_CLASS)[0];
119
+ if (divTextElement)
120
+ this.root.removeChild(divTextElement);
121
+ }
122
+
85
123
  render(): void {
86
- if (!this.curvesColumnName) {
124
+ const g = this.canvas.getContext('2d')!
125
+ g.clearRect(0, 0, this.canvas.width, this.canvas.height);
126
+
127
+ if (this.curvesColumnNames?.length === 0) {
87
128
  this._showErrorMessage('The MultiCurveViewer viewer requires a minimum of 1 curves column.');
88
129
  return;
89
130
  }
90
-
91
- const g = this.canvas.getContext('2d')!
92
- g.clearRect(0, 0, this.canvas.width, this.canvas.height);
131
+ this._removeErrorMessage();
93
132
  this.renderer.renderCurves(g, 0, 0, this.canvas.width, this.canvas.height, this.data);
94
133
  }
95
134
  }
package/src/package.ts CHANGED
@@ -1,13 +1,20 @@
1
1
  /* Do not change these import lines to match external modules in webpack configuration */
2
2
  import * as DG from 'datagrok-api/dg';
3
3
 
4
- import {FitGridCellHandler} from './fit/fit-grid-cell-handler';
5
- import {FitChartCellRenderer} from './fit/fit-renderer';
4
+ import {FitGridCellHandler, calculateSeriesStats, getChartDataAggrStats} from './fit/fit-grid-cell-handler';
5
+ import {FitChartCellRenderer, TAG_FIT_CHART_FORMAT, TAG_FIT_CHART_FORMAT_3DX, getChartData, substituteZeroes} from './fit/fit-renderer';
6
6
  import {MultiCurveViewer} from './fit/multi-curve-viewer';
7
7
  import {curveDemo} from './fit/fit-demo';
8
+ import {convertXMLToIFitChartData} from './fit/fit-parser';
9
+ import {LogOptions} from '@datagrok-libraries/statistics/src/fit/fit-data';
10
+ import {FitStatistics} from '@datagrok-libraries/statistics/src/fit/fit-curve';
8
11
 
9
12
 
10
13
  export const _package = new DG.Package();
14
+ const SOURCE_COLUMN_TAG = '.sourceColumn';
15
+ const SERIES_NUMBER_TAG = '.seriesNumber';
16
+ const SERIES_AGGREGATION_TAG = '.seriesAggregation';
17
+ const STATISTICS_TAG = '.statistics';
11
18
 
12
19
 
13
20
  //name: Fit
@@ -22,6 +29,7 @@ export function fitCellRenderer(): FitChartCellRenderer {
22
29
  //name: MultiCurveViewer
23
30
  //description: A viewer that superimposes multiple in-cell curves on one chart
24
31
  //tags: viewer
32
+ //meta.icon: icons/multi-curve-viewer.png
25
33
  //output: viewer result
26
34
  export function _FitViewer(): MultiCurveViewer {
27
35
  return new MultiCurveViewer();
@@ -39,3 +47,69 @@ export async function curveFitDemo(): Promise<void> {
39
47
  export function _initCurves(): void {
40
48
  DG.ObjectHandler.register(new FitGridCellHandler());
41
49
  }
50
+
51
+ //name: addStatisticsColumn
52
+ //tags: Transform
53
+ //input: dataframe df
54
+ //input: string colName
55
+ //input: string propName
56
+ //input: string seriesName
57
+ //input: int seriesNumber
58
+ export function addStatisticsColumn(df: DG.DataFrame, colName: string, propName: string, seriesName: string, seriesNumber: number): void {
59
+ const grid = DG.Viewer.grid(df);
60
+ const chartColumn = grid.col(colName)!;
61
+ const column = DG.Column.float(`${colName}_${seriesName}_${propName}`, chartColumn.column?.length);
62
+ column.tags[SOURCE_COLUMN_TAG] = colName;
63
+ column.tags[SERIES_NUMBER_TAG] = seriesNumber;
64
+ column.tags[STATISTICS_TAG] = propName;
65
+
66
+ column
67
+ .init((i) => {
68
+ const gridCell = DG.GridCell.fromColumnRow(grid, colName, grid.tableRowToGrid(i));
69
+ if (gridCell.cell.value === '')
70
+ return null;
71
+ const chartData = gridCell.cell.column.getTag(TAG_FIT_CHART_FORMAT) === TAG_FIT_CHART_FORMAT_3DX ?
72
+ convertXMLToIFitChartData(gridCell.cell.value) : getChartData(gridCell);
73
+ if (chartData.series![seriesNumber] === undefined || chartData.series![seriesNumber].points.every((p) => p.outlier))
74
+ return null;
75
+ if (chartData.chartOptions?.allowXZeroes && chartData.chartOptions?.logX &&
76
+ chartData.series?.some((series) => series.points.some((p) => p.x === 0)))
77
+ substituteZeroes(chartData);
78
+ const chartLogOptions: LogOptions = {logX: chartData.chartOptions?.logX, logY: chartData.chartOptions?.logY};
79
+ const fitResult = calculateSeriesStats(chartData.series![seriesNumber], chartLogOptions);
80
+ return fitResult[propName as keyof FitStatistics];
81
+ });
82
+ df.columns.insert(column, chartColumn.idx);
83
+ }
84
+
85
+ //name: addAggrStatisticsColumn
86
+ //tags: Transform
87
+ //input: dataframe df
88
+ //input: string colName
89
+ //input: string propName
90
+ //input: string aggrType
91
+ export function addAggrStatisticsColumn(df: DG.DataFrame, colName: string, propName: string, aggrType: string): void {
92
+ const grid = DG.Viewer.grid(df);
93
+ const chartColumn = grid.col(colName)!;
94
+ const column = DG.Column.float(`${colName}_${aggrType}_${propName}`, chartColumn.column?.length);
95
+ column.tags[SOURCE_COLUMN_TAG] = colName;
96
+ column.tags[SERIES_AGGREGATION_TAG] = aggrType;
97
+ column.tags[STATISTICS_TAG] = propName;
98
+
99
+ column
100
+ .init((i) => {
101
+ const gridCell = DG.GridCell.fromColumnRow(grid, colName, grid.tableRowToGrid(i));
102
+ if (gridCell.cell.value === '')
103
+ return null;
104
+ const chartData = gridCell.cell.column.getTag(TAG_FIT_CHART_FORMAT) === TAG_FIT_CHART_FORMAT_3DX ?
105
+ convertXMLToIFitChartData(gridCell.cell.value) : getChartData(gridCell);
106
+ if (chartData.series?.every((series) => series.points.every((p) => p.outlier)))
107
+ return null;
108
+ if (chartData.chartOptions?.allowXZeroes && chartData.chartOptions?.logX &&
109
+ chartData.series?.some((series) => series.points.some((p) => p.x === 0)))
110
+ substituteZeroes(chartData);
111
+ const fitResult = getChartDataAggrStats(chartData, aggrType);
112
+ return fitResult[propName as keyof FitStatistics];
113
+ });
114
+ df.columns.insert(column, chartColumn.idx);
115
+ }