【发布时间】:2019-10-25 22:31:09
【问题描述】:
我正在研究流体动力学的固定实验数据。我们在三个通道上测量了数据,因此样本不直接重合(同时测量)。我想用窗口方案过滤它们以获得重合样本并忽略所有其他样本。
很遗憾,由于公司的限制,我无法上传原始数据集。但我试图建立一个最小的例子,它生成一个类似(更小)的数据集。原始数据集由每个通道 500000 个值组成,每个值都标有到达时间。使用这些时间戳检查巧合。
刚才,我循环遍历第一个通道的每个样本,并查看与其他通道的时间差异。如果它小于指定的窗口宽度,则保存索引。如果我指定一个间隔来检查差异(例如附近的 100 或 1000 个样本),可能会快一点。但是通道之间的数据速率可能会有很大差异,因此尚未实现。我更喜欢摆脱对每个样本的循环 - 如果可能的话。
def filterCoincidence(df, window = 50e-6):
'''
Filters the dataset with arbitrary different data rates on different channels to coincident samples.
The coincidence is checked with regard to a time window specified as argument.
'''
AT_cols = [col for col in df.columns if 'AT' in col]
if len(AT_cols) == 1:
print('only one group available')
return
used_ix = np.zeros( (df.shape[0], len(AT_cols)))
used_ix.fill(np.nan)
for ix, sample in enumerate(df[AT_cols[0]]):
used_ix[ix, 0] = ix
test_ix = np.zeros(2)
for ii, AT_col in enumerate(AT_cols[1:]):
diff = np.abs(df[AT_col] - sample)
index = diff[diff <= window].sort_values().index.values
if len(index) == 0:
test_ix[ii] = None
continue
test_ix[ii] = [ix_use if (ix_use not in used_ix[:, ii+1] or ix == 0) else None for ix_use in index][0]
if not np.any(np.isnan(test_ix)):
used_ix[ix, 1:] = test_ix
else:
used_ix[ix, 1:] = [None, None]
used_ix = used_ix[~np.isnan(used_ix).any(axis=1)]
print(used_ix.shape)
return
no_points = 10000
no_groups = 3
meas_duration = 60
df = pd.DataFrame(np.transpose([np.sort(np.random.rand(no_points)*meas_duration) for _ in range(no_groups)]), columns=['AT {}'.format(i) for i in range(no_groups)])
filterCoincidence(df, window=1e-3)
是否已经实现了可以进行这种过滤的模块?但是,如果您能给我一些提示以提高代码的性能,那就太棒了。
【问题讨论】:
-
df是 test 数据吗?它有三列,是时间标记吗?实际的 DataFrame 是否包含更多列,其中包含每个 时间戳 的测量数据? -
样本是周期性的,但三个通道之间是否存在偏移?或者样本是非周期性的?对于
AT 0中的每个样本,其他列中是否只有一个样本被视为巧合? -
是的。数据存储在 pandas 数据框
df中。每组至少存在 3 列,但只有粒子的到达时间 (AT) 才重要。到达时间以秒为单位测量,并相对于第一个样本发生偏移,因此 ist 从 0 开始,然后在触发测量时始终具有时间戳(以秒为单位)。 -
测量程序不能保证等距采样。所以每个组或多或少相互独立(物理上当然不是,我们测量 3 个速度分量但想同时知道速度 --> 需要巧合)。因此,没有恒定的偏移量,也不知道有多少样本或什至样本彼此重合。但我只需要找到彼此匹配的样本。所以从 10000 个样本中我会得到 1000 个样本,这些样本是重合的,可以被认为是同时测量的。
-
您的函数是否产生正确的结果?结果是
used_ix吗?
标签: python pandas performance numpy time-series