I often have to create plots where the color of some element reflects an underlying value. This mapping of color -> value is generally easily accomplished by using colormaps in matplotlib. The standard provided colormaps (such as cm.jet or cm.cool) map values over the interval [0,1]. Often times, however, our data will have a different range and will need to be normalized in order for it to be mapped to the full range of available colors.
The example below illustrates what the color range looks like without the normalization (top two plots) and with the normalization (bottom plot).
Notice how the unnormalized [-1,1] input values have a dark blue color over the
left half of the second plot. This is because cm.jet(-1)
= cm.jet(-.5)
=
cm.jet(0.)
Values outside of cm.jet
’s accepted input range of [0,1] all
return the same color. By using the Normalize
class, the input values are
scaled to the [0,1] input range and the output colors span the entire range of
the colormap (third plot).
The numpy library provides a plethora of fast functionality using for vectors. The question is, can we improve upon it by using cython and assuming vectors in three-dimensional space. Let’s take the following two functions which are not provided by numpy, but easily implemented. The first simply calculates the magnitude of a vector, while the second calculates the distance between two vectors.
Now let’s take the equivalent implementation in cython. The only major difference from the pure code is the definition of the data types and the handling of each value individually without any loops.
Finally, the timing:
Using the cython implementation leads to a nearly three-fold decrease in the running time for the vec_distance
function and an almost two-fold decrease in the running time for the magnitude
function. The cytvec implementation can simply be copied into a file (let’s say cytvec.pyx
) and compiled into a module by following the instructions in the cython tutorial.
An already compiled implementation can be found in the forgi package under forgi.threedee.utilities.cytvec
.
Both at home and at work I use some distribution of linux with Xfce as the window manager. Over the years, I’ve added various shortcuts to speed up the things I do often. The most important of them are the keyboard shortcuts for launching applications. These are set by going to Settings
-> Keyboard
and then opening the Application Shortcuts
tab. In there, my three most used programs are assigned the following shortcuts:
Command | Shortcut |
gnome-terminal |
<Alt> F1 |
chromium-browser |
<Alt> F3 |
thunar |
<Alt> F5 |
My speakers are further away than my keyboard. Changing the volume using the knob requires me to lean forward. Too much work. Moving the mouse is also annoying. Thankfully, I can add the following keybindings in Xfce and control the volume from my keyboard and subsequently raise the volume using <Ctrl><Alt> + and lower the volume using <Ctrl><Alt> - . Pretty convenient!
Command | Shortcut |
amixer set Master 4%- -q |
<Primary> <Alt> minus |
amixer set Master 4%+ -q |
<Primary> <Alt> plus |
Instead of pressing the up-arrow to repeat a command I’ve entered previously, I commonly hit <Ctrl> R to search for a particular command. This is generally really efficient except when I’m searching for something far back in time by repeatedly hitting <Ctrl> R and I end up missing the command I was looking for. Woe is me if there were a lot of commands between my target and the one I’m currently on. Ideally, I would like to just hit <Ctrl> S to go to the next occurance of my search string in my history file. Alas, <Ctrl> S is already bound and needs to be freed before I can use it this way. To do this, I added the following line to my .bashrc
file:
stty stop ^X
Now when I search for an old command using <Ctrl> R, I can reverse the search (and search forward) in the command history list by pressing <Ctrl> S.
There’s some command-option combinations that I use so often that I’ve assigned them aliases in my .bashrc
file. This allows me to substitue a short command for a longer one. Listed below are my most used aliases in order of their importance.
List the files in the directory in reverse-time sorted order:
alias lt='ls -lhtr'
This is my most used command, period. I’m almost always most interested in my recently accessed files. This command provides those at the bottom, along with all the information about them.
Shortcut for logging into my work computer:
alias work-login='ssh user@workcomputer.com'
My username and computer name don’t change very often at work. Why re-type them every time?
Create and attach to tmux windows:
alias tattach='tmux attach -t'
alias tnew='tmux new -s'
I’m a big fan of the screen multiplexer tmux. These aliases allow me to quickly create new sessions and attach to old ones.
Get the size of a folder and all its subfolders:
alias dum='du --max-depth=1 -h'
Comes in handy for finding out which directories are the biggest from the command line.
Get summary statistics for a list of numbers:
alias fivestats="awk '{if(min=="'""'"){min=max=\$1}; if(\$1>max) {max=\$1}; if(\$1<min) {min=\$1}; \
total+=\$1; count+=1} END {print total/count, max, min, count}'
I often want to compare two sets of numbers. This command allows me to get the mean, max, min, and count of the list without creating (or finding) an awk script every time.
Get the Chrome Extension New Tab Redirect and set the redirect option for a new tab to “about:blank”.