Private
Public Access
1
0

further enhancements

This commit is contained in:
Sander Roosendaal
2021-03-01 17:43:57 +01:00
parent 19560145e2
commit f2ec2585cc
3 changed files with 338 additions and 235 deletions

View File

@@ -508,7 +508,7 @@ class TeamRequest(models.Model):
from rowers.utils import (
workflowleftpanel,workflowmiddlepanel,
defaultleft,defaultmiddle,landingpages,
steps_read_fit, steps_write_fit
steps_read_fit, steps_write_fit, ps_dict_order
)
from rowers.utils import geo_distance
@@ -2418,11 +2418,27 @@ class PlannedSession(models.Model):
filename = 'session'+encoder.encode_hex(self.pk)+'.fit'
else:
file = tempfile.NamedTemporaryFile(mode='w+b',suffix='.fit',dir=settings.MEDIA_ROOT)
filename = file.name
filename = file
steps = self.steps
steps['filename'] = filename
steps['filename'] = settings.MEDIA_ROOT+'/'+filename
fitfile = steps_write_fit(steps)
self.fitfile.name = fitfile
filelist = fitfile.split('/media/')
self.fitfile.name = filelist[-1]
# calculate approximate distance
if self.steps:
sdict, totalmeters, totalseconds = ps_dict_order(self.steps)
self.approximate_distance = int(totalmeters)
self.approximate_duration = int(totalseconds/60.)
self.criterium = 'none'
if self.sessionmode == 'time':
self.sessionvalue = self.approximate_duration
elif self.sessionmode == 'distance':
self.sessionvalue = self.approximate_distance
super(PlannedSession,self).save(*args, **kwargs)

View File

@@ -15,7 +15,7 @@ import uuid
from django.conf import settings
import pytz
from dateutil import parser
from rowers.utils import myqueue,calculate_age,totaltime_sec_to_string
from rowers.utils import myqueue,calculate_age,totaltime_sec_to_string,ps_dict_order
from rowers.rows import handle_uploaded_file
import collections
import re
@@ -92,238 +92,17 @@ from rowers.tasks import (
)
from rowers.utils import totaltime_sec_to_string
def step_to_string(step):
type = 'Step'
repeatID = -1
nr = 0
name = ''
intensity = ''
duration = ''
unit = ''
target = ''
repeat = ''
durationtype = step['durationType']
if step['durationValue'] == 0:
return '',type, -1, -1
if durationtype == 'Time':
unit = 'min'
value = step['durationValue']
dd = timedelta(seconds=value/1000.)
duration = '{v}'.format(v=str(dd))
elif durationtype == 'Distance':
unit = 'm'
value = step['durationValue']/100.
duration = int(value)
elif durationtype == 'HrLessThan':
value = step['durationValue']
if value <= 100:
duration = 'until heart rate lower than {v}% of max'.format(v=value)
else:
duration = 'until heart rate lower than {v}'.format(v=value-100)
elif durationtype == 'HrGreaterThan':
value = step['durationValue']
if value <= 100:
duration = 'until heart rate greater than {v}% of max'.format(v=value)
else:
duration = 'until heart rate greater than {v}'.format(v=value-100)
elif durationtype == 'PowerLessThan':
value = step['durationValue']
targetvalue = step['targetvalue']
if value <= 1000:
duration = 'Repeat until Power is less than {targetvalue} % of FTP'.format(
targetvalue=targetvalue
)
else:
duration = 'Repeat until Power is less than {targetvalue} Watt'.format(
targetvalue=targetvalie
)
elif durationtype == 'PowerGreaterThan':
value = step['durationValue']
targetvalue = step['targetvalue']
if value <= 1000:
duration = 'Repeat until Power is greater than {targetvalue} % of FTP'.format(
targetvalue=targetvalue
)
else:
duration = 'Repeat until Power is greater than {targetvalue} Watt'.format(
targetvalue=targetvalie
)
elif durationtype == 'RepeatUntilStepsCmplt':
type = 'RepeatStep'
ntimes = str(step['targetValue'])+' times:'
repeatID = step['durationValue']
duration =ntimes
elif durationtype == 'RepeatUntilHrGreaterThan':
type = 'RepeatStep'
targetvalue = step['targetValue']
if targetvalue <= 100:
duration = 'Repeat until Heart Rate is greater than {targetvalue} % of max'.format(
targetvalue=targetvalue
)
else:
duration = 'Repeat until Heart Rate is greater than {targetvalue}:'.format(
targetvalue=targetvalue-100.
)
repeatID = step['durationValue']
elif durationtype == 'RepeatUntilHrLessThan':
type = 'RepeatStep'
targetvalue = step['targetValue']
if targetvalue <= 100:
duration = 'Repeat until Heart Rate is less than {targetvalue} % of max'.format(
targetvalue=targetvalue
)
else:
duration = 'Repeat until Heart Rate is less than {targetvalue}:'.format(
targetvalue=targetvalue-100.
)
repeatID = step['durationValue']
#
try:
targettype = step['targetType']
except KeyError:
targettype = None
if targettype == 'HeartRate':
value = step['targetValue']
valuelow = step['targetValueLow']
valuehigh = step['targetValueHigh']
if value < 10 and value>0:
target = 'Target: Heart Rate in zone {v}'.format(v=value)
else:
if valuelow < 100:
target = 'Target: Heart Rate between {l} and {h} % of max'.format(
l = valuelow,
h = valuehigh
)
else:
target = 'Target: Heart Rate between {l} and {h}'.format(
l = valuelow-100,
h = valuehigh+100
)
elif targettype == 'Power':
value = step['targetValue']
valuelow = step['targetValueLow']
valuehigh = step['targetValueHigh']
if value < 10 and value>0:
target = 'Target: Power in zone {v}'.format(v=value)
else:
if valuelow < 1000:
target = 'Target: Power between {l} and {h} % of FTP'.format(
l = valuelow,
h = valuehigh
)
else:
target = 'Target: Power between {l} and {h} W'.format(
l = valuelow-1000,
h = valuehigh-1000
)
elif targettype == 'Speed':
value = step['targetValue']
valuelow = step['targetValueLow']
valuehigh = step['targetValueHigh']
if value != 0:
v = value/1000.
pace = 500./v
target = 'Target: Speed at {v} m/s'.format(v=value/1000.)
elif valuelow != 0 and valuehigh != 0:
v = valuelow/1000.
pace = 500./v
pacestringlow = to_pace(pace)
v = valuehigh/1000.
pace = 500./v
pacestringhigh = to_pace(pace)
target = 'Target: Speed between {l:1.2f} and {h:1.2f} m/s ({ph} to {pl} per 500m)'.format(
l = valuelow/1000.,
h = valuehigh/1000.,
pl = pacestringlow,
ph = pacestringhigh,
)
elif targettype == 'Cadence':
value = step['targetValue']
valuelow = step['targetValueLow']
valuehigh = step['targetValueHigh']
if value != 0:
target = 'Target: Cadence at {v} SPM'.format(v=value)
elif valuelow != 0 and valuehigh != 0:
target = 'Target: Cadence between {l} and {h} SPM'.format(
l = valuelow/1000.,
h = valuehigh/1000.,
)
nr = step['stepId']
name = step['wkt_step_name']
intensity = step['intensity']
s = '{name} {intensity} {duration} {unit} {target} {repeat}'.format(
nr = nr,
name = name,
unit=unit,
intensity = intensity,
duration = duration,
target=target,
repeat = repeat,
)
if type == 'RepeatStep':
s = 'Repeat {duration}'.format(duration=duration)
return s,type, nr, repeatID
def ps_dict_order(d):
sdict = collections.OrderedDict({})
steps = d['steps']
for step in steps:
sstring, type, stepID, repeatID = step_to_string(step)
sdict[stepID] = {
'string':sstring,
'type':type,
'stepID': stepID,
'repeatID': repeatID,
}
sdict2 = collections.OrderedDict(reversed(list(sdict.items())))
sdict3 = []
hold = []
holduntil = []
spaces = ''
for key, item in sdict2.items():
if item['type'] == 'RepeatStep':
hold.append(item)
holduntil.append(item['repeatID'])
spaces += '&nbsp;&nbsp;&nbsp;'
if item['type'] == 'Step':
item['string'] = spaces+item['string']
sdict3.append(item)
if len(holduntil)>0 and item['stepID'] == holduntil[-1]:
sdict3.append(hold.pop())
spaces = spaces[:-18]
holduntil.pop()
sdict = list(reversed(sdict3))
# nog niet af
def ps_dict_get_durationdistance(d):
sdict = ps_dict_order(d)
return sdict
def ps_dict_get_description(d):
sdict = ps_dict_order(d)
sdict,totalmeters,totalseconds = ps_dict_order(d)
s = ''
for item in sdict:
s += item['string']+'\n'
@@ -331,7 +110,7 @@ def ps_dict_get_description(d):
return s
def ps_dict_get_description_html(d):
sdict = ps_dict_order(d)
sdict,totalmeters,totalseconds = ps_dict_order(d)
s = '<ul>'

View File

@@ -2,12 +2,14 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from datetime import timedelta
import math
import numpy as np
import pandas as pd
import colorsys
from django.conf import settings
import collections
import uuid
import datetime
@@ -579,3 +581,309 @@ def steps_write_fit(steps):
return None
return filename
def step_to_time_dist(step,avgspeed = 3.7):
seconds = 0
distance = 0
durationtype = step['durationType']
if step['durationValue'] == 0:
return 0,0
try:
targettype = step['targetType']
except KeyError:
targettype = 0
if durationtype == 'Time':
value = step['durationValue']
seconds = value/1000.
distance = avgspeed*seconds
if targettype == 'Speed':
value = step['targetValue']
valuelow = step['targetValueLow']
valuehigh = step['targetValueHigh']
if value != 0:
distance = seconds*value
elif valuelow != 0 and valuehigh != 0:
distance = seconds*(valuelow+valuehigh)/2.
return seconds,distance
elif durationtype == 'Distance':
value = step['durationValue']
distance = value/100.
seconds = distance/avgspeed
if targettype == 'Speed':
value = step['targetValue']
valuelow = step['targetValueLow']
valuehigh = step['targetValueHigh']
if value != 0:
seconds = distance/value
elif valuelow != 0 and valuehigh != 0:
midspeed = (valuelow+valuehigh)/2.
seconds = distance/midspeed
return seconds, distance
elif durationtype in ['PowerLessThan','PowerGreaterThan','HrLessThan','HrGreaterThan']:
seconds = 600
distance = seconds*avgspeed
return seconds,distance
return seconds,distance
def ps_dict_order(d):
sdict = collections.OrderedDict({})
steps = d['steps']
for step in steps:
sstring, type, stepID, repeatID, repeatValue = step_to_string(step)
seconds, meters = step_to_time_dist(step)
sdict[stepID] = {
'string':sstring,
'type':type,
'stepID': stepID,
'repeatID': repeatID,
'repeatValue': repeatValue,
'seconds': seconds,
'meters': meters,
}
sdict2 = collections.OrderedDict(reversed(list(sdict.items())))
sdict3 = []
hold = []
multiplier = []
holduntil = []
spaces = ''
totalmeters = 0
totalseconds = 0
factor = 1
for key, item in sdict2.items():
if item['type'] == 'RepeatStep':
hold.append(item)
holduntil.append(item['repeatID'])
multiplier.append(item['repeatValue'])
factor *= item['repeatValue']
spaces += '&nbsp;&nbsp;&nbsp;'
if item['type'] == 'Step':
item['string'] = spaces+item['string']
sdict3.append(item)
totalmeters += factor*item['meters']
totalseconds += factor*item['seconds']
if len(holduntil)>0 and item['stepID'] == holduntil[-1]:
sdict3.append(hold.pop())
factor /= multiplier.pop()
spaces = spaces[:-18]
holduntil.pop()
sdict = list(reversed(sdict3))
return sdict,totalmeters,totalseconds
def step_to_string(step):
type = 'Step'
repeatID = -1
repeatValue = 1
nr = 0
name = ''
intensity = ''
duration = ''
unit = ''
target = ''
repeat = ''
durationtype = step['durationType']
if step['durationValue'] == 0:
return '',type, -1, -1
if durationtype == 'Time':
unit = 'min'
value = step['durationValue']
if value/1000. >= 3600:
unit = 'hour'
dd = timedelta(seconds=value/1000.)
duration = '{v}'.format(v=str(dd))
elif durationtype == 'Distance':
unit = 'm'
value = step['durationValue']/100.
duration = int(value)
elif durationtype == 'HrLessThan':
value = step['durationValue']
if value <= 100:
duration = 'until heart rate lower than {v}% of max'.format(v=value)
else:
duration = 'until heart rate lower than {v}'.format(v=value-100)
elif durationtype == 'HrGreaterThan':
value = step['durationValue']
if value <= 100:
duration = 'until heart rate greater than {v}% of max'.format(v=value)
else:
duration = 'until heart rate greater than {v}'.format(v=value-100)
elif durationtype == 'PowerLessThan':
value = step['durationValue']
targetvalue = step['targetvalue']
if value <= 1000:
duration = 'Repeat until Power is less than {targetvalue} % of FTP'.format(
targetvalue=targetvalue
)
else:
duration = 'Repeat until Power is less than {targetvalue} Watt'.format(
targetvalue=targetvalie
)
elif durationtype == 'PowerGreaterThan':
value = step['durationValue']
targetvalue = step['targetvalue']
if value <= 1000:
duration = 'Repeat until Power is greater than {targetvalue} % of FTP'.format(
targetvalue=targetvalue
)
else:
duration = 'Repeat until Power is greater than {targetvalue} Watt'.format(
targetvalue=targetvalie
)
elif durationtype == 'RepeatUntilStepsCmplt':
type = 'RepeatStep'
ntimes = str(step['targetValue'])+' times:'
repeatID = step['durationValue']
duration =ntimes
repeatValue = step['targetValue']
elif durationtype == 'RepeatUntilHrGreaterThan':
type = 'RepeatStep'
targetvalue = step['targetValue']
if targetvalue <= 100:
duration = 'Repeat until Heart Rate is greater than {targetvalue} % of max'.format(
targetvalue=targetvalue
)
else:
duration = 'Repeat until Heart Rate is greater than {targetvalue}:'.format(
targetvalue=targetvalue-100.
)
repeatID = step['durationValue']
elif durationtype == 'RepeatUntilHrLessThan':
type = 'RepeatStep'
targetvalue = step['targetValue']
if targetvalue <= 100:
duration = 'Repeat until Heart Rate is less than {targetvalue} % of max'.format(
targetvalue=targetvalue
)
else:
duration = 'Repeat until Heart Rate is less than {targetvalue}:'.format(
targetvalue=targetvalue-100.
)
repeatID = step['durationValue']
#
try:
targettype = step['targetType']
except KeyError:
targettype = None
if targettype == 'HeartRate':
value = step['targetValue']
valuelow = step['targetValueLow']
valuehigh = step['targetValueHigh']
if value < 10 and value>0:
target = 'Target: Heart Rate in zone {v}'.format(v=value)
else:
if valuelow < 100:
target = 'Target: Heart Rate between {l} and {h} % of max'.format(
l = valuelow,
h = valuehigh
)
else:
target = 'Target: Heart Rate between {l} and {h}'.format(
l = valuelow-100,
h = valuehigh+100
)
elif targettype == 'Power':
value = step['targetValue']
valuelow = step['targetValueLow']
valuehigh = step['targetValueHigh']
if value < 10 and value>0:
target = 'Target: Power in zone {v}'.format(v=value)
else:
if valuelow < 1000:
target = 'Target: Power between {l} and {h} % of FTP'.format(
l = valuelow,
h = valuehigh
)
else:
target = 'Target: Power between {l} and {h} W'.format(
l = valuelow-1000,
h = valuehigh-1000
)
elif targettype == 'Speed':
value = step['targetValue']
valuelow = step['targetValueLow']
valuehigh = step['targetValueHigh']
if value != 0:
v = value/1000.
pace = 500./v
target = 'Target: Speed at {v} m/s'.format(v=value/1000.)
elif valuelow != 0 and valuehigh != 0:
v = valuelow/1000.
pace = 500./v
pacestringlow = to_pace(pace)
v = valuehigh/1000.
pace = 500./v
pacestringhigh = to_pace(pace)
target = 'Target: Speed between {l:1.2f} and {h:1.2f} m/s ({ph} to {pl} per 500m)'.format(
l = valuelow/1000.,
h = valuehigh/1000.,
pl = pacestringlow,
ph = pacestringhigh,
)
elif targettype == 'Cadence':
value = step['targetValue']
valuelow = step['targetValueLow']
valuehigh = step['targetValueHigh']
if value != 0:
target = 'Target: Cadence at {v} SPM'.format(v=value)
elif valuelow != 0 and valuehigh != 0:
target = 'Target: Cadence between {l} and {h} SPM'.format(
l = valuelow/1000.,
h = valuehigh/1000.,
)
nr = step['stepId']
name = step['wkt_step_name']
intensity = step['intensity']
s = '{name} {intensity} {duration} {unit} {target} {repeat}'.format(
nr = nr,
name = name,
unit=unit,
intensity = intensity,
duration = duration,
target=target,
repeat = repeat,
)
if type == 'RepeatStep':
s = 'Repeat {duration}'.format(duration=duration)
return s,type, nr, repeatID, repeatValue