@@ -16,7 +16,7 @@ def __init__(self, path_results, dtw_inc=0.01, hrs_per=50, z_thresh=3.75):
16
16
# highest values (closest to 0) = most hours with lowest DTW
17
17
self .mat_dtw_sum = (self .mat_dtw * - 1 ).sum (axis = 1 )
18
18
self .summer = pd .date_range ('2012-06-01-00' , '2012-09-01-00' , freq = 'h' )
19
-
19
+
20
20
def _ccap_wetlands (self ):
21
21
""" Get CCAP wetlands df and full matrix grid """
22
22
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):
86
86
df_dry_dtw = pd .DataFrame (mat_dry_dtw .T , index = self .ts_yr_hr ).loc [self .summer , :]
87
87
mat_wet_dtw = df_wet_dtw .values .T
88
88
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' ]
90
90
91
91
print ('Finding optimum criteria; will take a bit' )
92
92
dtw_tests = np .arange (0 , 1 , self .dtw_inc )
@@ -153,68 +153,67 @@ def apply_indicator(self, seasonal=False):
153
153
154
154
def find_cells (self , dtw_thresh = 0.05 , hrs_thresh = 4442 ):
155
155
""" 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
177
178
# return df_passed_wets, df_passed_drys
178
- return df_low
179
+ return slr_dict
179
180
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 )
191
183
192
184
fig , axes = plt .subplots (ncols = 3 , sharey = True )
193
185
axe = axes .ravel ()
194
- # cm = plt.get_cmap('coolwarm')
195
186
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
198
195
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 ),
201
197
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 ]),
203
199
)
200
+ if test :
201
+ break
204
202
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 ])
206
204
207
205
cbar = fig .colorbar (im , cbar_ax , spacing = 'proportional' )
208
206
cbar .ax .get_yaxis ().set_ticks ([])
209
207
cbar .ax .set_xlabel ('Legend' , fontsize = 12 , labelpad = 10 )
210
208
cbar .ax .xaxis .set_label_position ('top' )
211
209
for j , lab in enumerate (['inactive' ,'wetland (CCAP)' ,'wetland(nonCCAP)' ,'upland' ]):
212
210
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' )
214
212
215
213
fig .subplots_adjust (left = 0.05 , right = 0.915 , wspace = 0.25 , hspace = 0.25 )
216
214
plt .show ()
217
215
216
+ ##### not using below this
218
217
def optimize (self , increment = 1 ):
219
218
""" Maximize the percent correctly identiified """
220
219
optimal = []
@@ -351,7 +350,6 @@ def dtw_wet_avg_ann(self):
351
350
df_drys = df_dtw [~ df_dtw .index .isin (self .df_wets .Zone )].dropna ()
352
351
return df_wets , df_drys
353
352
354
-
355
353
PATH_res = op .join (op .expanduser ('~' ), 'Google_Drive' ,
356
354
'WNC' , 'Wetlands_Paper' , 'Results_Default' )
357
355
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):
362
360
## nonseasonal indicators: dtw < 0.05; hrs_thresh>4443 --------- best
363
361
## seasonal indicators : dtw < 0.17; hrs_thresh > 1211
364
362
365
- # res.find_cells()
363
+ res .find_cells (0.05 , hrs_thresh = 4442 )
366
364
res .plot_wets_drys ()
0 commit comments