Home > Blockchain >  Trim an array with respective to another array with numpy
Trim an array with respective to another array with numpy

Time:04-26

I am handling a set of data recorded by a 2D detector. Therefore, the data are represented by three arrays: x and y labelling the coordinate of a pixel and intensity storing the measured signal.

For example, a 6x6 grid will give a set of data:

xraw = np.array([0,1,2,3,4,5,0,1,2,3,4,5,...])
yraw = np.array([0,0,0,0,0,0,1,1,1,1,1,1,...])
intensity = np.array([i_00,i_01,i_02,i_03,i_04,i_05,i_10,i_11,...])

Due to various reasons, such as pixel defects, some of the data points are discarded in the raw data. Therefore, xraw, yraw, intensity have a size smaller than 36 (if that's a 6x6 grid), with, say, the point at (2,3) missing.

The intensity data needs further treatment by an element-wise multiplication with another array. This treatment array is from theoretical calculation and so it has a size of nxn (6x6 in this case). However, as some of the points in the true data are missing, the two arrays have different sizes.

I can use a loop to check for the missing points and eliminate the corresponding element in the treatment array. I wonder if there are some methods in numpy that take care of such operations. Thanks

CodePudding user response:

First, construct the indices of available and all possible pixel positions by

avail_ind = yraw * h   xraw
all_ind = np.arange(0, h * w)

where h and w is the image's height and width in pixels.

Then, find the indices of the missing pixels by

missing_ind = all_ind[~np.in1d(all_ind, avail_ind)]

Once having the missing indices, use np.delete to construct a copy of the treatment_array with elements at the indices removed, then simply multiply that with your intensity array.

result = intensity * np.delete(treatment_array, missing_ind)
  • Related