Private
Public Access
1
0

boxplot sort of working

This commit is contained in:
Sander Roosendaal
2017-05-14 14:19:23 +02:00
parent 218fecdb73
commit 6208110a0a
6 changed files with 345 additions and 4 deletions

View File

@@ -297,6 +297,9 @@ formaxlabels = axlabels.copy()
formaxlabels.pop('None')
parchoices = list(sorted(formaxlabels.items(), key = lambda x:x[1]))
class BoxPlotChoiceForm(forms.Form):
yparam = forms.ChoiceField(choices=parchoices,initial='spm',
label='Metric')
class ChartParamChoiceForm(forms.Form):
plotchoices = (

View File

@@ -64,11 +64,15 @@
</p>
</div>
<div class="grid_2 omega">
<p class="button white small">
Pro Feature 3
<p>
{% if user.rower.rowerplan == 'pro' or user.rower.rowerplan == 'coach' %}
<a class="button blue small" href="/rowers/user-boxplot-select">Box Chart</a>
{% else %}
<a class="button blue small" href="/rowers/promembership">Box Chart</a>
{% endif %}
</p>
<p>
Reserved for future functionality.
BETA: Box Chart Statistics of stroke metrics over a date range
</p>
</div>
</div>

View File

@@ -0,0 +1,63 @@
{% extends "base.html" %}
{% load staticfiles %}
{% load rowerfilters %}
{% block title %}View Comparison {% endblock %}
{% block content %}
<script type="text/javascript" src="/static/js/bokeh-0.12.3.min.js"></script>
<script async="true" type="text/javascript">
Bokeh.set_log_level("info");
</script>
{{ interactiveplot |safe }}
<script>
// Set things up to resize the plot on a window resize. You can play with
// the arguments of resize_width_height() to change the plot's behavior.
var plot_resize_setup = function () {
var plotid = Object.keys(Bokeh.index)[0]; // assume we have just one plot
var plot = Bokeh.index[plotid];
var plotresizer = function() {
// arguments: use width, use height, maintain aspect ratio
plot.resize_width_height(true, false, false);
};
window.addEventListener('resize', plotresizer);
plotresizer();
};
window.addEventListener('load', plot_resize_setup);
</script>
<style>
/* Need this to get the page in "desktop mode"; not having an infinite height.*/
html, body {height: 100%; margin:5px;}
</style>
<div id="workouts" class="grid_8 alpha">
<h1>Box Chart</h1>
</div>
<div class="grid_4 omega">
<form enctype="multipart/form-data" action="/rowers/user-boxplot" method="post">
{% csrf_token %}
<table>
{{ chartform.as_table }}
</table>
<div class="grid_1 prefix_2 suffix_1">
<p>
<input name='workoutselectform' class="button green" type="submit" value="Submit">
</p>
</div>
</form>
</div>
<div class="grid_12 alpha">
<div id="theplot" class="grid_12 alpha flexplot">
{{ the_div|safe }}
</div>
</div>
{% endblock %}

View File

@@ -0,0 +1,89 @@
{% extends "base.html" %}
{% load staticfiles %}
{% load rowerfilters %}
{% block title %}Workouts{% endblock %}
{% block content %}
<div class="grid_12 alpha">
<div class="grid_8 suffix_4 alpha">
{% include "teambuttons.html" with teamid=team.id %}
</div>
</div>
<div class="grid_12 alpha">
<h3>{{ team.name }} Team Workouts</h3>
</div>
<div class="grid_12 alpha">
<div class="grid_4 alpha">
{% if theuser %}
<form enctype="multipart/form-data" action="/rowers/user-boxplot-select/user/{{ theuser.id }}/" method="post">
{% else %}
<form enctype="multipart/form-data" action="/rowers/user-boxplot-select/" method="post">
{% endif %}
<table>
{{ dateform.as_table }}
</table>
{% csrf_token %}
</div>
<div class="grid_2">
<input name='daterange' class="button green" type="submit" value="Submit"> </form>
</div>
<div class="grid_5 prefix_1 omega">
<form id="searchform" action="/rowers/user-boxplot-select/{{ startdate|date:"Y-m-d" }}/{{ enddate|date:"Y-m-d" }}"
method="get" accept-charset="utf-8">
<div class="grid_3 prefix_1 alpha">
<input class="searchfield" id="searchbox" name="q" type="text" placeholder="Search">
</div>
<div class="grid_1 omega">
<button class="button blue small" type="submit">
Search
</button>
</div>
</form>
</div>
</div>
<form enctype="multipart/form-data" action="/rowers/user-boxplot" method="post">
<div id="workouts_table" class="grid_8 alpha">
{% if workouts %}
<table width="100%" class="listtable">
{{ form.as_table }}
</table>
{% else %}
<p> No workouts found </p>
{% endif %}
</div>
<div id="form_settings" class="grid_4 alpha">
<p><b>Warning: You are on an experimental part of the site. Use at your own risk.</b></p>
<p>Select two or more workouts on the left, set your plot settings below,
and press submit"</p>
{% csrf_token %}
<table>
{{ chartform.as_table }}
</table>
<div class="grid_1 prefix_2 suffix_1">
<p>
<input name='workoutselectform' class="button green" type="submit" value="Submit">
</p>
</div>
<div class="grid_4">
<p>You can use the date and search forms above to search through all
workouts from this team.</p>
<p>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.</p>
</div>
</div>
</form>
{% endblock %}

View File

@@ -124,6 +124,10 @@ urlpatterns = [
url(r'^team-compare-select/team/(?P<teamid>\d+)/$',views.team_comparison_select),
url(r'^team-compare-select/(?P<startdatestring>\w+.*)/(?P<enddatestring>\w+.*)$',views.team_comparison_select),
url(r'^team-compare-select/$',views.team_comparison_select),
url(r'^user-boxplot-select/team/(?P<userid>\d+)/(?P<startdatestring>\w+.*)/(?P<enddatestring>\w+.*)$',views.user_boxplot_select),
url(r'^user-boxplot-select/user/(?P<userid>\d+)/$',views.user_boxplot_select),
url(r'^user-boxplot-select/(?P<startdatestring>\w+.*)/(?P<enddatestring>\w+.*)$',views.user_boxplot_select),
url(r'^user-boxplot-select/$',views.user_boxplot_select),
url(r'^list-graphs/$',views.graphs_view),
url(r'^(?P<theuser>\d+)/ote-bests/(?P<startdatestring>\w+.*)/(?P<enddatestring>\w+.*)$',views.rankings_view),
url(r'^(?P<theuser>\d+)/ote-bests/(?P<deltadays>\d+)$',views.rankings_view),
@@ -220,6 +224,7 @@ urlpatterns = [
url(r'^workout/(?P<id>\d+)/underarmouruploadw/$',views.workout_underarmour_upload_view),
url(r'^workout/(?P<id>\d+)/tpuploadw/$',views.workout_tp_upload_view),
url(r'^multi-compare$',views.multi_compare_view),
url(r'^user-boxplot$',views.boxplot_view),
url(r'^me/teams/$',views.rower_teams_view),
url(r'^team/(?P<id>\d+)/$',views.team_view),
url(r'^team/(?P<id>\d+)/edit$',views.team_edit_view),

View File

@@ -34,7 +34,7 @@ from rowers.forms import (
EmailForm, RegistrationForm, RegistrationFormTermsOfService,
RegistrationFormUniqueEmail,CNsummaryForm,UpdateWindForm,
UpdateStreamForm,WorkoutMultipleCompareForm,ChartParamChoiceForm,
FusionMetricChoiceForm,
FusionMetricChoiceForm,BoxPlotChoiceForm,
)
from rowers.models import Workout, User, Rower, WorkoutForm,FavoriteChart
from rowers.models import (
@@ -2847,6 +2847,8 @@ def team_comparison_select(request,
'teams':get_my_teams(request.user),
})
# Team comparison
@login_required()
def multi_compare_view(request):
promember=0
@@ -2926,6 +2928,181 @@ def multi_compare_view(request):
url = reverse(workouts_view)
return HttpResponseRedirect(url)
# Box plots
@user_passes_test(ispromember,login_url="/",redirect_field_name=None)
def user_boxplot_select(request,
startdatestring="",
enddatestring="",
message='',
successmessage='',
startdate=timezone.now()-datetime.timedelta(days=30),
enddate=timezone.now()+datetime.timedelta(days=1),
userid=0):
if userid == 0:
user = request.user
else:
user = User.objects.get(id=userid)
try:
r = Rower.objects.get(user=user)
except Rower.DoesNotExist:
raise Http404("Rower doesn't exist")
if request.method == 'POST':
dateform = DateRangeForm(request.POST)
if dateform.is_valid():
startdate = dateform.cleaned_data['startdate']
enddate = dateform.cleaned_data['enddate']
else:
dateform = DateRangeForm(initial={
'startdate':startdate,
'enddate':enddate,
})
startdate = datetime.datetime.combine(startdate,datetime.time())
enddate = datetime.datetime.combine(enddate,datetime.time(23,59,59))
enddate = enddate+datetime.timedelta(days=1)
if startdatestring:
startdate = iso8601.parse_date(startdatestring)
if enddatestring:
enddate = iso8601.parse_date(enddatestring)
if enddate < startdate:
s = enddate
enddate = startdate
startdate = s
workouts = Workout.objects.filter(user=r,
startdatetime__gte=startdate,
startdatetime__lte=enddate).order_by("-date", "-starttime")
query = request.GET.get('q')
if query:
query_list = query.split()
workouts = workouts.filter(
reduce(operator.and_,
(Q(name__icontains=q) for q in query_list)) |
reduce(operator.and_,
(Q(notes__icontains=q) for q in query_list))
)
form = WorkoutMultipleCompareForm()
form.fields["workouts"].queryset = workouts
chartform = BoxPlotChoiceForm()
messages.info(request,successmessage)
messages.error(request,message)
return render(request, 'user_boxplot_select.html',
{'workouts': workouts,
'dateform':dateform,
'startdate':startdate,
'enddate':enddate,
'theuser':user,
'form':form,
'chartform':chartform,
'teams':get_my_teams(request.user),
})
@user_passes_test(ispromember,login_url="/",redirect_field_name=None)
def boxplot_view(request,userid=0,
options={
'includereststrokes':False,
}):
if 'options' in request.session:
options = request.session['options']
includereststrokes = options['includereststrokes']
workstrokesonly = not includereststrokes
if request.method == 'POST' and 'workouts' in request.POST:
form = WorkoutMultipleCompareForm(request.POST)
chartform = BoxPlotChoiceForm(request.POST)
if form.is_valid() and chartform.is_valid():
cd = form.cleaned_data
workouts = cd['workouts']
plotfield = chartform.cleaned_data['yparam']
ids = [int(w.id) for w in workouts]
request.session['ids'] = ids
labeldict = {
int(w.id): w.__unicode__() for w in workouts
}
datemapping = {
w.id:w.date for w in workouts
}
fieldlist,fielddict = dataprep.getstatsfields()
fieldlist = [plotfield,'workoutid']
# prepare data frame
datadf = dataprep.read_cols_df_sql(ids,fieldlist)
datadf = dataprep.clean_df_stats(datadf,workstrokesonly=workstrokesonly)
datadf['workoutid'].replace(datemapping,inplace=True)
datadf.rename(columns={"workoutid":"date"},inplace=True)
datadf = datadf.sort_values(['date'])
script,div = interactive_boxchart(datadf,plotfield)
return render(request,'boxplot.html',
{'interactiveplot':script,
'the_div':div,
'chartform':chartform,
'teams':get_my_teams(request.user),
})
else:
return HttpResponse("Form is not valid")
if request.method == 'POST' and 'ids' in request.session:
chartform = BoxPlotChoiceForm(request.POST)
if chartform.is_valid():
plotfield = chartform.cleaned_data['yparam']
ids = request.session['ids']
request.session['ids'] = ids
workouts = [Workout.objects.get(id=id) for id in ids]
labeldict = {
int(w.id): w.__unicode__() for w in workouts
}
datemapping = {
w.id:w.date for w in workouts
}
fieldlist,fielddict = dataprep.getstatsfields()
fieldlist = [plotfield,'workoutid']
# prepare data frame
datadf = dataprep.read_cols_df_sql(ids,fieldlist)
datadf = dataprep.clean_df_stats(datadf,workstrokesonly=workstrokesonly)
datadf['workoutid'].replace(datemapping,inplace=True)
datadf.rename(columns={"workoutid":"date"},inplace=True)
datadf = datadf.sort_values(['date'])
script,div = interactive_boxchart(datadf,plotfield)
return render(request,'boxplot.html',
{'interactiveplot':script,
'the_div':div,
'chartform':chartform,
'teams':get_my_teams(request.user),
})
else:
return HttpResponse("invalid form")
else:
url = reverse(workouts_view)
return HttpResponseRedirect(url)
# List Workouts
@login_required()
def workouts_view(request,message='',successmessage='',