Skip to content

Commit 81f7f11

Browse files
committed
Elevation Threshold before Optimization
1 parent 0652299 commit 81f7f11

File tree

1 file changed

+66
-48
lines changed

1 file changed

+66
-48
lines changed

utils/Wetlands.py

+66-48
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import pickle
55

66
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):
88
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()
1215
self.mask_wet = np.isnan(self.mat_wets.reshape(-1))
1316
# highest values (closest to 0) = most hours with lowest DTW
1417
self.mat_dtw_sum = (self.mat_dtw * -1).sum(axis=1)
@@ -28,61 +31,53 @@ def _ccap_wetlands(self):
2831
return (df_wet, mat_wetlands)
2932

3033
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 """
3235
mat_heads = np.load(op.join(self.path_picks, 'swmm_heads_grid_{}.npy'
3336
.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)
3739

3840
### truncate init conditions & transpose
3941
mat_dtw_trunc = mat_dtw[self.ts_st:self.ts_end].T
4042

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])
4248

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.))
6350

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
6852

69-
def make_indicator(self, dtw_inc=0.1, hrs_per=50, seasonal=False):
53+
def make_indicator(self, masked=True, seasonal=False):
7054
"""
7155
Make an indicator by iterating over depth to water and hours at that dtw
7256
dtw_inc = dtw increment, use 0.01 for increased precision (expensive)
7357
hrs_per = percent of total hours to begin minimum threshold
7458
seasonal = search just summer?
7559
"""
7660
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]
8074
mat_dry_dtw = mat_nonwet_dtw[~np.isnan(mat_nonwet_dtw)].reshape(
8175
-1, mat_nonwet_dtw.shape[1])
8276

77+
## to view the amount of wetlands and drylands working with
78+
# print (mat_wet_dtw.shape)
79+
# print (mat_dry_dtw.shape)
8380

84-
85-
names = ['dtw_hrs_wet_dry1.npy', 'dtw_hrs_wet_dry1.df']
8681
## truncate for just summer
8782
if seasonal:
8883
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):
9489
names = ['dtw_hrs_wet_dry_summer1.npy', 'dtw_hrs_wet_dry_summer1.df']
9590

9691
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])
9994
mat_all = np.zeros([len(dtw_tests) * len(hrs_tests), 7])
10095

10196
for i, dtw_test in enumerate(dtw_tests):
@@ -156,7 +151,7 @@ def apply_indicator(self, seasonal=False):
156151
### best for summer is ~ 61% wetlands and 34.4% of drylands
157152
BB.print_all(df_new)
158153

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):
160155
""" Find locations that meet the threshold conditions """
161156
df_dtw = pd.DataFrame(self.mat_dtw, columns=self.ts_yr_hr)
162157

@@ -179,9 +174,6 @@ def find_cells(self, dtw_thresh=0.05, hrs_thresh=4442, z_thresh=1.0):
179174
print ('Wets: {}'.format(df_passed_wets.shape[0]))
180175
print ('Drys: {}'.format(df_passed_drys.shape[0]))
181176

182-
183-
184-
185177
def optimize(self, increment=1):
186178
""" Maximize the percent correctly identiified """
187179
optimal = []
@@ -231,6 +223,32 @@ def plot_indicators(self, cut=-2500):
231223
plt.show()
232224

233225
### 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
234252
def dtw_wet_all(self, transpose=False):
235253
""" Separate ALL dtw / cells / times by wetlands/nonwetland """
236254
## convert to dtw for subsetting
@@ -294,12 +312,12 @@ def dtw_wet_avg_ann(self):
294312

295313
PATH_res = op.join(op.expanduser('~'), 'Google_Drive',
296314
'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)
298316
# 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)
300318
# res.apply_indicator(seasonal=False)
301319

302320
## nonseasonal indicators: dtw < 0.05; hrs_thresh>4443 --------- best
303321
## seasonal indicators : dtw < 0.17; hrs_thresh > 1211
304322

305-
res.find_cells()
323+
# res.find_cells()

0 commit comments

Comments
 (0)