API
src.Process.API
Functions
healthcare_request(env='production', endpoint='HealthcareService', test_mode=False, config_file_dir=None)
Fetches healthcare service data based on specified environment and endpoint, processes the data, and returns a concatenated DataFrame of all service locations.
This function generates request filters, uses an ROR client to fetch data from the specified endpoint, extracts location information from each response, and compiles all locations into a single DataFrame.
Parameters:
env : Literal["production", "recette"], optional The environment to use for the request. Defaults to "production". endpoint : str, optional The name of the endpoint to request data from. Defaults to "HealthcareService".
Returns:
pd.DataFrame A DataFrame containing all extracted service locations.
Raises:
Exception If an error occurs during the request or data processing.
Example:
df = healthcare_request(env="recette", endpoint="HealthcareService") print(df.head())
Source code in src/Process/API.py
def healthcare_request(env: Literal["production", "recette"] = "production", endpoint: str = "HealthcareService", test_mode: bool = False, config_file_dir=None) -> pd.DataFrame:
"""
Fetches healthcare service data based on specified environment and endpoint, processes the data,
and returns a concatenated DataFrame of all service locations.
This function generates request filters, uses an ROR client to fetch data from the specified endpoint,
extracts location information from each response, and compiles all locations into a single DataFrame.
Parameters:
-----------
env : Literal["production", "recette"], optional
The environment to use for the request. Defaults to "production".
endpoint : str, optional
The name of the endpoint to request data from. Defaults to "HealthcareService".
Returns:
--------
pd.DataFrame
A DataFrame containing all extracted service locations.
Raises:
-------
Exception
If an error occurs during the request or data processing.
Example:
--------
>>> df = healthcare_request(env="recette", endpoint="HealthcareService")
>>> print(df.head())
"""
try:
mapping_config_df = generate_request_filter()
groupe_columns_names = ["Service", "Code_Groupe", "Code_Sous-groupe", "Regroupements"]
client = RORClient(env=env, config_file_dir=config_file_dir)
list_hcs_org_dfs = []
list_anomalies = []
nombre_total = len(mapping_config_df) # Calcul du nombre total de requêtes
for i in range(nombre_total):
subgroupe, r_filter = mapping_config_df["Regroupements"][i], mapping_config_df["filter"][i]
# La condition if r_filter nécessaire sinon la requette s'effectura sur l'ensemble de healtcar !!! Toute la BDD
logger.info(f"Requesting {i}/{nombre_total}: {subgroupe}")
response, _ = client.request_all_pages(endpoint=endpoint, request_filter=r_filter, test_mode=test_mode)
# Try, dans le cas où aucune donnée n'est renvoyé
try:
data_df_brut = pd.json_normalize(
response,
record_path = 'entry',
errors='ignore')
# ----- Traitements pour HealthcareService -----
df_HcS = data_df_brut[data_df_brut['resource.resourceType']=="HealthcareService"]
# Traitement des données
healthcare_df = healthcareservice_data_management(df_HcS)
# --- Gestion des anomalies ---
df_anomalies = anomalies(healthcare_df)
list_anomalies.append(df_anomalies)
id_anomalies = list(df_anomalies["HCS_id"].unique())
healthcare_df = healthcare_df[~healthcare_df["HCS_id"].isin(id_anomalies)]
# --- Application des filtre d'exclusion ---
filters = {
"Exclusion_OA_filter": ("activiteOperationnelle", exclude_by_list, "Exclusion_OA"),
"Exclusif_filter": ("activiteOperationnelle", exclude_by_length, "Exclusif"),
"Exclusion_typeOffre_filter": ("typeOffre", exclude_by_typeoffre, "TypeOffre"),
"Exclusion_niveauExp_filter": ("niveauExpertise", exclude_by_niveauexpertise_actespecifique, "NiveauExpertise", "Code_Sous-groupe"),
"Exclusion_acteSpe_filter": ("acteSpecifique", exclude_by_niveauexpertise_actespecifique, "ActeSpecifique", "Code_Sous-groupe")
}
# Applique les filtres d'exclusion
for new_column, (column_name, func, *args) in filters.items():
healthcare_df[new_column] = healthcare_df[column_name].apply(lambda lst: func(lst, mapping_config_df[args[0]][i], *(mapping_config_df[arg][i] for arg in args[1:]))).astype(bool)
# Cas spécifique R4.2
if subgroupe == "R4.2":
healthcare_df.loc[~healthcare_df["Exclusion_niveauExp_filter"] | ~healthcare_df["Exclusion_acteSpe_filter"],
["Exclusion_niveauExp_filter","Exclusion_acteSpe_filter"]] = False
# Exclusion des éléments incohérents
mask = ~(healthcare_df[filters.keys()].eq(True).any(axis=1))
healthcare_df = healthcare_df.loc[mask]
# Sélectionner la ligne du mapping config et répliquer
line_group_map_df = mapping_config_df.loc[i, :]
# Répliquer la ligne du mapping config pour qu'elle ait le même nombre de lignes que healthcare_df
replicated_config_df = pd.DataFrame([line_group_map_df] * len(healthcare_df), index=healthcare_df.index) # columns=groupe_columns_names
# Concaténer horizontalement (ajouter les colonnes)
healthcare_df = pd.concat([replicated_config_df, healthcare_df], axis=1)
# ----- Traitements pour Organization -----
df_Orga = data_df_brut[data_df_brut['resource.resourceType']=="Organization"]
organization_df = organization_data_management(df_Orga)
healthcare_org_df = merge_try_error(
healthcare_df,
organization_df,
left_on="provided_by",
right_on="provided_by"
)
length_doublon = len(healthcare_org_df)
healthcare_org_df = healthcare_org_df.drop_duplicates(subset=["Code_Sous-groupe", "HCS_id"], keep="first")
logger.info(f"Nombre d'observations en doublon pour {subgroupe} : {length_doublon - len(healthcare_org_df)}")
list_hcs_org_dfs.append(healthcare_org_df)
logger.success(f"End of process for {subgroupe}:{i}/{nombre_total}")
except KeyError:
logger.info(f"Pas de résultat pour la requête {subgroupe}")
# Concaténer verticalement tous les DataFrames de la liste
logger.debug(list_hcs_org_dfs)
healthcare_org_df = pd.concat(list_hcs_org_dfs, ignore_index=True)
df_anomalies = pd.concat(list_anomalies, ignore_index=True)
# Gestion du regroupement C4 et C6 (requête divisé en 2)
length_doublon = len(healthcare_org_df)
healthcare_org_df = count_dub(healthcare_org_df, ["Code_Sous-groupe", "HCS_id"])
healthcare_org_df = healthcare_org_df.loc[~((healthcare_org_df["size"]==1) & (healthcare_org_df["Code_Sous-groupe"]=="C6"))] # Pour C6, on ne garde que ceux en doublons
healthcare_org_df = healthcare_org_df.drop_duplicates(subset=["Code_Sous-groupe", "HCS_id"], keep="first") # Pour C4, on dédoublonne
logger.info(f"Nombre d'observations en doublon global : {length_doublon - len(healthcare_org_df)}")
# Dédoublonner les anomalies
df_anomalies = df_anomalies.drop_duplicates(subset=["HCS_id", "Nom_Anomalie"], keep="first")
# Sortie des anomalies d'Organization
organization_anomalies_df = organization_anomalies(healthcare_org_df)
# ----- Dernier traitement de post-processing -----
return healthcare_org_df, df_anomalies, organization_anomalies_df
except Exception as e:
logger.error(f"An error occurred: {e}")
raise
location_request(endpoint='Location', env='production', test_mode=False, config_file_dir=None)
Fetches Location data based on specified environment and endpoint, processes the data, and returns a concatenated DataFrame of all locations and their bed capacity.
This function generates request filters, uses an ROR client to fetch data from the specified endpoint, extracts location information from each response, and compiles all locations into a single DataFrame.
Parameters:
env : Literal["production", "recette"], optional The environment to use for the request. Defaults to "production". endpoint : str, optional The name of the endpoint to request data from. Defaults to "HealthcareService".
Returns:
pd.DataFrame A DataFrame containing all extracted service locations.
Raises:
Exception If an error occurs during the request or data processing.
Example:
df = location_request(env="recette", endpoint="Location") print(df.head())
Source code in src/Process/API.py
def location_request(endpoint: str = "Location", env: Literal["production", "recette"] = "production", test_mode: bool = False, config_file_dir=None)-> pd.DataFrame :
"""
Fetches Location data based on specified environment and endpoint, processes the data,
and returns a concatenated DataFrame of all locations and their bed capacity.
This function generates request filters, uses an ROR client to fetch data from the specified endpoint,
extracts location information from each response, and compiles all locations into a single DataFrame.
Parameters:
-----------
env : Literal["production", "recette"], optional
The environment to use for the request. Defaults to "production".
endpoint : str, optional
The name of the endpoint to request data from. Defaults to "HealthcareService".
Returns:
--------
pd.DataFrame
A DataFrame containing all extracted service locations.
Raises:
-------
Exception
If an error occurs during the request or data processing.
Example:
--------
>>> df = location_request(env="recette", endpoint="Location")
>>> print(df.head())
"""
try:
# Paramètres
client = RORClient(env=env, config_file_dir=config_file_dir)
# Requete
response, page = client.request_all_pages(endpoint = endpoint, test_mode = test_mode)
logger.info(f"Total {endpoint} page resquest : {page}")
# Transformation de la requette en dataframe
location_df_brut = pd.json_normalize(
response,
record_path = 'entry',
errors='ignore')
# Calcul des capacités de lits
location_df = location_data_management(location_df_brut)
location_df_agg = calcul_bed_capacity(location_df)
# Ajout des départements/régions, des colonnes dates
location_df_agg = format_postal_code(location_df_agg, col_postal_code = "postal_code")
location_df_agg = mapping_dep(location_df_agg, col_key_on = "departement")
logger.info(f"{location_df_agg.shape[0]} location unique gardé à partir des {location_df.shape[0]} requêtés")
# Signalisation des anomalies (code postal vide et lits_installes négatifs)
location_df_agg, location_anomalies_df = location_anomalies(location_df_agg)
return location_df_agg, location_anomalies_df
except Exception as e:
logger.error(f"An error occurred: {e}")
raise