├── images ├── fit1.png ├── fit2.png ├── plot1.png ├── plot2.png ├── rot1.png ├── rot2.png ├── interp1.png ├── interp2.png ├── interp3.png ├── tt_ltex.png └── histogram.png ├── README └── code ├── trapz2d.py ├── poly_lsq.py └── gaussian_fit.py /images/fit1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiagopereira/python_tips/HEAD/images/fit1.png -------------------------------------------------------------------------------- /images/fit2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiagopereira/python_tips/HEAD/images/fit2.png -------------------------------------------------------------------------------- /images/plot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiagopereira/python_tips/HEAD/images/plot1.png -------------------------------------------------------------------------------- /images/plot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiagopereira/python_tips/HEAD/images/plot2.png -------------------------------------------------------------------------------- /images/rot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiagopereira/python_tips/HEAD/images/rot1.png -------------------------------------------------------------------------------- /images/rot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiagopereira/python_tips/HEAD/images/rot2.png -------------------------------------------------------------------------------- /images/interp1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiagopereira/python_tips/HEAD/images/interp1.png -------------------------------------------------------------------------------- /images/interp2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiagopereira/python_tips/HEAD/images/interp2.png -------------------------------------------------------------------------------- /images/interp3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiagopereira/python_tips/HEAD/images/interp3.png -------------------------------------------------------------------------------- /images/tt_ltex.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiagopereira/python_tips/HEAD/images/tt_ltex.png -------------------------------------------------------------------------------- /images/histogram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiagopereira/python_tips/HEAD/images/histogram.png -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This repository is mostly concerned with python tips and tricks. 2 | These are focused on scientific/astronomical uses of Python, with numpy, scipy and matplotlib. 3 | Most of the content lives on the wiki, with some bits of code in the repository when useful. 4 | Mainly for my own reference, but hopefully you can find something useful. 5 | -------------------------------------------------------------------------------- /code/trapz2d.py: -------------------------------------------------------------------------------- 1 | def trapz2d(z,x=None,y=None,dx=1.,dy=1.): 2 | ''' Integrates a regularly spaced 2D grid using the composite trapezium rule. 3 | IN: 4 | z : 2D array 5 | x : (optional) grid values for x (1D array) 6 | y : (optional) grid values for y (1D array) 7 | dx: if x is not supplied, set it to the x grid interval 8 | dy: if y is not supplied, set it to the x grid interval 9 | ''' 10 | import numpy as N 11 | 12 | sum = N.sum 13 | if x != None: 14 | dx = (x[-1]-x[0])/(N.shape(x)[0]-1) 15 | if y != None: 16 | dy = (y[-1]-y[0])/(N.shape(y)[0]-1) 17 | 18 | s1 = z[0,0] + z[-1,0] + z[0,-1] + z[-1,-1] 19 | s2 = sum(z[1:-1,0]) + sum(z[1:-1,-1]) + sum(z[0,1:-1]) + sum(z[-1,1:-1]) 20 | s3 = sum(z[1:-1,1:-1]) 21 | 22 | return 0.25*dx*dy*(s1 + 2*s2 + 4*s3) 23 | -------------------------------------------------------------------------------- /code/poly_lsq.py: -------------------------------------------------------------------------------- 1 | from scipy.odr import odrpack as odr 2 | from scipy.odr import models 3 | 4 | def poly_lsq(x,y,n,verbose=False,itmax=200): 5 | ''' Performs a polynomial least squares fit to the data, 6 | with errors! Uses scipy odrpack, but for least squares. 7 | 8 | IN: 9 | x,y (arrays) - data to fit 10 | n (int) - polinomial order 11 | verbose - can be 0,1,2 for different levels of output 12 | (False or True are the same as 0 or 1) 13 | itmax (int) - optional maximum number of iterations 14 | 15 | OUT: 16 | coeff - polynomial coefficients, lowest order first 17 | err - standard error (1-sigma) on the coefficients 18 | 19 | --Tiago, 20071114 20 | ''' 21 | 22 | # http://www.scipy.org/doc/api_docs/SciPy.odr.odrpack.html 23 | # see models.py and use ready made models!!!! 24 | 25 | func = models.polynomial(n) 26 | mydata = odr.Data(x, y) 27 | myodr = odr.ODR(mydata, func,maxit=itmax) 28 | 29 | # Set type of fit to least-squares: 30 | myodr.set_job(fit_type=2) 31 | if verbose == 2: myodr.set_iprint(final=2) 32 | 33 | fit = myodr.run() 34 | 35 | # Display results: 36 | if verbose: fit.pprint() 37 | 38 | if fit.stopreason[0] == 'Iteration limit reached': 39 | print '(WWW) poly_lsq: Iteration limit reached, result not reliable!' 40 | 41 | # Results and errors 42 | coeff = fit.beta[::-1] 43 | err = fit.sd_beta[::-1] 44 | 45 | return coeff, err 46 | -------------------------------------------------------------------------------- /code/gaussian_fit.py: -------------------------------------------------------------------------------- 1 | from scipy.odr import odrpack as odr 2 | from scipy.odr import models 3 | import math 4 | import numpy as N 5 | 6 | def gaussian(B,x): 7 | ''' Returns the gaussian function for B=m,stdev,max,offset ''' 8 | return B[3]+B[2]/(B[1]*math.sqrt(2*math.pi))*N.exp(-((x-B[0])**2/(2*B[1]**2))) 9 | 10 | 11 | def gauss_lsq(x,y,verbose=False,itmax=200,iparams=[]): 12 | ''' Performs a gaussian least squares fit to the data, 13 | with errors! Uses scipy odrpack, but for least squares.''' 14 | 15 | def _gauss_fjd(B,x): 16 | # Analytical derivative of gaussian with respect to x 17 | return 2*(x-B[0])*gaussian(B,x) 18 | 19 | # these derivatives need to be fixed! 20 | def _gauss_fjb(B,x): 21 | # Analytical derivatives of gaussian with respect to parameters 22 | _ret = N.concatenate(( -2*(x-B[0])*gaussian(B,x),\ 23 | ((x-B[0])**2/(2*B[1]**2)-1)/B[1]*gaussian(B,x),\ 24 | gaussian(B,x)/B[2] ,\ 25 | N.ones(x.shape, float) )) 26 | _ret.shape = (4,) + x.shape 27 | return _ret 28 | 29 | # Centre data in mean(x) (makes better conditioned matrix) 30 | mx = N.mean(x) 31 | x2 = x - mx 32 | 33 | if not any(iparams): 34 | # automatic guessing of gaussian's initial parameters (saves iterations) 35 | iparams = N.array([x2[N.argmax(y)],N.std(y),math.sqrt(2*math.pi)*N.std(y)*N.max(y),1.]) 36 | 37 | gauss = odr.Model(gaussian, fjacd=_gauss_fjd, fjacb=_gauss_fjb) 38 | 39 | mydata = odr.Data(x2, y) 40 | myodr = odr.ODR(mydata, gauss, beta0=iparams, maxit=itmax) 41 | 42 | # Set type of fit to least-squares: 43 | myodr.set_job(fit_type=2) 44 | if verbose == 2: myodr.set_iprint(final=2) 45 | 46 | fit = myodr.run() 47 | 48 | # Display results: 49 | if verbose: 50 | fit.pprint() 51 | print 'Re-centered Beta: [%f %f %f %f]' % \ 52 | (fit.beta[0]+mx,fit.beta[1],fit.beta[2],fit.beta[3]) 53 | 54 | itlim = False 55 | if fit.stopreason[0] == 'Iteration limit reached': 56 | itlim = True 57 | print '(WWW) gauss_lsq: Iteration limit reached, result not reliable!' 58 | 59 | # Results and errors 60 | coeff = fit.beta 61 | coeff[0] += mx # Recentre in original axis 62 | err = fit.sd_beta 63 | 64 | 65 | return coeff,err,itlim 66 | --------------------------------------------------------------------------------