183 lines
6.3 KiB
Python
183 lines
6.3 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: UTF-8 -*-
|
|
#Nom : : pers.py
|
|
#Description :
|
|
#Copyright : 2021, CEN38
|
|
#Auteur : Colas Geier
|
|
#Version : 1.0
|
|
|
|
from os import register_at_fork
|
|
from pandas import read_sql, merge, DataFrame
|
|
from pandas.core.indexing import is_nested_tuple
|
|
from ..tools import to_upper, to_tuple, to_upperfirst, _get_table,_aggr_cols
|
|
from ..params import con
|
|
|
|
schema = 'personnes'
|
|
|
|
def get_auteur(nom=None, prenom=None):
|
|
table = 'personne'
|
|
sql = 'SELECT * FROM %s.%s'%(schema,table)
|
|
if nom or prenom : sql = sql + ' WHERE '
|
|
if nom :
|
|
sql = sql + 'nom IN %(nom)s'
|
|
nom = to_upper(nom)
|
|
if nom and prenom : sql = sql + ' AND '
|
|
if prenom :
|
|
sql = sql + 'prenom IN %(prenom)s'
|
|
prenom = to_upperfirst(prenom)
|
|
df = read_sql(
|
|
sql = sql,
|
|
con = con,
|
|
params = {'nom': to_tuple(nom), 'prenom': to_tuple(prenom) })
|
|
df['nom_prenom'] = (df.nom + ' ' + df.prenom).str.strip()
|
|
df.loc[df.nom_prenom.isna(), 'nom_prenom'] = df.loc[df.nom_prenom.isna(), 'nom']
|
|
|
|
return df
|
|
|
|
def get_auteur2(nom=None, prenom=None, organisme=None):
|
|
# table = 'personne'
|
|
# sql = 'SELECT * FROM %s.%s'%(schema,table)
|
|
# if nom or prenom : sql = sql + ' WHERE '
|
|
# if nom :
|
|
# sql = sql + 'nom IN %(nom)s'
|
|
# nom = to_upper(nom)
|
|
# if nom and prenom : sql = sql + ' AND '
|
|
# if prenom :
|
|
# sql = sql + 'prenom IN %(prenom)s'
|
|
# prenom = to_upperfirst(prenom)
|
|
# df = read_sql(
|
|
# sql = sql,
|
|
# con = con,
|
|
# params = {'nom': to_tuple(nom), 'prenom': to_tuple(prenom) })
|
|
# df['nom_prenom'] = (df.nom + ' ' + df.prenom).str.strip()
|
|
# df.loc[df.nom_prenom.isna(), 'nom_prenom'] = df.loc[df.nom_prenom.isna(), 'nom']
|
|
df = get_auteur(nom=nom, prenom=prenom)
|
|
org = get_organisme(nom=organisme).rename(columns={'nom':'organisme'})
|
|
df = df.merge(org,how='left',left_on='id_organisme',right_index=True)
|
|
|
|
df['np_orga'] = ['%s (%s)'%(x.nom_prenom,x.organisme) for i,x in df.iterrows()]
|
|
df['np_abbrev'] = ['%s (%s)'%(x.nom_prenom,x.abbrev) for i,x in df.iterrows()]
|
|
df.loc[(df.organisme!='Inconnu')&(df.abbrev.isna()),['nom_prenom']] = df.loc[(df.organisme!='Inconnu')&(df.abbrev.isna()),'np_orga']
|
|
df.loc[~df.abbrev.isna(),['nom_prenom']] = df.loc[~df.abbrev.isna(),'np_abbrev']
|
|
df.drop(columns=[
|
|
'np_orga','np_abbrev',*org.columns.drop('organisme'),'id_organisme'], inplace=True)
|
|
df.set_index('id', inplace=True)
|
|
return df
|
|
|
|
def get_organisme(ids=None, nom=None):
|
|
table = 'organisme'
|
|
df = _get_table(con, schema, table, ids=ids, nom=nom)
|
|
return df.set_index('id')
|
|
|
|
|
|
def _merge_orga(df, split_cols):
|
|
org = get_organisme()
|
|
aut = get_auteur()
|
|
df = df.copy()
|
|
for c in split_cols:
|
|
if not isinstance(df[c], int): df[c] = df[c].astype(float)
|
|
df[c].replace(aut.id.tolist(), aut.id_organisme.tolist(), inplace=True)
|
|
df[c].replace(org.id.tolist(), org.nom.tolist(), inplace=True)
|
|
df['organisme'] = None
|
|
for c in split_cols:
|
|
df.loc[df.organisme.isna(), 'organisme'] = df.loc[df['organisme'].isna(), c]
|
|
for c in split_cols:
|
|
comp = df.loc[~df[c].isna(),'organisme'].compare(df.loc[~df[c].isna(), c])
|
|
if not comp.empty:
|
|
comp['test'] = comp.apply(lambda x: x['other'] in x['self'], axis=1)
|
|
comp = comp[~comp.test]
|
|
if not comp.empty:
|
|
df.loc[comp.index,'organisme'] = comp.self + ' & ' + comp.other
|
|
df.drop(columns=split_cols, inplace=True)
|
|
return df
|
|
|
|
|
|
def _merge_author2(df, col_aut):
|
|
# récupération des auteurs
|
|
aut = get_auteur2().fillna('')
|
|
# aut['nom_prenom'] = (aut['nom'] + ' ' + aut['prenom']).str.strip()
|
|
aut.reset_index(drop=False, inplace=True)
|
|
aut['id'] = aut['id'].astype(str)
|
|
id_inconnu = aut[aut.nom == 'INCONNU'].id[0]
|
|
|
|
# merge des auteurs
|
|
df[col_aut].fillna(id_inconnu, inplace=True)
|
|
df[col_aut] = df[col_aut].replace(['\.0'],'',regex=True)
|
|
r_id = df[['id', col_aut]].copy()
|
|
r_idSplit = r_id[col_aut].str.split(' & ', expand=True)
|
|
r_id = r_id.join(r_idSplit)
|
|
cSplit = r_idSplit.shape[1]
|
|
cSplit = list(range(cSplit))
|
|
|
|
r_id[cSplit] = r_id[cSplit].replace(aut['id'].tolist(),aut['nom_prenom'].tolist())
|
|
r_id = _aggr_cols(r_id,cSplit,' & ') \
|
|
.rename(columns={'aggreg': 'auteur'}) \
|
|
.drop(columns=cSplit)
|
|
|
|
df = merge(df,r_id,how='left', on=['id', col_aut], suffixes=[None,'_y']) \
|
|
.drop(columns=[col_aut])
|
|
|
|
return df
|
|
|
|
|
|
|
|
def _merge_author(df, col_aut, orga=False, on_index=False):
|
|
# récupération des auteurs
|
|
aut = get_auteur().fillna('')
|
|
# aut['nom_prenom'] = (aut['nom'] + ' ' + aut['prenom']).str.strip()
|
|
aut['id'] = aut['id'].astype(str)
|
|
id_inconnu = aut[aut.nom == 'INCONNU'].id[0]
|
|
|
|
# merge des auteurs
|
|
df[col_aut].fillna(id_inconnu, inplace=True)
|
|
df[col_aut] = df[col_aut].replace(['\.0'],'',regex=True)
|
|
r_id = df[['id', col_aut]].copy()
|
|
r_idSplit = r_id[col_aut].str.split(' & ', expand=True)
|
|
r_id = r_id.join(r_idSplit)
|
|
cSplit = r_idSplit.shape[1]
|
|
cSplit = list(range(cSplit))
|
|
|
|
if orga:
|
|
# récup des organismes
|
|
org = _merge_orga(r_id, cSplit)
|
|
|
|
r_id[cSplit] = r_id[cSplit].replace(aut['id'].tolist(),aut['nom_prenom'].tolist())
|
|
r_id = _aggr_cols(r_id,cSplit,' & ') \
|
|
.rename(columns={'aggreg': 'auteur'}) \
|
|
.drop(columns=cSplit)
|
|
|
|
if orga:
|
|
# merge des organismes
|
|
r_id = merge(r_id,org, on=['id', col_aut], suffixes=[None,'_y'])
|
|
|
|
if on_index:
|
|
df = merge(df,r_id,how='left', right_index=True,left_index=True, suffixes=[None,'_y']) \
|
|
.drop(columns=['id'+'_y',col_aut+'_y',col_aut])
|
|
else:
|
|
df = merge(df,r_id,how='left', on=['id', col_aut], suffixes=[None,'_y']) \
|
|
.drop(columns=[col_aut])
|
|
|
|
return df
|
|
|
|
|
|
def _merge_relation(df, table, schema, id=None, left_id=None,right_id=None):
|
|
if id:
|
|
params = {id: df[id].tolist() }
|
|
elif left_id and right_id:
|
|
params = {right_id: df[left_id].tolist() }
|
|
|
|
mrg = _get_table(con, schema, table, params_col=params)
|
|
# if table == 'r_sites_auteur' or table == 'r_geomsites_auteur':
|
|
if table.startswith('r_') and table.endswith('_auteur'):
|
|
mrg = mrg[[right_id,'id_auteur']].groupby(
|
|
[right_id])['id_auteur'].apply(lambda x: ' & '.join(x.astype(str)))
|
|
mrg = DataFrame(data=mrg)
|
|
|
|
if id:
|
|
df = merge(df,mrg, how='left', on=id)
|
|
elif left_id and right_id:
|
|
df = merge(df,mrg, how='left', left_on=left_id, right_on=right_id,suffixes=[None,'_y'])
|
|
rmcol = df.columns[df.columns.str.endswith('_y')].tolist()
|
|
if rmcol:
|
|
df.drop(columns=[*rmcol], inplace=True)
|
|
return df |