简体   繁体   English

使用间隔掩盖numpy数组

[英]Mask numpy array using intervals

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. 我试图使用带范围的掩码文件掩盖numpy数组的某些部分,我无法弄清楚如何有效地这样做。 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. 我希望有一个简单的方法来掩盖任何start和stop值之间的数组值。 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! 任何帮助将不胜感激!

Given an array: 给定一个数组:

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

and a second array of lower and upper bounds ( l , u ), 以及第二个下限和上限( lu )的数组,

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 迭代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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM