labda 0.0.3__tar.gz → 0.0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {labda-0.0.3 → labda-0.0.4}/PKG-INFO +1 -1
- {labda-0.0.3 → labda-0.0.4}/labda/parsers/gadgetbridge.py +7 -13
- {labda-0.0.3 → labda-0.0.4}/labda/parsers/qstarz.py +5 -2
- {labda-0.0.3 → labda-0.0.4}/labda/parsers/utils.py +22 -5
- {labda-0.0.3 → labda-0.0.4}/labda/processing/spatial/timeline.py +2 -1
- {labda-0.0.3 → labda-0.0.4}/labda/processing/spatial/transportation.py +3 -0
- {labda-0.0.3 → labda-0.0.4}/labda/processing/spatial/trips.py +65 -36
- {labda-0.0.3 → labda-0.0.4}/labda/structure/domains.py +1 -1
- {labda-0.0.3 → labda-0.0.4}/labda/structure/linkage.py +2 -3
- {labda-0.0.3 → labda-0.0.4}/labda/structure/subject.py +1 -1
- labda-0.0.4/labda/visualisation/spatial.py +127 -0
- {labda-0.0.3 → labda-0.0.4}/pyproject.toml +1 -1
- labda-0.0.3/labda/assets/__pycache__/__init__.cpython-311.pyc +0 -0
- labda-0.0.3/labda/assets/__pycache__/counts_cut_points.cpython-311.pyc +0 -0
- labda-0.0.3/labda/assets/__pycache__/transportation_cut_points.cpython-311.pyc +0 -0
- labda-0.0.3/labda/expanders/__pycache__/__init__.cpython-311.pyc +0 -0
- labda-0.0.3/labda/expanders/__pycache__/accelerometer.cpython-311.pyc +0 -0
- labda-0.0.3/labda/expanders/__pycache__/extras.cpython-311.pyc +0 -0
- labda-0.0.3/labda/expanders/__pycache__/spatial.cpython-311.pyc +0 -0
- labda-0.0.3/labda/parsers/__pycache__/__init__.cpython-311.pyc +0 -0
- labda-0.0.3/labda/parsers/__pycache__/actigraph.cpython-311.pyc +0 -0
- labda-0.0.3/labda/parsers/__pycache__/bulk.cpython-311.pyc +0 -0
- labda-0.0.3/labda/parsers/__pycache__/gadgetbridge.cpython-311.pyc +0 -0
- labda-0.0.3/labda/parsers/__pycache__/qstarz.cpython-311.pyc +0 -0
- labda-0.0.3/labda/parsers/__pycache__/utils.cpython-311.pyc +0 -0
- labda-0.0.3/labda/parsers/__pycache__/vendors.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/__pycache__/__init__.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/__pycache__/bouts.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/__pycache__/domains.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/__pycache__/manipulations.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/accelerometer/__pycache__/__init__.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/accelerometer/__pycache__/activity_intensity.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/accelerometer/__pycache__/cut_points.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/accelerometer/__pycache__/steps.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/accelerometer/__pycache__/wear.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/spatial/__pycache__/__init__.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/spatial/__pycache__/manipulations.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/spatial/__pycache__/timeline.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/spatial/__pycache__/transportation.cpython-311.pyc +0 -0
- labda-0.0.3/labda/processing/spatial/__pycache__/trips.cpython-311.pyc +0 -0
- labda-0.0.3/labda/structure/__pycache__/__init__.cpython-311.pyc +0 -0
- labda-0.0.3/labda/structure/__pycache__/collection.cpython-311.pyc +0 -0
- labda-0.0.3/labda/structure/__pycache__/domains.cpython-311.pyc +0 -0
- labda-0.0.3/labda/structure/__pycache__/linkage.cpython-311.pyc +0 -0
- labda-0.0.3/labda/structure/__pycache__/merging.cpython-311.pyc +0 -0
- labda-0.0.3/labda/structure/__pycache__/resampling.cpython-311.pyc +0 -0
- labda-0.0.3/labda/structure/__pycache__/subject.cpython-311.pyc +0 -0
- labda-0.0.3/labda/structure/validation/__pycache__/__init__.cpython-311.pyc +0 -0
- labda-0.0.3/labda/structure/validation/__pycache__/domains.cpython-311.pyc +0 -0
- labda-0.0.3/labda/structure/validation/__pycache__/linkage.cpython-311.pyc +0 -0
- labda-0.0.3/labda/structure/validation/__pycache__/subject.cpython-311.pyc +0 -0
- labda-0.0.3/labda/visualisation/__pycache__/__init__.cpython-311.pyc +0 -0
- labda-0.0.3/labda/visualisation/__pycache__/spatial.cpython-311.pyc +0 -0
- labda-0.0.3/labda/visualisation/spatial.py +0 -93
- {labda-0.0.3 → labda-0.0.4}/CHANGELOG.md +0 -0
- {labda-0.0.3 → labda-0.0.4}/README.md +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/__init__.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/assets/__init__.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/assets/counts_cut_points.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/assets/transportation_cut_points.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/expanders/__init__.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/expanders/accelerometer.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/expanders/extras.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/expanders/spatial.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/logging.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/parallel.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/parsers/__init__.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/parsers/actigraph.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/parsers/bulk.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/parsers/garmin.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/processing/__init__.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/processing/accelerometer/__init__.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/processing/accelerometer/activity_intensity.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/processing/accelerometer/steps.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/processing/accelerometer/wear.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/processing/bouts.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/processing/domains.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/processing/manipulations.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/processing/spatial/__init__.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/processing/spatial/manipulations.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/processing/wear_validity.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/structure/__init__.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/structure/collection.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/structure/merging.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/structure/resampling.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/structure/validation/__init__.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/structure/validation/domains.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/structure/validation/linkage.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/structure/validation/subject.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/utils.py +0 -0
- {labda-0.0.3 → labda-0.0.4}/labda/visualisation/__init__.py +0 -0
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import secrets
|
|
2
1
|
from datetime import datetime
|
|
3
2
|
from pathlib import Path
|
|
4
3
|
|
|
@@ -112,20 +111,15 @@ def _get_xiaomi_devices(
|
|
|
112
111
|
return devices
|
|
113
112
|
|
|
114
113
|
|
|
115
|
-
def _get_wear_by_hr(df: pd.DataFrame) -> pd.DataFrame:
|
|
116
|
-
|
|
117
|
-
df = df.copy().reset_index(drop=True)
|
|
114
|
+
def _get_wear_by_hr(df: pd.DataFrame, limit: int | None = None) -> pd.DataFrame:
|
|
115
|
+
df = df.copy()
|
|
118
116
|
df[Column.WEAR] = pd.NA
|
|
119
|
-
|
|
120
|
-
intervals = indices.to_series().diff()
|
|
117
|
+
df.loc[df[Column.HEART_RATE].notna(), Column.WEAR] = True
|
|
121
118
|
|
|
122
|
-
if
|
|
123
|
-
|
|
124
|
-
df.loc[df[Column.HEART_RATE].notna(), Column.WEAR] = True
|
|
119
|
+
if limit:
|
|
120
|
+
df[Column.WEAR] = df[Column.WEAR].bfill(limit=limit)
|
|
125
121
|
|
|
126
|
-
|
|
127
|
-
df[Column.WEAR] = df[Column.WEAR].bfill(limit=interval)
|
|
128
|
-
df.loc[df[Column.WEAR].isna(), Column.WEAR] = False
|
|
122
|
+
df.loc[df[Column.WEAR].isna(), Column.WEAR] = False
|
|
129
123
|
|
|
130
124
|
return df
|
|
131
125
|
|
|
@@ -168,8 +162,8 @@ def _generate_records(
|
|
|
168
162
|
for record in records:
|
|
169
163
|
id = record[0] # type: ignore
|
|
170
164
|
df = record[1] # type: ignore
|
|
171
|
-
df = _get_wear_by_hr(df)
|
|
172
165
|
df.set_index(Column.DATETIME, inplace=True)
|
|
166
|
+
df = _get_wear_by_hr(df, 4)
|
|
173
167
|
df = SCHEMA.validate(df)
|
|
174
168
|
|
|
175
169
|
sampling_frequency = get_sampling_frequency(df)
|
|
@@ -27,8 +27,11 @@ def detect_environment_from_gps_signal(
|
|
|
27
27
|
df = df.copy()
|
|
28
28
|
|
|
29
29
|
methods = [
|
|
30
|
-
Column.
|
|
31
|
-
Column.
|
|
30
|
+
Column.SNR_USED, # 225 (F1: 46,80)
|
|
31
|
+
Column.SNR_VIEWED, # 260 (F1: 71,18)
|
|
32
|
+
Column.NSAT_USED, # 7 (F1: 48,86)
|
|
33
|
+
Column.NSAT_VIEWED, # 9 (F1: 42,93)
|
|
34
|
+
Column.NSAT_RATIO, # 70 (F1: 46,40)
|
|
32
35
|
]
|
|
33
36
|
if method not in methods:
|
|
34
37
|
raise ValueError(f"Method muset be one of {methods}")
|
|
@@ -7,18 +7,31 @@ from ..utils import change_crs, change_timezone, get_crs, get_timezone
|
|
|
7
7
|
|
|
8
8
|
DEFAULT_CRS_SENSOR = "EPSG:4326"
|
|
9
9
|
|
|
10
|
+
# TODO: Maybe those functions should be refactored, i.e. if (None, timezone) -> timezone would be guessed from the data and then changed to the target timezone. Same for CRS.
|
|
11
|
+
|
|
10
12
|
|
|
11
13
|
def set_timezone(
|
|
12
14
|
df: pd.DataFrame,
|
|
13
15
|
timezone: str | None | Tuple[str, str] = None,
|
|
14
|
-
) -> Tuple[pd.DataFrame, str]:
|
|
15
|
-
if
|
|
16
|
+
) -> Tuple[pd.DataFrame, str | None]:
|
|
17
|
+
if (
|
|
18
|
+
isinstance(timezone, tuple)
|
|
19
|
+
and len(timezone) == 2
|
|
20
|
+
and all(isinstance(tz, str) for tz in timezone)
|
|
21
|
+
):
|
|
16
22
|
source_tz = timezone[0]
|
|
17
23
|
target_tz = timezone[1]
|
|
18
24
|
df = change_timezone(df, source_tz, target_tz)
|
|
19
25
|
timezone = target_tz
|
|
26
|
+
elif isinstance(timezone, str):
|
|
27
|
+
pass
|
|
20
28
|
elif not timezone:
|
|
21
|
-
|
|
29
|
+
if "latitude" in df.columns and "longitude" in df.columns:
|
|
30
|
+
timezone = get_timezone(df)
|
|
31
|
+
else:
|
|
32
|
+
timezone = None
|
|
33
|
+
else:
|
|
34
|
+
raise ValueError(f"Invalid value for timezone: {timezone}")
|
|
22
35
|
|
|
23
36
|
return df, timezone
|
|
24
37
|
|
|
@@ -27,7 +40,11 @@ def set_crs(
|
|
|
27
40
|
df: pd.DataFrame,
|
|
28
41
|
crs: str | None | Tuple[str, str] = None,
|
|
29
42
|
) -> Tuple[pd.DataFrame, str]:
|
|
30
|
-
if
|
|
43
|
+
if (
|
|
44
|
+
isinstance(crs, tuple)
|
|
45
|
+
and len(crs) == 2
|
|
46
|
+
and all(isinstance(tz, str) for tz in crs)
|
|
47
|
+
):
|
|
31
48
|
source_crs = crs[0]
|
|
32
49
|
target_crs = crs[1]
|
|
33
50
|
df = change_crs(df, source_crs, target_crs)
|
|
@@ -38,6 +55,6 @@ def set_crs(
|
|
|
38
55
|
crs = get_crs(df)
|
|
39
56
|
df = change_crs(df, DEFAULT_CRS_SENSOR, crs)
|
|
40
57
|
else:
|
|
41
|
-
|
|
58
|
+
raise ValueError(f"Invalid value for crs: {crs}")
|
|
42
59
|
|
|
43
60
|
return df, crs
|
|
@@ -80,7 +80,8 @@ def get_transport_information(df: pd.DataFrame, crs: str):
|
|
|
80
80
|
|
|
81
81
|
|
|
82
82
|
def get_timeline(df: pd.DataFrame, crs: str) -> pd.DataFrame:
|
|
83
|
-
# TODO: Add a check for specific trip columns
|
|
83
|
+
# TODO: Add a check for specific trip columns (that they are exists, so timeline can be calculated)
|
|
84
|
+
# TODO: Add indoor/outdoor trip detection, same one as in removal of indoor trips inside trips.py (it should mark whole trip if it was inside or not - maybe?)
|
|
84
85
|
df = df.copy()
|
|
85
86
|
|
|
86
87
|
trips = (
|
|
@@ -7,6 +7,9 @@ from ...expanders.spatial import add_speed
|
|
|
7
7
|
from ...structure.validation.subject import Column
|
|
8
8
|
from ...utils import columns_exists, columns_not_exists, get_unique_column_name
|
|
9
9
|
|
|
10
|
+
# TODO: Add something, so even pause has a mode.
|
|
11
|
+
# TODO: Option: pause_mode = True/False, before, after, mixed?
|
|
12
|
+
|
|
10
13
|
|
|
11
14
|
def _set_trip_mode(df: pd.DataFrame, name: str):
|
|
12
15
|
modes = df[name].value_counts()
|
|
@@ -58,7 +58,22 @@ def get_trips(
|
|
|
58
58
|
# Set the trip status to 'stationary' if the point is a stop, otherwise 'transport'.
|
|
59
59
|
df["trip_status"] = np.where(df["stop"], "stationary", "transport")
|
|
60
60
|
|
|
61
|
-
|
|
61
|
+
# Creating category column and setting the categories.
|
|
62
|
+
# FIXME: Import categories from Subject SCHEMA.
|
|
63
|
+
# TODO: Maybe move all the setting of categories to a separate function and as initialisation (start of the detect_trips function).
|
|
64
|
+
categories = [
|
|
65
|
+
"start",
|
|
66
|
+
"end",
|
|
67
|
+
"start-end",
|
|
68
|
+
"transport",
|
|
69
|
+
"pause",
|
|
70
|
+
"stationary",
|
|
71
|
+
]
|
|
72
|
+
df["trip_status"] = pd.Categorical(
|
|
73
|
+
df["trip_status"], categories=categories, ordered=False
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
return df.astype({"stop_id": "UInt16"})
|
|
62
77
|
|
|
63
78
|
|
|
64
79
|
def get_pauses(
|
|
@@ -133,7 +148,6 @@ def add_boundaries(
|
|
|
133
148
|
Returns:
|
|
134
149
|
None. The function modifies df in-place.
|
|
135
150
|
"""
|
|
136
|
-
df[status_col] = df[status_col].cat.add_categories(["start", "end", "start-end"])
|
|
137
151
|
|
|
138
152
|
# If 'transport' is at the start or end of the DataFrame, change it to 'start' or 'end' respectively.
|
|
139
153
|
if extremities:
|
|
@@ -227,7 +241,7 @@ def adjust_stationary_statutes(df: pd.DataFrame):
|
|
|
227
241
|
|
|
228
242
|
df.drop("group", axis=1, inplace=True)
|
|
229
243
|
|
|
230
|
-
return df
|
|
244
|
+
return df
|
|
231
245
|
|
|
232
246
|
|
|
233
247
|
def add_ids(
|
|
@@ -268,6 +282,7 @@ def remove_trips_by_duration(df: pd.DataFrame, min_duration: timedelta) -> None:
|
|
|
268
282
|
Returns:
|
|
269
283
|
None: The function modifies the DataFrame in-place, and does not return anything.
|
|
270
284
|
"""
|
|
285
|
+
|
|
271
286
|
trips = df[
|
|
272
287
|
df["trip_id"].notna()
|
|
273
288
|
& df["trip_status"].isin(["start", "transport", "end", "start-end"])
|
|
@@ -282,16 +297,17 @@ def remove_trips_by_duration(df: pd.DataFrame, min_duration: timedelta) -> None:
|
|
|
282
297
|
# Select only the trips that are shorter than the minimum duration.
|
|
283
298
|
removed_trips = trips[trips["duration"] < min_duration][["segment_id", "trip_id"]]
|
|
284
299
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
300
|
+
if not removed_trips.empty:
|
|
301
|
+
# Change rows based on both segment_id and trip_id and set the trip_id and trip_status to NA.
|
|
302
|
+
mask = df.set_index(["segment_id", "trip_id"]).index.isin(
|
|
303
|
+
removed_trips.set_index(["segment_id", "trip_id"]).index
|
|
304
|
+
)
|
|
305
|
+
df.loc[mask, "trip_id"] = pd.NA
|
|
306
|
+
# df.loc[mask, "trip_status"] = (
|
|
307
|
+
# pd.NA # HACK: Possibly set this to "stationary" if removed trips should be part of locations, but then need to adjust the stationary_id as well.
|
|
308
|
+
# )
|
|
309
|
+
df.loc[mask, "trip_status"] = "stationary"
|
|
310
|
+
df.loc[mask, "stationary_id"] = df.loc[mask, "stationary_id"].ffill()
|
|
295
311
|
|
|
296
312
|
|
|
297
313
|
def remove_trips_by_length(df: pd.DataFrame, min_length: int | float, crs: str) -> None:
|
|
@@ -321,16 +337,17 @@ def remove_trips_by_length(df: pd.DataFrame, min_length: int | float, crs: str)
|
|
|
321
337
|
# Select only the trips that are shorter than the minimum length.
|
|
322
338
|
removed_trips = trips[trips["length"] < min_length][["segment_id", "trip_id"]]
|
|
323
339
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
340
|
+
if not removed_trips.empty:
|
|
341
|
+
# Change rows based on both segment_id and trip_id and set the trip_id and trip_status to NA.
|
|
342
|
+
mask = df.set_index(["segment_id", "trip_id"]).index.isin(
|
|
343
|
+
removed_trips.set_index(["segment_id", "trip_id"]).index
|
|
344
|
+
)
|
|
345
|
+
df.loc[mask, "trip_id"] = pd.NA
|
|
346
|
+
# df.loc[mask, "trip_status"] = (
|
|
347
|
+
# pd.NA # HACK: Possibly set this to "stationary" if removed trips should be part of locations, but then need to adjust the stationary_id as well.
|
|
348
|
+
# )
|
|
349
|
+
df.loc[mask, "trip_status"] = "stationary"
|
|
350
|
+
df.loc[mask, "stationary_id"] = df.loc[mask, "stationary_id"].ffill()
|
|
334
351
|
|
|
335
352
|
|
|
336
353
|
def _change_indoor_trip_to_stationary(df: pd.DataFrame, limit: float) -> str | None:
|
|
@@ -357,21 +374,25 @@ def remove_indoor_trips(df: pd.DataFrame, indoor_limit: float):
|
|
|
357
374
|
trips = trips.groupby(["segment_id", "trip_id"], as_index=False).apply(
|
|
358
375
|
lambda x: _change_indoor_trip_to_stationary(x, indoor_limit) # type: ignore
|
|
359
376
|
)
|
|
360
|
-
trips.rename(columns={None: "environment"}, inplace=True) # type: ignore
|
|
361
|
-
trips = trips.astype({"environment": "category"})
|
|
362
377
|
|
|
363
|
-
|
|
378
|
+
if not trips.empty:
|
|
379
|
+
trips.rename(columns={None: "environment"}, inplace=True) # type: ignore
|
|
380
|
+
trips = trips.astype({"environment": "category"})
|
|
364
381
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
382
|
+
removed_trips = trips[trips["environment"] == "indoor"][
|
|
383
|
+
["segment_id", "trip_id"]
|
|
384
|
+
]
|
|
385
|
+
|
|
386
|
+
# Change rows based on both segment_id and trip_id and set the trip_id and trip_status to NA.
|
|
387
|
+
mask = df.set_index(["segment_id", "trip_id"]).index.isin(
|
|
388
|
+
removed_trips.set_index(["segment_id", "trip_id"]).index
|
|
389
|
+
)
|
|
390
|
+
df.loc[mask, "trip_id"] = pd.NA
|
|
391
|
+
# df.loc[mask, "trip_status"] = (
|
|
392
|
+
# pd.NA # HACK: Possibly set this to "stationary" if removed trips should be part of locations, but then need to adjust the stationary_id as well.
|
|
393
|
+
# )
|
|
394
|
+
df.loc[mask, "trip_status"] = "stationary"
|
|
395
|
+
df.loc[mask, "stationary_id"] = df.loc[mask, "stationary_id"].bfill()
|
|
375
396
|
|
|
376
397
|
|
|
377
398
|
def get_segment_trips(
|
|
@@ -509,6 +530,13 @@ def detect_trips(
|
|
|
509
530
|
pd.DataFrame: The processed DataFrame with updated 'trip_id' and 'trip_status' columns.
|
|
510
531
|
"""
|
|
511
532
|
|
|
533
|
+
# TODO: Check sampling frequency against some parameters, e.g. if sampling frequency is 2 minute, we can't have a gap_duration of 1 minute etc. Check what happens if some parameters are the same as sampling frequency.
|
|
534
|
+
if (not min_duration or min_duration == 0) and (not min_length or min_length == 0):
|
|
535
|
+
raise ValueError("Both 'min_duration' and 'min_length' cannot be None or zero.")
|
|
536
|
+
|
|
537
|
+
if df.empty:
|
|
538
|
+
raise ValueError("The input DataFrame is empty.")
|
|
539
|
+
|
|
512
540
|
columns_not_exists(
|
|
513
541
|
df,
|
|
514
542
|
["segment_id", "trip_id", "trip_status", "stationary_id"],
|
|
@@ -561,6 +589,7 @@ def detect_trips(
|
|
|
561
589
|
remove_trips_by_length(temp_df, min_length=min_length, crs=crs)
|
|
562
590
|
|
|
563
591
|
if "environment" in temp_df.columns and indoor_limit:
|
|
592
|
+
# TODO: Should be this set outside of the function? And maybe just annotated if the trip is indoor or not, so researchers can exclude indoor trips and say that they are stationary.
|
|
564
593
|
remove_indoor_trips(temp_df, indoor_limit)
|
|
565
594
|
|
|
566
595
|
temp_df = temp_df[temp_df["trip_status"].notna()]
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
from pathlib import Path
|
|
2
2
|
|
|
3
3
|
import pandas as pd
|
|
4
|
-
from shapely import wkt
|
|
5
4
|
|
|
6
5
|
from .validation.linkage import SCHEMA
|
|
7
6
|
|
|
@@ -20,9 +19,9 @@ def from_csv(path: str | Path) -> pd.DataFrame:
|
|
|
20
19
|
)
|
|
21
20
|
|
|
22
21
|
if "start" in df.columns:
|
|
23
|
-
df["start"] = pd.to_datetime(df["start"])
|
|
22
|
+
df["start"] = pd.to_datetime(df["start"], format="%Y-%m-%d %H:%M:%S")
|
|
24
23
|
|
|
25
24
|
if "end" in df.columns:
|
|
26
|
-
df["end"] = pd.to_datetime(df["end"])
|
|
25
|
+
df["end"] = pd.to_datetime(df["end"], format="%Y-%m-%d %H:%M:%S")
|
|
27
26
|
|
|
28
27
|
return SCHEMA.validate(df)
|
|
@@ -199,7 +199,7 @@ class Subject(BaseModel):
|
|
|
199
199
|
gap_duration: timedelta,
|
|
200
200
|
stop_radius: int | float,
|
|
201
201
|
stop_duration: timedelta,
|
|
202
|
-
window: int | None =
|
|
202
|
+
window: int | None = None,
|
|
203
203
|
pause_radius: int | float | None = None,
|
|
204
204
|
pause_duration: timedelta | None = None,
|
|
205
205
|
min_duration: timedelta | None = None,
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
from typing import Any, Dict, Tuple
|
|
2
|
+
|
|
3
|
+
import geopandas as gpd
|
|
4
|
+
import pandas as pd
|
|
5
|
+
import pydeck as pdk
|
|
6
|
+
from shapely.geometry import Point
|
|
7
|
+
|
|
8
|
+
from ..utils import convert_to_geodataframe
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def get_status(row):
|
|
12
|
+
if row["status"] == "stationary":
|
|
13
|
+
return f"Stationary {row['stationary_id']}"
|
|
14
|
+
elif row["status"] == "transport":
|
|
15
|
+
return f"Trip {row['trip_id']}"
|
|
16
|
+
elif row["status"] == "pause":
|
|
17
|
+
return f"Trip {row['trip_id']} (Pause {row['stationary_id']})"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def get_color(row):
|
|
21
|
+
if row["status"] == "stationary":
|
|
22
|
+
return [255, 0, 0]
|
|
23
|
+
elif row["status"] == "transport":
|
|
24
|
+
return [0, 255, 0]
|
|
25
|
+
elif row["status"] == "pause":
|
|
26
|
+
return [50, 205, 50]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def timeline_layer(
|
|
30
|
+
df: pd.DataFrame, crs: str | None = None
|
|
31
|
+
) -> Tuple[pdk.Layer, Dict[str, Any], Point]:
|
|
32
|
+
gdf = gpd.GeoDataFrame(df, geometry="geometry", crs=crs) # type: ignore
|
|
33
|
+
center = gdf.geometry.centroid.to_crs(epsg=4326).unary_union.centroid
|
|
34
|
+
|
|
35
|
+
gdf["duration"] = gdf["duration"].astype(str)
|
|
36
|
+
gdf["start"] = gdf["start"].dt.strftime("%Y-%m-%d %H:%M:%S")
|
|
37
|
+
gdf["end"] = gdf["end"].dt.strftime("%Y-%m-%d %H:%M:%S")
|
|
38
|
+
gdf["distance"] = gdf["distance"].round(2)
|
|
39
|
+
gdf["status_text"] = gdf.apply(get_status, axis=1)
|
|
40
|
+
gdf["mode"] = gdf["mode"].cat.add_categories(["Stationary"])
|
|
41
|
+
gdf["mode"] = gdf["mode"].fillna("Stationary")
|
|
42
|
+
gdf["color"] = gdf.apply(get_color, axis=1)
|
|
43
|
+
|
|
44
|
+
gdf.to_crs(epsg=4326, inplace=True)
|
|
45
|
+
|
|
46
|
+
layer = pdk.Layer(
|
|
47
|
+
"GeoJsonLayer",
|
|
48
|
+
gdf,
|
|
49
|
+
get_position="geometry.coordinates",
|
|
50
|
+
get_radius=10, # Radius is in meters
|
|
51
|
+
get_fill_color="color",
|
|
52
|
+
get_line_color="color",
|
|
53
|
+
get_line_width=7.5,
|
|
54
|
+
pickable=True,
|
|
55
|
+
auto_highlight=True,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
tooltip = {
|
|
59
|
+
"html": """<strong>Status:</strong> {status_text}<br/>
|
|
60
|
+
<strong>Start:</strong> {start}<br/>
|
|
61
|
+
<strong>End:</strong> {end}<br/>
|
|
62
|
+
<strong>Duration:</strong> {duration}<br/>
|
|
63
|
+
<strong>Distance:</strong> {distance}<br/>
|
|
64
|
+
<strong>Mode:</strong> {mode}<br/>
|
|
65
|
+
""",
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return layer, tooltip, center
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def gps_layer(
|
|
72
|
+
df: pd.DataFrame,
|
|
73
|
+
crs: str | None = None,
|
|
74
|
+
) -> Tuple[pdk.Layer, Dict[str, Any], Point]:
|
|
75
|
+
gdf = convert_to_geodataframe(df, crs=crs)
|
|
76
|
+
gdf.to_crs(epsg=4326, inplace=True)
|
|
77
|
+
center = gdf.geometry.unary_union.centroid
|
|
78
|
+
|
|
79
|
+
gdf["latitude"] = gdf.geometry.y
|
|
80
|
+
gdf["longitude"] = gdf.geometry.x
|
|
81
|
+
gdf.reset_index(inplace=True)
|
|
82
|
+
gdf["datetime"] = gdf["datetime"].dt.strftime("%Y-%m-%d %H:%M:%S")
|
|
83
|
+
|
|
84
|
+
layer = pdk.Layer(
|
|
85
|
+
"GeoJsonLayer",
|
|
86
|
+
gdf,
|
|
87
|
+
stroked=False,
|
|
88
|
+
filled=True,
|
|
89
|
+
get_position="geometry.coordinates",
|
|
90
|
+
get_radius=7.5, # Radius is in meters
|
|
91
|
+
get_fill_color=[255, 0, 0],
|
|
92
|
+
get_line_width=5,
|
|
93
|
+
pickable=True,
|
|
94
|
+
auto_highlight=True,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
tooltip = {
|
|
98
|
+
"html": """<strong>Datetime:</strong> {datetime}<br/>
|
|
99
|
+
<strong>Latitude:</strong> {latitude}<br/>
|
|
100
|
+
<strong>Longitude:</strong> {longitude}<br/>
|
|
101
|
+
""",
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return layer, tooltip, center
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def plot(
|
|
108
|
+
df: pd.DataFrame,
|
|
109
|
+
kind: str,
|
|
110
|
+
*,
|
|
111
|
+
crs: str | None = None,
|
|
112
|
+
) -> str:
|
|
113
|
+
match kind:
|
|
114
|
+
case "timeline":
|
|
115
|
+
layer, tooltip, center = timeline_layer(df, crs)
|
|
116
|
+
case "gps":
|
|
117
|
+
layer, tooltip, center = gps_layer(df, crs)
|
|
118
|
+
case _:
|
|
119
|
+
raise ValueError(f"Kind '{kind}' not supported.")
|
|
120
|
+
|
|
121
|
+
# Set the viewport location
|
|
122
|
+
view_state = pdk.ViewState(longitude=center.x, latitude=center.y, zoom=10)
|
|
123
|
+
|
|
124
|
+
# Render
|
|
125
|
+
r = pdk.Deck(layers=[layer], initial_view_state=view_state, tooltip=tooltip) # type: ignore
|
|
126
|
+
|
|
127
|
+
return r.to_html() # type: ignore
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
from typing import Any, Dict, Tuple
|
|
2
|
-
|
|
3
|
-
import geopandas as gpd
|
|
4
|
-
import pandas as pd
|
|
5
|
-
import pydeck as pdk
|
|
6
|
-
from shapely.geometry import Point
|
|
7
|
-
|
|
8
|
-
from ..utils import convert_to_geodataframe
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def timeline_layer(df: pd.DataFrame, crs: str | None = None) -> Tuple[pdk.Layer, Point]:
|
|
12
|
-
gdf = gpd.GeoDataFrame(df, geometry="geometry", crs=crs) # type: ignore
|
|
13
|
-
center = gdf.geometry.centroid.to_crs(epsg=4326).unary_union.centroid
|
|
14
|
-
|
|
15
|
-
gdf["duration"] = gdf["duration"].dt.total_seconds()
|
|
16
|
-
gdf["start"] = gdf["start"].dt.strftime("%Y-%m-%d %H:%M:%S")
|
|
17
|
-
gdf["end"] = gdf["end"].dt.strftime("%Y-%m-%d %H:%M:%S")
|
|
18
|
-
|
|
19
|
-
gdf.to_crs(epsg=4326, inplace=True)
|
|
20
|
-
gdf = gdf.astype(
|
|
21
|
-
{
|
|
22
|
-
"trip_id": "float64",
|
|
23
|
-
"stationary_id": "float64",
|
|
24
|
-
"status": "object",
|
|
25
|
-
"mode": "object",
|
|
26
|
-
}
|
|
27
|
-
) # type: ignore
|
|
28
|
-
|
|
29
|
-
geojson = gdf.__geo_interface__ # type: ignore
|
|
30
|
-
|
|
31
|
-
layer = pdk.Layer(
|
|
32
|
-
"GeoJsonLayer",
|
|
33
|
-
geojson,
|
|
34
|
-
stroked=False,
|
|
35
|
-
filled=True,
|
|
36
|
-
get_position="geometry.coordinates",
|
|
37
|
-
get_radius=10, # Radius is in meters
|
|
38
|
-
get_fill_color=[255, 0, 0],
|
|
39
|
-
get_line_color=[127, 0, 0],
|
|
40
|
-
get_line_width=5,
|
|
41
|
-
pickable=True,
|
|
42
|
-
auto_highlight=True,
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
return layer, center
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
def gps_layer(
|
|
49
|
-
df: pd.DataFrame,
|
|
50
|
-
crs: str | None = None,
|
|
51
|
-
) -> Tuple[pdk.Layer, Point]:
|
|
52
|
-
gdf = convert_to_geodataframe(df, crs=crs)
|
|
53
|
-
gdf.to_crs(epsg=4326, inplace=True)
|
|
54
|
-
center = gdf.geometry.unary_union.centroid
|
|
55
|
-
geojson = gdf.__geo_interface__ # type: ignore
|
|
56
|
-
layer = pdk.Layer(
|
|
57
|
-
"GeoJsonLayer",
|
|
58
|
-
geojson,
|
|
59
|
-
stroked=False,
|
|
60
|
-
filled=True,
|
|
61
|
-
get_position="geometry.coordinates",
|
|
62
|
-
get_radius=10, # Radius is in meters
|
|
63
|
-
get_fill_color=[255, 0, 0],
|
|
64
|
-
get_line_color=[127, 0, 0],
|
|
65
|
-
get_line_width=5,
|
|
66
|
-
pickable=True,
|
|
67
|
-
auto_highlight=True,
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
return layer, center
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
def plot(
|
|
74
|
-
df: pd.DataFrame,
|
|
75
|
-
kind: str,
|
|
76
|
-
*,
|
|
77
|
-
crs: str | None = None,
|
|
78
|
-
) -> str:
|
|
79
|
-
match kind:
|
|
80
|
-
case "timeline":
|
|
81
|
-
layer, center = timeline_layer(df, crs)
|
|
82
|
-
case "gps":
|
|
83
|
-
layer, center = gps_layer(df, crs)
|
|
84
|
-
case _:
|
|
85
|
-
raise ValueError(f"Kind '{kind}' not supported.")
|
|
86
|
-
|
|
87
|
-
# Set the viewport location
|
|
88
|
-
view_state = pdk.ViewState(longitude=center.x, latitude=center.y, zoom=10)
|
|
89
|
-
|
|
90
|
-
# Render
|
|
91
|
-
r = pdk.Deck(layers=[layer], initial_view_state=view_state)
|
|
92
|
-
|
|
93
|
-
return r.to_html() # type: ignore
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|