From the phone’s sensors to clinical indicators: turning raw, sparse time series into reliable indicators for tracking depression
Thousands of raw measurements a day, a few indicators you can trust in the morning.
A phone is not a measuring instrument. It switches off, stays in a bag, loses the network, changes time zone, and the system puts it to sleep whenever it can. The data received is raw and incomplete: mood estimates when measurement is possible, step counts, position data, all at irregular hours. Yet EmoDTx has to draw from it, for each patient, indicators of mood, physical activity and routine sound enough to be shown to a psychiatrist.
As a co-founder of Emobot, I designed this chain with the team and built a large part of it. The difficulty is not in any one computation: every link has to hold when the one before gives way. On the phone, capture is broken up to spare the battery and the heat: audio while the phone is still, GPS at regular intervals, physical activity at the end of the day; and measurements stay queued when the network is missing. The server takes up the last few days again every night, so a measurement that arrives late always ends up in its place.
At the end, the chain also knows when to say nothing: when a week does not hold enough measurements, it answers “insufficient data” rather than a figure.
The result
What was done
01
Measuring on the phone
Passive measuring that spares the battery: short windows, whose pace follows the phone’s movement, the hour and its temperature.
Swift · Kotlin
02
Sending
A queue on the phone, protected by the device’s encryption, that survives network cuts and the system’s sleep, with no loss and no duplicate.
Swift · Kotlin
03
The scheduled jobs
Jobs that run by themselves, hour after hour, time zone by time zone, and take up the last few days again at every pass.
Python · APScheduler
04
The mood indicators
Algorithms to sum up a day, a week and a fortnight, and to compare them with each patient’s usual by robust statistics.
pandas · NumPy
05
The routine
An algorithm to find the stops, the usual places and the home in noisy positions.
scikit-learn · MovingPandas
06
Reliability
One rule shared by every signal, to refuse to conclude when too little was measured.
Python
07
The data warehouse
Research data under pseudonyms, in complete, dated versions, so an analysis can be run again exactly.
Amazon S3 · Parquet · Athena
08
The tests
Scenarios of invented patients, replayed with every change, and architecture rules checked with every change.
pytest
How it works
Scroll: the drawing follows.
01 / 07
Measure without draining the battery
Running such demanding algorithms continuously would drain the phone’s battery and make it overheat. So capture is broken up: audio only while the phone is still, GPS at regular intervals, physical activity at the end of the day. The data is pre-processed on the phone while it charges or the battery is full, and everything pauses when it gets too hot, then resumes. A queue keeps the measurements waiting, and routines restart by themselves whatever the system has stopped.
02 / 07
Lose nothing on the way
A phone loses the network in the underground, and the system cuts off apps running in the background. So each measurement is first written on the phone, in a queue that leaves oldest first, in small batches. If sending fails, the app waits, then tries again, doubling the wait at each failure, so as not to hammer a network that is not there. A measurement is erased from the phone only once it has been received.
03 / 07
Process each day at the right hour
A day ends at midnight for the patient, not for the server. So the jobs run every hour and only handle the time zones where the day has just ended. At each pass they also take up the last few days again: a measurement that arrived late, or a missed run, is caught up with no one stepping in. Doing the same computation twice gives the same result, and an error on one patient does not stop the others.
04 / 07
Find the routine in the movements
A GPS jumps from one street to the next, even when the phone is not moving. An algorithm first separates stops from trips, then groups nearby stops into usual places, and recognizes home as the place where the nights are spent. The routine then reads simply: does the patient go out as much as usual, do they return to the same places?
05 / 07
Sum up a day, and compare it with the usual
Hundreds of mood estimates become a few numbers that can be read: the median, the quartiles, the extremes. Each patient is then compared with themselves. The usual range is computed with robust statistics, which one exceptional day does not move: that is what allows saying a day is truly out of the ordinary.
06 / 07
Say when the data is not enough
An indicator computed from three measurements misleads more than it helps. For every signal, one rule looks at what was actually measured: a day counts only if enough of its hours are covered, and a week counts only with enough days. Below that, the chain answers “insufficient data”, and everything that depends on it knows, from the curves to the AI assistant.
07 / 07
Serve the app, the portal and research
The indicators are computed once, then read by the patient’s app, the psychiatrist’s portal and the practitioners’ AI assistant. They also feed a data warehouse for research, where a pseudonym stands for each patient. A version is published there only when complete: a failure in the middle of a write never leaves half a version, and an analysis can be run again on the exact data of a past date.
Specifications
- Role
- Co-founder and chief product officer; design and development, with Emobot’s team
- Built with
- Python · pandas · scikit-learn · Swift · Kotlin