Monday, June 20, 2011

Test Scripts in Python-II (Tues-Fri last week)

Over the next four days I played around with Python's matplotlib's various graphing capabilities.

Tuesday-Wednesday:
Made a file called gauss.py:
-Made a Gaussian function that returns the gaussian at a certain x for a given sigma and mu
-Made a plotting function that plotted the gaussian at a range of x values, using the function described above. This graph definitely had some other nice things thrown in: choice of having a vertical line at mu, x and y labels, title, and resolution specifications.
Graph:

Wednesday-Friday Morning:
Made a file called mc_pi:
-Made a function that estimated the value of pi using the Monte Carlo Method given a number n (it can only go up to 1e8 though, or my computer will complain and crash).
-Created an array of pi values estimated given a certain number n with a specified number of array elements (niter).
-Put these array values into a histogram graph, where the user can specify the number of "bins", n, and niter. Also graphed a gaussian curve over it (sigma=standard deviation, mu=mean). The dotted line is at Pi. 
Graph:
-Finally, I plotted a log graph (x-axis) where pi is estimated with an array of n values, niter times for each value. The error bars are of length sigma=1, and the dots are given by mu.
Graph:

Tim also helped me install matplotlibrc on my computer; however it doesn't seem to work in Python. I still need to try changing all the backends.

Also, on Wednesday I started a new file called utils.py; in here, I'll add random functions that will be useful during this summer/in general so that I won't have to retype them every time I need them. Currently only hosting:
-a distance function that finds distance in 3-space given x,y,z coordinates
-a coordDist function that finds distances in 2-space given two points (tuples)
-a setfig function that has been rather abandoned as Python seems to automatically set the figure number of each graph

Friday
No plotting exercise today! Today I made a file called makecirc.py
-It hosts one function. Given a 2-D data array, it makes an array of the same dimensions and fills them with zeros. Then within a circle with a fixed center and radius specified by the user, it fills the circle with ones. This will definitely be useful when I  have to plot the brightness of stars with respect to a radius (distance from the center of the star).
Here's an example:
Given:


>>>from numpy import*
>>>import makecirc.py
>>>dataArr=zeros((10,10))
>>> dataArr
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

>>> makecirc.makecirc(dataArr,5,4,3)
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]
 [ 0.  0.  1.  1.  1.  1.  1.  0.  0.  0.]
 [ 0.  0.  1.  1.  1.  1.  1.  0.  0.  0.]
 [ 0.  1.  1.  1.  1.  1.  1.  1.  0.  0.]
 [ 0.  0.  1.  1.  1.  1.  1.  0.  0.  0.]
 [ 0.  0.  1.  1.  1.  1.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

Tim also helped me install PyFITS on my computer, which is nice as now I open up pretty pictures of stars on my computer (the FITS files). I also learned some command line code as we had so much trouble installing PyFITS on a Windows computer.

Stuff Learned:
-Too much to write on here. Mostly how to use different functions of matplotlib and how making graphics in Python works. I wrote them in my notebook.
Problems: 
-matplotlibrc won't work.
To Do:
-Next week I'll start making a 2-D Gaussian plot.
-Learn more about how to use PyFITS.
-Read assigned research papers.

No comments:

Post a Comment