Private
Public Access
1
0

sort of working

This commit is contained in:
Sander Roosendaal
2018-06-25 18:42:16 +02:00
parent 1e9caf244c
commit 90dcb2f4f9
4 changed files with 84 additions and 40 deletions

View File

@@ -1030,6 +1030,8 @@ def dataprep(rowdatadf,id=0,bands=True,barchart=True,otwpower=True,
p2 = p.fillna(method='ffill').apply(lambda x: timedeltaconv(x)) p2 = p.fillna(method='ffill').apply(lambda x: timedeltaconv(x))
print p
try: try:
drivespeed = drivelength/rowdatadf[' DriveTime (ms)']*1.0e3 drivespeed = drivelength/rowdatadf[' DriveTime (ms)']*1.0e3
except KeyError: except KeyError:

View File

@@ -26,6 +26,12 @@ from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
import django_rq
queue = django_rq.get_queue('default')
queuelow = django_rq.get_queue('low')
queuehigh = django_rq.get_queue('low')
# Project # Project
# from .models import Profile # from .models import Profile
from rowingdata import rowingdata from rowingdata import rowingdata
@@ -34,10 +40,17 @@ from rowers.models import Rower,Workout
from rowers.models import checkworkoutuser from rowers.models import checkworkoutuser
import dataprep import dataprep
from dataprep import columndict from dataprep import columndict
from utils import uniqify,isprorower from utils import uniqify,isprorower,myqueue
from uuid import uuid4 from uuid import uuid4
import stravalib import stravalib
from stravalib.exc import ActivityUploadFailed,TimeoutExceeded from stravalib.exc import ActivityUploadFailed,TimeoutExceeded
import iso8601
from iso8601 import ParseError
import pytz
import arrow
from rowers.tasks import handle_strava_import_stroke_data
from rowsandall_app.settings import C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET, STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET from rowsandall_app.settings import C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET, STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET
@@ -46,28 +59,8 @@ try:
except ImportError: except ImportError:
JSONDecodeError = ValueError JSONDecodeError = ValueError
# Exponentially weighted moving average
# Used for data smoothing of the jagged data obtained by Strava
# See bitbucket issue 72
def ewmovingaverage(interval,window_size):
# Experimental code using Exponential Weighted moving average
try: from utils import geo_distance,ewmovingaverage
intervaldf = pd.DataFrame({'v':interval})
idf_ewma1 = intervaldf.ewm(span=window_size)
idf_ewma2 = intervaldf[::-1].ewm(span=window_size)
i_ewma1 = idf_ewma1.mean().ix[:,'v']
i_ewma2 = idf_ewma2.mean().ix[:,'v']
interval2 = np.vstack((i_ewma1,i_ewma2[::-1]))
interval2 = np.mean( interval2, axis=0) # average
except ValueError:
interval2 = interval
return interval2
from utils import geo_distance
# Custom exception handler, returns a 401 HTTP message # Custom exception handler, returns a 401 HTTP message
@@ -155,12 +148,16 @@ def get_strava_workout_list(user):
def add_stroke_data(user,stravaid,workoutid,startdatetime,csvfilename): def add_stroke_data(user,stravaid,workoutid,startdatetime,csvfilename):
r = Rower.objects.get(user=user) r = Rower.objects.get(user=user)
print 'Queueing job for workout {workoutid} (strava {stravaid})'.format(
workoutid=workoutid,
stravaid=stravaid)
starttimeunix = arrow.get(startdatetime).timestamp starttimeunix = arrow.get(startdatetime).timestamp
job = myqueue(queue, job = myqueue(queue,
handle_strava_import_stroke_data, handle_strava_import_stroke_data,
r.stravatoken, r.stravatoken,
stravid, stravaid,
workoutid, workoutid,
starttimeunix, starttimeunix,
csvfilename) csvfilename)
@@ -187,16 +184,14 @@ def get_strava_workouts(rower):
]) ])
newids = [stravaid for stravaid in stravaids if not stravaid in knownstravaids] newids = [stravaid for stravaid in stravaids if not stravaid in knownstravaids]
print newids,'aap' for stravaid in newids[:1]:
for stravaid in newids:
workoutid = create_async_workout(alldata,rower.user,stravaid) workoutid = create_async_workout(alldata,rower.user,stravaid)
return 1 return 1
def create_async_workout(alldata,user,stravaid): def create_async_workout(alldata,user,stravaid):
data = alldata[stravid] data = alldata[stravaid]
r = Rower.objects.get(user=user)
distance = data['distance'] distance = data['distance']
stravaid = data['id'] stravaid = data['id']
try: try:
@@ -240,6 +235,14 @@ def create_async_workout(alldata,user,stravaid):
except: except:
title = 'Imported' title = 'Imported'
workoutdate = rowdatetime.astimezone(
pytz.timezone(thetimezone)
).strftime('%Y-%m-%d')
starttime = rowdatetime.astimezone(
pytz.timezone(thetimezone)
).strftime('%H:%m:%S')
totaltime = data['elapsed_time'] totaltime = data['elapsed_time']
duration = dataprep.totaltime_sec_to_string(totaltime) duration = dataprep.totaltime_sec_to_string(totaltime)

View File

@@ -37,7 +37,7 @@ from django_rq import job
from django.utils import timezone from django.utils import timezone
from django.utils.html import strip_tags from django.utils.html import strip_tags
from utils import deserialize_list from utils import deserialize_list,ewmovingaverage
from rowers.dataprepnodjango import ( from rowers.dataprepnodjango import (
update_strokedata, new_workout_from_file, update_strokedata, new_workout_from_file,
@@ -45,7 +45,7 @@ from rowers.dataprepnodjango import (
update_agegroup_db,fitnessmetric_to_sql, update_agegroup_db,fitnessmetric_to_sql,
add_c2_stroke_data_db,totaltime_sec_to_string, add_c2_stroke_data_db,totaltime_sec_to_string,
create_c2_stroke_data_db,update_empower, create_c2_stroke_data_db,update_empower,
database_url_debug,database_url, database_url_debug,database_url,dataprep
) )
@@ -85,7 +85,7 @@ def handle_strava_import_stroke_data(stravatoken,
# ready to fetch. Hurray # ready to fetch. Hurray
fetchresolution = 'high' fetchresolution = 'high'
series_type = 'time' series_type = 'time'
authorizationstring = str('Bearer ' + r.stravatoken) authorizationstring = str('Bearer ' + stravatoken)
headers = {'Authorization': authorizationstring, headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal', 'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -109,6 +109,7 @@ def handle_strava_import_stroke_data(stravatoken,
url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/latlng?resolution="+fetchresolution+"&series_type="+series_type url = "https://www.strava.com/api/v3/activities/"+str(stravaid)+"/streams/latlng?resolution="+fetchresolution+"&series_type="+series_type
latlongjson = requests.get(url,headers=headers) latlongjson = requests.get(url,headers=headers)
try: try:
t = np.array(timejson.json()[0]['data']) t = np.array(timejson.json()[0]['data'])
nr_rows = len(t) nr_rows = len(t)
@@ -161,19 +162,37 @@ def handle_strava_import_stroke_data(stravatoken,
pace = 500./(1.0*velo2) pace = 500./(1.0*velo2)
pace[np.isinf(pace)] = 0.0 pace[np.isinf(pace)] = 0.0
df = pd.DataFrame({'t':10*t, unixtime = starttimeunix+10*t
'd':10*d,
'p':10*pace, nr_strokes = len(t)
'spm':spm,
'hr':hr, df = pd.DataFrame({'TimeStamp (sec)':unixtime,
'lat':lat, ' ElapsedTime (sec)':10*t,
'lon':lon, ' Horizontal (meters)':10*d,
'strokelength':strokelength, ' Stroke500mPace (sec/500m)':10*pace,
' Cadence (stokes/min)':spm,
' HRCur (bpm)':hr,
' latitude':lat,
' longitude':lon,
' StrokeDistance (meters)':strokelength,
'cum_dist':10*d,
' DragFactor':np.zeros(nr_strokes),
' DriveLength (meters)':np.zeros(nr_strokes),
' StrokeDistance (meters)':np.zeros(nr_strokes),
' DriveTime (ms)':np.zeros(nr_strokes),
' StrokeRecoveryTime (ms)':np.zeros(nr_strokes),
' AverageDriveForce (lbs)':np.zeros(nr_strokes),
' PeakDriveForce (lbs)':np.zeros(nr_strokes),
' lapIdx':np.zeros(nr_strokes),
' Power (watts)':0*d,
}) })
res = df.to_csv(csvfilename+'.gz',index_label='index',compression='gzip')
data = dataprep(df,id=workoutid,bands=False,debug=debug)
# startdatetime = datetime.datetime.strptime(startdatetime,"%Y-%m-%d-%H:%M:%S") # startdatetime = datetime.datetime.strptime(startdatetime,"%Y-%m-%d-%H:%M:%S")
return [workoutsummary,df] return 1
@app.task @app.task

View File

@@ -378,3 +378,23 @@ def isprorower(r):
return result return result
# Exponentially weighted moving average
# Used for data smoothing of the jagged data obtained by Strava
# See bitbucket issue 72
def ewmovingaverage(interval,window_size):
# Experimental code using Exponential Weighted moving average
try:
intervaldf = pd.DataFrame({'v':interval})
idf_ewma1 = intervaldf.ewm(span=window_size)
idf_ewma2 = intervaldf[::-1].ewm(span=window_size)
i_ewma1 = idf_ewma1.mean().ix[:,'v']
i_ewma2 = idf_ewma2.mean().ix[:,'v']
interval2 = np.vstack((i_ewma1,i_ewma2[::-1]))
interval2 = np.mean( interval2, axis=0) # average
except ValueError:
interval2 = interval
return interval2