[docs]defcompute_fisher_z_score(correlation_file,timeseries_one_d):""" Computes the fisher z transform of the input correlation map If the correlation map contains data for multiple ROIs then the function returns z score for each ROI as a seperate nifti file Parameters ---------- correlation_file : string Input correlations file Returns ------- out_file : list (nifti files) list of z_scores for mask or ROI """importnibabelasnbimportnumpyasnpimportosroi_numbers=[]if'#'inopen(timeseries_one_d,'r').readline().rstrip('\r\n'):roi_numbers=open(timeseries_one_d,'r').readline().rstrip('\r\n').replace('#','').split('\t')corr_img=nb.load(correlation_file)corr_data=corr_img.get_fdata()hdr=corr_img.headercorr_data=np.log((1+corr_data)/(1-corr_data))/2.0dims=corr_data.shapeout_file=[]iflen(dims)==5orlen(roi_numbers)>0:iflen(dims)==5:x,y,z,one,roi_number=dimscorr_data=np.reshape(corr_data,(x*y*z,roi_number),order='F')foriinrange(0,len(roi_numbers)):sub_data=corr_dataiflen(dims)==5:sub_data=np.reshape(corr_data[:,i],(x,y,z),order='F')sub_img=nb.Nifti1Image(sub_data,header=corr_img.header,affine=corr_img.affine)sub_z_score_file=os.path.join(os.getcwd(),'z_score_ROI_number_%s.nii.gz'%(roi_numbers[i]))sub_img.to_filename(sub_z_score_file)out_file.append(sub_z_score_file)else:z_score_img=nb.Nifti1Image(corr_data,header=hdr,affine=corr_img.affine)z_score_file=os.path.join(os.getcwd(),'z_score.nii.gz')z_score_img.to_filename(z_score_file)out_file.append(z_score_file)returnout_file
defcheck_ts(in_file):importosimportnumpyasnpifin_file.endswith('.txt'):try:timepoints,rois=np.loadtxt(in_file).shapeexceptValueError:timepoints=np.loadtxt(in_file).shape[0]rois=1out_file=in_fileelifin_file.endswith('.csv')orin_file.endswith('.1D'):csv_array=np.genfromtxt(in_file,delimiter=',')ifnp.isnan(csv_array[0][0]):csv_array=csv_array[1:]timepoints,rois=csv_array.shape# multiple regression (fsl_glm) needs this format for -design inputout_file=os.path.join(os.getcwd(),os.path.basename(in_file).replace('.csv','.txt'))np.savetxt(out_file,csv_array,delimiter='\t')ifrois>timepoints:message=('\n\n\n****The number of timepoints ('+str(timepoints)+') is smaller than the number of ROIs to run ('+str(rois)+') - therefore the GLM is'+' underspecified and can\'t run.****\n\n\n')print(message)raiseException(message)else:returnout_file
[docs]defmap_to_roi(timeseries,maps):""" Renames the outputs of the temporal multiple regression workflow for sca according to the header information of the timeseries.txt file that was passed NOTE: This is only run if the temporal regression is run as part of sca (which = 'RT') when calling the temporal regression workflow. If you run the temporal regression workflow manually, don\'t set (which = 'RT') unless you provide a timeseries.txt file with a header containing the names of the timeseries Parameters ---------- timeseries : string Input timeseries.txt file maps : List (nifti files) List of output files generated by the temporal regression workflow if (which == 'RT') Returns ------- labels : List (strings) List of names that the output files should be renamed to maps : List (nifti files) List of output files generated by the temporal regression workflow if (which == 'RT') """importnumpyasnpimportpandasaspdfromnipypeimportlogginglogger=logging.getLogger('nipype.workflow')testMat=pd.read_csv(timeseries)timepoints,rois=testMat.shapeifrois>timepoints:logger.warning('The number of timepoints is smaller than the number ''of ROIs to run - therefore the GLM is ''underspecified and can\'t run.')# pull in the ROI labels from the header of the extracted timeseries# CSV filewithopen(timeseries,"r")asf:roi_file_lines=f.read().splitlines()roi_err="\n\n[!] The output of 3dROIstats, used in extracting " \
"the timeseries, was not in the expected format.\n\nROI " \
"output file: {0}\n\n".format(timeseries)forlineinroi_file_lines:if"Mean_"inline:try:roi_list=line.split(",")# clear out any blank strings/non ROI labels in the listroi_list=[xforxinroi_listif"Mean"inx]# rename labelsroi_list= \
[x.replace("Mean","sca_tempreg_z_maps_roi").replace(" ","").replace("#","")forxinroi_list]except:raiseException(roi_err)breakelse:raiseException(roi_err)new_labels=[]forroi_labelinroi_list:new_labels.append(os.path.join(os.getcwd(),roi_label))numMaps=len(maps)maps.sort()# if not numMaps / 2 == rois:# raise Exception('You specified {0} timeseries but only {1} spatial '# 'maps were generated'.format(str(rois),# str(numMaps/2)))maps=maps[:rois]returnroi_list,maps