def dashboard(request, athleteId): print('getting athlete id...') athleteId = int(athleteId) #df = pd.read_pickle(str(path)+"/master_dfs/"+str(athleteId)+"masterDf.txt") #summaryDf = thresher.getSummaryDf(df) #summaryDf_json = summaryDf.to_json(orient="records") print('getting athlete object...') ath = Athlete.objects.get(athleteId=athleteId) print('getting run summary from athlete object...') summaryDf = pd.read_json(ath.runsSummary) # setting index again for use in rolling summaryDf = summaryDf.set_index(summaryDf.date) #print(summaryDf) summaryDf_json = ath.runsSummary # making weekly summary weekSummarySum = summaryDf[['climb', 'easy', 'stamina', 'recovery', 'impulse', 'realMiles', 'totalDist', 'totalTime']].resample('W', how='sum') # fix variation. this isn't a helpful number weekSummaryMean = summaryDf[['avgHr', 'variation']].resample('W', how='mean') weekSummary = pd.concat([weekSummarySum, weekSummaryMean], axis=1) #ws_json = weekSummary.to_json(orient='records') #print('weekSummary') #print(weekSummary) #### Summary by zone WEEKLY recovery = DataFrame({'x':weekSummary.index, 'y':weekSummary.recovery}) recovery = recovery.set_index(np.arange(len(recovery))) recoveryW_json = recovery.to_json(orient="index") # "index" used for stacked specific layout. easy = DataFrame({'x':weekSummary.index, 'y':weekSummary.easy}) easy = easy.set_index(np.arange(len(easy))) easyW_json = easy.to_json(orient="index") stamina = DataFrame({'x':weekSummary.index, 'y':weekSummary.stamina}) stamina = stamina.set_index(np.arange(len(stamina))) staminaW_json = stamina.to_json(orient="index") impulse = DataFrame({'x':weekSummary.index, 'y':weekSummary.impulse}) impulse = impulse.set_index(np.arange(len(impulse))) impulseW_json = impulse.to_json(orient="index") #### Summary by zone print('recovery') recovery = DataFrame({'x':summaryDf.date, 'y':summaryDf.recovery}) recovery = recovery.set_index(np.arange(len(recovery))) recovery_json = recovery.to_json(orient="index") # "index" used for stacked specific layout. print recovery_json print('easy') easy = DataFrame({'x':summaryDf.date, 'y':summaryDf.easy}) easy = easy.set_index(np.arange(len(easy))) easy_json = easy.to_json(orient="index") stamina = DataFrame({'x':summaryDf.date, 'y':summaryDf.stamina}) stamina = stamina.set_index(np.arange(len(stamina))) stamina_json = stamina.to_json(orient="index") impulse = DataFrame({'x':summaryDf.date, 'y':summaryDf.impulse}) impulse = impulse.set_index(np.arange(len(impulse))) impulse_json = impulse.to_json(orient="index") rList = list(recovery) eList = list(easy) sList = list(stamina) iList = list(impulse) stackedTester = [rList, eList, sList, iList] print stackedTester print('rolling') ### rolling rollDf = thresher.getRollingSummaryDf(summaryDf) rollDf =rollDf.fillna(0) rollingDf_json = rollDf.to_json(orient="records") rollDfLastFive_json = rollDf.sort_values(['date'], ascending=False)[:5].to_json(orient="records") print('rollrec') rollRec = DataFrame({'x':rollDf.date, 'y':rollDf.rollRec}) rollRec["y"] = rollRec.y + 100 rollRec = rollRec.set_index(np.arange(len(rollRec))) rollRec_json = rollRec.to_json(orient="index") print('rolleasy') rollEasy = DataFrame({'x':rollDf.date, 'y':rollDf.rollEasy}) rollEasy = rollEasy.set_index(np.arange(len(rollEasy))) rollEasy_json = rollEasy.to_json(orient="index") print('rollstam') rollStam = DataFrame({'x':rollDf.date, 'y':rollDf.rollStam}) rollStam = rollStam.set_index(np.arange(len(rollStam))) rollStam_json = rollStam.to_json(orient="index") print('rollimp') rollImp = DataFrame({'x':rollDf.date, 'y':rollDf.rollImp}) rollImp = rollImp.set_index(np.arange(len(rollImp))) rollImp_json = rollImp.to_json(orient="index") # dealing w fitlines #testLine = Activity.objects.get(act_id=535167099).fitline aWeekAgo = datetime.datetime.now() - datetime.timedelta(days=7) last7Days = summaryDf[summaryDf.date > aWeekAgo] dist7 = round(np.sum(last7Days.totalDist), 1) try: var7 = int(round(np.mean(last7Days.variation), 0)) # weight this average avgHr7 = int(round(np.mean(last7Days.avgHr), 0)) # weight this average except: var7 = 0.0 avgHr7 = 0.0 climb7 = int(round(np.sum(last7Days.climb), 0)) time7 = round(np.sum(last7Days.totalTime), 1) trainingEfficiency7 = (dist7 - np.sum(last7Days.stamina)) / (dist7 +1) #print "7DAYs" #print last7Days #print time7 ################################################ # for maf pts mafPts = summaryDf[['date','mafScore']].dropna().sort_values(['date'], ascending=False) mafPts_json = mafPts.to_json(orient="records") mafPts_smoothed = thresher.makeLws(df=mafPts, frac=.20) mafPts_smoothed['date'] = mafPts_smoothed.date / 1000000 #lws messed up the dates mafPts_smoothed_json = mafPts_smoothed.to_json(orient="records") #print mafPts_smoothed_json print "\n\n" print "mafPts" print mafPts # for maf dash lastFive = mafPts[:5] lastFive_json = lastFive.to_json(orient="records") ath.mafLastFive = lastFive_json ath.save(update_fields=['mafLastFive']) print "lastfive" print lastFive # current maf score currentMaf = str(np.round(lastFive.mafScore.iloc[0], 2)) athName = ath.name print('rendering...') print summaryDf """ taking these out of render package: 'tE7':trainingEfficiency7, 'dist7':dist7, 'var7':var7, 'avgHr7':avgHr7, 'climb7':climb7, 'time7':time7, 'rollingDf_json':rollingDf_json, 'rollRec_json':rollRec_json, 'rollEasy_json':rollEasy_json, 'rollStam_json':rollStam_json, 'rollImp_json':rollImp_json, 'fitlinesAll':ath.fitLines """ return render(request, 'stravaChimp/dashboard.html', {'athName':athName, 'stackedTester':stackedTester, 'r5':rollDfLastFive_json, 'summaryDf_json':summaryDf_json, 'recovery_json':recovery_json, 'easy_json':easy_json, 'stamina_json':stamina_json, 'impulse_json':impulse_json,'recoveryW_json':recoveryW_json, 'easyW_json':easyW_json, 'staminaW_json':staminaW_json, 'impulseW_json':impulseW_json, 'athleteId':athleteId, 'ath':ath, 'mafPts_json':mafPts_json, 'currentMaf': currentMaf, 'maf_json':lastFive_json, 'mafPts_smoothed_json': mafPts_smoothed_json, 'datesList':summaryDf['date'].to_json(orient='records')})
def dashboard(request, athleteId): print('getting athlete id...') athleteId = int(athleteId) #df = pd.read_pickle(str(path)+"/master_dfs/"+str(athleteId)+"masterDf.txt") #summaryDf = thresher.getSummaryDf(df) #summaryDf_json = summaryDf.to_json(orient="records") print('getting athlete object...') ath = Athlete.objects.get(athleteId=athleteId) print('getting run summary from athlete object...') summaryDf = pd.read_json(ath.runsSummary) # setting index again for use in rolling summaryDf = summaryDf.set_index(summaryDf.date) #print(summaryDf) summaryDf_json = ath.runsSummary # making weekly summary weekSummarySum = summaryDf[[ 'climb', 'easy', 'stamina', 'recovery', 'impulse', 'realMiles', 'totalDist', 'totalTime' ]].resample('W', how='sum') # fix variation. this isn't a helpful number weekSummaryMean = summaryDf[['avgHr', 'variation']].resample('W', how='mean') weekSummary = pd.concat([weekSummarySum, weekSummaryMean], axis=1) #ws_json = weekSummary.to_json(orient='records') #print('weekSummary') #print(weekSummary) #### Summary by zone WEEKLY recovery = DataFrame({'x': weekSummary.index, 'y': weekSummary.recovery}) recovery = recovery.set_index(np.arange(len(recovery))) recoveryW_json = recovery.to_json( orient="index") # "index" used for stacked specific layout. easy = DataFrame({'x': weekSummary.index, 'y': weekSummary.easy}) easy = easy.set_index(np.arange(len(easy))) easyW_json = easy.to_json(orient="index") stamina = DataFrame({'x': weekSummary.index, 'y': weekSummary.stamina}) stamina = stamina.set_index(np.arange(len(stamina))) staminaW_json = stamina.to_json(orient="index") impulse = DataFrame({'x': weekSummary.index, 'y': weekSummary.impulse}) impulse = impulse.set_index(np.arange(len(impulse))) impulseW_json = impulse.to_json(orient="index") #### Summary by zone print('recovery') recovery = DataFrame({'x': summaryDf.date, 'y': summaryDf.recovery}) recovery = recovery.set_index(np.arange(len(recovery))) recovery_json = recovery.to_json( orient="index") # "index" used for stacked specific layout. print recovery_json print('easy') easy = DataFrame({'x': summaryDf.date, 'y': summaryDf.easy}) easy = easy.set_index(np.arange(len(easy))) easy_json = easy.to_json(orient="index") stamina = DataFrame({'x': summaryDf.date, 'y': summaryDf.stamina}) stamina = stamina.set_index(np.arange(len(stamina))) stamina_json = stamina.to_json(orient="index") impulse = DataFrame({'x': summaryDf.date, 'y': summaryDf.impulse}) impulse = impulse.set_index(np.arange(len(impulse))) impulse_json = impulse.to_json(orient="index") rList = list(recovery) eList = list(easy) sList = list(stamina) iList = list(impulse) stackedTester = [rList, eList, sList, iList] print stackedTester print('rolling') ### rolling rollDf = thresher.getRollingSummaryDf(summaryDf) rollDf = rollDf.fillna(0) rollingDf_json = rollDf.to_json(orient="records") rollDfLastFive_json = rollDf.sort_values( ['date'], ascending=False)[:5].to_json(orient="records") print('rollrec') rollRec = DataFrame({'x': rollDf.date, 'y': rollDf.rollRec}) rollRec["y"] = rollRec.y + 100 rollRec = rollRec.set_index(np.arange(len(rollRec))) rollRec_json = rollRec.to_json(orient="index") print('rolleasy') rollEasy = DataFrame({'x': rollDf.date, 'y': rollDf.rollEasy}) rollEasy = rollEasy.set_index(np.arange(len(rollEasy))) rollEasy_json = rollEasy.to_json(orient="index") print('rollstam') rollStam = DataFrame({'x': rollDf.date, 'y': rollDf.rollStam}) rollStam = rollStam.set_index(np.arange(len(rollStam))) rollStam_json = rollStam.to_json(orient="index") print('rollimp') rollImp = DataFrame({'x': rollDf.date, 'y': rollDf.rollImp}) rollImp = rollImp.set_index(np.arange(len(rollImp))) rollImp_json = rollImp.to_json(orient="index") # dealing w fitlines #testLine = Activity.objects.get(act_id=535167099).fitline aWeekAgo = datetime.datetime.now() - datetime.timedelta(days=7) last7Days = summaryDf[summaryDf.date > aWeekAgo] dist7 = round(np.sum(last7Days.totalDist), 1) try: var7 = int(round(np.mean(last7Days.variation), 0)) # weight this average avgHr7 = int(round(np.mean(last7Days.avgHr), 0)) # weight this average except: var7 = 0.0 avgHr7 = 0.0 climb7 = int(round(np.sum(last7Days.climb), 0)) time7 = round(np.sum(last7Days.totalTime), 1) trainingEfficiency7 = (dist7 - np.sum(last7Days.stamina)) / (dist7 + 1) #print "7DAYs" #print last7Days #print time7 ################################################ # for maf pts mafPts = summaryDf[['date', 'mafScore']].dropna().sort_values(['date'], ascending=False) mafPts_json = mafPts.to_json(orient="records") mafPts_smoothed = thresher.makeLws(df=mafPts, frac=.20) mafPts_smoothed[ 'date'] = mafPts_smoothed.date / 1000000 #lws messed up the dates mafPts_smoothed_json = mafPts_smoothed.to_json(orient="records") #print mafPts_smoothed_json print "\n\n" print "mafPts" print mafPts # for maf dash lastFive = mafPts[:5] lastFive_json = lastFive.to_json(orient="records") ath.mafLastFive = lastFive_json ath.save(update_fields=['mafLastFive']) print "lastfive" print lastFive # current maf score currentMaf = str(np.round(lastFive.mafScore.iloc[0], 2)) athName = ath.name print('rendering...') print summaryDf """ taking these out of render package: 'tE7':trainingEfficiency7, 'dist7':dist7, 'var7':var7, 'avgHr7':avgHr7, 'climb7':climb7, 'time7':time7, 'rollingDf_json':rollingDf_json, 'rollRec_json':rollRec_json, 'rollEasy_json':rollEasy_json, 'rollStam_json':rollStam_json, 'rollImp_json':rollImp_json, 'fitlinesAll':ath.fitLines """ return render( request, 'stravaChimp/dashboard.html', { 'athName': athName, 'stackedTester': stackedTester, 'r5': rollDfLastFive_json, 'summaryDf_json': summaryDf_json, 'recovery_json': recovery_json, 'easy_json': easy_json, 'stamina_json': stamina_json, 'impulse_json': impulse_json, 'recoveryW_json': recoveryW_json, 'easyW_json': easyW_json, 'staminaW_json': staminaW_json, 'impulseW_json': impulseW_json, 'athleteId': athleteId, 'ath': ath, 'mafPts_json': mafPts_json, 'currentMaf': currentMaf, 'maf_json': lastFive_json, 'mafPts_smoothed_json': mafPts_smoothed_json, 'datesList': summaryDf['date'].to_json(orient='records') })
import statsmodels.api as sm import matplotlib.pyplot as plt import thresher from math import radians, cos, sin, asin, sqrt #urllib3.contrib.pyopenssl.inject_into_urllib3() df_summary = pd.read_pickle("runsSummary.txt") df_master = thresher.basicClean(pd.read_pickle("master_dfs/10319226masterDf.txt")) maf = df_summary[['date', 'mafScore']] print maf maf_smoothed = thresher.makeLws(df=maf, frac=.20) print maf_smoothed fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(maf_smoothed.date, maf_smoothed.mafScore, c='blue', alpha=0.25) #ax.scatter(df[x], df[y2], c='red', alpha=0.25) #ax.plot(df['speedLwsX'], df['speedLwsY'], c='blue') #ax.plot(lwsHatX, lwsHatY, c='red') #ax.set_title(str(x)+' '+str(y)) #ax.set_ylabel(str(y)) #ax.set_xlabel(str(x)) #ax.set_ylim([0,6]) #ax.set_xlim([0, 1500]) #ax.legend([y, y2]) plt.show()
from pandas import DataFrame, pivot_table import numpy as np import statsmodels.api as sm import matplotlib.pyplot as plt import thresher from math import radians, cos, sin, asin, sqrt #urllib3.contrib.pyopenssl.inject_into_urllib3() df_summary = pd.read_pickle("runsSummary.txt") df_master = thresher.basicClean( pd.read_pickle("master_dfs/10319226masterDf.txt")) maf = df_summary[['date', 'mafScore']] print maf maf_smoothed = thresher.makeLws(df=maf, frac=.20) print maf_smoothed fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(maf_smoothed.date, maf_smoothed.mafScore, c='blue', alpha=0.25) #ax.scatter(df[x], df[y2], c='red', alpha=0.25) #ax.plot(df['speedLwsX'], df['speedLwsY'], c='blue') #ax.plot(lwsHatX, lwsHatY, c='red') #ax.set_title(str(x)+' '+str(y)) #ax.set_ylabel(str(y)) #ax.set_xlabel(str(x)) #ax.set_ylim([0,6]) #ax.set_xlim([0, 1500]) #ax.legend([y, y2]) plt.show()