Massive Interval Mask Matrix

I am trying to mask certain parts of a numpy array using a mask file with ranges, and I cannot figure out how to do this efficiently. I have two arrays (time and data) with thousands of values, and then a mask file that includes start and stop times. I hope it is easy to mask array values ​​that are between any start and stop values. Below is some pseudo code that will 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])]

Obviously, this will not work, because time and mask do not have the same length.

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

+3
source share
1 answer

Given an array:

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

(l, u),

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

(re)

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)
+4

All Articles