-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
28 lines (24 loc) · 823 Bytes
/
utils.py
File metadata and controls
28 lines (24 loc) · 823 Bytes
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
import numpy as np
# Function to import OMX matrices
def import_matrix(matfile):
f = open(matfile, 'r')
all_rows = f.read()
blocks = all_rows.split('Origin')[1:]
matrix = {}
for k in range(len(blocks)):
orig = blocks[k].split('\n')
dests = orig[1:]
orig=int(orig[0])
d = [eval('{'+a.replace(';',',').replace(' ','') +'}') for a in dests]
destinations = {}
for i in d:
destinations = {**destinations, **i}
matrix[orig] = destinations
zones = max(matrix.keys())
mat = np.zeros((zones, zones))
for i in range(zones):
for j in range(zones):
# We map values to a index i-1, as Numpy is base 0
mat[i, j] = matrix.get(i+1,{}).get(j+1,0)
index = np.arange(zones) + 1
return matrix, index