-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
180 lines (134 loc) · 5.27 KB
/
algorithms.py
File metadata and controls
180 lines (134 loc) · 5.27 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# -*- Encoding: Latin-1 -*-
#!/usr/bin/python
from math import *
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import det, svd
from sklearn.decomposition import PCA, TruncatedSVD
from scipy.sparse import kron, coo_matrix
# ------------------------------------------------------------------------------
# 1: PROJECTION ALGORITHMS
# ------------------------------------------------------------------------------
def projection(Y, n_sources, method='pca'):
"""
Performs the projection of the dataset on the reduced dimension space
The projection can be performed by relying on a PCA or on a SVD transform
Ref: Bioucas Dias et al.
Parameters
----------
Y: numpy array (size: n_bands by n_samples)
data matrix
n_sources: int
number of endmembers. The number of endmembers determines the dimension
of the reduced dimension space.
method: string (default: 'pca')
method used to perform the projection ('pca' or 'svd')
Return
------
Y_rec: numpy array (size: n_bands by n_samples)
data reconstruction from the projected data after applying an inverse
transform
Y_proj: numpy array
projected data
"""
if(method == None):
SNR = estimate_SNR(Y, Y_rec, n_sources)
SNR_th = 15 + 10*np.log10(n_sources)
if(SNR < SNR_th):
print('Selection of the PCA projection')
method = 'pca'
else:
print('SVD projection')
method = 'svd'
if(method == 'pca'):
pca = PCA(n_components=n_sources - 1)
Y_proj = pca.fit_transform(Y.T)
Y_rec = pca.inverse_transform(Y_proj)
elif(method == 'svd'):
svd = TruncatedSVD(n_components=n_sources)
Y_proj = svd.fit_transform(Y.T).T
u = np.mean(Y_proj, axis=1).reshape((1, -1))
Y_proj /= np.dot(u, Y_proj)
Y_rec = svd.inverse_transform(Y_proj.T).T
return Y_proj, Y_rec
# ------------------------------------------------------------------------------
# ENDMEMBERS ESTIMATION: Pure-pixel based algorithms
# ------------------------------------------------------------------------------
class NFINDR:
"""
N-FINDR algorithm implementation
Ref: Winter, M. E. (1999). N-FINDR: An algorithm for fast autonomous
spectral endmember determination in hyperspectral data. In: Imaging
Spectrometry V (Vol. 3753, pp. 266-275). International Society for Optics
and Photonics.
Parameters
----------
Y: numpy array (size: n_bands by n_samples)
data matrix
n_sources: int
number of endmembers
Attributes
----------
Y: numpy array (size: n_bands by n_samples)
data points
n_samples: int
number of samples in the dataset
n_sources: int
number of endmembers
n_bands: int
spectral dimension
Y_proj: numpy array (size: n_sample by n_sources-1 matrix)
data points in the reduced dimension space
M_proj: numpy array (size: n_sources by n_sources-1 matrix)
endmembers vectors in the reduced dimension space
vol: float
volume of the simplex formed by the endmembers
M: numpy array (size: n_bands by n_sources)
endmembers matrix
"""
def __init__(self, Y, n_sources):
self.Y = Y
self.n_bands, self.n_samples = self.Y.shape
self.n_sources = n_sources
# Project the data points
pca = PCA(n_components=self.n_sources - 1)
self.Y_proj = pca.fit_transform(Y.T)
# Select random data points as initial guess for the endmembers
random_indices = np.random.choice(self.n_samples, size=self.n_sources,
replace=False)
self.M_proj = self.Y_proj[random_indices, :]
# Computes the volume of the endmembers simplex
self.vol = abs(det(self.M_proj[1:, :] - self.M_proj[0, :]))
def run(self):
"""
Run the N-FINDR algorithm
"""
indexes = []
for s in range(self.n_sources):
endmembers = np.copy(self.M_proj)
idx = 0
# Iterate over the data points
for n in range(self.n_samples):
# Try replacing the selected endmember by the data point
endmembers[s, :] = self.Y_proj[n, :]
vol = abs(det(endmembers[1:, :] - endmembers[0, :]))
# Update the endmember if the volume is greater than
# the current one
if(vol > self.vol):
self.M_proj = np.copy(endmembers)
self.vol = vol
idx = n
indexes.append(idx)
self.M = self.Y[:, np.array(indexes)]
def display(self):
"""
Display the data points in the reduced dimension space.
This visualization method is mostly appropriate when the number of
sources equals 3.
"""
if(self.n_sources != 3):
print("Warning: the number of sources is larger than 3")
plt.figure()
plt.scatter(self.Y_proj[:, 0], self.Y_proj[:, 1], color='blue')
plt.scatter(self.M_proj[:, 0], self.M_proj[:, 1], color='yellow')
plt.show()