Begin ERS logic for 90-minutes events.

imputation_and_cleaning
Primoz 2022-11-04 15:09:04 +00:00
parent c1c9f4d05a
commit 9f441afc16
1 changed files with 40 additions and 11 deletions

View File

@ -24,7 +24,7 @@ def format_timestamp(x):
def extract_ers_from_file(esm_df, device_id):
# pd.set_option("display.max_rows", None)
pd.set_option("display.max_rows", 20)
pd.set_option("display.max_columns", None)
with open('config.yaml', 'r') as stream:
@ -37,26 +37,55 @@ def extract_ers_from_file(esm_df, device_id):
classified = classify_sessions_by_completion_time(esm_preprocessed)
esm_filtered_sessions = classified[classified["session_response"] == 'ema_completed'].reset_index()[['device_id', 'esm_session']]
esm_df = esm_preprocessed.loc[(esm_preprocessed['device_id'].isin(esm_filtered_sessions['device_id'])) & (esm_preprocessed['esm_session'].isin(esm_filtered_sessions['esm_session']))]
# Problem ne bo ekstrahiranje posameznih začetkov in trajanj stresnih dogodkov - večji problem je pridobitev ustreznega targeta,
# tako da bo poravnan s tem dogodkom, saj se lahko zgodi, da je timestamp zabeležene intenzitete stresnega dogodka ne pade v okno stresnega dogodka.
# Edina izjema tega so, če je označen odgovor "1 - Še vedno traja" pri vprašanju appraisal_event_duration
# Extract time-relevant information
targets_method = config["TIME_SEGMENTS"]["TAILORED_EVENTS"]["TARGETS_METHOD"]
if targets_method == "thirty_before": # takes 30-minute peroid before the questionnaire + the duration of the questionnaire
time_before_questionnaire = 30 * 60 # in seconds (30 minutes)
extracted_ers = esm_df.groupby(["device_id", "esm_session"])['timestamp'].apply(lambda x: math.ceil((x.max() - x.min()) / 1000)).reset_index() # in rounded up in seconds
if targets_method in ["30_before", "90_before"]: # takes 30-minute peroid before the questionnaire + the duration of the questionnaire
extracted_ers = esm_df.groupby(["device_id", "esm_session"])['timestamp'].apply(lambda x: math.ceil((x.max() - x.min()) / 1000)).reset_index() # is rounded up in seconds
extracted_ers["label"] = f"straw_event_{targets_method}_" + snakemake.params["pid"] + "_" + extracted_ers.index.astype(str).str.zfill(3)
extracted_ers[['event_timestamp', 'device_id']] = esm_df.groupby(["device_id", "esm_session"])['timestamp'].min().reset_index()[['timestamp', 'device_id']]
extracted_ers = extracted_ers[extracted_ers["timestamp"] <= 15 * 60].reset_index(drop=True) # ensure that the longest duration of the questionnaire anwsering is 15 min
extracted_ers["label"] = "straw_event_" + snakemake.params["pid"] + "_" + extracted_ers.index.astype(str).str.zfill(3)
extracted_ers["length"] = (extracted_ers["timestamp"] + time_before_questionnaire).apply(lambda x: format_timestamp(x))
extracted_ers["shift"] = time_before_questionnaire
extracted_ers["shift"] = extracted_ers["shift"].apply(lambda x: format_timestamp(x))
extracted_ers["shift_direction"] = -1
if targets_method == "30_before":
time_before_questionnaire = 30 * 60 # in seconds (30 minutes)
extracted_ers["length"] = (extracted_ers["timestamp"] + time_before_questionnaire).apply(lambda x: format_timestamp(x))
extracted_ers["shift"] = time_before_questionnaire
extracted_ers["shift"] = extracted_ers["shift"].apply(lambda x: format_timestamp(x))
elif targets_method == "90_before":
time_before_questionnaire = 90 * 60 # in seconds (90 minutes)
extracted_ers[['end_event_timestamp', 'device_id']] = esm_df.groupby(["device_id", "esm_session"])['timestamp'].max().reset_index()[['timestamp', 'device_id']]
extracted_ers['diffs'] = extracted_ers['event_timestamp'].astype('int64') - extracted_ers['end_event_timestamp'].shift(1, fill_value=0).astype('int64')
extracted_ers.loc[extracted_ers['diffs'] > time_before_questionnaire * 1000, 'diffs'] = time_before_questionnaire * 1000
# TODO: združi celotno trajanje in formatiraj v HH:MM:SS
sys.exit()
elif targets_method == "stress_events":
pass
# VV Testiranje različnih povpraševanj za VV
# print(esm_df[esm_df.questionnaire_id == 87])
# filter_esm = esm_df[(esm_df.esm_type == 7) & ((esm_df.questionnaire_id == 90.) | (esm_df.questionnaire_id == 91.))][['questionnaire_id', 'esm_user_answer', 'esm_session']]
# print(filter_esm[filter_esm.esm_user_answer == "1 - Še vedno traja"].shape)
# print(filter_esm.shape)
else:
extracted_ers = pd.DataFrame(columns=["label", "event_timestamp", "length", "shift", "shift_direction", "device_id"])
# sys.exit()
sys.exit()
return extracted_ers[["label", "event_timestamp", "length", "shift", "shift_direction", "device_id"]]
if snakemake.params["stage"] == "extract":