def plotSNRAvg(): snr_perScan = dict() subjects, subjects_lbl = utils.getBandaLabels(outliers) with open( '/autofs/space/erebus_001/users/data/scores/new2/snr_avg_output_rjj020419.csv', 'r') as csvfile: spamreader = csv.reader(csvfile, delimiter=',', quotechar='|') for row in spamreader: try: s = row[0] scan = row[1][:-1] snr = float(row[2]) if snr > 0: if not scan in snr_perScan: snr_perScan[scan] = [[], [], []] if s in subjects_lbl['control']: snr_perScan[scan][0].append(snr) elif s in subjects_lbl['anx']: snr_perScan[scan][1].append(snr) elif s in subjects_lbl['dep']: snr_perScan[scan][2].append(snr) except: print(row) f, axarr = plt.subplots(1, 6, figsize=(23, 5)) f.subplots_adjust(left=.03, bottom=.06, right=.97, top=.95, wspace=.18, hspace=.30) plotted = 0 for scanName, values in snr_perScan.items(): print(scanName) axarr[plotted].set_title(scanName) for q in range(len(values)): axarr[plotted].violinplot(values[q], [q], points=20, widths=0.3, showmeans=True) #,showmedians=True) if scanName == 'T': axarr[plotted].set_ylim((0, 15)) else: axarr[plotted].set_ylim((5, 30)) axarr[plotted].set_xticks([0, 1, 2], ["Controls", "Anxious", "Depressed"]) #f.savefig("/space/erebus/1/users/vsiless/QA_plots/snr",dpi=199) plotted += 1 for ax in axarr.flatten(): ax.set_xticks([0, 1, 2]) ax.set_xticklabels(["Controls", "Anxious", "Depressed"]) f.savefig( "/autofs/space/erebus_001/users/data/scores/new2/plots/snrAvg_140", dpi=199)
def snr(): output = csv.writer( open( '/autofs/space/erebus_001/users/data/scores/new2/snr_avg_output_vivs.csv', 'w+')) output.writerow(['subject', 'scan', 'score']) subjects, subjects_lbl = utils.getBandaLabels(outliers) for scan in scans: print(scan) for s_index, s in enumerate(subjects_lbl['allsubjects']): roi = "/space/erebus/1/users/data/preprocess/" + s + "/snr/WMROI5001_2" + studies[ scan] image = "/space/erebus/1/users/data/preprocess/" + s + "/" + studies[ scan] mean2 = "/space/erebus/1/users/data/preprocess/" + s + "/snr/mean" + studies[ scan] std2 = "/space/erebus/1/users/data/preprocess/" + s + "/snr/std" + studies[ scan] snr2 = "/space/erebus/1/users/data/preprocess/" + s + "/snr/snr" + studies[ scan] #ROI= nib.load("/space/erebus/1/users/data/preprocess/"+s+"/snr/WMROI4010_2"+studies[scan]).get_data() snr = 0 if os.path.isfile(image): #os.popen("mri_concat --i "+image+ " --mean --o "+mean2).read() #os.popen("mri_concat --i "+image+ " --std --o "+std2).read() if "T1" in scan or "T2" in scan: mean = os.popen("fslstats " + image + " -k " + roi + " -m").read() image = nib.load("/space/erebus/1/users/data/preprocess/" + s + "/" + studies[scan]).get_data() std = np.std(image[:20, :20, :20]) snr = float(mean.split("\n")[0]) / std print(snr) elif "Diffusion" in scan: mean = os.popen("fslstats " + mean2 + " -k " + roi + " -m").read() image = nib.load("/space/erebus/1/users/data/preprocess/" + s + "/" + studies[scan]).get_data() std = np.std(image[:20, :20, :20]) snr = float(mean.split("\n")[0]) / std print(snr) elif os.path.isfile(std2): #os.popen("fscalc "+mean2 +" div "+std2 +" --o " +snr2).read() res = os.popen("fslstats " + snr2 + " -k " + roi + " -m").read() snr = float(res.split("\n")[0]) output.writerow([s, scan, snr])
def plotWithinScanMotion(): perScan = [dict(), dict()] tipo = "within" #"between" labels = ["Rotation", "Translation"] subjects, subjects_lbl = utils.getBandaLabels(outliers) with open( '/space/erebus/1/users/data/scores/new2/motion_' + tipo + '_scan_output_140.csv', 'r') as csvfile: spamreader = csv.reader(csvfile, delimiter=',', quotechar='|') for row in spamreader: try: s = row[0] if "Rotation" in row[1]: ind = 0 else: ind = 1 if "T" not in row[2]: scan = row[2][:-1] else: scan = row[2] val = float(row[3]) #except: # val=0 if val > 0: if not scan in perScan[ind]: perScan[ind][scan] = [[], [], []] if s in subjects_lbl['control']: perScan[ind][scan][0].append(val) elif s in subjects_lbl['anx']: perScan[ind][scan][1].append(val) elif s in subjects_lbl['dep']: perScan[ind][scan][2].append(val) except: print(row) f, axarr = plt.subplots(2, 9, figsize=(23, 5)) f.subplots_adjust(left=.03, bottom=.06, right=.97, top=.95, wspace=.18, hspace=.30) for i in range(2): plotted = 0 for scanName, values in perScan[i].items(): print(scanName) axarr[i][plotted].set_title(scanName) for q in range(len(values)): axarr[i][plotted].violinplot( values[q], [q], points=20, widths=0.3, showmeans=True) #,showmedians=True) axarr[i][plotted].set_ylim((0, 1)) axarr[i][plotted].set_ylabel(labels[i]) axarr[i][plotted].set_xticks([0, 1, 2], ["Controls", "Anxious", "Depressed"]) # f.savefig("/space/erebus/1/users/vsiless/QA_plots/"+tipo,dpi=199) plotted += 1 for ax in axarr.flatten(): ax.set_xticks([0, 1, 2, 3]) ax.set_xticklabels( ["Controls", "Anxious", "Comorbid", "Depressed"]) # plt.show() f.savefig("/autofs/space/erebus_001/users/data/scores/new2/plots/" + tipo + "_withinScanMotion", dpi=199)
def plotLTAMovement(): subjects, subjects_lbl = utils.getBandaLabels(outliers) fg, axarr = plt.subplots(1, 2) files = [ 'dMRI_AP1_2_T1.lta_diff', 'dMRI_PA1_2_dMRI_AP1.lta_diff', 'fMRI_rest1_AP_2_dMRI_PA1.lta_diff', 'fMRI_rest2_PA_2_fMRI_rest1_AP.lta_diff', 'dMRI_AP2_2_fMRI_rest2_PA.lta_diff', 'dMRI_PA2_2_dMRI_AP2.lta_diff', 'fMRI_rest3_AP_2_dMRI_PA2.lta_diff', 'fMRI_rest4_PA_2_fMRI_rest3_AP.lta_diff', 'tfMRI_gambling1_AP_2_fMRI_rest4_PA.lta_diff', 'tfMRI_gambling2_PA_2_tfMRI_gambling1_AP.lta_diff', 'tfMRI_faceMatching1_AP_2_tfMRI_gambling2_PA.lta_diff', 'tfMRI_faceMatching2_PA_2_tfMRI_faceMatching1_AP.lta_diff', 'tfMRI_conflict1_AP_2_tfMRI_faceMatching2_PA.lta_diff', 'tfMRI_conflict2_PA_2_tfMRI_conflict1_AP.lta_diff', 'tfMRI_conflict3_AP_2_tfMRI_conflict2_PA.lta_diff', 'tfMRI_conflict4_PA_2_tfMRI_conflict3_AP.lta_diff', 'T2_2_tfMRI_conflict4_PA.lta_diff' ] labels = [ 'T1', 'dMRI1', 'dMRI2', 'fMRI1', 'fMR2', 'dMRI3', 'dMRI4', 'fMRI3', 'fMRI4', 'gambling1', 'gambling2', 'faceMatching1', 'faceMatchin2', 'conflict1', 'conflict2', 'conflict3', 'conflict4', 'T2' ] for s in subjects: angles = [] trans = [] for f in files: lta_diff = open("/space/erebus/1/users/data/preprocess/" + s + "/motion/" + f) for line in lta_diff: if "RotAngle" in line: angles.append((float(line.split()[2]) * 180 / 3.15) % 180) if "AbsTrans" in line: trans.append(line.split()[2]) axarr[0].plot(angles, label=s) axarr[0].set_xlabel('scans') axarr[0].set_ylabel('angle') #axarr[0].get_xticklabels().set_rotation(90) #plt.setp(axarr, xticks=range(len(labels)), xticklabels=labels) axarr[1].plot(trans, label=s) axarr[1].set_xlabel('scans') axarr[1].set_ylabel('translation (mm)') labels2 = [] for i in range(1, len(labels)): labels2.append(labels[i] + "-" + labels[i - 1]) matplotlib.pyplot.sca(axarr[1]) plt.xticks(rotation=90) #axarr[1].get_xticklabels().set_rotation(90) axarr[1].legend(ncol=3) #plt.setp(axarr, xticks=range(len(labels)), xticklabels=labels) plt.setp(axarr, xticks=range(len(labels)), xticklabels=labels2) matplotlib.pyplot.sca(axarr[0]) plt.xticks(rotation=90) # plt.show() fg.savefig( "/autofs/space/erebus_001/users/data/scores/new2/plots/motion_LTA_movement_patvscontrol", dpi=199)
def plotBetweenScanMotion(): #output=csv.writer(open('/space/erebus/1/users/data/scores/motion_between_scan_output.csv','w+')) #output.writerow(['subject','rot_trans','scan_type','score']) output = csv.writer( open( '/space/erebus/1/users/data/scores/_motion_between_scan_FD_output_140.csv', 'w+')) output.writerow(['subject', 'rot_trans', 'scan_type', 'score']) scans.append("T1 all vnavs - no reacq") scans.append("T2 all vnavs - no reacq") # outliers=["BANDA141","BANDA142","BANDA143"] subjects, subjects_lbl = utils.getBandaLabels(outliers) for m in metric: fg, axarr = plt.subplots(3, 4) fg.subplots_adjust(left=.03, bottom=.06, right=.97, top=.95, wspace=.18, hspace=.28) num = 0 #for name1, name2 in pairsOfStudies.items(): for a in range(len(pairsOfStudies)): name1, name2 = pairsOfStudies[a] print(name1, name2) i = num % 3 j = int(num / 3) axarr[i, j].set_title(name1 + "_" + name2) name = "%s_%s" % (name1, name2) y_values = [] for s_index, s in enumerate(subjects_lbl['allsubjects']): column, acq_time = getFileData(studies[name1]) if "Diffusion" in name1: f1 = getFilePath(studies[name1], s) f2 = getFilePath(studies[name2], s) ind1 = indexPerDiffusion[name1] ind2 = indexPerDiffusion[name2] acq_time *= 99 elif "T1" in name1: f1 = getFilePath("/motion/structural_motion.nii.gz.par", s) f2 = getFilePath("/motion/structural_motion.nii.gz.par", s) ind1 = 0, 1 ind2 = 1, 2 else: f1 = getFilePath("/motion/fmriFirsts_motion.nii.gz.par", s) f2 = getFilePath("/motion/fmriFirsts_motion.nii.gz.par", s) ind1 = fmri_order[name1], fmri_order[name1] + 1 ind2 = fmri_order[name2], fmri_order[name2] + 1 if f1 != None and f2 != None: t = m(f1, column, ind1, acq_time, 0, f2, ind2) #/acq_time y_values.append(t) #axarr[i,j].plot(subjects.index(s)+1, t, "o",c='C'+str(s_index%10)) output.writerow([s, m, name, t]) #color by subject type if s in subjects_lbl['control']: axarr[i, j].plot(subjects_lbl['allsubjects'].index(s) + 1, t, "o", c='r') elif s in subjects_lbl['anx']: axarr[i, j].plot(subjects_lbl['allsubjects'].index(s) + 1, t, "o", c='c') elif s in subjects_lbl['dep']: axarr[i, j].plot(subjects_lbl['allsubjects'].index(s) + 1, t, "o", c='y') if j == 0: axarr[i, j].set_ylabel(metric_label[metric.index(m)]) if i == 2: axarr[i, j].set_xlabel('Subject') #axarr[i,j].axvline(14.5, color='k', linestyle='--') axarr[i, j].set_xticks(range(1, len(subjects) + 1), 20) #axarr[i,j].set_xticklabels(np.arange(1,len(subjects)+1,20)) num += 1 #if metric.index(m)==1 : if max(y_values) < 10: axarr[i, j].set_ylim(-.1, max(y_values) + 1) control_patch = mpatches.Patch(color='r', label='control') anxious_patch = mpatches.Patch(color='c', label='anxious') depressed_patch = mpatches.Patch(color='y', label='depressed') axarr[i, j].legend( handles=[control_patch, anxious_patch, depressed_patch]) plt.show()
def plotWithinScanMotion(): output = csv.writer( open( '/space/erebus/1/users/data/scores/motion_within_scan_FD_output_140.csv', 'w+')) output.writerow(['subject', 'rot_trans', 'scan_type', 'score']) scans.append("T1 all vnavs - no reacq") scans.append("T2 all vnavs - no reacq") # outliers=["BANDA141","BANDA142","BANDA143"] subjects, subjects_lbl = utils.getBandaLabels(outliers) for m in metric: fg, axarr = plt.subplots(4, 5) fg.subplots_adjust(left=.03, bottom=.06, right=.97, top=.95, wspace=.18, hspace=.28) num = 0 for name in scans: fileN = studies[name.split()[0]] i = num % 4 j = int(num / 4) column, acq_time = getFileData(fileN) if "no vnavs" in name: if "T1" in name: index = [0, 166] else: index = [0, 111] else: index = [0, 1000] if "dMRI" in fileN: index = diffusion[i] axarr[i, j].set_title(name) #for s in subjects: for s_index, s in enumerate(subjects_lbl['allsubjects']): f = getFilePath(fileN, s) if f != None: print(name) if "T1" == name: ind = readVNavsScoreFiles(s, "T1") #print(np.sort(ind) ) elif "T2" == name: ind = readVNavsScoreFiles(s, "T2") #print(np.sort(ind)) else: #print("hola") ind = range(index[0], index[1]) t = m(f, column, np.sort(ind), acq_time) #axarr[i,j].plot(subjects.index(s)+1, t, "o",c='C'+str(s_index%10)) output.writerow([s, m, name, t]) #color by subject type if s in subjects_lbl['control']: axarr[i, j].plot(subjects_lbl['allsubjects'].index(s) + 1, t, "o", c='r') elif s in subjects_lbl['anx']: axarr[i, j].plot(subjects_lbl['allsubjects'].index(s) + 1, t, "o", c='c') elif s in subjects_lbl['dep']: axarr[i, j].plot(subjects_lbl['allsubjects'].index(s) + 1, t, "o", c='y') if j == 0: axarr[i, j].set_ylabel(metric_label[metric.index(m)]) if i == 3: axarr[i, j].set_xlabel('Subject') if metric.index(m) > 1: axarr[i, j].set_ylim((-.1, 7)) else: axarr[i, j].set_ylim((-.1, 1)) axarr[i, j].axvline(14.5, color='k', linestyle='--') axarr[i, j].set_xticks(range(1, len(subjects) + 1), 20) num += 1 control_patch = mpatches.Patch(color='r', label='control') anxious_patch = mpatches.Patch(color='c', label='anxious') depressed_patch = mpatches.Patch(color='y', label='depressed') axarr[i, j].legend( handles=[control_patch, anxious_patch, depressed_patch]) #fg.savefig("/autofs/space/erebus_001/users/data/scores/new2/plots/motion_within_scan_patvscontrol"+m,dpi=199) plt.show()