/
io.py
578 lines (483 loc) · 20.5 KB
/
io.py
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
import numpy as np
from numpy.lib import format
from pycompss.api.api import compss_wait_on
from pycompss.api.constraint import constraint
from pycompss.api.parameter import Depth, COLLECTION_INOUT, COLLECTION_OUT, \
Type, FILE_IN, IN_DELETE, COLLECTION_FILE_IN
from pycompss.api.task import task
import os
from dislib.data.array import Array
from math import ceil
_CRD_LINE_SIZE = 81
def load_svmlight_file(path, block_size, n_features, store_sparse):
""" Loads a SVMLight file into a distributed array.
Parameters
----------
path : string
File path.
block_size : tuple (int, int)
Size of the blocks for the output ds-array.
n_features : int
Number of features.
store_sparse : boolean
Whether to use scipy.sparse data structures to store data. If False,
numpy.array is used instead.
Returns
-------
x, y : (ds-array, ds-array)
A distributed representation (ds-array) of the X and y.
"""
n, m = block_size
lines = []
x_blocks, y_blocks = [], []
n_rows = 0
with open(path, "r") as f:
for line in f:
n_rows += 1
lines.append(line.encode())
if len(lines) == n:
# line 0 -> X, line 1 -> y
out_blocks = Array._get_out_blocks((1, ceil(n_features / m)))
out_blocks.append([object()])
# out_blocks.append([])
_read_svmlight(lines, out_blocks, col_size=m,
n_features=n_features,
store_sparse=store_sparse)
# we append only the list forming the row (out_blocks depth=2)
x_blocks.append(out_blocks[0])
y_blocks.append(out_blocks[1])
lines = []
if lines:
out_blocks = Array._get_out_blocks((1, ceil(n_features / m)))
out_blocks.append([object()])
_read_svmlight(lines, out_blocks, col_size=m,
n_features=n_features, store_sparse=store_sparse)
# we append only the list forming the row (out_blocks depth=2)
x_blocks.append(out_blocks[0])
y_blocks.append(out_blocks[1])
x = Array(x_blocks, top_left_shape=block_size, reg_shape=block_size,
shape=(n_rows, n_features), sparse=store_sparse)
# y has only a single line but it's treated as a 'column'
y = Array(y_blocks, top_left_shape=(n, 1), reg_shape=(n, 1),
shape=(n_rows, 1), sparse=False)
return x, y
def load_txt_file(path, block_size, discard_first_row=False,
col_of_index=False, delimiter=","):
""" Loads a text file into a distributed array.
Parameters
----------
path : string
File path.
block_size : tuple (int, int)
Size of the blocks of the array.
discard_first_row : bool
Boolean that indicates if the first row should be discarded.
col_of_index : bool
Boolean that indicates if the first column is a column
of indexes and therefore it should be discarded.
delimiter : string, optional (default=",")
String that separates columns in the file.
Returns
-------
x : ds-array
A distributed representation of the data divided in blocks.
"""
with open(path, "r") as f:
first_line = f.readline().strip()
n_cols = len(first_line.split(delimiter))
n_blocks = ceil(n_cols / block_size[1])
blocks = []
lines = []
n_lines = 0
with open(path, "r") as f:
if discard_first_row:
f.readline()
for line in f:
n_lines += 1
lines.append(line.encode())
if len(lines) == block_size[0]:
out_blocks = [object() for _ in range(n_blocks)]
_read_lines(lines, block_size[1], delimiter, out_blocks,
col_of_index=col_of_index)
blocks.append(out_blocks)
lines = []
if lines:
out_blocks = [object() for _ in range(n_blocks)]
_read_lines(lines, block_size[1], delimiter, out_blocks,
col_of_index=col_of_index)
blocks.append(out_blocks)
if col_of_index:
n_cols = n_cols - 1
return Array(blocks, top_left_shape=block_size, reg_shape=block_size,
shape=(n_lines, n_cols), sparse=False)
def load_npy_file(path, block_size):
""" Loads a file in npy format (must be 2-dimensional).
Parameters
----------
path : str
Path to the npy file.
block_size : tuple (int, int)
Block size of the resulting ds-array.
Returns
-------
x : ds-array
"""
try:
fid = open(path, "rb")
version = format.read_magic(fid)
format._check_version(version)
shape, fortran_order, dtype = format._read_array_header(fid, version)
if fortran_order:
raise ValueError("Fortran order not supported for npy files")
if len(shape) != 2:
raise ValueError("Array is not 2-dimensional")
if block_size[0] > shape[0] or block_size[1] > shape[1]:
raise ValueError("Block size is larger than the array")
blocks = []
n_blocks = int(ceil(shape[1] / block_size[1]))
for i in range(0, shape[0], block_size[0]):
read_count = min(block_size[0], shape[0] - i)
read_size = int(read_count * shape[1] * dtype.itemsize)
data = fid.read(read_size)
out_blocks = [object() for _ in range(n_blocks)]
_read_from_buffer(data, dtype, shape[1], block_size[1], out_blocks)
blocks.append(out_blocks)
return Array(blocks=blocks, top_left_shape=block_size,
reg_shape=block_size, shape=shape, sparse=False)
finally:
fid.close()
def load_mdcrd_file(path, block_size, n_atoms, copy=False):
""" Loads a mdcrd trajectory file into a distributed array.
Parameters
----------
path : string
File path.
block_size : tuple (int, int)
Size of the blocks of the array.
n_atoms : int
Number of atoms in the trajectory. Each frame in the mdcrd file has
3*n_atoms float values (corresponding to 3-dimensional position).
copy : boolean, default=False
Send the file to every task, as opposed to reading it once in the
master program.
Returns
-------
x : ds-array
A distributed representation of the data divided in blocks.
"""
n_coord = 3
line_length = 10
bytes_per_value = 6
bytes_per_gap = 2
n_cols = n_atoms * n_coord
n_hblocks = ceil(n_cols / block_size[1])
lines_per_snap = ceil((n_atoms * n_coord) / line_length)
last_line_length = n_cols % 10
last_line_size = last_line_length * bytes_per_value + \
(last_line_length - 1) * bytes_per_gap + 3
bytes_per_snap = (lines_per_snap - 1) * _CRD_LINE_SIZE + last_line_size
bytes_per_block = block_size[0] * bytes_per_snap
if not copy:
return _load_mdcrd(path, block_size, n_cols, n_hblocks,
bytes_per_snap, bytes_per_block)
else:
return _load_mdcrd_copy(path, block_size, n_cols, n_hblocks,
bytes_per_snap, bytes_per_block)
def load_hstack_npy_files(path, cols_per_block=None):
""" Loads the .npy files in a directory into a ds-array, stacking them
horizontally, like (A|B|C). The order of concatenation is alphanumeric.
At least 1 valid .npy file must exist in the directory, and every .npy file
must contain a valid array. Every array must have the same dtype, order,
and number of rows.
The blocks of the returned ds-array will have the same number of rows as
the input arrays, and cols_per_block columns, which defaults to the number
of columns of the first array.
Parameters
----------
path : string
Folder path.
cols_per_block : tuple (int, int)
Number of columns of the blocks for the output ds-array. If None, the
number of columns of the first array is used.
Returns
-------
x : ds-array
A distributed representation (ds-array) of the stacked arrays.
"""
dirlist = os.listdir(path)
folder_paths = [os.path.join(path, name) for name in sorted(dirlist)]
# Full path of .npy files in the folder
files = [pth for pth in folder_paths
if os.path.isfile(pth) and pth[-4:] == '.npy']
# Read the header of the first file to get shape, order, and dtype
with open(files[0], "rb") as fid:
version = format.read_magic(fid)
format._check_version(version)
shape0, order0, dtype0 = format._read_array_header(fid, version)
rows = shape0[0]
if cols_per_block is None:
cols_per_block = shape0[1]
# Check that all files have the same number of rows, order and datatype,
# and store the number of columns for each file.
files_cols = [shape0[1]]
for filename in files[1:]:
with open(filename, "rb") as fid:
version = format.read_magic(fid)
format._check_version(version)
shape, order, dtype = format._read_array_header(fid, version)
if shape[0] != shape0[0] or order0 != order or dtype0 != dtype:
raise AssertionError()
files_cols.append(shape[1])
# Compute the parameters block_files, start_col and end_col for each block,
# and call the task _load_hstack_npy_block() to generate each block.
blocks = []
file_idx = 0
start_col = 0
while file_idx < len(files):
block_files = [files[file_idx]]
cols = files_cols[file_idx] - start_col
while cols < cols_per_block: # while block not completed
if file_idx + 1 == len(files): # last file
break
file_idx += 1
block_files.append(files[file_idx])
cols += files_cols[file_idx]
# Compute end_col of last file in block (last block may be smaller)
end_col = files_cols[file_idx] - max(0, (cols - cols_per_block))
blocks.append(_load_hstack_npy_block(block_files, start_col, end_col))
if end_col == files_cols[file_idx]: # file completed
file_idx += 1
start_col = 0
else: # file uncompleted
start_col = end_col
return Array(blocks=[blocks], top_left_shape=(rows, cols_per_block),
reg_shape=(rows, cols_per_block),
shape=(rows, sum(files_cols)),
sparse=False)
def save_txt(arr, dir, merge_rows=False):
"""
Save a ds-array by blocks to a directory in txt format.
Parameters
----------
arr : ds-array
Array data to be saved.
dir : str
Directory into which the data is saved.
merge_rows : boolean, default=False
Merge blocks along rows before saving.
"""
os.makedirs(dir, exist_ok=True)
if merge_rows:
for i, h_block in enumerate(arr._iterator(0)):
path = os.path.join(dir, str(i))
np.savetxt(path, h_block.collect())
else:
for i, blocks_row in enumerate(arr._blocks):
for j, block in enumerate(blocks_row):
fname = '{}_{}'.format(i, j)
path = os.path.join(dir, fname)
block = compss_wait_on(block)
np.savetxt(path, block)
def save_npy_file(arr, directory, merge_rows=False):
"""
Save a ds-array by blocks to a directory in npy format.
Parameters
----------
arr : ds-array
Array data to be saved.
dir : str
Directory into which the data is saved.
merge_rows : boolean, default=False
Merge blocks along rows before saving.
"""
os.makedirs(directory, exist_ok=True)
if merge_rows:
for i, h_block in enumerate(arr._iterator(0)):
path = os.path.join(directory, str(i))
np.save(path, h_block.collect())
else:
for i, blocks_row in enumerate(arr._blocks):
for j, block in enumerate(blocks_row):
fname = '{}_{}'.format(i, j)
path = os.path.join(directory, fname)
block = compss_wait_on(block)
np.save(path, block)
def load_npy_files(path, shape=None):
""" Loads the .npy files in a directory into a ds-array, stacking them
in a way that the returned ds-array has the same shape as the one
specified on array_shape. The order of concatenation is alphanumeric.
At least 1 valid .npy file must exist in the directory, and every .npy
file must contain a valid array. Every array must have the same dtype,
order, and number of rows.
The blocks of the returned ds-array will have the same number of rows
as the input arrays, and cols_per_block columns, which defaults to the
number of columns of the first array.
Parameters
----------
path : string
Folder path.
shape : tuple (int, int)
Number of rows and columns that the returned ds-array will have.
Returns
-------
x : ds-array
A distributed representation (ds-array) of the stacked arrays.
"""
if shape is None:
raise ValueError("The shape of the final dsarray must be specified")
dirlist = os.listdir(path)
folder_paths = [os.path.join(path, name) for name in sorted(dirlist)]
files = [pth for pth in folder_paths
if os.path.isfile(pth) and pth[-4:] == '.npy']
with open(files[0], "rb") as fid:
version = format.read_magic(fid)
format._check_version(version)
shape0, order0, dtype0 = format._read_array_header(fid, version)
blocks = []
n_blocks0 = int(ceil(shape[0] / shape0[0]))
n_blocks1 = int(ceil(shape[1] / shape0[1]))
for i in range(n_blocks0):
blocks.append([])
for j in range(n_blocks1):
fname = '{}_{}.npy'.format(i, j)
file_to_load = os.path.join(path, fname)
blocks[-1].append(np.load(file_to_load))
return Array(blocks=blocks, top_left_shape=shape0,
reg_shape=shape0, shape=shape, sparse=False)
def load_blocks_rechunk(blocks, shape, block_size, new_block_size):
""" Loads the blocks contained in the parameter blocks into
an ds-array with reg_shape equal to the block_size specified.
The blocks are loaded respecting the specified shape for the array.
Finally a rechunk is performed on the ds-array in order to return
a ds-array with the block size specified in the parameter new_block_size
Parameters
----------
blocks : list()
List of the blocks to be set on the ds-array (They should be
Future objects).
shape : tuple (int, int)
Number of rows and columns that the returned ds-array will have.
block_size : tuple (int, int)
Number of rows and columns that each block will contain.
new_block_size : tuple (int, int)
Number of rows and columns that will contain each block after
the rechunk operation.
Returns
-------
x : ds-array
A distributed representation (ds-array) build with the list
of blocks, with the corresponding shape and with a reg_shape
set to new_block_size.
"""
if shape[0] < new_block_size[0] or shape[1] < new_block_size[1]:
raise ValueError("The block size requested for rechunk"
"is greater than the ds-array")
number_rows = int(shape[0] / block_size[0])
number_cols = int(shape[1] / block_size[1])
final_blocks = [[] for _ in range(number_rows)]
actual_col = 0
for i in range(number_rows):
for col in range(number_cols):
final_blocks[i].append(blocks[actual_col])
actual_col = actual_col + 1
arr = _load_blocks_array(final_blocks, shape, block_size)
return arr.rechunk(new_block_size)
@constraint(computing_units="${ComputingUnits}")
@task(out_blocks=COLLECTION_OUT)
def _read_from_buffer(data, dtype, shape, block_size, out_blocks):
arr = np.frombuffer(data, dtype=dtype)
arr = arr.reshape((-1, shape))
for i in range(len(out_blocks)):
out_blocks[i] = arr[:, i * block_size:(i + 1) * block_size]
@constraint(computing_units="${ComputingUnits}")
@task(out_blocks=COLLECTION_OUT)
def _read_lines(lines, block_size, delimiter, out_blocks, col_of_index=False):
samples = np.genfromtxt(lines, delimiter=delimiter)
if len(samples.shape) == 1:
samples = samples.reshape(1, -1)
if col_of_index:
for i, j in enumerate(range(0, samples.shape[1], block_size)):
out_blocks[i] = samples[:, j + 1:j + block_size + 1]
else:
for i, j in enumerate(range(0, samples.shape[1], block_size)):
out_blocks[i] = samples[:, j:j + block_size]
@constraint(computing_units="${ComputingUnits}")
@task(out_blocks={Type: COLLECTION_OUT, Depth: 2})
def _read_svmlight(lines, out_blocks, col_size, n_features, store_sparse):
from tempfile import SpooledTemporaryFile
from sklearn.datasets import load_svmlight_file
# Creating a tmp file to use load_svmlight_file method should be more
# efficient than parsing the lines manually
tmp_file = SpooledTemporaryFile(mode="wb+", max_size=2e8)
tmp_file.writelines(lines)
tmp_file.seek(0)
x, y = load_svmlight_file(tmp_file, n_features=n_features)
if not store_sparse:
x = x.toarray()
# tried also converting to csc/ndarray first for faster splitting but it's
# not worth. Position 0 contains the X
for i in range(ceil(n_features / col_size)):
out_blocks[0][i] = x[:, i * col_size:(i + 1) * col_size]
# Position 1 contains the y block
out_blocks[1][0] = y.reshape(-1, 1)
def _load_blocks_array(blocks, shape, block_size):
if shape[0] < block_size[0] or shape[1] < block_size[1]:
raise ValueError("The block size is greater than the ds-array")
return Array(blocks, shape=shape, top_left_shape=block_size,
reg_shape=block_size, sparse=False)
def _load_mdcrd_copy(path, block_size, n_cols, n_hblocks, bytes_per_snap,
bytes_per_block):
file_size = os.stat(path).st_size - _CRD_LINE_SIZE
blocks = []
for i in range(0, file_size, bytes_per_block):
out_blocks = [object() for _ in range(n_hblocks)]
_read_crd_file(path, i, bytes_per_block, block_size[1], n_cols,
out_blocks)
blocks.append(out_blocks)
n_samples = int(file_size / bytes_per_snap)
return Array(blocks, top_left_shape=block_size, reg_shape=block_size,
shape=(n_samples, n_cols), sparse=False)
def _load_mdcrd(path, block_size, n_cols, n_blocks, bytes_per_snap,
bytes_per_block):
blocks = []
file_size = os.stat(path).st_size - _CRD_LINE_SIZE
try:
fid = open(path, "rb")
fid.read(_CRD_LINE_SIZE) # skip header
for _ in range(0, file_size, bytes_per_block):
data = fid.read(bytes_per_block)
out_blocks = [object() for _ in range(n_blocks)]
_read_crd_bytes(data, block_size[1], n_cols, out_blocks)
blocks.append(out_blocks)
finally:
fid.close()
n_samples = int(file_size / bytes_per_snap)
return Array(blocks, top_left_shape=block_size, reg_shape=block_size,
shape=(n_samples, n_cols), sparse=False)
@constraint(computing_units="${ComputingUnits}")
@task(data=IN_DELETE, out_blocks=COLLECTION_INOUT)
def _read_crd_bytes(data, hblock_size, n_cols, out_blocks):
arr = np.fromstring(data.decode(), sep=" ")
arr = arr.reshape((-1, n_cols))
for i in range(len(out_blocks)):
out_blocks[i] = arr[:, i * hblock_size:(i + 1) * hblock_size]
@constraint(computing_units="${ComputingUnits}")
@task(path=FILE_IN, out_blocks=COLLECTION_INOUT)
def _read_crd_file(path, start, read_size, hblock_size, n_cols, out_blocks):
with open(path, "rb") as fid:
fid.seek(start + _CRD_LINE_SIZE) # skip header and go to start
data = fid.read(read_size)
arr = np.fromstring(data.decode(), sep=" ")
arr = arr.reshape((-1, n_cols))
for i in range(len(out_blocks)):
out_blocks[i] = arr[:, i * hblock_size:(i + 1) * hblock_size]
@constraint(computing_units="${ComputingUnits}")
@task(block_files=COLLECTION_FILE_IN)
def _load_hstack_npy_block(block_files, start_col, end_col):
if len(block_files) == 1:
return np.load(block_files[0])[:, start_col:end_col]
arrays = [np.load(block_files[0])[:, start_col:]]
for file in block_files[1:-1]:
arrays.append(np.load(file))
arrays.append(np.load(block_files[-1])[:, :end_col])
return np.concatenate(arrays, axis=1)