-
Notifications
You must be signed in to change notification settings - Fork 26
/
PCA_bigData.py
executable file
·277 lines (243 loc) · 10.2 KB
/
PCA_bigData.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/python
# -*- coding: utf-8 -*-
########################################################################################################################
#
# PCA_bigData.py
# A python script to perform PCA transformation to remote sesning data without loading the entire data
# into local memory. The script uses parallel processing.
#
#
# Author: Javier Lopatin
# Email: [email protected]
# Date: 27/11/2020
# Version: 1.0
#
# Usage:
#
# python MNF.py -i <Imput raster>
# -c <Number of components [default = inputRaster bands]>
# -p <Preprocessing: Brightness Normalization of spectral data [Optional]>
# -n <Raster chunk size to be loaded into memory. It must be divisible by 16>
# -j <n_jobs; number of parallel works to use>
#
# # --preprop [-p]: Brightness Normalization presented in Feilhauer et al., 2010
#
# # examples:
# # Get the regular MNF transformation
# python PCA_bigData.py -i raster.tif
#
# # Get the regular MNF transformation of the first component
# python PCA_bigData.py -i raster.tif -c 1
#
# # with Brightness Normalization
# python PCA_bigData.py -i raster.tif -p
#
#
#
# Bibliography:
#
# Feilhauer, H., Asner, G. P., Martin, R. E., Schmidtlein, S. (2010): Brightness-normalized Partial Least Squares
# Regression for hyperspectral data. Journal of Quantitative Spectroscopy and Radiative Transfer 111(12-13),
# pp. 1947-1957. 10.1016/j.jqsrt.2010.03.007
#
# C-I Change and Q Du. 1999. Interference and Noise-Adjusted Principal Components Analysis.
# IEEE TGRS, Vol 36, No 5.
#
########################################################################################################################
from __future__ import division
import concurrent.futures
from functools import partial
import warnings
import argparse
import os
import numpy as np
from sklearn.decomposition import IncrementalPCA
from tqdm import tqdm
import rasterio
try:
import pysptools.noise as ns
except ImportError:
print("ERROR: Could not import Pysptools Python library.")
print("Check if Pysptools is installed.")
#%%
################
### Functions
################
def _norm(X):
X = X.astype(np.float32)
return X / np.sqrt( np.sum((X**2), 0) )
def _brightNorm(X):
return np.apply_along_axis(_norm, 0, X)
def _parallel_process(inData, outData, do_work, count, n_jobs, chuckSize):
"""
Process infile block-by-block with parallel processing
and write to a new file.
chunckSize needs to be divisible by 16
"""
if chuckSize % 16 == 0:
# apply parallel processing with rasterio
with rasterio.Env():
with rasterio.open(inData) as src:
# Create a destination dataset based on source params. The
# destination will be tiled, and we'll process the tiles
# concurrently.
profile = src.profile
profile.update(blockxsize=chuckSize, blockysize=chuckSize,
count=count, dtype='float32', tiled=True)
with rasterio.open(outData, "w", **profile) as dst:
# Materialize a list of destination block windows
# that we will use in several statements below.
windows = [window for ij, window in dst.block_windows()]
# This generator comprehension gives us raster data
# arrays for each window. Later we will zip a mapping
# of it with the windows list to get (window, result)
# pairs.
data_gen = (src.read(window=window) for window in windows)
with concurrent.futures.ProcessPoolExecutor(
max_workers=n_jobs
) as executor:
# Map the a function over the raster
# data generator, zip the resulting iterator with
# the windows list, and as pairs come back we
# write data to the destination dataset.
for window, result in zip(
tqdm(windows), executor.map(do_work, data_gen)
):
dst.write(result, window=window)
else:
print('ERROR! chuckSize needs to be divisible by 16')
def brightNorm(inData, n_jobs=4, chuckSize=256):
"""
Process the Brightness Normalization in parallel
"""
# get names for output bands
with rasterio.open(inData) as r:
count = r.count # number of bands
# call _getPheno2 function to lcoal
do_work = partial(_brightNorm)
# out data
outData = inData[:-4]+"_BN.tif"
# apply PhenoShape with parallel processing
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
_parallel_process(inData, outData, do_work, count, n_jobs, chuckSize)
except AttributeError:
print('ERROR in parallel processing...')
def PCA_pred(w):
"""
Process the Brightness Normalization in parallel
"""
count, width, height = w.shape
w = np.transpose(w, [1,2,0])
w = w.reshape((width*height, count))
if np.any(np.isinf(w))==True:
w[w == np.inf] = 0
if np.any(np.isnan(w))==True:
w[w == np.nan] = 0
# predict PCA
w = ipca.transform(w)
w = w.reshape((height, width, count))
w = np.transpose(w, [2,0,1])
w = w[:n_components,:,:]
return w.astype(np.float32)
def PCA_train(inData, outData, n_jobs, chuckSize):
"""
Process infile block-by-block with parallel processing
and write to a new file.
chunckSize needs to be divisible by 16
"""
do_work = partial(PCA_pred) # pass prediction function to loop
if chuckSize % 16 == 0:
with rasterio.Env():
with rasterio.open(inData) as src:
count = src.count
profile = src.profile
profile.update(blockxsize=chuckSize, blockysize=chuckSize,
count=n_components, dtype='float32', tiled=True)
with rasterio.open(outData, "w", **profile) as dst:
windows = [window for ij, window in dst.block_windows()]
data_gen = (src.read(window=window) for window in windows)
# train incrementalPCA with raster chunks
print('Training incrementalPCA with chuncks...')
for i in tqdm(range(len(windows))):
w = src.read(window=windows[i]) # read window-by-window
width = windows[i].width
height = windows[i].height
w = np.transpose(w, [1,2,0])
w = w.reshape((width*height, count))
# check for nan and inf
if np.any(np.isinf(w))==True:
w[w == np.inf] = 0
if np.any(np.isnan(w))==True:
w[w == np.nan] = 0
# train pca partially
ipca.partial_fit(w)
print('')
print("The explained variance per component is:")
print(ipca.explained_variance_ratio_)
print("The accumulative explained variance per component is:")
print(np.cumsum(np.round(ipca.explained_variance_ratio_, decimals=4)*100))
print('')
print('Applying PCA transformation and saving into disk...')
with concurrent.futures.ProcessPoolExecutor(
max_workers=n_jobs
) as executor:
for window, result in zip(
tqdm(windows), executor.map(do_work, data_gen)
):
dst.write(result, window=window)
else:
print('ERROR! chuckSize needs to be divisible by 16')
#%%
### Run process
if __name__ == "__main__":
# create the arguments for the algorithm
parser = argparse.ArgumentParser()
parser.add_argument('-i','--inputRaster',
help='Input raster', type=str, required=True)
parser.add_argument('-c','--components',
help='Number of components.', type=int, default=10000)
parser.add_argument('-p','--preprop',
help='Preprocessing: Brightness Normalization of Hyperspectral data [Optional].',
action="store_true", default=False)
parser.add_argument('-j','--n_jobs',
help='Number of cores to be used in parallel processing.',
type=int, default=4)
parser.add_argument('-n','--chuckSize',
help='Size of the raster chunks to be loaded into memory [needs to be divisible by 16]',
type=int, default=256)
parser.add_argument('--version', action='version', version='%(prog)s 2.0')
args = vars(parser.parse_args())
# data imputps/outputs
# inData = 'trialdata.tif'
inData = args["inputRaster"]
n_jobs = args["n_jobs"]
chuckSize = args["chuckSize"]
# Normalization?
BrightnessNormalization = args["preprop"]
# set number of components to retrive
with rasterio.open(inData) as r:
count = r.count
if args['components'] == 10000:
n_components = count
else:
n_components = args['components']
# apply Brigthness Normalization if needed
if BrightnessNormalization==True:
print('Applying preprocessing...')
brightNorm(inData)
print('')
# PCA
ipca = IncrementalPCA()
if BrightnessNormalization==True:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
PCA_train(inData[:-4]+"_BN.tif", inData[:-4]+"_prepro_PCA.tif",
n_jobs, chuckSize)
# delete temporal preprocessing file
os.remove(inData[:-4]+"_BN.tif")
else:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
PCA_train(inData, inData[:-4]+"_PCA.tif", n_jobs, chuckSize)