GEEKERY  
ADVENTURE  
CONTEMPLATION  

20160118

HDF5: where have you been all my life?

Nathaniel introduced me to HDF5 around the winter holidays (because, yes, that's the kind of thing we talk about while on vacation), but I just started using in in earnest this past week via h5py.  I may never go back to plain text data storage if I can help it—we'll see if I can convince you too.

This is a script that simulates a big matrix off random data, writes in both formats, and reads from both formats.  The syntax is similar for both.

To compare h5py against plain text, I ran the above (plus timing code added in) 100 times with different random data.  Here are the average results.

plain text
h5py
writing
9.44 sec
0.0634 sec
reading
7.94 sec
0.0051 sec
row access
3e-5 sec
6e-4 sec
col access
1e-6 sec
0.016 sec
file size
239 MB
77 MB

If your data is small enough or you need to access almost all of it repeatedly, plain text files might still be good for you.  I usually sample rows from large datasets that eat memory like chocolate cake. So for almost everything I do, h5py is the clear winner.

20150204

Allison's Law: "The mess has to go somewhere"

When I was a growing up, we had a standard of cleanliness in our house called "daddy-clean." My brother and I were asked regularly to clean are rooms, like most American children, but when we were done, mom would always ask: Is it daddy-clean?  This usually resulted in a second round of cleaning to make sure everything was out of sight.

There was a flaw to this paradigm, however, which was that daddy-clean only applied to things that were visible.  Thus, I learned the art of shoving everything under my bed, which had a convenient bedskirt to hide everything.  Toys, clothes, paper; everything went underneath.  When under-the-bed got full, the closet was my second choice.  Eventually my parents found out about this, due to an abundance of random objects poking out, but they allowed me my secret messes so long as they didn't get in the way of finding important things, which they occasionally did.

Nowadays my messes look a little different.  In addition to paper, I have more abstract things like source code.  And my experience growing up has taught me: the mess has to go somewhere.  Most of the time, this is just a trade off between different aspects of cleanness of an end product and time, but it applies in so many cases.

Consider the process of creating a user interface.  The mess can go into the source code; everything hacked together in an ugly mess underneath.  The mess could also go into the UI itself: bad design with beautifully easy implementation.  Or, the mess could be absorbed with lots of time to have pretty code and sleek presentation.

Or consider a different piece of software, like an operating system.  The mess could go into the kernel, into the user experience, or passed on to developers for that platform.  Or, again, the mess can be absorbed by lots of time and effort.

In my experience, the mess of the very pretty Mac OS is passed on to developers.  D3, with its steep learning curve and beautiful graphics, also passes the mess to programmers.  Easy-to-use and powerful libraries like ggplot2 for R probably put the mess in some combination of the under-the-hood code and time.

I've also been thinking about this in terms of (machine learning) model development.  Usually elegant models require an intense amount of time to polish into their perfected forms.

It's not always the right choice to absorb mess with time; sometimes a project isn't worth doing exceptionally cleanly.  I think it is always worth it, however, to consider where your mess will be going in order to make a measured choice.

20140130

Project Euler

I learned about Project Euler this weekend, and it's been addictive.  I binged at first, but it's a little unhealthy to do such similar things for fun and work; I need to stay balanced and keep off the computer in my down time.  I'm trying to only do one per day now, and go back to knitting and reading.  It's deepening my understanding of both math and programming, but I don't want to burn out too quickly.

20140121

spammer fail

NWC and I will often get research or student spam.  Today, he got one with a failure in the script, addressing him literally as "Dr {$name}."  Full message below the break.

20110117

not fast enough

Tonight I was working on processing data about a topic model on some 360 thousand documents from ASCII files into a database.  Bweh, what a task.  I started with a pretty naïve approach, and after getting it to work piecewise, I set it to run on the whole shebang.  After watching numbers fly by for a few minutes, I crunched some numbers and figured that it would take about 10 days to finish--not okay.  This was a piece of code that was pretty tailored to my task and would likely only ever be seen by me, and even then, only run this once; I didn't really want to sink a lot of time into it, but I'd like it to finish, in say, under 24 hours.

First was the problem of finding links to the documents.  Nature has this entire set online, but finding a link given a document id (doi) wasn't a find-and-replace task.  Take a look at this document on The Rockefeller Foundation (doi: 10.1038/147811a0), for instance.  Part of the doi is in the link, but there's also a volume number and one other number prefixed by n that makes the link unique.  And I didn't have those numbers, so I was querying nature for the doi and finding the pdf link on the search page (such as this).  Good for a handful of links, but not 360k times.  Turns out, I was able to find those numbers buried in my data, but it was pretty obscure.  Plug-n-play on the link took me down to 5 days.

Next, there were two types of data file about the documents, ones with a doi and an abstract per line and ones with all sorts of other info, including topic model data and also the doi, again one line per document.  The files themselves weren't one-to-one (there being 8 of one kind and 25 of the other), but they were one-to-one when it came to the relevant document lines: each doi had one line in each of the two file sets.  However, the organization wasn't intuitive and I wasn't about to pry through them by hand.  Instead, I noted that the matches occurred in groups, though again, not in any way that made logical sense that I could hard-code.  So instead of looking through all the other files for a match, I just had it stash the last-used file and look in that one first each time.  If it wasn't there, it looked in the others and updated the last-used file accordingly.  That took the expected run time down to under 24 hours, and I declared myself done for the night.

I guess the lesson learned was that even for simple pieces of code that are for private use and only to be used once, it's important to take the time to do things right instead of just the easiest/stupidest way possible.