added stats
This commit is contained in:
@@ -5,6 +5,19 @@ from __future__ import unicode_literals
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
from rowers.views.statements import *
|
||||
|
||||
from jinja2 import Template,Environment,FileSystemLoader
|
||||
|
||||
def floatformat(x,prec=2):
|
||||
return '{x}'.format(x=round(x,prec))
|
||||
|
||||
|
||||
env = Environment(loader = FileSystemLoader(["rowers/templates"]))
|
||||
env.filters['floatformat'] = floatformat
|
||||
|
||||
|
||||
from django.contrib.staticfiles import finders
|
||||
|
||||
|
||||
# generic Analysis view -
|
||||
|
||||
defaultoptions = {
|
||||
@@ -458,11 +471,77 @@ def histodata(workouts, options):
|
||||
script = ''.join(scripta)
|
||||
|
||||
return(script,div)
|
||||
|
||||
def statsdata(workouts, options):
|
||||
includereststrokes = options['includereststrokes']
|
||||
spmmin = options['spmmin']
|
||||
spmmax = options['spmmax']
|
||||
workmin = options['workmin']
|
||||
workmax = options['workmax']
|
||||
ids = options['ids']
|
||||
userid = options['userid']
|
||||
plotfield = options['plotfield']
|
||||
function = options['function']
|
||||
|
||||
workstrokesonly = not includereststrokes
|
||||
|
||||
ids = [w.id for w in workouts]
|
||||
|
||||
datamapping = {
|
||||
w.id:w.date for w in workouts
|
||||
}
|
||||
|
||||
fieldlist,fielddict = dataprep.getstatsfields()
|
||||
|
||||
# prepare data frame
|
||||
datadf,extracols = dataprep.read_cols_df_sql(ids,fieldlist)
|
||||
|
||||
datadf = dataprep.clean_df_stats(datadf,workstrokesonly=workstrokesonly)
|
||||
|
||||
# Create stats
|
||||
stats = {}
|
||||
fielddict.pop('workoutstate')
|
||||
fielddict.pop('workoutid')
|
||||
|
||||
for field,verbosename in fielddict.items():
|
||||
thedict = {
|
||||
'mean':datadf[field].mean(),
|
||||
'min': datadf[field].min(),
|
||||
'std': datadf[field].std(),
|
||||
'max': datadf[field].max(),
|
||||
'median': datadf[field].median(),
|
||||
'firstq':datadf[field].quantile(q=0.25),
|
||||
'thirdq':datadf[field].quantile(q=0.75),
|
||||
'verbosename':verbosename,
|
||||
}
|
||||
stats[field] = thedict
|
||||
|
||||
# Create a dict with correlation values
|
||||
cor = datadf.corr(method='spearman')
|
||||
cor.fillna(value=0,inplace=True)
|
||||
cordict = {}
|
||||
for field1,verbosename in fielddict.items():
|
||||
thedict = {}
|
||||
for field2,verbosename in fielddict.items():
|
||||
try:
|
||||
thedict[field2] = cor.loc[field1,field2]
|
||||
except KeyError:
|
||||
thedict[field2] = 0
|
||||
|
||||
cordict[field1] = thedict
|
||||
|
||||
context = {
|
||||
'stats':stats,
|
||||
'cordict':cordict,
|
||||
}
|
||||
|
||||
htmly = env.get_template('statsdiv.html')
|
||||
html_content = htmly.render(context)
|
||||
|
||||
return('',html_content)
|
||||
|
||||
def boxplotdata(workouts,options):
|
||||
|
||||
|
||||
includereststrokes = options['includereststrokes']
|
||||
spmmin = options['spmmin']
|
||||
spmmax = options['spmmax']
|
||||
@@ -570,6 +649,8 @@ def analysis_view_data(request,userid=0):
|
||||
script, div = histodata(workouts, options)
|
||||
elif function == 'flexall':
|
||||
script,div = flexalldata(workouts,options)
|
||||
elif function == 'stats':
|
||||
script,div = statsdata(workouts,options)
|
||||
else:
|
||||
script = ''
|
||||
div = 'Unknown analysis functions'
|
||||
|
||||
Reference in New Issue
Block a user