jump to navigation

How to *not* name your octave scripts February 8, 2009

Posted by haskelladdict in Linux.
Tags: , , , , ,
trackback

Last week, I helped a co-worker debug one of her analysis scripts that used GNU octave. The script worked fine until one day it just stopped. Pretty quickly it turned out that a call to the std function which calculated the row-wise standard deviation of a fairly big matrix crashed for some unknown reason. The error messages that octave spewed at us were quite unhelpful so we were stumped. Since the size of the matrix had recently been doubled we tried pruning it down which did no good. I fired up another octave session in a different terminal, entered a small matrix by hand, tried to use std and …. it worked like a charm. So maybe something was wrong with the big matrix? After another 30 minutes we convinced ourselves that this was not the case and, furthermore, the script worked just fine on a different machine. Fair enough. So I moved the data plus script into /tmp and tried to run it there and – voila – it did its job without any problems.

Finally, it dawned on me what had happened. When octave executes commands either in the interactive shell or by running a script, it will look for the corresponding .m files in a number of directories (customizable by the addpath command) including the current directory. Importantly, it will (at least on my systems) try ‘.’ first. Hence, if your current directory contains a script foo.m it will overload octave’s foo command and instead execute whatever is in foo.m, which probably isn’t what you want at all. To make things more subtle, in our case the problem wasn’t even a std.m file that overloaded the real std, but rather a mean.m file that would overload the real mean which was called from somewhere inside std. After renaming my co worker’s version of mean.m (which, by the way did not really calculate the mean) everything was fine again and the original script worked as expected.

The moral of the story is that you should never choose names for your .m files that match actual octave commands unless you know what you are doing – or like surprises. Doing so will silently overload the real commands with your versions. Make it a habbit to always prefix your commands in an obvious manner, e.g. my_mean.m instead of mean.m or something similar.

Advertisements

Comments»

1. Cal - July 11, 2009

Namespace mistakes are among the most annoying to debug. Does Octave have no facility to emit a warning on name shadowing?

2. haskelladdict - July 12, 2009

Unfortunately, I am not aware of any warning facility for this shadowing issue. Since octave will just go through its path (which happens to start off with ./) to search for unknown symbols and use and execute the first matching function encountered it probably isn’t even aware of any possible additional matches somewhere later in the path. This could of course be solved by always scanning the full path but maybe the octave developers didn’t do it this way for efficiency reasons.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: