From c0236b251cfca5ed35c765198b5fa8fb51c218a5 Mon Sep 17 00:00:00 2001 From: junos Date: Mon, 3 Jul 2023 20:11:11 +0200 Subject: [PATCH] Fix question IDs. --- features/esm_COPE.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/features/esm_COPE.py b/features/esm_COPE.py index 3f25362..5cbb16f 100644 --- a/features/esm_COPE.py +++ b/features/esm_COPE.py @@ -136,7 +136,7 @@ def reassign_question_ids(df_cope_cleaned: pd.DataFrame) -> pd.DataFrame: ) # Tabulate all possible answers to each question (group by question ID). - df_cope_fixed = df_cope_cleaned.copy() + # First, check that we anticipated all esm instructions. for q_id in dict_COPE_question_ids.keys(): # Look for all questions ("instructions") occurring in the dataframe. actual_questions = df_esm_cope_unique_questions.loc[ @@ -154,6 +154,20 @@ def reassign_question_ids(df_cope_cleaned: pd.DataFrame) -> pd.DataFrame: raise KeyError(actual_questions[~questions_matches]) # In case there is an unexpected answer, raise an exception. + # Next, replace question IDs. + df_cope_fixed = df_cope_cleaned.copy() + df_cope_fixed["question_id"] = df_cope_cleaned["esm_instructions"].apply( + lambda x: next( + ( + key + for key, values in dict_COPE_question_ids.items() + if x.startswith(values) + ), + None, + ) + ) + + # Finally, increment numeric answers. try: df_cope_fixed = df_cope_fixed.assign( esm_user_score=lambda x: x.esm_user_answer_numeric + 1