From 86a97366a30e18f2e54f8026ddd75ca3eddad2f4 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Tue, 26 Jun 2018 20:22:20 +0200 Subject: [PATCH] underarmour (mapmyfitness) done --- rowers/underarmourstuff.py | 193 ++++++++++++++++++++++++++++++++++++- rowers/urls.py | 2 +- rowers/views.py | 179 ---------------------------------- 3 files changed, 191 insertions(+), 183 deletions(-) diff --git a/rowers/underarmourstuff.py b/rowers/underarmourstuff.py index cb5a479e..f0ec9db5 100644 --- a/rowers/underarmourstuff.py +++ b/rowers/underarmourstuff.py @@ -13,10 +13,15 @@ import arrow import numpy as np from dateutil import parser import time +from time import strftime +import arrow +import dataprep import math from math import sin,cos,atan2,sqrt import os,sys import urllib +import iso8601 +from uuid import uuid4 # Django from django.shortcuts import render_to_response @@ -43,6 +48,14 @@ from utils import NoTokenError,ewmovingaverage from utils import geo_distance, custom_exception_handler +def splituadata(lijst): + t = [] + y = [] + for d in lijst: + t.append(d[0]) + y.append(d[1]) + + return np.array(t),np.array(y) # Checks if user has UnderArmour token, renews them if they are expired @@ -137,7 +150,6 @@ def get_token(code): def make_authorization_url(request): # Generate a random string for the state parameter # Save it for use later to prevent xsrf attacks - from uuid import uuid4 state = str(uuid4()) params = {"client_id": UNDERARMOUR_CLIENT_KEY, @@ -170,7 +182,7 @@ def get_underarmour_workout_list(user): return s # Get workout summary data by Underarmour ID -def get_underarmour_workout(user,underarmourid): +def get_workout(user,underarmourid): r = Rower.objects.get(user=user) if (r.underarmourtoken == '') or (r.underarmourtoken is None): return custom_exception_handler(401,s) @@ -185,7 +197,14 @@ def get_underarmour_workout(user,underarmourid): url = "https://api.ua.com/v7.1/workout/"+str(underarmourid)+"/?field_set=time_series" s = requests.get(url,headers=headers) - return s + data = s.json() + + strokedata = pd.DataFrame.from_dict({ + key: pd.Series(value) for key, value in data.items() + }) + + return data,strokedata + # Create Workout Data for upload to Underarmour def createunderarmourworkoutdata(w): @@ -453,3 +472,171 @@ def workout_ua_upload(user,w): return message, uaid return message, uaid + +# Create workout from SportTracks Data, which are slightly different +# than Strava or Concept2 data +def add_workout_from_data(user,importid,data,strokedata, + source='mapmyfitness', + workoutsource='mapmyfitness'): + workouttype = 'water' + + try: + comments = data['notes'] + except: + comments = '' + + try: + thetimezone = tz(data['start_locale_timezone']) + except: + thetimezone = 'UTC' + + r = Rower.objects.get(user=user) + try: + rowdatetime = iso8601.parse_date(data['start_datetime']) + except iso8601.ParseError: + try: + rowdatetime = datetime.strptime(data['start_datetime'],"%Y-%m-%d %H:%M:%S") + rowdatetime = thetimezone.localize(rowdatetime).astimezone(utc) + except: + try: + rowdatetime = dateutil.parser.parse(data['start_datetime']) + rowdatetime = thetimezone.localize(rowdatetime).astimezone(utc) + except: + rowdatetime = datetime.strptime(data['date'],"%Y-%m-%d %H:%M:%S") + rowdatetime = thetimezone.localize(rowdatetime).astimezone(utc) + starttimeunix = arrow.get(rowdatetime).timestamp + #starttimeunix = mktime(rowdatetime.utctimetuple()) + + + try: + title = data['name'] + except: + title = "Imported data" + + timeseries = data['time_series'] + + # position, distance, speed, cadence, power, + + res = splituadata(timeseries['distance']) + + distance = res[1] + + times_distance = res[0] + + + try: + l = timeseries['position'] + + res = splituadata(l) + times_location = res[0] + latlong = res[1] + latcoord = [] + loncoord = [] + + for coord in latlong: + lat = coord['lat'] + lon = coord['lng'] + latcoord.append(lat) + loncoord.append(lon) + except: + times_location = times_distance + latcoord = np.zeros(len(times_distance)) + loncoord = np.zeros(len(times_distance)) + if workouttype in types.otwtypes: + workouttype = 'rower' + + try: + res = splituadata(timeseries['cadence']) + times_spm = res[0] + spm = res[1] + except KeyError: + times_spm = times_distance + spm = 0*times_distance + + try: + res = splituadata(timeseries['heartrate']) + hr = res[1] + times_hr = res[0] + except KeyError: + times_hr = times_distance + hr = 0*times_distance + + + # create data series and remove duplicates + distseries = pd.Series(distance,index=times_distance) + distseries = distseries.groupby(distseries.index).first() + latseries = pd.Series(latcoord,index=times_location) + latseries = latseries.groupby(latseries.index).first() + lonseries = pd.Series(loncoord,index=times_location) + lonseries = lonseries.groupby(lonseries.index).first() + spmseries = pd.Series(spm,index=times_spm) + spmseries = spmseries.groupby(spmseries.index).first() + hrseries = pd.Series(hr,index=times_hr) + hrseries = hrseries.groupby(hrseries.index).first() + + + # Create dicts and big dataframe + d = { + ' Horizontal (meters)': distseries, + ' latitude': latseries, + ' longitude': lonseries, + ' Cadence (stokes/min)': spmseries, + ' HRCur (bpm)' : hrseries, + } + + + + df = pd.DataFrame(d) + + df = df.groupby(level=0).last() + + cum_time = df.index.values + df[' ElapsedTime (sec)'] = cum_time + + velo = df[' Horizontal (meters)'].diff()/df[' ElapsedTime (sec)'].diff() + + df[' Power (watts)'] = 0.0*velo + + nr_rows = len(velo.values) + + df[' DriveLength (meters)'] = np.zeros(nr_rows) + df[' StrokeDistance (meters)'] = np.zeros(nr_rows) + df[' DriveTime (ms)'] = np.zeros(nr_rows) + df[' StrokeRecoveryTime (ms)'] = np.zeros(nr_rows) + df[' AverageDriveForce (lbs)'] = np.zeros(nr_rows) + df[' PeakDriveForce (lbs)'] = np.zeros(nr_rows) + df[' lapIdx'] = np.zeros(nr_rows) + + + + unixtime = cum_time+starttimeunix + unixtime[0] = starttimeunix + + df['TimeStamp (sec)'] = unixtime + + + dt = np.diff(cum_time).mean() + wsize = round(5./dt) + + df = df.fillna(0) + + df.sort_values(by='TimeStamp (sec)',ascending=True) + + timestr = strftime("%Y%m%d-%H%M%S") + + csvfilename ='media/{code}_{importid}.csv'.format( + importid=importid, + code = uuid4().hex[:16] + ) + + res = df.to_csv(csvfilename+'.gz',index_label='index', + compression='gzip') + + id,message = dataprep.save_workout_database(csvfilename,r, + workouttype=workouttype, + workoutsource='mapmyfitness', + title=title, + notes=comments) + + return (id,message) + diff --git a/rowers/urls.py b/rowers/urls.py index c800e2d1..2b774ab1 100644 --- a/rowers/urls.py +++ b/rowers/urls.py @@ -333,7 +333,7 @@ urlpatterns = [ url(r'^workout/runkeeperimport/$',views.workout_runkeeperimport_view), # url(r'^workout/runkeeperimport/(?P\d+)/$',views.workout_getrunkeeperworkout_view), url(r'^workout/underarmourimport/$',views.workout_underarmourimport_view), - url(r'^workout/underarmourimport/(?P\d+)/$',views.workout_getunderarmourworkout_view), +# url(r'^workout/underarmourimport/(?P\d+)/$',views.workout_getunderarmourworkout_view), url(r'^workout/(?P\d+)/deleteconfirm$',views.workout_delete_confirm_view), url(r'^workout/(?P\d+)/c2uploadw/$',views.workout_c2_upload_view), url(r'^workout/(?P\d+)/stravauploadw/$',views.workout_strava_upload_view), diff --git a/rowers/views.py b/rowers/views.py index 3b902137..66db2e87 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -845,14 +845,6 @@ def getidfromuri(uri): m = re.search('/(\w.*)\/(\d+)',uri) return m.group(2) -def splituadata(lijst): - t = [] - y = [] - for d in lijst: - t.append(d[0]) - y.append(d[1]) - - return np.array(t),np.array(y) from utils import ( @@ -1245,177 +1237,6 @@ def add_workout_from_strokedata(user,importid,data,strokedata, -# Create workout from SportTracks Data, which are slightly different -# than Strava or Concept2 data -def add_workout_from_underarmourdata(user,importid,data): - workouttype = 'water' - - try: - comments = data['notes'] - except: - comments = '' - - try: - thetimezone = tz(data['start_locale_timezone']) - except: - thetimezone = 'UTC' - - r = getrower(user) - try: - rowdatetime = iso8601.parse_date(data['start_datetime']) - except iso8601.ParseError: - try: - rowdatetime = datetime.datetime.strptime(data['start_datetime'],"%Y-%m-%d %H:%M:%S") - rowdatetime = thetimezone.localize(rowdatetime).astimezone(utc) - except: - try: - rowdatetime = dateutil.parser.parse(data['start_datetime']) - rowdatetime = thetimezone.localize(rowdatetime).astimezone(utc) - except: - rowdatetime = datetime.datetime.strptime(data['date'],"%Y-%m-%d %H:%M:%S") - rowdatetime = thetimezone.localize(rowdatetime).astimezone(utc) - starttimeunix = arrow.get(rowdatetime).timestamp - #starttimeunix = mktime(rowdatetime.utctimetuple()) - - - try: - title = data['name'] - except: - title = "Imported data" - - timeseries = data['time_series'] - - # position, distance, speed, cadence, power, - - res = splituadata(timeseries['distance']) - - distance = res[1] - - times_distance = res[0] - - - try: - l = timeseries['position'] - - res = splituadata(l) - times_location = res[0] - latlong = res[1] - latcoord = [] - loncoord = [] - - for coord in latlong: - lat = coord['lat'] - lon = coord['lng'] - latcoord.append(lat) - loncoord.append(lon) - except: - times_location = times_distance - latcoord = np.zeros(len(times_distance)) - loncoord = np.zeros(len(times_distance)) - if workouttype in types.otwtypes: - workouttype = 'rower' - - try: - res = splituadata(timeseries['cadence']) - times_spm = res[0] - spm = res[1] - except KeyError: - times_spm = times_distance - spm = 0*times_distance - - try: - res = splituadata(timeseries['heartrate']) - hr = res[1] - times_hr = res[0] - except KeyError: - times_hr = times_distance - hr = 0*times_distance - - - # create data series and remove duplicates - distseries = pd.Series(distance,index=times_distance) - distseries = distseries.groupby(distseries.index).first() - latseries = pd.Series(latcoord,index=times_location) - latseries = latseries.groupby(latseries.index).first() - lonseries = pd.Series(loncoord,index=times_location) - lonseries = lonseries.groupby(lonseries.index).first() - spmseries = pd.Series(spm,index=times_spm) - spmseries = spmseries.groupby(spmseries.index).first() - hrseries = pd.Series(hr,index=times_hr) - hrseries = hrseries.groupby(hrseries.index).first() - - - # Create dicts and big dataframe - d = { - ' Horizontal (meters)': distseries, - ' latitude': latseries, - ' longitude': lonseries, - ' Cadence (stokes/min)': spmseries, - ' HRCur (bpm)' : hrseries, - } - - - - df = pd.DataFrame(d) - - df = df.groupby(level=0).last() - - cum_time = df.index.values - df[' ElapsedTime (sec)'] = cum_time - - velo = df[' Horizontal (meters)'].diff()/df[' ElapsedTime (sec)'].diff() - - df[' Power (watts)'] = 0.0*velo - - nr_rows = len(velo.values) - - df[' DriveLength (meters)'] = np.zeros(nr_rows) - df[' StrokeDistance (meters)'] = np.zeros(nr_rows) - df[' DriveTime (ms)'] = np.zeros(nr_rows) - df[' StrokeRecoveryTime (ms)'] = np.zeros(nr_rows) - df[' AverageDriveForce (lbs)'] = np.zeros(nr_rows) - df[' PeakDriveForce (lbs)'] = np.zeros(nr_rows) - df[' lapIdx'] = np.zeros(nr_rows) - - - - unixtime = cum_time+starttimeunix - unixtime[0] = starttimeunix - - df['TimeStamp (sec)'] = unixtime - - - dt = np.diff(cum_time).mean() - wsize = round(5./dt) - - # velo2 = stravastuff.ewmovingaverage(velo,wsize) - - # df[' Stroke500mPace (sec/500m)'] = 500./velo2 - - - df = df.fillna(0) - - df.sort_values(by='TimeStamp (sec)',ascending=True) - - timestr = strftime("%Y%m%d-%H%M%S") - -# csvfilename ='media/Import_'+str(importid)+'.csv' - csvfilename ='media/{code}_{importid}.csv'.format( - importid=importid, - code = uuid4().hex[:16] - ) - - res = df.to_csv(csvfilename+'.gz',index_label='index', - compression='gzip') - - id,message = dataprep.save_workout_database(csvfilename,r, - workouttype=workouttype, - workoutsource='mapmyfitness', - title=title, - notes=comments) - - return (id,message) -