@datagrok/compute 0.6.3 → 0.6.7
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 +2 -1
- package/src/export-funccall.ts +102 -162
- package/src/function-editor/function-editor.ts +304 -225
- package/src/package.ts +9 -38
- package/scripts/pMax.js +0 -191
- package/scripts/vMaxJS.js +0 -208
- package/src/outliers-selection/outliers-selection.ts +0 -281
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
import * as grok from 'datagrok-api/grok';
|
|
2
|
-
import * as ui from 'datagrok-api/ui';
|
|
3
|
-
import * as DG from 'datagrok-api/dg';
|
|
4
|
-
import {_package} from '../package';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @param inputData The data to select the outliers in
|
|
8
|
-
* @returns Two dataframes: 1) with selected outliers and 2) with removed outliers
|
|
9
|
-
* @deprecated Use the OutliersSelectionViewer instead
|
|
10
|
-
*/
|
|
11
|
-
export async function selectOutliersManually(inputData: DG.DataFrame) {
|
|
12
|
-
const IS_OUTLIER_COL_LABEL = 'isOutlier';
|
|
13
|
-
const OUTLIER_RATIONALE_COL_LABEL = 'Rationale';
|
|
14
|
-
const OUTLIER_COUNT_COL_LABEL = 'Count';
|
|
15
|
-
const IS_GROUP_CONFIRMED_LABEL = 'isConfirmed';
|
|
16
|
-
const FLUX = 't/V (hr/(L/m²))';
|
|
17
|
-
const DEFAULT_TEST_AREA = 3.5;
|
|
18
|
-
|
|
19
|
-
if (!inputData.columns.byName(IS_OUTLIER_COL_LABEL)) {
|
|
20
|
-
inputData.columns
|
|
21
|
-
.add(DG.Column.fromBitSet(IS_OUTLIER_COL_LABEL, DG.BitSet.create(inputData.rowCount, () => false)));
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (!inputData.columns.byName(OUTLIER_RATIONALE_COL_LABEL)) {
|
|
25
|
-
inputData.columns
|
|
26
|
-
.add(DG.Column.fromStrings(OUTLIER_RATIONALE_COL_LABEL, Array.from({length: inputData.rowCount}, () => '')));
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (!inputData.columns.byName(FLUX)) {
|
|
30
|
-
(inputData.columns as DG.ColumnList).addNew(FLUX, 'double').init((index) => (inputData.cell(index, 'time (min)').value/60.0) / ((inputData.cell(index, 'filtrate volume (mL)').value/1000.0) / (DEFAULT_TEST_AREA / 10000.0)));
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const initialData = inputData.clone();
|
|
34
|
-
|
|
35
|
-
const scatterPlot = DG.Viewer.scatterPlot(inputData, {
|
|
36
|
-
'color': OUTLIER_RATIONALE_COL_LABEL,
|
|
37
|
-
'lassoTool': true,
|
|
38
|
-
'legendVisibility': 'Never',
|
|
39
|
-
'filterByZoom': false,
|
|
40
|
-
});
|
|
41
|
-
scatterPlot.root.style.height = '100%';
|
|
42
|
-
scatterPlot.root.style.width = '100%';
|
|
43
|
-
|
|
44
|
-
const clearTable = () => {
|
|
45
|
-
return DG.DataFrame.fromColumns([
|
|
46
|
-
DG.Column.fromStrings(OUTLIER_RATIONALE_COL_LABEL, []),
|
|
47
|
-
DG.Column.fromInt32Array(OUTLIER_COUNT_COL_LABEL, new Int32Array([])),
|
|
48
|
-
DG.Column.fromStrings('Actions', []),
|
|
49
|
-
DG.Column.fromBitSet(IS_GROUP_CONFIRMED_LABEL, DG.BitSet.create(0, () => false)),
|
|
50
|
-
]);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const groupsListGrid = DG.Viewer.grid(clearTable());
|
|
54
|
-
groupsListGrid.root.style.width = '100%';
|
|
55
|
-
groupsListGrid.columns.setVisible([OUTLIER_RATIONALE_COL_LABEL, OUTLIER_COUNT_COL_LABEL, 'Actions']);
|
|
56
|
-
|
|
57
|
-
groupsListGrid.onCellPrepare((gc) => {
|
|
58
|
-
if (!gc.isTableCell) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const confirmBtn = () => ui.div(
|
|
63
|
-
ui.iconFA('check', () => {
|
|
64
|
-
if (!groupsListGrid.dataFrame) return;
|
|
65
|
-
|
|
66
|
-
const newRationale =
|
|
67
|
-
groupsListGrid.dataFrame?.cell(groupsListGrid.dataFrame.rowCount-1, OUTLIER_RATIONALE_COL_LABEL).value;
|
|
68
|
-
|
|
69
|
-
inputData.selection.getSelectedIndexes().forEach((selectedIndex: number) => {
|
|
70
|
-
inputData.set(IS_OUTLIER_COL_LABEL, selectedIndex, true);
|
|
71
|
-
inputData.set(
|
|
72
|
-
OUTLIER_RATIONALE_COL_LABEL,
|
|
73
|
-
selectedIndex,
|
|
74
|
-
newRationale,
|
|
75
|
-
);
|
|
76
|
-
});
|
|
77
|
-
inputData.selection.setAll(false);
|
|
78
|
-
}, 'Confirm the outliers'), {style: {'text-align': 'center', 'margin': '6px'}},
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
const cancelBtn = () => ui.div(
|
|
82
|
-
ui.iconFA('times', () => {
|
|
83
|
-
if (!groupsListGrid.dataFrame) return;
|
|
84
|
-
|
|
85
|
-
groupsListGrid.dataFrame.rows.removeAt(groupsListGrid.dataFrame.rowCount-1);
|
|
86
|
-
inputData.selection.setAll(false);
|
|
87
|
-
}, 'Cancel the outliers'), {style: {'text-align': 'center', 'margin': '6px'}},
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
const deleteBtn = (rationale: string) => ui.div(
|
|
91
|
-
ui.icons.delete(() => {
|
|
92
|
-
for (let i = 0; i < inputData.rowCount; i++) {
|
|
93
|
-
if (inputData.columns.byName(OUTLIER_RATIONALE_COL_LABEL).get(i) === rationale) {
|
|
94
|
-
inputData.columns.byName(OUTLIER_RATIONALE_COL_LABEL).set(i, '');
|
|
95
|
-
inputData.columns.byName(IS_OUTLIER_COL_LABEL).set(i, false);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}, 'Remove the outliers group'), {style: {'text-align': 'center', 'margin': '6px'}},
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
if (gc.gridColumn.name === 'Actions') {
|
|
102
|
-
gc.gridColumn.cellType = 'html';
|
|
103
|
-
gc.style.element =
|
|
104
|
-
ui.divH([
|
|
105
|
-
...!gc.grid.dataFrame?.get(IS_GROUP_CONFIRMED_LABEL, gc.gridRow) ?
|
|
106
|
-
[confirmBtn(), cancelBtn()] :
|
|
107
|
-
[deleteBtn(gc.grid.dataFrame?.get(OUTLIER_RATIONALE_COL_LABEL, gc.gridRow))],
|
|
108
|
-
]);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
groupsListGrid.onCellValueEdited.subscribe((editedCell) => {
|
|
113
|
-
if (!groupsListGrid.dataFrame || editedCell.gridColumn.name !== OUTLIER_RATIONALE_COL_LABEL) return;
|
|
114
|
-
|
|
115
|
-
if (!(groupsListGrid.dataFrame.columns as DG.ColumnList)
|
|
116
|
-
.byName(IS_GROUP_CONFIRMED_LABEL).get(editedCell.gridRow)) return;
|
|
117
|
-
|
|
118
|
-
const uniqueValues= new Set<string>();
|
|
119
|
-
for (let i = 0; i < groupsListGrid.dataFrame?.rowCount; i++) {
|
|
120
|
-
const record = groupsListGrid.dataFrame?.columns.byName(OUTLIER_RATIONALE_COL_LABEL).get(i);
|
|
121
|
-
uniqueValues.add(record);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
for (let i = 0; i < inputData.rowCount; i++) {
|
|
125
|
-
const currentCellValue = inputData.columns.byName(OUTLIER_RATIONALE_COL_LABEL).get(i);
|
|
126
|
-
if (currentCellValue != '' && !uniqueValues.has(currentCellValue)) {
|
|
127
|
-
inputData.columns.byName(OUTLIER_RATIONALE_COL_LABEL).set(i, editedCell.cell.value);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
const updateGroupsTable = () => {
|
|
133
|
-
const uniqueValues: {[key:string]: number} = {};
|
|
134
|
-
for (let i = 0; i < inputData.rowCount; i++) {
|
|
135
|
-
const record = inputData.columns.byName(OUTLIER_RATIONALE_COL_LABEL).get(i);
|
|
136
|
-
const count = uniqueValues[record];
|
|
137
|
-
if (record != '') {
|
|
138
|
-
count ?
|
|
139
|
-
uniqueValues[record]++ :
|
|
140
|
-
uniqueValues[record] = 1;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
groupsListGrid.dataFrame = clearTable();
|
|
144
|
-
groupsListGrid.columns.setVisible([OUTLIER_RATIONALE_COL_LABEL, OUTLIER_COUNT_COL_LABEL, 'Actions']);
|
|
145
|
-
|
|
146
|
-
Object.keys(uniqueValues).map((key: string) => {
|
|
147
|
-
groupsListGrid.dataFrame?.rows.addNew([key, uniqueValues[key], '', true]);
|
|
148
|
-
});
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
inputData.onDataChanged.subscribe(updateGroupsTable);
|
|
152
|
-
inputData.onSelectionChanged.subscribe(() => {
|
|
153
|
-
if (!groupsListGrid.dataFrame || !inputData.selection.trueCount) return;
|
|
154
|
-
|
|
155
|
-
const isConfirmedColumn = (groupsListGrid.dataFrame.columns as DG.ColumnList)
|
|
156
|
-
.byName(IS_GROUP_CONFIRMED_LABEL).toList();
|
|
157
|
-
|
|
158
|
-
if (isConfirmedColumn.some((value) => value === false)) {
|
|
159
|
-
groupsListGrid.dataFrame.set(
|
|
160
|
-
OUTLIER_COUNT_COL_LABEL,
|
|
161
|
-
groupsListGrid.dataFrame.rowCount-1,
|
|
162
|
-
inputData.selection.trueCount,
|
|
163
|
-
);
|
|
164
|
-
} else {
|
|
165
|
-
const newRow = groupsListGrid.dataFrame.rows.addNew(['Manual', inputData.selection.trueCount, '', false]);
|
|
166
|
-
groupsListGrid.dataFrame.currentCell = groupsListGrid.dataFrame.cell(newRow.idx, OUTLIER_RATIONALE_COL_LABEL);
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
let shouldCancel = true;
|
|
171
|
-
|
|
172
|
-
const cancelAllChanges = () => {
|
|
173
|
-
if (shouldCancel) {
|
|
174
|
-
(inputData.columns as DG.ColumnList).byName(IS_OUTLIER_COL_LABEL).init(
|
|
175
|
-
(index) => initialData.get(IS_OUTLIER_COL_LABEL, index),
|
|
176
|
-
);
|
|
177
|
-
(inputData.columns as DG.ColumnList).byName(OUTLIER_RATIONALE_COL_LABEL).init(
|
|
178
|
-
(index) => initialData.get(OUTLIER_RATIONALE_COL_LABEL, index),
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
const removeOutlierGroupBtn = ui.button(
|
|
184
|
-
'RESET SELECTED',
|
|
185
|
-
() => {
|
|
186
|
-
inputData.selection.getSelectedIndexes().forEach((selectedIndex: number) => {
|
|
187
|
-
inputData.set(IS_OUTLIER_COL_LABEL, selectedIndex, false);
|
|
188
|
-
inputData.set(OUTLIER_RATIONALE_COL_LABEL, selectedIndex, '');
|
|
189
|
-
});
|
|
190
|
-
inputData.selection.setAll(false);
|
|
191
|
-
},
|
|
192
|
-
'Remove the selected points from oultiers list',
|
|
193
|
-
);
|
|
194
|
-
|
|
195
|
-
const autoOutlierGroupBtn = ui.button(
|
|
196
|
-
'AUTO DETECT...',
|
|
197
|
-
() => {
|
|
198
|
-
const intInput = ui.intInput('N', 3);
|
|
199
|
-
const columnInput = ui.columnInput('Column', inputData, null);
|
|
200
|
-
ui.dialog('Detect outliers automatically')
|
|
201
|
-
.add(columnInput.root)
|
|
202
|
-
.add(intInput.root)
|
|
203
|
-
.onOK(()=>{
|
|
204
|
-
const meanValue = (columnInput.value as DG.Column).stats.avg;
|
|
205
|
-
const stddev = (columnInput.value as DG.Column).stats.stdev;
|
|
206
|
-
inputData.selection.init((index)=>
|
|
207
|
-
Math.abs(meanValue - (columnInput.value as DG.Column).get(index)) > stddev*intInput.value,
|
|
208
|
-
);
|
|
209
|
-
inputData.selection.getSelectedIndexes().forEach((selectedIndex: number) => {
|
|
210
|
-
inputData.set(IS_OUTLIER_COL_LABEL, selectedIndex, true);
|
|
211
|
-
inputData.set(OUTLIER_RATIONALE_COL_LABEL, selectedIndex, `${intInput.value}x stddev rule`);
|
|
212
|
-
});
|
|
213
|
-
inputData.selection.setAll(false);
|
|
214
|
-
})
|
|
215
|
-
.show();
|
|
216
|
-
},
|
|
217
|
-
'Detect outliers automatically',
|
|
218
|
-
);
|
|
219
|
-
|
|
220
|
-
inputData.selection.setAll(false);
|
|
221
|
-
|
|
222
|
-
inputData.onSelectionChanged.subscribe(() => {
|
|
223
|
-
if (inputData.selection.trueCount === 0) {
|
|
224
|
-
removeOutlierGroupBtn.classList.add('disabled');
|
|
225
|
-
} else {
|
|
226
|
-
removeOutlierGroupBtn.classList.remove('disabled');
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
updateGroupsTable();
|
|
231
|
-
removeOutlierGroupBtn.classList.add('disabled');
|
|
232
|
-
|
|
233
|
-
const result = new Promise<{augmentedInput: DG.DataFrame, editedInput: DG.DataFrame}>((resolve, reject) => {
|
|
234
|
-
const selectionDialog = ui.dialog({title: 'Select outliers', helpUrl: `${_package.webRoot}/help/outliers_selection/main.md`})
|
|
235
|
-
.add(
|
|
236
|
-
ui.info(
|
|
237
|
-
ui.div([
|
|
238
|
-
ui.p('Hold the “SHIFT” key and start to draw a freehand selection on the scatterplot area'),
|
|
239
|
-
]),
|
|
240
|
-
),
|
|
241
|
-
)
|
|
242
|
-
.add(
|
|
243
|
-
ui.divH([
|
|
244
|
-
ui.block75([scatterPlot.root]),
|
|
245
|
-
ui.block25([
|
|
246
|
-
ui.divV([
|
|
247
|
-
ui.divH([
|
|
248
|
-
removeOutlierGroupBtn, autoOutlierGroupBtn,
|
|
249
|
-
], {style: {'text-align': 'center'}}),
|
|
250
|
-
groupsListGrid.root,
|
|
251
|
-
], {style: {height: '75%'}}),
|
|
252
|
-
]),
|
|
253
|
-
], {style: {'height': '100%', 'min-height': '600px'}}),
|
|
254
|
-
)
|
|
255
|
-
.onOK(() => {
|
|
256
|
-
shouldCancel = false;
|
|
257
|
-
const editedInput = inputData.clone();
|
|
258
|
-
editedInput.rows.filter((row) => !inputData.get(IS_OUTLIER_COL_LABEL, row.idx));
|
|
259
|
-
resolve({augmentedInput: inputData, editedInput});
|
|
260
|
-
})
|
|
261
|
-
.onCancel(() => {
|
|
262
|
-
const editedInput = initialData.clone();
|
|
263
|
-
editedInput.rows.filter((row) => !initialData.get(IS_OUTLIER_COL_LABEL, row.idx));
|
|
264
|
-
cancelAllChanges();
|
|
265
|
-
resolve({augmentedInput: inputData, editedInput});
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
// TODO: change the resolving strategy after API update
|
|
269
|
-
// onClose is called before onOK or onCancel, thus, the timeout is requierd
|
|
270
|
-
selectionDialog.onClose.subscribe(() =>
|
|
271
|
-
setTimeout(()=>{
|
|
272
|
-
const editedInput = initialData.clone();
|
|
273
|
-
editedInput.rows.filter((row) => !initialData.get(IS_OUTLIER_COL_LABEL, row.idx));
|
|
274
|
-
cancelAllChanges();
|
|
275
|
-
resolve({augmentedInput: inputData, editedInput});
|
|
276
|
-
}, 100));
|
|
277
|
-
selectionDialog.show({width: 1000});
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
return result;
|
|
281
|
-
}
|