Simplify pivoting a table and fix other mistakes.

communication
junos 2021-07-27 20:41:13 +02:00
parent 0f5af21f71
commit 9bd42afa02
1 changed files with 12 additions and 27 deletions

View File

@ -1,3 +1,4 @@
import numpy as np
import pandas as pd import pandas as pd
import features.esm import features.esm
@ -18,7 +19,6 @@ GROUP_QUESTIONNAIRES_BY = [
"participant_id", "participant_id",
"device_id", "device_id",
"esm_session", "esm_session",
"questionnaire_id",
] ]
# Each questionnaire occurs only once within each esm_session on the same device within the same participant. # Each questionnaire occurs only once within each esm_session on the same device within the same participant.
@ -51,7 +51,7 @@ def extract_stressful_events(df_esm: pd.DataFrame) -> pd.DataFrame:
df_esm_events = df_esm_events.join( df_esm_events = df_esm_events.join(
df_esm_event_work_related[ df_esm_event_work_related[
GROUP_QUESTIONNAIRES_BY.append("event_work_related") GROUP_QUESTIONNAIRES_BY + ["event_work_related"]
].set_index(GROUP_QUESTIONNAIRES_BY) ].set_index(GROUP_QUESTIONNAIRES_BY)
) )
@ -59,7 +59,7 @@ def extract_stressful_events(df_esm: pd.DataFrame) -> pd.DataFrame:
df_esm_event_time = convert_event_time(df_esm_sam_clean) df_esm_event_time = convert_event_time(df_esm_sam_clean)
df_esm_events = df_esm_events.join( df_esm_events = df_esm_events.join(
df_esm_event_time[GROUP_QUESTIONNAIRES_BY.append("event_time")].set_index( df_esm_event_time[GROUP_QUESTIONNAIRES_BY + ["event_time"]].set_index(
GROUP_QUESTIONNAIRES_BY GROUP_QUESTIONNAIRES_BY
) )
) )
@ -89,30 +89,14 @@ def calculate_threat_challenge_means(df_esm_sam_clean: pd.DataFrame) -> pd.DataF
) )
] ]
# Calculate mean of threat and challenge subscales for each ESM session. # Calculate mean of threat and challenge subscales for each ESM session.
df_esm_event_threat_challenge_mean = ( df_esm_event_threat_challenge_mean_wide = pd.pivot_table(df_esm_event_threat_challenge, index=["participant_id","device_id", "esm_session"], columns=["questionnaire_id"], values=["esm_user_answer_numeric"], aggfunc="mean")
df_esm_event_threat_challenge.groupby(GROUP_QUESTIONNAIRES_BY) # Drop unnecessary column values.
.esm_user_answer_numeric.agg("mean") df_esm_event_threat_challenge_mean_wide.columns = df_esm_event_threat_challenge_mean_wide.columns.get_level_values(1)
.reset_index() df_esm_event_threat_challenge_mean_wide.columns.name = None
.rename(columns={"esm_user_answer_numeric": "esm_numeric_mean"}) df_esm_event_threat_challenge_mean_wide.rename(columns={
)
# Rename questionnaire ID to indicate their names.
df_esm_event_threat_challenge_mean[
"event_subscale"
] = df_esm_event_threat_challenge_mean.questionnaire_id.astype(
"category"
).cat.rename_categories(
{
QUESTIONNAIRE_ID_SAM.get("event_threat"): "threat_mean", QUESTIONNAIRE_ID_SAM.get("event_threat"): "threat_mean",
QUESTIONNAIRE_ID_SAM.get("event_challenge"): "challenge_mean", QUESTIONNAIRE_ID_SAM.get("event_challenge"): "challenge_mean"
} }, inplace=True)
)
# Pivot a table so that each ESM session is represented by one row with threat and challenge means as two columns.
df_esm_event_threat_challenge_mean_wide = pd.pivot(
df_esm_event_threat_challenge_mean,
index=GROUP_QUESTIONNAIRES_BY,
columns=["event_subscale"],
values=["esm_numeric_mean"],
)
return df_esm_event_threat_challenge_mean_wide return df_esm_event_threat_challenge_mean_wide
@ -168,6 +152,7 @@ def extract_event_duration(df_esm_sam_clean: pd.DataFrame) -> pd.DataFrame:
# Generally, these answers were converted to esm_user_answer_numeric in clean_up_esm, # Generally, these answers were converted to esm_user_answer_numeric in clean_up_esm,
# but only the numeric types of questions and answers. # but only the numeric types of questions and answers.
# Since this was of "datetime" type, convert these specific answers here again. # Since this was of "datetime" type, convert these specific answers here again.
df_esm_event_duration["event_duration_info"] = np.nan
df_esm_event_duration[ df_esm_event_duration[
df_esm_event_duration.event_duration.isna() df_esm_event_duration.event_duration.isna()
] = df_esm_event_duration[df_esm_event_duration.event_duration.isna()].assign( ] = df_esm_event_duration[df_esm_event_duration.event_duration.isna()].assign(