def neuropowermodel(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowermodel.html" steps = get_neuropower_steps(template,sid) context = {"steps":steps} if not ParameterModel.objects.filter(SID=sid): # We should not be able to get to this step context["text"] = "No data found. Go to 'Input' and fill out the form." return render(request,template,context) else: parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] peakdata = PeakTableModel.objects.filter(SID=sid)[::-1][0] peaks = peakdata.data bum = BUM.bumOptim(peaks.pval.tolist(),starts=10) # :) modelfit = neuropowermodels.modelfit(peaks.peak.tolist(), bum['pi1'], exc = float(parsdata.ExcZ), starts=10, method="RFT") mixtureform = MixtureForm() form = mixtureform.save(commit=False) form.SID = sid form.pi1 = bum['pi1'] form.a = bum['a'] form.mu = modelfit['mu'] form.sigma = modelfit['sigma'] form.save() return render(request,template,context)
def neuropowercrosstab(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowercrosstab.html" steps = get_neuropower_steps(template, sid) context = {"steps": steps} powerinputform = PowerForm(request.POST or None) if not MixtureModel.objects.filter(SID=sid): context[ "text"] = "Before doing any power calculations, the distribution of effects has to be estimated. Please go to 'Model Fit'to initiate and inspect the fit of the mixture model to the distribution." if not ParameterModel.objects.filter(SID=sid): context["text"] = "No data found. Go to 'Input' and fill out the form." else: powerdata = PowerTableModel.objects.filter(SID=sid)[::-1][0] powertable = powerdata.data[['newsamplesize', 'RFT', 'BF', 'BH', 'UN']].round(decimals=2) powertable.columns = [ 'Sample Size', 'Random Field Theory', 'Bonferroni', 'Benjamini-Hochberg', 'Uncorrected' ] context["power"] = powertable.to_html(index=False, col_space='120px', classes=["table table-striped"]) return render(request, template, context)
def neuropowerviewer(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowerviewer.html" steps = get_neuropower_steps(template,sid) viewer = "" url = "" text = "" thr = "" if not ParameterModel.objects.filter(SID=sid): text = "Please first fill out the input." else: parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] if parsdata.url == "": text = "The viewer is only available for publicly available data (specify a url in the input)." else: url = parsdata.url thr = parsdata.Exc viewer = "<div class='papaya' data-params='params'></div>" context = {"url":url, "viewer":viewer, "text":text, "thr":thr, "steps":steps} return render(request,template,context)
def neuropowermodel(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowermodel.html" steps = get_neuropower_steps(template, sid) context = {"steps": steps} if not ParameterModel.objects.filter(SID=sid): # We should not be able to get to this step context["text"] = "No data found. Go to 'Input' and fill out the form." return render(request, template, context) else: parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] peakdata = PeakTableModel.objects.filter(SID=sid)[::-1][0] peaks = peakdata.data bum = BUM.bumOptim(peaks.pval.tolist(), starts=10) # :) modelfit = neuropowermodels.modelfit(peaks.peak.tolist(), bum['pi1'], exc=float(parsdata.ExcZ), starts=10, method="RFT") mixtureform = MixtureForm() form = mixtureform.save(commit=False) form.SID = sid form.pi1 = bum['pi1'] form.a = bum['a'] form.mu = modelfit['mu'] form.sigma = modelfit['sigma'] form.save() return render(request, template, context)
def neuropowerviewer(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowerviewer.html" steps = get_neuropower_steps(template, sid) viewer = "" url = "" text = "" thr = "" if not ParameterModel.objects.filter(SID=sid): text = "Please first fill out the input." else: parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] if parsdata.url == "": text = "The viewer is only available for publicly available data (specify a url in the input)." else: url = parsdata.url thr = parsdata.Exc viewer = "<div class='papaya' data-params='params'></div>" context = { "url": url, "viewer": viewer, "text": text, "thr": thr, "steps": steps } return render(request, template, context)
def neuropowertable(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowertable.html" steps = get_neuropower_steps(template,sid) context = {"steps":steps} if not ParameterModel.objects.filter(SID=sid): # Should not be able to reach this condition context["text"] = "No data found. Go to 'Input' and fill out the form." return render(request,template,context) else: sid = request.session.session_key #why are we getting session id again? parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] SPM = nib.load(parsdata.location).get_data() if parsdata.ZorT == 'T': SPM = -norm.ppf(t.cdf(-SPM,df=float(parsdata.DoF))) cluster.cluster(SPM,parsdata.ExcZ,parsdata.peaktable) peaks = pd.read_csv(parsdata.peaktable,sep="\t") if len(peaks) < 30: context["text"] = "There are too few peaks for a good estimation. Either the ROI is too small or the screening threshold is too high." else: pvalues = np.exp(-float(parsdata.ExcZ)*(np.array(peaks.peak)-float(parsdata.ExcZ))) pvalues = [max(10**(-6),p) for p in pvalues] peaks['pval'] = pvalues peakform = PeakTableForm() form = peakform.save(commit=False) form.SID = sid form.data = peaks form.save() context["peaks"] = peaks.to_html(classes=["table table-striped"]) return render(request,template,context)
def neuropowersamplesize(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowersamplesize.html" steps = get_neuropower_steps(template,sid) context = {"steps":steps} powerinputform = PowerForm(request.POST or None) if not ParameterModel.objects.filter(SID=sid): texttop = "No data found. Go to 'Input' and fill out the form." elif not MixtureModel.objects.filter(SID=sid): texttop = "Before doing any power calculations, the distribution of effects has to be estimated. Please go to 'Model Fit'to initiate and inspect the fit of the mixture model to the distribution." else: texttop = "Hover over the lines to see detailed power predictions" parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] peakdata = PeakTableModel.objects.filter(SID=sid)[::-1][0] mixdata = MixtureModel.objects.filter(SID=sid)[::-1][0] peaks = peakdata.data thresholds = neuropowermodels.threshold(peaks.peak,peaks.pval,FWHM=8,nvox=float(parsdata.nvox),alpha=0.05,exc=float(parsdata.ExcZ)) effect_cohen = float(mixdata.mu)/np.sqrt(float(parsdata.Subj)) power_predicted = [] newsubs = range(parsdata.Subj,301) for s in newsubs: projected_effect = float(effect_cohen)*np.sqrt(float(s)) powerpred = {k:1-neuropowermodels.altCDF(v,projected_effect,float(mixdata.sigma),exc=float(parsdata.ExcZ),method="RFT") for k,v in thresholds.items()} power_predicted.append(powerpred) powertable = pd.DataFrame(power_predicted) powertable['newsamplesize']=newsubs powertableform = PowerTableForm() savepowertableform = powertableform.save(commit=False) savepowertableform.SID = sid savepowertableform.data = powertable savepowertableform.save() powerinputform = PowerForm(request.POST or None) plothtml = plotPower(sid)['code'] if request.method == "POST": if powerinputform.is_valid(): savepowerinputform = powerinputform.save(commit=False) savepowerinputform.SID = sid savepowerinputform.save() powerinputdata = PowerModel.objects.filter(SID=sid)[::-1][0] pow = float(powerinputdata.reqPow) ss = powerinputdata.reqSS plotpower = plotPower(sid,powerinputdata.MCP,pow,ss) plothtml = plotpower['code'] context["textbottom"] = plotpower['text'] context["texttop"] = texttop context["plothtml"] = plothtml context["powerinputform"] = powerinputform return render(request,template,context)
def neuropowerstart(request): '''step 1: start''' # Get the template/step status sid = get_session_id(request) print(sid) template = "neuropower/neuropowerstart.html" steps = get_neuropower_steps(template, sid) context = {"steps": steps} return render(request, template, context)
def neuropowerviewer(request): # Get the template sid = get_session_id(request) template = "neuropower/neuropowerviewer.html" context = {} parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] if parsdata.url == "": context[ "text"] = "The viewer is only available for publicly available data (specify a url in the input)." else: context["url"] = parsdata.url context["thr"] = parsdata.Exc context["viewer"] = "<div class='papaya' data-params='params'></div>" context["steps"] = get_neuropower_steps(template, sid) return render(request, template, context)
def neuropowermodel(request): # Get the template sid = get_session_id(request) template = "neuropower/neuropowermodel.html" context = {} # Load model data parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] peakdata = PeakTableModel.objects.filter(SID=sid)[::-1][0] if not peakdata.err == "": context["text"] = peakdata.err return render(request, template, context) peaks = peakdata.data # Estimate pi1 bum = BUM.bumOptim(peaks.pval.tolist(), starts=10) # :) # Estimate mixture model modelfit = neuropowermodels.modelfit(peaks.peak.tolist(), bum['pi1'], exc=float(parsdata.ExcZ), starts=10, method="RFT") # Save estimates to form mixtureform = MixtureForm() form = mixtureform.save(commit=False) form.SID = sid form.pi1 = bum['pi1'] form.a = bum['a'] form.mu = modelfit['mu'] form.sigma = modelfit['sigma'] form.save() # Get step status context["steps"] = get_neuropower_steps(template, sid) return render(request, template, context)
def neuropowertable(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowertable.html" context = {} # Initiate peak table peakform = PeakTableForm() form = peakform.save(commit=False) form.SID = sid # Load model data parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] # Compute peaks SPM = nib.load(parsdata.location).get_data() MASK = nib.load(parsdata.masklocation).get_data() if parsdata.ZorT == 'T': SPM = -norm.ppf(t.cdf(-SPM, df=float(parsdata.DoF))) peaks = cluster.cluster(SPM, float(parsdata.ExcZ), MASK) if len(peaks) < 30: context[ "text"] = "There are too few peaks for a good estimation. Either the ROI is too small or the screening threshold is too high." form.err = context["text"] else: pvalues = np.exp(-float(parsdata.ExcZ) * (np.array(peaks.peak) - float(parsdata.ExcZ))) pvalues = [max(10**(-6), p) for p in pvalues] peaks['pval'] = pvalues form.data = peaks context["peaks"] = peaks.to_html(classes=["table table-striped"]) form.save() # Get step status context["steps"] = get_neuropower_steps(template, sid) return render(request, template, context)
def neuropowertable(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowertable.html" steps = get_neuropower_steps(template, sid) context = {"steps": steps} if not ParameterModel.objects.filter(SID=sid): # Should not be able to reach this condition context["text"] = "No data found. Go to 'Input' and fill out the form." return render(request, template, context) else: sid = request.session.session_key #why are we getting session id again? parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] SPM = nib.load(parsdata.location).get_data() if parsdata.ZorT == 'T': SPM = -norm.ppf(t.cdf(-SPM, df=float(parsdata.DoF))) cluster.cluster(SPM, parsdata.ExcZ, parsdata.peaktable) peaks = pd.read_csv(parsdata.peaktable, sep="\t") if len(peaks) < 30: context[ "text"] = "There are too few peaks for a good estimation. Either the ROI is too small or the screening threshold is too high." else: pvalues = np.exp(-float(parsdata.ExcZ) * (np.array(peaks.peak) - float(parsdata.ExcZ))) pvalues = [max(10**(-6), p) for p in pvalues] peaks['pval'] = pvalues peakform = PeakTableForm() form = peakform.save(commit=False) form.SID = sid form.data = peaks form.save() context["peaks"] = peaks.to_html(classes=["table table-striped"]) return render(request, template, context)
def neuropowercrosstab(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowercrosstab.html" context = {} # Load model data peakdata = PeakTableModel.objects.filter(SID=sid)[::-1][0] if not peakdata.err == "": context["text"] = peakdata.err return render(request, template, context) powerdata = PowerTableModel.objects.filter(SID=sid)[::-1][0] # Restyle power table for export names = powerdata.data.columns.tolist()[:-1] names.insert(0, 'newsamplesize') powertable = powerdata.data[names].round(decimals=2) repldict = { 'BF': 'Bonferroni', 'BH': 'Benjamini-Hochberg', 'UN': 'Uncorrected', 'RFT': 'Random Field Theory', 'newsamplesize': 'Samplesize' } for word, initial in repldict.items(): names = [i.replace(word, initial) for i in names] powertable.columns = names context["power"] = powertable.to_html(index=False, col_space='120px', classes=["table table-striped"]) # Get step status context["steps"] = get_neuropower_steps(template, sid) return render(request, template, context)
def neuropowercrosstab(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowercrosstab.html" steps = get_neuropower_steps(template,sid) context = {"steps":steps} powerinputform = PowerForm(request.POST or None) if not MixtureModel.objects.filter(SID=sid): context["text"] = "Before doing any power calculations, the distribution of effects has to be estimated. Please go to 'Model Fit'to initiate and inspect the fit of the mixture model to the distribution." if not ParameterModel.objects.filter(SID=sid): context["text"] = "No data found. Go to 'Input' and fill out the form." else: powerdata = PowerTableModel.objects.filter(SID=sid)[::-1][0] powertable = powerdata.data[['newsamplesize','RFT','BF','BH','UN']].round(decimals=2) powertable.columns=['Sample Size','Random Field Theory','Bonferroni','Benjamini-Hochberg','Uncorrected'] context["power"] = powertable.to_html(index=False,col_space='120px',classes=["table table-striped"]) return render(request,template,context)
def neuropowersamplesize(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowersamplesize.html" steps = get_neuropower_steps(template, sid) context = {"steps": steps} powerinputform = PowerForm(request.POST or None) if not ParameterModel.objects.filter(SID=sid): texttop = "No data found. Go to 'Input' and fill out the form." elif not MixtureModel.objects.filter(SID=sid): texttop = "Before doing any power calculations, the distribution of effects has to be estimated. Please go to 'Model Fit'to initiate and inspect the fit of the mixture model to the distribution." else: texttop = "Hover over the lines to see detailed power predictions" parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] peakdata = PeakTableModel.objects.filter(SID=sid)[::-1][0] mixdata = MixtureModel.objects.filter(SID=sid)[::-1][0] peaks = peakdata.data thresholds = neuropowermodels.threshold(peaks.peak, peaks.pval, FWHM=8, nvox=float(parsdata.nvox), alpha=0.05, exc=float(parsdata.ExcZ)) effect_cohen = float(mixdata.mu) / np.sqrt(float(parsdata.Subj)) power_predicted = [] newsubs = range(parsdata.Subj, 301) for s in newsubs: projected_effect = float(effect_cohen) * np.sqrt(float(s)) powerpred = { k: 1 - neuropowermodels.altCDF(v, projected_effect, float(mixdata.sigma), exc=float(parsdata.ExcZ), method="RFT") for k, v in thresholds.items() } power_predicted.append(powerpred) powertable = pd.DataFrame(power_predicted) powertable['newsamplesize'] = newsubs powertableform = PowerTableForm() savepowertableform = powertableform.save(commit=False) savepowertableform.SID = sid savepowertableform.data = powertable savepowertableform.save() powerinputform = PowerForm(request.POST or None) plothtml = plotPower(sid)['code'] if request.method == "POST": if powerinputform.is_valid(): savepowerinputform = powerinputform.save(commit=False) savepowerinputform.SID = sid savepowerinputform.save() powerinputdata = PowerModel.objects.filter(SID=sid)[::-1][0] pow = float(powerinputdata.reqPow) ss = powerinputdata.reqSS plotpower = plotPower(sid, powerinputdata.MCP, pow, ss) plothtml = plotpower['code'] context["textbottom"] = plotpower['text'] context["texttop"] = texttop context["plothtml"] = plothtml context["powerinputform"] = powerinputform return render(request, template, context)
def neuropowerinput(request, neurovault_id=None, end_session=False): '''step 2: input''' # Create the session id for the user sid = get_session_id(request) # Get the template/step status template = "neuropower/neuropowerinput.html" steps = get_neuropower_steps(template, sid) parsform = ParameterForm(request.POST or None, request.FILES or None, default_url="URL to nifti image", err="") neurovault_id = request.GET.get('neurovault', '') context = {"steps": steps} # If the user has ended their session, give message if end_session == True: context["message"] = "Session has been successfully reset." if neurovault_id: neurovault_image = get_url( "http://neurovault.org/api/images/%s/?format=json" % (neurovault_id)) collection_id = str(neurovault_image['collection_id']) neurovault_collection = get_url( "http://neurovault.org/api/collections/%s/?format=json" % (collection_id)) parsform = ParameterForm( request.POST or None, request.FILES or None, default_url="", err="", initial={ "url": neurovault_image["file"], "ZorT": "T" if neurovault_image["map_type"] == "T map" else "Z", "Subj": neurovault_collection["number_of_subjects"] }) context["parsform"] = parsform # fields = ['url','spmfile','maskfile','ZorT','Exc','Subj','Samples', # 'alpha','Smoothx','Smoothy','Smoothz','Voxx','Voxy','Voxz'] return render(request, template, context) if not request.method == "POST" or not parsform.is_valid(): context["parsform"] = parsform return render(request, template, context) else: form = parsform.save(commit=False) form.SID = sid mapID = "%s_%s" % (str(sid), str(uuid.uuid4())) form.mapID = mapID peaktable = os.path.join(settings.MEDIA_ROOT, "peaktables", "peaks_%s.csv" % (mapID)) form.peaktable = peaktable form.save() # handle data: copy to temporary location parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] mapID = "%s_%s" % (str(sid), str(uuid.uuid4())) form.mapID = mapID if not parsdata.url == "": url = parsform.cleaned_data['url'] location = utils.create_temporary_copy(url, mapID, mask=False, url=True) elif not parsdata.spmfile == "": spmfile = os.path.join(settings.MEDIA_ROOT, str(parsdata.spmfile)) location = utils.create_temporary_copy(spmfile, mapID, mask=False, url=False) form.location = location form.save() parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] SPM = nib.load(parsdata.location) # check if the IQR is realistic (= check whether these are Z- or T-values) IQR = np.subtract(*np.percentile(SPM.get_data(), [75, 25])) if IQR > 20: parsform = ParameterForm(request.POST or None, request.FILES or None, default_url="URL to nifti image", err="median") context["parsform"] = parsform return render(request, template, context) # save other parameters form.DoF = parsdata.Subj - 1 if parsdata.Samples == 1 else parsdata.Subj - 2 form.ExcZ = float(parsdata.Exc) if float( parsdata.Exc) > 1 else -norm.ppf(float(parsdata.Exc)) # handle mask if parsdata.maskfile == "": mask = masking.compute_background_mask(SPM, border_size=2, opening=True) nvox = np.sum(mask.get_data()) form.nvox = nvox else: maskfile = os.path.join(settings.MEDIA_ROOT, str(parsdata.maskfile)) masklocation = utils.create_temporary_copy(maskfile, mapID, mask=True, url=False) mask = nib.load(masklocation).get_data() # return error when dimensions are different if SPM.get_data().shape != mask.shape: parsform = ParameterForm(request.POST or None, request.FILES or None, default_url="URL to nifti image", err="dim") context["parsform"] = parsform return render(request, template, context) else: SPM_masked = np.multiply(SPM.get_data(), mask) SPM_nib = nib.Nifti1Image(SPM_masked, np.eye(4)) nib.save(SPM_nib, parsdata.location) form.nvox = np.sum(mask) form.save() if parsdata.spmfile == "": return HttpResponseRedirect('/neuropowerviewer/') else: return HttpResponseRedirect('/neuropowertable/')
def neuropowerinput(request,neurovault_id=None,end_session=False): '''step 2: input''' # Create the session id for the user sid = get_session_id(request) # Get the template/step status template = "neuropower/neuropowerinput.html" steps = get_neuropower_steps(template,sid) parsform = ParameterForm(request.POST or None, request.FILES or None, default_url="URL to nifti image", err="") neurovault_id = request.GET.get('neurovault','') context = {"steps":steps} # If the user has ended their session, give message if end_session == True: context["message"] = "Session has been successfully reset." if neurovault_id: neurovault_image = get_url("http://neurovault.org/api/images/%s/?format=json" %(neurovault_id)) collection_id = str(neurovault_image['collection_id']) neurovault_collection = get_url("http://neurovault.org/api/collections/%s/?format=json" %(collection_id)) parsform = ParameterForm(request.POST or None, request.FILES or None, default_url = "", err = "", initial = {"url":neurovault_image["file"], "ZorT":"T" if neurovault_image["map_type"] =="T map" else "Z", "Subj":neurovault_collection["number_of_subjects"]}) context["parsform"] = parsform # fields = ['url','spmfile','maskfile','ZorT','Exc','Subj','Samples', # 'alpha','Smoothx','Smoothy','Smoothz','Voxx','Voxy','Voxz'] return render(request,template,context) if not request.method=="POST" or not parsform.is_valid(): context["parsform"] = parsform return render(request,template,context) else: form = parsform.save(commit=False) form.SID = sid mapID = "%s_%s" %(str(sid),str(uuid.uuid4())) form.mapID = mapID peaktable = os.path.join(settings.MEDIA_ROOT,"peaktables","peaks_%s.csv" %(mapID)) form.peaktable = peaktable form.save() # handle data: copy to temporary location parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] mapID = "%s_%s" %(str(sid),str(uuid.uuid4())) form.mapID = mapID if not parsdata.url == "": url = parsform.cleaned_data['url'] location = utils.create_temporary_copy(url,mapID,mask=False,url=True) elif not parsdata.spmfile == "": spmfile = os.path.join(settings.MEDIA_ROOT,str(parsdata.spmfile)) location = utils.create_temporary_copy(spmfile,mapID,mask=False, url=False) form.location = location form.save() parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] SPM = nib.load(parsdata.location) # check if the IQR is realistic (= check whether these are Z- or T-values) IQR = np.subtract(*np.percentile(SPM.get_data(),[75,25])) if IQR > 20: parsform = ParameterForm(request.POST or None, request.FILES or None, default_url = "URL to nifti image", err = "median") context["parsform"] = parsform return render(request,template,context) # save other parameters form.DoF = parsdata.Subj-1 if parsdata.Samples==1 else parsdata.Subj-2 form.ExcZ = float(parsdata.Exc) if float(parsdata.Exc)>1 else -norm.ppf(float(parsdata.Exc)) # handle mask if parsdata.maskfile == "": mask = masking.compute_background_mask(SPM,border_size=2, opening=True) nvox = np.sum(mask.get_data()) form.nvox = nvox else: maskfile = os.path.join(settings.MEDIA_ROOT,str(parsdata.maskfile)) masklocation = utils.create_temporary_copy(maskfile,mapID,mask=True,url=False) mask = nib.load(masklocation).get_data() # return error when dimensions are different if SPM.get_data().shape != mask.shape: parsform = ParameterForm(request.POST or None, request.FILES or None, default_url="URL to nifti image", err="dim") context["parsform"] = parsform return render(request,template,context) else: SPM_masked = np.multiply(SPM.get_data(),mask) SPM_nib = nib.Nifti1Image(SPM_masked,np.eye(4)) nib.save(SPM_nib,parsdata.location) form.nvox = np.sum(mask) form.save() if parsdata.spmfile == "": return HttpResponseRedirect('/neuropowerviewer/') else: return HttpResponseRedirect('/neuropowertable/')
def plotModel(request): plt.switch_backend('agg') sid = get_session_id(request) mixdata = MixtureModel.objects.filter(SID=sid)[::-1][0] parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] peakdata = PeakTableModel.objects.filter(SID=sid)[::-1][0] peaks = peakdata.data twocol = Paired_12.mpl_colors xn = np.arange(-10, 30, 0.01) nul = [1 - float(mixdata.pi1)] * npm.nulPDF( xn, exc=float(parsdata.ExcZ), method="RFT") alt = float(mixdata.pi1) * npm.altPDF(xn, mu=float(mixdata.mu), sigma=float(mixdata.sigma), exc=float(parsdata.ExcZ), method="RFT") mix = npm.mixprobdens(xn, pi1=float(mixdata.pi1), mu=float(mixdata.mu), sigma=float(mixdata.sigma), exc=2, method="RFT") xn_p = np.arange(0, 1, 0.01) alt_p = [1 - float(mixdata.pi1)] * scipy.stats.beta.pdf( xn_p, float(mixdata.a), 1) + 1 - float(mixdata.pi1) null_p = [1 - float(mixdata.pi1)] * len(xn_p) mpl.rcParams['font.size'] = '11.0' fig, axs = plt.subplots(1, 2, figsize=(14, 5)) fig.patch.set_facecolor('None') fig.subplots_adjust(hspace=.5, wspace=0.3) axs = axs.ravel() axs[0].hist(peaks.pval, lw=0, normed=True, facecolor=twocol[0], bins=np.arange(0, 1.1, 0.1), label="observed distribution") axs[0].set_ylim([0, 3]) axs[0].plot(xn_p, null_p, color=twocol[3], lw=2, label="null distribution") axs[0].plot(xn_p, alt_p, color=twocol[5], lw=2, label="alternative distribution") axs[0].legend(loc="upper right", frameon=False) axs[0].set_title("Distribution of " + str(len(peaks)) + " peak p-values \n $\pi_1$ = " + str(round(float(mixdata.pi1), 2))) axs[0].set_xlabel("Peak p-values") axs[0].set_ylabel("Density") axs[1].hist(peaks.peak, lw=0, facecolor=twocol[0], normed=True, bins=np.arange(min(peaks.peak), 30, 0.3), label="observed distribution") axs[1].set_xlim([float(parsdata.ExcZ), np.max(peaks.peak)]) axs[1].set_ylim([0, 1]) axs[1].plot(xn, nul, color=twocol[3], lw=2, label="null distribution") axs[1].plot(xn, alt, color=twocol[5], lw=2, label="alternative distribution") axs[1].plot(xn, mix, color=twocol[1], lw=2, label="total distribution") peak_heights_string = str( round(float(mixdata.mu) / np.sqrt(parsdata.Subj), 2)) axs[1].set_title("Distribution of peak heights \n $\delta_1$ = %s" % (peak_heights_string)) axs[1].set_xlabel("Peak heights (z-values)") axs[1].set_ylabel("Density") axs[1].legend(loc="upper right", frameon=False) canvas = FigureCanvas(fig) response = HttpResponse(content_type='image/png') canvas.print_png(response) return response
def neuropowersamplesize(request): # Get the template/step status sid = get_session_id(request) template = "neuropower/neuropowersamplesize.html" context = {} # Load model data context[ 'texttop'] = "Hover over the lines to see detailed power predictions" parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] peakdata = PeakTableModel.objects.filter(SID=sid)[::-1][0] if not peakdata.err == "": context["text"] = peakdata.err return render(request, template, context) mixdata = MixtureModel.objects.filter(SID=sid)[::-1][0] peaks = peakdata.data # Estimate smoothness if parsdata.SmoothEst == 1: #Manual FWHM = np.array([parsdata.Smoothx, parsdata.Smoothy, parsdata.Smoothz]) voxsize = np.array([parsdata.Voxx, parsdata.Voxy, parsdata.Voxz]) elif parsdata.SmoothEst == 2: # Estimate from data cmd_smooth = "smoothest -V -z " + parsdata.location + " -m " + parsdata.masklocation tmp = os.popen(cmd_smooth).read() FWHM = np.array( [float(x[8:15]) for x in tmp.split("\n")[16].split(",")]) voxsize = np.array([1, 1, 1]) # Compute thresholds and standardised effect size thresholds = neuropowermodels.threshold(peaks.peak, peaks.pval, FWHM=FWHM, voxsize=voxsize, nvox=float(parsdata.nvox), alpha=float(parsdata.alpha), exc=float(parsdata.ExcZ)) effect_cohen = float(mixdata.mu) / np.sqrt(float(parsdata.Subj)) # Compute predicted power power_predicted = [] newsubs = range(parsdata.Subj, parsdata.Subj + 600) for s in newsubs: projected_effect = float(effect_cohen) * np.sqrt(float(s)) powerpred = { k: 1 - neuropowermodels.altCDF([v], projected_effect, float(mixdata.sigma), exc=float(parsdata.ExcZ), method="RFT")[0] for k, v in thresholds.items() if not v == 'nan' } power_predicted.append(powerpred) # Check if there are thresholds (mainly BH) missing missing = [k for k, v in thresholds.items() if v == 'nan'] if len(missing) > 0: context[ 'MCPwarning'] = "There is not enough power to estimate a threshold for " + " and ".join( missing) + "." # Save power calculation to table and model powertable = pd.DataFrame(power_predicted) powertable['newsamplesize'] = newsubs powertableform = PowerTableForm() savepowertableform = powertableform.save(commit=False) savepowertableform.SID = sid savepowertableform.data = powertable savepowertableform.save() powerinputform = PowerForm(request.POST or None) context['plothtml'] = plotPower(sid)['code'] # Adjust plot with specific power or sample size question if request.method == "POST": if powerinputform.is_valid(): savepowerinputform = powerinputform.save(commit=False) savepowerinputform.SID = sid savepowerinputform.save() powerinputdata = PowerModel.objects.filter(SID=sid)[::-1][0] pow = float(powerinputdata.reqPow) ss = powerinputdata.reqSS plotpower = plotPower(sid, powerinputdata.MCP, pow, ss) context['plothtml'] = plotpower['code'] context["textbottom"] = plotpower['text'] context["powerinputform"] = powerinputform # Get step status context["steps"] = get_neuropower_steps(template, sid) return render(request, template, context)