Commit 674d4daa authored by Roelofsen, Hans's avatar Roelofsen, Hans
Browse files

improved results list

parent 2ba941b6
......@@ -65,8 +65,10 @@ def plant_dk(ffargs):
# Read list of equested beheertypen to analyse, add mnp-style code
try:
bts = pd.read_csv(args.bt_src, sep=',', usecols=['Land_type_code'])
except (ValueError, FileNotFoundError) as e:
bts = pd.read_csv(args.bt_src, sep=',', comment='#')
bts.rename(dict(zip(list(bts), [x.capitalize() for x in list(bts)])), axis=1, inplace=True)
assert 'Land_type_code' in list(bts)
except (ValueError, FileNotFoundError, AssertionError) as e:
raise AssertionError('cannot read beheertypen source: {}. Check for column Land_type_code!'.format(e))
bts['bt_code'] = bts.Land_type_code.apply(fix_bt, as_mnp=True) # Add MNP-style Beheertype codes
bts.drop_duplicates(subset='bt_code', inplace=True)
......@@ -83,7 +85,8 @@ def plant_dk(ffargs):
raise AssertionError('Cannot find species list, or species list is missing "Species_code" (S09xxxxxx) '
'"Local_name" "Scientific_name" columns: {}'.format(e))
species.drop_duplicates(subset='Species_code', inplace=True)
assert all(species.Species_code.apply(code2tax) == 'P'), "non plants found in requested species list."
plant_species = species.loc[species.Species_code.apply(code2tax) == "P"].index
# assert all(species.Species_code.apply(code2tax) == 'P'), "non plants found in requested species list."
# species dicts
code2scientific = dict(zip(species.Species_code, species.Scientific_name))
......@@ -91,17 +94,19 @@ def plant_dk(ffargs):
"""Loop trough alll Species/Beheertype combis. Calculate mean frequentie/trouw where possible. """
dk_lst = []
for (sp_code, bt) in itertools.product(species.Species_code, bts.bt_code):
for (sp_code, bt) in itertools.product(species.loc[plant_species, 'Species_code'], bts.bt_code):
print('Doing {0}-{1}'.format(sp_code, bt))
# Dictionary to hold results
d = dict.fromkeys(['Scientific_name', 'Land_type_code', 'mean_trouw', 'mean_freq', 'hits', 'hits_gte_th',
'dk_ww'], np.nan)
d = {'Land_type_code': bt, 'Species_code': sp_code,
'hits': responses.loc[(responses.Soortnaam == code2scientific[sp_code]) &
(responses.syntaxon_gen.isin(bt2asso[bt]))].shape[0],
'hits_gte_th': 0, 'mean_trouw': np.nan, 'mean_freq': np.nan, 'dk_ww': np.nan}
# Get initial selection from plant response data
d['hits'] = responses.loc[(responses.Soortnaam == code2scientific[sp_code]) &
(responses.syntaxon_gen.isin(bt2asso[bt]))].shape[0]
# Continue with next if there are 0 hits for species/bt combination in the database
if d['hits'] == 0:
dk_lst.append(pd.DataFrame(d, index=pd.RangeIndex(1)))
continue
# Narrow selection further down to rows meeting thresholds
sel1 = responses.loc[(responses.Soortnaam == code2scientific[sp_code]) &
......@@ -114,15 +119,6 @@ def plant_dk(ffargs):
d['mean_trouw'] = responses.loc[sel1, 'Trouw'].mean()
d['dk_ww'] = dk_ww(frequentie=d['mean_freq'], trouw=d['mean_trouw'], th=args.th_dk, tuning=args.A)
d['Species_code'] = sp_code
d['Land_type_code'] = bt
try:
d['Scientific_name'] = code2scientific[sp_code]
d['Local_name'] = code2local[sp_code]
d['Land_type_omschrijving'] = btcode2omschrijving[bt]
except KeyError as e:
raise AssertionError("{} is not recognized".format( e))
# Wrap up results and append to list
dk_lst.append(pd.DataFrame(d, index=pd.RangeIndex(1)))
......@@ -131,7 +127,15 @@ def plant_dk(ffargs):
dk.hits = np.where(dk.hits == 0, np.nan, dk.hits)
dk.hits_gte_th = np.where(dk.hits_gte_th == 0, np.nan, dk.hits_gte_th)
# Merge with MNP default draagkracht values
# Add additional information
try:
dk['Scientific_name'] = dk.Species_code.map(code2scientific)
dk['Local_name'] = dk.Species_code.map(code2local)
dk['Land_type_omschrijving'] = dk.Land_type_code.map(btcode2omschrijving)
except KeyError as e:
raise AssertionError("{} is not recognized".format( e))
# Merge with MNP default draagkracht values based on Species_codeLand_type_code combination
dk = pd.merge(left=dk.assign(combi=dk.Species_code.str.cat(dk.Land_type_code)),
right=mnp_dk.assign(combi=mnp_dk.Species_code.str.cat(mnp_dk.Land_type_code))\
.drop(labels=['Species_code', 'Land_type_code'], axis=1)\
......@@ -139,11 +143,12 @@ def plant_dk(ffargs):
left_on='combi', right_on='combi', how='left').drop('combi', axis=1)
# Report on results
ts = datetime.datetime.now().strftime("%Y%m%d%H%M")
ts_full = datetime.datetime.now().strftime("%Y%m%dt%H%M%S")
species_requested = species.shape[0] # Aantal soorten gevraagd
plants_requested = len(plant_species) # Aantal plant soorten gevraagd
bts_requested = bts.shape[0] # Aantal BTs gevraagd
combis_potential = np.multiply(species_requested, bts_requested) # potentiaal aantal draakgrachts
combis_potential = np.multiply(plants_requested, bts_requested) # potentiaal aantal draakgrachts
combis_wo_data = dk.hits.isna().sum() # combis without data
combis_w_data = dk.hits.notna().sum() # combi where data is provided
combis_meeting_th = dk.hits_gte_th.notna().sum() # combies where Trouw & Frequentie data meets criteria
combis_lost = (dk.dk_ww.isna() & dk.dk_mnp.notna()).sum() # Combi die eerst wel, nu geen Draagkracht heeft
......@@ -151,11 +156,19 @@ def plant_dk(ffargs):
combis_kept = (dk.dk_ww.notna() & dk.dk_mnp.notna()).sum() # Combie die die eerst wel en nog steeds DK heeft
combis_never = (dk.dk_ww.isna() & dk.dk_mnp.isna()).sum() # Combie die toen niet, nu niet, nooit niet DK heeft
# Pivot table
piv = pd.pivot_table(dk.assign(Soort=dk.Scientific_name.str.cat(dk.Local_name, sep=" (")
.str.cat(pd.Series([")"]), na_rep=")"),
BT=dk.Land_type_code.str.cat(dk.Land_type_omschrijving, sep=' (')
.str.cat(pd.Series([")"]), na_rep=")")),
index='Soort', columns='BT', aggfunc='mean', values='dk_ww', dropna=False).round(2)
species_never = piv.loc[piv.count(axis=1) == 0].index
beheertype_never = piv.count(axis=0).loc[piv.count(axis=0) == 0].index
# X soorten met Wieger Wamelink draagkrachten voor N beheertypen
aantal_bts, aantal_soorten = np.unique(pd.pivot_table(data=dk, index='Species_code', aggfunc='count', values='dk_ww'),
return_counts=True)
dkww_df = pd.DataFrame({'n_beheertypen': aantal_bts, 'n_soorten': aantal_soorten})
report = \
"==RAPPORT AUTOMATISCHE DRAAGKRACHT BEPALING VOOR PLANTEN TBV MNP==\n"\
"door: {0}\ndatum: {1}\n\n" \
......@@ -173,19 +186,25 @@ def plant_dk(ffargs):
"referentie beheertypen: {10}\n"\
"referentie mnp draagkr: {11}\n\n"\
"n species gevraagd: {12}\n" \
"n beheertypen gevraagd: {13}\n"\
"n potentiale dk combis: {14}\n"\
"n combis w data: {15}\n"\
"n combis w data >= th: {16}\n"\
"n combis new tov ref: {17}\n"\
" lost : {18}\n"\
" kept : {19}\n"\
" never : {20}\n\n"\
"Aantal soorten met draagkracht voor X beheertypen:\n"\
"{21}\n"\
"n planten gevraagd: {13}\n"\
"n beheertypen gevraagd: {14}\n"\
"n potentiale dk combis: {15}\n"\
"n combis w/o data: {16}\n"\
"n combis w data: {17}\n"\
"n combis w data >= th: {18}\n"\
"n combis new tov ref: {19}\n"\
" lost : {20}\n"\
" kept : {21}\n"\
" never : {22}\n\n" \
"Aantal soorten met draagkracht voor X beheertypen:\n" \
"{27}\n" \
"List of {23} species w/o dk\n"\
"{24}\n\n"\
"List of {25} Beheertypen w/o dk\n"\
"{26}\n\n"\
"Draaitabel:\n"\
"{22}".format(os.getenv('USERNAME'),
ts,
"{28}".format(os.getenv('USERNAME'),
ts_full,
args.th_dk,
args.A,
args.th_f,
......@@ -197,19 +216,23 @@ def plant_dk(ffargs):
bt_master_src,
mnp_dk_src,
species_requested,
plants_requested,
bts_requested,
combis_potential,
combis_wo_data,
combis_w_data,
combis_meeting_th,
combis_new,
combis_lost,
combis_kept,
combis_never,
len(species_never),
'\n'.join(['{0}. {1}'.format(a, b) for (a, b) in enumerate(species_never, start=1)]),
len(beheertype_never),
'\n'.join(['{0}. {1}'.format(a, b) for (a, b) in enumerate(beheertype_never, start=1)]),
dkww_df.to_csv(index=False, sep='\t', line_terminator='\n'),
pd.pivot_table(dk.assign(Soort=dk.Scientific_name.str.cat(dk.Local_name, sep="_"),
BT=dk.Land_type_code.str.cat(dk.Land_type_omschrijving, sep='_')),
index='Soort', columns='BT', values='dk_ww', aggfunc='mean')
.to_csv(sep=',', line_terminator='\n'))
piv.to_csv(sep=',', line_terminator='\n'))
# Prepare output
basename = '03PlantDensityFactors{0}'.format(ts_full)
......
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