From b5c9c9cddcb1cafb9a1081059c02c4f7b804a273 Mon Sep 17 00:00:00 2001 From: Zachary Clement Date: Tue, 9 Jan 2024 20:33:46 -0500 Subject: [PATCH 1/9] pep8 fixes --- forest/jasmine/traj2stats.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/forest/jasmine/traj2stats.py b/forest/jasmine/traj2stats.py index ed2d8a92..712fcd76 100644 --- a/forest/jasmine/traj2stats.py +++ b/forest/jasmine/traj2stats.py @@ -1643,6 +1643,13 @@ def gps_stats_main( participant_id, study_folder, "gps", tz_str, time_start, time_end, ) + + if ((data["longitude"].max() > 180) + or (data["longitude"].min() < -180)): + logger.info("Reconciled bad longitude data for user %s", + participant_id) + data["longitude"] = data["longitude"] % 360 - 180 + if data.shape == (0, 0): logger.info("No data available.") continue From 4972e87ec23758328e0b795415a2c631be189bf4 Mon Sep 17 00:00:00 2001 From: Zachary Clement Date: Tue, 9 Jan 2024 21:00:05 -0500 Subject: [PATCH 2/9] add warning to users requesting OSM summaries, make the transformation keep values the same if within valid range --- forest/jasmine/traj2stats.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/forest/jasmine/traj2stats.py b/forest/jasmine/traj2stats.py index 712fcd76..7ec7d154 100644 --- a/forest/jasmine/traj2stats.py +++ b/forest/jasmine/traj2stats.py @@ -1648,7 +1648,19 @@ def gps_stats_main( or (data["longitude"].min() < -180)): logger.info("Reconciled bad longitude data for user %s", participant_id) - data["longitude"] = data["longitude"] % 360 - 180 + data["longitude"] = (data["longitude"] + 180) % 360 - 180 + if ((places_of_interest is not None) + or (osm_tags is not None)): + logger.warning("Warning: user %s had longitude values " + "outside the valid range [-180, 180] " + "but OSM location summaries were " + "requested. Longitude values outside " + "the valid range may signify that GPS " + "fuzzing was directed to be used in " + "the study setup file. If GPS " + "coordinates were fuzzed, OSM " + "location summaries are meaningless", + participant_id) if data.shape == (0, 0): logger.info("No data available.") From 913e362256b09e6c4f2f2c6f492ecc11ed93a467 Mon Sep 17 00:00:00 2001 From: Zachary Clement Date: Thu, 18 Jan 2024 22:41:30 -0500 Subject: [PATCH 3/9] force compute_flight_positions and compute_future_flight_positions to return valid coordinates --- forest/jasmine/data2mobmat.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/forest/jasmine/data2mobmat.py b/forest/jasmine/data2mobmat.py index 2d8a78a5..ff959d23 100644 --- a/forest/jasmine/data2mobmat.py +++ b/forest/jasmine/data2mobmat.py @@ -614,6 +614,12 @@ def gps_to_mobmat( return mobmat +def force_valid_longitude(longitude): + """Forces a longitude coordinate to be within -180 and 180 + Args: + longitude: float. The longitude to be coerced + """ + return (longitude + 180) % 360 - 180 def compute_flight_positions( index: int, mobmat: np.ndarray, interval: float @@ -656,8 +662,8 @@ def compute_flight_positions( # Update the mobility matrix with the new start and end positions mobmat[index, 1] = start_x mobmat[index, 4] = end_x - mobmat[index, 2] = start_y - mobmat[index, 5] = end_y + mobmat[index, 2] = force_valid_longitude(start_y) + mobmat[index, 5] = force_valid_longitude(end_y) return mobmat @@ -704,8 +710,8 @@ def compute_future_flight_positions( # Update the mobility matrix with the new start and end positions mobmat[index, 1] = start_x mobmat[index, 4] = end_x - mobmat[index, 2] = start_y - mobmat[index, 5] = end_y + mobmat[index, 2] = force_valid_longitude(start_y) + mobmat[index, 5] = force_valid_longitude(end_y) return mobmat From d7e73ca22aa9888c6ef0a273d4a8c4a59ac1417e Mon Sep 17 00:00:00 2001 From: Zachary Clement Date: Thu, 18 Jan 2024 22:44:44 -0500 Subject: [PATCH 4/9] pep8 fixes --- forest/jasmine/data2mobmat.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/forest/jasmine/data2mobmat.py b/forest/jasmine/data2mobmat.py index ff959d23..5a86784c 100644 --- a/forest/jasmine/data2mobmat.py +++ b/forest/jasmine/data2mobmat.py @@ -614,6 +614,7 @@ def gps_to_mobmat( return mobmat + def force_valid_longitude(longitude): """Forces a longitude coordinate to be within -180 and 180 Args: @@ -621,6 +622,7 @@ def force_valid_longitude(longitude): """ return (longitude + 180) % 360 - 180 + def compute_flight_positions( index: int, mobmat: np.ndarray, interval: float ) -> np.ndarray: From ccf30f70b170f9fb59dcc2fb9e83a90471da84ae Mon Sep 17 00:00:00 2001 From: Zachary Clement Date: Thu, 18 Jan 2024 22:58:04 -0500 Subject: [PATCH 5/9] add type hint --- forest/jasmine/data2mobmat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forest/jasmine/data2mobmat.py b/forest/jasmine/data2mobmat.py index 6af0b201..8883ead8 100644 --- a/forest/jasmine/data2mobmat.py +++ b/forest/jasmine/data2mobmat.py @@ -619,7 +619,7 @@ def gps_to_mobmat( return mobmat -def force_valid_longitude(longitude): +def force_valid_longitude(longitude: float) -> float: """Forces a longitude coordinate to be within -180 and 180 Args: longitude: float. The longitude to be coerced From a1eb117263ba9e831426017e472ab5bbf9277888 Mon Sep 17 00:00:00 2001 From: Zachary Clement Date: Tue, 30 Jan 2024 19:07:29 -0500 Subject: [PATCH 6/9] Ensure longitude is in data columns before type checking --- forest/jasmine/traj2stats.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/forest/jasmine/traj2stats.py b/forest/jasmine/traj2stats.py index 206792ac..2695747c 100644 --- a/forest/jasmine/traj2stats.py +++ b/forest/jasmine/traj2stats.py @@ -1661,8 +1661,13 @@ def gps_stats_main( tz_str, time_start, time_end, ) - if ((data["longitude"].max() > 180) - or (data["longitude"].min() < -180)): + if ( + ("longitude" in data.columns) + and ( + (data["longitude"].max() > 180) + or (data["longitude"].min() < -180) + ) + ): logger.info("Reconciled bad longitude data for user %s", participant_id) data["longitude"] = (data["longitude"] + 180) % 360 - 180 From 798a8d74ce54e0599ff8ca9e4e10f39b0a008429 Mon Sep 17 00:00:00 2001 From: Zachary Clement Date: Mon, 12 Feb 2024 18:13:36 -0500 Subject: [PATCH 7/9] Add comment explaining logic behind wrapping --- forest/jasmine/traj2stats.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/forest/jasmine/traj2stats.py b/forest/jasmine/traj2stats.py index 2695747c..43852c59 100644 --- a/forest/jasmine/traj2stats.py +++ b/forest/jasmine/traj2stats.py @@ -1660,7 +1660,11 @@ def gps_stats_main( participant_id, study_folder, "gps", tz_str, time_start, time_end, ) - + # If the data comes from a study thata hada GPS fuzzing, + # and the study was prior to March 2023, the longitude + # coordinates may be outside of the required range of + # (-180, 180). This chunk of code wraps out of range + # coordinates to be in that range if ( ("longitude" in data.columns) and ( From d364b3e1884b6850cd41037a6f13a4faad7fb2df Mon Sep 17 00:00:00 2001 From: Zachary Clement Date: Mon, 12 Feb 2024 18:17:24 -0500 Subject: [PATCH 8/9] Explain better what the force_valid_coordinate function does --- forest/jasmine/data2mobmat.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/forest/jasmine/data2mobmat.py b/forest/jasmine/data2mobmat.py index 8883ead8..97a8ec22 100644 --- a/forest/jasmine/data2mobmat.py +++ b/forest/jasmine/data2mobmat.py @@ -621,6 +621,13 @@ def gps_to_mobmat( def force_valid_longitude(longitude: float) -> float: """Forces a longitude coordinate to be within -180 and 180 + + In some cases, the imputation code seems to yield out-of-range + GPS coordinates. This function wrps longitude coordinates to be back + in the correct range so an error isn't thrown. + + For example, 190 would get transformed into -170. + Args: longitude: float. The longitude to be coerced """ From 13668f4a3120f664d69878ffd8a44d2331e43bd1 Mon Sep 17 00:00:00 2001 From: Zachary Clement Date: Mon, 12 Feb 2024 18:27:53 -0500 Subject: [PATCH 9/9] PEP8 fixes --- forest/jasmine/data2mobmat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/forest/jasmine/data2mobmat.py b/forest/jasmine/data2mobmat.py index 97a8ec22..9b3f5c30 100644 --- a/forest/jasmine/data2mobmat.py +++ b/forest/jasmine/data2mobmat.py @@ -624,10 +624,10 @@ def force_valid_longitude(longitude: float) -> float: In some cases, the imputation code seems to yield out-of-range GPS coordinates. This function wrps longitude coordinates to be back - in the correct range so an error isn't thrown. + in the correct range so an error isn't thrown. For example, 190 would get transformed into -170. - + Args: longitude: float. The longitude to be coerced """