-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpca_kmeans_ndvismooth_UPDATED.py
399 lines (349 loc) · 12.4 KB
/
pca_kmeans_ndvismooth_UPDATED.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 8 21:36:31 2022
@author: santhosh
"""
import sys, os
from skimage import io
from sklearn.cluster import KMeans
import numpy as np
from sklearn.decomposition import PCA
import pandas as pd
#from osgeo import gdal, gdalconst
#from osgeo import gdal, osr
#import Early_season_rice_detection_rule as Rice_detection_rule
import rasterio as rio
import os, os.path
import geopandas as gpd
import matplotlib.pyplot as plt
from rasterstats import zonal_stats
from scipy.signal import savgol_filter
import statistics as s
import time
import glob
from scipy.ndimage.filters import uniform_filter1d
import os
window_size = 3
def moving_avg(arr):
return uniform_filter1d(arr, size=window_size)
def raster_writer(ref_raster, out_array, destination):
# print("file: ", ref_raster)
with rio.open(ref_raster) as dataset:
meta_data = dataset.meta
new_dataset = rio.open(
destination,
'w',
driver='GTiff',
compress= "DEFLATE",
height=out_array.shape[1],
width=out_array.shape[2],
count=out_array.shape[0],
crs=meta_data['crs'],
dtype = out_array.dtype, #meta_data['dtype'],
transform=meta_data['transform'],
)
new_dataset.write(out_array.astype(out_array.dtype))
new_dataset.close()
def cluster_pca(n_clusters, n_components, raster):
raster =np.rollaxis(raster,axis=1)
raster =np.rollaxis(raster,axis=2)
raster =np.rollaxis(raster,axis=1)
row, col,band = raster.shape
flat_raster = np.reshape(raster, (row*col,band))
pca = PCA(n_components = n_components)
flat_raster = pca.fit_transform(flat_raster)
kmeans = KMeans(n_clusters = n_clusters, init = 'k-means++').fit(flat_raster)
#center_means = kmeans.cluster_centers_
labels = kmeans.labels_
labels = np.reshape(labels, (row, col))
return labels
def extract_VH_means(labels, raster, n_clusters):
ndvi_mean =[]
columns = []
raster =np.rollaxis(raster,axis=1)
raster =np.rollaxis(raster,axis=2)
raster =np.rollaxis(raster,axis=1)
row, col, band = raster.shape
flat_raster = np.reshape(raster, (row*col, band))
flat_labels = np.reshape(labels, (row*col))
for i in range(n_clusters):
print(i)
mean = np.mean(flat_raster[(flat_labels == i), :],axis=0)
#mean = np.percentile(flat_raster[(flat_labels == i), :],95,axis=0)
ndvi_mean.append(mean)
columns.append('Mean_{}'.format(i))
#np.save(str(i)+'_center_VH_mean.npy', mean)
return ndvi_mean,columns
def original(df,a):
original = []
for i in range(len(df)):
x = df.iloc[i]
x = x[a:]
x = x.to_list()
original.append(x)
return original
#MVC maximum vaule composite
def MVC(original):
resampled = []
for i in range(0, len(original), 2):
j = i+1
if len(original)%2==0:
length = len(original)
else:
length = len(original)-1
if i<length:
temp_max = max(original[i], original[j])
resampled.append(temp_max)
return resampled
#computing moving average with window size 3
def compute_MA(resampled):
moving_average = []
for j in range(len(resampled)):
if j == len(resampled)-2: #condition for last second value in the list
ma = (resampled[j]+resampled[j+1]+resampled[0])/3
moving_average.append(ma)
elif j == len(resampled)-1: #condition for the last value in the list
ma = (resampled[j]+resampled[0]+resampled[1])/3
moving_average.append(ma)
else:
ma = (resampled[j]+resampled[j+1]+resampled[j+2])/3
moving_average.append(ma)
return moving_average
#Imputing values using straight line formula
def straight_line_dropout_MA(resampled, moving_average):
prev = next = 0
j = -1
thresh = abs(0.09*s.mean(resampled))
while j < len(resampled)-2:
j = j+1
if resampled[j] < (moving_average[j]-200): #checking if the current value of list is less than the threshold w.r.t moving average list
prev = j-1
next = j+1
while resampled[prev] < thresh and prev>0: #checking if the previous value is less than the threshold
prev = prev-1
while resampled[next] < thresh and next<len(resampled)-1: #likewise checking if the next value is less than the threshold
next = next+1
#calculating the straight line dropout
slope = (resampled[next]-resampled[prev])/(next-prev)
c = resampled[next] - (slope*next)
resampled[j] = abs((slope*j) + c)
return resampled
def consecutive(data, stepsize=1):
return np.split(data, np.where(np.diff(data) != stepsize)[0]+1)
def fill_gap(arr):
if sum(arr) != 0 :
indexs = np.argwhere(arr == 0)
gaps = consecutive(indexs[:,0])
gaps_arr1 = []
gaps_arr2 = []
for gap in gaps:
if 0 not in gap and len(arr)-1 not in gap and len(gap) > 0 and len(gap) < 10:
gaps_arr1.append(gap)
elif 0 in gap or len(arr)-1 in gap and len(gap) < 10:
gaps_arr2.append(gap)
if len(gaps_arr1) > 0:
for gap in gaps_arr1:
if arr[min(gap)-1] == arr[max(gap)+1]:
arr[gap] = arr[min(gap)-1]
else:
diff = arr[min(gap)-1] - arr[max(gap)+1]
add = diff/(len(gap)+1)
for i in range(len(gap)):
arr[gap[i]] = arr[min(gap)-1] - (i+1)*add
if len(gaps_arr2) > 0:
for gap in gaps_arr2:
if 0 in gap:
diff = arr[max(gap)+1] - arr[max(gap)+3]
add = diff/2
for i in range(len(gap)):
arr[gap[i]] = arr[max(gap)+1] + (len(gap) - i)*add
elif len(arr)-1 in gap:
diff = arr[min(gap)-1] - arr[min(gap)-3]
add = diff/2
for i in range(len(gap)):
arr[gap[i]] = arr[min(gap)-1] + (i+1)*add
return arr
else:
return arr
'''
def SG_Gapfill(arr):
iter = 1
if sum(arr) == 0:
return arr
else:
smoother_ts = savgol_filter(arr, window_length=wnds[0], polyorder=orders[1])
## print(smoother_ts)
diff = smoother_ts - arr
## print(diff)
sign = diff > 0
## print(sign)
W = 1 - np.abs(diff) / np.max(np.abs(diff)) * sign
## print(W)
arr[W<0.9] = 0
## if iter == 1:
## arr[W<0.9] = 0
## else:
## arr[(W>0.3) & (W<0.9)] = 0
smooth_ts = fill_gap(arr)
return smooth_ts
'''
def SG_weight(arr):
if sum(arr) == 0:
return arr
else:
interp_ts = pd.Series(arr)
interp_ts = interp_ts.interpolate(method='linear', limit=14)
## print(interp_ts)
smooth_ts = interp_ts
wnd = 11
order = 4
F = 1e8
W = None
it = 0
smoother_ts = savgol_filter(smooth_ts, window_length=wnd, polyorder=order)
## print(smoother_ts)
diff = smoother_ts - interp_ts
## print(diff)
sign = diff > 0
## print(sign)
if W is None:
W = 1 - np.abs(diff) / np.max(np.abs(diff)) * sign
return W.to_numpy()
def SG_filter(SG_list):
SG_smooth = []
for i in range(len(SG_list)):
x = SG_list[i]
smooth = savgol_filter(x, 11, 4)
SG_smooth.append(smooth)
return SG_smooth
def Timeseries_Expand(Original_timeseries, Original_MVC_timeseries, Corrected_MVC_timeseries):
#print('expanding timeserie...')
Expanded_timeseries =np.zeros(len(Original_timeseries), float)
count = 0
for i in range(0, len(Original_timeseries), 2):
j = i+1
if len(Original_timeseries)%2==0:
length = len(Original_timeseries)
else:
length = len(Original_timeseries)-1
if i<length:
if Original_MVC_timeseries[count] == Original_timeseries[i]:
Expanded_timeseries[i] = Corrected_MVC_timeseries[count]
elif Original_MVC_timeseries[count] == Original_timeseries[j]:
Expanded_timeseries[j] = Corrected_MVC_timeseries[count]
count += 1
return Expanded_timeseries
def main_process(df):
original_list = df.values
final_list = []
for i in range(len(original_list)):
print(len(original_list[i]))
#MVC_pol = MVC(original_list[i])
#print('MVC',len(MVC_pol))
#moving_average = moving_avg(MVC_pol)
#print('moving_average',len(moving_average))
#MVC_dropout = straight_line_dropout_MA(MVC_pol, moving_average)
#print('MVC_dropout',len(MVC_dropout))
#fill_gap_arr = fill_gap(np.array(MVC_dropout))
#SG_gap_arr = SG_Gapfill(np.array(fill_gap_arr))
smooth = savitzky_golay_filtering(original_list[i])
#print('smooth',len(smooth))
#Expanded_timeseries = Timeseries_Expand(original_list[i], MVC_pol, SG_gap_arr)
#print('Expanded_timeseries',len(Expanded_timeseries))
#Expanded_timeseries_gapfil = fill_gap(Expanded_timeseries)
final_list.append(smooth)
return final_list
def cor_ndvi(labels, raster, n_clusters,final):
raster =np.rollaxis(raster,axis=1)
raster =np.rollaxis(raster,axis=2)
raster =np.rollaxis(raster,axis=1)
row, col, band = raster.shape
flat_raster = np.reshape(raster, (row*col, band))
flat_labels = np.reshape(labels, (row*col))
rascopy = flat_raster.copy()
for i in range(n_clusters):
rascopy[np.where(flat_labels == i)]=final[i]
return rascopy
def extract_VH_means(labels, raster, n_clusters):
ndvi_mean =[]
columns = []
raster =np.rollaxis(raster,axis=1)
raster =np.rollaxis(raster,axis=2)
raster =np.rollaxis(raster,axis=1)
row, col, band = raster.shape
flat_raster = np.reshape(raster, (row*col, band))
flat_labels = np.reshape(labels, (row*col))
for i in range(n_clusters):
#print(i)
#mean = np.mean(flat_raster[(flat_labels == i), :],axis=0)
mean = np.percentile(flat_raster[(flat_labels == i), :],85,axis=0)
ndvi_mean.append(mean)
columns.append('Mean_{}'.format(i))
#np.save(str(i)+'_center_VH_mean.npy', mean)
return ndvi_mean,columns
wnds=[11, 7]
orders=[2, 4]
def savitzky_golay_filtering(arr):
if sum(arr) == 0 or arr[0] > 20000:
return arr
elif np.mean(arr, axis = 0) <= 2000:
return arr
else:
interp_ts = pd.Series(arr)
interp_ts = interp_ts.interpolate(method='linear', limit=14)
## print(interp_ts)
smooth_ts = interp_ts
wnd, order = 11, 4
F = 1e8
W = None
it = 0
while True:
smoother_ts = savgol_filter(smooth_ts, window_length=wnd, polyorder=order)
## print(smoother_ts)
diff = smoother_ts - interp_ts
## print(diff)
sign = diff > 0
## print(sign)
if W is None:
W = 1 - np.abs(diff) / np.max(np.abs(diff)) * sign
wnd, order = wnds[1], orders[1]
## print(W)
fitting_score = np.sum(np.abs(diff) * W)
#print it, ' : ', fitting_score
if fitting_score > F:
break
else:
F = fitting_score
it += 1
## print('iteration', it)
## print('F-score', F)
smooth_ts = smoother_ts * sign + interp_ts * (1 - sign)
return smooth_ts
kmeans_folder = "PCA/KMEANS/"
for file in glob.glob('/home/santhosh/Music/TS_9_PROJECTS_WET_DRY_NDVI_MASK/pending/*.tif'):
print(file)
ras = file
ds= rio.open(ras).read()
cluster = 400
print("PCA and KMEANS started for: ",os.path.basename(file))
kmeans_ras = kmeans_folder + os.path.basename(file)[:-4] + "_pca_kmeans.img"
#labels = cluster_pca(n_clusters = cluster, n_components = 5, raster =ds)
labels = rio.open(kmeans_ras).read(1)
cluster = np.max(labels)
#np.save(file[:-4]+'_labels.npy', labels)
print("Computing 85th percentile at each cluster: ",os.path.basename(file))
ndvi,column = extract_VH_means(labels, ds, cluster)
ab = np.array(ndvi)
df = pd.DataFrame(ab)
print("SG filter ongoing")
final_18_19 = main_process(df)
print("SG filter done")
print("Replacing original image with smoothened NDVI")
final_arr = cor_ndvi(labels, ds, cluster,final_18_19)
print("Replaced original image with smoothened NDVI")
final_arr = np.rollaxis(final_arr,axis=1)
final_arr = final_arr.reshape(ds.shape)
if os.path.isdir(os.path.dirname(file)+'/NDVI_cor/') is False:
os.mkdir(os.path.dirname(file)+'/NDVI_cor/')
raster_writer(ras, final_arr, os.path.dirname(file)+'/NDVI_cor/'+os.path.basename(file)[:-4]+'_cor_85.tif')