Repository navigation
Expand file tree
/
Copy pathmatrixmarket.js
More file actions
362 lines (344 loc) · 10 KB
/
Copy pathmatrixmarket.js
File metadata and controls
362 lines (344 loc) · 10 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
'use strict';
var fs = require('fs'),
zlib = require('zlib'),
tar = require('tar'),
math = require('../index'),
Q = require('q'),
typed = require('typed-function'),
Spa = math.type.Spa,
DenseMatrix = math.type.DenseMatrix,
SparseMatrix = math.type.SparseMatrix,
FibonacciHeap = math.type.FibonacciHeap;
var _importFromStream = function (stream, deferred) {
// header regex
var headerRegex = /%%MatrixMarket ([a-zA-Z]+) ([a-zA-Z]+) ([a-zA-Z]+) ([a-zA-Z]+)/;
var coordinateHeaderRegex = /(\d+) (\d+) (\d+)/;
var coordinateDataRegex = /(\d+) (\d+) (.*)/;
var coordinatePatternRegex = /(\d+) (\d+)/;
var arrayHeaderRegex = /(\d+) (\d+)/;
var arrayDataRegex = /(\d+)/;
// Matrix Market supported formats
var typecodes = ['matrix'];
var formats = ['coordinate', 'array'];
var datatypes = ['real', 'pattern'];
var qualifiers = ['general', 'symmetric'];
// matrix data
var mm = null;
var buffer = '';
var readHeader = function (line) {
// check line is a header
var matches = line.match(headerRegex);
if (matches !== null) {
// get matches values
var typecode = matches[1];
var format = matches[2];
var datatype = matches[3];
var qualifier = matches[4];
// check typecode
if (typecodes.indexOf(typecode) === -1) {
// typecode not supported
deferred.reject(new Error('Matrix Market type code is not supported: ' + typecode));
// close stream
stream.close();
}
// check format
if (formats.indexOf(format) === -1) {
// typecode not supported
deferred.reject(new Error('Matrix Market format is not supported: ' + format));
// close stream
stream.close();
}
// check datatype
if (datatypes.indexOf(datatype) === -1) {
// typecode not supported
deferred.reject(new Error('Matrix Market datatype is not supported: ' + datatype));
// close stream
stream.close();
}
if (qualifiers.indexOf(qualifier) === -1) {
// typecode not supported
deferred.reject(new Error('Matrix Market qualifier is not supported: ' + qualifier));
// close stream
stream.close();
}
// initialize matrix market structure
mm = {
typecode: typecode,
format: format,
datatype: datatype,
qualifier: qualifier,
data: null
};
}
else {
// invalid header
deferred.reject(new Error('Invalid file header: ' + line));
// close stream
stream.close();
}
};
var readStructure = function (line) {
// vars
var matches;
// check matrix format
switch (mm.format) {
case 'coordinate':
// rows columns entries
matches = line.match(coordinateHeaderRegex);
if (matches !== null) {
// read structure
8000
mm.rows = parseInt(matches[1]);
mm.columns = parseInt(matches[2]);
mm.entries = parseInt(matches[3]);
// initialize data
mm.data = new FibonacciHeap();
}
break;
case 'array':
matches = line.match(arrayHeaderRegex);
if (matches !== null) {
// read structure
mm.rows = parseInt(matches[1]);
mm.columns = parseInt(matches[2]);
// initialize data
mm.data = [];
}
break;
}
};
var readValue = function (text) {
// check datatype
switch (mm.datatype) {
case 'real':
return parseFloat(text);
case 'pattern':
return 1;
}
};
var readData = function (line) {
// vars
var matches;
// check matrix format
switch (mm.format) {
case 'coordinate':
// regex to use
var rx = mm.datatype !== 'pattern' ? coordinateDataRegex : coordinatePatternRegex;
// check data line is correct
matches = line.match(rx);
if (matches !== null) {
// row, columns, value
var r = parseInt(matches[1]) - 1;
var c = parseInt(matches[2]) - 1;
var v = readValue(matches.length === 4 ? matches[3] : null);
// insert entry
mm.data.insert(c, {i: r, j: c, v: v});
// check matrix is simmetric
if (mm.qualifier === 'symmetric' && c !== r) {
// insert entry
mm.data.insert(r, {i: c, j: r, v: v});
}
}
break;
case 'array':
// check data line is correct
matches = line.match(arrayDataRegex);
if (matches !== null) {
// get values in row
var values = [];
for (var j = 1; j < matches.length; j++)
values.push(readValue(matches[j]));
// push entry
mm.data.push(values);
}
break;
}
};
var processLine = function (line) {
// check this is the first line
if (mm !== null) {
// skip all comments
if (line.charAt(0) !== '%') {
// check data is ready to be processed
if (mm.data !== null) {
// it is a data row
readData(line);
}
else {
// read matrix structure
readStructure(line);
}
}
}
else {
// read header, initialize data
readHeader(line);
}
};
stream.on('data', function (chunk) {
// concatenate chunk
buffer += chunk;
// eol
var index = buffer.indexOf('\n');
// process lines
while (index !== -1) {
// extract line
var line = buffer.substr(0, index);
// process line
processLine(line.trim());
// update buffer
buffer = buffer.length > index ? buffer.substr(index + 1) : '';
// next line
index = buffer.indexOf('\n');
}
});
stream.on('end', function () {
// check mm
if (mm !== null) {
// process matrix format
switch (mm.format) {
case 'coordinate':
// CCS structure
var values = mm.datatype !== 'pattern' ? [] : undefined;
var index = [];
var ptr = [];
var datatype = mm.datatype === 'real' ? 'number' : undefined;
// mm data & pointer
var d = mm.data;
var p = -1;
var spa = new Spa(mm.rows);
// push value
var pushValue = function (i, v) {
// push row
index.push(i);
// check there is a value (pattern matrix)
if (values)
values.push(v);
};
// extract node (column sorted)
var n = d.extractMinimum();
// loop all nodes
while (n !== null) {
// check column changed
if (p !== n.key) {
// process sparse accumulator
spa.forEach(0, mm.rows, pushValue);
// process columns from p + 1 to n.j
for (var j = p + 1; j <= n.key; j++) {
// ptr update
ptr.push(index.length);
}
// create sparse accumulator
spa = new Spa(mm.rows);
// reset p
p = n.key;
}
// store value in spa
spa.set(n.value.i, n.value.v);
// extract node
n = d.extractMinimum();
}
// process sparse accumulator
spa.forEach(0, mm.rows, pushValue);
// ptr update
ptr.push(index.length);
// resolve promise
deferred.resolve(new SparseMatrix({
values: values,
index: index,
ptr: ptr,
size: [mm.rows, mm.columns],
datatype: datatype
}));
break;
case 'array':
// resolve promise
deferred.resolve(new DenseMatrix({
data: mm.data,
size: [mm.rows, mm.columns]
}));
break;
}
}
});
stream.on('error', function (e) {
// reject promise
deferred.reject(new Error(e));
});
};
var _importFile = function (filename, deferred) {
return function () {
// input stream
var input = fs.createReadStream(filename);
// import from stream
_importFromStream(input, deferred);
};
};
/**
* Imports a Matrix Market matrix from the filesystem. (http://math.nist.gov/MatrixMarket/)
*/
var _import = typed('importMatrix', {
'Array': function (files) {
// array of promises
var promises = [];
// loop files
for (var i = 0; i < files.length; i++) {
// file name
var filename = files[i];
// create deferred instance
var deferred = Q.defer();
// check file exists, import file
fs.exists(filename, _importFile(filename, deferred));
// import file
promises.push(deferred.promise);
}
// return promise
return Q.all(promises);
},
'string, Array': function (archive, files) {
// array of deferrred & promises
var deferred = [];
var promises = [];
// initialize promises
for (var i = 0; i < files.length; i++) {
var d = Q.defer();
deferred[i] = d;
promises[i] = d.promise;
}
// check archive exists
fs.exists(archive, function () {
// input stream
var input = fs.createReadStream(archive);
// gz
input = input.pipe(zlib.createUnzip());
// tar
input = input.pipe(new tar.Parse());
// process entries
input.on('entry', function (e) {
// check we need to process entry
var index = files.indexOf(e.path);
if (index !== -1) {
// current deferred instance
var d = deferred[index];
// process entry
_importFromStream(e, d);
}
});
// error
input.on('error', function (e) {
// check error is in one of the files
var index = files.indexOf(e.path);
if (index !== -1) {
// current deferred instance
var d = deferred[index];
// reject promise with error
d.reject(new Error(e));
}
});
});
// return promise
return Q.all(promises);
}
});
module.exports = {
import: _import
};