-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathio_utils.py
More file actions
69 lines (51 loc) · 1.75 KB
/
io_utils.py
File metadata and controls
69 lines (51 loc) · 1.75 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
import numpy as np
import csv
from scipy.sparse import csr_matrix
from scipy.io import loadmat
import os
import errno
def matlab_matrix(path, **kwargs):
return loadmat(path, **kwargs)
def regular_matrix(path, **kwargs):
return np.loadtxt(path, **kwargs)
def numpy_matrix(path, **kwargs):
return np.load(path, **kwargs)
def sparse_matrix(path, column_count, delimiter=' ', index_value_delimiter=':'):
indptr = [0]
indices = []
data = []
shape = [0, column_count]
with open(path) as csvFile:
reader = csv.reader(csvFile, delimiter=delimiter)
for row in reader:
indptr.append(indptr[-1] + len(row))
shape[0] += 1
for i in range(len(row)):
try:
index, value = row[i].split(index_value_delimiter)
indices.append(int(index))
data.append(float(value))
except ValueError:
indptr[-1] -= 1
return csr_matrix((np.array(data), indices, indptr), shape).T.toarray()
def sparse_binary_matrix(path, column_count, delimiter=' '):
indptr = [0]
indices = []
shape = [0, column_count]
with open(path) as csvFile:
reader = csv.reader(csvFile, delimiter=delimiter)
for row in reader:
indptr.append(indptr[-1] + len(row))
shape[0] += 1
for i in range(len(row)):
if row[i]:
indices.append(int(row[i]))
else:
indptr[-1] -= 1
return csr_matrix((np.ones(len(indices)), indices, indptr), shape).T.toarray()
def mkdir(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise