Skip to content

Commit

Permalink
shared code for improved formatting of numbers in Jupyter notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Goetz committed Jan 31, 2022
1 parent b37522b commit 431228c
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 31 deletions.
9 changes: 9 additions & 0 deletions Jupyter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Python code for Jupyter notebooks."""

__author__ = "Tom Goetz"
__copyright__ = "Copyright Tom Goetz"
__license__ = "GPL"

# flake8: noqa

from .jupyter_funcs import format_number
12 changes: 9 additions & 3 deletions Jupyter/activities.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"from garmindb.garmindb import GarminDb, Attributes, ActivitiesDb, Activities, StepsActivities, ActivityLaps, ActivityRecords\n",
"from idbutils.list_and_dict import list_not_none\n",
"\n",
"from jupyter_funcs import format_number\n",
"\n",
"\n",
"db_params_dict = ConfigManager.get_db_params()\n",
"\n",
Expand All @@ -40,7 +42,7 @@
" average_distance = 0\n",
" else:\n",
" average_distance = total_distance / records\n",
" return [sport_title, records, total_distance, average_distance]\n",
" return [sport_title, records, format_number(total_distance, 1), format_number(average_distance, 1)]\n",
"\n",
"doc = snakemd.new_doc(\"activities\")\n",
"\n",
Expand Down Expand Up @@ -77,8 +79,8 @@
" if activity:\n",
" if activity.is_steps_activity():\n",
" steps_activity = StepsActivities.get(garmin_act_db, activity.activity_id)\n",
" return [activity.activity_id, activity.name, activity.type, activity.sport, activity.distance, activity.elapsed_time, activity.avg_speed, steps_activity.avg_pace, activity.calories]\n",
" return [activity.activity_id, activity.name, activity.type, activity.sport, activity.distance, activity.elapsed_time, activity.avg_speed, '', activity.calories]\n",
" return [activity.activity_id, activity.name, activity.type, activity.sport, format_number(activity.distance, 1), activity.elapsed_time, format_number(activity.avg_speed, 1), steps_activity.avg_pace, format_number(activity.calories)]\n",
" return [activity.activity_id, activity.name, activity.type, activity.sport, format_number(activity.distance, 1), activity.elapsed_time, format_number(activity.avg_speed, 1), '', format_number(activity.calories)]\n",
" return ['', '', '', '', '', '', '', '', '']\n",
"\n",
"activities = Activities.get_latest(garmin_act_db, 10)\n",
Expand Down Expand Up @@ -106,6 +108,10 @@
" ['Longest Paddle'] + __format_activity(Activities.get_longest_by_sport(garmin_act_db, fitfile.Sport.paddling))\n",
"])\n",
"\n",
"doc.add_header(\"Courses\", 3)\n",
"courses = Activities.get_col_distinct(garmin_act_db, Activities.course_id)\n",
"doc.add_paragraph(str(courses))\n",
"\n",
"display(Markdown(str(doc)))"
]
}
Expand Down
11 changes: 7 additions & 4 deletions Jupyter/activity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"from garmindb.garmindb import GarminDb, Attributes, ActivitiesDb, Activities, ActivityLaps\n",
"from idbutils import Location\n",
"\n",
"from jupyter_funcs import format_number\n",
"\n",
"\n",
"activity_id = input('Enter the id of the activity you would like to display')\n",
"\n",
"doc = snakemd.new_doc(\"activity\")\n",
Expand Down Expand Up @@ -49,9 +52,9 @@
" \"Calories\", f\"Avg Temp (deg {temp_units})\", 'Start Location', 'End Location'\n",
" ],\n",
" [\n",
" [activity_id, activity.sport, activity.type, activity.distance, activity.elapsed_time, activity.moving_time, activity.avg_hr, activity.max_hr,\n",
" [activity_id, activity.sport, activity.type, format_number(activity.distance, 1), activity.elapsed_time, activity.moving_time, activity.avg_hr, activity.max_hr,\n",
" activity.avg_rr if activity.avg_rr else '', activity.max_rr if activity.max_rr else '',\n",
" round(activity.ascent), round(activity.descent), activity.calories, activity.avg_temperature, snakemd.InlineText(str(start_loc), start_loc.to_google_maps_url()),\n",
" format_number(activity.ascent), format_number(activity.descent), activity.calories, format_number(activity.avg_temperature, 1), snakemd.InlineText(str(start_loc), start_loc.to_google_maps_url()),\n",
" snakemd.InlineText(str(end_loc), end_loc.to_google_maps_url())]\n",
" ]\n",
")\n",
Expand All @@ -64,8 +67,8 @@
" end_loc = Location(lap.stop_lat , lap.stop_long)\n",
" lap_stats.append(\n",
" [\n",
" lap.lap, lap.distance, lap.elapsed_time, lap.moving_time, lap.avg_hr, lap.max_hr, lap.avg_rr if lap.avg_rr else '', lap.max_rr if lap.max_rr else '',\n",
" round(lap.ascent), round(lap.descent), lap.calories, lap.avg_temperature,\n",
" lap.lap, format_number(lap.distance, 1), lap.elapsed_time, lap.moving_time, lap.avg_hr, lap.max_hr, lap.avg_rr if lap.avg_rr else '', lap.max_rr if lap.max_rr else '',\n",
" format_number(lap.ascent), format_number(lap.descent), lap.calories, format_number(lap.avg_temperature, 1),\n",
" snakemd.InlineText(str(start_loc), start_loc.to_google_maps_url()), snakemd.InlineText(str(end_loc), end_loc.to_google_maps_url())\n",
" ]\n",
" )\n",
Expand Down
7 changes: 5 additions & 2 deletions Jupyter/course.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"from garmindb import ConfigManager\n",
"from garmindb.garmindb import GarminDb, Attributes, ActivitiesDb, Activities, ActivityLaps, ActivityRecords, StepsActivities\n",
"\n",
"from jupyter_funcs import format_number\n",
"\n",
"\n",
"doc = snakemd.new_doc(\"checkup\")\n",
"\n",
"course_id = input('Enter the id of a course to summarize')\n",
Expand All @@ -46,8 +49,8 @@
"def __activity_data(activity, title):\n",
" if activity.is_steps_activity():\n",
" steps_activity = StepsActivities.get(activity_db, activity.activity_id)\n",
" return [title, activity.start_time, activity.activity_id, activity.name, activity.distance, activity.elapsed_time, steps_activity.avg_pace, activity.avg_speed]\n",
" return [title, activity.start_time, activity.activity_id, activity.name, activity.distance, activity.elapsed_time, '', activity.avg_speed]\n",
" return [title, activity.start_time, activity.activity_id, activity.name, format_number(activity.distance), activity.elapsed_time, steps_activity.avg_pace, format_number(activity.avg_speed)]\n",
" return [title, activity.start_time, activity.activity_id, activity.name, format_number(activity.distance), activity.elapsed_time, '', format_number(activity.avg_speed)]\n",
"\n",
"doc.add_table(\n",
" ['Type', 'On', 'Id', 'Name', f'Distance ({unit_strings[fitfile.units.UnitTypes.distance_long]})', 'Time', f'Pace ({unit_strings[fitfile.units.UnitTypes.pace]})', f'Speed ({unit_strings[fitfile.units.UnitTypes.speed]})'],\n",
Expand Down
21 changes: 16 additions & 5 deletions Jupyter/daily.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@
"from garmindb import ConfigManager, Graph\n",
"from garmindb.garmindb import GarminSummaryDb, DaysSummary\n",
"\n",
"from jupyter_funcs import format_number\n",
"\n",
"\n",
"def __render_day(garmin_sum_db, graph, date):\n",
" doc = snakemd.new_doc(\"garmin\")\n",
" day = DaysSummary.get_day(garmin_sum_db, date)\n",
" if day:\n",
" doc.add_header(f\"{date} Summary\")\n",
" doc.add_table(['Weight', 'Resting HR', 'Max HR', 'Waking Avg RR', 'Steps', 'Intensity Mins', 'Calories', 'Sleep', 'REM Sleep', 'Stress'],\n",
" doc.add_header(f\"Summary of {date}\")\n",
" doc.add_table(['Weight', 'Resting HR', 'Max HR', 'Waking Avg RR', 'Steps', 'Floors', 'Intensity Mins', 'Calories', 'Sleep', 'REM Sleep', 'Stress'],\n",
" [\n",
" [round(day.weight_avg) if day.weight_avg else '', day.rhr_avg, day.hr_max, day.rr_waking_avg, day.steps, day.intensity_time, day.calories_avg, day.sleep_avg, day.rem_sleep_avg, day.stress_avg]\n",
" [format_number(day.weight_avg), day.rhr_avg, day.hr_max, day.rr_waking_avg, day.steps, format_number(day.floors, 1),\n",
" day.intensity_time, day.calories_avg, day.sleep_avg, day.rem_sleep_avg, day.stress_avg]\n",
" ]\n",
" )\n",
" display(Markdown(str(doc)))\n",
Expand All @@ -38,7 +41,15 @@
"db_params_dict = ConfigManager.get_db_params()\n",
"garmin_sum_db = GarminSummaryDb(db_params_dict)\n",
"\n",
"graph = Graph()\n",
"graph = Graph()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"__render_day(garmin_sum_db, graph, datetime.date.today() - datetime.timedelta(days = 1))\n"
]
Expand All @@ -49,7 +60,7 @@
"metadata": {},
"outputs": [],
"source": [
"__render_day(garmin_sum_db, graph, datetime.date.today() - datetime.timedelta(days = 2))\n"
"__render_day(garmin_sum_db, graph, datetime.date.today() - datetime.timedelta(days = 2))"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion Jupyter/garmin.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"doc = snakemd.new_doc(\"garmin\")\n",
"\n",
"doc.add_header(\"GarminDb Statistics\")\n",
"doc.add_paragraph(\"Analysis of all monitoring data in the database.\")\n",
"doc.add_paragraph(\"Metadata for data in the database.\")\n",
"\n",
"file_stats = [\n",
" ['All', File.row_count(garmin_db)]\n",
Expand Down
12 changes: 12 additions & 0 deletions Jupyter/jupyter_funcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Utility functions for Jupyter notebooks."""

__author__ = "Tom Goetz"
__copyright__ = "Copyright Tom Goetz"
__license__ = "GPL"


def format_number(number, digits=0):
"""Format a number for display."""
if number is not None:
return round(number, digits)
return ''
1 change: 1 addition & 0 deletions Jupyter/monitoring.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"doc.add_paragraph(f\"Monitoring Years with data ({len(years)}): {years}\")\n",
"total_days = 0\n",
"for year in years:\n",
" doc.add_header(str(year), 4)\n",
" __get_monitoring_months(year)\n",
" total_days += __get_monitoring_days(year)\n",
"doc.add_paragraph(f\"Total days with monitoring data: {total_days}\")\n",
Expand Down
35 changes: 19 additions & 16 deletions Jupyter/summary.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Weekly Summary Graphs"
"# Summary Graphs\n",
"Graphs of data across multiple years sampled weekly."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import garmindb\n",
"\n",
"years_to_display = 4\n",
"days_to_display = (years_to_display * 365)\n",
"\n",
"graph = garmindb.Graph()"
]
},
{
Expand All @@ -20,10 +35,7 @@
"metadata": {},
"outputs": [],
"source": [
"import garmindb\n",
"\n",
"graph = garmindb.Graph()\n",
"graph.graph_activity('hr', days=(365*4))"
"graph.graph_activity('hr', days=days_to_display)"
]
},
{
Expand All @@ -39,10 +51,7 @@
"metadata": {},
"outputs": [],
"source": [
"import garmindb\n",
"\n",
"graph = garmindb.Graph()\n",
"graph.graph_activity('itime', days=(365*4))"
"graph.graph_activity('itime', days=days_to_display)"
]
},
{
Expand All @@ -58,9 +67,6 @@
"metadata": {},
"outputs": [],
"source": [
"import garmindb\n",
"\n",
"graph = garmindb.Graph()\n",
"graph.graph_activity('steps')"
]
},
Expand All @@ -77,10 +83,7 @@
"metadata": {},
"outputs": [],
"source": [
"import garmindb\n",
"\n",
"graph = garmindb.Graph()\n",
"graph.graph_activity('weight', days=(365*4))"
"graph.graph_activity('weight', days=days_to_display)"
]
}
],
Expand Down

0 comments on commit 431228c

Please sign in to comment.