Category Archives: Geekery (Tech)

Various aspects of manipulating solid state physical devices to do “neat” things.

Use Python to time c++ binaries from the command line

Often I want to benchmark c++ code to try to understand if I’ve made things faster or slower. Though I don’t doubt there are possibly better and possibly more accurate measures out there, I have yet to find a straightforward and clear answer to this question. But this may be trivially easy in Python, using timeit.

This assumes you have Python installed (of some compatible version, this is only tested on 2.7). Assuming your c++ binary name is called “a.out” and is located in the directory you’re currently in, you can run:

$ python -m timeit -n 1 'import subprocess;"./a.out", shell=True)'

What I know:
0. Respect the quotes.
1. The option “-n 1” tells you the number of times to run the function. n=1 appears to run the function 3 times and report the best time of those 3.
2. The python function we’re using here is in the module subprocess and is called “call”.

A few real notes:
0. Note that this function will not purely benchmark your code, as there is technically some overhead with import subprocess and running If you care about this, then you’ll need another solution. You might be able to get a sense of the overhead on your machine by running:

$ python -m timeit -n 1 'import subprocess;"", shell=True)'

This runs in 2.5 ms on my laptop. You could assume linearity here and just subtract off this time, if you don’t mind the back-of-the-envelopeness of this.

1. There might be a hundred caveats on this, with respect to subprocess and shell=True, which are details I have yet to fully figure out. But again this is a qnd benchmark, good enough for my uses, and possibly useful to others.


Installing NEURON, MPI, and Python on OS X Lion

I’ve recently run into the problem of trying to compile the source for NEURON 7.3a that includes support for parallel NEURON (using MPI) and Python on Mac OS X Lion. Using a number of helpful web resources, I wanted to cobble together an “as-of-this-writing” practice to get a kitchen sink working install for all components.

UPDATED 01-Sept-2012: Added instructions for mpi4py

Because I don’t have the resources to test many version combinations, etc., this assumes OS X 10.7.4 on a Retina MacBook Pro 10,1 (Mid 2012), with a working copy of XCode 4.4.1 installed (available for free from the Mac App Store). I suspect but cannot verify that this will work with many different versions of all of these components. Note: MacPorts is particularly sensitive to very new XCode and OS releases, so right after a new one, things don’t always work right away.

Finally this assumes you have admin access to the computer on which you are installing things and that you will use sudo for good and not evil.

And super-finally, this is basically an aggregate of web sources, some of which I had to modify to get it working. Sources are inline, below. Much is duplicated here because of the transience of web links.

Continue reading

Installing XCode on OS X Lion and Mountain Lion

This is a series of posts leading toward a complete installation of NEURON neural simulation environment on OS X. All of this information is relevant for other purposes as well.

There are many places that have instructions on how to install XCode. For Lion and Mountain Lion, you go to the Mac App Store and download and install the XCode installer. If you previously had a version of XCode that installed in a directory called /Developer, excellent directions on how to use xcode-select are found on MacPorts.

Command line tools are now treated as a separate download. There are (at least) two ways of getting them.

1. Open Xcode. Go to the Xcode application menu and select Preferences. From there, go to Downloads and select the button for Command Line Tools.

2. Go to the Apple Developer website. You’ll need to be a free developer member to use this website. From there you will go to the Mac Developer downloads section, and you’ll have to find the relevant package for Command Line Tools for your OS. I’ve not tested whether or not you can install these without Xcode, but I’ve also heard that you may be able to either directly or with a little Homebrew hackery (note: I still recommend and use MacPorts to both Fink and Homebrew, but they all have strengths and weaknesses).

Forcing line breaks in LaTeX titles using maketitle

I had occasion while using the typesetting program LaTeX to force specific line breaks in my very long title. In order to do this, you must protect the line break command. I am not sure what this means or why, but it works with no apparent ill effects for me:

\title{Insert very long and informative title \protect\\ in this space with considerations of \protect\\ strange automatic line breaks}

If your title used to read:


it will now read:


Ten days of code

Open code in academic computational science should be the standard. After all, the scientific ideal is to share information toward progress. This is at least an idealistic view of why we publish so competitively, with standards that demand we share our findings with our peers and with the public. And while the computational methods of my most recent joint experimental/computational paper far exceeded the length of our experimental methods explanation, the rather large step of implementation of our model into a numerical scheme is non-trivial (at least I’d like to think so).

Continue reading

LaTeX modernization challenge

For those in technical fields, the LaTeX typesetting system is only slightly more invaluable than infuriating, not to mention shady to search for appropriately on the internet. As they say, LaTeX makes very difficult tasks easy and very easy tasks difficult. Though there are several excellent GUIs that address many of these shortcomings, there’s a problem in compiling efficiency that I think can be addressed. Every time you make a change and run the LaTeX command on a file, it recompiles the entire document, soup to pie. This is obviously useful when you have references that need updating throughout the entire file, but I bet there’s a way to implement a system in which LaTeX compiles only what changes, and beyond.

Let’s take a simple example. Let’s say I have a beamer presentation in LaTeX that has 10 slides. If I change information on slide 3 that has NO bearing on slides 1-2, I should be able to compile the document so that it only compiles slides 3-10, since potentially pagination on slide 3 changes the slides that follow. (In fact, beamer slides should be such that most changes made to slide 3 will not affect any other slide, so in that case LaTeX should retypeset page 3 and then maintain the other pages.)

Pitfalls of this are pretty clear: there are several scenarios, including references and labels, in which the entire document may need to be recompiled, and it may seem like considering these exceptions is cumbersome/inelegant. But I think that modern computing is easily capable of handling these challenges in a fairly efficient manner. It doesn’t even have to be all-inclusive for a first try.

It’s also not clear to me whether or not such a thing exists. I haven’t spent enough time on LaTeX community boards or mailing lists to really know. The reason for this is partly time, of course, but mostly because I don’t have the technical acumen to actually help solve these problems. But here I’m throwing out the challenge to do so, if it hasn’t been done already. It’s clearly not in the mainstream LaTeX distributions, but it’s a sorely needed feature that would save enormous amounts of time in preparing these manuscripts in LaTeX.

Browser spaces

Like many, my work involves computers, and researching scientific articles is almost completely online now. Ever since Xerox’s Tab Manager that was a drop in replacement for Progman.exe (oh yeah) in Windows 3.1 (was it autoexec.ini that you edit?), I’ve been hooked on tabs. Tabbed browsing could not have appeared fast enough, but it’s now ubiquitous. Similarly in the window-organization world, virtual desktops are something I used in some iteration since the early 90s — in Windows, a virtual desktop was a large number of pixels, and the focus was simply moved around as your monitor showed different parts of one big desktop. In Debian, I think it was a Gnome window manager that enabled N distinct desktop spaces that were all of the monitor’s native resolution but were discrete — you could not see half of two different spaces. However, these were seamless in the sense that you could drag windows between them easily or even have a window appear on all spaces. In Mac OS X, it has taken forever to get a reasonable virtual desktop solution, from the CodeTek Virtual Desktop to Spaces+Hyperspaces+Warp. It’s not without its problems, but that’s beyond the scope of this post.

Instead, I was thinking about my workflow in a browser. I generally keep 1-2 windows open that contain anywhere from 1-10 tabs each. Window 1 is usually reserved for Pubmed/Google Scholar searches for various topics I need to read about. Each tab is a different paper on a specific topic. Window 2 is everything else — email, RSS, news, etc. However, I would love to see a feature in a browser that is based on the idea of projects in the browser. Each project gets its own named window. Window 1 could be “Papers” and Window 2 could be “Other.” But this could be very powerful for me — Window 1 could be “Interlaminar connections of auditory cortex” while Window 3 could be “Pharmacology of auditory thalamus.” The integration in the browser could run deep enough so that I could tell a tab to go to a specific window using a menu, and I could be able to recall any window I wanted very easily, almost in a cmd+tab type interface, using cmd+~. Additionally, I could have an “Open in Tab in Project 1” option on links so that I can easily keep my windows organized in tabs in projects. The possibilities are endless.

The basic idea is simple, I think, but it would take some thoughtful integration to make this useful. Go, forth, Safari/Chrome/Camino/Firefox developers.