Tag: Cool code

How to get up-to-date Python packages without bothering your cluster admin

If you have ever been stuck as a user on an out-of-date cluster without root access it can be frustrating to ask the admin guy to install packages for you. Even if they respond, by the time they get round to it you might have moved onto something else. The moment could be gone.

Luckily, as far as Python is concerned, the pyenv project allows users to install their own local Python version or even assign different versions to different directories/projects.

Sir Andrew Smith - A. Smith: Illustrations of the zoology of South Africa, Reptilia. Smith, Elder, and Co., London 1840 PYTHON NATALENSIS (Southern African Python) (Reptilia Plate 9) in A. Smith: Illustrations of the zoology of South Africa, Reptilia. Smith, Elder, and Co., London 1840

Public domain image.

João Moreira has written a great beginner’s guide on the Amaral Lab homepage in order to get started. I now have the latest version of Python 2 (v2.7.12) installed along with essential packages like Scipy and Pandas, which I added using pip.

Installation of pyenv is easy.

curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash 

Different versions of python can then be installed with

pyenv install 3.4.0

Switching your global Python version is then as simple as typing

pyenv global 3.4.0

From first impressions I can say I highly recommend pyenv, and will continue to learn about it over the coming days through using it. Please refer to João’s excellent post for more details.




Equations in Gmail with the “TeX for Gmail” Chrome extension

Science via email

One thing scientists and engineers have to do daily is discuss collaborative work via email exchanges. This often includes the need to share and discuss mathematical equations and to represent variables with subscripts and superscripts or special characters; something that is tricky when you are emailing in plain text.

WikiImages / Pixabay

Of course it is possible to work around this problem! Email was invented by scientists, and for decades they have been communicating in this manner, using various conventions to convey the correct information using plaintext. However, if you are a Gmail user there is a nice extension that will make your equations look proper good.

Tex for GmailGmail-logo

TeX for Gmail is a Chrome browser extension that checks a Gmail email that you are writing for LaTeX markup and converts the markup to a visually prettier equation, using one of two modes. In Simple Math mode, subscripts and superscripts are correctly formatted but the current font is maintained and text remains ediatble. In Rich Math mode, the equation is rendered into TeX and replaced by an embedded image.  The email recipient doesn’t need the extension installed on their browser in order to read your nice equations!

Example

Original markup:

$E = mc^{2}$

Simple Math mode:

E = mc2

Rich Math mode:
E = mc^{2}

Issues

One problem; once the extension has converted my markup to formatted text, I cannot get the markup back. So editing a small mistake usually means re-doing all the curly brackets and other stuff that a TeX equation requires. The only workaround seems to be to stay vigilant and use Undo (Ctrl-z), but this doesn’t work when you notice a mistake in an equation that you wrote a while ago. One improvement could be the option to restore any equation to the original markup.

Conclusions

Overall, a great little tool to improve the clarity of science and maths communications over email. With a few small improvements it could be even better but it is already very usable.




Firefox search bar – setting the region for Google searches

The problem

If you are visiting/living abroad but still want the Firefox search bar to default to your home version of Google, it is possible to fix it! In the following solution I assume that you are from the UK and want to use the UK/GB version of Google Search:

The solution

  1. Access the Firefox settings: type “about:config” in the address bar, then click “I’ll be careful, I promise”.
  2. For each setting you want to modify, use the search bar to find it more quickly.
    1. Set both “browser.search.countryCode” and “browser.search.region” to “GB” by double-clicking it and typing the new value.
    2. Make sure “browser.search.isUS” is set to False.
    3. For both “distribution.searchplugins.defaultLocale” and “general.useragent.locale” use “en-GB”.
  3. Restart Firefox.
  4. Visit http://www.google.com/ncr, to activate a “no country redirect” cookie that stops the browser from looking for country-specific search results.

Searching from the search bar should now give results at Google.com, but notice that at the very end of the search results URL there is a flag for UK regional content.

Enjoy!




Cool code: plotting columns from many data files with Grace

Grace a.k.a. xmgrace is a really useful tool for plotting histograms from tabular data files. Its power comes from the command line control and being scriptable. Yes, there are other options which are sometimes more suitable for specific situations (e.g. GNUplot, Matplotlib/PyLab), but for quick, basic plotting I usually find myself relying on xmgrace.

Here is an example of a single line command to plot two columns from each of a large number of data files:

for i in ./a*/field.log; do echo -n " -block $i -bxy 10:44" ; done | xargs xmgrace

The command searches all current subdirectories with names beginning “a” for files called field.log. For each field.log file found it uses an echo command to generate a commandline argument string that we normally use to make a 2D plot with the 10th (x axis) and 44th (y axis) columns using xmgrace. The -n option for echo makes sure there is no newline after each echo, generating one long collection of commands. We next want to pass this on to xmgrace…

xmgrace does not take input from stdin so we first pipe the total echo output to xargs, which is a tool used to construct command line parameters from stdin. xargs then passes these command line arguments to xmgrace.

This little code snippet saves a lot of typing, which grows of the order N for N data files. Now I need to work out how to name each data set on the fly too.