From 0f494020238379e6a5f30131923501d285d7c036 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Sun, 19 Feb 2017 16:45:53 +0100 Subject: [PATCH] rough version of comparison --- rowers/forms.py | 6 ++ rowers/interactiveplots.py | 35 ++++++++- rowers/stravastuff.py | 19 +++-- rowers/templates/biginteractive1.html | 92 +++++++++++------------ rowers/templates/list_workouts.html | 7 ++ rowers/templates/multicompare.html | 73 ++++++++++-------- rowers/templates/team_compare_select.html | 20 ++++- rowers/views.py | 14 +++- 8 files changed, 171 insertions(+), 95 deletions(-) diff --git a/rowers/forms.py b/rowers/forms.py index cf29f328..6f251a2e 100644 --- a/rowers/forms.py +++ b/rowers/forms.py @@ -264,5 +264,11 @@ axlabels = list(axlabels.items()) class ChartParamChoiceForm(forms.Form): + plotchoices = ( + ('line','Line Plot'), + ('scatter','Scatter Plot'), + ) xparam = forms.ChoiceField(choices=axlabels,initial='distance') yparam = forms.ChoiceField(choices=axlabels,initial='hr') + plottype = forms.ChoiceField(choices=plotchoices,initial='scatter') + teamid = forms.IntegerField(widget=forms.HiddenInput()) diff --git a/rowers/interactiveplots.py b/rowers/interactiveplots.py index f71d7e45..6789d0ce 100644 --- a/rowers/interactiveplots.py +++ b/rowers/interactiveplots.py @@ -1582,7 +1582,8 @@ def interactive_bar_chart(id=0,promember=0): return [script,div] def interactive_multiple_compare_chart(ids,xparam,yparam,plottype='line', - promember=0): + promember=0, + labeldict=None): columns = [xparam,yparam, 'ftime','distance','fpace', 'power','hr','spm', @@ -1636,21 +1637,49 @@ def interactive_multiple_compare_chart(ids,xparam,yparam,plottype='line', plot = Figure(x_axis_type=x_axis_type,y_axis_type=y_axis_type, tools=TOOLS, toolbar_location="above", + plot_width=920, toolbar_sticky=False) colors = itertools.cycle(palette) + cntr = 0 + for id,color in itertools.izip(ids,colors): group = datadf[datadf['workoutid']==int(id)].copy() group.sort_values(by='time',ascending=True,inplace=True) group['x'] = group[xparam] group['y'] = group[yparam] + + ymean = group['y'].mean() + ylabel = Label(x=100,y=70+20*cntr, + x_units='screen',y_units='screen', + text=yparam+": {ymean:6.2f}".format(ymean=ymean), + background_fill_alpha=.7, + text_color=color, + ) + if yparam != 'time' and yparam != 'pace': + plot.add_layout(ylabel) + + print cntr,id,len(group),ymean + source = ColumnDataSource( group ) - plot.line('x','y',source=source,color=color,legend=str(id)) - plot.legend.location='top_left' + if labeldict: + legend=labeldict[id] + else: + legend=str(id) + + if plottype=='line': + plot.line('x','y',source=source,color=color,legend=legend) + else: + plot.scatter('x','y',source=source,color=color,legend=legend, + fill_alpha=0.4,line_color=None) + + cntr += 1 + + plot.legend.location='bottom_right' plot.xaxis.axis_label = axlabels[xparam] plot.yaxis.axis_label = axlabels[yparam] diff --git a/rowers/stravastuff.py b/rowers/stravastuff.py index b0bf84e4..67780a93 100644 --- a/rowers/stravastuff.py +++ b/rowers/stravastuff.py @@ -39,15 +39,18 @@ from rowsandall_app.settings import C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SEC def ewmovingaverage(interval,window_size): # Experimental code using Exponential Weighted moving average - intervaldf = pd.DataFrame({'v':interval}) - idf_ewma1 = intervaldf.ewm(span=window_size) - idf_ewma2 = intervaldf[::-1].ewm(span=window_size) + 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'] - 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 + interval2 = np.vstack((i_ewma1,i_ewma2[::-1])) + interval2 = np.mean( interval2, axis=0) # average + except ValueError: + interval2 = interval return interval2 diff --git a/rowers/templates/biginteractive1.html b/rowers/templates/biginteractive1.html index 3613da98..97061e31 100644 --- a/rowers/templates/biginteractive1.html +++ b/rowers/templates/biginteractive1.html @@ -6,57 +6,57 @@ {% block content %} - - + + - {{ interactiveplot |safe }} +{{ interactiveplot |safe }} - - + +
- - -

Interactive Plot

- - {% if user.is_authenticated and mayedit %} -
-

- Edit Workout -

-
-
-

- Advanced Edit -

- -
- {% endif %} - -
+ + +

Interactive Plot

+ + {% if user.is_authenticated and mayedit %} +
+

+ Edit Workout +

+
+
+

+ Advanced Edit +

+ +
+ {% endif %} + +
{{ the_div|safe }} +
+
-
- -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/rowers/templates/list_workouts.html b/rowers/templates/list_workouts.html index 891e596b..33408fe0 100644 --- a/rowers/templates/list_workouts.html +++ b/rowers/templates/list_workouts.html @@ -112,6 +112,13 @@
+ {% if team %} + + {% endif %}
{% if announcements %}

What's New?

diff --git a/rowers/templates/multicompare.html b/rowers/templates/multicompare.html index 14751e7f..c5724579 100644 --- a/rowers/templates/multicompare.html +++ b/rowers/templates/multicompare.html @@ -6,44 +6,53 @@ {% block content %} - - + + - {{ interactiveplot |safe }} +{{ interactiveplot |safe }} - - + +
- - -

Interactive Comparison

- - -
+

Interactive Comparison

+ + +
+ Team Page +
+
+ + + +
{{ the_div|safe }} -
- +
+
{% endblock %} diff --git a/rowers/templates/team_compare_select.html b/rowers/templates/team_compare_select.html index 3908fb47..d855a82b 100644 --- a/rowers/templates/team_compare_select.html +++ b/rowers/templates/team_compare_select.html @@ -13,8 +13,7 @@
- Select start and end date for a date range: -
+
{% if team %}
@@ -69,12 +68,25 @@

No workouts found

{% endif %}
-
+
+

Warning: You are on an experimental part of the site. Use at your own risk.

+

Select two or more workouts on the left, set your plot settings below, + and press submit"

{% csrf_token %} {{ chartform.as_table }}
- +
+

+ +

+
+
+

You can use the date and search forms above to search through all + workouts from this team.

+

TIP: Agree with your team members to put tags (e.g. '8x500m') in the notes section of + your workouts. That makes it easy to search.

+
diff --git a/rowers/views.py b/rowers/views.py index 42431190..cd06ba03 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -2036,7 +2036,7 @@ def team_comparison_select(request, form = WorkoutMultipleCompareForm() form.fields["workouts"].queryset = workouts - chartform = ChartParamChoiceForm() + chartform = ChartParamChoiceForm(initial={'teamid':theteam.id}) return render(request, 'team_compare_select.html', {'workouts': workouts, @@ -2067,15 +2067,25 @@ def multi_compare_view(request): workouts = cd['workouts'] xparam = chartform.cleaned_data['xparam'] yparam = chartform.cleaned_data['yparam'] + plottype = chartform.cleaned_data['plottype'] + teamid = chartform.cleaned_data['teamid'] ids = [w.id for w in workouts] + labeldict = { + w.id: w.__unicode__() for w in workouts + } + res = interactive_multiple_compare_chart(ids,xparam,yparam, - promember=promember) + promember=promember, + plottype=plottype, + labeldict=labeldict) script = res[0] div = res[1] + return render(request,'multicompare.html', {'interactiveplot':script, 'the_div':div, 'promember':promember, + 'teamid':teamid, }) else: return HttpResponse("Form is not valid")