Repository: mie-lab/trackintel

Issue:
temporal_tracking_quality in datamodel accessors
the function `temporal_tracking_quality` should be added to each model accessor


Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/mie-lab/trackintel/pull/201?src=pr&el=h1) Report
> Merging [#201](https://codecov.io/gh/mie-lab/trackintel/pull/201?src=pr&el=desc) (a3021df) into [master](https://codecov.io/gh/mie-lab/trackintel/commit/af3866f76fd27d3f5ddc213f9018ec792f8907d0?el=desc) (af3866f) will **increase** coverage by `0.04%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/mie-lab/trackintel/pull/201/graphs/tree.svg?width=650&height=150&src=pr&token=ITDZEEE1MO)](https://codecov.io/gh/mie-lab/trackintel/pull/201?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #201      +/-   ##
==========================================
+ Coverage   88.19%   88.24%   +0.04%     
==========================================
  Files          36       36              
  Lines        1483     1489       +6     
  Branches      258      258              
==========================================
+ Hits         1308     1314       +6     
  Misses        136      136              
  Partials       39       39              
```


| [Impacted Files](https://codecov.io/gh/mie-lab/trackintel/pull/201?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [trackintel/analysis/tracking\_quality.py](https://codecov.io/gh/mie-lab/trackintel/pull/201/diff?src=pr&el=tree#diff-dHJhY2tpbnRlbC9hbmFseXNpcy90cmFja2luZ19xdWFsaXR5LnB5) | `98.79% <ø> (ø)` | |
| [trackintel/model/staypoints.py](https://codecov.io/gh/mie-lab/trackintel/pull/201/diff?src=pr&el=tree#diff-dHJhY2tpbnRlbC9tb2RlbC9zdGF5cG9pbnRzLnB5) | `97.05% <100.00%> (+0.18%)` | :arrow_up: |
| [trackintel/model/triplegs.py](https://codecov.io/gh/mie-lab/trackintel/pull/201/diff?src=pr&el=tree#diff-dHJhY2tpbnRlbC9tb2RlbC90cmlwbGVncy5weQ==) | `97.14% <100.00%> (+0.17%)` | :arrow_up: |
| [trackintel/model/trips.py](https://codecov.io/gh/mie-lab/trackintel/pull/201/diff?src=pr&el=tree#diff-dHJhY2tpbnRlbC9tb2RlbC90cmlwcy5weQ==) | `95.00% <100.00%> (+0.55%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/mie-lab/trackintel/pull/201?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/mie-lab/trackintel/pull/201?src=pr&el=footer). Last update [af3866f...a3021df](https://codecov.io/gh/mie-lab/trackintel/pull/201?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/trackintel/analysis/tracking_quality.py b/trackintel/analysis/tracking_quality.py
index d70f783..67085b8 100644
--- a/trackintel/analysis/tracking_quality.py
+++ b/trackintel/analysis/tracking_quality.py
@@ -6,7 +6,7 @@ import numpy as np
 
 def temporal_tracking_quality(source, granularity="all"):
     """
-    Calculate per-user temporal tracking quality defined as the temporal coverage.
+    Calculate per-user temporal tracking quality (temporal coverage).
 
     Parameters
     ----------
diff --git a/trackintel/model/staypoints.py b/trackintel/model/staypoints.py
index 86522b6..c837813 100644
--- a/trackintel/model/staypoints.py
+++ b/trackintel/model/staypoints.py
@@ -118,3 +118,11 @@ class StaypointsAccessor(object):
         See :func:`trackintel.io.postgis.write_staypoints_postgis`.
         """
         ti.io.postgis.write_staypoints_postgis(self._obj, conn_string, table_name)
+
+    def temporal_tracking_quality(self, *args, **kwargs):
+        """
+        Calculate per-user temporal tracking quality (temporal coverage).
+
+        See :func:`trackintel.analysis.tracking_quality.temporal_tracking_quality`.
+        """
+        return ti.analysis.tracking_quality.temporal_tracking_quality(self._obj, *args, **kwargs)
diff --git a/trackintel/model/triplegs.py b/trackintel/model/triplegs.py
index a7ebd68..2888481 100644
--- a/trackintel/model/triplegs.py
+++ b/trackintel/model/triplegs.py
@@ -118,3 +118,11 @@ class TriplegsAccessor(object):
         See :func:`trackintel.analysis.modal_split.calculate_modal_split`.
         """
         return ti.analysis.modal_split.calculate_modal_split(self._obj, *args, **kwargs)
+
+    def temporal_tracking_quality(self, *args, **kwargs):
+        """
+        Calculate per-user temporal tracking quality (temporal coverage).
+
+        See :func:`trackintel.analysis.tracking_quality.temporal_tracking_quality`.
+        """
+        return ti.analysis.tracking_quality.temporal_tracking_quality(self._obj, *args, **kwargs)
diff --git a/trackintel/model/trips.py b/trackintel/model/trips.py
index 7f67ceb..ca1dda1 100644
--- a/trackintel/model/trips.py
+++ b/trackintel/model/trips.py
@@ -84,3 +84,11 @@ class TripsAccessor(object):
         See :func:`trackintel.io.postgis.write_trips_postgis`.
         """
         ti.io.postgis.write_trips_postgis(self._obj, conn_string, table_name, schema, sql_chunksize, if_exists)
+
+    def temporal_tracking_quality(self, *args, **kwargs):
+        """
+        Calculate per-user temporal tracking quality (temporal coverage).
+
+        See :func:`trackintel.analysis.tracking_quality.temporal_tracking_quality`.
+        """
+        return ti.analysis.tracking_quality.temporal_tracking_quality(self._obj, *args, **kwargs)


Test patch:
diff --git a/tests/analysis/test_tracking_quality.py b/tests/analysis/test_tracking_quality.py
index a935bac..0d07230 100644
--- a/tests/analysis/test_tracking_quality.py
+++ b/tests/analysis/test_tracking_quality.py
@@ -1,7 +1,7 @@
 import os
 
 import pytest
-
+import pandas as pd
 import trackintel as ti
 
 
@@ -216,3 +216,26 @@ class TestTemporal_tracking_quality:
         # no record has different days after the split
         day_diff = splitted["finished_at"].dt.day - splitted["started_at"].dt.day
         assert (day_diff == 0).all()
+
+    def test_accessors(self):
+        """The result obtained from model accessors should be the same as calling the function."""
+        pfs, _ = ti.io.dataset_reader.read_geolife(os.path.join("tests", "data", "geolife_long"))
+        pfs, stps = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=25, time_threshold=5)
+        stps = stps.as_staypoints.create_activity_flag(time_threshold=15)
+        pfs, tpls = pfs.as_positionfixes.generate_triplegs(stps, method="between_staypoints")
+        stps, tpls, trips = ti.preprocessing.triplegs.generate_trips(stps, tpls, gap_threshold=15)
+
+        # for staypoints
+        stps_quality_accessor = stps.as_staypoints.temporal_tracking_quality()
+        stps_quality_method = ti.analysis.tracking_quality.temporal_tracking_quality(stps)
+        pd.testing.assert_frame_equal(stps_quality_accessor, stps_quality_method)
+
+        # for triplegs
+        tpls_quality_accessor = tpls.as_triplegs.temporal_tracking_quality()
+        tpls_quality_method = ti.analysis.tracking_quality.temporal_tracking_quality(tpls)
+        pd.testing.assert_frame_equal(tpls_quality_accessor, tpls_quality_method)
+
+        # for trips
+        trips_quality_accessor = trips.as_trips.temporal_tracking_quality()
+        trips_quality_method = ti.analysis.tracking_quality.temporal_tracking_quality(trips)
+        pd.testing.assert_frame_equal(trips_quality_accessor, trips_quality_method)