Posts

Showing posts with the label PERL

fun with PERL Math::Matrix

Wanting to do some matrix math, I installed the Math::Matrix module from CPAN . There's some nice instructions on installing CPAN modules on about.com . A common tool people use for matrix manipulation is MATLAB . I'm deeply embarrassed I have virtually no skills in MATLAB. However, this module is pretty cool. What makes it so nice it is uses overloading of the basic mathematical operators. So that makes it easy to add, subtract, and multiply matrices, as well as a string conversion when it's accessed in a scalar context. Here's an example: use Math::Matrix; use strict; my $a = new Math::Matrix ([1, 0, -1], [-1, 1, 0], [0, -1, 1]); warn("a =\n$a\n"); my $at = $a->transpose; warn("at =\n$at\n"); warn("at * a =\n", $at * $a, "\n"); The result: a = 1.00000 0.00000 -1.00000 -1.00000 1.00000 0.00000 0.00000 -1.00000 1.00000 at = 1.00000 -1.00000 0.00000 0.00000 1.00000 -1.0000...

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...

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 ...

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...

San Francisco Giants 2011 vs 2010: luck or skill?

The San Francisco Examiner devotes more than 1/3 of its content to sports, and of course a popular subject is the San Francisco Giants, who won the Major League Baseball championship last year. During the season in 2010, they won 92 games and lost 70 , a 56.8% winning fraction. As of last Friday they are 12 wins and 12 losses, only a 50% win fraction. Obviously, one concludes from reading the headlines, something has gone terribly wrong. The issue is I've not read past the headlines. Maybe there's compelling arguments made for how the team is playing. But invariably in the analysis of baseball and everything else, there is a lack of appreciation for the statistics of random numbers. Baseball games aren't fully random events, but there is clearly a random component to them. I think everyone recognizes that luck is a big factor. So a quick test: I'm going to assume the Giants had luck on their side last year, since they won the division (and ended up going on...

Garmin FIT activity splitter to eliminate large time gaps

I've gotten useful code on my third project for processing FIT files using Kiyokazu Suto 's Garmin::FIT package for Perl. First, I described fit_to_cols , which extracted selected data from FIT files and formatted it in a space-delimited file. Next I described fit_filter_motor_segments which attempted to identify segments where the Garmin was accidently left running in a fast car or train. That project's still being refined, as I described last post. Here is perhaps the most useful of the three, fit_split_on_gaps , which finds gaps of some specified minimum duration (default 8 hours, or otherwise specified with the -tgap option) and splits the FIT data into multiple sub-files at any gaps found which meet or exceed this threshold. This and the other codes can be found here, on Google Docs . It's fairly common in my experience to forget to "reset" my Garmin between activities, despite having set it to warn me at the start of a ride if I have not. Gol...

Garmin FIT motorized segment filter in Perl

Finally, squeezing in work in my train commute between San Francisco and Mountain View (which unfortunately hasn't been a bike commute as much as I'd like due to the pressures of two big projects at work, but digression opportunities ate limited in the middle of a sentence), I've managed to finish a working draft of my "motorized segment filter" in Perl. Like the fit_to_cols code I described recently, this code uses Kiyokazu Suto 's Garmin::FIT package for Perl. Unlike fit_to_cols, this one needs to be able to write as well as read FIT data. The Garmin:FIT perl module allows this, but to figure out how you really need to dig into the provided example, fitsed . Fitsed is uncommented and does a lot more than just read and write FIT files: it also has a parser for selecting and/or changing fields in records. All good stuff, but a simple equivalent of hello.c , an example which minimally demonstrates writing FIT data, would have been useful. But I worked i...

a Perl Garmin-FIT to space-delimited table converter

I've been working on a code to filter motorized segments from FIT files, using Kiyokazu Suto 's Garmin::FIT package for Perl. However, to help with that work, I needed a way to quickly plot the data in a FIT file. So I wrote a script, fit_to_csv , to create a readable table from FIT data. The code produces a space-delimited file with each column describing a field either contained in the FIT file or derived from data in one or more fields of the FIT file. I have a series of scripts for handling such files and generating plots from them, or they can be trivially loaded into a spreadsheet like oocalc or even Excel , or almost any other plotting package, using the "CSV" format. When importing the CSV, make sure spaces and not commas are selected as the delimiter. The code's available here . On Linux, simply save the file, make sure it has executable permission, then run it in the standard Linux fashion. On Windows, well, you're on your own. I avoid Wi...

matching names on Low-Key registration: Lingua::EN::MatchNames

One issue with the Low-Key Hillclimbs this year is we've gone to an on-line "RSVP" system where riders fill in a form indicating their plan to be at the climb. In this way, we can make sure the number attending doesn't exceed the capacity of the roads to support our "traffic". We can also better plan for how much food we need to purchase, and there's less opportunity for errors in recording names and numbers in a rushed registration area. It also saves a lot of time entering rider info: the riders do that for us so we simply export the Google spreadsheet where the data are stored. All good. But on the downside riders register by name, rather than by number, as was our previous model. Sure, we could have riders simply enter their number, but it is too easy to make a mistake with a number. People tend to be fairly well practiced at typing their names, on the other hand. But names aren't always typed exactly the same. For example, Steve may ...