Wednesday, June 29, 2011

Contrast Curves II

Hello!

I finally completed my contrast curve method today, but first:

One of the things Professor Johnson told me to do was to compartmentalize my code. If there are recurring actions I'm doing, I should just write a separate function for those actions. So today I redefined some of my original functions. It definitely makes my code look way neater, and will certainly save me a lot of typing later on.

Methods:

def annmask(self,cx,cy,ann_in,ann_out): Creates an annulus of 1's (other points outside annulus are set to zero)

def annulus(self,cx,cy,ann_in,ann_out): Creates an annulus of data (other points outside annulus are set to zero). This function just multiplies the array of 1's from annmask by the data values in the image.

def ann_meanbg(self,cx,cy,ann_in,ann_out): Finds the average bg noise of an annulus. This function uses the values taken from the annulus function and finds the mean of those points.

def ann_stdbg(self,cx,cy,ann_in,ann_out): Finds the standard deviation of brightness values inside an annulus. This function uses the values from the annulus function and finds the standard deviation of those points.

def phot(self,cx,cy,rad=5,ann_in=8,ann_out=10): This function was originally the aper_phot(...) function mentioned in this post: http://exolab-sandra.blogspot.com/2011/06/aperture-photometry.html. It now borrows heavily from the annulus(...) and annmask(...) functions.

Now, back to contrast curves.

def contrastcurve(self,ann_space=0): Returns an array of radii from star (rs) and corresponding sigma for annuli (lim). The annuli are two FWHM's wide and each element in rs is the average of the inner and outer radius of each annulus. ann_space is the space between the aperture and the innermost inner annulus radius. 

Example:
Using the same two images we used yesterday (with sim being the cropped image), we have:

>>> sim.contrastcurve()
(array([  3.43701872,   4.00985518,   4.58269163,   5.15552809,
         5.72836454,   6.30120099,   6.87403745,   7.4468739 ,
         8.01971036,   8.59254681,   9.16538326,   9.73821972,
        10.31105617,  10.88389262]), 
array([ 579.53765955,  191.44800124,  124.88768112,  52.21637822,
         29.54445459,   12.46020976,    8.8020379 ,   6.49041072,
          5.61804079,    5.24630489,    4.67690715,   4.30710904,
          4.28717292,    4.07445331]))


The first array contains the radii, and the second contains the corresponding sigma for each radius.

Learned:
-More about how fitgaussian(...) is defined.
To-Do:
-Have fun in Yosemite! (That's right, I'm going on a trip this weekend :D)

Tuesday, June 28, 2011

Contrast Curves (Monday-Tuesday)

Yesterday wasn't too exciting (as in I thought more than twice the work I produced), so I didn't blog.
First of all, I fixed up my point sources function. Now there are less points that are likely point sources, but there are still a few too many. I also started working on a contrast curve function yesterday, but didn't get far on the actual coding.

I'm still working on the contrast curve function(s) today... and will tomorrow as well.
-----
The idea is to create a contrast curve that plots contrast (basically magnitude of flux against some background noise) versus radial distance away from the star's center. The center is defined by fitting a Gaussian to the data and finding the x and y coordinates of the peak. The actual gaussianfit(...) function is found here: http://www.scipy.org/Cookbook/FittingData.

Now, in order to plot the magnitude and account for some differences in background noise near the star, we need to find annuli around the PSF of the star, and figure out the "background noise" in each annulus. I say this with quotes since a bit of the star's flux will leak out into this region and affect the overall flux level. We need many different concentric annuli since the flux varies in this region.

The annuli will have a width of two FWHM's to get a complete curve of other stars' PSF (if there are other stars in the picture). I will also calculate the background noise of overlapping annuli so that if we do see another star in the picture, we can hopefully get a PSF that isn't cut off by at least one annulus.

Anyways, I made a function called ann_meanbg(...). Given a center (cx,cy) and inner and outer radii (ann_in, ann_out) it can find the mean background flux of an annulus defined by the center coordinates and radii. Since there might be times where the annulus contains another fainter star that significantly increases the background flux, I have included a darkpix factor that will only take the darkest darkpix percentage of pixels in order to ignore the fainter star. 


def ann_meanbg(self,cx,cy,ann_in,ann_out,darkpix=1): Finds the average background noise of an annulus.

I also started a contrastcurve(...) method. It is supposed to return one array of radii that is the average of ann_in and ann_out, and one corresponding array that contains the annulus background noise at each radius. 


A third function I made is called subplot(...):
def subplot(self,cx,cy,xsz=50,ysz=50): Makes a cropped image from the original image with cx, cy as center coordinates and with a width of xsz and height of ysz.

Finally, because of the way my original class was defined, I had to completely redesign it so that contrastcurve(...) would be able to be a function of the class. Now the Image class takes the data array of the FITS file as opposed to the FITS file itself. I have defined a subclass called GuiderImage(Image) to take care of the FITS file header. 


Example: (assuming we have an Image (the new class) object called im)
>>>im.show()
This returns:
>>>sim=im.subplot(21,99,40,40)
>>>sim.show()
This returns:
>>>a=u.fitgaussian(sim.data)
>>>a
array([685.03745756, 20.29637509, 19.94028319, 27.96468548, 27.93414018])
>>>sim.ann_meanbg(a[1],a[0],5,8)
557.72357723577238

Learned:
-A little bit about the Gaussian/Bi-Variate Normal Distributions
-Python subclasses
-The use of lambda in Python functions
-How to dezero an array (take out all the zeros in an array) efficiently - using a[a!=0]
-Background about Contrast Curves
Problems/To-Do:
-Complete the contrastcurve(...) function
-Figure out how to find the difference in magnitude and plot the contrast curve