adding fitscore
This commit is contained in:
@@ -6,7 +6,9 @@ from __future__ import unicode_literals
|
||||
# All the data preparation, data cleaning and data mangling should
|
||||
# be defined here
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
from rowers.models import Workout, Team
|
||||
from rowers.models import (
|
||||
Workout, Team, CalcAgePerformance,C2WorldClassAgePerformance,
|
||||
)
|
||||
|
||||
import pytz
|
||||
import collections
|
||||
@@ -23,7 +25,10 @@ from rowingdata import (
|
||||
get_file_type, get_empower_rigging,get_empower_firmware
|
||||
)
|
||||
|
||||
from rowers.tasks import handle_sendemail_unrecognized,handle_setcp
|
||||
from rowers.tasks import (
|
||||
handle_sendemail_unrecognized,handle_setcp,
|
||||
handle_getagegrouprecords
|
||||
)
|
||||
from rowers.tasks import handle_zip_file
|
||||
|
||||
from pandas import DataFrame, Series
|
||||
@@ -1016,6 +1021,67 @@ def fetchcperg(rower,theworkouts):
|
||||
|
||||
return cpdf
|
||||
|
||||
from rowers.datautils import p0
|
||||
from rowers.utils import calculate_age
|
||||
from scipy import optimize
|
||||
|
||||
def fitscore(rower,workout):
|
||||
cpfile = 'media/cpdata_{id}.parquet.gz'.format(id=workout.id)
|
||||
try:
|
||||
df = pd.read_parquet(cpfile)
|
||||
except:
|
||||
df, delta, cpvalues = setcp(workout)
|
||||
|
||||
age = calculate_age(rower.birthdate,today=workout.date)
|
||||
agerecords = CalcAgePerformance.objects.filter(
|
||||
age=age,
|
||||
sex=rower.sex,
|
||||
weightcategory = rower.weightcategory
|
||||
)
|
||||
wcdurations = []
|
||||
wcpower = []
|
||||
for record in agerecords:
|
||||
wcdurations.append(record.duration)
|
||||
wcpower.append(record.power)
|
||||
|
||||
if len(agerecords)==0:
|
||||
durations = [1,4,10,20,30,60]
|
||||
distances = []
|
||||
df2 = pd.DataFrame(
|
||||
list(
|
||||
C2WorldClassAgePerformance.objects.filter(
|
||||
sex=rower.sex,
|
||||
weightcategory=rower.weightcategory
|
||||
).values()
|
||||
)
|
||||
)
|
||||
jsondf = df2.to_json()
|
||||
job = myqueue(queue,handle_getagegrouprecords,
|
||||
jsondf,distances,durations,age,rower.sex,rower.weightcategory)
|
||||
|
||||
wcpower = pd.Series(wcpower)
|
||||
wcdurations = pd.Series(wcdurations)
|
||||
|
||||
fitfunc = lambda pars,x: pars[0]/(1+(x/pars[2])) + pars[1]/(1+(x/pars[3]))
|
||||
errfunc = lambda pars,x,y: fitfunc(pars,x)-y
|
||||
|
||||
if len(wcdurations)>4:
|
||||
p1wc, success = optimize.leastsq(errfunc, p0[:],args=(wcdurations,wcpower))
|
||||
else:
|
||||
factor = fitfunc(p0,wcdurations.mean()/wcpower.mean())
|
||||
p1wc = [p0[0]/factor,p0[1]/factor,p0[2],p0[3]]
|
||||
success = 0
|
||||
|
||||
|
||||
times = df['delta']
|
||||
powers = df['cp']
|
||||
wcpowers = fitfunc(p1wc,times)
|
||||
scores = 100.*powers/wcpowers
|
||||
|
||||
indexmax = scores.idxmax()
|
||||
|
||||
return scores.max(),df.loc[indexmax,'delta']
|
||||
|
||||
def fetchcp_new(rower,workouts):
|
||||
|
||||
data = []
|
||||
@@ -3009,6 +3075,7 @@ def dataprep(rowdatadf, id=0, bands=True, barchart=True, otwpower=True,
|
||||
return data
|
||||
|
||||
|
||||
|
||||
def workout_trimp(w):
|
||||
r = w.user
|
||||
|
||||
|
||||
Reference in New Issue
Block a user