@datagrok/eda 1.1.31 → 1.1.32
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/CHANGELOG.md +4 -0
- package/README.md +1 -0
- package/dist/23.js.map +1 -1
- package/dist/242.js +1 -1
- package/dist/242.js.map +1 -1
- package/dist/449.js +1 -1
- package/dist/449.js.map +1 -1
- package/dist/738.js +1 -1
- package/dist/738.js.map +1 -1
- package/dist/77573759e3857711e15b.wasm +0 -0
- package/dist/990.js +2 -0
- package/dist/990.js.map +1 -0
- package/dist/package-test.js +1 -1
- package/dist/package-test.js.map +1 -1
- package/dist/package.js +1 -1
- package/dist/package.js.map +1 -1
- package/package.json +92 -91
- package/src/package-test.ts +2 -0
- package/src/package.ts +61 -2
- package/src/pls/pls-constants.ts +21 -5
- package/src/pls/pls-tools.ts +8 -2
- package/src/tests/classifiers-tests.ts +114 -0
- package/src/tests/linear-methods-tests.ts +150 -0
- package/src/tests/utils.ts +121 -0
- package/src/xgbooster.ts +260 -0
- package/wasm/XGBoostAPI.js +32 -0
- package/wasm/XGBoostAPI.wasm +0 -0
- package/wasm/XGBoostAPIinWebWorker.js +32 -0
- package/wasm/callWasmForWebWorker.js +11 -8
- package/wasm/workers/xgboostWorker.js +67 -0
- package/wasm/xgboost/CMakeLists.txt +23 -0
- package/wasm/xgboost/commands.txt +12 -0
- package/wasm/xgboost/xgboost/README.txt +1 -0
- package/wasm/xgboost/xgboost-api.cpp +134 -0
- package/wasm/xgbooster.js +161 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// JavaScript API for call wasm-functions from the XGBoostAPI module
|
|
2
|
+
|
|
3
|
+
// Data constants
|
|
4
|
+
const INT_BYTES = 4;
|
|
5
|
+
const FLOAT_BYTES = 4;
|
|
6
|
+
const SIZE_IDX = 0;
|
|
7
|
+
|
|
8
|
+
export async function initXgboost() {
|
|
9
|
+
await initXGBoostModule();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Fit and return model params */
|
|
13
|
+
export function fit(features, target, missingValue, iterations, eta, maxDepth, lambda, alpha,
|
|
14
|
+
modelReserve, utilsLength) {
|
|
15
|
+
// Data size
|
|
16
|
+
const samplesCount = target.length;
|
|
17
|
+
const featuresCount = features.length;
|
|
18
|
+
|
|
19
|
+
// Allocate memory
|
|
20
|
+
const featuresBuf = XGBoostModule._malloc(samplesCount * featuresCount * FLOAT_BYTES);
|
|
21
|
+
const targetBuf = XGBoostModule._malloc(samplesCount * FLOAT_BYTES);
|
|
22
|
+
const modelBuf = XGBoostModule._malloc(modelReserve * INT_BYTES);
|
|
23
|
+
const utilsBuf = XGBoostModule._malloc(utilsLength * INT_BYTES);
|
|
24
|
+
|
|
25
|
+
// Wasm buffer routine
|
|
26
|
+
const floatHeap = XGBoostModule.HEAPF32;
|
|
27
|
+
let intHeap = XGBoostModule.HEAP32;
|
|
28
|
+
let raw;
|
|
29
|
+
|
|
30
|
+
// Put features to wasm buffer
|
|
31
|
+
for (let j = 0; j < featuresCount; ++j) {
|
|
32
|
+
raw = features.byIndex(j).getRawData();
|
|
33
|
+
|
|
34
|
+
for (let i = 0; i < samplesCount; ++i)
|
|
35
|
+
floatHeap[featuresBuf / FLOAT_BYTES + i + j * samplesCount] = raw[i];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Put targets to wasm buffer
|
|
39
|
+
raw = target.getRawData();
|
|
40
|
+
for (let i = 0; i < samplesCount; ++i)
|
|
41
|
+
floatHeap[targetBuf / FLOAT_BYTES + i] = raw[i];
|
|
42
|
+
|
|
43
|
+
// Train model
|
|
44
|
+
XGBoostModule._train(
|
|
45
|
+
featuresBuf, samplesCount, featuresCount, missingValue, // features data
|
|
46
|
+
targetBuf, samplesCount, // target data
|
|
47
|
+
iterations, eta, maxDepth, lambda, alpha, // hyperparameters
|
|
48
|
+
utilsBuf, utilsLength, // utils
|
|
49
|
+
modelBuf, modelReserve, // model params to be trained
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// Extract model params from wasm buffer
|
|
53
|
+
intHeap = XGBoostModule.HEAP32;
|
|
54
|
+
const paramsCount = intHeap[utilsBuf / INT_BYTES + SIZE_IDX];
|
|
55
|
+
const params = new Int32Array(paramsCount);
|
|
56
|
+
|
|
57
|
+
for (let i = 0; i < paramsCount; ++i)
|
|
58
|
+
params[i] = intHeap[modelBuf / INT_BYTES + i];
|
|
59
|
+
|
|
60
|
+
// Free allocated memory
|
|
61
|
+
XGBoostModule._free(featuresBuf);
|
|
62
|
+
XGBoostModule._free(targetBuf);
|
|
63
|
+
XGBoostModule._free(utilsBuf);
|
|
64
|
+
XGBoostModule._free(modelBuf);
|
|
65
|
+
|
|
66
|
+
return params;
|
|
67
|
+
} // fit
|
|
68
|
+
|
|
69
|
+
/** Fit and return model params in webworker */
|
|
70
|
+
export async function fitInWebWorker(features, target, missingValue, iterations, eta, maxDepth, lambda, alpha,
|
|
71
|
+
modelReserve, utilsLength) {
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
// Data size
|
|
74
|
+
const samplesCount = target.length;
|
|
75
|
+
const featuresCount = features.length;
|
|
76
|
+
|
|
77
|
+
// Features raw data
|
|
78
|
+
const featuresRaw = new Float32Array(samplesCount * featuresCount);
|
|
79
|
+
let shift;
|
|
80
|
+
let raw;
|
|
81
|
+
for (let j = 0; j < featuresCount; ++j) {
|
|
82
|
+
raw = features.byIndex(j).getRawData();
|
|
83
|
+
shift = j * samplesCount;
|
|
84
|
+
|
|
85
|
+
for (let i = 0; i < samplesCount; ++i)
|
|
86
|
+
featuresRaw[i + shift] = raw[i];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const worker = new Worker(new URL('../wasm/workers/xgboostWorker.js', import.meta.url));
|
|
90
|
+
|
|
91
|
+
worker.postMessage({
|
|
92
|
+
features: featuresRaw,
|
|
93
|
+
target: target.getRawData(),
|
|
94
|
+
samplesCount: samplesCount,
|
|
95
|
+
featuresCount: featuresCount,
|
|
96
|
+
modelReserve: modelReserve,
|
|
97
|
+
utilsLength: utilsLength,
|
|
98
|
+
iterations: iterations,
|
|
99
|
+
eta: eta,
|
|
100
|
+
maxDepth: maxDepth,
|
|
101
|
+
lambda: lambda,
|
|
102
|
+
alpha: alpha,
|
|
103
|
+
missingValue: missingValue,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
worker.onmessage = function(e) {
|
|
107
|
+
worker.terminate();
|
|
108
|
+
resolve(e.data.params);
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
} // fitInWebWorker
|
|
112
|
+
|
|
113
|
+
/** Return prediction by trained model */
|
|
114
|
+
export function predict(features, missingValue, params) {
|
|
115
|
+
// Data & model sizes
|
|
116
|
+
const samplesCount = features.byIndex(0).length;
|
|
117
|
+
const featuresCount = features.length;
|
|
118
|
+
const paramsCount = params.length;
|
|
119
|
+
|
|
120
|
+
// Wasm buffer routine
|
|
121
|
+
let floatHeap = XGBoostModule.HEAPF32;
|
|
122
|
+
const intHeap = XGBoostModule.HEAP32;
|
|
123
|
+
|
|
124
|
+
// Allocate memory
|
|
125
|
+
const featuresBuf = XGBoostModule._malloc(samplesCount * featuresCount * FLOAT_BYTES);
|
|
126
|
+
const targetBuf = XGBoostModule._malloc(samplesCount * FLOAT_BYTES);
|
|
127
|
+
const modelBuf = XGBoostModule._malloc(paramsCount * INT_BYTES);
|
|
128
|
+
|
|
129
|
+
// Put features to wasm buffer
|
|
130
|
+
for (let j = 0; j < featuresCount; ++j) {
|
|
131
|
+
const raw = features.byIndex(j).getRawData();
|
|
132
|
+
|
|
133
|
+
for (let i = 0; i < samplesCount; ++i)
|
|
134
|
+
floatHeap[featuresBuf / FLOAT_BYTES + i + j * samplesCount] = raw[i];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Put model to wasm bufffer
|
|
138
|
+
for (let i = 0; i < paramsCount; ++i)
|
|
139
|
+
intHeap[modelBuf / INT_BYTES + i] = params[i];
|
|
140
|
+
|
|
141
|
+
// Compute predictions
|
|
142
|
+
XGBoostModule._predict(
|
|
143
|
+
featuresBuf, samplesCount, featuresCount, missingValue, // features
|
|
144
|
+
modelBuf, paramsCount, // model params
|
|
145
|
+
targetBuf, samplesCount, // target to be predicted
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
// Extract predictions from wasm buffer
|
|
149
|
+
floatHeap = XGBoostModule.HEAPF32;
|
|
150
|
+
const prediction = new Float32Array(samplesCount);
|
|
151
|
+
|
|
152
|
+
for (let i = 0; i < samplesCount; ++i)
|
|
153
|
+
prediction[i] = floatHeap[targetBuf / FLOAT_BYTES + i];
|
|
154
|
+
|
|
155
|
+
// Free allocated memory
|
|
156
|
+
XGBoostModule._free(featuresBuf);
|
|
157
|
+
XGBoostModule._free(targetBuf);
|
|
158
|
+
XGBoostModule._free(modelBuf);
|
|
159
|
+
|
|
160
|
+
return prediction;
|
|
161
|
+
} // predict
|