Posts

Showing posts with the label programming

recursive course timing for Low-Key Hillclimbs week 4: Portola Valley Road

There were a number of challenges with organizing the Portola Valley Hills Low-Key Hillclimbs . For example, the Edge 500 GPS quality on Joaquin Road. But one of the more interesting was the scoring conundrum represented by a rider who repeated one of the climbs. The scoring code I used here was originally developed for Kennedy Fire Road last year . There we had a long climb with a number of intermediate check-points. To get an overall time I assigned a time to each checkpoint, but overall time was essentially the time crossing the finish minus the time crossing the start. If the rider completed the course multiple times I'd keep track of times between the start and finish and take the shortest one. If a rider recrossed the start line I'd start over. If he crossed the finish line I'd ignore everything until he recrossed the start line. It was easy. For Portola Valley Hills I added in the cocnept of time budgets for time segments between checkpoints. If a rider ...

Hacking the Sudoku solver from The Ruby Programming Language

Image
On only page 18 of Flanagan and Matsumoto's excellent "The Ruby Programming Language" a relatively complex example is presented: a Sudoku puzzle solver. I've had a decades-long interest in maze solvers, and this tied in nicely with that. I resolved to give the code a close look when I'd finished the book. And surprising myself, I soon did. I read it cover-to-cover, frequently even re-reading sections I'd already covered when a lack of mastery of specific material became evident later. The language is a nice alternative to Perl, which had been my scripting language of choice since 1998. Ruby has a lot of cool characteristics, even if it sometimes falls weakness to Perl's desire to appease too much diversity in coding styles. I personally think being able to do things effectively two different ways is inferior to being able to do things as effectively only one, as the latter makes multi-author code easier to follow and coherently maintain. But as usu...

OLH power analysis

Image
I've neglected my blog. I admit it. Hopeless neglect. It's not for a shortage of material: I've had event reports, analyses, and opinions ready to go but abandoned due to an obsessively dedication to coding various Strava projects. Any project has a deadline, written or otherwise. Get it done or other priorities will displace it, it gets pushed to the back burner of good intentions, and there it will congeal, momentum lost, trajectory forgotten. I desparately didn't want that to happen, so I've been trying to squeeze every available snippet of time into getting the thing up and running. Unfortunately "snippets of time" aren't the best way to make coherent progress on tasks of any complexity. A day or two of dedicated work is far better than even a month of 30 minute bursts. It's why the "hack-a-thons" are popular: 24 hours straight of total focus can actually accomplish something. So I've not gotten past the unanticipated hur...

Installing xgraph on Mac OS/X Lion

Image
After years on Thinkpads running Linux, I decided to take the plunge on an Apple Mac Air. Macs have always had a premium price for the hardware, and I never considered the cost justified, since you're buying a lot of software with that money and I have all the software I need with Linux. But the deal with the Macs is they simply work, and the Air is a beautifully designed machine. After searching for a quick data plotting application, I was frustrated to find I still couldn't find anything which beat the old tried-and-true: xgraph , written at Berkeley in the 1980's It produces nice plots, data sets clearly differentiated in nice colors against a neutral grey background, with a super-simple data format trivially processed with scripting. The code's beauty is its simplicity and it's excellent defaults: no widgets or pull-down menus to deal with. At work I can tell people think I'm a bit strange for using such ancient code, but I get the last laugh when I to...

fun with JavaScript

Image
After adventures in Perl, then Java, then Ruby, I've been immersed in JavaScript, the language of web UI's. Writing command line apps is straightforward. For example if the user wants to analyze a specific ride, include the ride ID as a command line argument. Done. Now get on to the juicy data analysis. But with a user interface, things are much more complex. A bunch of my Strava app ideas begin the same: user specifies a ride, app maps the ride for user reference, various analyses are done and/or actions are taken. In the Strava API some methods require an authorization key from logging in, while others do not. Among those that do is the method which returns a reduced set of coordinates for a route to allow mapping without transferring the full set of data for the ride. This is important for maps, because it substantially reduces the bandwidth requirement, not only loading the data from Strava, but then subsequently uploading it to Google for map generation. And obv...

Slow going on coding project

I've embraced Ruby for my coding project. Ruby has been great: I really like it's object-oriented nature: it's inherently object-oriented, with that built in to the core of the language, rather than some sort of kludge built onto what is essentially a sequential language. Actually, I'm not sure whether "object" versus "sequential" is correct, here, since Ruby is still highly sequential, it just heavily uses objects. You can still write Ruby code which looks much like Perl code, with notable exceptions. But I don't want to write Ruby code which looks Perlish. I want it to look Rubyish. So therein is what makes for slow going: I write stuff, check it, re-read the on-line documentation to make sure I'm not reinventing any wheels (there are a lot of standard methods in Ruby), make adjustments, run, and track down the inevitable bugs. Slow going, but it's what it takes to build aptitude in a language. So my end-of-Feb deadline for t...

Ruby SmoothData class: now static-free

Last time I posted what I called a "SmoothData" class for Ruby. But even as I was hitting "publish" on Blogger I realized I wasn't being honest. There wasn't much "class" about it: it was a single static method which was similar to what one would write for any sequential procedural language. Indeed, since it was essentially ported from Perl, this shouldn't be a surprise. Initially my attitude was "this function doesn't need to preserve any local state. There doesn't need to be multiple instances of it. It's just an algorithm an nothing more. Why make life too complicated just for a dogmatic adherence to the object oriented cult?" But then I tried to use it and I realized... hmm... maybe I should reconsider. The plan was to, for various measured Strava parameters (altitude, speed, power) to have both measured values and smoothed values. So, for example, for altitude, I'd have an altitude array and a smoothed al...

SmoothData : a simple Ruby data smoothing class

Ruby self-teach continues, so I put together a data smoothing class for Ruby. Nothing new here: another in a series of codes I've used for comparing Java to Perl to Ruby. Data smoothing is an important part of anything I do with cycling data. So this is a critical component for me on any sort of project I might want to accomplish. For example, suppose I wanted to write a code to identify and rate climbs in an activity. I know -- Strava already does this, but I've got my own ideas about algorithms. Well, if I were to take altitude data raw, a small glitch yielding a 50% grade between two closely-spaced points might generate a huge spike in the climb rating for just those two points. Instead it would be important to smooth the altitude out, for example with a 50 meter characteristic smoothing distance. 50 meters is a fairly good number on climbs this length and shorter you can generally use momentum to blunt the blow. It also covers up small errors in position and altitu...

StravaToCSV : It's Ruby's turn

StravaToCSV has become my "test app" for various programming languages ( Perl , Java , and now Ruby). And for that it works fairly well: I need to process command line arguments, open an HTTP connection to Strava, download JSON data, convert it, then then output it as CSV. So there's a decent amount there. This project went much smoother than my Java implementation. It was fairly quick, taking a bit longer when I wanted to avoid the program imploding when it was fed a bad activity specification. This version takes as its only command line arguments activity numbers (for full activities) or activity-segment pairs, where the activity number is separated from the matched segment by a "#". It will sequentially load each of these, outputting a CSV stream with the header determined by what fields it finds in the first non-empty activity. It adds the activity number as the first column of the CSV stream. At first I expected the JSON library to have some sort ...

Perl, Java, Ruby

Well, I realized the Pine Flat Road Race was tomorrow (Sunday) not today (Saturday) and so, had I rested today, I probably could have gone. Ah, well. I have a rule that I need to do a hard group ride before I even think about racing, and I haven't yet done so this year, or even towards the end of last year. No, the Friday Noon Ride is not a "hard group ride".... nor is Wednesday. Only Tue and Thu come close to qualifying. So it probably wouldn't have been prudent trying to mix it up at Pine Flat, anyway. But on the positive side, that's 6-7 hours I will not be spending in a car, and more time I won't spend in a smelly motel room, more time to devote to my coding project. On Facebook, it was recommended I look at Ruby. This was an obvious thing to do, since it is what Strava uses for its API example . However, I had gravitated towards Java because with Java I knew I could generate an Android app. All it takes is a bit of Googling to note that Android a...

Setting Emacs tab stops for Java

I have a problem with Java code: with classes, methods, exception blocks, loops, etc, there's a lot of indentation. Especially when I'm posting code to this blog, with its narrow text column, things quickly flow off the right side of the page. And it's so unnecessary. I don't think 4-column tab stops, which I was using, is needed to produce readable code. The key to solve this is in the following file on my Linux system: ~/.emacs.d/init.el This contains "elisp" commands which are executed upon emacs start-up. Elisp is the Emacs variant of LISP. Honestly, even though I am good with basic MIT Scheme, and also the Elk Scheme I use at work, I find Elisp fairly challenging. But Google is my friend, so I inserted the following: ;Basic unit of spaces for each indentation level. ; c-basic-offset: sets offset for "C-class" languages ; I specifically set it smaller in Java, which tends to have extra indents ; with its class structure. (setq c-basic...

mired in Java

So, while this weekend was largely consumed with The MegaMonster Enduro , my quest to become a Java master continues. The Megamonster went okay. It's a long trip down to Paicines: I am in Howard's debt for the ride. And since I have the Low-Key "stuff", it made for a full car. I didn't really help much while there: this year I rode. I did the 100-miler for the first time in 3 years, the preceding two not having the fitness for it. I felt fairly good, but I was slow, taking 5:35 for the course. It's not easy: wind, hills, and this year light rain. I don't think the rain was much of a factor but the wind always is. Anyway, the big deal is doing the results after. I always have a hard time with Megamonster results: for each rider we have three checkpoint times, a start time, and a finish time. I'm prone to making typos, and my scoring scripts only get used once per year, so it's never as smooth as I want. I think things are in fairly good ...

implementing StravaToCsv in Java

In an earlier post I wrote how I put together a "Strava_to_CSV" code using Perl in maybe half an hour. It was pretty easy once I'd installed the Perl JSON libary. But my goal here is Java, not Perl, so I decided to try to write a similar code in Java. Well, I did it, but it took a lot longer than a half an hour (maybe 5 hours total) and it's a lot longer. Of course, I know Perl fairly well, while Java I'm trying to learn, so the time is an unfair comparison. Still, in the Java there's stuff to look after. I won't share the full code here, but will summarize the key points. First, to get the data from Strava, I once again used their "streams" method from version 1 of the API: System.err.println("processing Strava activity # " + stravaToCsv.activities[n]); String urlString = "http://app.strava.com/api/v1/streams/" + stravaToCsv.activities[n]; System.err.println("accessing ride data via URL = " + urlString); j...

update on Java coding project

Image
While it seems at times my early progress on my Android/Strava app project has stagnated, really I've gotten a decent amount done. On the computer side, I had to upgrade the Ubuntu on my Thinkpad T60 from 9.10 to 11.10 to allow for the installation of current packages. This was inhibited by an inability to access my CD drive, but a bit of isopropyl alcohol blown off with a blast of dry compressed air (borrowed from the tech support at work... shh!) on the sensitive bits seemed to fix that. As with my previous Ubuntu upgrade, I lost a lot of packages in the shuffle, even things as fundamental as emacs, but as these omissions are encountered I reinstall them. Things are basically up and running now. Second, I gave in and ordered a Mac Air. The Thinkpad is in a fairly sorry state at this point, and I really think the Mac will provide a better machine for my needs. I'll get that in another week. Then education: The Oracle (formerly Sun) Java Tutorial is quite good. I admi...

simple Strava to CSV ride decoder with Perl

Slight detour from my project... Just wanted confidence I could decode JSON data, at least in Perl. I wrote a little decode for Strava ride data using Perl. The code, which I call "Strava_to_csv", requires a command line option specified as follows: Strava_to_csv -activity activity-number where activity-number is the number of the activity. It's nothing fancy, and not very robust. It expects all data to be scalar except for "latlng", which is an array of two numbers, the first the latitude, the second the longitude. It worked for me. This code uses the JSON module version 2.53 from CPAN. There's a lot of stuff there about encoding type, but since Strava is unicode, it worked simply. The code is written for Linux and maybe OS/X: it uses a system call to the "wget" command to download the URL (a Perl package for HTML could be used instead). Anyway, here's my code. This hardly took any effort at all, maybe 30 minutes, which makes m...

writing my Strava Android app, part 2

More progress... First, I did more sketch work on my proposed pages for the app. One of these involves data plots: plotting the altitude, and on a separate plot, speed from a Strava activity on a graph. This is the biggest challenge of my GUI design: the rest consists of a text widget, a bunch of button widgets, and some labels, with more simple widgets in the "configuration" screen. Plots with limited pixels are a challenge: I believe the screen resolution is only 480 × 800 for which only a small subset, for example 400 × 100, will be available for each plot. Not so bad, actually, but real estates needs to be used efficiently. I'll avoid doing anything fancy: no scrolling or zooming, for example. The goal is just to identify portions of the ride in a clearly identifiable fashion. For this I can either do my own plots with graphic primitives or use a more general purpose plotting package. I'm not yet decided on this. Second, I looked into the Strava API itse...

writing my Strava app: part 1

The first step in writing my Strava app was to make a note of exactly what I wanted it to do. Well, perhaps not exactly: I don't know the capabilities yet of the Strava API. In particular, does the Strava API allow you to replace the data on a ride? Apparently it does, since StravaHaHa allows modification of ride data. Honestly I don't know where the API is documented, but I am deferring that until later. No rush. So then when I knew what I wanted the program to do, I set out to design some pages. So I measured the screen on my HTC Incredible phone and found it was 5 cm wide by 8 cm high. So I set up an image file using GIMP with 500 pixels wide by 800 pixels high, and for each screen, began "sketching out" how I wanted the page to look. It's different assembling a page from a widget tool kit, so details aren't critical here: I just the look and feel of each page. When I got tired of doing that, and I was on-line, I started downloading some of the s...

writing an Android app

I've long wanted to write an Android app so now seems a fairly good time to give that a shot. I want to port over some Perl stuff I've done, not directly necessarily, probably in Java, which I've done some work in before but long ago. I really liked Java, though: a proper object oriented environment rather than the layered kludge-fest which is C++, or for that matter "object-oriented Perl", neither of which I've ever liked. I end up with hybrid object oriented - sequential code. Better to have the environment optimized for the object oriented paradigm and stick with that. Step one didn't go too well... upgrade my Ubuntu to the 11.10, but then my CD drive on my old Thinkpad isn't working well. I tried to clean it but now it's working worse... Anyway, I got a book on the subject ( Android Applications for Dummies ), but am not waiting for that to arrive. There's plenty of on-line reference material and tutorials. Book form is just more ...