目录

0.前言

1.项目背景

2.项目任务

3.任务实现

3.1 将spike波形叠画在一起,并描述可分性

3.2 使用PCA将spike分别降维至2维和3维

3.3 采用合适的聚类方法把它们分成合理类别

3.4 把Spike sorting之后的波形按照神经细胞归类,用不同颜色再次叠画在一起,观察并描述它们的波形在类内和类间的差异

4.参考代码

0.前言

本文主要分享神经建模与数据分析课程项目,旨在记录项目内容、项目实现等。大部分代码为笔者自己编写,可能存在错误之处。代码地址:https://github.com/wubeizi/Analysis-of-nerve-cell-discharge

1.项目背景

记录分析神经元活动是神经科学研究的基础,也就是对电生理的研究。电生理研究的基础就是把这些活动从原始的电极电压信号中提取出来。而Spike Sorting正是从原始的电压信号中根据不同神经元放电信号的不同特征精确分离,从而从一组电压信号对单个神经元活动进行估计。Sorting之后的AP序列分别归类到单个神经细胞,进行下一步的定量分析,寻找神经细胞放电和外界刺激以及行为的关系。1981年Hubel和Wiessel采用这种神经记录方法开创了视觉神经编码研究的新领域,并一举获得诺贝尔生理与医学奖。

2.项目任务

本实验利用包括5376个spike波形,每个波形长度40个采样点的数据文件spikewave.mat,完成以下任务:

1) 把这些spike波形叠画在一起,观察并描述它们的可分性;

2) 用PCA方法把这些spike分别降维到2维和3维空间,并画出来

3) 在2维和3维空间分别采用合适的聚类方法把它们分成合理类别,这些类别就对应于临近的神经细胞放电。

4) 把Spike sorting之后的波形按照神经细胞归类,用不同颜色再次叠画在一起,观察并描述它们的波形在类内和类间的差异

3.任务实现

3.1 将spike波形叠画在一起,并描述可分性

实验思路:样本数据中包含5376项数据,每项数据有40个样本点,可以看作波形的40个采样点,利用python画图的形式画出曲线即可

关键代码:

from scipy.interpolate import make_interp_spline

#画出波形图

t = np.arange(0, 40) #按顺序生成40个点

for i in range(5376):

#曲线平滑处理,代码参考https://blog.csdn.net/m0_48300767/article/details/130075597

m = make_interp_spline(t, spike_data[i])

xs = np.linspace(0, 40, 500)

ys = m(xs)

plt.plot(xs, ys)

plt.title('origin spike')

plt.show()

实验结果:

结果分析:波形之间杂乱无章,看不出任何排布规律,无法通过肉眼进行区分,波形的峰值和和谷值不明显,可分性较差

3.2 使用PCA将spike分别降维至2维和3维

实验思路:调用PCA方法分别降维至2维和3维,再画出降维后的波形图和散点图即可

降至3维关键代码:

#降为3维进行分析

from sklearn.decomposition import PCA #主成分分析

transfer = PCA(n_components= 3)

new_data = transfer.fit_transform(spike_data)

#波形图

t = np.arange(0, 3)

for i in range(5376):

plt.plot(t, new_data[i])

plt.title('3d spike')

plt.show()

#散点图 代码参考课件和https://blog.csdn.net/qq_40985985/article/details/119676953

fig = plt.figure()

ax = fig.add_subplot(projection='3d')

ax.scatter(new_data.T[0], new_data.T[1], new_data.T[2]) # 绘制数据点 c: 'r'红色,'y'黄色,等颜色

ax.set_title('3d scatter')

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')

plt.show()

降至2维关键代码:

#降为2维进行分析

from sklearn.decomposition import PCA #主成分分析

transfer = PCA(n_components= 2)

new_data2 = transfer.fit_transform(spike_data)

#波形图

t = np.arange(0, 2)

for i in range(5376):

plt.plot(t, new_data2[i])

plt.title('2d spike')

plt.show()

#散点图 代码参考课件和https://blog.csdn.net/qq_40985985/article/details/119676953

plt.figure()

plt.scatter(new_data2.T[0], new_data2.T[1]) # 绘制数据点 c: 'r'红色,'y'黄色,等颜色

plt.title('2d scatter')

plt.xlabel('X')

plt.ylabel('Y')

plt.show()

实验结果:

3.3 采用合适的聚类方法把它们分成合理类别

实验思路:利用K-means聚类方法,将5376项数据聚集为5类,最后根据聚类结果,将数据分成5个子集画成散点图即可

3维空间聚类关键代码:

#利用K-means进行聚类

from sklearn.cluster import KMeans

kms = KMeans(n_clusters=5) #聚类成5类

kms.fit(new_data)

centers = kms.cluster_centers_

print(centers)

#代码参考https://blog.csdn.net/icefountain/article/details/129181949

y_train = pd.Series(kms.labels_)

y_train.rename('res',inplace=True)

result = pd.concat([pd.DataFrame(new_data),y_train],axis=1)

print(result)

Category_one = result[result['res'].values == 0]

k1 = result.iloc[Category_one.index]

Category_two = result[result['res'].values == 1]

k2 = result.iloc[Category_two.index]

Category_three = result[result['res'].values == 2]

k3 =result.iloc[Category_three.index]

Category_four = result[result['res'].values == 3]

k4 =result.iloc[Category_four.index]

Category_five = result[result['res'].values == 4]

k5 =result.iloc[Category_five.index]

2维空间聚类关键代码:

#利用K-means进行聚类

from sklearn.cluster import KMeans

kms2 = KMeans(n_clusters=5) #聚类成5类

kms2.fit(new_data2)

centers = kms.cluster_centers_

print(centers)

#代码参考https://blog.csdn.net/icefountain/article/details/129181949

y_train2 = pd.Series(kms2.labels_)

y_train2.rename('res',inplace=True)

result2 = pd.concat([pd.DataFrame(new_data2),y_train2],axis=1)

print(result2)

Category_one = result2[result2['res'].values == 0]

k1 = result2.iloc[Category_one.index]

Category_two = result2[result2['res'].values == 1]

k2 = result2.iloc[Category_two.index]

Category_three = result2[result2['res'].values == 2]

k3 =result2.iloc[Category_three.index]

Category_four = result2[result2['res'].values == 3]

k4 =result2.iloc[Category_four.index]

Category_five = result2[result2['res'].values == 4]

k5 =result2.iloc[Category_five.index]

实验结果:

3.4 把Spike sorting之后的波形按照神经细胞归类,用不同颜色再次叠画在一起,观察并描述它们的波形在类内和类间的差异

实验思路:按照聚类结果,将原数据分为5类,每类用对应颜色表示,同样画出波形图即可

3维关键代码:

new_result = np.array(result)

t = np.arange(0, 40) #按顺序生成40个点

for i in range(5376):

if new_result[i][3] == 0:

m = make_interp_spline(t, spike_data[i])

xs = np.linspace(0, 40, 500)

ys = m(xs)

plot1, = plt.plot(xs, ys, color='blue')

if new_result[i][3] == 1:

m = make_interp_spline(t, spike_data[i])

xs = np.linspace(0, 40, 500)

ys = m(xs)

plot2, = plt.plot(xs, ys, color='red')

if new_result[i][3] == 2:

m = make_interp_spline(t, spike_data[i])

xs = np.linspace(0, 40, 500)

ys = m(xs)

plot3, = plt.plot(xs, ys, color='green')

if new_result[i][3] == 3:

m = make_interp_spline(t, spike_data[i])

xs = np.linspace(0, 40, 500)

ys = m(xs)

plot4, = plt.plot(xs, ys, color='black')

if new_result[i][3] == 4:

m = make_interp_spline(t, spike_data[i])

xs = np.linspace(0, 40, 500)

ys = m(xs)

plot5, = plt.plot(xs, ys, color='yellow')

plt.legend([plot1, plot2, plot3, plot4, plot5], ['cluster_one', 'cluster_two', 'cluster_three', 'cluster_four', 'cluster_five'])

plt.title('Spike sorting--3d')

plt.show()

2维关键代码:

new_result2 = np.array(result2)

t = np.arange(0, 40) #按顺序生成40个点

for i in range(5376):

if new_result2[i][2] == 0:

m = make_interp_spline(t, spike_data[i])

xs = np.linspace(0, 40, 500)

ys = m(xs)

plot1, = plt.plot(xs, ys, color='blue')

if new_result2[i][2] == 1:

m = make_interp_spline(t, spike_data[i])

xs = np.linspace(0, 40, 500)

ys = m(xs)

plot2, = plt.plot(xs, ys, color='red')

if new_result2[i][2] == 2:

m = make_interp_spline(t, spike_data[i])

xs = np.linspace(0, 40, 500)

ys = m(xs)

plot3, = plt.plot(xs, ys, color='green')

if new_result2[i][2] == 3:

m = make_interp_spline(t, spike_data[i])

xs = np.linspace(0, 40, 500)

ys = m(xs)

plot4, = plt.plot(xs, ys, color='black')

if new_result2[i][2] == 4:

m = make_interp_spline(t, spike_data[i])

xs = np.linspace(0, 40, 500)

ys = m(xs)

plot5, = plt.plot(xs, ys, color='yellow')

plt.legend([plot1, plot2, plot3, plot4, plot5], ['cluster_one', 'cluster_two', 'cluster_three', 'cluster_four', 'cluster_five'])

plt.title('Spike sorting--2d')

plt.show()

实验结果:

结果分析:聚类之后的图像每类神经细胞类内相似度较高,能够区分出峰值和谷值,类内距离较接近,同时类间区分度明显,类间距离较远,表明可分性较好

4.参考代码

波形图的绘制与平滑处理参考:

matplotlib折线图(plot)平滑处理_matplotlib折线图平滑处理-CSDN博客

3维散点图的绘制参考:

Matplotlib三维绘图,这一篇就够了_matplotlib 3d-CSDN博客

对聚类结果进行可视化参考:

Python | 实现 K-means 聚类——多维数据聚类散点图绘制_python 多维聚类-CSDN博客

推荐文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。