forked from mdfahimhasan/Global-Subsidence-Groundwater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LOAO_Accuracy_Test.py
1093 lines (920 loc) · 63.1 KB
/
LOAO_Accuracy_Test.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Author: Md Fahim Hasan
# Email: [email protected]
import os
import pickle
import fiona
import numpy as np
import pandas as pd
from glob import glob
from osgeo import gdal
import geopandas as gpd
from rasterio.mask import mask
from shapely.geometry import mapping, shape
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, accuracy_score, classification_report, \
precision_score, recall_score, f1_score
from System_operations import makedirs
from Raster_operations import shapefile_to_raster, mosaic_rasters, read_raster_arr_object, \
write_raster, clip_resample_raster_cutline, resample_reproject
from ML_operations import reindex_df
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning) # to ignore future warning coming from pandas
warnings.filterwarnings(action='ignore')
No_Data_Value = -9999
referenceraster = '../Data/Reference_rasters_shapes/Global_continents_ref_raster.tif'
def combine_georeferenced_subsidence_polygons(input_polygons_dir, joined_subsidence_polygons,
search_criteria='*Subsidence*.shp', skip_polygon_processing=True):
"""
Combining georeferenced subsidence polygons.
Parameters:
input_polygons_dir : Input subsidence polygons' directory.
joined_subsidence_polygons : Output joined subsidence polygon filepath.
search_criteria : Search criteria for input polygons.
skip_polygon_processing : Set False if want to process georeferenced subsidence polygons.
Returns : Joined subsidence polygon.
"""
global gdf
if not skip_polygon_processing:
subsidence_polygons = glob(os.path.join(input_polygons_dir, search_criteria))
sep = joined_subsidence_polygons.rfind(os.sep)
makedirs([joined_subsidence_polygons[:sep]]) # creating directory for the prepare_subsidence_raster function
for each in range(1, len(subsidence_polygons) + 1):
if each == 1:
gdf = gpd.read_file(subsidence_polygons[each - 1])
gdf_new = gpd.read_file(subsidence_polygons[each - 1])
add_to_gdf = pd.concat([gdf, gdf_new], ignore_index=True)
gdf = add_to_gdf
gdf['Class_name'] = gdf['Class_name'].astype(float)
unique_area_name = gdf['Area_name'].unique().tolist()
unique_area_name_code = [i + 1 for i in range(len(unique_area_name))]
polygon_area_name_dict = {}
for name, code in zip(unique_area_name, unique_area_name_code):
polygon_area_name_dict[name] = code
Area_code = []
for index, row in gdf.iterrows():
Area_code.append(polygon_area_name_dict[row['Area_name']])
gdf['Area_code'] = pd.Series(Area_code)
gdf.to_file(joined_subsidence_polygons)
pickle.dump(polygon_area_name_dict, open('../Model Run/LOO_Test/InSAR_Data/polygon_area_name_dict.pkl',
mode='wb+'))
else:
joined_subsidence_polygons = '../Model Run/LOO_Test/InSAR_Data/georef_subsidence_polygons.shp'
polygon_area_name_dict = pickle.load(open('../Model Run/LOO_Test/InSAR_Data/polygon_area_name_dict.pkl',
mode='rb'))
return joined_subsidence_polygons, polygon_area_name_dict
def substitute_area_code_on_raster(input_raster, value_to_substitute, output_raster):
"""
Substitute raster values with area code for InSAR produced subsidence rasters (California, Arizona, Quetta, Qazvin,
China_Hebei, China_Hefei, Colorado, Coastal_subsidence etc.)
Parameters:
input_raster : Input subsidence raster filepath.
value_to_substitute : Area code that will substitute raster values.
output_raster : Filepath of output raster.
Returns : Raster with values substituted with area code.
"""
raster_arr, raster_file = read_raster_arr_object(input_raster)
raster_arr = np.where(np.isnan(raster_arr), raster_arr, value_to_substitute)
area_coded_raster = write_raster(raster_arr, raster_file, raster_file.transform, output_raster)
return area_coded_raster
def reclassify_resample_EGMS_insar(input_egms_dir='../InSAR_Data/Europe_EGMS/Interim_processing',
search_criteria='*neg_values*.tif',
output_dir='../InSAR_Data/Europe_EGMS/reclass_resample_EGMS',
nodata=No_Data_Value):
makedirs([output_dir])
egms_datasets = glob(os.path.join(input_egms_dir, search_criteria))
for data in egms_datasets:
basename = os.path.basename(data)
find_n = basename.rfind('n') # 'n' stands for start of the the neg_values
region_name = basename[: (find_n - 1)]
resampled_raster = os.path.join(output_dir, f'{region_name}_resampled.tif')
gdal.Warp(destNameOrDestDS=resampled_raster, srcDSOrSrcDSTab=data, dstSRS='EPSG:4326', xRes=0.02,
yRes=0.02, outputType=gdal.GDT_Float32)
region_arr, region_file = read_raster_arr_object(resampled_raster)
# converting mm/year to cm/year
region_arr_cm = region_arr * 0.1
region_arr_cm = np.where(region_arr_cm < -200, nodata, region_arr_cm) # -200 is arbitrary big to set nodata
# Classifying to model classes
sub_less_1cm = 1
sub_1cm_to_5cm = 5
sub_greater_5cm = 10
classified_arr = np.where(region_arr_cm >= -1, sub_less_1cm, region_arr_cm)
classified_arr = np.where((classified_arr < -1) & (classified_arr >= -5), sub_1cm_to_5cm, classified_arr)
classified_arr = np.where((classified_arr > nodata) & (classified_arr < -5), sub_greater_5cm, classified_arr)
classified_arr = np.where(np.isnan(region_arr_cm), np.nan, classified_arr)
classified_EGMS_insar = os.path.join(output_dir, f'{region_name}_reclass_resampled.tif')
write_raster(raster_arr=classified_arr, raster_file=region_file, transform=region_file.transform,
outfile_path=classified_EGMS_insar)
def combine_georef_insar_subsidence_raster(input_polygons_dir='../InSAR_Data/Georeferenced_subsidence_data',
joined_subsidence_polygon='../Model Run/LOO_Test/InSAR_Data/'
'georef_subsidence_polygons.shp',
insar_data_dir='../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir',
interim_dir='../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir',
output_dir='../Model Run/LOO_Test/InSAR_Data/'
'final_subsidence_raster',
skip_polygon_processing=False,
area_code_column='Area_code',
final_subsidence_raster='Subsidence_area_coded.tif',
polygon_search_criteria='*Subsidence*.shp', already_prepared=False,
refraster=referenceraster):
"""
Prepare area coded subsidence raster for training data by joining georeferenced polygons and insar data.
Parameters:
input_polygons_dir : Input subsidence polygons' directory.
joined_subsidence_polygons : Output joined subsidence polygon filepath.
insar_data_dir : InSAR data directory.
interim_dir : Intermediate working directory for storing interim data.
output_dir : Output raster directory.
skip_polygon_processing : Set to True if polygon merge is not required.
final_subsidence_raster : Final subsidence raster including georeferenced and insar data.
polygon_search_criteria : Input subsidence polygon search criteria.
insar_search_criteria : InSAR data search criteria.
already_prepared : Set to True if subsidence raster is already prepared.
refraster : Global Reference raster.
Returns : Final subsidence raster to be used as training data and a subsidence area code dictionary.
"""
if not already_prepared:
makedirs([interim_dir, output_dir])
# Processing georeferenced subsidence data from articles
print('Processing area coded subsidence polygons...')
subsidence_polygons, subsidence_areaname_dict = \
combine_georeferenced_subsidence_polygons(input_polygons_dir, joined_subsidence_polygon,
polygon_search_criteria, skip_polygon_processing)
print('Processed area coded subsidence polygons')
interim_georeferenced_raster_area_coded = \
shapefile_to_raster(subsidence_polygons, interim_dir, resolution=0.005,
raster_name='interim_georef_subsidence_raster_areacode_0005.tif',
use_attr=True, attribute=area_code_column, ref_raster=refraster, alltouched=False)
georeferenced_raster_area_coded = \
resample_reproject(interim_georeferenced_raster_area_coded, interim_dir,
raster_name='interim_georef_subsidence_raster_areacode.tif', resample=True,
reproject=False, both=False, resample_algorithm='near')
# Processing EGMS InSAR data
print('Processing EGMS InSAR data...')
reclassify_resample_EGMS_insar()
print('Processed EGMS InSAR data...')
print('Processing area coded InSAR data...')
georef_subsidence_gdf = gpd.read_file(joined_subsidence_polygon)
num_of_georef_subsidence = len(georef_subsidence_gdf['Area_code'].unique())
# author processed/preprocessed (7 regions)
california_area_code = num_of_georef_subsidence + 1
arizona_area_code = california_area_code + 1
quetta_area_code = arizona_area_code + 1
qazvin_area_code = quetta_area_code + 1
hebei_area_code = qazvin_area_code + 1
hefei_area_code = hebei_area_code + 1
colorado_area_code = hefei_area_code + 1
# EGMS (27 regions, hungary and romania together)
denmark_tinglev_area_code = colorado_area_code + 1
england_london_area_code = denmark_tinglev_area_code + 1
france_bordeaux_area_code = england_london_area_code + 1
greece_alexandreia_palamas_area_code = france_bordeaux_area_code + 1
greece_patras_katochi_area_code = greece_alexandreia_palamas_area_code + 1
hungary_szeged_romania_timisoara_area_code = greece_patras_katochi_area_code + 1
italy_cerignola_campagna_area_code = hungary_szeged_romania_timisoara_area_code + 1
italy_mazzafarro_area_code = italy_cerignola_campagna_area_code + 1
italy_podelta_area_code = italy_mazzafarro_area_code + 1
italy_rosarno_area_code = italy_podelta_area_code + 1
italy_salerno_area_code = italy_rosarno_area_code + 1
italy_schiavonea_area_code = italy_salerno_area_code + 1
netherlands_limburg_area_code = italy_schiavonea_area_code + 1
spain_murcia_area_code = netherlands_limburg_area_code + 1
# Coastal from Shirzaei et al. 2021
coastal_area_code = spain_murcia_area_code + 1
subsidence_areaname_dict['California'] = california_area_code
subsidence_areaname_dict['Arizona'] = arizona_area_code
subsidence_areaname_dict['Pakistan_Quetta'] = quetta_area_code
subsidence_areaname_dict['Iran_Qazvin'] = qazvin_area_code
subsidence_areaname_dict['China_Hebei'] = hebei_area_code
subsidence_areaname_dict['China_Hefei'] = hefei_area_code
subsidence_areaname_dict['Colorado'] = colorado_area_code
subsidence_areaname_dict['Denmark_Tinglev'] = denmark_tinglev_area_code
subsidence_areaname_dict['England_London'] = england_london_area_code
subsidence_areaname_dict['France_Bordeaux'] = france_bordeaux_area_code
subsidence_areaname_dict['Greece_Alexandreia_Palamas'] = greece_alexandreia_palamas_area_code
subsidence_areaname_dict['Greece_Patras_Katochi'] = greece_patras_katochi_area_code
subsidence_areaname_dict['Hungary_Szeged_Romania_Timisoara'] = hungary_szeged_romania_timisoara_area_code
subsidence_areaname_dict['Italy_Cerignola_Campagna'] = italy_cerignola_campagna_area_code
subsidence_areaname_dict['Italy_Mazzafarro'] = italy_mazzafarro_area_code
subsidence_areaname_dict['Italy_PoDelta'] = italy_podelta_area_code
subsidence_areaname_dict['Italy_Rosarno'] = italy_rosarno_area_code
subsidence_areaname_dict['Italy_Salerno'] = italy_salerno_area_code
subsidence_areaname_dict['Italy_Schiavonea'] = italy_schiavonea_area_code
subsidence_areaname_dict['Netherlands_Limburg'] = netherlands_limburg_area_code
subsidence_areaname_dict['Spain_Murcia'] = spain_murcia_area_code
subsidence_areaname_dict['Coastal'] = coastal_area_code
california_subsidence = '../InSAR_Data/Final_subsidence_data/resampled_insar_data' \
'/California_reclass_resampled.tif'
arizona_subsidence = '../InSAR_Data/Final_subsidence_data/resampled_insar_data/' \
'Arizona_reclass_resampled.tif'
quetta_subsidence = '../InSAR_Data/Final_subsidence_data/resampled_insar_data/' \
'Pakistan_Quetta_reclass_resampled.tif'
qazvin_subsidence = '../InSAR_Data/Final_subsidence_data/resampled_insar_data/' \
'Iran_Qazvin_reclass_resampled.tif'
hebei_subsidence = '../InSAR_Data/Final_subsidence_data/resampled_insar_data/' \
'China_Hebei_reclass_resampled.tif'
hefei_subsidence = '../InSAR_Data/Final_subsidence_data/resampled_insar_data/' \
'China_Hefei_reclass_resampled.tif'
colorado_subsidence = '../InSAR_Data/Final_subsidence_data/resampled_insar_data/' \
'Colorado_reclass_resampled.tif'
denmark_tinglev_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/'\
'Denmark_Tinglev_reclass_resampled.tif'
england_london_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/' \
'England_London_reclass_resampled.tif'
france_bordeaux_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/' \
'France_Bordeaux_reclass_resampled.tif'
greece_alexandreia_palamas_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/' \
'Greece_Alexandreia_palamas_reclass_resampled.tif'
greece_patras_katochi_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/' \
'Greece_Patras_Katochi_reclass_resampled.tif'
hungary_szeged_romania_timisoara_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/' \
'Hungary_Szeged_Romania_Timisoara_reclass_resampled.tif'
italy_cerignola_campagna_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/' \
'Italy_Cerignola_Campagna_reclass_resampled.tif'
italy_mazzafarro_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/' \
'Italy_Mazzafarro_reclass_resampled.tif'
italy_podelta_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/' \
'Italy_PoDelta_reclass_resampled.tif'
italy_rosarno_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/' \
'Italy_Rosarno_reclass_resampled.tif'
italy_salerno_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/' \
'Italy_Salerno_reclass_resampled.tif'
italy_schiavonea_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/' \
'Italy_Schiavonea_reclass_resampled.tif'
netherlands_limburg_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/' \
'Netherlands_Limburg_reclass_resampled.tif'
spain_murcia_subsidence = '../InSAR_Data/Europe_EGMS/reclass_resample_EGMS/Spain_Murcia_reclass_resampled.tif'
coastal_subsidence = '../InSAR_Data/Final_subsidence_data/resampled_insar_data' \
'/Coastal_subsidence.tif'
substitute_area_code_on_raster(california_subsidence, california_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/California_area_raster.tif')
substitute_area_code_on_raster(arizona_subsidence, arizona_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Arizona_area_raster.tif')
substitute_area_code_on_raster(quetta_subsidence, quetta_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Pakistan_Quetta_area_raster.tif')
substitute_area_code_on_raster(qazvin_subsidence, qazvin_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Iran_Qazvin_area_raster.tif')
substitute_area_code_on_raster(hebei_subsidence, hebei_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/China_Hebei_area_raster.tif')
substitute_area_code_on_raster(hefei_subsidence, hefei_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/China_Hefei_area_raster.tif')
substitute_area_code_on_raster(colorado_subsidence, colorado_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Colorado_area_raster.tif')
substitute_area_code_on_raster(denmark_tinglev_subsidence, denmark_tinglev_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Denmark_Tinglev_area_raster.tif')
substitute_area_code_on_raster(england_london_subsidence, england_london_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/England_London_area_raster.tif')
substitute_area_code_on_raster(france_bordeaux_subsidence, france_bordeaux_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/France_Bordeaux_area_raster.tif')
substitute_area_code_on_raster(greece_alexandreia_palamas_subsidence, greece_alexandreia_palamas_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Greece_Alexandreia_Palamas_area_raster.tif')
substitute_area_code_on_raster(greece_patras_katochi_subsidence, greece_patras_katochi_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Greece_Patras_Katochi_area_raster.tif')
substitute_area_code_on_raster(hungary_szeged_romania_timisoara_subsidence,
hungary_szeged_romania_timisoara_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Hungary_Szeged_Romania_Timisoara_area_raster.tif')
substitute_area_code_on_raster(italy_cerignola_campagna_subsidence, italy_cerignola_campagna_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Italy_Cerignola_Campagna_area_raster.tif')
substitute_area_code_on_raster(italy_mazzafarro_subsidence, italy_mazzafarro_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Italy_Mazzafarroo_area_raster.tif')
substitute_area_code_on_raster(italy_podelta_subsidence, italy_podelta_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Italy_PoDelta_area_raster.tif')
substitute_area_code_on_raster(italy_rosarno_subsidence, italy_rosarno_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Italy_Rosarno_area_raster.tif')
substitute_area_code_on_raster(italy_salerno_subsidence, italy_salerno_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Italy_Salerno_area_raster.tif')
substitute_area_code_on_raster(italy_schiavonea_subsidence, italy_schiavonea_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Italy_Schiavonea_area_raster.tif')
substitute_area_code_on_raster(netherlands_limburg_subsidence, netherlands_limburg_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Netherlands_Limburg_area_raster.tif')
substitute_area_code_on_raster(spain_murcia_subsidence, spain_murcia_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Spain_Murcia_area_raster.tif')
coastal_raster_area_coded = substitute_area_code_on_raster(coastal_subsidence, coastal_area_code,
'../Model Run/LOO_Test/InSAR_Data/'
'interim_working_dir/Coastal_raster.tif')
mosaic_rasters(insar_data_dir, output_dir=insar_data_dir, raster_name='interim_insar_Area_data.tif',
ref_raster=refraster, search_by='*area_raster.tif', resolution=0.02)
# merging georeferenced and insar subsidence data
georef_arr = read_raster_arr_object(georeferenced_raster_area_coded, get_file=False).flatten()
insar_arr = read_raster_arr_object(os.path.join(insar_data_dir, 'interim_insar_Area_data.tif'),
get_file=False).flatten()
interim_subsidence_arr = np.where(insar_arr > 0, insar_arr, georef_arr)
ref_arr, ref_file = read_raster_arr_object(referenceraster)
shape = ref_file.shape
# adding coastal area coded raster
coastal_arr = read_raster_arr_object(coastal_raster_area_coded, get_file=False).flatten()
interim_subsidence_arr = interim_subsidence_arr.flatten()
final_subsidence_arr = \
np.where(coastal_arr == coastal_area_code, coastal_arr, interim_subsidence_arr).reshape(shape)
subsidence_data = os.path.join(output_dir, final_subsidence_raster)
write_raster(final_subsidence_arr, ref_file, ref_file.transform, subsidence_data)
print('Created final area coded subsidence raster')
pickle.dump(subsidence_areaname_dict, open(os.path.join(output_dir, 'subsidence_areaname_dict.pkl'),
mode='wb+'))
return subsidence_data, subsidence_areaname_dict
else:
subsidence_data = os.path.join(output_dir, final_subsidence_raster)
subsidence_areaname_dict = pickle.load(open(os.path.join(output_dir, 'subsidence_areaname_dict.pkl'),
mode='rb'))
return subsidence_data, subsidence_areaname_dict
def create_traintest_df_loo_accuracy(input_raster_dir, subsidence_areacode_dict,
output_dir='../Model Run/LOO_Test/Predictors_csv',
search_by='*.tif', skip_dataframe_creation=False,
exclude_predictors=(
'Alexi ET', 'MODIS ET (kg/m2)', 'Irrigated Area Density (gfsad)',
'GW Irrigation Density giam', 'MODIS PET (kg/m2)', 'Clay content PCA',
'MODIS Land Use', 'Grace', 'Sediment Thickness (m)', 'Clay % 200cm',
'Tmin (°C)', 'RET (mm)', 'Clay Thickness (m)')
):
"""
create dataframe from predictor rasters along with area code.
Parameters:
input_raster_dir : Input rasters directory.
subsidence_areacode_dict : subsidence area code dictionary (output from 'combine_georef_insar_subsidence_raster'
function)
output_dir : Output directory path.
search_by : Input raster search criteria. Defaults to '*.tif'.
skip_predictor_subsidence_compilation : Set to True if want to skip processing.
exclude_predictors : Tuple of predictors to be excluded from fitted_model training.
Returns: predictor_df dataframe created from predictor rasters.
"""
if not skip_dataframe_creation:
print('Creating area coded predictors csv...')
predictors = glob(os.path.join(input_raster_dir, search_by))
predictor_name_dict = {'Alexi_ET': 'Alexi ET', 'Aridity_Index': 'Aridity Index',
'Clay_content_PCA': 'Clay content PCA', 'EVI': 'EVI', 'Grace': 'Grace',
'Global_Sediment_Thickness': 'Sediment Thickness (m)',
'GW_Irrigation_Density_giam': 'GW Irrigation Density giam',
'Irrigated_Area_Density_gfsad': 'Irrigated Area Density (gfsad)',
'MODIS_ET': 'MODIS ET (kg/m2)', 'MODIS_PET': 'MODIS PET (kg/m2)', 'NDWI': 'NDWI',
'Irrigated_Area_Density_meier': 'Normalized Irrigated Area Density',
'Population_Density': 'Normalized Population Density', 'SRTM_Slope': '% Slope',
'Subsidence': 'Subsidence', 'TRCLM_RET': 'RET (mm)',
'TRCLM_precp': 'Precipitation (average monthly) (mm)', 'TRCLM_soil': 'Soil moisture (mm)',
'TRCLM_Tmax': 'Tmax (°C)', 'TRCLM_Tmin': 'Tmin (°C)', 'MODIS_Land_Use': 'MODIS Land Use',
'TRCLM_ET': 'ET (average monthly) (mm)', 'Clay_Thickness': 'Clay Thickness (m)',
'Normalized_clay_indicator': 'Normalized Clay Indicator', 'Clay_200cm': 'Clay % 200cm',
'River_gaussian': 'River Gaussian', 'River_distance': 'River Distance (km)',
'Confining_layers': 'Confining Layers'}
predictor_dict = {}
for predictor in predictors:
variable_name = predictor[predictor.rfind(os.sep) + 1:predictor.rfind('.')]
variable_name = predictor_name_dict[variable_name]
if variable_name not in exclude_predictors:
raster_arr, file = read_raster_arr_object(predictor, get_file=True)
raster_arr = raster_arr.flatten()
predictor_dict[variable_name] = raster_arr
subsidence_area_arr, subsidence_area_file = \
read_raster_arr_object('../Model Run/LOO_Test/InSAR_Data/final_subsidence_raster/Subsidence_area_coded.tif')
predictor_dict['Area_code'] = subsidence_area_arr.flatten()
predictor_df = pd.DataFrame(predictor_dict)
predictor_df = predictor_df.dropna(axis=0)
area_code = predictor_df['Area_code'].tolist()
area_name_list = list(subsidence_areacode_dict.keys())
area_code_list = list(subsidence_areacode_dict.values())
area_name = []
for code in area_code:
position = area_code_list.index(code)
name = area_name_list[position]
area_name.append(name)
predictor_df['Area_name'] = area_name
makedirs([output_dir])
output_csv = os.path.join(output_dir, 'train_test_area_coded_2013_2019.csv')
predictor_df.to_csv(output_csv, index=False)
print('Area coded predictors csv created')
return predictor_df, output_csv
else:
print('Loading area coded predictors csv')
output_csv = output_dir + '/' + 'train_test_area_coded_2013_2019.csv'
predictor_df = pd.read_csv(output_csv)
return predictor_df, output_csv
def train_test_split_loo_accuracy(predictor_csv, loo_test_area_name, pred_attr='Subsidence',
outdir='../Model Run/LOO_Test/Predictors_csv'):
"""
Create x_train, y_train, x_test, y_test arrays for machine learning fitted_model.
Parameters:
predictor_dataframe_csv : Predictor csv filepath.
loo_test_area_name : Area name which will be used as test data.
pred_attr : Prediction attribute column name. Default set to 'Subsidence'.
outdir : Output directory where train and test csv will be saved.
Returns : x_train_csv_path, x_train, y_train, x_test, y_test arrays.
"""
predictor_df = pd.read_csv(predictor_csv)
predictor_df = reindex_df(predictor_df)
train_df = predictor_df[predictor_df['Area_name'] != loo_test_area_name]
x_train_df = train_df.drop(columns=['Area_name', 'Area_code', pred_attr])
y_train_df = train_df[pred_attr]
test_df = predictor_df[predictor_df['Area_name'] == loo_test_area_name]
x_test_df = test_df.drop(columns=['Area_name', 'Area_code', pred_attr])
y_test_df = test_df[[pred_attr]]
# x_train_arr = np.array(x_train_df)
# y_train_arr = np.array(y_train_df)
# x_test_arr = np.array(x_test_df)
# y_test_arr = np.array(y_test_df)
x_train_df_path = os.path.join(outdir, 'x_train_loo_test.csv')
x_train_df.to_csv(x_train_df_path, index=False)
y_train_df.to_csv(os.path.join(outdir, 'y_train_loo_test.csv'), index=False)
x_test_df.to_csv(os.path.join(outdir, 'x_test_loo_test.csv'), index=False)
y_test_df.to_csv(os.path.join(outdir, 'y_test_loo_test.csv'), index=False)
return x_train_df_path, x_train_df, y_train_df, x_test_df, y_test_df
def build_ml_classifier(predictor_csv, loo_test_area_name, model='RF', random_state=0,
n_estimators=300, max_depth=14, max_features=7, min_samples_leaf=1e-05, min_samples_split=7,
class_weight='balanced', bootstrap=True, oob_score=True, n_jobs=-1,
accuracy_dir=r'../Model Run/Accuracy_score_loo_test',
modeldir='../Model Run/LOO_Test/Model_Loo_test'):
"""
Build ML 'Random Forest' Classifier.
Parameters:
predictor_csv : Predictor csv (with filepath) containing all the predictors.
loo_test_area_name : Area name which will be used as test data.
fitted_model : Machine learning fitted_model to run.Can only run random forest 'RF' fitted_model.
random_state : Seed value. Defaults to 0.
n_estimators : The number of trees in the forest. Defaults to 500.
max_depth : Depth of each tree. Default set to 20.
min_samples_leaf : Minimum number of samples required to be at a leaf node. Defaults to 1.
min_samples_split : Minimum number of samples required to split an internal node. Defaults to 2.
max_features : The number of features to consider when looking for the best split. Defaults to 'log2'.
class_weight : To assign class weight. Default set to 'balanced'.
bootstrap : Whether bootstrap samples are used when building trees. Defaults to True.
oob_score : Whether to use out-of-bag samples to estimate the generalization accuracy. Defaults to True.
n_jobs : The number of jobs to run in parallel. Defaults to -1(using all processors).
accuracy_dir : Confusion matrix directory. If save=True must need a accuracy_dir.
modeldir : Model directory to store/load fitted_model. Default is '../Model Run/Model/Model_Loo_test'.
Returns: rf_classifier (A fitted random forest fitted_model)
"""
global classifier
x_train_csv, x_train, y_train, x_test, y_test = \
train_test_split_loo_accuracy(predictor_csv, loo_test_area_name, pred_attr='Subsidence',
outdir='../Model Run/LOO_test/Predictors_csv')
makedirs([modeldir])
model_file = os.path.join(modeldir, model)
if model == 'RF':
classifier = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth,
min_samples_leaf=min_samples_leaf, min_samples_split=min_samples_split,
max_features=max_features, max_samples=None, max_leaf_nodes=None,
class_weight=class_weight,
random_state=random_state, bootstrap=bootstrap,
n_jobs=n_jobs, oob_score=oob_score, )
classifier = classifier.fit(x_train, y_train)
# y_train_pred = classifier.predict(x_train)
y_pred = classifier.predict(x_test)
pickle.dump(classifier, open(model_file, mode='wb+'))
classification_accuracy(y_test, y_pred, loo_test_area_name, accuracy_dir)
# # Plotting and saving confusion matrix
# column_labels = [np.array(['Predicted', 'Predicted', 'Predicted']),
# np.array(['<1cm/yr', '1-5cm/yr', '>5cm/yr'])]
# index_labels = [np.array(['Actual', 'Actual', 'Actual']),
# np.array(['<1cm/yr', '1-5cm/yr', '>5cm/yr'])]
#
# cm_train = confusion_matrix(y_train, y_train_pred)
# cm_df_train = pd.DataFrame(cm_train, columns=column_labels, index=index_labels)
# print(cm_df_train)
#
# cm_test = confusion_matrix(y_test, y_pred)
# cm_df_test = pd.DataFrame(cm_test, columns=column_labels, index=index_labels)
# print(cm_df_test)
return classifier, loo_test_area_name
def classification_accuracy(y_test, y_pred, loo_test_area_name,
accuracy_dir=r'../Model Run/LOO_Test/Accuracy_score'):
"""
Classification accuracy assessment.
Parameters:
y_test : y_test array from train_test_split_loo_accuracy() function.
y_pred : y_pred data from build_ML_classifier() function.
classifier : ML classifier from build_ML_classifier() function.
x_train_csv : path of x train csv from 'train_test_split_loo_accuracy' function.
loo_test_area_name : test area name for which to create confusion matrix.
area_index : Index that will help saving accuracy score. (don't need to added manually, fitted_model will take from
run_loo_accuracy_test function)
accuracy_dir : Confusion matrix directory. If save=True must need a accuracy_dir.
predictor_importance : Default set to True to plot predictor importance plot.
predictor_imp_keyword : Keyword to save predictor important plot.
Returns: Confusion matrix, score and predictor importance graph.
"""
subsidence_training_area_list = [
'Arizona', 'Australia_Perth', 'Bangladesh_GBDelta', 'California', 'China_Beijing',
'China_Hebei', 'China_Hefei', 'China_Shanghai', 'China_Tianjin', 'China_Wuhan', 'China_Xian',
'China_YellowRiverDelta', 'Coastal', 'Colorado', 'Egypt_NileDelta', 'India_Delhi',
'Indonesia_Bandung', 'Indonesia_Semarang', 'Iran_MarandPlain', 'Iran_Mashhad', 'Iran_Qazvin',
'Iran_Tehran', 'Iraq_TigrisEuphratesBasin', 'Mexico_MexicoCity', 'Nigeria_Lagos', 'Pakistan_Quetta',
'Philippines_Manila', 'Taiwan_Yunlin', 'Turkey_Bursa', 'Turkey_Karapinar', 'US_Huston',
'Vietnam_Hanoi', 'Vietnam_HoChiMinh', 'Denmark_Tinglev', 'England_London', 'France_Bordeaux',
'Greece_Alexandreia_Palamas', 'Greece_Patras_Katochi', 'Hungary_Szeged_Romania_Timisoara',
'Italy_Cerignola_Campagna', 'Italy_Mazzafarro', 'Italy_PoDelta', 'Italy_Rosarno',
'Italy_Salerno', 'Italy_Schiavonea', 'Netherlands_Limburg', 'Spain_Murcia']
subsidence_training_area_list = sorted(subsidence_training_area_list)
makedirs([accuracy_dir])
cm = confusion_matrix(y_test, y_pred)
cm_df = pd.DataFrame(cm)
cm_name = loo_test_area_name + '_cmatrix.csv'
csv = os.path.join(accuracy_dir, cm_name)
cm_df.to_csv(csv, index=True)
overall_accuracy = round(accuracy_score(y_test, y_pred), 2)
# generating classification report
classification_report_dict = classification_report(y_test, y_pred, output_dict=True)
del classification_report_dict['accuracy']
classification_report_df = pd.DataFrame(classification_report_dict)
classification_report_df.drop(labels='support', inplace=True)
micro_precision = round(precision_score(y_test, y_pred, average='micro'), 2)
micro_recall = round(recall_score(y_test, y_pred, average='micro'), 2)
micro_f1 = round(f1_score(y_test, y_pred, average='micro'), 2)
classification_report_df['micro avg'] = [micro_precision, micro_recall, micro_f1]
cols = classification_report_df.columns.tolist()
if '1.0' not in cols:
classification_report_df['1.0'] = [np.nan, np.nan, np.nan]
if '5.0' not in cols:
classification_report_df['5.0'] = [np.nan, np.nan, np.nan]
if '10.0' not in cols:
classification_report_df['10.0'] = [np.nan, np.nan, np.nan]
classification_report_df = classification_report_df[['1.0', '5.0', '10.0', 'micro avg', 'macro avg',
'weighted avg']]
classification_report_df.rename(columns={'1.0': '<1cm/yr', '5.0': '1-5cm/yr', '10.0': '>5cm/yr'}, inplace=True)
classification_report_df = classification_report_df[classification_report_df.columns].round(2)
classification_report_csv_name = accuracy_dir + '/' + loo_test_area_name + '_classification_report.csv'
classification_report_df.to_csv(classification_report_csv_name)
print('Accuracy Score for {} : {}'.format(loo_test_area_name, overall_accuracy))
path = accuracy_dir + '/' + 'Accuracy_Reports_Joined' + '/' + 'Accuracy_scores.txt'
if loo_test_area_name == subsidence_training_area_list[0]:
os.remove(path)
txt_object = open(path, 'w+')
else:
txt_object = open(path, 'a')
txt_object.write('Accuracy Score for {} : {} \n'.format(loo_test_area_name, overall_accuracy))
txt_object.close()
return overall_accuracy
def create_prediction_raster(predictors_dir, model, yearlist=(2013, 2019), search_by='*.tif',
continent_search_by='*continent.shp', predictor_csv_exists=False,
continent_shapes_dir='../Data/Reference_rasters_shapes/continent_extents',
prediction_raster_dir='../Model Run/LOO_Test/Prediction_rasters',
exclude_columns=(), pred_attr='Subsidence',
prediction_raster_keyword='rf', predict_probability_greater_1cm=True):
"""
Create predicted raster from random forest fitted_model.
Parameters:
predictors_dir : Predictor rasters' directory.
fitted_model : A fitted fitted_model obtained from random_forest_classifier function.
yearlist :Tuple of years for the prediction. Default set to (2013, 2019).
search_by : Predictor rasters search criteria. Defaults to '*.tif'.
continent_search_by : Continent shapefile search criteria. Defaults to '*continent.tif'.
predictor_csv_exists : Set to True if predictor csv already exists. Defaults set to False. Should be False if
list of drop columns changes.
continent_shapes_dir : Directory path of continent shapefiles.
prediction_raster_dir : Output directory of prediction raster.
exclude_columns : Predictor rasters' name that will be excluded from the fitted_model. Defaults to ().
pred_attr : Variable name which will be predicted. Defaults to 'Subsidence_G5_L5'.
prediction_raster_keyword : Keyword added to final prediction raster name.
predict_probability_greater_1cm : Set to False if probability of prediction of each classes (<1cm, 1-5cm, >5cm)
is required. Default set to True to predict probability of prediction for >1cm.
Returns: Subsidence prediction raster and
Subsidence prediction probability raster (if prediction_probability=True).
"""
global raster_file
predictor_rasters = glob(os.path.join(predictors_dir, search_by))
continent_shapes = glob(os.path.join(continent_shapes_dir, continent_search_by))
drop_columns = list(exclude_columns) + [pred_attr]
continent_prediction_raster_dir = os.path.join(prediction_raster_dir, 'continent_prediction_rasters_'
+ str(yearlist[0]) + '_' + str(yearlist[1]))
makedirs([prediction_raster_dir])
makedirs([continent_prediction_raster_dir])
predictor_name_dict = {'Alexi_ET': 'Alexi ET', 'Aridity_Index': 'Aridity Index',
'Clay_content_PCA': 'Clay content PCA', 'EVI': 'EVI', 'Grace': 'Grace',
'Global_Sediment_Thickness': 'Sediment Thickness (m)',
'GW_Irrigation_Density_giam': 'GW Irrigation Density giam',
'Irrigated_Area_Density_gfsad': 'Irrigated Area Density (gfsad)',
'MODIS_ET': 'MODIS ET (kg/m2)', 'MODIS_PET': 'MODIS PET (kg/m2)', 'NDWI': 'NDWI',
'Irrigated_Area_Density_meier': 'Normalized Irrigated Area Density',
'Population_Density': 'Normalized Population Density', 'SRTM_Slope': '% Slope',
'Subsidence': 'Subsidence', 'TRCLM_RET': 'RET (mm)',
'TRCLM_precp': 'Precipitation (average monthly) (mm)', 'TRCLM_soil': 'Soil moisture (mm)',
'TRCLM_Tmax': 'Tmax (°C)', 'TRCLM_Tmin': 'Tmin (°C)', 'MODIS_Land_Use': 'MODIS Land Use',
'TRCLM_ET': 'ET (average monthly) (mm)', 'Clay_Thickness': 'Clay Thickness (m)',
'Normalized_clay_indicator': 'Normalized Clay Indicator', 'Clay_200cm': 'Clay % 200cm',
'River_gaussian': 'River Gaussian', 'River_distance': 'River Distance (km)',
'Confining_layers': 'Confining Layers'}
for continent in continent_shapes:
continent_name = continent[continent.rfind(os.sep) + 1:continent.rfind('_')]
predictor_csv_dir = '../Model Run/LOO_Test/Predictors_csv/continent_csv'
makedirs([predictor_csv_dir])
predictor_csv_name = continent_name + '_predictors.csv'
predictor_csv = os.path.join(predictor_csv_dir, predictor_csv_name)
nan_pos_dict_name = predictor_csv_dir + '/nanpos_' + continent_name # name to save nan_position_dict
clipped_predictor_dir = os.path.join('../Model Run/LOO_Test/Predictors_csv/Predictors_2013_2019',
continent_name + '_predictors_' + str(yearlist[0]) + '_' + \
str(yearlist[1]))
if not predictor_csv_exists:
predictor_dict = {}
nan_position_dict = {}
raster_shape = None
for predictor in predictor_rasters:
variable_name = predictor[predictor.rfind(os.sep) + 1:predictor.rfind('.')]
variable_name = predictor_name_dict[variable_name]
if variable_name not in drop_columns:
raster_arr, raster_file = clip_resample_raster_cutline(predictor, clipped_predictor_dir, continent,
naming_from_both=False,
naming_from_raster=True, assigned_name=None)
raster_shape = raster_arr.shape
raster_arr = raster_arr.reshape(raster_shape[0] * raster_shape[1])
nan_position_dict[variable_name] = np.isnan(raster_arr)
raster_arr[nan_position_dict[variable_name]] = 0
predictor_dict[variable_name] = raster_arr
pickle.dump(nan_position_dict, open(nan_pos_dict_name, mode='wb+'))
predictor_df = pd.DataFrame(predictor_dict)
predictor_df = predictor_df.dropna(axis=0)
predictor_df = reindex_df(predictor_df)
# this predictor df consists all input variables including the ones to drop
predictor_df.to_csv(predictor_csv, index=False)
else:
predictor_df = pd.read_csv(predictor_csv)
predictor_df = reindex_df(predictor_df)
nan_position_dict = pickle.load(open(nan_pos_dict_name, mode='rb'))
raster_arr, raster_file = clip_resample_raster_cutline(predictor_rasters[1], clipped_predictor_dir,
continent, naming_from_both=False,
naming_from_raster=True, assigned_name=None)
raster_shape = raster_arr.shape
# Model prediction
y_pred = model.predict(predictor_df)
for variable_name, nan_pos in nan_position_dict.items():
if variable_name not in drop_columns:
y_pred[nan_pos] = raster_file.nodata
y_pred_arr = y_pred.reshape(raster_shape)
prediction_raster_name = continent_name + '_prediction_' + str(yearlist[0]) + '_' + str(yearlist[1]) + '.tif'
predicted_raster = os.path.join(continent_prediction_raster_dir, prediction_raster_name)
write_raster(raster_arr=y_pred_arr, raster_file=raster_file, transform=raster_file.transform,
outfile_path=predicted_raster)
print('Prediction raster created for', continent_name)
if predict_probability_greater_1cm:
y_pred_proba = model.predict_proba(predictor_df)
y_pred_proba = y_pred_proba[:, 1] + y_pred_proba[:, 2]
for variable_name, nan_pos in nan_position_dict.items():
if variable_name not in drop_columns:
y_pred_proba[nan_pos] = raster_file.nodata
y_pred_proba_arr = y_pred_proba.reshape(raster_shape)
probability_raster_name = continent_name + '_proba_greater_1cm_' + str(yearlist[0]) + '_' + \
str(yearlist[1]) + '.tif'
probability_raster = os.path.join(continent_prediction_raster_dir, probability_raster_name)
write_raster(raster_arr=y_pred_proba_arr, raster_file=raster_file, transform=raster_file.transform,
outfile_path=probability_raster)
print('Prediction probability for >1cm created for', continent_name)
raster_name = prediction_raster_keyword + '_prediction_' + str(yearlist[0]) + '_' + str(yearlist[1]) + '.tif'
subsidence_arr, path = mosaic_rasters(continent_prediction_raster_dir, prediction_raster_dir, raster_name,
search_by='*prediction*.tif')
print('Global prediction raster created')
if predict_probability_greater_1cm:
proba_raster_name = prediction_raster_keyword + '_proba_greater_1cm_' + str(yearlist[0]) + '_' + \
str(yearlist[1]) + '.tif'
mosaic_rasters(continent_prediction_raster_dir, prediction_raster_dir, proba_raster_name,
search_by='*proba_greater_1cm*.tif')
print('Global prediction probability raster created')
def run_loo_accuracy_test(predictor_dataframe_csv, exclude_predictors_list, n_estimators=300, max_depth=20,
max_features=10, min_samples_leaf=1e-05, min_samples_split=2, class_weight='balanced',
predictor_raster_directory='../Model Run/Predictors_2013_2019',
skip_create_prediction_raster=False, predictor_csv_exists=False,
predict_probability_greater_1cm=False):
"""
Driver code for running Loo Accuracy Test.
Parameters:
predictor_dataframe_csv : filepath of predictor csv.
exclude_predictors_list : List of predictors to exclude for training the fitted_model.
n_estimators : The number of trees in the forest. Defaults to 500.
max_depth : Depth of each tree. Default set to 20.
min_samples_leaf : Minimum number of samples required to be at a leaf node. Defaults to 1.
min_samples_split : Minimum number of samples required to split an internal node. Defaults to 2.
max_features : The number of features to consider when looking for the best split. Defaults to 'log2'.
class_weight : To assign class weight. Default set to 'balanced'.
predictor_raster_directory : Original predictor raster directory. Default set to
'../Model Run/Predictors_2013_2019'.
skip_create_prediction_raster : Set to True if want to skip prediction raster creation.
predictor_csv_exists : Set to True if predictor csv for each continent exists. Default set to False to create
create new predictor csv (also needed if predictor combinations are changed).
predict_probability_greater_1cm : Set to True if want to create >1cm/yr probability raster. Default set to False.
Returns : Classification reports and confusion matrix for individual fitted_model training, Overall accuracy result
for each fitted_model as a single text file, prediction rasters for each fitted_model
(if skip_create_prediction_raster=False)
"""
subsidence_training_area_list = [
'Denmark_Tinglev', 'Netherlands_Limburg', 'Arizona', 'Australia_Perth', 'Bangladesh_GBDelta', 'California',
'China_Beijing','China_Hebei', 'China_Hefei', 'China_Shanghai', 'China_Tianjin', 'China_Wuhan', 'China_Xian',
'China_YellowRiverDelta', 'Coastal', 'Colorado', 'Egypt_NileDelta', 'India_Delhi',
'Indonesia_Bandung', 'Indonesia_Semarang', 'Iran_MarandPlain', 'Iran_Mashhad', 'Iran_Qazvin',
'Iran_Tehran', 'Iraq_TigrisEuphratesBasin', 'Mexico_MexicoCity', 'Nigeria_Lagos', 'Pakistan_Quetta',
'Philippines_Manila', 'Taiwan_Yunlin', 'Turkey_Bursa', 'Turkey_Karapinar', 'US_Huston',
'Vietnam_Hanoi', 'Vietnam_HoChiMinh', 'England_London', 'France_Bordeaux',
'Greece_Alexandreia_Palamas', 'Greece_Patras_Katochi', 'Hungary_Szeged_Romania_Timisoara',
'Italy_Cerignola_Campagna', 'Italy_Mazzafarro', 'Italy_PoDelta', 'Italy_Rosarno',
'Italy_Salerno', 'Italy_Schiavonea', 'Spain_Murcia']
subsidence_training_area_list = sorted(subsidence_training_area_list)
for area in subsidence_training_area_list:
print('Running without', area)
trained_rf, loo_area = build_ml_classifier(predictor_dataframe_csv, area, model='RF', random_state=0,
n_estimators=n_estimators, max_depth=max_depth,
max_features=max_features, min_samples_leaf=min_samples_leaf,
min_samples_split=min_samples_split, class_weight=class_weight,
bootstrap=True, oob_score=True, n_jobs=-1,
accuracy_dir=r'../Model Run/LOO_Test/Accuracy_score',
modeldir='../Model Run/LOO_Test/Model_Loo_test')
if not skip_create_prediction_raster:
create_prediction_raster(predictors_dir=predictor_raster_directory, model= trained_rf,
yearlist=[2013, 2019], search_by='*.tif',
continent_search_by='*continent.shp',
continent_shapes_dir='../Data/Reference_rasters_shapes/continent_extents',
prediction_raster_dir='../Model Run/LOO_Test/Prediction_rasters',
exclude_columns=exclude_predictors_list, pred_attr='Subsidence',
prediction_raster_keyword='Trained_without_' + area,
predictor_csv_exists=predictor_csv_exists,
predict_probability_greater_1cm=predict_probability_greater_1cm)
def concat_classification_reports(classification_csv_dir='../Model Run/LOO_Test/Accuracy_score'):
"""
Merge classification reports from all fitted_model runs.
Parameters:
classification_csv_dir : Directory of individual classification reports. Default set to
'../Model Run/LOO_Test/Accuracy_score'
Returns : A joined classification report.
"""
reports = glob(classification_csv_dir + '/' + '*classification_report*.csv')
report_df = [pd.read_csv(report) for report in reports]
area_name = []
for report in reports:
area = report[report.rfind(os.sep) + 1:report.find('classification') - 1]
area_name.append(area)
merged_reports_df = pd.concat(report_df, keys=area_name, ignore_index=False)
merged_reports_df = merged_reports_df.reset_index(level=1, drop=True)
merged_reports_df = merged_reports_df.rename(columns={'Unnamed: 0': 'metrics'})
merged_reports_df = merged_reports_df[['metrics', '<1cm/yr', '1-5cm/yr', '>5cm/yr', 'micro avg', 'macro avg',
'weighted avg']]
merged_reports_df.to_csv('../Model Run/LOO_Test/Accuracy_score/Accuracy_Reports_Joined/'
'Classification_reports_joined.csv')
def categorize_based_on_probability(run=False):
"""
Categorizing LOO accuracy results.
Categorizing Criterion-
if pixels_greater_40_proba > number_subsidence_pixels:
accuracy_category = 1, status = 'satisfactory'
elif 1 <= perc_pixels_greater_40_proba < 20:
accuracy_category = 2, status = 'acceptable'
else:
accuracy_category = 3, status = 'not satisfactory'
if region_name in region_subsidence_less_1cm:
accuracy_category = 1, status = 'satisfactory (only <1cm train data)'
Parameters:
run : Set to True if want to run this function to assess LOAO test accuracy.
Returns: A excel file with accuracy category for each region.
"""
if run:
polygon_boundary = '../Model Run/LOO_Test/global_georef_InSAR_subsidence_polygons.shp'
proba_predictions = glob(os.path.join('../Model Run/LOO_Test/Prediction_rasters', '*proba*.tif'))
region_file_dict = dict()
subsidence_training_data = '../Model Run/Predictors_2013_2019/Subsidence.tif'
for file in proba_predictions:
file_name = file[file.rfind(os.sep) + 1: file.rfind('.')]
str_list = file_name.split('_')
filterout_list = ['Trained', 'without', 'proba', 'greater', '1cm', '2013', '2019']
str_list_filtered = [i for i in str_list if i not in filterout_list]
area_name = '_'.join(str_list_filtered)
region_file_dict[area_name] = file
del region_file_dict['Coastal']
area_shape_list = [(pol['properties']['Area_Name'], shape(pol['geometry'])) for pol in
fiona.open(polygon_boundary)]
# Some areas where only 1/2/3 pixels are found to be >1 cm/year (compared to all pixeks in that area)
# are also added to 'region_subsidence_less_1cm' list as judging them by >1 cm/year criteria won't be fair as
# dominant <1 cm/year subsiding region
region_subsidence_less_1cm = ['Australia_Perth', 'Colorado', 'Egypt_NileDelta', 'Iraq_TigrisEuphratesBasin',
'Nigeria_Lagos', 'US_Huston', 'Denmark_Tinglev', 'Greece_Patras_Katochi',
'Italy_Mazzafarro', 'Italy_Rosarno', 'Italy_Salerno', 'Italy_Schiavonea',
'England_London', 'France_Bordeaux', 'Hungary_Szeged_Romania_Timisoara',
'Netherlands_Limburg']
for each in area_shape_list:
region_name, shapely_geom = each
geom = [mapping(shapely_geom)]
raster_arr, file = read_raster_arr_object(region_file_dict[region_name])
proba_prediction_arr, transform = mask(dataset=file, shapes=geom, filled=True, crop=True)
proba_prediction_arr = proba_prediction_arr.squeeze()
outdir = '../Model Run/LOO_Test/Accuracy_score/regional_probability_prediction'
makedirs([outdir])
saved_raster = os.path.join(outdir, region_name + '.tif')
write_raster(proba_prediction_arr, file, transform, saved_raster)
proba_prediction_arr = proba_prediction_arr.flatten()
total_pixels = np.count_nonzero(np.where(proba_prediction_arr != np.nan, 1, 0))
pixels_greater_40_proba = np.count_nonzero(np.where(proba_prediction_arr >= 0.40, 1, 0))
perc_pixels_greater_40_proba = pixels_greater_40_proba * 100 / total_pixels
subsidence_data, subsidence_file = read_raster_arr_object(subsidence_training_data)
subsidence_arr, sub_transform = mask(dataset=subsidence_file, shapes=geom, filled=True, crop=True)
subsidence_arr = subsidence_arr.flatten()
number_subsidence_pixels = np.count_nonzero(np.where(subsidence_arr > 1, 1, 0))
if '_' in region_name:
if 'US' in region_name:
country = 'United States'
else:
country = region_name.split('_')[0]
region = region_name[region_name.find('_') + 1:]
else:
country = 'United States'
region = region_name
if region_name in region_subsidence_less_1cm:
accuracy_category = 1
if 0 <= perc_pixels_greater_40_proba < 15:
status = 'satisfactory (only <1cm/year train data)'
else:
status = 'not satisfactory (only <1cm/year train data)'
else:
if pixels_greater_40_proba > number_subsidence_pixels:
accuracy_category = 1
status = 'satisfactory'