Skip to content

Commit

Permalink
added example of loading data from garmin directory in notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
bc299 committed Jan 5, 2024
1 parent 224d229 commit d20eaa8
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 15 deletions.
8 changes: 0 additions & 8 deletions dbdpy/apple_watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ def read_file(cls, filepath: str, device_name: str = "Apple Watch"):
.drop(columns="type")
.rename(columns={"value": "active_calories"})
)
steps = (
record_df[record_df["type"] == "StepCount"]
.drop(columns="type")
.rename(columns={"value": "steps"})
)
distance = (
record_df[record_df["type"] == "DistanceWalkingRunning"]
.drop(columns="type")
Expand All @@ -110,12 +105,9 @@ def read_file(cls, filepath: str, device_name: str = "Apple Watch"):
.drop(columns="type")
.rename(columns={"value": "heart_rate"})
)
sleep = record_df[record_df["type"] == "SleepAnalysis"].drop(columns="type")

return cls(
sleep=sleep,
energy=energy,
steps=steps,
distance=distance,
oxygen=oxygen,
heart_rate=heart_rate,
Expand Down
4 changes: 0 additions & 4 deletions dbdpy/commercial_device.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
class CommercialDevice:
def __init__(
self,
sleep=None,
energy=None,
steps=None,
distance=None,
oxygen=None,
heart_rate=None,
) -> None:
self.sleep = sleep
self.energy = energy
self.steps = steps
self.distance = distance
self.oxygen = oxygen
self.heart_rate = heart_rate
191 changes: 188 additions & 3 deletions notebooks/workflow_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,6 @@
"record_df[\"type\"] = record_df[\"type\"].str.replace(\"HKQuantityTypeIdentifier\", \"\")\n",
"record_df[\"type\"] = record_df[\"type\"].str.replace(\"HKCategoryTypeIdentifier\", \"\")\n",
"\n",
"# De-identify souce name? \n",
"\n",
"# Remove unnecessary columns? \n",
"\n",
"energy = record_df[record_df[\"type\"] == \"BasalEnergyBurned\"]\n",
"steps = record_df[record_df[\"type\"] == \"StepCount\"]\n",
Expand All @@ -218,6 +215,194 @@
"### Garmin"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import pandas as pd\n",
"from garmin_fit_sdk import Decoder, Stream"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"directory_path = Path(\"./data/garmin/\")\n",
"\n",
"monitoring_data = pd.DataFrame()\n",
"oxygen_data = pd.DataFrame()\n",
"for file in directory_path.rglob(\"*WELLNESS.fit\"):\n",
" stream = Stream.from_file(file)\n",
" decoder = Decoder(stream)\n",
" messages, _ = decoder.read(\n",
" apply_scale_and_offset=True,\n",
" convert_datetimes_to_dates=False,\n",
" convert_types_to_strings=True,\n",
" enable_crc_check=True,\n",
" expand_sub_fields=True,\n",
" expand_components=True,\n",
" merge_heart_rates=False,\n",
" mesg_listener=None,\n",
" )\n",
" \n",
" monitoring_df = pd.DataFrame(messages[\"monitoring_mesgs\"])[[\"timestamp\", \"distance\", \"heart_rate\", \"active_calories\"]]\n",
" try:\n",
" oxygen_df = pd.DataFrame(messages[\"spo2_data_mesgs\"])[[\"timestamp\", \"reading_spo2\"]]\n",
" except KeyError:\n",
" continue\n",
"\n",
" monitoring_data = pd.concat([monitoring_data, monitoring_df])\n",
" oxygen_data = pd.concat([oxygen_data, oxygen_df])\n",
"\n",
"# Convert the timestamp\n"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>timestamp</th>\n",
" <th>distance</th>\n",
" <th>heart_rate</th>\n",
" <th>active_calories</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.064462e+09</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1.064462e+09</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1.064462e+09</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>74.0</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>71.0</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>550</th>\n",
" <td>NaN</td>\n",
" <td>533.84</td>\n",
" <td>NaN</td>\n",
" <td>59.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>551</th>\n",
" <td>1.064242e+09</td>\n",
" <td>0.00</td>\n",
" <td>NaN</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>552</th>\n",
" <td>1.064242e+09</td>\n",
" <td>591.75</td>\n",
" <td>NaN</td>\n",
" <td>63.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>553</th>\n",
" <td>1.064242e+09</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>554</th>\n",
" <td>1.064242e+09</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>7847 rows × 4 columns</p>\n",
"</div>"
],
"text/plain": [
" timestamp distance heart_rate active_calories\n",
"0 1.064462e+09 NaN NaN NaN\n",
"1 1.064462e+09 NaN NaN NaN\n",
"2 1.064462e+09 NaN NaN NaN\n",
"3 NaN NaN 74.0 NaN\n",
"4 NaN NaN 71.0 NaN\n",
".. ... ... ... ...\n",
"550 NaN 533.84 NaN 59.0\n",
"551 1.064242e+09 0.00 NaN 1.0\n",
"552 1.064242e+09 591.75 NaN 63.0\n",
"553 1.064242e+09 NaN NaN NaN\n",
"554 1.064242e+09 NaN NaN NaN\n",
"\n",
"[7847 rows x 4 columns]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"monitoring_data"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit d20eaa8

Please sign in to comment.