/
utils.py
executable file
·75 lines (60 loc) · 1.91 KB
/
utils.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
72
73
74
75
import numpy as np
from multiprocessing import Pool
from itertools import repeat
def remove_zeros(S):
"""
Removes zero vectors from the given list of vectors
Parameters:
S: List of vectors
Returns:
S: The list with zero vectors removed
"""
# Maintain a counter so we can account for shift in indices
# that happen due to the 'del' operation on a list
ct = 0
zero_indices = []
# Loop and check for zero vectors
for i in range(len(S)):
if np.count_nonzero(S[i]) == 0:
zero_indices.append(i - ct)
ct += 1
# Loop over the 'corrected' indices and delete zero elements
for i in zero_indices:
del S[i]
# Return the list
return S
def par_remove_zeros_loop(S, i):
"""
Helper method for parallel execution of remove_zeros()
Simply checks the given index for the zero vector or
value None
Parameters:
S: The set of vectors
i: Index to check
Returns:
True if given index is 0 or None, else False
"""
return S[i] is None or np.count_nonzero(S[i]) == 0
def par_remove_zeros(S):
"""
Removes zero vectors and None elements from the given
list of vectors
Parameters:
S: List of vectors
Returns:
S: The list with zero vectors and None elements removed
"""
# Maintain a counter so we can account for shift in indices
# that happen due to the 'del' operation on a list
ct = 0
N = len(S)
# Start 16 processes to check which vectors are 0 or None
with Pool(16) as p:
res = p.starmap(par_remove_zeros_loop, zip(repeat(S, N), range(N)))
# Loop over the results and delete the right elements
for idx, val in enumerate(res):
if val:
del S[idx - ct]
ct += 1
# Return the new list
return S