Skip to content

Commit 5b59384

Browse files
committed
Refactor custom_optimization | prompt for overwrite
1 parent f820dc1 commit 5b59384

File tree

3 files changed

+106
-49
lines changed

3 files changed

+106
-49
lines changed

bb@69.141.206.241

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
syntax on
2+
3+
" converting block to _ in command/insert mode
4+
if &term =~ "screen*"
5+
let &t_ti.="\eP\e[2 q\e\\"
6+
let &t_SI.="\eP\e[5 q\e\\"
7+
let &t_EI.="\eP\e[2 q\e\\"
8+
let &t_SR.="\eP\e[3 q\e\\"
9+
10+
"let &t_te.="\eP\e[0 q\e\\"
11+
12+
else
13+
let &t_ti.="\<Esc>[2 q"
14+
let &t_SI.="\<Esc>[5 q"
15+
let &t_EI.="\<Esc>[2 q"
16+
let &t_SR.="\<Esc>[3 q"
17+
"let &t_te.="\<Esc>[6 q"
18+
19+
endif
20+
21+
" Recent versions of xterm (282 or above) also support
22+
" 0 -> blinking block
23+
" 1 -> blinking block
24+
" 2 -> solid block
25+
" 3 -> blinking underscore
26+
" 4 -> solid underscore
27+
" 5 -> blinking vertical bar
28+
" 6 -> solid vertical bar
29+
30+
set number
31+
set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab
32+
set colorcolumn=81
33+
set noesckeys
34+
35+
" underline in insert mode
36+
"" :autocmd InsertEnter,InsertLeave * set cul!
37+
38+
" Enable cursor line position tracking:
39+
:set cursorline
40+
" Remove the underline from enabling cursorline:
41+
:highlight clear CursorLine
42+
43+
" Set line numbering to red background:
44+
:autocmd InsertEnter,InsertLeave * :highlight CursorLineNR ctermbg=darkblue
45+
46+
" Folding
47+
set foldmethod=indent
48+
set foldnestmax=10
49+
set nofoldenable
50+
set foldlevel=2
51+

utils/Wetlands.py

+44-49
Original file line numberDiff line numberDiff line change
@@ -63,65 +63,60 @@ 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 indicator_wets_only(self, dtw_inc=0.01, hrs_beg=4000, make_new=False):
66+
def make_indicator(self, dtw_inc=0.01, hrs_beg=4000):
6767
""" Make an indicator that captures over 90 % of CCAP wetlands """
6868
### select wetland from all dtw information
6969
mat_wet_dtw = self.mat_dtw[~self.mask_wet]
7070
mat_nonwet_dtw = self.mat_dtw[self.mask_wet]
7171
mat_dry_dtw = mat_nonwet_dtw[~np.isnan(mat_nonwet_dtw)].reshape(
7272
-1, mat_nonwet_dtw.shape[1])
7373

74-
if op.exists(op.join(self.path_data, 'dtw_hrs_wet_dry.npy')) and not make_new:
75-
mat_all = np.load(op.join(self.path_data, 'dtw_hrs_wet_dry.npy'))
76-
df_all = pd.read_pickle(op.join(self.path_data, 'dtw_hrs_wet_dry.df'))
77-
print ('Loaded mat and df of dtw/hrs for wet and drylands')
78-
79-
else:
80-
print ('Finding optimum criteria; will take a bit')
81-
dtw_tests = np.arange(0, 1, dtw_inc)
82-
hrs_tests = range(hrs_beg, self.mat_dtw.shape[1])
83-
mat_all = np.zeros([len(dtw_tests) * len(hrs_tests), 7])
84-
85-
for i, dtw_test in enumerate(dtw_tests):
86-
for j, hrs_test in enumerate(hrs_tests):
87-
res_wet = ((mat_wet_dtw <= dtw_test).sum(axis=1) > hrs_test).sum()
88-
res_dry = ((mat_dry_dtw <= dtw_test).sum(axis=1) > hrs_test).sum()
89-
mat_all[i*len(hrs_tests)+j, 0] = dtw_test
90-
mat_all[i*len(hrs_tests)+j, 1] = hrs_test
91-
mat_all[i*len(hrs_tests)+j, 2] = res_wet
92-
mat_all[i*len(hrs_tests)+j, 4] = res_dry
93-
94-
mat_good = mat_all[mat_all[:,2]>0]
95-
mat_good[:, 3] = mat_good[:,2]/float(mat_wet_dtw.shape[0])
96-
mat_best = mat_good[mat_good[:,4] >= 0.90]
97-
mat_best[:, 5] = mat_best[:,4] / float(mat_dry_dtw.shape[0])
98-
mat_best[:, 6] = mat_best[:,3] / (1 - (mat_best[:,5]))
99-
colnames = ['dtw_thresh', 'hrs_thresh', 'n_wet', 'perWet', 'n_dry', 'perDry', 'perRatio']
100-
df_all = pd.DataFrame(mat_best, columns=colnames).sort_values(by='perRatio', ascending=False)
101-
102-
np.save(op.join(self.path_data, 'dtw_hrs_wet_dry.npy'), mat_best)
103-
df_all.to_pickle(op.join(self.path_data, 'dtw_hrs_wet_dry.df'))
104-
105-
## do some cropping
106-
df_new = df_all[df_all.hrs_thresh > 7000]
107-
BB.print_all (df_new.head(250))
74+
print ('Finding optimum criteria; will take a bit')
75+
dtw_tests = np.arange(0, 1, dtw_inc)
76+
hrs_tests = range(hrs_beg, self.mat_dtw.shape[1])
77+
mat_all = np.zeros([len(dtw_tests) * len(hrs_tests), 7])
78+
79+
for i, dtw_test in enumerate(dtw_tests):
80+
for j, hrs_test in enumerate(hrs_tests):
81+
res_wet = ((mat_wet_dtw <= dtw_test).sum(axis=1) > hrs_test).sum()
82+
res_dry = ((mat_dry_dtw <= dtw_test).sum(axis=1) > hrs_test).sum()
83+
mat_all[i*len(hrs_tests)+j, 0] = dtw_test
84+
mat_all[i*len(hrs_tests)+j, 1] = hrs_test
85+
mat_all[i*len(hrs_tests)+j, 2] = res_wet
86+
mat_all[i*len(hrs_tests)+j, 4] = res_dry
87+
88+
mat_good = mat_all[mat_all[:,2]>0]
89+
mat_good[:, 3] = mat_good[:,2]/float(mat_wet_dtw.shape[0])
90+
mat_best = mat_good[mat_good[:,4] >= 0.90]
91+
mat_best[:, 5] = mat_best[:,4] / float(mat_dry_dtw.shape[0])
92+
mat_best[:, 6] = mat_best[:,3] / (1 - (mat_best[:,5]))
93+
colnames = ['dtw_thresh', 'hrs_thresh', 'n_wet', 'perWet', 'n_dry', 'perDry', 'perRatio']
94+
df_all = pd.DataFrame(mat_best, columns=colnames).sort_values(by='perRatio', ascending=False)
95+
96+
answered = False
97+
while not answered:
98+
overwrite = raw_input('Overwrite pickles? (y/n) ')
99+
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'))
102+
answered = True
103+
elif overwrite == 'n':
104+
print ('Not overwriting pickles')
105+
answered = True
106+
else:
107+
print ('Choose y or n')
108108

109109
return mat_all, df_all
110110

111-
### possibly deprecated since optimizing with indicator_wets
112111
def apply_indicator(self):
113-
""" Test the indicator developed using indicator_wets_only on drys """
114-
### eventually return these from other func
115-
dtw_thresh = 0.301
116-
hrs_thresh = 7000
112+
""" 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'))
117115

118-
## get only dryland cells
119-
mat_nonwet_dtw = self.mat_dtw[self.mask_wet]
120-
mat_dry_dtw = mat_nonwet_dtw[~np.isnan(mat_nonwet_dtw)].reshape(-1, mat_nonwet_dtw.shape[1])
116+
## do some cropping
117+
df_new = df_all[df_all.hrs_thresh > 5000].sort_values(by=['n_dry', 'n_wet'], ascending=[False, True])
121118

122-
# apply conditions
123-
result = ((mat_dry_dtw <= dtw_thresh).sum(axis=1) > hrs_thresh).sum()
124-
print (result)
119+
BB.print_all(df_new.head(250))
125120

126121
def optimize(self, increment=1):
127122
""" Maximize the percent correctly identiified """
@@ -234,14 +229,14 @@ def dtw_wet_avg_ann(self):
234229
df_drys = df_dtw[~df_dtw.index.isin(df_wet.Zone)].dropna()
235230
return df_wets, df_drys
236231

232+
237233
start = time.time()
238234
PATH_res = op.join(op.expanduser('~'), 'Google_Drive',
239235
'WNC', 'Wetlands_Paper', 'Results_Default')
240236
res = Wetlands(PATH_res)
241237
# res.optimize(increment=10)
242-
res.indicator_wets_only(dtw_inc=0.01, hrs_beg=4000, make_new=True)
243-
# res.apply_indicator()
244-
# res.minimum_drys(make_new=True)
238+
# res.make_indicator(dtw_inc=0.01, hrs_beg=4000)
239+
res.apply_indicator()
245240
end = time.time()
246241

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

utils/test.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
answered = False
2+
while not answered:
3+
overwrite = raw_input('Overwrite pickles? (y/n) ')
4+
if overwrite == 'y':
5+
print ('chose yes')
6+
answered = True
7+
elif overwrite == 'n':
8+
print ('Not overwriting pickles')
9+
answered = True
10+
else:
11+
print ('Choose y or n')

0 commit comments

Comments
 (0)