Skip to content
Snippets Groups Projects
Commit 0d64e0c8 authored by Roelofsen, Hans's avatar Roelofsen, Hans
Browse files

updates and new script for inspecting MNP parameter files

parent 8bb12341
No related branches found
No related tags found
1 merge request!19Working 20230228
......@@ -6,6 +6,7 @@ Hans Roelofsen, jan 2022
import sys
import pandas as pd
import os
import numpy as np
from fix_bt import fix_bt # imports a function
from snl_beheertypen import get_snl_beheertypen_list # imports a function
......@@ -14,36 +15,33 @@ from Classes import Species as species # imports a module with two classes
species_table = species.TabularSpecies()
class DraagKracht:
def __init__(self):
self.dkdir = r'W:\PROJECTS\QMAR\MNP-SNL-ParameterSet\Parameters_v05_2021_04_08'
self.defaultdk = r'03_MNP_versie5_par_density_factors_BT2021_v3.csv'
self.dk_src = None # To do, set to default if self.use_default, else to someting else
self.snl_bt = get_snl_beheertypen_list(of='full')
def __init__(self, src: str = r'w:\PROJECTS\QMAR\MNP-SNL-ParameterSet\Parameters_v05_2021_04_08\03_MNP_versie5_par_density_factors_BT2021_v3.csv',
bt_column: str = 'Land_type_code',
id_column: str = 'Species_code',
quality_column: str = 'Land_type_quality'):
self.src = src
self.snl_bt = get_snl_beheertypen_list(of="full")
self.use_default = True
self.bt_column = 'Land_type_code'
self.dk = None
self.bt_column = bt_column
self.id_column = id_column
self.quality_column = quality_column
self.df = pd.read_csv(src)
self.read_dk()
self.code_to_groupname = {
"S02": "S02 vogel",
"S06": "S06 vlinder",
"S09": "S09 plant",
}
def read_dk(self, dk_file='default', bt_column=None):
"""
Read a draagkrachten file
:param dk_file: name of a draagrkachten file
:param bt_column: name of column in non-default file with BT codes
:return: pd dataframe
"""
if dk_file != 'default':
self.use_default = False
self.bt_column = bt_column
target = dk_file
else:
target = self.defaultdk
setattr(self, "dk", pd.read_csv(os.path.join(self.dkdir, target)))
def print_src(self):
print(
f"Analysing {os.path.join(os.path.basename(os.path.dirname(self.src)), os.path.basename(self.src))}"
)
def query4bt(self, bt_lst: list, of='brief', sp_sel='281'):
def query4bt(self, bt_lst: list, of="brief", sp_sel="281"):
"""
query the draagkrachten for a specific beheertype
:param bt_lst: beheertype code
......@@ -56,36 +54,67 @@ class DraagKracht:
bt_lst = [bt_lst]
for bt in bt_lst:
query = '({0} in {1}) and selection == True'.format(self.bt_column, [fix_bt(bt, as_mnp=True)])
sel = self.dk.assign(selection=self.dk.Species_code.map(getattr(species_table, 'code2sel{}'.format(sp_sel)))) \
.query(query)
counts = sel.Species_code.map(species_table.code2taxon).value_counts()
message_brief = '{0}-{1}: {2} vogel, {3} vlinder {4} plant. {5} total (out of {6}).' \
.format(bt, code2name[fix_bt(bt, as_mnp=True)],
counts.get('V', 0), counts.get('E', 0), counts.get('P', 0),
sel.shape[0], sp_sel if sp_sel != 'all' else '1081')
df_out = sel.assign(local=sel.Species_code.map(species_table.code2local),
taxon=sel.Species_code.map(species_table.code2taxon),
latin=sel.Species_code.map(species_table.code2scientific),
lst468=sel.Species_code.map(getattr(species_table, 'code2sel468')).multiply(1),
lst281=sel.Species_code.map(getattr(species_table, 'code2sel281')).multiply(1),
lst146=sel.Species_code.map(getattr(species_table, 'code2sel146')).multiply(1))\
.sort_values(by=['taxon', 'local']) \
.loc[:, ['Species_code', 'taxon', 'Land_type_quality', 'local', 'latin', 'lst468',
'lst281', 'lst146']]
message_full = df_out.to_csv(sep='\t', index=False, header=True)
dict_out = dict(zip(sel['Species_code'], sel['Land_type_quality']))
if of == 'brief':
query = "({0} in {1}) and selection == True".format(
self.bt_column, [fix_bt(bt, as_mnp=True)]
)
sel = self.df.assign(
selection=self.df.loc[:, self.id_column].map(
getattr(species_table, "code2sel{}".format(sp_sel))
)
).query(query)
counts = sel.loc[:, self.id_column].map(species_table.code2taxon).value_counts()
message_brief = "{0}-{1}: {2} vogel, {3} vlinder {4} plant. {5} total (out of {6}).".format(
bt,
code2name[fix_bt(bt, as_mnp=True)],
counts.get("V", 0),
counts.get("E", 0),
counts.get("P", 0),
sel.shape[0],
sp_sel if sp_sel != "all" else "1081",
)
df_out = (
sel.assign(
local=sel.loc[:, self.id_column].map(species_table.code2local),
taxon=sel.loc[:, self.id_column].map(species_table.code2taxon),
latin=sel.loc[:, self.id_column].map(species_table.code2scientific),
lst468=sel.loc[:, self.id_column].map(
getattr(species_table, "code2sel468")
).multiply(1),
lst281=sel.loc[:, self.id_column].map(
getattr(species_table, "code2sel281")
).multiply(1),
lst146=sel.loc[:, self.id_column].map(
getattr(species_table, "code2sel146")
).multiply(1),
)
.sort_values(by=["taxon", "local"])
.loc[
:,
[
self.id_column,
"taxon",
self.quality_column,
"local",
"latin",
"lst468",
"lst281",
"lst146",
],
]
)
message_full = df_out.to_csv(sep="\t", index=False, header=True)
dict_out = dict(zip(sel[self.id_column], sel.loc[:, self.quality_column]))
if of == "brief":
print(message_brief)
elif of == 'full':
elif of == "full":
print(message_brief)
print(message_full)
df_out.to_clipboard(index=False, sep='\t')
elif of == 'df':
df_out.to_clipboard(index=False, sep="\t")
elif of == "df":
return df_out
elif of == 'dict':
elif of == "dict":
return dict_out
def query4species(self, species_lst: list, of, verbose=False):
......@@ -102,42 +131,128 @@ class DraagKracht:
for x in species_lst:
sp = species.IndividualSpecies(x)
query = 'Species_code in ["{0}"]'.format(sp.code)
sel = self.dk.query(query)
sel = self.df.query(query)
if verbose:
print("{0} ({1}-{2}. Listed in: {3}): {4} beheertypen".format(sp.local, sp.scientific, sp.code,
sp.groupinfo, sel.shape[0]))
print(
"{0} ({1}-{2}. Listed in: {3}): {4} beheertypen".format(
sp.local, sp.scientific, sp.code, sp.groupinfo, sel.shape[0]
)
)
df = sel.assign(desc=getattr(sel, self.bt_column).map(code2name)) \
.loc[:, ['Land_type_quality', self.bt_column, 'desc']] \
df = (
sel.assign(desc=getattr(sel, self.bt_column).map(code2name))
.loc[:, ["Land_type_quality", self.bt_column, "desc"]]
.sort_values(by=self.bt_column)
)
if of == 'full':
df.to_clipboard(sep='\t', index=False)
print(df.to_csv(sep='\t', index=False, header=False))
elif of == 'class':
if of == "full":
df.to_clipboard(sep="\t", index=False)
print(df.to_csv(sep="\t", index=False, header=False))
elif of == "class":
for row in df.itertuples():
setattr(sp, getattr(row, self.bt_column), row.Land_type_quality)
return sp
def u_species(self):
return list(getattr(self.df, self.id_column).unique())
def n_species(self, of="print"):
u_species = pd.Series(self.u_species())
counts = (
u_species.str[:3]
.map(self.code_to_groupname)
.value_counts()
.loc[[x for x in self.code_to_groupname.values()]]
)
if of == "print":
self.print_src()
print(counts.loc[list(self.code_to_groupname.values())])
else:
return counts
def n_landscapes(
self, of="print"):
self.df["hoofd"] = np.where(
self.df.loc[:, self.bt_column].str[-2:] != "00",
"N-neer",
self.df.loc[:, self.bt_column].str[:1],
)
p1 = pd.pivot_table(
self.df, index="hoofd", values=self.bt_column, aggfunc=lambda x: len(x.unique())
)
p1 = p1.join(
other=pd.pivot_table(
self.df,
index="hoofd",
values=self.id_column,
aggfunc=lambda x: len(x.unique()),
)
).rename(columns={self.bt_column: 'n landscapes', self.id_column: 'n species'})
if of == "print":
print(p1.sort_index())
else:
return p1.sort_index()
if __name__ == '__main__':
def summary(self):
self.print_src()
print(
f"\n==Summary==\n{self.df.shape[0]:4d} combinations\n{len(self.u_species()):4d} species\n{len(self.df.loc[:, self.bt_column].unique()):4d} landscapes\n"
)
print('==Species overview==')
print(self.n_species(of="other").to_csv(sep='\t', header=False))
print('==Landscape overview==')
print(self.n_landscapes(of="other"))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--qbt', nargs='+', type=str)
parser.add_argument('--qsp', nargs='+', type=str)
parser.add_argument('--of', choices=['full', 'sparse', 'df', 'dict'], default='full')
parser.add_argument('--sp_list', choices=['281', '468', '146', 'all'], default='281', type=str)
# parser.add_argument('--dk_file', help='draagrkachten file', default=)
parser.add_argument(
"src",
type=str,
help="Source file",
default="w:\PROJECTS\QMAR\mnp_rebuild_2022\tabs\land_type_suitability_index.csv",
)
parser.add_argument("--bt_col", type=str, default=r"Land_type_code")
parser.add_argument("--id_col", type=str, default=r"Species_code")
parser.add_argument("--quality_col", type=str, default=r"Land_type_quality")
parser.add_argument("--qbt", nargs="+", type=str)
parser.add_argument("--qsp", nargs="+", type=str)
parser.add_argument(
"--of", choices=["full", "sparse", "df", "dict"], default="full"
)
parser.add_argument(
"--sp_list",
choices=["468", "281", "146", "NSensitive", "all"],
default="281",
type=str,
)
parser.add_argument("--n_species", help="Return species count", action="store_true")
parser.add_argument(
"--n_landscapes", help="Return landscape count", action="store_true"
)
parser.add_argument("--summary", help="Print content summary", action="store_true")
args = parser.parse_args()
hans = DraagKracht()
hans.read_dk()
draagkracht_tabel = DraagKracht(
src=args.src, bt_column=args.bt_col, id_column=args.id_col,
quality_column=args.quality_col
)
try:
if args.qbt:
hans.query4bt(bt_lst=args.qbt, of=args.of, sp_sel=args.sp_list)
if args.qsp:
hans.query4species(species_lst=args.qsp, of=args.of)
draagkracht_tabel.query4bt(bt_lst=args.qbt, of=args.of, sp_sel=args.sp_list)
elif args.qsp:
draagkracht_tabel.query4species(species_lst=args.qsp, of=args.of)
elif args.n_species:
draagkracht_tabel.n_species()
elif args.summary:
draagkracht_tabel.summary()
# print(draagkracht_tabel.n_species(of='other'))
except AssertionError as e:
print(e)
sys.exit(1)
......@@ -165,3 +280,7 @@ if __name__ == '__main__':
# full.loc[:, 'sel1018'] = 1
# full.loc[:, 'selall'] = 1
# full.to_clipboard(sep=',', index=False)
import geopandas as gp
tab = gp.read_file(r'w:\PROJECTS\MNP2020\MNP2023_WSNEval\beheertypenkaarten\2030Ambitie\delivery_pbl_20230315\tiff\ambitietypen_2030_zonder_SPUKS_date230315.tif.vat.dbf')
pd.DataFrame.from_dict(tab.set_index('Value').Ambitietyp.to_dict(), orient='index', columns=['bt']).to_clipboard(excel=True)
\ No newline at end of file
import os.path
import pandas as pd
import pathlib
class ResponseFile:
def __init__(self, src, sep=',', comment='#', species_column='species_code'):
self.src = src
self.df = pd.read_csv(src, sep=sep, comment=comment)
self.species_column = species_column
assert self.df.shape[0] == len(set(getattr(self.df, species_column)))
self.code_to_groupname = {
'S02': 'S02 vogel',
'S06': 'S06 vlinder',
'S09': 'S09 plant'
}
def print_src(self):
print(f'Analysing {os.path.join(os.path.basename(os.path.dirname(self.src)), os.path.basename(self.src))}')
def n_species(self):
self.print_src()
print(f'{self.df.shape[0]} species.')
def n_species_group(self, of='print'):
counts = getattr(self.df, self.species_column).str[:3].map(self.code_to_groupname).value_counts()
counts['total'] = counts.sum()
if of == 'print':
self.print_src()
print(counts.sort_index())
else:
return counts
def u_species(self):
return list(getattr(self.df, self.species_column).unique())
if __name__ == '__main__':
import argparse
argumentparser = argparse.ArgumentParser()
argumentparser.add_argument('src', help='source file', type=str)
argumentparser.add_argument('--n', action='store_true', help='total species count.')
argumentparser.add_argument('--n_group', action='store_true', help='count per group.')
argumentparser.add_argument('--sep', default=',', help='column seperator')
argumentparser.add_argument('--comment', default='#', help='comment char.')
argumentparser.add_argument('--id_col', default='species_code', help='species ID column.')
args = argumentparser.parse_args()
response_file = ResponseFile(src=args.src,
sep=args.sep,
comment=args.comment,
species_column=args.id_col)
if args.n:
response_file.n_species()
elif args.n_group:
response_file.n_species_group()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment