Message Boards Message Boards

1
|
3379 Views
|
0 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Running Streak Counter

Hello everyone!

After I finished reading An Elementary Introduction to Wolfram Language earlier this year, I was so excited to try out something that would be useful in my own life. Before I get into what I did, I'll just give a little background.

I have been running for several years now, and in September of 2017 I decided that I would start a "run streak." A running streak is defined as running at least 1 mile (1.61km) every calendar day consecutively. I did this mostly because I found that I couldn't stick to only running 3 or 4 days a week - I would suddenly find myself not having run for several weeks or months. A little while before I started my running streak, I began using RunKeeper to track my runs. I would go to the RunKeeper website and copy all my running data to an excel spreadsheet to see how I was doing by hand.

I knew there had to be a better way, and once I was introduced to Wolfram Language I was sure I could do much better!

First, I used ServiceConnect and retrieved my running data from my last 1250 days (The service returns running data in chunks of 25 once given a date) using NestList

runkeeper=ServiceConnect["RunKeeper","New"]
runkeeper["UserData"]["Name"] (*Display Username after logging in to RunKeeper, just to make sure*)
data=runkeeper["FitnessActivities","NoLaterThan"->#]&/@NestList[DayPlus[#,-25]&,Today,50]

Great! But to do cool things with the data it needs to be cleaned up a bit since there are empty entries which will return errors, and the data is a list of associations which isn't nice to deal with.

cleandata[d_]:=ArrayFlatten[d//.{}->Sequence[],1]
dataset=Dataset[cleandata[data]]

People often record other activities using RunKeeper like swimming, biking etc. But only running should be included in any calculations so that needs to be defined.

runningdataset=dataset[Select[#Type\[Equal]"Running"&]]

Here's a sample of what my running data looks like today: enter image description here

Now, dealing with time is a pain. I spent the last two summers in Germany, and most people have run in a different time zone at least a few times. Dealing with this programatically gave me a bit of trouble, but I settled on taking the AbsoluteTime of the datetime (AbsoluteTime returns the total number of seconds since the beginning of January 1, 1900) Then, I wanted to find the current running streak with the criterea that each run must have occured within 2 days (172 000s) of each other to count.

absolutesdata[ds_]:=AbsoluteTime/@Query[All,#StartTime&]@ds
differencesdata[ds_]:=#*-1&/@Differences[ds]
removesduplicates[ds_]:=Select[ds,#>3600&] (*Function that removes activities that were started  less than 1 hour of each other*)

currentstreaks[ds_]:=LengthWhile[removesduplicates[differencesdata[absolutesdata[ds]]],#<172000&]+1

(Plus one because we are counting the number of differences, so there will be one missing unless we add one)

Finally! The moment I spent so long trying to get to - a variable that contains my current streak number and a dataset of runs in my current streak:

currentstreak=currentstreaks[runningdataset]
currentstreakdataset=Query[1;;currentstreak,All]@runningdataset

My currentstreak=590 days!

Right now this code doesn't test for the >1 mile criterea, but that's something that I can easily include in a later version. I did a couple other things with my data, like visually representing the distance of each run on a plot and returning a dataset of runs that are longer than a certain distance, but I think I'll save that for another post. For something so simple I'm really happy with it!

I would like to deploy this code by making a little web app that anyone can use. Unfortunately I immediately hit a stumbling block! I can't figure out how to make ServiceConnect work using ExportForm/FormFunction. I tried making a little button that you can click to use ServiceConnect but it didn't seem to work. Any help on how to deploy this code would be so great! I think it would be really cool to share this with the running community.

Thanks for reading! I'm going to be attending the Wolfram Summer School this year, and I'm looking forward to meeting some people who read this post in person!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract