/
decisionTree.py
299 lines (237 loc) · 10.6 KB
/
decisionTree.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
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 16 13:30:29 2020
@author: galinavj
"""
import rasterio as rio
import matplotlib.pyplot as plt
#import rasterio.warp as warp
#import rasterio.plot as rioPlt
import numpy as np
import re
import os
from pathlib import Path
from colours import cMap
import gdal
from rasterio.crs import CRS
from rasterio.transform import Affine
from examineGeologz import geolNames
# decision tree
from sklearn import tree
import graphviz # to visualise graph
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
geolNames[-9999]='NaN'
win_dataPath = r'C://Users/galinavj/OneDrive - NTNU/thesisAnalysis/DecisionTreeFiles'
el_dataPath = '/Volumes/ElementsSE/thesisData/FCCclippedMsk2/'
decisionTree_folder = '/Volumes/ElementsSE/thesisData/decisionTrees/'
def setMskValue(inArray):
#print('setting mask value')
outArray = inArray.copy()
outArray[np.isnan(outArray)] = -9999
outArray[outArray == 0] = -9999
outArray[outArray < -999] = -9999
outArray = np.nan_to_num(outArray, nan=-9999,posinf=-9999,neginf=-9999)
return outArray
def readBands(filepath):
file = rio.open(filepath)
print('----File Information ----')
print(file.meta)
df_hh = file.read(1)
df_hv = file.read(2)
HH_nan = setMskValue(df_hh)
HV_nan = setMskValue(df_hv)
HH_non = np.nan_to_num(HH_nan, nan=-9999,posinf=-9999,neginf=-9999)
#HH_non[np.isnan(HH_non)] = -9999
HV_non = np.nan_to_num(HV_nan, nan=-9999,posinf=-9999,neginf=-9999)
#HV_non[np.isnan(HV_non)] = -9999
return HH_non, HV_non
def readBand(filepath):
file = rio.open(filepath)
print('----File Information ----')
print(file.meta)
b1_arr = file.read(1)
b1_nan = setMskValue(b1_arr)
return b1_nan
## reading input rasters
#----INPUT data
# DEM
# tifs of satellite img
geoFp = '/Volumes/ElementsSE/thesisData/Datasets/geologicalMap/geolTypesRaster_12600.tif' # contains 8 target group plus nan group
binFp = '/Volumes/ElementsSE/thesisData/Datasets/GlacierOutline/glimsBinary_12600.tif'
#---TARGET DEFINITION file---
#geology groups
def readTargetFile(targetFp=geoFp):
#target_f = 'geolMap_8groups.tif'
target_raster = rio.open(targetFp)
target_arr = target_raster.read(1)
target_arr_nan = setMskValue(target_arr)
target_msk_flat = target_arr_nan.flatten()
return target_msk_flat
geo_msk_flat = readTargetFile(geoFp)
bin_msk_raster = rio.open(binFp)
bin_msk_arr = bin_msk_raster.read(1)
binMsk_arr_nan = bin_msk_arr.copy()
binMsk_arr_nan[bin_msk_arr < -999] = -9999
bin_msk_flat = binMsk_arr_nan.flatten()
#---TEST data---
inDir = '/Volumes/ElementsSE/thesisData/FCCclippedMsk2/'
inFile = 'FCC_Sigma0_HHHV_20191009_clipped_msk_12600.tif'
snowProbFile = '/Volumes/ElementsSE/thesisData/validation/s2Mask/maskBool/s2mskAligned_new_12600.tif'
snwMsk = rio.open(snowProbFile)
snwMsk_arr = snwMsk.read(1)
snwprb_flat = snwMsk_arr.flatten()
def readAndStackBands(fp):
hh_msk,hv_msk = readBands(fp)
#print('HH read msk contains nan: '+str(np.isnan(hh_msk).any()))
#print('HV read msk contains nan: '+str(np.isnan(hv_msk).any()))
hh_msk_flat = hh_msk.flatten()
hv_msk_flat = hv_msk.flatten()
# --- probably don't need these as nan_to_num is supposed to solve it but keep them just in case ---
hh_msk_flat_nonan = hh_msk_flat.copy()
hh_msk_flat_nonan[np.isnan(hh_msk_flat_nonan)] = -9999
hv_msk_flat_nonan = hv_msk_flat.copy()
hv_msk_flat_nonan[np.isnan(hv_msk_flat_nonan)] = -9999
#print('HH contains nan: '+str(np.isnan(hh_msk_flat_nonan).any()))
#print('HV contains nan: '+str(np.isnan(hv_msk_flat_nonan).any()))
# =============================================================================
# stack hh hv arrays as input features for classification
#hhhv_stacked = np.stack((hh_msk_flat,hv_msk_flat),axis=1)
hhhv_stacked = np.stack((hh_msk_flat_nonan,hv_msk_flat_nonan),axis=1)
return hhhv_stacked
def readAndStackBandsInclSnwProb(fp,snwprobfp=''):
if len(snwprobfp)<3:
snwprob_flat = snwprb_flat
else:
snwprb = rio.open(snwprobfp)
snwprb_arr = snwprb.read(1)
snwprob_flat = snwprb_arr.flatten()
hh_msk,hv_msk = readBands(fp)
hh_msk_flat = hh_msk.flatten()
hv_msk_flat = hv_msk.flatten()
# stack hh hv arrays as input features for classification
stacked = np.stack((hh_msk_flat,hv_msk_flat,snwprob_flat),axis=1)
return stacked
def decTreeHHHV(fp=inDir+inFile,targetMsk=geo_msk_flat,tree_depth=3,b=True):
# reading bands, flatting them and preparing them for classification
hhhv_stacked=readAndStackBands(fp)
# Training decision tree
if b==True:
clf = tree.DecisionTreeClassifier(random_state=0, max_depth=tree_depth,class_weight='balanced')
clf.fit(hhhv_stacked,targetMsk)
else:
clf = tree.DecisionTreeClassifier(random_state=0, max_depth=tree_depth)
clf.fit(hhhv_stacked,targetMsk)
# to visualise
clf_classes = clf.classes_
classNames = []
for c in clf_classes:
classNames.append(geolNames[c])
clfTree_hhhv_dot = tree.export_graphviz(clf,feature_names=['HH','HV'],class_names=classNames)
graph = graphviz.Source(clfTree_hhhv_dot)
return clf, graph
def decTreeHHHVsnwprb(fp=inDir+inFile,targetMsk=bin_msk_flat,tree_depth=3,b=True):
stacked = readAndStackBandsInclSnwProb(fp,snwprobfp='')
# Training decision tree
if b==True:
clf = tree.DecisionTreeClassifier(random_state=0, max_depth=tree_depth,class_weight='balanced')
clf.fit(stacked,targetMsk)
else:
clf = tree.DecisionTreeClassifier(random_state=0, max_depth=tree_depth)
clf.fit(stacked,targetMsk)
# to visualise
clf_classes = clf.classes_
classNames = []
if len(clf_classes) > 4:
for c in clf_classes:
classNames.append(geolNames[c])
else:
classNames = ['NULL','Not Ice', 'Ice']
clfTree_hhhv_dot = tree.export_graphviz(clf,feature_names=['HH','HV', 'Snow Probability'],class_names=classNames)
graph = graphviz.Source(clfTree_hhhv_dot)
return clf, graph
def predictFromTree(inFp, dtree,outfn):
hhhv_stacked = readAndStackBands(inFp)
pred = dtree.predict(hhhv_stacked)
pred_reshaped = pred.reshape(-1,11908)
meta_outFile = {'driver': 'GTiff', 'dtype': 'float32', 'nodata': -9999.0, 'width': 11908, 'height': 12600, 'count': 1, 'crs': CRS.from_epsg(3413), 'transform': Affine(10.0, 0.0, -384702.1263054441,
0.0, -10.0, -2122443.806211936)}
with rio.open(outfn,'w',**meta_outFile) as outTif:
outTif.write(pred_reshaped,indexes=1)
def predictFromTreeSnwprb(inFp, dtree,outfn,snwPrbfp=''):
hhhv_stacked = readAndStackBandsInclSnwProb(inFp)
pred = dtree.predict(hhhv_stacked)
pred_reshaped = pred.reshape(-1,11908)
meta_outFile = {'driver': 'GTiff', 'dtype': 'float32', 'nodata': -9999.0, 'width': 11908, 'height': 12600, 'count': 1, 'crs': CRS.from_epsg(3413), 'transform': Affine(10.0, 0.0, -384702.1263054441,
0.0, -10.0, -2122443.806211936)}
with rio.open(outfn,'w',**meta_outFile) as outTif:
outTif.write(pred_reshaped,indexes=1)
def treeWithCrossVal(fp=inDir+inFile,target=bin_msk_flat,tree_depth=3,b=True):
hhhv_stacked=readAndStackBands(fp)
# split data in training and test set
X_train, X_test, y_train, y_test = train_test_split(hhhv_stacked,target,test_size=0.33, random_state=13)
#print(set(X_train[0]))
if b==True:
clf = tree.DecisionTreeClassifier(random_state=0, max_depth=tree_depth,class_weight='balanced')
clf.fit(X_train,y_train)
else:
clf = tree.DecisionTreeClassifier(random_state=0, max_depth=tree_depth)
clf.fit(X_train,y_train)
print('--Cross validation score for tree with depth '+str(tree_depth)+' on training file '+fp+'--')
print(cross_val_score(clf, X_test, y_test))
# to visualise
clf_classes = clf.classes_
classNames = []
if len(clf_classes) > 4:
for c in clf_classes:
classNames.append(geolNames[c])
else:
classNames = ['NULL','Not Ice', 'Ice']
clfTree_dot = tree.export_graphviz(clf,feature_names=['HH','HV'],class_names=classNames)
graph = graphviz.Source(clfTree_dot)
return clf, graph
dem_fp = '/Volumes/ElementsSE/thesisData/Datasets/DEM/arcticDEM_10m_12600.tif'
dem = readBand(dem_fp)
dem_flat = dem.flatten()
def treeCrossValInclDEM(fp=inDir+inFile,target=bin_msk_flat,tree_depth=3,b=True):
hh_msk,hv_msk = readBands(fp)
#print('HH read msk contains nan: '+str(np.isnan(hh_msk).any()))
#print('HV read msk contains nan: '+str(np.isnan(hv_msk).any()))
hh_msk_flat = hh_msk.flatten()
hv_msk_flat = hv_msk.flatten()
hhhvDEM_stacked = np.stack((hh_msk_flat,hv_msk_flat,dem_flat),axis=1)
# split data in training and test set
X_train, X_test, y_train, y_test = train_test_split(hhhvDEM_stacked,target,test_size=0.33, random_state=13)
#print(set(X_train[0]))
if b==True:
clf = tree.DecisionTreeClassifier(random_state=0, max_depth=tree_depth,class_weight='balanced')
clf.fit(X_train,y_train)
else:
clf = tree.DecisionTreeClassifier(random_state=0, max_depth=tree_depth)
clf.fit(X_train,y_train)
print('--Cross validation score for tree with depth '+str(tree_depth)+' on training file '+fp+'--')
print(cross_val_score(clf, X_test, y_test))
# to visualise
clf_classes = clf.classes_
classNames = []
if len(clf_classes) > 4:
for c in clf_classes:
classNames.append(geolNames[c])
else:
classNames = ['NULL','Not Ice', 'Ice']
clfTree_dot = tree.export_graphviz(clf,feature_names=['HH','HV','DEM'],class_names=classNames)
graph = graphviz.Source(clfTree_dot)
return clf, graph
def predictFromTreeDEM(fp, dtree,outfn):
hh_msk,hv_msk = readBands(fp)
#print('HH read msk contains nan: '+str(np.isnan(hh_msk).any()))
#print('HV read msk contains nan: '+str(np.isnan(hv_msk).any()))
hh_msk_flat = hh_msk.flatten()
hv_msk_flat = hv_msk.flatten()
hhhvDEM_stacked = np.stack((hh_msk_flat,hv_msk_flat,dem_flat),axis=1)
pred = dtree.predict(hhhvDEM_stacked)
pred_reshaped = pred.reshape(-1,11908)
meta_outFile = {'driver': 'GTiff', 'dtype': 'float32', 'nodata': -9999.0, 'width': 11908, 'height': 12600, 'count': 1, 'crs': CRS.from_epsg(3413), 'transform': Affine(10.0, 0.0, -384702.1263054441,
0.0, -10.0, -2122443.806211936)}
with rio.open(outfn,'w',**meta_outFile) as outTif:
outTif.write(pred_reshaped,indexes=1)