-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
142 lines (112 loc) · 4.37 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import matplotlib.pyplot as plt
import numpy as np
import scipy.linalg
from scipy.fft import dct
from PIL import Image
from math import ceil, log10
figsize=(6866/500, 2386/500)
# figsize = (6946/700,3906/700)
# figsize = (12000/1000,14000/1000) ##fat
def getColouredImage(path):
img_colour = Image.open(path)
#plt.figure(figsize=figsize)
#plt.imshow(img_colour)
#plt.title("Original coloured image")
return img_colour
def toGrayScale(img):
img_bw = img.convert('L')
# plt.figure(figsize=figsize)
# plt.imshow(img_bw, cmap='gray')
# plt.title("Black and white image")
# print("Image array shape: {}".format(img_bw.size))
return img_bw
def gaussianMatrixGenerator(m, n, param = (0, 1)):
omega = []
for i in range(n):
omega.append(np.random.normal(param[0], param[1], size=m))
omega = np.asarray(omega).transpose()
shape = omega.shape
if ((shape[0] == 1) or (shape[1] == 1)):
omega = omega.flatten()
return omega
def rankk_random_matrix_generator(m, n, rank):
mat = np.empty((0,m))
for i in range(rank):
mat = np.append(mat, [np.random.normal(100, 75, size=m)], axis=0)
for i in range(rank, n):
shape = mat.shape
w = np.random.uniform(low = 0, high = 1, size = shape[1])
mat = np.append(mat, [w + np.random.normal(0, 0.00025, size=m)], axis=0)
return np.asarray(mat).transpose()
def compute_error(A, A_tilde):
"""Computes the exact error and RMS' magnitude for two matrices.
Args:
A (np.array): The original matrix
A_tilde (np.array): The approximated matrix with which we want to compute the error
Returns:
rsse : the exact error overall for the matrix (summ of each component's errors)
rmse magnitude: the exact error divided by the size of the matrix, to the log10
"""
m,n = A.shape
size = m*n
exact_error = abs(np.linalg.norm(A - A_tilde))
rms_magnitude = ceil(log10(exact_error/size))
return exact_error, rms_magnitude
def print_result(error, magnitude, duration, name):
print("\n####################################################################")
print(name)
print(">>> Duration : {:.5f} sec.".format(duration))
print(">>> Rooted mean squared Error (RMSE) : 10e" + str(magnitude))
print(">>> Rooted sum squared Erros (RSSE) : " + str(error))
print("####################################################################")
return
def DFR_random_matrix(n, l):
""" Creates a DFR matrix of size n * l with F fourier matrix
"""
if n < l:
raise ValueError(f"l ({l}) can't be higher than n ({n})")
D = np.random.uniform(-1, 1, size = n) + np.random.uniform(-1, 1, size = n) * 1.j
D = D / np.abs(D)
F = scipy.linalg.dft(n)
R = np.zeros(shape=(n, l))
deck = np.arange(l)
for i in range(R.shape[1]):
random_int = np.random.choice(deck)
R[random_int, i] = 1
idx = np.where(deck == random_int)[0][0]
deck = np.delete(deck, idx)
return np.sqrt(1 / l) * np.multiply(D[:, None], F).real @ R
def hadamard_random_matrix(n, l):
""" Creates a DFR matrix of size n * l with H Hadamard matrix
"""
if n < l:
raise ValueError(f"l ({l}) can't be higher than n ({n})")
size = 2 ** ceil(np.log(n)/np.log(2))
D = np.random.randint(0, 2, size=size) # n * n
D[np.where(D == 0)] = -1
H = scipy.linalg.hadamard(size) # n * n
R = np.zeros(shape=(size, l)) # l * n
deck = np.arange(size)
for i in range(R.shape[1]):
random_int = np.random.choice(deck)
R[random_int, i] = 1
idx = np.where(deck == random_int)[0][0]
deck = np.delete(deck, idx)
return (np.sqrt(size / l) * np.diag(D) @ H @ R)[:n]
def DCT_random_matrix(n, l):
""" Creates a DFR matrix of size n * l with F a Discrete Cosine Transform matrix
"""
if n < l:
raise ValueError(f"l ({l}) can't be higher than n ({n})")
D = np.random.randint(0, 2, size=n) # n * n
D[np.where(D == 0)] = -1
F = dct(np.identity(n), axis=0, type=2, norm='ortho') # n * n
R = np.zeros(shape=(n, l)) # n*l
deck = np.arange(n)
for i in range(R.shape[1]):
random_int = np.random.choice(deck)
R[random_int, i] = 1
idx = np.where(deck == random_int)[0][0]
deck = np.delete(deck, idx)
return (np.sqrt(n / l) * np.diag(D) @ F @ R)[:n]