-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_valid.py
More file actions
82 lines (75 loc) · 1.94 KB
/
Copy pathutils_valid.py
File metadata and controls
82 lines (75 loc) · 1.94 KB
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
76
77
78
79
80
81
from collections import Counter
import numpy as np
def idx_sort(train, m):
data_list = []
for i in range(m):
assign_temp = list(train[i,:].astype('int8'))
assign_str = ' '.join(map(str, assign_temp))
data_list.append(assign_str)
data_counter = Counter(data_list)
idx = data_counter.most_common()
return idx
def build_A(p,n,idx_need):
A = np.zeros([p,n])
for i in range(p):
key, val = idx_need[i]
num = [int(char) for char in key.split()]
num = np.array(num)
A[i,:] = num
A = A.astype('int8')
return A
def gaussianElimination(A):
Perm = []
p = A.shape[0]
for i in range(p):
row_keep = np.where(np.sum(A, axis=1) > 0)[0]
A = A[row_keep]
if i >= A.shape[0]:
break
if np.where(A[i:,i] == 1)[0].size == 0:
col_swap = np.where(A[i,:] == 1)[0][0]
Perm.append((i,col_swap))
col_temp = A[:,i] + 0
A[:,i] = A[:,col_swap] + 0
A[:,col_swap] = col_temp + 0
elif A[i,i] == 0:
row_swap = np.where(A[i:,i] == 1)[0][0] + i
row_temp = A[i,:] + 0
A[i,:] = A[row_swap,:] + 0
A[row_swap,:] = row_temp + 0
for j in np.where(A[:,i] == 1)[0]:
if j != i:
A[j,:] = A[j,:] ^ A[i,:]
A = A[:p,:]
return A, Perm
def solutionH(A, Perm):
G = A[:,A.shape[0]:]
H = np.append(G,np.eye(G.shape[1]), axis = 0).astype('int8')
Perm.reverse()
if Perm != []:
for pair in Perm:
i,j = pair
row_temp = H[i,:] + 0
H[i,:] = H[j,:] + 0
H[j,:] = row_temp + 0
return H
def posInFourier(H):
posF = np.zeros([2**(H.shape[1]),H.shape[0]])
for k in range(2**(H.shape[1])):
a = "{0:b}".format(k)
a = a.zfill(H.shape[1])
a = [int(char) for char in str(a)]
a = np.array(a)
# print H.dot(a) % 2
posF[k,:] = H.dot(a) % 2
return posF
def coefficients(posF, train):
coeff = np.zeros(posF.shape[0])
for i in range(posF.shape[0]):
pos = posF[i,:]
pos_x = np.where(pos==1)
e = train[:,pos_x][:,0,:] - 1
mul = np.sum(e,axis=1) % 2
mul = -2 * mul + 1
coeff[i] = np.mean(mul) * 1.0 / (2**posF.shape[1])
return coeff