Skip to content

Commit

Permalink
Start with mhealth geolocation reader
Browse files Browse the repository at this point in the history
  • Loading branch information
rantahar committed Dec 1, 2023
1 parent ddf6a61 commit 924172d
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
5 changes: 3 additions & 2 deletions niimpy/config/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ studentlife_activity = %(sample_data)s/sl_activity.csv
sqlite_singleuser= %(sample_data)s/singleuser.sqlite3
sqlite_multiuser= %(sample_data)s/multiuser.sqlite3

mhealth_total_sleep_time= %(sample_data)s/mhealth_total_sleep_time.json
mhealth_heart_rate= %(sample_data)s/mhealth_heart_rate.json
mhealth_total_sleep_time = %(sample_data)s/mhealth_total_sleep_time.json
mhealth_heart_rate = %(sample_data)s/mhealth_heart_rate.json
mhealth_geolocation = %(sample_data)s/mhealth_geolocation.json

[plotly_settings]

Expand Down
1 change: 1 addition & 0 deletions niimpy/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
SQLITE_MULTIUSER_PATH =os.path.join(ROOT, config['sample_paths']['sqlite_multiuser'])
MHEALTH_TOTAL_SLEEP_TIME_PATH = os.path.join(ROOT, config['sample_paths']['mhealth_total_sleep_time'])
MHEALTH_HEART_RATE_PATH = os.path.join(ROOT, config['sample_paths']['mhealth_heart_rate'])
MHEALTH_GEOLOCATION_PATH = os.path.join(ROOT, config['sample_paths']['mhealth_geolocation'])
54 changes: 54 additions & 0 deletions niimpy/reading/mhealth.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def heart_rate(data_list):
Returns
-------
data: A pandas.DataFrame containing geolocation data
'''
df = pd.json_normalize(data_list)

Expand Down Expand Up @@ -325,3 +326,56 @@ def heart_rate_from_file(filename):
df = heart_rate(data)

return df



def geolocation(data_list):
''' Format the geolocation json data into a niimpy dataframe.
Parameters
----------
data_list: list of dictionaries
MHealth formatted geolocation data loaded using json.load().
Returns
-------
data: A pandas.DataFrame containing geolocation data
'''
df = pd.json_normalize(data_list)

# Keep rows where latitude and longitude are given correctly
df = df[df["latitude.unit"] == "deg"]
df = df[df["longitude.unit"] == "deg"]
df.rename(columns={
"latitude.value": "latitude",
"longitude.value": "longitude",
}, inplace=True)

return df


def geolocation_from_file(filename):
'''Read mHealth formatted geolocation data from a file and convert it to
a Niimpy compatible dataframe.
Parameters
----------
filename: string
Path to the file containing mhealth formatted geolocation data.
Returns
-------
data: A pandas.DataFrame containing geolocation data
'''

with open(filename) as f:
data = json.load(f)

df = geolocation(data)

return df

40 changes: 40 additions & 0 deletions niimpy/sampledata/mhealth_geolocation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[
{
"latitude": {
"value": 60.1867,
"unit": "deg"
},
"longitude": {
"value": 24.8283,
"unit": "deg"
},
"effective_time_frame": {
"time_interval": {
"start_date_time": "2016-02-05T20:35:00-08:00",
"end_date_time": "2016-02-06T06:35:00-08:00"
}
},
"positioning_system": "GPS"
},
{
"latitude": {
"value": 60.1867,
"unit": "deg"
},
"longitude": {
"value": 24.8283,
"unit": "deg"
},
"elevation": {
"value": 20.4,
"unit": "m"
},
"effective_time_frame": {
"time_interval": {
"start_date_time": "2016-02-05T20:35:00-08:00",
"end_date_time": "2016-02-06T06:35:00-08:00"
}
},
"positioning_system": "GPS"
}
]
10 changes: 8 additions & 2 deletions tests/reading/test_read_mhealth.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def test_format_time_interval():


def test_read_mhealth_total_sleep_time():
"""test reading mixed mhealth data from the example file."""
data = niimpy.reading.mhealth.total_sleep_time_from_file(config.MHEALTH_TOTAL_SLEEP_TIME_PATH)
assert data['total_sleep_time'][0] == pd.Timedelta(465, unit="minutes")
assert data['descriptive_statistic'][1] == "average"
Expand All @@ -81,7 +80,6 @@ def test_read_mhealth_total_sleep_time():


def test_read_mhealth_heart_rate():
"""test reading mixed mhealth data from the example file."""
data = niimpy.reading.mhealth.heart_rate_from_file(config.MHEALTH_HEART_RATE_PATH)
assert data['heart_rate'][0] == 70
assert data['heart_rate'][1] == 65
Expand All @@ -91,3 +89,11 @@ def test_read_mhealth_heart_rate():
assert data['start'][2] == pd.to_datetime("2023-12-20T01:50:00-02:00")


def test_read_mhealth_geolocation():
data = niimpy.reading.mhealth.geolocation_from_file(config.MHEALTH_GEOLOCATION_PATH)

assert data['latitude'][0] == 60.1867
assert data['longitude'][0] == 24.8283
assert data['latitude'][1] == 60.1867
assert data['longitude'][1] == 24.8283

0 comments on commit 924172d

Please sign in to comment.