Flutter Package: Mobility Features in Real-time

--

Source: https://martechtoday.com/location-data-accuracy-leads-stronger-personalization-210112

Author: Thomas Nilsson thomasnilsson.github.io

Mobility Features Package: pub.dev/packages/mobility_features

CACHET Flutter Plugins: pub.dev/publishers/cachet.dk/packages

Introduction

This contents of this article are derived from the Master’s Thesis, A Flutter Package for Real-Time Mobility Feature Computation (2020), by Thomas Nilsson which is available here.

The result of the thesis was the Mobility Features package, available on the Dart package manager.

This package is part of the collection of CACHET Flutter plugins, a collection of plugins used for mobile health applications. This includes data channels such as step counting, location, noise, weather, and many more.

Background

Research has shown that mobility features derived from location data can be used to describe behavior changes in people suffering from depression-related diseases [Saeb et al 2015, Canzian et al 2015].

Existing contributions dealing with the context generation are difficult to reproduce due to a lack of publicly available source code. Furthermore, real-time computation on a smart-phone is often not considered in their implementation, meaning features are not available as data comes in.

These two research problems were addressed in this thesis, by implementing the Mobility Features package — a software package for the Flutter framework. The package makes it possible for you to compute the following features with just a few lines of Flutter code:

Stop: A cluster set of raw GPS data points represented by the median latitude and longitude. A Stop also has an arrival and departure time. Stops are found using distance grouping.

Place: A cluster of Stops found with the DBSCAN algorithm (minimum 1 stop).

Significant Place: A Place with a minimum duration, ex 3 minutes.

Move: A trajectory between two Stops with a distance (in meters).

The number of Significant Places: The size/cardinality of the set of Significant Places (see above).

Distance Travelled: The sum of Move distances.

Entropy: The entropy of the time distribution of the places.

Normalized Entropy: The entropy normalized by the maximum potential entropy.

Home Stay: The percentage of time spent at the home cluster.

Location Variance: The combined statistical variance of the latitude and longitude.

Using the Flutter package

First, add the package to your pubspec.yaml file and import the package

import 'package:mobility_features/mobility_features.dart';

No permissions are required to use the package itself, however, a location plugin should be used to collect location data.

Next, make a reference to the Mobility Factory singleton instance.

MobilityFactory mobilityFactory = MobilityFactory.instance;

Optionally, the parameters can be configured, which will influence the algorithms for producing features (see the Computing features section):

mobilityFactory.stopDuration = Duration(seconds: 20);
mobilityFactory.placeRadius = 50.0;
mobilityFactory.stopRadius = 5.0;

The stop radius should be kept low (5–20 meters)

The place radius somewhat higher (25–50 meters).

The stop duration can also be set to any desired duration, for most cases it should be kept lower than 3 minutes.

Features computation is triggered when the user moves around and changes their geo-position by a certain distance (stop distance). If the stop was long enough (stop duration) the stop will be saved. Places are computed by grouping stops based on the distance between them (place radius).

Common for these parameters is that their value depends on what you are trying to capture: Low parameter values will make the features more fine-grained but will trigger computation more often and will likely also lead to noisy features.

For example, given a low stop duration, stopping for a red light in traffic will count as a stop. Such granularity will be irrelevant for many use cases but may be useful if questions such as ‘do they take the same route to work every day?’ are to be answered.

Tracking location data

When using the package you can flexibly choose any Flutter plugin for tracking location data, as long as it can track location while the application is running in the background.

We recommend using the CARP Background Location Plugin, which works for both Android and iOS. Many existing Flutter plugins do not support background location tracking on Android any longer: as of February 2020, Google has limited the background location tracking for Android apps to apps that run a background service.

It is necessary to convert the location stream using some Location Data Transfer Objects to a Stream of LocationSamples . This is done via the map() function, where the latitude and longitude are extracted from the DTO,and are used to construct a LocationSample

dtoStream.map((e) => LocationSample(GeoLocation(e.latitude, e.longitude), DateTime.now()));

Example usage

Below is shown an example of how to set this up, using the CARP Background Location plugin for location tracking.

All features are implemented as getters for a MobilityContext object, meaning we could access the fields rather than just printing the object in the onMobilityContext method.

/// Location features
context.places;
context.stops;
context.moves;
/// Derived features
context.numberOfSignificantPlaces;
context.homeStay;
context.entropy;
context.normalizedEntropy;
context.distanceTravelled;

Example App

The example app demonstrates how an application can subscribe to feature updates, and thereby update each time a new set of features is computed.

The source code for the example app is available at our Github repository.

Handling missing data

When data is missing and a feature cannot be evaluated, the value of a given feature will often be -1.

Examples:

  • The Homestay feature requires data to be collected between 00:00 and 06:00, otherwise, the feature cannot be evaluated and will result in a Homestay of -1.
  • The Entropy and Normalized Entropy features require at least 2 places to be evaluated. If only a single place was found, this will result in an Entropy of 0, and if no places were found, the result is -1.

Architecture

The package uses the MobilityFactory (a singleton object) to organize data and compute features.

Features are computed using the observer pattern, where the MobilityFactory is the subject, and a mobile app using the package is the observer.

The subject exposes a stream object which can be subscribed to by observers. Every time a MobiltyContext object is pushed on to the stream, all observers are notified.

Mobility Factory is itself also an observer, listening to a separate Flutter plugin that provides a stream of GPS data: Each time a new GPS data point is available, the Mobility Factory instance is notified.

Computing features

Features are computed using a distance-grouping trigger, as described by Cuttone et. al 2014. The algorithm uses a list of collected GPS data points that have not yet been used for feature computation, this is referred to as a cluster.

Each time a new data point comes in the distance between the median location of the cluster and the new data point is measured. If this distance is greater than the stop-radius parameter, then the cluster is converted into a stop. If the stop lasted longer than the stop-duration parameter, then the stop is added to our stops today, and the features are computed:

  • Places are computed by using the DBSCAN algorithm by Ester et al. 1996 on the sequence of stops — in doing so, stops are assigned a label that indicates which place they each belong to.
  • Stops are merged based on their place label — this helps to smooth out gaps in the data, which are especially prevalent during the night where location tracking is throttled.
  • Moves are computed from the merged stops, as well as the set collected GPS points today. Each move is computed using the path of GPS points between each pair of stops in the stop-sequence.
  • Lastly, all the remaining features are derived from the stops, places, and moves, and are defined according to Canzian et al 2015 and Saeb et al 2015.

The algorithm is given below in pseudo-python code:

on_receive(point)
points.add(point)
if (point.distance(cluster) > stop_distance):
stop = consume(cluster)
if (stop.duration > stop_duration):
stops.add(stop)
stops, places = compute_places(stops)
stops = merge_on_place_id(stop)
moves = compute_moves(stops, points)
context = compute_features(stops, places, moves)
notify_observers(context)
cluster = {}
else:
cluster.add(point)

References

Canzian et al 2015
Luca Canzian and Mirco Musolesi. “Trajectories of Depression: Unobtrusive Monitoring of Depressive States by Means of Smartphone Mobility Traces Analysis”. In: Proceedings of the 2015 ACM International Joint Conference on Pervasive and Ubiquitous Computing. UbiComp ’15. Os- aka, Japan: Association for Computing Machinery, 2015, pages 1293– 1304. isbn: 9781450335744. doi: 10.1145/2750858.2805845.
URL: https://doi.org/10.1145/2750858.2805845.

Saeb et al 2015
Sohrab Saeb et al. “The Relationship between Clinical, Momentary, and Sensor-based Assessment of Depression”. In: International Conference on
Pervasive Computing Technologies for Healthcare : [proceedings]. Inter-
National Conference on Pervasive Computing Technologies for Healthcare
2015 (August 2015). PMC4667797[pmcid], 10.4108/icst.pervasivehealth.2015.259034. issn: 2153–1633. doi: 10.4108/icst.pervasivehealth.2015.259034.
url:
https://www.ncbi.nlm.nih.gov/pubmed/26640739.

Cuttone et al 2014
Andrea Cuttone, Sune Lehmann, and Jakob Eg Larsen. “Inferring Human Mobility from Sparse Low Accuracy Mobile Sensing Data”. In: Proceedings of the 2014 ACM International Joint Conference on Pervasive and Ubiquitous Computing: Adjunct Publication. UbiComp ’14 Adjunct. Seattle, Washington: Association for Computing Machinery, 2014, pages 995–1004. isbn: 9781450330473. doi: 10.1145/2638728.2641283. url: https://doi.org/10.1145/2638728.2641283.

Ester et al 1996
Martin Ester et al. “A Density-Based Algorithm for Discovering Clusters a Density-Based Algorithm for Discovering Clusters in Large Spatial Databases with Noise”. In: Proceedings of the Second International Conference on Knowledge Discovery and Data Mining. KDD’96. Portland, Oregon: AAAI Press, 1996, pages 226–231.

--

--