Skip to content

Commit 87a9100

Browse files
champierreyining1023
authored andcommitted
feat(KNNClassifier): Add loadData() that enables user to load dat… (#279)
* feat(KNNClassifier): Add loadData() that enables user to load dataset from raw data instead of file With load(), file path needs to be provided to load dataset. This means that JSON file that has dataset needs to be located on the server. You can give a way to download dataset using save(), but you cannot give a way to upload that file and load the dataset. This commit adds loadData() that enables user to load dataset from raw data. By combining this method and <INPUT type="file"> tag, you can give a way for user to upload dataset file. * Merge load(path, callback) and loadData(data, callback) into load(pathOrJson, callback). * Merge load() and loadData() in a cleaner way without using Symbol. Ref. #279 (comment)
1 parent 1e0192e commit 87a9100

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

src/KNNClassifier/index.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -162,29 +162,33 @@ class KNN {
162162
await io.saveBlob(JSON.stringify({ dataset, tensors }), fileName, 'application/octet-stream');
163163
}
164164

165-
load(path, callback) {
166-
io.loadFile(path, (err, data) => {
167-
if (data) {
168-
const { dataset, tensors } = data;
169-
this.mapStringToIndex = Object.keys(dataset).map(key => dataset[key].label);
170-
const tensorsData = tensors
171-
.map((tensor, i) => {
172-
if (tensor) {
173-
const values = Object.keys(tensor).map(v => tensor[v]);
174-
return tf.tensor(values, dataset[i].shape, dataset[i].dtype);
175-
}
176-
return null;
177-
})
178-
.reduce((acc, cur, j) => {
179-
acc[j] = cur;
180-
return acc;
181-
}, {});
182-
this.knnClassifier.setClassifierDataset(tensorsData);
183-
if (callback) {
184-
callback();
185-
}
165+
async load(pathOrData, callback) {
166+
let data;
167+
if (typeof pathOrData === 'object') {
168+
data = pathOrData;
169+
} else {
170+
data = await io.loadFile(pathOrData);
171+
}
172+
if (data) {
173+
const { dataset, tensors } = data;
174+
this.mapStringToIndex = Object.keys(dataset).map(key => dataset[key].label);
175+
const tensorsData = tensors
176+
.map((tensor, i) => {
177+
if (tensor) {
178+
const values = Object.keys(tensor).map(v => tensor[v]);
179+
return tf.tensor(values, dataset[i].shape, dataset[i].dtype);
180+
}
181+
return null;
182+
})
183+
.reduce((acc, cur, j) => {
184+
acc[j] = cur;
185+
return acc;
186+
}, {});
187+
this.knnClassifier.setClassifierDataset(tensorsData);
188+
if (callback) {
189+
callback();
186190
}
187-
});
191+
}
188192
}
189193
}
190194

src/utils/io.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ const saveBlob = async (data, name, type) => {
1616
const loadFile = async (path, callback) => fetch(path)
1717
.then(response => response.json())
1818
.then((json) => {
19-
callback(null, json);
19+
if (callback) {
20+
callback(null, json);
21+
}
2022
return json;
2123
})
2224
.catch((error) => {
23-
callback(error);
25+
if (callback) {
26+
callback(error);
27+
}
2428
console.error(`There has been a problem loading the file: ${error.message}`);
2529
throw error;
2630
});

0 commit comments

Comments
 (0)