Private
Public Access
1
0

fix performance chart to polars

This commit is contained in:
2024-04-14 20:44:24 +02:00
parent 30fdabd2cd
commit 2d2923362f

View File

@@ -19,6 +19,7 @@ import rowers.dataprep as dataprep
from rowers.dataprep import rdata from rowers.dataprep import rdata
import rowers.utils as utils import rowers.utils as utils
import polars as pl import polars as pl
import pytz
from rowers.rower_rules import ispromember from rowers.rower_rules import ispromember
from polars.exceptions import ColumnNotFoundError from polars.exceptions import ColumnNotFoundError
@@ -479,7 +480,7 @@ def interactive_forcecurve(theworkouts):
def weightfromrecord(row,metricchoice): def weightfromrecord(row,metricchoice):
vv = row[metricchoice] vv = row[metricchoice][0]
if vv > 0: if vv > 0:
return vv return vv
if metricchoice == 'rscore': # pragma: no cover if metricchoice == 'rscore': # pragma: no cover
@@ -505,18 +506,19 @@ def getfatigues(
lambda_c = 2/(kfitness+1) lambda_c = 2/(kfitness+1)
nrdays = (enddate-startdate).days nrdays = (enddate-startdate).days
for i in range(nrdays+1): for i in range(nrdays+1):
date = startdate+datetime.timedelta(days=i) date = startdate+datetime.timedelta(days=i)
datekey = date.strftime('%Y-%m-%d') datekey = date.strftime('%Y-%m-%d')
weight = 0 weight = 0
try: try:
df2 = df.loc[date.date()] df2 = df.filter(pl.col("date") == date.date())
if type(df2) == pd.Series: # pragma: no cover if type(df2) == pl.Series: # pragma: no cover
weight += weightfromrecord(df2,metricchoice) weight += weightfromrecord(df2,metricchoice)
else: else:
for index, row in df2.iterrows(): for row in df2.iter_slices(n_rows=1):
weight += weightfromrecord(row,metricchoice) weight += weightfromrecord(row,metricchoice)
except KeyError: except KeyError:
pass pass
@@ -693,8 +695,8 @@ def performance_chart(user, startdate=None, enddate=None, kfitness=42, kfatigue=
} }
records.append(dd) records.append(dd)
df = pd.DataFrame.from_records(records) df = pl.from_records(records)
if df.empty: # pragma: no cover if df.is_empty(): # pragma: no cover
return ['', 'No Data', 0, 0, 0, outids] return ['', 'No Data', 0, 0, 0, outids]
markerworkouts = Workout.objects.filter( markerworkouts = Workout.objects.filter(
@@ -727,7 +729,7 @@ def performance_chart(user, startdate=None, enddate=None, kfitness=42, kfatigue=
kfatigue, kfitness kfatigue, kfitness
) )
df = pd.DataFrame({ df = pl.DataFrame({
'date': dates, 'date': dates,
'testpower': testpower, 'testpower': testpower,
'testduration': testduration, 'testduration': testduration,
@@ -742,19 +744,19 @@ def performance_chart(user, startdate=None, enddate=None, kfitness=42, kfatigue=
endform = endfitness-endfatigue endform = endfitness-endfatigue
if modelchoice == 'banister': # pragma: no cover if modelchoice == 'banister': # pragma: no cover
df['fatigue'] = k2*df['fatigue'] df = df.with_columns((pl.col("fatigue")*k2))
df['fitness'] = p0+k1*df['fitness'] df = df.with_columns((p0+pl.col("fitness")*k1))
df['form'] = df['fitness']-df['fatigue'] df = df.with_columns((pl.col("fitness")-pl.col("fatigue")).alias("form"))
df = df.sort("date")
df.sort_values(['date'], inplace=True) df = df.group_by('date').max()
df = df.groupby(['date']).max() startdate = startdate.replace(tzinfo=None)
df['date'] = df.index.values startdate = pytz.utc.localize(startdate)
mask = df['date'] > np.datetime64(startdate.astimezone(
tz=datetime.timezone.utc).replace(tzinfo=None))
df = df.loc[mask]
df2 = pd.DataFrame({ df = df.filter(pl.col("date") > startdate)
df2 = pl.DataFrame({
"testpower" :df['testpower'], "testpower" :df['testpower'],
"testduration":df['testduration'].apply( "testduration":df['testduration'].apply(
lambda x: totaltime_sec_to_string(x, shorten=True)), lambda x: totaltime_sec_to_string(x, shorten=True)),
@@ -762,14 +764,13 @@ def performance_chart(user, startdate=None, enddate=None, kfitness=42, kfatigue=
"fatigue":df['fatigue'], "fatigue":df['fatigue'],
"form":df['form'], "form":df['form'],
"impulse":df['impulse'], "impulse":df['impulse'],
"date": df['date'].map(lambda x: x.strftime('%Y-%m-%d')), "date": df['date'].apply(lambda x: x.strftime('%Y-%m-%d')),
}) })
df2 = df2.fill_nan(0)
df2.fillna(value=0, inplace=True) data_dict = df2.to_dicts()
data_dict = df2.to_dict("records")
chart_data = { chart_data = {
'data': data_dict, 'data': data_dict,