-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
190 lines (162 loc) · 4.02 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import express from "express";
import fs, { write } from "fs";
import npyjs from "npyjs";
import path from "path";
import { fileURLToPath } from "url";
import ndarray from "ndarray";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function getFilePath() {
const path = process.argv[2];
if (path === undefined) {
return undefined;
}
// Default to json data format
if (!path.slice(1).includes(".")) {
return path + ".json";
}
return path;
}
async function fileExists(file) {
try {
await fs.promises.access(file, fs.constants.F_OK);
return true;
} catch {
return false;
}
}
function storeWorkingJSON(data) {
fs.writeFile("data/temp/temp.json", data, "utf8", (err) => {
if (err) {
console.log(`Error writing file: ${err}`);
}
});
}
function loadJSON(path) {
fs.readFile(path, "utf8", (err, data) => {
if (err) {
console.log(`Error reading file from disk: ${err}`);
return;
}
storeWorkingJSON(data);
});
}
function ndArrayToArray(ndArray) {
switch (ndArray.shape.length) {
case 1:
return ndArrayToArray1D(ndArray);
case 2:
return ndArrayToArray2D(ndArray);
case 3:
return ndArrayToArray3D(ndArray);
default:
throw new Error("Only ndArrays with 1, 2 or 3 dimensions are supported.");
}
}
function ndArrayToArray1D(ndArray) {
return Array.from(ndArray.data);
}
function ndArrayToArray2D(ndArray) {
const arr = new Array(ndArray.shape[0]).fill(
new Array(ndArray.shape[1]).fill(0)
);
for (let i = 0; i < ndArray.shape[0]; i++) {
for (let j = 0; j < ndArray.shape[1]; j++) {
arr[i][j] = ndArray.get(i, j);
}
}
return arr;
}
function ndArrayToArray3D(ndArray) {
const arr = new Array(ndArray.shape[0]).fill(
new Array(ndArray.shape[1]).fill(new Array(ndArray.shape[2]).fill(0))
);
for (let i = 0; i < ndArray.shape[0]; i++) {
for (let j = 0; j < ndArray.shape[1]; j++) {
for (let k = 0; k < ndArray.shape[2]; k++) {
arr[i][j][k] = ndArray.get(i, j, k);
}
}
}
return arr;
}
function loadNumPy(filePath) {
const n = new npyjs();
const buf = fs.readFileSync(filePath);
const npyData = n.parse(buf.buffer.slice(0, buf.buffer.length));
const npyArray = ndarray(npyData.data, npyData.shape);
const data = JSON.stringify(ndArrayToArray(npyArray));
storeWorkingJSON(data);
}
function loadNumPyWithPython(filePath) {
// Run script to load target .npy file and save it in json format.
PythonShell.run(
"scripts/load.py",
{ args: [filePath] },
function (err, results) {
if (err) {
console.log(err, results);
}
}
);
}
function convertToJSON(filePath) {
if (filePath === undefined) {
return;
}
const extension = getFileExtension(filePath);
switch (extension) {
case "json":
loadJSON(filePath);
break;
case "npy":
case "npz":
// Load target .npy file and save it in json format.
loadNumPy(filePath);
break;
default:
throw new Error("File type not supported.");
}
}
function getFileExtension(filePath) {
return filePath.split(".").slice(-1)[0];
}
function run(guiEnabled) {
const app = express();
const port = process.env.PORT || 8080;
app.use(express.static(__dirname + "/public"));
app.get("/", function (req, res) {
res.render("index.html");
});
// To return whether GUI version enabled.
app.get("/gui", function (req, res) {
res.send(guiEnabled);
});
if (!guiEnabled) {
// To return array data read from file.
app.get("/data", function (req, res) {
const readable = fs.createReadStream("data/temp/temp.json");
readable.pipe(res);
});
}
app.listen(port);
console.log("Server started at: http://localhost:" + port);
}
function isGUIEnabled() {
// GUI enabled if no file path argument has been provided.
return process.argv.length != 3;
}
const guiEnabled = isGUIEnabled();
if (guiEnabled) {
run(guiEnabled);
} else {
const filePath = getFilePath();
fileExists(filePath).then((exists) => {
if (exists) {
convertToJSON(filePath); // Save target data into data/temp.json.
run(guiEnabled);
} else {
console.log(`Data file path ${filePath} is invalid.`);
}
});
}