diff --git a/features/esm_SAM.py b/features/esm_SAM.py index e4c5adc..1f30ed6 100644 --- a/features/esm_SAM.py +++ b/features/esm_SAM.py @@ -55,6 +55,15 @@ def extract_stressful_events(df_esm: pd.DataFrame) -> pd.DataFrame: ) ) + # 4. + df_esm_event_duration = extract_event_duration(df_esm_sam_clean) + + df_esm_events = df_esm_events.join( + df_esm_event_duration[ + GROUP_QUESTIONNAIRES_BY + ["event_duration", "event_duration_info"] + ].set_index(GROUP_QUESTIONNAIRES_BY) + ) + return df_esm_events @@ -118,3 +127,36 @@ def convert_event_time(df_esm_sam_clean: pd.DataFrame) -> pd.DataFrame: ) ) return df_esm_event_time + + +def extract_event_duration(df_esm_sam_clean: pd.DataFrame) -> pd.DataFrame: + df_esm_event_duration = df_esm_sam_clean[ + df_esm_sam_clean["questionnaire_id"] + == QUESTIONNAIRE_ID_SAM.get("event_duration") + ].assign( + event_duration=lambda x: pd.to_datetime( + x.esm_user_answer.str.slice(start=0, stop=-6), errors="coerce" + ).dt.time + ) + # TODO Explore the values recorded in event_duration and possibly fix mistakes. + # For example, participants reported setting 23:50:00 instead of 00:50:00. + + # For the events that no duration was found (i.e. event_duration = NaT), + # we can determine whether: + # - this event is still going on ("1 - It is still going on") + # - the participant couldn't remember it's duration ("0 - I do not remember") + # Generally, these answers were converted to esm_user_answer_numeric in clean_up_esm, + # but only the numeric types of questions and answers. + # Since this was of "datetime" type, convert these specific answers here again. + df_esm_event_duration[ + df_esm_event_duration.event_duration.isna() + ] = df_esm_event_duration[df_esm_event_duration.event_duration.isna()].assign( + event_duration_info=lambda x: x.esm_user_answer.str.slice(stop=1).astype( + int + ) + ) + + return df_esm_event_duration + + +# TODO: How many questions about the stressfulness of the period were asked and how does this relate to events?