From efd01b89c353e672117f04ae2877f63cbdd74d81 Mon Sep 17 00:00:00 2001 From: TeachMeTW Date: Mon, 21 Oct 2024 21:37:57 -0700 Subject: [PATCH] Added #121 --- utils/db_utils.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/utils/db_utils.py b/utils/db_utils.py index c3ffc91..a44b4c8 100644 --- a/utils/db_utils.py +++ b/utils/db_utils.py @@ -176,7 +176,24 @@ def query_confirmed_trips(start_date: str, end_date: str, tz: str): # Add primary modes from the sensed, inferred and ble summaries. Note that we do this # **before** filtering the `all_trip_columns` because the # *_section_summary columns are not currently valid - get_max_mode_from_summary = lambda md: max(md["distance"], key=md["distance"].get) if len(md["distance"]) > 0 else "INVALID" + # Check if 'md' is not a dictionary or does not contain the key 'distance' + # or if 'md["distance"]' is not a dictionary. + # If any of these conditions are true, return "INVALID". + get_max_mode_from_summary = lambda md: ( + "INVALID" + if not isinstance(md, dict) + or "distance" not in md + or not isinstance(md["distance"], dict) + # If 'md' is a dictionary and 'distance' is a valid key pointing to a dictionary: + else ( + # Get the maximum value from 'md["distance"]' using the values of 'md["distance"].get' as the key for 'max'. + # This operation only happens if the length of 'md["distance"]' is greater than 0. + # Otherwise, return "INVALID". + max(md["distance"], key=md["distance"].get) + if len(md["distance"]) > 0 + else "INVALID" + ) + ) df["data.primary_sensed_mode"] = df.cleaned_section_summary.apply(get_max_mode_from_summary) df["data.primary_predicted_mode"] = df.inferred_section_summary.apply(get_max_mode_from_summary) if 'ble_sensed_summary' in df.columns: