In [1]:
from __future__ import division
from neuropower import cluster
from neuropower import BUM
from neuropower import neuropowermodels
from neuropower import peakdistribution
import nibabel as nib
import nilearn.plotting
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import seaborn as sns
import scipy
import brewer2mpl
import pandas as pd
colors = brewer2mpl.get_map('Set2', 'qualitative', 5).mpl_colors
twocol = brewer2mpl.get_map('Paired', 'qualitative', 5).mpl_colors
%matplotlib inline
In [2]:
map_display=nilearn.plotting.plot_stat_map("Zstat1.nii",threshold=2,title='Z-statistic image')
In [3]:
SPM = nib.load("Zstat1.nii").get_data()
maskfile = "Mask.nii"
MASK = nib.load(maskfile).get_data()
exc = 2
In [4]:
peaks = cluster.PeakTable(SPM,exc,MASK)
pvalues = np.exp(-exc*(np.array(peaks.peak)-exc))
peaks['pval'] = [max(10**(-6),p) for p in pvalues]
peaks[1:10]
Out[4]:
In [5]:
bum = BUM.EstimatePi1(peaks['pval'],starts=10,seed=1)
bum
sns.set_style("white")
sns.set_palette(sns.color_palette("Paired"))
ax = sns.distplot(peaks['pval'],kde=False,norm_hist=True,bins=10)
ax.set(xlabel="P-values",ylabel="Frequency",title="Histogram of peak p-values")
Out[5]:
In [6]:
xn_p = np.arange(0,1,0.01)
alt_p = bum['pi1']*scipy.stats.beta.pdf(xn_p,bum['a'],1)+1-bum['pi1']
nul_p = [1-bum['pi1']]*len(xn_p)
In [7]:
ax = sns.distplot(peaks['pval'],kde=False,norm_hist=True,bins=10)
ax.set(xlabel="P-values",ylabel="Frequency",title="Histogram of peak p-values with estimated mixture distribution \n $\pi_0$="+str(1-bum['pi1']))
plt.plot(xn_p,alt_p,lw=3,color=twocol[1])
plt.plot(xn_p,nul_p,lw=3,color=twocol[0])
Out[7]:
In [8]:
plt.figure(figsize=(4.5, 4))
sns.set_palette(sns.color_palette("Paired"))
ax = sns.distplot(peaks['peak'],kde=False,norm_hist=True,bins=10)
ax.set(xlabel="peak z-values",ylabel="Frequency",title="Histogram of peak z-values (exc = 2.0)")
Out[8]:
In [9]:
modelfit = neuropowermodels.modelfit(peaks.peak,bum['pi1'],exc=exc,starts=10,method="RFT")
modelfit
Out[9]:
In [10]:
xn_t = np.arange(exc,10,0.01)
alt_t = bum['pi1']*neuropowermodels.altPDF(xn_t,modelfit['mu'],modelfit['sigma'],exc=exc)
nul_t = [1-bum['pi1']]*neuropowermodels.nulPDF(xn_t,exc=exc)
mix_t = neuropowermodels.mixPDF(xn_t,pi1=bum['pi1'],mu=modelfit['mu'],sigma=modelfit['sigma'],exc=exc)
In [11]:
plt.figure(figsize=(4.5, 4))
ax = sns.distplot(peaks['peak'],kde=False,norm_hist=True,bins=10)
ax.set(xlabel="peak z-values",ylabel="Frequency",title="Histogram of peak z-values with theoretical nul distibution",xlim=[exc,6])
plt.plot(xn_t,nul_t)
Out[11]:
In [12]:
plt.figure(figsize=(4.5, 4))
ax = sns.distplot(peaks['peak'],kde=False,norm_hist=True,bins=10)
ax.set(xlabel="peak z-values",ylabel="Frequency",title="Histogram of peak z-values with fitted distribution",xlim=[exc,6])
plt.plot(xn_t,nul_t,color=twocol[0])
plt.plot(xn_t,alt_t,color=twocol[0])
plt.plot(xn_t,mix_t,color=twocol[1])
Out[12]:
In [13]:
thresholds = neuropowermodels.threshold(peaks.peak,peaks.pval,FWHM=[8,8,8],voxsize=[3,3,3],nvox=np.sum(MASK),alpha=0.05,exc=exc)
effect_cohen = modelfit['mu']/np.sqrt(13)
power_predicted = []
newsubs = range(10,71)
for s in newsubs:
projected_effect = effect_cohen*np.sqrt(s)
powerpred = {k:1-neuropowermodels.altCDF(v,projected_effect,modelfit['sigma'],exc=exc,method="RFT") for k,v in thresholds.items() if v!='nan'}
power_predicted.append(powerpred)
power_predicted_df = pd.DataFrame(power_predicted)
thresholds
Out[13]:
In [14]:
names = ["UN","BF","RFT","BH"]
plt.figure(figsize=(4.5, 4))
ax = sns.distplot(peaks['peak'],kde=False,norm_hist=True,bins=10)
ax.set(xlabel="peak z-values",ylabel="Frequency",title="Histogram of peak z-values with theoretical nul distibution",xlim=[exc,6])
plt.plot(xn_t,nul_t)
k = 0
for method in names:
k+=1
plt.axvline(thresholds[method],color=colors[k])
In [15]:
plt.figure(figsize=(4.5, 4))
names = ["UN","BF","RFT","BH"]
for col in range(4):
plt.plot(newsubs,power_predicted_df[names[col]],color=colors[col+1])
plt.legend(loc='lower right')
plt.xlabel("subjects")
plt.ylabel("power")
plt.title("Power curves")
Out[15]:
In [16]:
power_predicted_df['newsubs'] = newsubs
power_predicted_df[0:10]
Out[16]:
In [17]:
ind = np.min([i for i,elem in enumerate(power_predicted_df['UN']) if elem>0.8])
sub = power_predicted_df['newsubs'][ind]
In [18]:
sub
Out[18]: