0

I want to perform statistics on an annulus around a central portion of a 2D array (performing statistics on the background around a star in an image,for instance). I know how to obtain a 2D slice of a region inside of the array and return the indices of that slice, but is there any way of obtaining the indices of the values outside of the slice?

I have a 2D array called 'Z' and some box size (PSF_box) around which I want to perform some statistics. This is what I've got so far:

center = np.ceil(np.shape(Z)[0]/2.0) # center of the array
# Make a 2d slice of the star, and convert those pixels to nan
annulus[center-ceil(PSF_size/2.0):center+ceil(PSF_size/2.0)-1,\
center-ceil(PSF_size/2.0):center+ceil(PSF_size/2.0)-1] = np.nan
np.savetxt('annulus.dat',annulus,fmt='%s')

I convert the pixels inside of this box slice to nan, but I don't know how to output the indices of pixels outside of the box that are not 'nan'. Or better yet, is there a way to perform some operations on just the area around the slice directly? (As opposed to outputting pixel values that aren't nan)

  • 1
    There's no way to create a numpy array without the hole and treat it exactly as a regular numpy array. For one thing, it won't be rectangular anymore. You could use `nan`s, masked arrays, zeros, or boolean arrays to work around the hole, but without a specific question or task, it's hard to know which is the best approach. – tom10 Mar 07 '16 at 02:19

1 Answers1

1

I hope this represents roughly what you want to do, ie. get the elements of an annulus in your 2d data. If you like the data outside the annulus just change the condition.

import numpy as np

#construct a grid
x= np.linspace(0,1,5)
y= np.linspace(0,1,5)
xv,yv = np.meshgrid(x, y, sparse=False, indexing='ij')


# a gaussian function
x0,y0=0.5,0.5
zz= np.exp(- (xv-x0)**2 - (yv-y0)**2)  # a function over the grid

print 'function\n', zz
# a distance metric on the grid
distance =  np.sqrt(   (xv-x0)**2+ (yv-y0)**2) 

print 'distance from center\n', distance

# make a condition and apply it to the array
cond= (distance>0.3) & (distance<0.7)
print  'selection\n',zz[cond]


# if you care about the locations of the annulus   
print xv[cond]
print yv[cond]

output:

function
[[ 0.60653066  0.73161563  0.77880078  0.73161563  0.60653066]
 [ 0.73161563  0.8824969   0.93941306  0.8824969   0.73161563]
 [ 0.77880078  0.93941306  1.          0.93941306  0.77880078]
 [ 0.73161563  0.8824969   0.93941306  0.8824969   0.73161563]
 [ 0.60653066  0.73161563  0.77880078  0.73161563  0.60653066]]
distance from center
[[ 0.70710678  0.55901699  0.5         0.55901699  0.70710678]
 [ 0.55901699  0.35355339  0.25        0.35355339  0.55901699]
 [ 0.5         0.25        0.          0.25        0.5       ]
 [ 0.55901699  0.35355339  0.25        0.35355339  0.55901699]
 [ 0.70710678  0.55901699  0.5         0.55901699  0.70710678]]
selection
[ 0.73161563  0.77880078  0.73161563  0.73161563  0.8824969   0.8824969
  0.73161563  0.77880078  0.77880078  0.73161563  0.8824969   0.8824969
  0.73161563  0.73161563  0.77880078  0.73161563]
[ 0.    0.    0.    0.25  0.25  0.25  0.25  0.5   0.5   0.75  0.75  0.75
  0.75  1.    1.    1.  ]
[ 0.25  0.5   0.75  0.    0.25  0.75  1.    0.    1.    0.    0.25  0.75
  1.    0.25  0.5   0.75]

See also this great answer : Numpy where function multiple conditions

Community
  • 1
  • 1
roadrunner66
  • 7,772
  • 4
  • 32
  • 38