Skip to content

Commit 2d257f8

Browse files
committed
Plot all 3 with test
1 parent b359bde commit 2d257f8

File tree

1 file changed

+43
-45
lines changed

1 file changed

+43
-45
lines changed

utils/Wetlands.py

+43-45
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, path_results, dtw_inc=0.01, hrs_per=50, z_thresh=3.75):
1616
# highest values (closest to 0) = most hours with lowest DTW
1717
self.mat_dtw_sum = (self.mat_dtw * -1).sum(axis=1)
1818
self.summer = pd.date_range('2012-06-01-00', '2012-09-01-00', freq='h')
19-
19+
2020
def _ccap_wetlands(self):
2121
""" Get CCAP wetlands df and full matrix grid """
2222
df_subs = pd.read_csv(op.join(self.path_data, 'SWMM_subs.csv'))
@@ -86,7 +86,7 @@ def make_indicator(self, masked=True, seasonal=False):
8686
df_dry_dtw = pd.DataFrame(mat_dry_dtw.T, index=self.ts_yr_hr).loc[self.summer, :]
8787
mat_wet_dtw = df_wet_dtw.values.T
8888
mat_dry_dtw = df_dry_dtw.values.T
89-
names = ['dtw_hrs_wet_dry_summer1.npy', 'dtw_hrs_wet_dry_summer1.df']
89+
names = ['dtw_hrs_wet_dry_summer.npy', 'dtw_hrs_wet_dry_summer.df']
9090

9191
print ('Finding optimum criteria; will take a bit')
9292
dtw_tests = np.arange(0, 1, self.dtw_inc)
@@ -153,68 +153,67 @@ def apply_indicator(self, seasonal=False):
153153

154154
def find_cells(self, dtw_thresh=0.05, hrs_thresh=4442):
155155
""" Find locations that meet the threshold conditions """
156-
df_dtw = pd.DataFrame(self.mat_dtw, columns=self.ts_yr_hr)
157-
158-
## number of hrs below dtw threshold
159-
df_dtw['dtw_counts'] = ((df_dtw<=dtw_thresh).sum(axis=1))
160-
161-
## put true if passed hrs threshold
162-
df_dtw['wet_test'] = df_dtw.dtw_counts.apply(lambda x: True if x >= hrs_thresh else False)
163-
164-
## attach elevation data ; zones col is one indexed in df_subs
165-
df_dtw.index += 10001
166-
df_merged = df_dtw.merge(self.df_swmm.loc[:, ['Esurf', 'Majority']],
167-
left_index=True, right_index=True, how='outer')
168-
df_passed = df_merged[df_merged['wet_test']]
169-
170-
df_low = df_passed[df_passed.Esurf <= self.z_thresh]
171-
# df_passed_wets = df_low[df_low.index.isin(self.df_wets.Zone)]
172-
# df_passed_drys = df_low[~df_low.index.isin(self.df_wets.Zone)]
173-
df_low['dry'] = df_low.index.map(lambda x: -1 if x in self.df_wets.loc[:, 'Zone'].values else 0)
174-
175-
print ('Wets: {}'.format(df_low[df_low['dry']<0].shape[0]))
176-
print ('Drys: {}'.format(df_low[df_low['dry']==0].shape[0]))
156+
slr_dict = OrderedDict()
157+
for slr in self.slr_sh:
158+
mat_dtw = self._make_dtw(slr)[0]
159+
160+
df_dtw = pd.DataFrame(mat_dtw, columns=self.ts_yr_hr)
161+
162+
## number of hrs below dtw threshold
163+
df_dtw['dtw_counts'] = ((df_dtw<=dtw_thresh).sum(axis=1))
164+
165+
## put true if passed hrs threshold
166+
df_dtw['wet_test'] = df_dtw.dtw_counts.apply(lambda x: True if x >= hrs_thresh else False)
167+
## attach elevation data ; zones col is one indexed in df_subs
168+
df_dtw.index += 10001
169+
df_merged = df_dtw.merge(self.df_swmm.loc[:, ['Esurf', 'Majority']],
170+
left_index=True, right_index=True, how='outer')
171+
df_passed = df_merged[df_merged['wet_test']]
172+
df_low = df_passed[df_passed.Esurf <= self.z_thresh]
173+
df_low['dry'] = df_low.index.map(lambda x: -1 if x in self.df_wets.loc[:, 'Zone'].values else 0)
174+
175+
print ('Wets: {}'.format(df_low[df_low['dry']<0].shape[0]))
176+
print ('Drys: {}'.format(df_low[df_low['dry']==0].shape[0]))
177+
slr_dict[slr] = df_low
177178
# return df_passed_wets, df_passed_drys
178-
return df_low
179+
return slr_dict
179180

180-
181-
def plot_wets_drys(self):
182-
df_lows = self.find_cells(dtw_thresh=0.05, hrs_thresh=4442)
183-
184-
## change value for cells not considered (visualization purposes)
185-
df_subs = pd.DataFrame(index=self.df_swmm.index)
186-
df_subs['dry'] = df_subs.index.map(lambda x: df_lows.loc[x, 'dry']
187-
if x in df_lows.index.values else 1)
188-
189-
## converts dfs to matrixes
190-
mat_lows = res_base.fill_grid(df_subs.dry, fill_value=-2)
181+
def plot_wets_drys(self, test=False):
182+
slr_dict = self.find_cells(dtw_thresh=0.05, hrs_thresh=4442)
191183

192184
fig, axes = plt.subplots(ncols=3, sharey=True)
193185
axe = axes.ravel()
194-
# cm = plt.get_cmap('coolwarm')
195186
cm = mpl.colors.ListedColormap(['black', 'blue', 'green', 'gray'])
196-
# axe = axes.ravel()
197-
for i, ax in enumerate(axes.ravel()):
187+
for i, (slr, df) in enumerate(slr_dict.items()):
188+
df_subs = pd.DataFrame(index=self.df_swmm.index)
189+
## change value for cells not considered (visualization purposes)
190+
df_subs['dry'] = df_subs.index.map(lambda x: df.loc[x, 'dry']
191+
if x in df.index.values else 1)
192+
## converts dfs to matrixes
193+
mat_lows = res_base.fill_grid(df_subs.dry, fill_value=-2)
194+
## plot
198195
im = axe[i].imshow(mat_lows.reshape(self.nrows, self.ncols), cmap=cm)
199-
200-
axe[i].set(title='SLR: {}'.format(0.0),
196+
axe[i].set(title='SLR: {} m'.format(slr),
201197
xlabel='# Wetlands: {}'.format(
202-
df_lows[((df_lows['dry']==0) | (df_lows['dry']==1))].shape[0]),
198+
df[((df['dry']==0) | (df['dry']==1))].shape[0]),
203199
)
200+
if test:
201+
break
204202

205-
cbar_ax = fig.add_axes([0.945, 0.18, 0.025, 0.62])
203+
cbar_ax = fig.add_axes([0.945, 0.12, 0.025, 0.75])
206204

207205
cbar = fig.colorbar(im, cbar_ax, spacing='proportional')
208206
cbar.ax.get_yaxis().set_ticks([])
209207
cbar.ax.set_xlabel('Legend', fontsize=12, labelpad=10)
210208
cbar.ax.xaxis.set_label_position('top')
211209
for j, lab in enumerate(['inactive','wetland (CCAP)','wetland(nonCCAP)','upland']):
212210
cbar.ax.text(.5, (2 * j + 1) / 8.0, lab, ha='center', va='center',
213-
bbox=dict(facecolor='white', alpha=0.75), rotation=90)
211+
rotation=90, fontsize=12, color='white')
214212

215213
fig.subplots_adjust(left=0.05, right=0.915, wspace=0.25, hspace=0.25)
216214
plt.show()
217215

216+
##### not using below this
218217
def optimize(self, increment=1):
219218
""" Maximize the percent correctly identiified """
220219
optimal = []
@@ -351,7 +350,6 @@ def dtw_wet_avg_ann(self):
351350
df_drys = df_dtw[~df_dtw.index.isin(self.df_wets.Zone)].dropna()
352351
return df_wets, df_drys
353352

354-
355353
PATH_res = op.join(op.expanduser('~'), 'Google_Drive',
356354
'WNC', 'Wetlands_Paper', 'Results_Default')
357355
res = Wetlands(PATH_res, dtw_inc=0.01, hrs_per=50, z_thresh=3.75)
@@ -362,5 +360,5 @@ def dtw_wet_avg_ann(self):
362360
## nonseasonal indicators: dtw < 0.05; hrs_thresh>4443 --------- best
363361
## seasonal indicators : dtw < 0.17; hrs_thresh > 1211
364362

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

0 commit comments

Comments
 (0)