Question

I am trying to mask out certain portions of a numpy array using a mask file with ranges and I cannot figure out how to do so efficiently. I have an two arrays (time and data) with thousands of values, and then a mask file that includes start and stop times. I am hoping there is an easy when to mask the values of array that are between any of the start and stop values. Below is some pseudo code to help conceptualize what I'm trying to do.

# the mask file is two-column with start time and stop time
mask = np.loadtxt(maskfile)

time, data = np.loadtxt(datafile, unpack=True)

data = data[(time > mask[:,0]) & (time < mask[:,1])]

Clearly, that won't work because time and mask are not the same length.

Is something like this possible? Any help would be greatly appreciated!

Was it helpful?

Solution

Given an array:

In [1]: x = np.arange(100).reshape(10, 10)

and a second array of lower and upper bounds (l, u),

In [2]: y = np.array([[6, 11], [41, 47], [85, 98]])

iterate through the bounds array and (re)mask the data array according to the bounds

In [3]: for l, u in y:
  ....:     x = np.ma.masked_where((x > l) & (x < u), x)
  ....: 

In [4]: x
Out[4]: 
masked_array(data =
 [[0 1 2 3 4 5 6 -- -- --]
 [-- 11 12 13 14 15 16 17 18 19]
 [20 21 22 23 24 25 26 27 28 29]
 [30 31 32 33 34 35 36 37 38 39]
 [40 41 -- -- -- -- -- 47 48 49]
 [50 51 52 53 54 55 56 57 58 59]
 [60 61 62 63 64 65 66 67 68 69]
 [70 71 72 73 74 75 76 77 78 79]
 [80 81 82 83 84 85 -- -- -- --]
 [-- -- -- -- -- -- -- -- 98 99]],
             mask =
 [[False False False False False False False  True  True  True]
 [ True False False False False False False False False False]
 [False False False False False False False False False False]
 [False False False False False False False False False False]
 [False False  True  True  True  True  True False False False]
 [False False False False False False False False False False]
 [False False False False False False False False False False]
 [False False False False False False False False False False]
 [False False False False False False  True  True  True  True]
 [ True  True  True  True  True  True  True  True False False]],
       fill_value = 999999)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top