Add a function to determine EMA session time.

communication
junos 2021-06-11 16:34:09 +02:00
parent f48e5469e0
commit a3417c182a
1 changed files with 23 additions and 1 deletions

View File

@ -65,7 +65,7 @@ def preprocess_esm(df_esm: pd.DataFrame) -> pd.DataFrame:
return df_esm.join(df_esm_json) return df_esm.join(df_esm_json)
def classify_sessions_adherence(df_esm_preprocessed: pd.DataFrame) -> pd.DataFrame: def classify_sessions_by_completion(df_esm_preprocessed: pd.DataFrame) -> pd.DataFrame:
""" """
For each distinct EMA session, determine how the participant responded to it. For each distinct EMA session, determine how the participant responded to it.
Possible outcomes are: SESSION_STATUS_UNANSWERED, SESSION_STATUS_DAY_FINISHED, and SESSION_STATUS_COMPLETE Possible outcomes are: SESSION_STATUS_UNANSWERED, SESSION_STATUS_DAY_FINISHED, and SESSION_STATUS_COMPLETE
@ -143,3 +143,25 @@ def classify_sessions_adherence(df_esm_preprocessed: pd.DataFrame) -> pd.DataFra
] = SESSION_STATUS_COMPLETE ] = SESSION_STATUS_COMPLETE
return df_session_counts return df_session_counts
def classify_sessions_by_time(df_esm_preprocessed: pd.DataFrame) -> pd.DataFrame:
"""
For each EMA session, determine the time of the first user answer and its time type (morning, workday, or evening.)
Parameters
----------
df_esm_preprocessed: pd.DataFrame
A preprocessed dataframe of esm data, which must include the session ID (esm_session).
Returns
-------
df_session_time: pd.DataFrame
A dataframe of all sessions (grouped by GROUP_SESSIONS_BY) with their time type and timestamp of first answer.
"""
df_session_time = (
df_esm_preprocessed.sort_values(["participant_id", "datetime_lj"])
.groupby(GROUP_SESSIONS_BY)
.first()[["time", "datetime_lj"]]
)
return df_session_time