-
Notifications
You must be signed in to change notification settings - Fork 0
/
decision_tree.py
429 lines (385 loc) · 13.3 KB
/
decision_tree.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
from time import time
from angles_geom import get_zenith_angle
from bias_checking import comparision_algorithms, comparision_visible
from bias_checking import statistics_classes
from get_data import get_features
from image_processing import segmentation, segmentation_otsu_2d
from read_metadata import read_satellite_step
from recognise_shadows import recognize_cloud_shade
from tomas_outputs import get_tomas_outputs, reduce_tomas_2_classes
from utils import *
from visualize import visualize_map_time, get_bbox
def get_classes_v1_point(
latitudes, longitudes, beginning, ending, slot_step=1, shades_detection=False
):
visible_features = get_features(
"visible",
latitudes,
longitudes,
beginning,
ending,
"abstract",
slot_step,
gray_scale=False,
)
infrared_features = get_features(
"infrared",
latitudes,
longitudes,
beginning,
ending,
"abstract",
slot_step,
gray_scale=False,
)
# classes: classified_cli, snow over the ground, other (ground, sea...), unknown
visibles = get_features(
"visible",
latitudes,
longitudes,
beginning,
ending,
"channel",
slot_step,
gray_scale=False,
)
angles = get_zenith_angle(
get_times_utc(beginning, ending, read_satellite_step(), 1),
latitudes,
longitudes,
)
# bright = (classify_brightness(visible_features[:, :, :, 0]) == 1) & (visibles[:,:,:,1] > 0.25*angles)
# bright = (visible_features[:, :, :, 0] > 0.3) & (visibles[:,:,:,1] > 0.25*angles)
bright = segmentation(
"watershed-3d", visible_features[:, :, :, 0], thresh_method="static", static=0.3
) # & (visibles[:,:,:,1] > 0.2*angles)
# negative_variable_brightness = (classifiy_brightness_variability(visible_features[:, :, :, 1]) == 1)
# positive_variable_brightness = (classifiy_brightness_variability(visible_features[:, :, :, 2]) == 1)
negative_variable_brightness = (visible_features[:, :, :, 1] > 0.2) & bright
positive_variable_brightness = (visible_features[:, :, :, 2] > 0.2) & bright
# from classification_cloud_index import classify_cloud_covertness, classify_cloud_variability
cold = infrared_features[:, :, :, 2] == 1
thin_clouds = infrared_features[:, :, :, 0] > 0.2
obvious_clouds = infrared_features[:, :, :, 1] > 10
# obvious_clouds = (classify_cloud_covertness(infrared_features[:, :, :, 0]) == 1) & (infrared_features[:, :, :, 1] > 1)
snow = (
bright
& ~negative_variable_brightness
& ~positive_variable_brightness
& ~obvious_clouds
& ~cold
)
del bright
(nb_slots, nb_latitudes, nb_longitudes) = np.shape(visible_features)[0:3]
classes = np.zeros((nb_slots, nb_latitudes, nb_longitudes))
begin_affectation = time()
classes[
(visibles[:, :, :, 1] > 0.5 * angles) & (visibles[:, :, :, 0] > 0.1 * angles)
] = 3 # before all the other classes (VERY IMPORTANT)
classes[
(infrared_features[:, :, :, 0] == -10)
] = 13 # before all the other classes (important)
classes[
(visible_features[:, :, :, 3] == 1)
] = 12 # before all the other classes (important)
classes[snow] = 5 # class ground snow or ice
classes[positive_variable_brightness] = 6
classes[negative_variable_brightness] = 7
classes[obvious_clouds] = 1
classes[thin_clouds] = 2
# classes[bright & ~negative_variable_brightness & warm] = 10
# classes[bright & negative_variable_brightness & warm] = 9
classes[cold] = 8
# classes[obvious_clouds & bright] = 3
if shades_detection:
cloudy = (classes != 0) & (classes != 5)
shades_detection = recognize_cloud_shade(
visibles[:, :, :, 1],
cloudy,
get_zenith_angle(
get_times_utc(beginning, ending, read_satellite_step(), slot_step=1),
latitudes,
longitudes,
),
)
classes[shades_detection] = 11
print("allegedly uncovered lands:0")
print("obvious clouds:1")
print("thin clouds:2")
print("visible but undecided:3")
print("slight clouds and bright:4")
print("snowy:5")
print("snowy clouds:6")
print("covered snow:7")
print("cold:8")
# print('hot bright corpses:9')
# print('hot bright variable corpses:10')
print("foggy:11")
print(
"sea clouds identified by visibility:12"
) #### WARNING: what about icy lakes??? ####
# print('suspect high snow index (over sea / around sunset or sunrise):13')
print("undefined:13")
return classes
def get_classes_v2_image(
latitudes,
longitudes,
beginning,
ending,
slot_step=1,
method="otsu-3d",
shades_detection=False,
):
visible_features = get_features(
"visible",
latitudes,
longitudes,
beginning,
ending,
True,
slot_step,
gray_scale=True,
)
infrared_features = get_features(
"infrared",
latitudes,
longitudes,
beginning,
ending,
True,
slot_step,
gray_scale=True,
)
if method in ["watershed-2d", "watershed-3d"]:
visible = get_features(
"visible",
latitudes,
longitudes,
beginning,
ending,
"abstract",
slot_step,
gray_scale=False,
)
# visualize_map_time(segmentation_otsu_2d(vis), bbox)
bright = segmentation_otsu_2d(visible_features[:, :, :, 0]) & (
visible[:, :, :, 1] > 0.35
)
# visualize_map_time(bright, bbox)
bright = segmentation(method, bright, thresh_method="binary")
else:
visible = get_features(
"visible",
latitudes,
longitudes,
beginning,
ending,
"abstract",
slot_step,
gray_scale=False,
)
visible = segmentation(
method,
visible,
1,
)
bright = segmentation(method, visible_features[:, :, :, 0]) & visible
# negative_variable_brightness = visible_features[:, :, :, 1] > 25
# positive_variable_brightness = visible_features[:, :, :, 2] > 25
negative_variable_brightness = segmentation(
method, visible_features[:, :, :, 1], thresh_method="static", static=30
)
positive_variable_brightness = segmentation(
method, visible_features[:, :, :, 2], thresh_method="static", static=30
)
# slight_clouds = segmentation(method, infrared_features[:, :, :, 1])
# obvious_clouds = (infrared_features[:, :, :, 0] == 1)
obvious_clouds = segmentation(method, infrared_features[:, :, :, 0]) & segmentation(
method, infrared_features[:, :, :, 1], thresh_method="static", static=20
)
cold = infrared_features[:, :, :, 2] == 1
# warm = (infrared_features[:, :, :, 2] == 1)
# foggy: low snow index, good vis
(nb_slots, nb_latitudes, nb_longitudes) = np.shape(visible_features)[0:3]
classes = np.zeros((nb_slots, nb_latitudes, nb_longitudes))
# clouds = obvious_clouds | slight_clouds
begin_affectation = time()
classes[
(infrared_features[:, :, :, 0] == -10)
] = 13 # before all the other classes (important)
classes[
(visible_features[:, :, :, 3] == 1)
] = 12 # before all the other classes (important)
classes[
bright & ~obvious_clouds & ~negative_variable_brightness
] = 5 # class ground snow or ice
classes[bright & positive_variable_brightness] = 6
classes[bright & negative_variable_brightness] = 7
# classes[bright & ~negative_variable_brightness & warm] = 10
# classes[bright & negative_variable_brightness & warm] = 9
classes[cold] = 8
# WARNING: slight clouds AND obvious clouds => obvious clouds
# classes[slight_clouds & bright] = 4
# classes[slight_clouds & ~bright] = 2
classes[obvious_clouds & ~bright] = 1
classes[obvious_clouds & bright] = 3
if shades_detection:
cloudy = (classes != 0) & (classes != 5)
shades_detection = recognize_cloud_shade(
visible[:, :, :, 1],
cloudy,
get_zenith_angle(
get_times_utc(beginning, ending, read_satellite_step(), slot_step=1),
latitudes,
longitudes,
),
)
classes[shades_detection] = 11
# classes[bright & (infrared_features[:, :, :, 3] == 1)] = 7 # = cold and bright. opaque obvious_clouds or cold obvious_clouds over snowy stuff
# classes[persistent_snow & (obvious_clouds | cold_opaque_clouds)] = 4
# classes[foggy] = 11
print("allegedly uncovered lands:0")
print("obvious clouds:1")
print("thin clouds:2")
print("visible but undecided:3")
print("slight clouds and bright:4")
print("snowy:5")
print("snowy clouds:6")
print("covered snow:7")
print("cold:8")
# print('hot bright corpses:9')
# print('hot bright variable corpses:10')
print("foggy:11")
print(
"sea clouds identified by visibility:12"
) #### WARNING: what about icy lakes??? ####
# print('suspect high snow index (over sea / around sunset or sunrise):13')
print("undefined:13")
return classes
def reduce_classes(classes):
to_return = np.full_like(classes, 3)
cloudless = (classes == 0) | (classes == 9) | (classes == 10) | (classes == 5)
cloudless = cloudless & np.roll(cloudless, 1) & np.roll(cloudless, -1)
uncovered_snow = cloudless & (classes == 5)
snow_free_cloudless = cloudless & (classes != 5)
to_return[snow_free_cloudless] = 0
to_return[uncovered_snow] = 1
to_return[(classes == 2) | (classes == 4)] = 2
to_return[classes == 13] = 4
print("uncovered_snow free cloud free: 0")
print("uncovered_snow:1")
print("slight clouds:2")
print("clouds:3")
print("undefined:4")
return to_return
def reduce_two_classes(classes):
classes = reduce_classes(classes)
to_return = np.full_like(classes, 1)
to_return[(classes == 1) | (classes == 0)] = 0
return to_return
if __name__ == "__main__":
nb_classes = 14
slot_step = 1
beginning = 13525 + 5
nb_days = 5
ending = beginning + nb_days - 1
# method = 'watershed-3d' # 'on-point', 'otsu-2d', 'otsu-3d', 'watershed-2d', 'watershed-3d'
method = (
"on-point" # 'on-point', 'otsu-2d', 'otsu-3d', 'watershed-2d', 'watershed-3d'
)
latitude_beginning = 40.0 - 5
latitude_end = 45.0
longitude_beginning = 120.0
longitude_end = 130.0
(
beginning,
ending,
latitude_beginning,
latitude_end,
longitude_beginning,
longitude_end,
) = typical_input()
latitudes, longitudes = get_latitudes_longitudes(
latitude_beginning, latitude_end, longitude_beginning, longitude_end
)
date_begin, date_end = print_date_from_dfb(beginning, ending)
print(
"NS:",
latitude_beginning,
latitude_end,
" WE:",
longitude_beginning,
longitude_end,
)
bbox = get_bbox(
latitude_beginning, latitude_end, longitude_beginning, longitude_end
)
t_begin = time()
if method == "on-point":
classes_ped = get_classes_v1_point(
latitudes, longitudes, beginning, ending, slot_step, shades_detection=False
)
elif method in ["otsu-2d", "otsu-3d", "watershed-2d", "watershed-3d"]:
classes_ped = get_classes_v2_image(
latitudes,
longitudes,
beginning,
ending,
slot_step,
method,
shades_detection=True,
)
print("classification time: ", time() - t_begin)
visualize_map_time(
classes_ped,
bbox,
vmin=0,
vmax=nb_classes - 1,
title=method + " Classes 0-" + str(nb_classes - 1) + " from" + str(date_begin),
)
# statistics_classes(classes_ped, display_now=True)
# visualize_map_time(reduce_classes(classes_ped), bbox, vmin=0, vmax=4, title=method + ' Classes 0-' + str(5 - 1) +
# ' from' + str(date_begin))
visualize_map_time(
comparision_visible(
get_features(
"visible", latitudes, longitudes, beginning, ending, "channel"
)[:, :, :, 1],
classes_ped,
),
bbox,
vmin=-1,
vmax=1,
title="comparision visible",
)
classes_ped = reduce_two_classes(classes_ped)
visualize_map_time(
classes_ped,
bbox,
vmin=0,
vmax=1,
title="ped-" + method + " Classes 0-" + str(1) + " from" + str(date_begin),
)
# raise Exception('stop here for now pliz')
classes_tomas = get_tomas_outputs(
beginning,
ending,
latitude_beginning,
latitude_end,
longitude_beginning,
longitude_end,
)
classes_tomas = reduce_tomas_2_classes(classes_tomas)
visualize_map_time(
classes_tomas,
bbox,
vmin=0,
vmax=1,
title="Tomas classification " + " from" + str(date_begin),
)
statistics_classes(classes_ped, display_now=True)
statistics_classes(classes_tomas, display_now=True)
visualize_map_time(
comparision_algorithms(classes_ped, classes_tomas), bbox, "comparision"
)
del classes_tomas