Masked arrays

Robert Kern robert.kern at gmail.com
Wed May 10 20:45:29 CEST 2006


Tommy Grav wrote:
> I am trying to get the flux of a star in an image. I have been using numpy 
> and pyfits and have the code.

You will probably want to ask on numpy-discussion, instead.

  http://www.scipy.org/Mailing_Lists

> def distance(im,xc,yc):
>     (rows,cols) = im.shape
>     dist = zeros([rows,cols]).astype(Float64)
>     for row in range(rows):
>         for col in range(cols):
>             dist[row,col] = sqrt(((row + 0.5) - yc)**2 + ((col + 0.5) -
> xc)**2)
>     return dist

You will probably want to use numpy.fromfunction() instead.

> def apphot(im,x,y,r):
>     dist = distance(im,x,y)
>     appmask = where(dist <= r,1,0)

You don't have to use where() here. (dist <= r) returns an array of bools, which
are already 1 and 0 (for most purposes).

>     fluxim = where(appmask,im,0)
>     appflux = sum(sum(fluxim))
>     skymask = where(dist > r, 1,0)
>     skyim = where(skymask,im,0)
>     sky = mean(skyim)
>     print skyim
>     print sky
>    
>     return 1
> 
> Output:
>> array (20,20) , type = f, has 400 elements
>> [ 45.89742126, 45.92555847, 45.8874054 , 45.88538208, 45.88244934,
> 45.9353241 ,
>        36.75245361, 29.85816345, 27.53547668, 22.93712311, 22.93178101,
>        22.93699799, 22.91038208, 27.4988739 , 29.84021606, 36.71789551,
>        45.86646729, 45.86741638, 45.85328979, 45.823349  ,]
> 
> where im is a ndarray, x and y are the position of the star and r is 
> the radius of the aperture. I calculate the flux inside the aperture,
> but when I want to calculate the mean of the pixels outside the 
> aperture I run into problems as the pixels values inside the aperture
> is 0 and is still considered in the mean calculation. Is there a way to 
> do this without using masked arrays?

Sure! But ...

> How would I use a masked array
> to do it?

... masked arrays already do the appropriate tasks for you.

In [1]: from numpy import ma

In [2]: ma.average?
Type:           function
Base Class:     <type 'function'>
String Form:    <function average at 0x697ef0>
Namespace:      Interactive
File:
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/numpy-0.9.7.2476-py2.4-macosx-10.4-ppc.egg/numpy/core/ma.py
Definition:     ma.average(a, axis=0, weights=None, returned=0)
Docstring:
    average(a, axis=0, weights=None)
    Computes average along indicated axis.
    If axis is None, average over the entire array
    Inputs can be integer or floating types; result is of type float.

    If weights are given, result is sum(a*weights)/(sum(weights)*1.0)
    weights must have a's shape or be the 1-d with length the size
    of a in the given axis.

    If returned, return a tuple: the result and the sum of the weights
    or count of values. Results will have the same shape.

    masked values in the weights will be set to 0.0

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list