[PYTHON] Looking back on iOS'Healthcare App' 2019

This article is the 9th day of the DoCoMo Advent Calendar.

This is Yamamoto from DoCoMo. In my work, I am engaged in research and development related to healthcare. In this article, we will introduce how to utilize the data acquired by the iPhone Healthcare App.

■ Introduction

□ Target person

・ Interested in healthcare using mobile wearable devices ・ I want to see the data of the iOS healthcare app ・ Those who have reached this article

□ What to do

・ How to extract logs collected by iOS "Healthcare App" ・ Introduction of extracted logs ・ Looking back on the Healthcare App log 2019

■ What is iOS "Healthcare App"?

The Healthcare App automatically counts your steps, walking distance and mileage. Plus, if you have an Apple Watch, your Apple Watch will automatically record your activity data for you. You can also enter information about your healthcare category and retrieve data from your everyday apps and devices that support healthcare apps. Reference: Official website (https://www.apple.com/jp/ios/health/)

In short, it is an app that can be used for confirmation and self-care by collecting and aggregating various healthcare data. If you are using an iPhone, you can start using it by tapping the Healthcare App icon as shown in the figure below and completing the initial settings quickly. If you start the Healthcare App and tap Sleep Analysis, for example, the sleep time acquired in "Clock App> Bedtime" will be displayed on a weekly or monthly basis. By tapping "Show all healthcare data", you can also check data such as headphone volume, activity, and heart rate.

health_app.png

■ Output the data collected by "Healthcare App"

There is a humanoid icon in the upper right corner of the Healthcare App screen. If you tap here, the page will change to the one shown on the left, so scroll the screen to the bottom. Then, it says "Export all health care data", so tap here. It depends on the amount of data, but it takes a little time (I took a few minutes to a few tens of minutes) to write it out. If you move the exported zip file to your PC and check the contents,

There are two types of .xml files, and various healthcare data are stored in "export.xml".

out_xml.png

■ Check the data of "Healthcare App"

When you open export.xml with an editor, the data is as follows.

type ='hoge' is the type of data acquired (eg step count data or heart rate data), startDate ='hoge' is the acquisition start date and time (UTC), endDate ='hoge' is the acquisition end date and time (UTC), value = It seems that the measured values (for example, the number of steps taken and the measured heart rate) are stored in'hoge'.

<HealthData locale="ja_JP">
 <ExportDate value="2019-11-15 12:51:14 +0900"/>
 <Me HKCharacteristicTypeIdentifierDateOfBirth="***" HKCharacteristicTypeIdentifierBiologicalSex="HKBiologicalSexMale" HKCharacteristicTypeIdentifierBloodType="HKBloodTypeNotSet" HKCharacteristicTypeIdentifierFitzpatrickSkinType="HKFitzpatrickSkinTypeNotSet"/>
 <Record type="HKQuantityTypeIdentifierHeight" sourceName="Healthcare" sourceVersion="10.0.1" unit="cm" creationDate="2016-09-22 09:57:08 +0900" startDate="2016-09-22 09:57:08 +0900" endDate="2016-09-22 09:57:08 +0900" value="***"/>
 <Record type="HKQuantityTypeIdentifierBodyMass" sourceName="Healthcare" sourceVersion="10.0.1" unit="kg" creationDate="2016-09-22 09:57:08 +0900" startDate="2016-09-22 09:57:08 +0900" endDate="2016-09-22 09:57:08 +0900" value="***"/>

Since it is difficult to handle as it is, create a DataFrame.

import pandas as pd
from dateutil.parser import parse
import argparse
import datetime as dt

parser = argparse.ArgumentParser()
columns = ['type', 'unit', 'startDate', 'endDate', 'value']
parsed = objectify.parse(open('export.xml'))
root = parsed.getroot()
data = []
for r in root.Record:
    data.append({c: t for c, t in r.attrib.items() if c in columns})
health_df = pd.DataFrame(data)

###Coordinated Universal Time(UTC)➡︎ Japan Standard Time(JST)Conversion to
health_df.index = pd.to_datetime(health_df['startDate'], utc=True)
health_df['startDate'] = health_df.index.tz_convert('Asia/Tokyo')
health_df.index = pd.to_datetime(health_df['endDate'], utc=True)
health_df['endDate'] = health_df.index.tz_convert('Asia/Tokyo')

###I want to aggregate by day, so create a column of date
health_df['startDate_date'] = health_df['startDate'].dt.date
health_df['endDate_date'] = health_df['endDate'].dt.date

health_df = health_df.reset_index(drop=True)

Here is the created DataFrame. value stores the actually measured value.
dataframe.png

By focusing on the type, you can limit it to the life log you want to see. Please refer to the official (https://developer.apple.com/documentation/healthkit/data_types?language=objc) for the data of each.

■ Looking back based on life logs-2019

The Healthcare App also presents various analysis results, but let's analyze and check your own life log. In this article, we will limit ourselves to step count data and distance traveled data.

(There was data for 1339 days from March 16, 2016 to November 15, 2019 ... !!!)

###Steps###
df_StepCount = health_df.loc[health_df['type']=='HKQuantityTypeIdentifierStepCount',:].reset_index(drop=True)
df_StepCount = df_StepCount.sort_values(['startDate_date'])

###Walking / running distance###
df_DistanceWalkingRunning = health_df.loc[health_df['type']=='HKQuantityTypeIdentifierDistanceWalkingRunning',:].reset_index(drop=True)
df_DistanceWalkingRunning = df_DistanceWalkingRunning.sort_values(['startDate_date'])


###Aggregate on a daily basis###
df_StepCount_date = df_StepCount.loc[:,['startDate_date', 'value']].groupby(['startDate_date']).sum()
df_StepCount_date = df_StepCount_date.rename(columns={'value': 'StepCount'}) 

df_DistanceWalkingRunning_date = df_DistanceWalkingRunning.loc[:,['startDate_date', 'value']].groupby(['startDate_date']).sum()
df_DistanceWalkingRunning_date = df_DistanceWalkingRunning_date.rename(columns={'value': 'DistanceWalkingRunning'}) 

First, let's visualize it.

[Upper] Left figure: Travel distance (* x * axis: date, * y * axis: km), Right figure: Steps (* x * axis: date, * y * axis: steps) [Middle] Left figure: Travel distance (* x * axis: km, * y * axis: frequency), Right figure: Steps (* x * axis: steps, * y * axis: frequency) [Bottom] Left table: Statistic of distance traveled, right figure: Statistic of steps

distance_walk.png

When you visualize it, you may notice something unexpected.

・ The number of steps (distance traveled) during the year-end and New Year holidays is small every year. ➡︎ Is it because you live a crazy life at home? I think I'll be active for the New Year in 2020 for two years.

・ July-October is an active time when the number of steps (distance traveled) changes drastically ➡︎, such as traveling and going to music festivals‼ ︎

・ In 2019, there will be several days when the number of steps is close to 0. ➡︎ If you are tired, of course you will spend your time at home. Rather, was I so active in the past?

・ As the days of 2017 → 2018 → 2019 pass, the number of steps (distance traveled) tends to decrease ➡︎ This is surprising. It is 1,600 steps less than two years ago ... Is it the effect of changes in the living environment? The fact that the workplace moved to Tokyo in early 2018 may also have an effect. I'll dig a little deeper later.

・ The total travel distance as of November 15, 2019 (limited to 2019) 1603km is almost the same as the 1521km foot race from Aomori to Shimonoseki, which runs through Honshu. The record for the first place in the history of this tournament is 5 hours 38 minutes on the 18th of 2017, so it means that there is an iron man who can finish running in 3 weeks on the way I walked for 10 months ...

・ The total travel distance of 7081km for 4 years is the distance that can cross the Americas (San Francisco-Los Angeles-Las Vegas-Miami-New York-Boston, total travel distance of about 7533km). Students who are thinking of crossing the United States on their graduation trip, please refer to it lol By the way, when crossing the Pacific Ocean, the distance between Tokyo and San Francisco is about 8800km, so I have not yet reached the United States with my travel distance.

I was surprised that the number of steps (distance traveled) is decreasing year by year, so I will dig a little deeper. The result of plotting the number of steps [steps / day] of each year for each day of the week is as follows. [Upper left: 2016, upper right: 2017, lower left: 2018, lower right: 2019]

step_year.png

From 2017 to 2019, you can see the decrease in the number of steps, especially on Saturdays and Sundays.

[Upper] Left figure: Saturday steps, Right figure: Sunday steps [Bottom] Left table: Saturday step count comparison (p-value), Right table: Sunday step count comparison (p-value)

sat_sun.png

The major factor in the decrease in the number of steps seems to be how to spend Saturdays and Sundays. When I work, I have a regular life on weekdays, and it seems that there is not much change over time. On the other hand, on days when you can spend your day freely, such as Saturdays and Sundays, you can live an extreme life, such as not going out at all. (Personally, staying at home is rather stressful, so I go out on Saturdays and Sundays.) It may be important to review my life from the viewpoint of health care on such a free day. ..

If you visualize the life log in this way and look at it objectively, you can see that it was possible before, but now it is not. In self-care in health management, first of all, you should objectively re-examine your current situation and look back on your life, such as why the number of steps decreased every year and how you spent your days on Saturdays and Sundays. I think that is important. By paying a little attention to my health condition, I suddenly thought, "Let's take a detour from the station today and walk home" or "Let's stretch and relax before going to bed". I think that will lead to behavior change.

■ Summary

A variety of healthcare apps are provided on the Google Store and App Store, and it is becoming a world where individuals can easily record life logs and use them for self-care. Why don't you analyze your life log as a first step to look at your health condition, not just to record your life log? There may be unexpected discoveries.

Thank you for staying with us until the end. May 2020 be a healthy year as well.

Recommended Posts

Looking back on iOS'Healthcare App' 2019
Looking back on ABC155
Looking back on Python 2020 around me
Looking back on 2016 in the Crystal language
Looking back on the data M-1 Grand Prix 2020
Looking back on learning with Azure Machine Learning Studio
Looking back on creating a web service with Django 1
Looking back on the transition of the Qiita Advent calendar
Looking back on creating a web service with Django 2
Deploy masonite app on Heroku 2020
[Python] Looking back on what I taught programming beginners from functions
Implement a Django app on Hy
Detect app releases on the App Store
Deploy Flask app on heroku (bitterly)
Deploy the Flask app on Heroku
Use ndb.tasklet on Google App Engine
Deploy the Flask app on heroku
[Spotify API] Looking back on 2020 with playlists --Part.1 Acquisition of playlist data
Looking back on the 10 months before a programming beginner becomes a Kaggle Expert
[Spotify] Looking back on 2020 with playlists --Part.2 EDA (basic statistics), data preprocessing