Skip to content

Commit 96741fb

Browse files
committed
Fix hr threshold bug
1 parent 2d257f8 commit 96741fb

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

utils/Wetlands.py

+22-22
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import pickle
55

66
class Wetlands(res_base):
7-
def __init__(self, path_results, dtw_inc=0.01, hrs_per=50, z_thresh=3.75):
7+
def __init__(self, path_results, z_thresh=3.75):
88
super(Wetlands, self).__init__(path_results)
99
self.path_res = path_results
10-
self.dtw_inc = dtw_inc
11-
self.hrs_per = 50
1210
self.z_thresh = 3.75
1311
self.mat_dtw, self.mat_dtw_masked = self._make_dtw()
1412
self.df_wets, self.mat_wets = self._ccap_wetlands()
@@ -50,7 +48,7 @@ def _make_dtw(self, slr=0.0):
5048

5149
return mat_dtw_trunc, mask_z
5250

53-
def make_indicator(self, masked=True, seasonal=False):
51+
def make_indicator(self, dtw_inc=0.01, hrs_per=50, masked=True, seasonal=False):
5452
"""
5553
Make an indicator by iterating over depth to water and hours at that dtw
5654
dtw_inc = dtw increment, use 0.01 for increased precision (expensive)
@@ -73,11 +71,6 @@ def make_indicator(self, masked=True, seasonal=False):
7371
mat_nonwet_dtw = mat_dtw[mask_wet]
7472
mat_dry_dtw = mat_nonwet_dtw[~np.isnan(mat_nonwet_dtw)].reshape(
7573
-1, mat_nonwet_dtw.shape[1])
76-
77-
## to view the amount of wetlands and drylands working with
78-
# print (mat_wet_dtw.shape)
79-
# print (mat_dry_dtw.shape)
80-
8174
## truncate for just summer
8275
if seasonal:
8376
df_wet_dtw1 = pd.DataFrame(mat_wet_dtw.T, index=self.ts_yr_hr)
@@ -86,11 +79,13 @@ def make_indicator(self, masked=True, seasonal=False):
8679
df_dry_dtw = pd.DataFrame(mat_dry_dtw.T, index=self.ts_yr_hr).loc[self.summer, :]
8780
mat_wet_dtw = df_wet_dtw.values.T
8881
mat_dry_dtw = df_dry_dtw.values.T
89-
names = ['dtw_hrs_wet_dry_summer.npy', 'dtw_hrs_wet_dry_summer.df']
82+
## fix this to add to previously made name
83+
names = ['{}_summer'.format(n) for n in names]
9084

85+
## to view the amount of wetlands and drylands working with
9186
print ('Finding optimum criteria; will take a bit')
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])
87+
dtw_tests = np.arange(0, 1, dtw_inc)
88+
hrs_tests = range(int(hrs_per/100.*self.mat_dtw.shape[1]), self.mat_dtw.shape[1])
9489
mat_all = np.zeros([len(dtw_tests) * len(hrs_tests), 7])
9590

9691
for i, dtw_test in enumerate(dtw_tests):
@@ -126,18 +121,25 @@ def make_indicator(self, masked=True, seasonal=False):
126121

127122
print ('Elapsed time: ~{} min'.format(round((end-start)/60.), 4))
128123

129-
def apply_indicator(self, seasonal=False):
124+
def apply_indicator(self, z=True, seasonal=False):
130125
""" Analyze the indicator developed using make_indicator """
131-
if seasonal:
126+
if seasonal and not z:
132127
names = ['dtw_hrs_wet_dry_summer.npy', 'dtw_hrs_wet_dry_summer.df']
133128
perWet_thresh = 0.61
134129
perDry_thresh = 0.35
135-
else:
130+
elif not z:
136131
names = ['dtw_hrs_wet_dry.npy', 'dtw_hrs_wet_dry.df']
137132
perWet_thresh = 0.645
138133
perDry_thresh = 0.35
134+
elif z and not seasonal:
135+
names = ['dtw_hrs_wet_dry_masked.npy', 'dtw_hrs_wet_dry_masked.df']
136+
perWet_thresh = 0.7
137+
perDry_thresh = 0.4
138+
139139
mat_all = np.load(op.join(self.path_data, names[0]))
140140
df_all = pd.read_pickle(op.join(self.path_data, names[1]))
141+
print (df_all.shape)
142+
return
141143
# print (df_all.head(25))
142144
## do some cropping
143145
df_new = (df_all[((df_all.hrs_thresh > df_all.hrs_thresh.max()/2.) &
@@ -197,8 +199,7 @@ def plot_wets_drys(self, test=False):
197199
xlabel='# Wetlands: {}'.format(
198200
df[((df['dry']==0) | (df['dry']==1))].shape[0]),
199201
)
200-
if test:
201-
break
202+
if test: break
202203

203204
cbar_ax = fig.add_axes([0.945, 0.12, 0.025, 0.75])
204205

@@ -352,13 +353,12 @@ def dtw_wet_avg_ann(self):
352353

353354
PATH_res = op.join(op.expanduser('~'), 'Google_Drive',
354355
'WNC', 'Wetlands_Paper', 'Results_Default')
355-
res = Wetlands(PATH_res, dtw_inc=0.01, hrs_per=50, z_thresh=3.75)
356+
res = Wetlands(PATH_res, z_thresh=3.75)
356357
# res.optimize(increment=10)
357-
# res.make_indicator(masked=True, seasonal=False)
358-
# res.apply_indicator(seasonal=False)
358+
res.make_indicator(dtw_inc=0.01, hrs_per=50, masked=True, seasonal=False)
359359

360360
## nonseasonal indicators: dtw < 0.05; hrs_thresh>4443 --------- best
361361
## seasonal indicators : dtw < 0.17; hrs_thresh > 1211
362362

363-
res.find_cells(0.05, hrs_thresh=4442)
364-
res.plot_wets_drys()
363+
# res.find_cells(0.05, hrs_thresh=4442)
364+
# res.plot_wets_drys()

0 commit comments

Comments
 (0)