@@ 185,6 185,21 @@ def estimate_energy(distance: float,
return kcals
+# Thresholds for classifying activities based solely on average moving speed:
+AVG_SPEED_RIDE_THRESHOLD = 20 / 3.6
+AVG_SPEED_RUN_THRESHOLD = 6 / 3.6
+
+def guess_type(analysis) -> str:
+ try:
+ avg_speed = analysis['distance'] / analysis['moving_seconds']
+ if avg_speed >= AVG_SPEED_RIDE_THRESHOLD:
+ return 'Ride'
+ if avg_speed >= AVG_SPEED_RUN_THRESHOLD:
+ return 'Run'
+ return 'Walk'
+ except:
+ return 'Exercise'
+
def analyse(track: Track, athlete_data: Mapping[str, float], raw: bool = False):
pos = track.positions
raw_hr = track.hr
@@ 9,7 9,7 @@ from django.contrib.auth.models import User
from django import forms
from .models import Activity, ActivityUpload, HealthData
-from .activity import analyse, from_fit_bytes, from_gpx_bytes
+from .activity import analyse, from_fit_bytes, from_gpx_bytes, guess_type
import datetime
import gzip
@@ 35,7 35,7 @@ def create_activity(user: User, title: str, data: bytes):
user=user,
start_date=datetime.datetime.now(),
activity_title=title,
- activity_type='Run',
+ activity_type='Unknown',
orig_upload=ul,
)
@@ 64,6 64,19 @@ def get_user_healthdata(userId: int, type: str, time: datetime.datetime):
return None
+def name_time(t: datetime.datetime) -> str:
+ """
+ Return the English word to approximately describe in which part of the day
+ the provided time object falls.
+ """
+ if 5 <= t.hour < 12:
+ return 'Morning'
+ if 12 <= t.hour < 17:
+ return 'Afternoon'
+ if 17 <= t.hour < 21:
+ return 'Evening'
+ return 'Night'
+
def process_activity(act: Activity):
logger.debug('begin processing activity %d', act.id)
begin = time.monotonic()
@@ 111,6 124,13 @@ def process_activity(act: Activity):
act.moving_time = analysis['moving_seconds']
act.start_date = analysis['start_date']
+ if act.activity_type == 'Unknown':
+ act.activity_type = guess_type(analysis)
+
+ if act.activity_title in (None, ''):
+ part_of_day = name_time(analysis['start_date'])
+ act.activity_title = f'{part_of_day} {act.activity_type}'
+
# copy the dictionary, so we can remove unwanted entries:
del analysis['start_date']
act.analysis = analysis