带条件范围的行计数
Row counting with conditional range
这是我第一次发布问题,所以如果我输入的尝试有误,请原谅我。
我的目标: 我正在尝试计算满足条件范围的行数。各个数组元素表示峰值出现的时间(以秒为单位)。输入数据中的每一行代表一个 active/firing 单元格。我想计算每分钟(60 秒的迭代)活动单元格(行)的数量。
我的数据: 我的输入数据 (T) 是作为整数数组从 txt 导入的,并且有几个我不想在其他操作中计算的 0。我在下面复制了该数据的一个子集。
我的问题:我的具体问题是我没有发现我的尝试有任何问题(如下),但由于数组相当小,我能够手动检查输出的真实性。无论出于何种原因,True 参数从 'correct' 迭代开始,但随后保持为 True(当它们应该 return false 时)直到循环中出现另一个 True。然后输出仍然是 'correctly' false。这让我发疯,我将不胜感激任何帮助。以下尝试甚至不尝试对行求和,而只是 return True/False 参数的正确排列。
import numpy as np
T = T.astype(float)
T[T==0] = np.nan
for x in xrange(0, 1321, 60):
RowSum = np.any(T>x, axis = 1) & np.any(T<x+60, axis = 1)
print RowSum
输入数据:
array([[ 111., 184., 221., 344., 366., 0., 0., 0.,
0., 0., 0.],
[ 408., 518., 972., 1165., 1186., 0., 0., 0.,
0., 0., 0.],
[ 208., 432., 1290., 1321., 0., 0., 0., 0.,
0., 0., 0.],
[ 553., 684., 713., 888., 1012., 1108., 1134., 0.,
0., 0., 0.],
[ 285., 552., 1159., 1183., 0., 0., 0., 0.,
0., 0., 0.],
[ 304., 812., 852., 0., 0., 0., 0., 0.,
0., 0., 0.]])
E 先生是对的 - np.histogram
可能是最简单的方法:
import numpy as np
# array of spike times
t = np.array([[ 111, 184, 221, 344, 366, 0, 0, 0, 0, 0, 0],
[ 408, 518, 972, 1165, 1186, 0, 0, 0, 0, 0, 0],
[ 208, 432, 1290, 1321, 0, 0, 0, 0, 0, 0, 0],
[ 553, 684, 713, 888, 1012, 1108, 1134, 0, 0, 0, 0],
[ 285, 552, 1159, 1183, 0, 0, 0, 0, 0, 0, 0],
[ 304, 812, 852, 0, 0, 0, 0, 0, 0, 0, 0]],
dtype=np.float)
# 60 second time bins
bins = np.arange(0, t.max() + 60, 60)
# get the total number of spikes in each 60 second bin over all rows (cells). we
# can treat t as 1D since we don't care which spike times correspond to which
# cell.
counts, edges = np.histogram(t[t != 0], bins)
print(bins)
# [ 0. 60. 120. 180. 240. 300. 360. 420. 480. 540.
# 600. 660. 720. 780. 840. 900. 960. 1020. 1080. 1140.
# 1200. 1260. 1320. 1380.]
print(counts)
# [0 1 0 3 1 2 2 1 1 2 0 2 0 1 2 0 2 0 2 4 0 1 1]
所以我们在 0 到 60 秒之间的总尖峰为零,在 60 到 120 秒之间有一个尖峰等等。顺便说一下,我建议您避免使用 T
作为变量名 - 它可能会导致混淆,因为在 numpy 中 .T
用于获取数组的转置。
要获得每个细胞的尖峰计数,您需要遍历 t
:
的行
cell_counts = np.empty((t.shape[0], bins.shape[0] - 1), np.int)
for ii, row in enumerate(t):
cell_counts[ii], edges = np.histogram(row[row != 0], bins)
print(cell_counts)
# [[0 1 0 2 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 2 0 0 0]
# [0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1]
# [0 0 0 0 0 0 0 0 0 1 0 2 0 0 1 0 1 0 2 0 0 0 0]
# [0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0]
# [0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0]]
更新:
如果我没理解错的话,您想知道每 60 秒时间间隔内出现尖峰的细胞总数,而不考虑每个细胞发出的尖峰数。一种简单的方法是将 cell_counts
数组中的值截断为 1,然后沿行求和:
total_active_cells = (cell_counts > 0).sum(0)
print(total_active_cells)
# [0 1 0 2 1 2 2 1 1 2 0 1 0 1 2 0 2 0 1 2 0 1 1]
这是我第一次发布问题,所以如果我输入的尝试有误,请原谅我。
我的目标: 我正在尝试计算满足条件范围的行数。各个数组元素表示峰值出现的时间(以秒为单位)。输入数据中的每一行代表一个 active/firing 单元格。我想计算每分钟(60 秒的迭代)活动单元格(行)的数量。
我的数据: 我的输入数据 (T) 是作为整数数组从 txt 导入的,并且有几个我不想在其他操作中计算的 0。我在下面复制了该数据的一个子集。
我的问题:我的具体问题是我没有发现我的尝试有任何问题(如下),但由于数组相当小,我能够手动检查输出的真实性。无论出于何种原因,True 参数从 'correct' 迭代开始,但随后保持为 True(当它们应该 return false 时)直到循环中出现另一个 True。然后输出仍然是 'correctly' false。这让我发疯,我将不胜感激任何帮助。以下尝试甚至不尝试对行求和,而只是 return True/False 参数的正确排列。
import numpy as np
T = T.astype(float)
T[T==0] = np.nan
for x in xrange(0, 1321, 60):
RowSum = np.any(T>x, axis = 1) & np.any(T<x+60, axis = 1)
print RowSum
输入数据:
array([[ 111., 184., 221., 344., 366., 0., 0., 0.,
0., 0., 0.],
[ 408., 518., 972., 1165., 1186., 0., 0., 0.,
0., 0., 0.],
[ 208., 432., 1290., 1321., 0., 0., 0., 0.,
0., 0., 0.],
[ 553., 684., 713., 888., 1012., 1108., 1134., 0.,
0., 0., 0.],
[ 285., 552., 1159., 1183., 0., 0., 0., 0.,
0., 0., 0.],
[ 304., 812., 852., 0., 0., 0., 0., 0.,
0., 0., 0.]])
E 先生是对的 - np.histogram
可能是最简单的方法:
import numpy as np
# array of spike times
t = np.array([[ 111, 184, 221, 344, 366, 0, 0, 0, 0, 0, 0],
[ 408, 518, 972, 1165, 1186, 0, 0, 0, 0, 0, 0],
[ 208, 432, 1290, 1321, 0, 0, 0, 0, 0, 0, 0],
[ 553, 684, 713, 888, 1012, 1108, 1134, 0, 0, 0, 0],
[ 285, 552, 1159, 1183, 0, 0, 0, 0, 0, 0, 0],
[ 304, 812, 852, 0, 0, 0, 0, 0, 0, 0, 0]],
dtype=np.float)
# 60 second time bins
bins = np.arange(0, t.max() + 60, 60)
# get the total number of spikes in each 60 second bin over all rows (cells). we
# can treat t as 1D since we don't care which spike times correspond to which
# cell.
counts, edges = np.histogram(t[t != 0], bins)
print(bins)
# [ 0. 60. 120. 180. 240. 300. 360. 420. 480. 540.
# 600. 660. 720. 780. 840. 900. 960. 1020. 1080. 1140.
# 1200. 1260. 1320. 1380.]
print(counts)
# [0 1 0 3 1 2 2 1 1 2 0 2 0 1 2 0 2 0 2 4 0 1 1]
所以我们在 0 到 60 秒之间的总尖峰为零,在 60 到 120 秒之间有一个尖峰等等。顺便说一下,我建议您避免使用 T
作为变量名 - 它可能会导致混淆,因为在 numpy 中 .T
用于获取数组的转置。
要获得每个细胞的尖峰计数,您需要遍历 t
:
cell_counts = np.empty((t.shape[0], bins.shape[0] - 1), np.int)
for ii, row in enumerate(t):
cell_counts[ii], edges = np.histogram(row[row != 0], bins)
print(cell_counts)
# [[0 1 0 2 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 2 0 0 0]
# [0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1]
# [0 0 0 0 0 0 0 0 0 1 0 2 0 0 1 0 1 0 2 0 0 0 0]
# [0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0]
# [0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0]]
更新:
如果我没理解错的话,您想知道每 60 秒时间间隔内出现尖峰的细胞总数,而不考虑每个细胞发出的尖峰数。一种简单的方法是将 cell_counts
数组中的值截断为 1,然后沿行求和:
total_active_cells = (cell_counts > 0).sum(0)
print(total_active_cells)
# [0 1 0 2 1 2 2 1 1 2 0 1 0 1 2 0 2 0 1 2 0 1 1]