Skip to content

Commit 50b0bfa

Browse files
committed
Done with indicator
1 parent 5b59384 commit 50b0bfa

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

utils/Wetlands.py

+48-19
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,34 @@ def indicator_all(self, cutoff=-2500, show=False):
6363
# print ('Percent corretly identified: {} %\n'.format(round(performance, 3)))
6464
return (performance, count_correct, count_incorrect)
6565

66-
def make_indicator(self, dtw_inc=0.01, hrs_beg=4000):
67-
""" Make an indicator that captures over 90 % of CCAP wetlands """
66+
def make_indicator(self, dtw_inc=0.01, hrs_per=50, seasonal=False):
67+
"""
68+
Make an indicator by iterating over depth to water and hours at that dtw
69+
dtw_inc = dtw increment, use 0.01 for increased precision (expensive)
70+
hrs_per = percent of total hours to begin minimum threshold
71+
seasonal = search just summer?
72+
"""
73+
start = time.time()
6874
### select wetland from all dtw information
6975
mat_wet_dtw = self.mat_dtw[~self.mask_wet]
7076
mat_nonwet_dtw = self.mat_dtw[self.mask_wet]
7177
mat_dry_dtw = mat_nonwet_dtw[~np.isnan(mat_nonwet_dtw)].reshape(
7278
-1, mat_nonwet_dtw.shape[1])
79+
names = ['dtw_hrs_wet_dry.npy', 'dtw_hrs_wet_dry.df']
80+
## truncate for just summer
81+
if seasonal:
82+
summer = pd.date_range('2012-06-01-00', '2012-09-01-00', freq='h')
83+
df_wet_dtw1 = pd.DataFrame(mat_wet_dtw.T, index=self.ts_yr_hr)
84+
df_wet_dtw = pd.DataFrame(mat_wet_dtw.T, index=self.ts_yr_hr).loc[summer, :]
85+
86+
df_dry_dtw = pd.DataFrame(mat_dry_dtw.T, index=self.ts_yr_hr).loc[summer, :]
87+
mat_wet_dtw = df_wet_dtw.values.T
88+
mat_dry_dtw = df_dry_dtw.values.T
89+
names = ['dtw_hrs_wet_dry_summer.npy', 'dtw_hrs_wet_dry_summer.df']
7390

7491
print ('Finding optimum criteria; will take a bit')
7592
dtw_tests = np.arange(0, 1, dtw_inc)
76-
hrs_tests = range(hrs_beg, self.mat_dtw.shape[1])
93+
hrs_tests = range(int(np.floor(1./hrs_per)*self.mat_dtw.shape[1]), self.mat_dtw.shape[1])
7794
mat_all = np.zeros([len(dtw_tests) * len(hrs_tests), 7])
7895

7996
for i, dtw_test in enumerate(dtw_tests):
@@ -87,36 +104,52 @@ def make_indicator(self, dtw_inc=0.01, hrs_beg=4000):
87104

88105
mat_good = mat_all[mat_all[:,2]>0]
89106
mat_good[:, 3] = mat_good[:,2]/float(mat_wet_dtw.shape[0])
90-
mat_best = mat_good[mat_good[:,4] >= 0.90]
107+
mat_best = mat_good[mat_good[:,3] >= 0.50]
91108
mat_best[:, 5] = mat_best[:,4] / float(mat_dry_dtw.shape[0])
92109
mat_best[:, 6] = mat_best[:,3] / (1 - (mat_best[:,5]))
93110
colnames = ['dtw_thresh', 'hrs_thresh', 'n_wet', 'perWet', 'n_dry', 'perDry', 'perRatio']
94111
df_all = pd.DataFrame(mat_best, columns=colnames).sort_values(by='perRatio', ascending=False)
95112

96113
answered = False
114+
end = time.time()
97115
while not answered:
98116
overwrite = raw_input('Overwrite pickles? (y/n) ')
99117
if overwrite == 'y':
100-
np.save(op.join(self.path_data, 'dtw_hrs_wet_dry.npy'), mat_best)
101-
df_all.to_pickle(op.join(self.path_data, 'dtw_hrs_wet_dry.df'))
118+
np.save(op.join(self.path_data, names[0]), mat_best)
119+
df_all.to_pickle(op.join(self.path_data, names[1]))
102120
answered = True
103121
elif overwrite == 'n':
104122
print ('Not overwriting pickles')
105123
answered = True
106124
else:
107125
print ('Choose y or n')
108126

109-
return mat_all, df_all
127+
print ('Elapsed time: ~{} min'.format(round((end-start)/60.), 4))
110128

111-
def apply_indicator(self):
129+
def apply_indicator(self, seasonal=False):
112130
""" Analyze the indicator developed using make_indicator """
113-
mat_all = np.load(op.join(self.path_data, 'dtw_hrs_wet_dry.npy'))
114-
df_all = pd.read_pickle(op.join(self.path_data, 'dtw_hrs_wet_dry.df'))
115-
131+
if seasonal:
132+
names = ['dtw_hrs_wet_dry_summer.npy', 'dtw_hrs_wet_dry_summer.df']
133+
perWet_thresh = 0.61
134+
perDry_thresh = 0.35
135+
else:
136+
names = ['dtw_hrs_wet_dry.npy', 'dtw_hrs_wet_dry.df']
137+
perWet_thresh = 0.645
138+
perDry_thresh = 0.35
139+
mat_all = np.load(op.join(self.path_data, names[0]))
140+
df_all = pd.read_pickle(op.join(self.path_data, names[1]))
141+
# print (df_all.head(25))
116142
## do some cropping
117-
df_new = df_all[df_all.hrs_thresh > 5000].sort_values(by=['n_dry', 'n_wet'], ascending=[False, True])
143+
df_new = (df_all[((df_all.hrs_thresh > df_all.hrs_thresh.max()/2.) &
144+
(df_all.perWet > perWet_thresh) &
145+
(df_all.perDry < perDry_thresh))]
146+
.sort_values(by=['perDry', 'perWet'],
147+
ascending=[True, False]))
118148

119-
BB.print_all(df_new.head(250))
149+
### can get about 1/2 the wetlands and 1/4 of the uplands
150+
### best for all is ~ 65% wetlands, 35% of drylands
151+
### best for summer is ~ 61% wetlands and 34.4% of drylands
152+
BB.print_all(df_new)
120153

121154
def optimize(self, increment=1):
122155
""" Maximize the percent correctly identiified """
@@ -230,13 +263,9 @@ def dtw_wet_avg_ann(self):
230263
return df_wets, df_drys
231264

232265

233-
start = time.time()
234266
PATH_res = op.join(op.expanduser('~'), 'Google_Drive',
235267
'WNC', 'Wetlands_Paper', 'Results_Default')
236268
res = Wetlands(PATH_res)
237269
# res.optimize(increment=10)
238-
# res.make_indicator(dtw_inc=0.01, hrs_beg=4000)
239-
res.apply_indicator()
240-
end = time.time()
241-
242-
print ('Elapsed time: {} min'.format(round((end-start)/60.), 4))
270+
# res.make_indicator(dtw_inc=0.01, hrs_per=50, seasonal=True)
271+
res.apply_indicator(seasonal=True)

0 commit comments

Comments
 (0)