4
4
import pickle
5
5
6
6
class Wetlands (res_base ):
7
- def __init__ (self , path_results ):
7
+ def __init__ (self , path_results , dtw_inc = 0.01 , hrs_per = 50 , z_thresh = 3.75 ):
8
8
super (Wetlands , self ).__init__ (path_results )
9
- self .path_res = path_results
10
- self .mat_dtw = self ._make_dtw ()
11
- self .df_wets , self .mat_wets = self ._ccap_wetlands ()
9
+ self .path_res = path_results
10
+ self .dtw_inc = dtw_inc
11
+ self .hrs_per = 50
12
+ self .z_thresh = 3.75
13
+ self .mat_dtw , self .mat_dtw_masked = self ._make_dtw ()
14
+ self .df_wets , self .mat_wets = self ._ccap_wetlands ()
12
15
self .mask_wet = np .isnan (self .mat_wets .reshape (- 1 ))
13
16
# highest values (closest to 0) = most hours with lowest DTW
14
17
self .mat_dtw_sum = (self .mat_dtw * - 1 ).sum (axis = 1 )
@@ -28,61 +31,53 @@ def _ccap_wetlands(self):
28
31
return (df_wet , mat_wetlands )
29
32
30
33
def _make_dtw (self , slr = 0.0 ):
31
- """ dtw, all locations all times """
34
+ """ dtw, all locations all times; z threshold cuts elevations, grabs 84% of wetlands """
32
35
mat_heads = np .load (op .join (self .path_picks , 'swmm_heads_grid_{}.npy'
33
36
.format (slr )))
34
- mat_z = (np .load (op .join (self .path_data , 'Land_Z.npy' ))
35
- .reshape (self .nrows , self .ncols ))
36
- mat_dtw = (mat_z - mat_heads ).reshape (mat_heads .shape [0 ], - 1 )
37
+ mat_z = np .load (op .join (self .path_data , 'Land_Z.npy' ))
38
+ mat_dtw = (mat_z .reshape (self .nrows , self .ncols ) - mat_heads ).reshape (mat_heads .shape [0 ], - 1 )
37
39
38
40
### truncate init conditions & transpose
39
41
mat_dtw_trunc = mat_dtw [self .ts_st :self .ts_end ].T
40
42
41
- return mat_dtw_trunc
43
+ ### truncate by elevations
44
+ ## need to keep track of index
45
+ mat_dtw_indexed = np .zeros ([mat_dtw_trunc .shape [0 ], mat_dtw_trunc .shape [1 ]+ 1 ])
46
+ mat_dtw_indexed [:, :- 1 ] = mat_dtw_trunc
47
+ mat_dtw_indexed [:, - 1 ] = np .arange (10001 , 10001 + mat_dtw_trunc .shape [0 ])
42
48
43
- def indicator_all (self , cutoff = - 2500 , show = False ):
44
- """ Compare results form 'developing indicator' to actual CCAP wetlands """
45
- ## get just highest cells (longest time at lowest DTW)
46
- mat_indicator = np .where (self .mat_dtw_sum <= cutoff , np .nan , self .mat_dtw_sum )
47
- if show :
48
- print ('Indicated cells: {}' .format (np .count_nonzero (~ np .isnan (mat_indicator ))))
49
- print ('Wetland cells: {}' .format (np .count_nonzero (~ np .isnan (self .mat_wets ))))
50
- return mat_indicator
51
-
52
- mat_ind = mat_indicator [~ self .mask_wet & ~ np .isnan (mat_indicator )]
53
-
54
- df_ind = pd .DataFrame (mat_indicator .reshape (- 1 )).dropna ()
55
- df_wet = pd .DataFrame (self .mat_wets .reshape (- 1 )).dropna ()
56
-
57
- ## get count of how many correct
58
- count_correct = (len (mat_ind ))
59
- ## get count of how many incorrect
60
- count_incorrect = (len (mat_indicator [~ np .isnan (mat_indicator )]) - len (mat_ind ))
61
- # print ('Correct: {}'.format(count_correct))
62
- # print ('Incorrect: {}'.format(count_incorrect))
49
+ mask_z = ((mat_z <= self .z_thresh ) & (mat_z > 0. ))
63
50
64
- # performance = (float(count_correct) / float(count_incorrect) * 100.
65
- performance = (count_correct - count_incorrect ) / float (np .count_nonzero (~ mask_wet )) * 100
66
- # print ('Percent corretly identified: {} %\n'.format(round(performance, 3)))
67
- return (performance , count_correct , count_incorrect )
51
+ return mat_dtw_trunc , mask_z
68
52
69
- def make_indicator (self , dtw_inc = 0.1 , hrs_per = 50 , seasonal = False ):
53
+ def make_indicator (self , masked = True , seasonal = False ):
70
54
"""
71
55
Make an indicator by iterating over depth to water and hours at that dtw
72
56
dtw_inc = dtw increment, use 0.01 for increased precision (expensive)
73
57
hrs_per = percent of total hours to begin minimum threshold
74
58
seasonal = search just summer?
75
59
"""
76
60
start = time .time ()
77
- ### select wetland from all dtw information
78
- mat_wet_dtw = self .mat_dtw [~ self .mask_wet ]
79
- mat_nonwet_dtw = self .mat_dtw [self .mask_wet ]
61
+ if masked :
62
+ ### chop off cells whose elevation is too high
63
+ mat_dtw = self .mat_dtw [self .mat_dtw_masked ]
64
+ mask_wet = self .mask_wet [self .mat_dtw_masked ]
65
+ names = ['dtw_hrs_wet_dry_masked.npy' , 'dtw_hrs_wet_dry_masked.df' ]
66
+ else :
67
+ mat_dtw = self .mat_dtw
68
+ mask_wet = self .mask_wet
69
+ names = ['dtw_hrs_wet_dry.npy' , 'dtw_hrs_wet_dry.df' ]
70
+
71
+ ### select wetland/nonwetland from all dtw information
72
+ mat_wet_dtw = mat_dtw [~ mask_wet ]
73
+ mat_nonwet_dtw = mat_dtw [mask_wet ]
80
74
mat_dry_dtw = mat_nonwet_dtw [~ np .isnan (mat_nonwet_dtw )].reshape (
81
75
- 1 , mat_nonwet_dtw .shape [1 ])
82
76
77
+ ## to view the amount of wetlands and drylands working with
78
+ # print (mat_wet_dtw.shape)
79
+ # print (mat_dry_dtw.shape)
83
80
84
-
85
- names = ['dtw_hrs_wet_dry1.npy' , 'dtw_hrs_wet_dry1.df' ]
86
81
## truncate for just summer
87
82
if seasonal :
88
83
df_wet_dtw1 = pd .DataFrame (mat_wet_dtw .T , index = self .ts_yr_hr )
@@ -94,8 +89,8 @@ def make_indicator(self, dtw_inc=0.1, hrs_per=50, seasonal=False):
94
89
names = ['dtw_hrs_wet_dry_summer1.npy' , 'dtw_hrs_wet_dry_summer1.df' ]
95
90
96
91
print ('Finding optimum criteria; will take a bit' )
97
- dtw_tests = np .arange (0 , 1 , dtw_inc )
98
- hrs_tests = range (int (np .floor (1. / hrs_per )* self .mat_dtw .shape [1 ]), self .mat_dtw .shape [1 ])
92
+ dtw_tests = np .arange (0 , 1 , self . dtw_inc )
93
+ hrs_tests = range (int (np .floor (1. / self . hrs_per )* self .mat_dtw .shape [1 ]), self .mat_dtw .shape [1 ])
99
94
mat_all = np .zeros ([len (dtw_tests ) * len (hrs_tests ), 7 ])
100
95
101
96
for i , dtw_test in enumerate (dtw_tests ):
@@ -156,7 +151,7 @@ def apply_indicator(self, seasonal=False):
156
151
### best for summer is ~ 61% wetlands and 34.4% of drylands
157
152
BB .print_all (df_new )
158
153
159
- def find_cells (self , dtw_thresh = 0.05 , hrs_thresh = 4442 , z_thresh = 1.0 ):
154
+ def find_cells (self , dtw_thresh = 0.01 , hrs_thresh = 4442 ):
160
155
""" Find locations that meet the threshold conditions """
161
156
df_dtw = pd .DataFrame (self .mat_dtw , columns = self .ts_yr_hr )
162
157
@@ -179,9 +174,6 @@ def find_cells(self, dtw_thresh=0.05, hrs_thresh=4442, z_thresh=1.0):
179
174
print ('Wets: {}' .format (df_passed_wets .shape [0 ]))
180
175
print ('Drys: {}' .format (df_passed_drys .shape [0 ]))
181
176
182
-
183
-
184
-
185
177
def optimize (self , increment = 1 ):
186
178
""" Maximize the percent correctly identiified """
187
179
optimal = []
@@ -231,6 +223,32 @@ def plot_indicators(self, cut=-2500):
231
223
plt .show ()
232
224
233
225
### probably useless
226
+ def indicator_all (self , cutoff = - 2500 , show = False ):
227
+ """ Compare results form 'developing indicator' to actual CCAP wetlands """
228
+ ## get just highest cells (longest time at lowest DTW)
229
+ mat_indicator = np .where (self .mat_dtw_sum <= cutoff , np .nan , self .mat_dtw_sum )
230
+ if show :
231
+ print ('Indicated cells: {}' .format (np .count_nonzero (~ np .isnan (mat_indicator ))))
232
+ print ('Wetland cells: {}' .format (np .count_nonzero (~ np .isnan (self .mat_wets ))))
233
+ return mat_indicator
234
+
235
+ mat_ind = mat_indicator [~ self .mask_wet & ~ np .isnan (mat_indicator )]
236
+
237
+ df_ind = pd .DataFrame (mat_indicator .reshape (- 1 )).dropna ()
238
+ df_wet = pd .DataFrame (self .mat_wets .reshape (- 1 )).dropna ()
239
+
240
+ ## get count of how many correct
241
+ count_correct = (len (mat_ind ))
242
+ ## get count of how many incorrect
243
+ count_incorrect = (len (mat_indicator [~ np .isnan (mat_indicator )]) - len (mat_ind ))
244
+ # print ('Correct: {}'.format(count_correct))
245
+ # print ('Incorrect: {}'.format(count_incorrect))
246
+
247
+ # performance = (float(count_correct) / float(count_incorrect) * 100.
248
+ performance = (count_correct - count_incorrect ) / float (np .count_nonzero (~ mask_wet )) * 100
249
+ # print ('Percent corretly identified: {} %\n'.format(round(performance, 3)))
250
+ return (performance , count_correct , count_incorrect )
251
+ ### probably useless
234
252
def dtw_wet_all (self , transpose = False ):
235
253
""" Separate ALL dtw / cells / times by wetlands/nonwetland """
236
254
## convert to dtw for subsetting
@@ -294,12 +312,12 @@ def dtw_wet_avg_ann(self):
294
312
295
313
PATH_res = op .join (op .expanduser ('~' ), 'Google_Drive' ,
296
314
'WNC' , 'Wetlands_Paper' , 'Results_Default' )
297
- res = Wetlands (PATH_res )
315
+ res = Wetlands (PATH_res , dtw_inc = 0.01 , hrs_per = 50 , z_thresh = 3.75 )
298
316
# res.optimize(increment=10)
299
- # res.make_indicator(dtw_inc=0.01, hrs_per=50, seasonal=True )
317
+ res .make_indicator (masked = True , seasonal = False )
300
318
# res.apply_indicator(seasonal=False)
301
319
302
320
## nonseasonal indicators: dtw < 0.05; hrs_thresh>4443 --------- best
303
321
## seasonal indicators : dtw < 0.17; hrs_thresh > 1211
304
322
305
- res .find_cells ()
323
+ # res.find_cells()
0 commit comments