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