-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_util.py
58 lines (52 loc) · 1.7 KB
/
io_util.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
def write_matrix_to_csv(matrix, filename):
"""
Given any matrix, write the matrix to a csv file
The rows of the csv are the inner lists of the matrix
"""
rows = []
for x in range(len(matrix)):
row = []
for y in range(len(matrix[x])):
row.append(str(matrix[x][y]))
rows.append(",".join(row))
csv = "\n".join(rows)
newfile = open(filename, "w")
newfile.writelines(csv)
newfile.close()
def load_matrix_from_csv(filename):
"""
Load a csv file with given filename into a matrix
Is the inverse operation of the function `write_matrix_to_csv`
Loads the entries as raw strings; use `set_matrix_to_integers` or `set_matrix_to_floats`
to convert to the relevant type
"""
datafile = open(filename, "r")
raw_data = datafile.read().split("\n")
line_data = []
for line in raw_data:
line_data.append(line.split(","))
return line_data
def transpose_matrix(matrix):
"""
Transpose the given matrix, making its rows its columns and its columns its rows
"""
new_matrix = []
for y in range(len(matrix[0])):
new_matrix.append([])
for x in range(len(matrix)):
new_matrix[y].append(matrix[x][y])
return new_matrix
def set_matrix_to_integers(matrix):
"""
Set all entries of a matrix to integers, from e.g. strings
"""
for x in range(len(matrix)):
for y in range(len(matrix[x])):
matrix[x][y] = int(matrix[x][y])
def set_matrix_to_floats(matrix):
"""
Set all entries of a matrix to floats, from e.g. strings
"""
for x in range(len(matrix)):
for y in range(len(matrix[x])):
matrix[x][y] = float(matrix[x][y])