From bfd637eb9c40872b954b608aef420b24393d3e6f Mon Sep 17 00:00:00 2001 From: Primoz Date: Tue, 25 Oct 2022 08:53:44 +0000 Subject: [PATCH] Improve strings formatting in straw_events file. --- .../process_user_event_related_segments.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/features/phone_esm/straw/process_user_event_related_segments.py b/src/features/phone_esm/straw/process_user_event_related_segments.py index 8d85231f..1574c690 100644 --- a/src/features/phone_esm/straw/process_user_event_related_segments.py +++ b/src/features/phone_esm/straw/process_user_event_related_segments.py @@ -8,6 +8,19 @@ from esm_preprocess import preprocess_esm, clean_up_esm input_data_files = dict(snakemake.input) +def format_timestamp(x): + tstring="" + space = False + if x//3600 > 0: + tstring += f"{x//3600}H" + space = True + if x % 3600 // 60 > 0: + tstring += f" {x % 3600 // 60}M" if "H" in tstring else f"{x % 3600 // 60}M" + if x % 60 > 0: + tstring += f" {x % 60}S" if "M" in tstring or "H" in tstring else f"{x % 60}S" + + return tstring + def extract_ers_from_file(esm_df, device_id): # TODO: kako se bodo pridobili device_id? Bo torej potreben tudi p0??.yaml? pd.set_option("display.max_rows", None) @@ -26,11 +39,11 @@ def extract_ers_from_file(esm_df, device_id): # TODO: kako se bodo pridobili dev extracted_ers = esm_df.groupby("esm_session")['timestamp'].apply(lambda x: math.ceil((x.max() - x.min()) / 1000)).reset_index() # in rounded up seconds time_before_questionnaire = 30 * 60 # in seconds (30 minutes) - extracted_ers["label"] = "straw_event_" + snakemake.params["pid"] + "_" + extracted_ers["esm_session"].astype(str) + extracted_ers["label"] = "straw_event_" + snakemake.params["pid"] + "_" + extracted_ers["esm_session"].astype(str).str.zfill(3) extracted_ers["event_timestamp"] = esm_df.groupby("esm_session")['timestamp'].min().reset_index()['timestamp'] - extracted_ers["length"] = (extracted_ers["timestamp"] + time_before_questionnaire).apply(lambda x: f"{x//3600}H {x % 3600 // 60}M {x % 60}S" if x//3600 > 0 else f"{x % 3600 // 60}M {x % 60}S") + 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: f"{x//3600}H {x % 3600 // 60}M {x % 60}S" if x//3600 > 0 else f"{x % 3600 // 60}M {x % 60}S") + extracted_ers["shift"] = extracted_ers["shift"].apply(lambda x: format_timestamp(x)) extracted_ers["shift_direction"] = -1 extracted_ers["device_id"] = device_id