forked from dinhinfotech/DiGI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
71 lines (52 loc) · 1.65 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
"""
Util file includes utility functions
"""
from os import listdir
from os.path import isfile, join
import numpy as np
def list_files_in_folder(folder_path):
"""
Return: A list which contains all file names in the folder
"""
list = listdir(folder_path)
onlyfiles = [ f for f in list if isfile(join(folder_path,f)) ]
return onlyfiles
def load_list_from_file(file_path):
"""
Return: A list saved in a file
"""
f = open(file_path,'r')
listlines = [line.rstrip() for line in f.readlines()]
f.close()
return listlines
def save_list_to_file(file_path=None, list_to_save=None):
f = open(file_path, 'w')
f.writelines([line + "\n" for line in list_to_save])
f.close()
def load_matrices(folder_path):
"""
Return: A list of matrices saved in the folder
"""
file_names = list_files_in_folder(folder_path)
matrices = []
for file_name in file_names:
matrices.append(np.loadtxt(folder_path + file_name))
return matrices
def extract_submatrix(row_indices, col_indices, A):
""" Extracting a submatrix from matrix A
Parameter:
row_indices: row index list that we want to extract
col_indices: Column index list that we want to extract
A: Matrix
Return:
submatrix of A
"""
len_row = len(row_indices)
len_col = len(col_indices)
#M = matrix(0.0,(len_row,len_col))
M = np.zeros((len_row,len_col))
for order1, idx_row in enumerate(row_indices):
for order2, idx_col in enumerate(col_indices):
M[order1, order2] = A[idx_row, idx_col]
return M