Commit 37e7b8ba authored by Roelofsen, Hans's avatar Roelofsen, Hans
Browse files

structuurtypen as list attribute

parent 85811319
......@@ -38,7 +38,6 @@ doren.add_posch(posch_src_dir=param_posch)
doren.get_requested_species(xls=sp_req_src, sheet=sp_req_sheet, col=column, simplify_names=False)
doren.test()
doren.write_stuff('species_list')
doren.write_stuff('typische_soorten')
doren.write_stuff('report')
......
......@@ -18,7 +18,7 @@ n_batches = int(args.n_batches)
# Recover the Pickled doren object
pkl_src = os.path.join('./pkl_src/', args.pkl)
#pkl_src = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\b_compiled_data\a_pkl\doren_20201118.pkl'
# pkl_src = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\b_compiled_data\a_pkl\doren_20201121.pkl'
with open(pkl_src, 'rb') as handle:
doren = pickle.load(handle)
......@@ -28,19 +28,17 @@ doren.base_out_dir = r'./c_out'
# sp_req_src = r'c:\Users\roelo008\Wageningen University & Research\DOREN - General\2020-09-17 uniek soorten per habitat (2).xlsx'
# doren.get_requested_species(xls=sp_req_src, sheet='Verdringingssoorten', col='wetenschappelijke naam', simplify_names=False)
structuurtypes = [x for x in list(set(doren.eva.structuurtype)) if x != '?']
# Determine subset of requested species to proces
sel = np.array_split(np.arange(0, len(doren.req_sp)), n_batches)[batch_nr].tolist()
print('Commencing processing of {0} species...'.format(len(sel)))
# Process subset of requested species
for i, j in enumerate(sel, start=0):
for i, j in enumerate(sel, start=1):
sp = doren.req_sp[j]
try:
print('Doing species {0} of {1}: {2}'.format(i, len(sel), sp))
doren.select_plts_w_species(species_name=sp)
for structuurtype in structuurtypes:
for structuurtype in doren.structuurtypes:
doren.nearest_positive_queryd(col='structuurtype', val=structuurtype)
doren.get_bedekking_selected_sp()
doren.write_stuff('species_single_file')
......
......@@ -46,6 +46,7 @@ class Doren:
self.buffer_gdf = None # holder for geodataframe w. buffers
self.sel_species = None # holder for a species
self.buffer_size = None # holder for buffer size
self.structuurtypes = None # holder for list of structuurtypes. Must be list, not set, for ordering!
self.sample = False # Dataset is a random sample of all plots, for testing purposes.
# relevant stats
self.status = {'n_plots': 0, 'columns': [], 'n_species': 0, 'covars': []}
......@@ -516,6 +517,9 @@ class Doren:
self.eva.structuurtype.fillna('?', inplace=True)
self.eva.openbos.fillna('?', inplace=True)
# Append unique structuurtypes as a list
self.structuurtypes = [x for x in set(self.eva.structuurtype) if x != '?']
def select_plts_w_species(self, species_name):
"""
Idenitfy all plots that contain a species
......@@ -638,7 +642,6 @@ class Doren:
if len(query) > 0:
ids, dist = do.ckdnearest(gdA=self.eva.loc[self.negative_plots, :], gdB=self.eva.loc[query, :],
id_col='plot_obs_id')
else:
ids = pd.Series([0]*len(self.negative_plots))
dist = pd.Series([np.nan]*len(self.negative_plots))
......@@ -792,8 +795,10 @@ class Doren:
os.mkdir(pg_dir)
# Compile dataframe for writing to file
out_cols = [col for col in list(self.eva) if col.startswith(('dist2', 'nearest'))]
out_df = self.eva.loc[:, out_cols + ['plot_obs_id']]
nearest_cols = [col for col in list(self.eva) if col.startswith('dist2')]
id_cols = [col for col in list(self.eva) if col.startswith('nearest')]
out_df = self.eva.loc[:, ['plot_obs_id'] + nearest_cols + id_cols]
out_df.loc[self.positive_plots, 'respons'] = np.uint8(1)
out_df.loc[self.negative_plots, 'respons'] = np.uint8(0)
as_type = {'respons': np.byte}
......@@ -804,38 +809,43 @@ class Doren:
out_df.coverage.fillna(np.uint8(0), inplace=True)
as_type['coverage'] = np.uint8
# Write to file and update report
nearest_cols = [col for col in list(out_df) if col.startswith('dist2')]
id_cols = [col for col in list(out_df) if col.startswith('nearest')]
# Write file with plots obs ids, coverage, respons and dist to nearest each structuurtype and update report
round_dict = dict.fromkeys(nearest_cols, 2)
round_dict.update(**dict.fromkeys(id_cols, 0))
out_df.round(round_dict).astype(as_type) \
.to_csv(os.path.join(pg_dir, '{}.csv'.format(pg_out_name)), sep=',', index=False, header=True)
sel1 = out_df.loc[:, ['plot_obs_id', 'respons', 'coverage'] + nearest_cols]
sel1.round(round_dict).astype(as_type) \
.to_csv(os.path.join(out_dir, '{}.csv'.format(pg_out_name)), sep=',', index=False, header=True)
self.report += 'Written single file output for species {0} to file: ' \
'{1}'.format(self.sel_species, os.path.join(pg_dir, pg_out_name))
# Write to file w plot_obs_id and dist to nearest ID
round_dict = dict.fromkeys(id_cols, 0)
sel2 = out_df.loc[:, ['plot_obs_id', 'respons', ] + id_cols]
sel2.round(round_dict).astype({'respons': np.byte}) \
.to_csv(os.path.join(out_dir, '{}_ids.csv'.format(pg_out_name)), sep=',', index=False, header=True)
elif what == 'plot_covars_file':
"""
Single file with covariable information on all plots.
"""
# check if all required data is present
if self.sel_species is None or 'tot_N_kmol_ha' not in self.eva.columns:
if 'tot_N_kmol_ha' not in self.eva.columns:
print('Cannot report plot covars file')
return None
# Dedicated directory for the PG output and output filename
pg_out_name = 'Plot_Covars'.format(self.sel_species.replace(' ', '_'))
pg_out_name = 'Plot_Covars'
pg_dir = os.path.join(out_dir, 'pg_input')
if not os.path.isdir(pg_dir):
os.mkdir(pg_dir)
out_cols = ['plot_obs_id', 'tot_N_kmol_ha', 'soil_type_label', 'country_label', 'five_yearly_precip',
'five_yearly_temp', 'eunis_code', 'structuurtype', 'openbos']
for col in out_cols:
assert hasattr(self.eva, col), 'Cannot write due to missing column {}'.format(col)
out_df = self.eva.loc[:, out_cols]
out_df.round({'plot_obs_id': 0, 'tot_N_kmol_ha': 2, 'five_yearly_precip': 2, 'five_yearly_temp': 2, })\
.to_csv(os.path.join(pg_dir, '{}.csv'.format(pg_out_name)), sep=',', index=False, header=True)
.to_csv(os.path.join(out_dir, '{}.csv'.format(pg_out_name)), sep=',', index=False, header=True)
self.report += 'Written plot covar output file: {0}'.format(os.path.join(pg_dir, pg_out_name))
elif what == 'headers_shp':
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment