Thursday, May 24, 2012

Hidden features of Python

I learned about this link with many useful hidden features of Python via Eduardo.

I particularly like:

  • the use of enumerator in loops: for i,x in enumerate(array)
  • decorators as a simple way of enhancing methods: @method
  • one-line swapping of variables: a,b=b,a

Wednesday, May 9, 2012

Parallel programming with Python: coming soon

Coming soon here: parallel programming in python for the masses!

I will write a tutorial as soon as I have time from my work duties.

Stay tuned.

Thursday, April 26, 2012

Using git to manage source code and more

Recently I learned how to use Git to manage source code (thanks to this guy). Let me tell you, it is such a fantastic tool! Especially when you have thousands of line of source code constantly evolving and you need to keep track of what changes.

In my case, I have been using it to manage the source code I wrote for my different scientific projects. And I will soon begin using it even to manage the writing of one paper.

Let me list the tutorials that I read and have been very useful in getting me started quickly:

  • Git Magic: I began learning git with this one. It goes straight to the point and illustrates the most important commands.
  • Pro Git: need more detailed information and have more time to spend learning git? Have a look at this one.

Quick reference for gitds:

I use SourceTree, a GUI on Mac, to check the evolution of the source code.



Changelog
May 24th 2012: replaced suggestion of GUI GitX -> SourceTree.

Wednesday, April 11, 2012

How to easily do error propagation with Python

You can easily do error propagation using the uncertainties package in Python, without having to estimate analytically the propagated error or doing Monte Carlo simulations.

Example: Suppose you have two arrays x and y which have associated uncertainties errx and erry. Using x and y, you calculate some function z = f(x, y) which can be arbitrarily complicated (but can be expressed in an analytical form) and you want to estimate the resulting uncertainty errz in z from errx and erry.

The script below

  • defines the arrays x, y, errx and erry using numpy
  • defines a function z=log10(x+y^2) for illustration purposes
  • demonstrates how to invoke 'uncertainties' in order to estimate the uncertainty in z from errx and erry (i.e., errz = f(errx, erry) )


Requirements:



 import uncertainties as unc  
 import uncertainties.unumpy as unumpy  
 import numpy  
 import nemmen  
   
 # Defines x and y  
 x=numpy.linspace(0,10,50)  
 y=numpy.linspace(15,20,50)  
   
 # Defines the error arrays, values follow a normal distribution  
 # (method random_normal defined in http://astropython.blogspot.com/2012/04/how-to-generate-array-of-random-numbers.html)  
 errx=nemmen.random_normal(0.1,0.2,50);     errx=numpy.abs(errx)  
 erry=nemmen.random_normal(0.3,0.2,50);     erry=numpy.abs(erry)  
   
 # Defines special arrays holding the values *and* errors  
 x=unumpy.uarray(( x, errx ))  
 y=unumpy.uarray(( y, erry ))  
   
 """  
 Now any operation that you carry on xerr and yerr will   
 automatically propagate the associated errors, as long  
 as you use the methods provided with uncertainties.unumpy  
 instead of using the numpy methods.  
   
 Let's for instance define z as   
 z = log10(x+y**2)  
 and estimate errz.  
 """  
 z=unumpy.log10(x+y**2)  
   
 # Print the propagated error errz  
 errz=unumpy.std_devs(z)  
 print errz  

Update Oct. 23rd 2014: code snippet available on github.

How to generate an array of random numbers following a normal distribution with arbitrary mean and standard deviation

Here is a recipe to generate an array of random numbers following a normal distribution with the supplied mean and standard deviation in Python:

 def random_normal(mean,std,n):  
      """  
 Returns an array of n elements of random variables, following a normal   
 distribution with the supplied mean and standard deviation.  
      """  
      import scipy  
      return std*scipy.random.standard_normal(n)+mean  

Update Oct. 23rd 2014: code snippet available on github. 

Friday, March 23, 2012

BCES code ported to Python!

Just ported the good old BCES linear regression algorithm (the four methods described in the Akritas & Bershady 1996 paper), originally written in Fortran 77, to Python!!

I am testing the python code and making sure it produces exactly the same answers as the old code.

More news soon...

Update (March 26th 2012): implemented the bootstrapping BCES!

Update (May 19th 2014): the BCES python code is now available as a Github project. Feel free to download it and even contribute improvements.

Thursday, March 22, 2012

Simple progress bar in terminal

If you need to incorporate a simple progress bar in your code, there is a module that does that and is very easy to use: fish.

The following script illustrates how to implement a simple progress bar which advances each time a loop counter increases.

 import fish  
 import time  
   
 steps=input('How many steps? ')  
   
 # Progress bar initialization  
 peixe = fish.ProgressFish(total=steps)       
   
 for i in range(steps):  
      # Progress bar  
      peixe.animate(amount=i)  
        
      time.sleep(0.1)  

Here is a screenshot of what the progress bar looks like in action: