forked from RS-PRISMATIC/PRISMATIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
336 lines (297 loc) · 18.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import logging
import hydra
import os
os.environ['R_HOME'] = os.path.join(os.environ['CONDA_PREFIX'], 'lib/R')
# # when adding new content to main.py
# # import functions at top of main.py
# # add functions to run in body of main.py
# # track in utils.py
# # track rerun in sites.yaml
# # add to function_workflow.drawio and data structure diagram
# # push to github
from utils.utils import build_cache_site, build_cache_all, force_rerun
from initialize.inventory import download_veg_structure_data, \
download_trait_table, \
prep_veg_structure
from initialize.plots import download_polygons, \
prep_polygons
from initialize.lidar import download_lidar, \
clip_lidar_by_plots, \
normalize_laz
from initialize.biomass import prep_biomass
from initialize.lad import prep_lad
from initialize.hyperspectral import download_hyperspectral, \
generate_pft_reference, \
prep_manual_training_data, \
prep_aop_imagery, \
extract_spectra_from_polygon, \
correct_flightlines, \
train_pft_classifier
from initialize.generate_initial_conditions import generate_initial_conditions
log = logging.getLogger(__name__)
@hydra.main(version_base='1.2', config_path='conf', config_name='config')
def main(cfg):
log.info(f'Run with configuration: {cfg}')
data_raw_aop_path = cfg.paths.data_raw_aop_path
data_raw_inv_path = cfg.paths.data_raw_inv_path
data_int_path = cfg.paths.data_int_path
data_final_path = cfg.paths.data_final_path
# Parameters to change manually
use_case = cfg.others.use_case
ic_type = cfg.others.ic_type
hs_type = cfg.others.hs_type
month_window= cfg.others.month_window
n_plots = cfg.others.n_plots
plot_length = cfg.others.plot_length
px_thresh = cfg.others.px_thresh
ntree = cfg.others.ntree
min_distance = cfg.others.min_distance
use_tiles_w_veg = cfg.others.use_tiles_w_veg
randomMinSamples = cfg.others.randomMinSamples
aggregate_from_1m_to_2m_res = cfg.others.aggregate_from_1m_to_2m_res
independentValidationSet = cfg.others.independentValidationSet
pcaInsteadOfWavelengths = cfg.others.pcaInsteadOfWavelengths
# balance_training_to_min_PFT = cfg.others.balance_training_to_min_PFT
global_force_rerun = cfg.sites.global_run_params.force_rerun
global_run = cfg.sites.global_run_params.run
if isinstance(global_run, str):
global_run = [global_run]
if use_case=="train":
# First download/process raw data for all site-years to get stacked AOP product
for site, v in cfg.sites.run.items():
print(cfg.sites.run.items())
if not global_run or site in global_run:
for year_inventory, p in v.items():
rerun_status = {}
for k, v in p.force_rerun.items():
rerun_status[k] = v or global_force_rerun.get(k, False)
year_aop = p.year_aop
log.info(f'Download raw data for site: {site}, '
f'year: {year_inventory}, year_aop: {year_aop}, '
f'with rerun status: {rerun_status}')
cache = build_cache_site(site=site,
year_inventory=year_inventory,
year_aop=year_aop,
data_raw_aop_path=data_raw_aop_path,
data_raw_inv_path=data_raw_inv_path,
data_int_path=data_int_path,
hs_type=hs_type)
# download lidar
laz_path, tif_path = (force_rerun(cache, force=rerun_status)
(download_lidar)
(site=site,
year=year_aop,
lidar_path=data_raw_aop_path,
use_tiles_w_veg=use_tiles_w_veg))
# download hs data
hs_path = (force_rerun(cache, force=rerun_status)
(download_hyperspectral)
(site=site,
year=year_aop,
data_raw_aop_path=data_raw_aop_path,
hs_type=hs_type))
_, _ = (force_rerun(cache, force=rerun_status)
(download_veg_structure_data)
(site=site,
data_path=data_raw_inv_path))
neon_plots_path = (force_rerun(cache, force=rerun_status)
(download_polygons)
(data_path=data_raw_inv_path))
# download neon_trait_table
url = cfg.others.neon_trait_table.neon_trait_link
trait_table_path = (force_rerun(cache, force={
'download_trait_table':
(cfg
.others
.neon_trait_table
.force_rerun)})
(download_trait_table)
(download_link=url,
data_path=data_raw_inv_path))
sites = []
for site, v in cfg.sites.run.items():
sites.append(site)
pft_reference_path = (force_rerun(cache, force=rerun_status)
(generate_pft_reference)
(sites=sites,
data_raw_inv_path=data_raw_inv_path,
data_int_path=data_int_path,
trait_table_path=trait_table_path))
# Process intermediate data per site
for site, v in cfg.sites.run.items():
if not global_run or site in global_run:
for year_inventory, p in v.items():
rerun_status = {}
for k, v in p.force_rerun.items():
rerun_status[k] = v or global_force_rerun.get(k, False)
year_aop = p.year_aop
log.info(f'Run process for site: {site}, '
f'year: {year_inventory}, year_aop: {year_aop}, '
f'with rerun status: {rerun_status}')
cache = build_cache_site(site=site,
year_inventory=year_inventory,
year_aop=year_aop,
data_raw_aop_path=data_raw_aop_path,
data_raw_inv_path=data_raw_inv_path,
data_int_path=data_int_path,
hs_type = hs_type )
# Reset variable names to proper site/year
# ais is there a cleaner way to do this? ask sy-toan
laz_path=os.path.join(data_raw_aop_path,site,year_aop,"laz")
if hs_type=="tile":
hs_path=os.path.join(data_raw_aop_path,site,year_aop,"hs_tile_h5")
else:
hs_path=os.path.join(data_raw_aop_path,site,year_aop,"hs_flightline_h5")
tif_path=os.path.join(data_raw_aop_path,site,year_aop,"tif")
neon_plots_path=os.path.join(data_raw_inv_path,"All_NEON_TOS_Plots_V9")
# process plots
inventory_file_path, \
sampling_effort_path = (force_rerun(cache, force=rerun_status)
(prep_veg_structure)
(site=site,
year_inv=year_inventory,
year_aop=year_aop,
data_path=data_raw_inv_path,
month_window=month_window))
partitioned_plots_path = (force_rerun(cache, force=rerun_status)
(prep_polygons)
(input_data_path=neon_plots_path,
sampling_effort_path=sampling_effort_path,
inventory_path=inventory_file_path,
site=site,
year=year_inventory,
output_data_path=data_int_path))
# clip lidar data
normalized_laz_path = (force_rerun(cache, force=rerun_status)
(normalize_laz)
(laz_path=laz_path,
site=site,
year=year_inventory,
output_path=data_int_path))
# ais walk through workflow below with a fine-toothed comb
# ais is this for both laz and tif files? or just laz?
# if for both laz and tif, then the output, clipped_laz_path, should be used more than just 1 time in following function...
clipped_laz_path = (force_rerun(cache, force=rerun_status)
(clip_lidar_by_plots)
(laz_path=normalized_laz_path,
tif_path=tif_path,
site_plots_path=partitioned_plots_path,
site=site,
year=year_inventory,
output_laz_path=data_int_path,
end_result=True))
# leaf area density
(force_rerun(cache, force=rerun_status)
(prep_lad)
(laz_path=clipped_laz_path,
inventory_path=inventory_file_path,
site=site,
year=year_inventory,
output_path=data_int_path,
use_case=use_case)) #ais or should I just use "train" here - whats best practice
# biomass
biomass_path = (force_rerun(cache, force=rerun_status)
(prep_biomass)
(data_path=inventory_file_path,
site_plots_path=partitioned_plots_path,
sampling_effort_path=sampling_effort_path,
site=site,
year=year_inventory,
data_int_path=data_int_path,
neon_trait_table_path=trait_table_path,
end_result=False))
# ais check warnings for utils.allometry - lots of species not detected in neon_trait_table
if hs_type=="flightline":
(force_rerun(cache, force=rerun_status)
(correct_flightlines)
(site=site,
year_inv=year_inventory,
year_aop=year_aop,
data_raw_aop_path=data_raw_aop_path,
data_int_path=data_int_path))
training_crown_shp_path = (force_rerun(cache,
force=rerun_status)
(prep_manual_training_data)
(site=site,
year=year_inventory,
data_raw_inv_path=data_raw_inv_path,
data_int_path=data_int_path,
biomass_path=biomass_path,
pft_reference_path=pft_reference_path,
px_thresh=px_thresh))
# prep NEON AOP data for classifier
stacked_aop_path = (force_rerun(cache, force=rerun_status)
(prep_aop_imagery)
(site=site,
year=year_inventory,
hs_type=hs_type,
hs_path=hs_path,
tif_path=tif_path,
data_int_path=data_int_path,
use_tiles_w_veg=use_tiles_w_veg))
training_spectra_csv_path = (force_rerun(cache,
force=rerun_status)
(extract_spectra_from_polygon)
(site=site,
year=year_inventory,
shp_path=training_crown_shp_path,
data_int_path=data_int_path,
data_final_path=data_final_path,
stacked_aop_path=stacked_aop_path,
use_case=use_case,
aggregate_from_1m_to_2m_res=aggregate_from_1m_to_2m_res,
ic_type=ic_type))
# ^ intermediate data finished processing for all site/years. Next, train RF
for k, v in p.force_rerun.items():
rerun_status[k] = v or global_force_rerun.get(k, False)
# log.info(f'Run process for all sites, '
# f'with rerun status: {rerun_status}')
#ais ask sy-toan how to code this to train RF only once, and not for every function in sites.yaml (stepped through k)
cache = build_cache_all(
data_int_path=data_int_path,
data_final_path=data_final_path,
use_case=use_case,
site=site,
year_inventory=year_inventory,
ic_type=ic_type)
if use_case=="predict" and ic_type=="field_inv_plots":
rf_model_path = None
else:
rf_model_path = (force_rerun(cache, force=rerun_status)
(train_pft_classifier)
(sites=sites,
data_int_path=data_int_path,
pcaInsteadOfWavelengths=pcaInsteadOfWavelengths,
ntree=ntree,
randomMinSamples=randomMinSamples,
independentValidationSet=independentValidationSet))
# ais why does this run 5 times, for each site
if use_case=="predict": #ais why is the below function running even when use_case is train
# ais set this up so that I can specify site/year or generate initial conditions for ALL sites
rf_model_path=os.path.join(data_int_path,'rf_dir/rf_model_tree_crowns_training.joblib')
site='SOAP' #ais how to specify this automatically? - when I specify 'usecase=predict'
year_inventory='2021' #ais how to specify this automatically?
year_aop = '2021-07' #ais how to specify this automatically?
# ais do I actually need to specify year_inv and year_aop?
# Generate initial conditions
cohort_path, patch_path = (force_rerun(cache, force=rerun_status)
(generate_initial_conditions)
(site=site,
year_inv=year_inventory,
year_aop = year_aop,
data_raw_aop_path = data_raw_aop_path,
data_int_path=data_int_path,
data_final_path=data_final_path,
rf_model_path = rf_model_path,
stacked_aop_path = os.path.join(data_int_path,site,year_inventory,'stacked_aop'),
biomass_path = os.path.join(data_int_path,site,year_inventory,"biomass/pp_veg_structure_IND_IBA_IAGB_live.csv"),
use_case=use_case,
ic_type=ic_type,
n_plots = n_plots,
min_distance = min_distance,
plot_length = plot_length,
aggregate_from_1m_to_2m_res=aggregate_from_1m_to_2m_res,
pcaInsteadOfWavelengths=pcaInsteadOfWavelengths))
log.info('DONE')
if __name__ == '__main__':
main()