-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemporelStability.py
More file actions
360 lines (285 loc) · 12.6 KB
/
Copy pathTemporelStability.py
File metadata and controls
360 lines (285 loc) · 12.6 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
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
# -*-coding: utf-8 -*-
import matplotlib
# matplotlib.use("Qt4Agg")
import numpy as np
import skimage.io as sk
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from PIL import Image
from sklearn.cluster import MiniBatchKMeans
from joblib import Parallel, delayed
import os
import pickle as pkl
import re
import random
import TemporalStability as c_func
from yellowbrick.cluster import KElbowVisualizer
class TemporelStability:
def __init__(self, n_clusters=8, miniBtach=False, data="all"):
"""
"""
self.selectedPoint = None
# Initialisation du KMeans
if n_clusters is None:
n_clusters = 8 # default value of n_clusters is 8
self.n_clusters = None
else:
self.n_clusters = n_clusters
if miniBtach == True:
self.km = MiniBatchKMeans(
n_clusters=n_clusters, init='k-means++', max_iter=100, batch_size=100, random_state=0)
else:
self.km = KMeans(n_clusters=n_clusters,
init='k-means++', max_iter=100, random_state=0)
self.elbow = KElbowVisualizer(self.km, k=(4,10))
self.km_param = self.km.get_params()
self.data = data
self.globale = False
self.locale = False
def setSelectedPoint(self, XTrue):
"""
:param trueX: image of the ROIs (1 if consideried pixels)
:return:
"""
self.selectedPoint = XTrue
def fit(self, X):
"""
:param *X: list of STIS
:return:
"""
if len(X) == 0:
print("Give at least 1 STIS('ndarray' where shape is nuber of line and nuber of cols and dim is the number of images where each image is token at time t)")
return
if X.ndim == 4:
self.l, self.c, self.d, self.t = X.shape
else:
self.l, self.c, self.t = X.shape
self.d = 1
#print(self.l, self.c, self.d, self.t)
if self.data == "10":
self.nbPix = int(self.l * self.c * self.t * 0.1)
#print("nb pixel: ", self.nbPix)
idx = np.random.randint(self.l * self.c * self.t, size=self.nbPix)
#print("shape randint ", idx.shape)
self.features = X.reshape((-1, self.d))[idx] #np.random.choice(X.reshape((-1, self.d)), self.nbPix)
elif self.data != 'all':
if self.selectedPoint is None:
print("Please give an array where intersted point are equal to 1")
return -1
else:
#print("Discrize of selected points ")
self.features = X[self.selectedPoint != 0].reshape(
(-1, 1))
else:
#print("discrize all points ")
self.features = X.reshape((-1, self.d))
#print("X shape ", X.shape)
#print("features shape ", self.features.shape)
#exit(0)
if self.n_clusters is None:
self.elbow.fit(self.features)
self.n_clusters = self.elbow.elbow_value_
self.km_param['n_clusters'] = self.n_clusters
print(self.km_param)
self.km.set_params(**self.km_param)
#self.elbow.show()
# passer les donnees au kmeans pour trouver les centroides
self.km.fit(self.features)
#print(self.km.cluster_centers_)
self.cluster = (self.km.cluster_centers_.squeeze())
X = X.reshape((-1, self.d))
self.serie = self.km.predict(X).reshape((self.l, self.c, self.t))
"""serie_tmp = []
for i in range(self.t):
s = self.km.predict(X[:, :, i].reshape((-1, self.d)))
#s = np.choose(s, self.cluster)
#aa_ = tools.normalisation2(s.reshape((self.l, self.c))).astype(np.uint8)
#aa_[self.selectedPoint == 0 ] = 0
#tools.saveImage_(aa_, "E:/newSITS/vergers/img_k4/"+str(i)+".tif")
serie_tmp.append(s.reshape((self.l, self.c)))
#plt.imshow(s.reshape((self.l, self.c)))
#plt.pause(0.1)
#plt.show()
serie_tmp = np.ascontiguousarray(np.dstack(serie_tmp), dtype=np.double)
self.serie = serie_tmp"""
def getDiskrizedSits(self):
return self.serie[0]
def setROI(self, XTrue):
"""
Set the image of the ROI. interested pixels are those who are different from 0
:param XTrue: Image of the ROI
:return:
"""
self.selectedPoint = XTrue
def discritize(self, date_vec, *X):
"""
This funtion is used just to discrize a new STIS
====> it is developed to be used for the local method
:param *X: list of STIS
:return:
"""
if len(X) == 0:
print("Give at least 1 STIS('ndarray' where shape is nuber of line and nuber of cols and dim is the number of images where each image is token at time t)")
return -1
self.l, self.c, self.d = X[0].shape
self.serie = []
for stis in X:
self.serie_tmp = []
for i in range(self.d):
s = self.km.predict(stis[:, :, i].reshape((-1, 1)))
s = np.choose(s, self.km.cluster_centers_.squeeze())
self.serie_tmp.append(s.ravel())
self.serie.append(np.array(self.serie_tmp).T)
# self.__fit__(date_vec)
def get_TS(self, date_vec):
date_vec = np.ascontiguousarray(np.array(date_vec), dtype=np.int)
#print("Python print\n", date_vec)
# print("Python: Calcul des MS, NB et MSS;\n dates = ", date_vec)
self.ts = np.ascontiguousarray(np.zeros((self.l, self.c, 3)), dtype=np.double)
c_func.getTemporalStability(self.serie.astype(np.double), date_vec, self.ts)
if not (self.selectedPoint is None):
self.ts[self.selectedPoint == 0] = 0
return self.ts
def get_Reconstracted_TS(self, date_vec):
date_vec = np.ascontiguousarray(np.array(date_vec), dtype=np.int)
print("Python print\n", date_vec)
# print("Python: Calcul des MS, NB et MSS;\n dates = ", date_vec)
ts = np.ascontiguousarray(np.zeros((self.l, self.c, self.d)), dtype=np.double)
ts = c_func.getReconstruncted_TemporalStability(self.serie[0].astype(np.double), date_vec, ts)
# print(ts.shape)
self.reco_series= ts
if not (self.selectedPoint is None):
self.reco_series[self.selectedPoint == 0] = 0
return self.reco_series
def getMS(self):
return self.ts[:, :, 0]
def getNb(self):
return self.ts[:, :, 1]
def getMSS(self):
return self.ts[:, :, 2]
def get_TS_local(self, date_vec, window):
self.smt_local = list()
self.nbt_local = list()
self.map_stratMax_local = list()
for i in range(0, len(date_vec), window):
print("========= ", i, " =========")
print(date_vec[i:i + window])
date_vec_ = np.ascontiguousarray(
np.array(date_vec[i:i + window]), dtype=np.int)
# print("Python: Calcul des MS, NB et MSS;\n dates = ", date_vec)
ts = np.ascontiguousarray(
np.zeros((self.l, self.c, 3)), dtype=np.double)
ts = c_func.getTemporalStability(
self.serie[0][:,:,i:i + window].astype(np.double), date_vec_, ts)
# print(ts.shape)
if not (self.selectedPoint is None):
ts[self.selectedPoint == 0] = 0
self.smt_local.append(ts[:, :, 0])
self.nbt_local.append(ts[:, :, 1])
self.map_stratMax_local.append(ts[:, :, 2])
del ts
self.smt_local = np.dstack(self.smt_local)
self.nbt_local = np.dstack(self.nbt_local)
self.map_stratMax_local = np.dstack(self.map_stratMax_local)
def getSM_t_local(self):
return self.smt_local
def getNb_t_local(self):
return self.nbt_local
def getMapStartMax_local(self):
return self.map_stratMax_local
############## Relaxation Temporelle ##################
def get_TS_t(self, date_vec):
date_vec = np.ascontiguousarray(np.array(date_vec), dtype=np.int)
self.ts_t = np.ascontiguousarray(np.zeros((self.l, self.c, 3)), dtype=np.double)
c_func.getTemporalStability_temp(self.serie.astype(np.double), date_vec, self.ts_t)
if not (self.selectedPoint is None):
self.ts_t[self.selectedPoint == 0] = 0
return self.ts_t
def getMS_t(self):
return self.ts_t[:, :, 0]
def getNb_t(self):
return self.ts_t[:, :, 1]
def getMSS_t(self):
return self.ts_t[:, :, 2]
def get_Reconstracted_TS_temp(self, date_vec):
date_vec = np.ascontiguousarray(np.array(date_vec), dtype=np.int)
print("Python print\n", date_vec)
# print("Python: Calcul des MS, NB et MSS;\n dates = ", date_vec)
ts = np.ascontiguousarray(np.zeros((self.l, self.c, self.d)), dtype=np.double)
ts = c_func.getReconstruncted_TemporalStability_temp(self.serie[0].astype(np.double), date_vec, ts)
# print(ts.shape)
self.reco_series_temp= ts
if not (self.selectedPoint is None):
self.reco_series_temp[self.selectedPoint == 0] = 0
return self.reco_series_temp
############## Relaxation Spatiale ##################
def get_TS_s(self, date_vec):
"""
Calcule de la stabilite temporelle dans un voisinnage (tyuau large ==> tunel)
:param date_vec:
:return:
"""
date_vec = np.ascontiguousarray(
np.array(date_vec), dtype=np.int)
# print(date_vec.shape)
# print("Python: Calcul des MS, NB et MSS;\n dates = ", date_vec)
self.ts_s = np.ascontiguousarray(np.zeros((self.l, self.c, 3)), dtype=np.double)
c_func.getTemporalStability_spatio(self.serie.astype(np.double), date_vec, self.ts_s )
if not (self.selectedPoint is None):
self.ts_s[self.selectedPoint == 0] = 0
return self.ts_s
def getMS_s(self):
return self.ts_s[:, :, 0]
def getNb_s(self):
return self.ts_s[:, :, 1]
def getMSS_s(self):
return self.ts_s[:, :, 2]
def get_Reconstracted_TS_spatio(self, date_vec):
date_vec = np.ascontiguousarray(np.array(date_vec), dtype=np.int)
print("Python print\n", date_vec)
# print("Python: Calcul des MS, NB et MSS;\n dates = ", date_vec)
ts = np.ascontiguousarray(np.zeros((self.l, self.c, self.d)), dtype=np.double)
ts = c_func.getReconstruncted_TemporalStability_spatio(self.serie[0].astype(np.double), date_vec, ts)
# print(ts.shape)
self.reco_series_spatio= ts
if not (self.selectedPoint is None):
self.reco_series_spatio[self.selectedPoint == 0] = 0
return self.reco_series_spatio
############## Relaxation Spatio-temporelle ##################
def get_TS_st(self, date_vec):
"""
Calcule de la stabilite temporelle dans un voisinnage (tyuau large ==> tunel) et permettre de sauter une image
si different pour voir ce qui vient apres
:exemple:
v= [[2 2 2 1 6 5]
[3 3 1 1 4 4]
[1 1 1 1 2 6]]
encode_spatioTempShift(v)
output:
[5, 1]
:param date_vec:
:return:
"""
date_vec = np.ascontiguousarray( np.array(date_vec), dtype=np.int)
self.ts_st = np.ascontiguousarray(np.zeros((self.l, self.c, 3)), dtype=np.double)
c_func.getTemporalStability_spatiotemp(self.serie.astype(np.double), date_vec, self.ts_st )
if not (self.selectedPoint is None):
self.ts_st[self.selectedPoint == 0] = 0
return self.ts_st
def getMS_st(self):
return self.ts_st[:, :, 0]
def getNb_st(self):
return self.ts_st[:, :, 1]
def getMSS_st(self):
return self.ts_st[:, :, 2]
def get_Reconstracted_TS_spatiotemp(self, date_vec):
date_vec = np.ascontiguousarray(np.array(date_vec), dtype=np.int)
print("Python print\n", date_vec)
# print("Python: Calcul des MS, NB et MSS;\n dates = ", date_vec)
ts = np.ascontiguousarray(np.zeros((self.l, self.c, self.d)), dtype=np.double)
ts = c_func.getReconstruncted_TemporalStability_spatiotemp(self.serie[0].astype(np.double), date_vec, ts)
# print(ts.shape)
self.reco_series_spatiotemp= ts
if not (self.selectedPoint is None):
self.reco_series_spatiotemp[self.selectedPoint == 0] = 0
return self.reco_series_spatiotemp