Friday, February 26, 2010

Inkscape: powerful tool to edit plots and PDF files

The free graphics editor Inkscape is a powerful PDF editor. It can be used to change / enhance figures embedded in PDF documents, without the need of having the source data using to produce the plot.

Suppose there is a given plot in a paper and you want to use it in a talk or another document. Suppose that you would like to change the color of a line, or make the points thicker etc. That is easy with Inkscape. If the plot was embedded in the PDF including vectorial data, then you can change all its properties by opening the PDF file in Inkscape, making the changes (the interface is intuitive) and then exporting it as a PDF, PNG etc.

Inkscape is available for Mac OS X, Linux and Windows.

New colors in SM

I like to use Super Mongo to make my plots. It is a very nice software, but somethings are, some times, hard to do (or hard to find in the tutorial). My last 3 problems were related to:

a) Log scales in SM.

All what you need to do is:

: ticksize -1 0 -1 0
: box


Note: you need to work with the data in log, what you can do by:

: read {f1 1}
: set f1=lg(f1)

b) Add grid lines in the plot.

: box grid 1 (to small and long grid lines) or 0 (for only long grid).

c) The last one was in trying to add more colors to SM color pallet.
To solve this issue I found Annika Peter page, which  I resume below.

Note: The figures were taken from Annika Peter page.

The problem: There are only seven "default" colors in SM.



But is very easy to add more colors to SM color pallet.
i) use the command "add_ctype"

: add_ctype mycolor 170 170 0   

This will produce darkgold

A very nice tool to help here is the http://www.colorschemer.com/online.html web site.



ii) Use the : xtcolors to add new colors. This will produce the following colors.




I hope this will help you.

Friday, February 19, 2010

Suggested reading: The trouble with physics by Lee Smolin

I just devoured this book on my trip to Chile last week. The author, Lee Smolin, gives a very clear description off the main revolutions in physics and explain why and how does the string theory fails as a theory by itself. There are great examples explaining how the great scientist, such as Galileo, Kepler, and Einstein got theirs insights and played important roles in modern science.

This is a book for the layman, although a little bit of basic physics helps to see the big picture more clearly. I recommend it for everyone that is starting a career on physics/astronomy (at least it helped me a lot!).

Monday, February 1, 2010

Using f2py

The purpose of the F2PY --Fortran to Python interface generator-- is to provide connection between Python and Fortran languages. Here I will show a simple example of how to use this tool and compare the its efficiency against a pure python code.

First, lets write our fortran routine:

Subroutine test(a,b,n,c)

Implicit none
Integer::i
Integer, intent(in)::n
Real, dimension(n), intent(in)::a,b
Real, dimension(n), intent(out)::c
!f2py depend(n)::a,b,c
!f2py intent(in)::a,b
!f2py intent(out)::c

Do i=1,n
c(i) = sqrt(a(i)**2.0 + b(i)**2.0)
EndDo

End subroutine test

This code simply calculate the distance to the origin of a bunch of points. The input is two vectors with identical dimension n. Note that you must specify what goes in and what goes out to the f2py parser, this is done by using the a comment line followed by f2py:

!f2py depend(n)::a,b,c
!f2py intent(in)::a,b
!f2py intent(out)::c

Now lets compile and link it to python using:

f2py -m func -h func.pyf func.f --overwrite-signature
f2py --fcompiler=gfortran -c func.pyf func.f

Unfortunately f2py does not easily support the Intel Fortran Compiler (ifort), although gfortran does a good job on most of the cases.

It ridiculously easy to call this routine inside python. The script bellow call the fortran routine and then compares its execution time with an identical pure python routine plus the same done using Numpy.
#!/usr/bin/python
import numpy as np
import time
import func

n = 10000
#generates a normal distribution with n points
x = np.random.randn(n)
y = np.random.randn(n)

# Using the fortran routine
t1 = time.time()
f = func.test(x,y,n)
t2 = time.time()

t = (t2-t1)*1000

print 'Fortran runtime (ms): ',t

# Using pure python
t1 = time.time()
r = np.zeros(n)
for i in range(n):
r[i] = np.sqrt(x[i]**2 + y[i]**2)
t2 = time.time()

t = (t2-t1)*1000

print 'Python runtime (ms): ',t

# Using pure python
t1 = time.time()
r = np.sqrt(x**2 + y**2)
t2 = time.time()
t = (t2-t1)*1000

print 'Python + numpy runtime (ms): ',t

If everything goes OK the result should be something like this:

Fortran runtime (ms): 0.150918960571
Python runtime (ms): 76.4260292053
Python + numpy runtime (ms): 0.485181808472

The advantages of using f2py are obvious, the runtime of the pure python code is almost 500 times greater! When using numpy to handle the arrays we get a much better execution time, but still slower when compared to fortran.

I hope this post was useful to the people thinking of migrating to python in the near future.
 
Locations of visitors to this page