-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCommunication.js
232 lines (198 loc) · 7.35 KB
/
Communication.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* @License Starts
*
* Copyright © 2015 - present. MongoExpUser
*
* License: MIT - See: https://github.com/MongoExpUser/Shale-Reservoir-DNN-and-Drilling-Rare-Events-Graph/blob/master/README.md
*
* @License Ends
*
* ...Ecotert's Communication.js (released as open-source under MIT License) implements:
*
* Relevant communications/data transmission/formatting methods - e.g.:
*
* (1) date format
* (2) check file paths and existence
* (3) read CSV files
* (4) get tensor statistics
* (5) read/write files on disk
*
*/
class Communication
{
constructor()
{
return null;
}
static commonLongDateFormat()
{
return {
weekday: "long", year: "numeric",
month: "long", day: "numeric",
hour: "2-digit", minute: "2-digit",
second: "2-digit"
}
}
static isFile(filePath, fileName)
{
const path = require('path');
const fileExt = path.extname(filePath + fileName);
if(!fileExt)
{
return false;
}
if(fileExt)
{
return true;
}
}
readInputCSVfile(pathTofile)
{
function loadFile(filetoRead)
{
const fs = require('fs');
return fs.readFileSync(filetoRead).toString();
}
function castCSVToArray(inputCsvFile)
{
var finalArraydata = [];
var splitRows = inputCsvFile.split(/\r?\n|\r/);
for (var i in splitRows)
{
finalArraydata.push(splitRows[i].split(','));
}
return finalArraydata;
}
function castCSVToArrayToNumeric(input, removeHeader=false)
{
var output = [];
for(var i in input)
{
var element = input[i];
var elementOut = [];
for(var j in element)
{
var value = parseFloat(element[j])
var valid = !isNaN(value);
if(valid === true)
{
elementOut.push(value);
}
else
{
elementOut.push(element[j]);
}
}
output.push(elementOut);
}
if(removeHeader === true)
{
output.shift();
}
return output;
}
return castCSVToArrayToNumeric(castCSVToArray(loadFile(pathTofile)), true);
}
getStatisticsOfTensor(csvFileArrayOutput, printcsvFileArrayOutput=false)
{
console.log("converted/formatted output");
if(printcsvFileArrayOutput === true)
{
console.log(csvFileArrayOutput);
}
console.log("inputDim: ", csvFileArrayOutput.length);
console.log("inputSize: ", csvFileArrayOutput[0].length);
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
const csvFileArrayOutputToTensor = tf.tensor2d(csvFileArrayOutput);
csvFileArrayOutputToTensor.print(true);
console.log(csvFileArrayOutputToTensor);
return csvFileArrayOutputToTensor;
}
writeReadFile(filePath, outputFileName, readInputFile, unlink, streamOption)
{
const fs = require('fs');
const filePathOut = filePath + outputFileName;
if(streamOption === false)
{
// option 1: regular "writeFile" and "readFile" in async mode
//write file to folder to get instance to object/file to used (consumed) i.e. to be read
fs.writeFile(filePathOut, readInputFile, function(writeError)
{
if(writeError)
{
console.log(writeError);
return;
}
console.log('Finished writing/saving with "writeFile" !.....');
if(!writeError)
{
//read file from written file above for other purposes e.g. downloading from server-to-client or vice versa
fs.readFile(filePathOut, function(readError)
{
if(readError)
{
console.log(readError);
return;
}
console.log('Finished reading with "readFile" !.....');
if(unlink === true)
{
fs.unlink(filePathOut, function (linkError)
{
if(linkError && linkError.code === 'ENOENT')
{
console.log('File does not exist or already deleted !.....');
}
});
}
});
}
});
}
else
{
//option 2: using "WriteStream" and "ReadStream" -> better option, b'cos of better memory management
//write-stream file to folder that can be read later
const options = {'bufferSize': 2048};
const ws = fs.createWriteStream(filePathOut, options); //create file;
ws.write(Buffer.from(readInputFile)); //write to file
ws.on('error', function(writeError)
{
console.log(writeError);
return;
});
ws.on('finish', function()
{
console.log('Finished writing/saving with "WriteStream" !.....');
});
ws.end(function ()
{
//call end to flush out stream and prevent:
//"Error: ENOSPC: no space left on device, write"
console.log('End writing/saving with "WriteStream" !.....');
});
//read-stream existing written file (fom above) to be downloaded from server-to-client, vice versa, etc.
var rs = fs.createReadStream(filePathOut, options).pipe(ws);
rs.on('error', function(readError)
{
console.log(readError);
return;
});
rs.on('finish', function()
{
console.log('Finished reading file with "ReadStream" !.....');
if(unlink === true)
{
fs.unlink(filePathOut, function (linkError)
{
if(linkError && linkError.code === 'ENOENT')
{
console.log('File does not exist or already deleted !.....');
}
});
}
});
}
}
}
module.exports = {Communication};