使用基于python的neurora进行表征相似性分析(RSA)

发现了一款用于表征分析的Python API,还是挺方便的,这里简单介绍一下进行表征相似性分析的使用方法。

官方文档见https://neurora.github.io/documentation/index.html

下载

pip install neurora

表征相似性分析的知识可见:

https://zhuanlan.zhihu.com/p/363691154?utm_id=0

一、得到矩阵不相似性矩阵(Representational Dissimilarity Matrix,RDM)

这里随意创建了一个矩阵,使用了neurora里面的对人类行为分析构造RDM的函数,当然也可以选别的构造RDM的函数。

import neurora.rdm_cal as rc
import neurora.rsa_plot
import neurora.rdm_corr
import torch
import torchvision as tv
import numpy as np
# 构造数据
bhv_data = np.random.randn(5,5,4) #  the number of conidtions, the number of subjects, the number of trials
'''
Calculate the subject-result or average-result.

The method to calculate the dissimilarities.
If method='correlation', the dissimilarity is calculated by 
Pearson Correlation. If method='euclidean', the dissimilarity 
is calculated by Euclidean Distance, the results will be 
normalized. If method='mahalanobis', the dissimilarity is 
calculated by Mahalanobis Distance, the results will be 
normalized.

Calculate the absolute value of Pearson r or not.
'''
# 得到RDM矩阵
rdm = rc.bhvRDM(bhv_data, sub_opt=0, method='correlation', abs=False)
print(rdm.shape)
# 绘制图
neurora.rsa_plot.plot_rdm(rdm, lim=[0, 1], rescale=False, conditions=None, cmap=None)
# 绘制带数值的图
neurora.rsa_plot.plot_rdm_withvalue(rdm, lim=[0, 1], value_fontsize=10, conditions=None, con_fontsize=12, cmap='rainbow')

绘制出的两张图如下:

使用基于python的neurora进行表征相似性分析(RSA)_第1张图片
使用基于python的neurora进行表征相似性分析(RSA)_第2张图片

二、进行RSA分析

这里随机创建两个数据,对他们的RDM进行相关性检验

bhv_data1 = np.random.randn(5,5,4) #  the number of conidtions, the number of subjects, the number of trials
bhv_data2 = np.random.randn(5,5,4) #  the number of conidtions, the number of subjects, the number of trials
rdm1 = rc.bhvRDM(bhv_data1, sub_opt=0, method='correlation', abs=False)
rdm2 = rc.bhvRDM(bhv_data2, sub_opt=0, method='correlation', abs=False)

# 使用pearson,当然也可以用spearman等,如rdm_correlation_spearman
spearman_result = neurora.rdm_corr.rdm_correlation_pearson(rdm1, rdm2, rescale=False, permutation=False, iter=5000)
print(spearman_result)

输出内容为皮尔森检验的r值和p值

[0.00366008 0.99199368]

p>0.05,说明不相关,这两个数据都是随机生成的,不相关很正常。

你可能感兴趣的:(python,计算神经科学,可视化,python,数据分析)