摘要:遇到此类错误,可以通过分段调试的方法找到引发错误的位置。
引发错误的原因不详,可能很基础。
---------------------------------------------------------------------------------------------------------------------------------
# Distribution of the peak number
file = pd.read_excel("......xls", sheet_name = "...")
DA_ratemap_allmice1 = getDaysAligned_Ratemap_allmice(maze_type = 1, isGood = 0)
DA_ratemap_allmice2 = getDaysAligned_Ratemap_allmice(maze_type = 2, isGood = 0)
print(np.shape(DA_ratemap_allmice1))
print(np.shape(DA_ratemap_allmice2))
# np.shape(DA_ratemap_allmice1) = (3955, 1296)
# np.shape(DA_ratemap_allmice1) = (2025, 1296)
# 自己生成一个试试,假定每个数组每一行都有一个最大值,最大值出现的位置为 x, x 属于[0,1296)
# 统计所有行中最大值在[0,1296)上出现的频数
def find_Decision_point(maze_type = 1):
graph = {
'1':[1,2],
'2':[2,3,4],
...: [...],
'144':[5,4,3],
}
co_path = [2,4,6,8,10]
inc_path = [1,3,5,7,9] #注意这是list,不是np.array()
dp = []
for key in graph.keys():
# decision points
if len(graph[key]) >= 3:
dp.append(key)
new_dp = np.zeros(len(dp))
# rank the decision point to new order:
for i in range(len(dp)):
if dp[i] in co_path:
ind = np.where(co_path == dp[i])[0]
new_dp[i] = ind+1
else:
ind = np.where(inc_path == dp[i])[0]
new_dp[i] = ind + len(co_path)+1
print(new_dp)
return new_dp
print(find_Decision_point(1))
def CountDistributionOfPeak(DA_ratemap_allmice, day, DoyouwantPlot,maze_type):
DA_ratemap_allmice_arr = np.array(DA_ratemap_allmice)
DA_ratemap = DA_ratemap_allmice_arr[:,144*(day-1):144*day]
peak_DA_ratemap = np.zeros_like(DA_ratemap)
for i in range(len(DA_ratemap)):
if np.sum(DA_ratemap[i])==0:
continue
peak_DA_ratemap[i][np.argmax(DA_ratemap[i])] = 1
Dist = np.zeros(144)
for i in range(np.shape(DA_ratemap)[1]):
for j in range(np.shape(DA_ratemap)[0]):
if peak_DA_ratemap[j][i]==1:
Dist[i] += 1
if DoyouwantPlot==0:
return Dist
else:
dp = find_Decision_point(maze_type)
plt.plot(range(1,145),Dist,'o',label = "other bins")
plt.title("maze "+str(maze_type)+' Day '+str(day))
plt.xlabel('Spatial ID')
plt.ylabel('Frequency')
for i in range(len(dp)):
plt.axvline(dp[i],color = 'black',linewidth = 1, ls = ':')
plt.plot(dp,[Dist[dp[i]-1] for i in range(len(dp))], 'o', color = 'red',label = "decision points")
plt.legend()
return Dist
dist = CountDistributionOfPeak(DA_ratemap_allmice1,0,1,1)
print(dist)
maze_type = 1
fig = plt.figure(figsize=(48,24))
for i in range(9):
print("Day",i+1)
plt.subplot(2,5,i+1)
dist = CountDistributionOfPeak(DA_ratemap_allmice1, i+1,1,maze_type)
plt.savefig(r"......",dpi=600)
plt.savefig(r"......",dpi=600)
plt.show()
fig = plt.figure(figsize=(48,24))
maze_type = 2
for i in range(9):
print("Day",i+1)
plt.subplot(2,5,i+1)
dist = CountDistributionOfPeak(DA_ratemap_allmice2, i+1,1,maze_type)
plt.savefig(r"......",dpi=600)
plt.savefig(r"......",dpi=600)
plt.show()
报错
ERROR! Session/line number was not unique in database. History logging moved to new session
debug建议:分段debug,查找产生错误的位置。
# Distribution of the peak number
file = pd.read_excel("......xls", sheet_name = "...")
DA_ratemap_allmice1 = getDaysAligned_Ratemap_allmice(maze_type = 1, isGood = 0)
DA_ratemap_allmice2 = getDaysAligned_Ratemap_allmice(maze_type = 2, isGood = 0)
print(np.shape(DA_ratemap_allmice1))
print(np.shape(DA_ratemap_allmice2))
[out]
(3955, 1296)
(2025, 1296)
接着验证函数 find_Decision_point:
bug出现了
说明该函数有问题。该函数由两部分组成,先验证其前一部分,没有问题。
再验证后一部分,发现有问题。(验证方法,用……注释掉其中一部分,然后再另一部分print()随便一些内容进行输出调试。)
之后发现,for循环内这句话有问题:
print(np.where(co_path == dp[i])[0])
if dp[i] in co_path:
ind = np.where(co_path == dp[i])[0] # co_path is list type
new_dp[i] = ind+1 # new_dp[i] = [] + 1
原来,co_path是list类型,无法用np.where()找到对应值下标。ind的结果并非是一个int,而是[](empty list)。
只要前面一个cell出现了Session/line number...的error,后边都会出现。
奇怪的是,单独试验 [] + 1其实是会报错的,这是典型的TypeError
所以,本文最终并没有解决这个bug如何出现,而是表明,这个bug有望通过修改代码本身即可解决,而不必像下文中所指的进行部分文件操作。当然这可能和不同具体情况相关,都是解决方案。ERROR! Session/line number was not unique in database. History logging moved to new session 178