Repository navigation
Expand file tree
/
Copy pathMLPDataFile.mqh
More file actions
325 lines (305 loc) · 8.46 KB
/
Copy pathMLPDataFile.mqh
File metadata and controls
325 lines (305 loc) · 8.46 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* https://github.com/Roffild/RoffildLibrary
*/
#include <Math/Alglib/matrix.mqh>
/**
* MLPDataFile = CSV in a binary format
*
* The byte order is Little-Endian.
*
* Format:
* @code
* int - NIn (only for Alglib_MultilayerPerceptron and Alglib_RandomForest)
* int - NOut (only for Alglib_MultilayerPerceptron and Alglib_RandomForest)
* int - header size
* if (header size > 0) {
* int - length of string
* byte[] - UTF-16LE
* ...
* }
* int - data size
* double[data size] - data array
* ...
* @endcode
*/
class CMLPDataFile
{
protected:
/**
* @param file path of file
* @param[in] nin NIn
* @param[in] nout NOut
* @param[in] _header
* @return handleFile
*/
int initWrite0(const string file, const int nin, const int nout, const string &_header[])
{
path = file;
handleFile = FileOpen(path, FILE_BIN|FILE_WRITE|FILE_COMMON);
if (handleFile != INVALID_HANDLE) {
FileWriteInteger(handleFile, nin);
FileWriteInteger(handleFile, nout);
const int size = ArraySize(_header);
FileWriteInteger(handleFile, size);
for (int x = 0; x < size; x++) {
FileWriteInteger(handleFile, StringLen(_header[x]));
FileWriteString(handleFile, _header[x]);
}
}
return handleFile;
}
/**
* @param file path of file
* @param[out] nin NIn
* @param[out] nout NOut
* @return handleFile
*/
int initRead0(const string file, int &nin, int &nout)
{
path = file;
handleFile = FileOpen(path, FILE_BIN|FILE_READ|FILE_SHARE_READ|FILE_COMMON);
if (handleFile != INVALID_HANDLE) {
nin = FileReadInteger(handleFile);
nout = FileReadInteger(handleFile);
const int size = FileReadInteger(handleFile);
ArrayResize(header, size);
for (int x = 0; x < size; x++) {
header[x] = FileReadString(handleFile, FileReadInteger(handleFile));
}
}
return handleFile;
}
public:
int handleFile;
string path;
string header[];
CMLPDataFile() : handleFile(INVALID_HANDLE)
{}
~CMLPDataFile()
{
close();
}
/**
* @param file number of file
* @param[in] nin NIn
* @param[in] nout NOut
* @return handleFile
*/
int initWrite(const int file, const int nin, const int nout)
{
ArrayResize(header, 0);
return initWrite0("MLPData/mlp_" + (string)(file) + ".bin", nin, nout, header);
}
/**
* @param file number of file
* @param[in] nin NIn
* @param[in] nout NOut
* @param[in] _header
* @return handleFile
*/
int initWrite(const int file, const int nin, const int nout, const string &_header[])
{
return initWrite0("MLPData/mlp_" + (string)(file) + ".bin", nin, nout, _header);
}
/**
* @param file number of file
* @param[in] nin NIn
* @param[in] nout NOut
* @return handleFile
*/
int initWriteValidation(const int file, const int nin, const int nout)
{
ArrayResize(header, 0);
return initWrite0("MLPData/mlp_" + (string)(file) + "_validation.bin", nin, nout, header);
}
/**
* @param file number of file
* @param[in] nin NIn
* @param[in] nout NOut
* @param[in] _header
* @return handleFile
*/
int initWriteValidation(const int file, const int nin, const int nout, const string &_header[])
{
return initWrite0("MLPData/mlp_" + (string)(file) + "_validation.bin", nin, nout, _header);
}
/**
* @param file number of file
* @param[out] nin NIn
* @param[out] nout NOut
* @return handleFile
*/
int initRead(const int file, int &nin, int &nout)
{
return initRead0("MLPData/mlp_" + (string)(file) + ".bin", nin, nout);
}
/**
* @param file number of file
* @param[out] nin NIn
* @param[out] nout NOut
* @return handleFile
*/
int initReadValidation(const int file, int &nin, int &nout)
{
return initRead0("MLPData/mlp_" + (string)(file) + "_validation.bin", nin, nout);
}
/**
* @return Number of recorded items.
*/
uint write(const double &data[])
{
const int size = ArraySize(data);
if (size > 0 && handleFile != INVALID_HANDLE) {
FileWriteInteger(handleFile, size);
return FileWriteArray(handleFile, data);
}
return 0;
}
/**
* @return Number of recorded items.
*/
uint write(const CRowDouble &data)
{
uint count = 0;
if (handleFile != INVALID_HANDLE) {
double array[];
convert(data, array);
count += write(array);
}
return count;
}
/**
* @return Number of recorded items.
*/
uint writeAll(const CMatrixDouble &data)
{
uint count = 0;
if (handleFile != INVALID_HANDLE) {
const int size = data.Size();
if (size > 0) {
if (data[0].Size() > 0) {
for (int x = 0; x < size; x++) {
count += write(data[x]);
}
}
}
}
return count;
}
/**
* @return Number of elements read.
*/
uint read(double &data[], int &size)
{
if (handleFile != INVALID_HANDLE && FileIsEnding(handleFile) == false) {
size = FileReadInteger(handleFile);
return FileReadArray(handleFile, data, 0, size);
}
size = 0;
return 0;
}
/**
* @return Number of elements read.
*/
uint readAll(CMatrixDouble &data, const bool append = false)
{
const int reserve = 50000;
uint count = 0;
if (handleFile != INVALID_HANDLE) {
int size1 = 0;
int size2 = 0, sz = 0;
if (append) {
size1 = data.Size();
if (size1 > 0) {
size2 = data[0].Size();
}
}
double dt[];
data.Resize(size1 + reserve, size2);
while (read(dt, sz) == sz && sz > 0) {
size1++;
if (sz > size2) {
size2 = sz;
}
if ((size1 % reserve) == 0) {
data.Resize(size1 + reserve, size2);
}
data[size1 - 1] = dt;
count += sz;
}
data.Resize(size1, size2);
}
return count;
}
static void convert(const CRowDouble &data, double &array[])
{
const int size = data.Size();
ArrayResize(array, size);
if (size > 0) {
for (int y = 0; y < size; y++) {
array[y] = data[y];
}
}
}
void flush()
{
if (handleFile != INVALID_HANDLE) {
FileFlush(handleFile);
}
}
void close()
{
if (handleFile != INVALID_HANDLE) {
FileClose(handleFile);
handleFile = INVALID_HANDLE;
}
}
static bool convertToCsv(const int file, const bool validation = false, const string delimiter = ";")
{
CMLPDataFile mlpfile();
int nin, nout;
if (validation) {
mlpfile.initReadValidation(file, nin, nout);
} else {
mlpfile.initRead(file, nin, nout);
}
if (mlpfile.handleFile == INVALID_HANDLE) {
return false;
}
string pcsv = mlpfile.path;
StringReplace(pcsv, ".bin", ".csv");
int hcsv = FileOpen(pcsv, FILE_CSV|FILE_ANSI|FILE_WRITE|FILE_COMMON, delimiter);
if (hcsv != INVALID_HANDLE) {
// BUG FileWriteArray not support CSV
double data[];
int x, size;
if ((size = ArraySize(mlpfile.header)) > 0) {
for (x = 0; x < (size - 1); x++) {
FileWriteString(hcsv, mlpfile.header[x] + delimiter);
}
FileWrite(hcsv, mlpfile.header[size - 1]);
}
while (mlpfile.read(data, size) > 0) {
for (x = 0; x < (size - 1); x++) {
FileWriteString(hcsv, DoubleToString(data[x]) + delimiter);
}
FileWrite(hcsv, DoubleToString(data[size - 1]));
}
FileClose(hcsv);
return true;
}
return false;
}
};