-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDTReader.py
61 lines (44 loc) · 1.47 KB
/
SDTReader.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
import numpy as np
def load(filename):
out = dict()
with open(filename, 'r') as f:
lines = f.readlines()
i = 0
while i < len(lines):
line = lines[i].strip()
words = line.split(' ')
if len(words) == 0 or words[0] == '':
i += 1
continue
if words[0] == '@matrix':
if len(words) is not 4:
raise Exception("Syntax error on line {0}: Expected '@matrix <name> <nrows> <ncols>'.".format(i+1))
name = words[1]
nrows = int(words[2])
ncols = int(words[3])
out[name] = loadMatrix(lines[(i+1):], nrows, ncols)
i += nrows
elif words[0] == "@string":
if len(words) is not 3:
raise Exception("Syntax error on line {0}: Expected '@string <name> <length>'.".format(i+1))
name = words[1]
length = words[2]
out[name] = loadString(lines[(i+1):], length)
i += 1
else:
raise Exception("Unrecognized data type on line {0}: '{1}'.".format(i+1, words[0]))
i += 1
return out
def loadMatrix(lines, nrows, ncols):
mat = np.zeros((nrows, ncols))
for i in range(0, nrows):
line = lines[i].split(' ')
for j in range(0, ncols):
mat[i,j] = float(line[j])
return mat
def loadString(lines, length):
s = ""
line = lines[0].split(' ')
for word in line:
s += chr(int(word))
return s