From af6c22b592eb5081e43aa68e53347bf542f81845 Mon Sep 17 00:00:00 2001 From: Vincent Emonet Date: Thu, 23 May 2024 11:54:21 +0200 Subject: [PATCH] check for duplicate categories --- backend/src/utils.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/backend/src/utils.py b/backend/src/utils.py index 3df25d8..128cf43 100644 --- a/backend/src/utils.py +++ b/backend/src/utils.py @@ -177,15 +177,16 @@ def retrieve_cohorts_metadata(user_email: str) -> dict[str, Cohort]: # Process categories of variables if "varName" in row and "categoryLabel" in row and "categoryValue" in row: - target_dict[cohort_id].variables[var_id].categories.append( - VariableCategory( - value=str(row["categoryValue"]["value"]), - label=str(row["categoryLabel"]["value"]), - concept_id=get_curie_value("categoryConceptId", row), - mapped_id=get_curie_value("categoryMappedId", row), - mapped_label=get_value("categoryMappedLabel", row), - ) + new_category = VariableCategory( + value=str(row["categoryValue"]["value"]), + label=str(row["categoryLabel"]["value"]), + concept_id=get_curie_value("categoryConceptId", row), + mapped_id=get_curie_value("categoryMappedId", row), + mapped_label=get_value("categoryMappedLabel", row), ) + # Check for duplicates before appending + if new_category not in target_dict[cohort_id].variables[var_id].categories: + target_dict[cohort_id].variables[var_id].categories.append(new_category) # Merge dictionaries, cohorts with variables first return {**cohorts_with_variables, **cohorts_without_variables}