def icc_plot(hemi='lh'): import numpy as np import pickle as pkl from matplotlib import pyplot as plt from nibrain.util.plotfig import auto_bar_width icc_file = pjoin(work_dir, f'ICC_{hemi}.pkl') data = pkl.load(open(icc_file, 'rb')) x = np.arange(len(data['roi_name'])) width = auto_bar_width(x, 2) y_mpm = data['mpm'][1] low_err_mpm = y_mpm - data['mpm'][0] high_err_mpm = data['mpm'][2] - y_mpm yerr_mpm = np.array([low_err_mpm, high_err_mpm]) y_ind = data['individual'][1] low_err_ind = y_ind - data['individual'][0] high_err_ind = data['individual'][2] - y_ind yerr_ind = np.array([low_err_ind, high_err_ind]) plt.bar(x - (width / 2), y_mpm, width, yerr=yerr_mpm, label='group') plt.bar(x + (width / 2), y_ind, width, yerr=yerr_ind, label='individual') plt.xticks(x, data['roi_name']) plt.ylabel('ICC') plt.title(hemi) plt.legend() plt.tight_layout() plt.show()
def plot_SEM_h2_TMA(): """ thickness, myelin, and activation """ import numpy as np import pandas as pd from matplotlib import pyplot as plt from nibrain.util.plotfig import auto_bar_width df_file = pjoin(work_dir, 'AE-h2estimate_TMA.csv') hemis = ('lh', 'rh') rois = ('pFus', 'mFus', 'pFus_mFus') roi2color = { 'pFus': 'limegreen', 'mFus': 'cornflowerblue', 'pFus_mFus': 'black' } meas2title = { 'thickness': 'thickness', 'myelin': 'myelin', 'activ': 'face-avg' } n_hemi = len(hemis) n_roi = len(rois) n_meas = len(meas2title) df = pd.read_csv(df_file) x = np.arange(n_hemi) width = auto_bar_width(x, n_roi) _, axes = plt.subplots(1, n_meas) for meas_idx, meas_name in enumerate(meas2title.keys()): ax = axes[meas_idx] offset = -(n_roi - 1) / 2 for roi in rois: cols = [f'{roi}_{meas_name}_{hemi}' for hemi in hemis] data = np.array(df[cols]) y = data[1] low_err = y - data[0] high_err = data[2] - y yerr = np.array([low_err, high_err]) ax.bar(x + width * offset, y, width, yerr=yerr, label=roi, ec=roi2color[roi], fc='w', hatch='//') offset += 1 ax.set_title(meas2title[meas_name]) ax.set_xticks(x) ax.set_xticklabels(hemis) if meas_idx == 0: ax.set_ylabel('heritability') if meas_idx == 1: ax.legend() plt.tight_layout() plt.show()
def plot_bar(): """ 把整组(-1)的和分组(1, 2)的都画到一起 """ import numpy as np import pickle as pkl from scipy.stats import sem from nibrain.util.plotfig import auto_bar_width # inputs gids = (-1, 1, 2) hemis = ('lh', 'rh') seeds = ('pFus-face', 'mFus-face') seed2color = {'pFus-face': 'limegreen', 'mFus-face': 'cornflowerblue'} file1 = pjoin(work_dir, 'rfMRI/rsfc_individual2Cole_{hemi}.pkl') file2 = pjoin(work_dir, 'grouping/rfMRI/' 'rsfc_individual2Cole_G{gid}_{hemi}.pkl') # outputs out_file = pjoin(out_dir, 'bar_G{}_mean-across-network.jpg') n_hemi = len(hemis) n_seed = len(seeds) x = np.arange(n_hemi) width = auto_bar_width(x, n_seed) for gid_idx, gid in enumerate(gids): plt.figure(gid, figsize=(1.4, 3)) ax = plt.gca() if gid == -1: hemi2meas = { 'lh': pkl.load(open(file1.format(hemi='lh'), 'rb')), 'rh': pkl.load(open(file1.format(hemi='rh'), 'rb'))} else: hemi2meas = { 'lh': pkl.load(open(file2.format(gid=gid, hemi='lh'), 'rb')), 'rh': pkl.load(open(file2.format(gid=gid, hemi='rh'), 'rb'))} offset = -(n_seed - 1) / 2 for seed in seeds: y = np.zeros(n_hemi) y_err = np.zeros(n_hemi) for hemi_idx, hemi in enumerate(hemis): meas = np.mean(hemi2meas[hemi][seed], 1) meas = meas[~np.isnan(meas)] y[hemi_idx] = np.mean(meas) y_err[hemi_idx] = sem(meas) ax.bar(x+width*offset, y, width, yerr=y_err, label=seed.split('-')[0], color=seed2color[seed]) offset += 1 ax.set_xticks(x) ax.set_xticklabels(hemis) ax.set_ylabel('RSFC') ax.set_ylim(0.1) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) # ax.legend() plt.tight_layout() plt.savefig(out_file.format(gid))
def plot_pattern_corr1(): import numpy as np import pandas as pd from scipy.stats import sem from matplotlib import pyplot as plt from nibrain.util.plotfig import auto_bar_width rois = ('pFus', 'mFus') meas_names = ('thickness', 'myelin', 'activ', 'rsfc') meas2ylabel = { 'thickness': 'thickness', 'myelin': 'myelin', 'activ': 'face-avg', 'rsfc': 'RSFC' } zygosity = ('MZ', 'DZ') zyg2color = {'MZ': (0.33, 0.33, 0.33, 1), 'DZ': (0.66, 0.66, 0.66, 1)} hemis = ('lh', 'rh') df_file = pjoin(work_dir, 'twins_pattern-corr_{}.csv') # outputs out_file = pjoin(work_dir, 'pattern_corr1.jpg') n_zyg = len(zygosity) x = np.arange(len(rois)) width = auto_bar_width(x, n_zyg) _, axes = plt.subplots(len(hemis), len(meas_names), figsize=(6.4, 4.8)) for meas_idx, meas_name in enumerate(meas_names): df = pd.read_csv(df_file.format(meas_name)) for hemi_idx, hemi in enumerate(hemis): ax = axes[hemi_idx, meas_idx] offset = -(n_zyg - 1) / 2 for zyg in zygosity: indices = df['zygosity'] == zyg cols = [f'{hemi}_{roi}' for roi in rois] data = np.array(df.loc[indices, cols]) y = np.mean(data, 0) yerr = sem(data, 0) ax.bar(x + width * offset, y, width, yerr=yerr, label=zyg, color=zyg2color[zyg]) offset += 1 ax.set_ylabel(meas2ylabel[meas_name]) # if meas_idx == 1: # ax.set_title(hemi) # if hemi_idx == 0: # ax.legend() ax.set_xticks(x) ax.set_xticklabels(rois) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) plt.tight_layout() plt.savefig(out_file)
def count_roi(): """ 可视化左右脑各组分半后的ROI数量 """ import numpy as np import pandas as pd from matplotlib import pyplot as plt from magicbox.algorithm.plot import show_bar_value from nibrain.util.plotfig import auto_bar_width hemis = ('lh', 'rh') n_hemi = len(hemis) gids = (0, 1, 2) n_gid = len(gids) rois = ('pFus-face', 'mFus-face') xticklabels = [roi.split('-')[0] for roi in rois] gh_id_file = pjoin(split_dir, 'half_id_{}.npy') roi_idx_vec_file = pjoin( proj_dir, 'analysis/s2/1080_fROI/refined_with_Kevin/' 'rois_v3_idx_vec.csv') df = pd.read_csv(roi_idx_vec_file) x = np.arange(len(rois)) width = auto_bar_width(x, 2) _, axes = plt.subplots(n_gid, n_hemi) for hemi_idx, hemi in enumerate(hemis): gh_ids = np.load(gh_id_file.format(hemi)) for gid_idx, gid in enumerate(gids): ax = axes[gid_idx, hemi_idx] gh1_id = gid * 10 + 1 gh2_id = gid * 10 + 2 y1 = [] y2 = [] for roi in rois: col = f'{hemi}_{roi}' y1.append(np.sum(df[col][gh_ids == gh1_id])) y2.append(np.sum(df[col][gh_ids == gh2_id])) rects1 = ax.bar(x - width / 2, y1, width, label='half1') rects2 = ax.bar(x + width / 2, y2, width, label='half2') show_bar_value(rects1, ax=ax) show_bar_value(rects2, ax=ax) # if hemi_idx == 0 and gid_idx == 0: # ax.legend() ax.set_xticks(x) ax.set_xticklabels(xticklabels) if hemi_idx == 0: ax.set_ylabel('#subject') if gid_idx == 0: ax.set_title(hemi) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) plt.tight_layout() plt.show()
def plot_bar(meas='thickness'): import numpy as np import pickle as pkl from scipy.stats import sem from nibrain.util.plotfig import auto_bar_width from matplotlib import pyplot as plt src_file = pjoin(work_dir, 'rois_v3_{}_{}.pkl') hemis = ('lh', 'rh') rois = ('pFus-face', 'mFus-face') roi2color = {'pFus-face': 'limegreen', 'mFus-face': 'cornflowerblue'} meas2ylabel = { 'thickness': 'thickness', 'myelin': 'myelination', 'activ': 'face selectivity', 'va': 'region size' } meas2ylim = {'thickness': 2.7, 'myelin': 1.3, 'activ': 2, 'va': 200} hemi2meas = { 'lh': pkl.load(open(src_file.format('lh', meas), 'rb')), 'rh': pkl.load(open(src_file.format('rh', meas), 'rb')) } n_roi = len(rois) n_hemi = len(hemis) x = np.arange(n_hemi) width = auto_bar_width(x, n_roi) offset = -(n_roi - 1) / 2 _, ax = plt.subplots() for roi in rois: y = np.zeros(n_hemi) y_err = np.zeros(n_hemi) for hemi_idx, hemi in enumerate(hemis): roi_idx = hemi2meas[hemi]['roi'].index(roi) meas_map = hemi2meas[hemi]['meas'][roi_idx] meas_map = meas_map[~np.isnan(meas_map)] print(f'{hemi}_{roi}:', len(meas_map)) y[hemi_idx] = np.mean(meas_map) y_err[hemi_idx] = sem(meas_map) ax.bar(x + width * offset, y, width, yerr=y_err, label=roi.split('-')[0], color=roi2color[roi]) offset += 1 ax.set_xticks(x) ax.set_xticklabels(hemis) ax.set_ylabel(meas2ylabel[meas]) ax.set_ylim(meas2ylim[meas]) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.legend() plt.tight_layout() plt.show()
def plot_probability_if_twins_belong_to_same_group(): """ Plot the probability whether a twin pair both belong to the same group """ import numpy as np import pandas as pd from matplotlib import pyplot as plt from nibrain.util.plotfig import auto_bar_width, plot_stacked_bar hemis = ('lh', 'rh') gids = (0, 1, 2) hatchs = [None, '//', '*'] # [None, '//', '*'] limit_name = f"limit_G{''.join(map(str, gids))}" zygosity = ('MZ', 'DZ') zyg2color = {'MZ': (0.33, 0.33, 0.33, 1), 'DZ': (0.66, 0.66, 0.66, 1)} count_file = pjoin(work_dir, 'count_if_same_group.csv') n_hemi = len(hemis) n_gid = len(gids) n_zyg = len(zygosity) df = pd.read_csv(count_file, index_col=0) x = np.arange(n_hemi) width = auto_bar_width(x, n_zyg) offset = -(n_zyg - 1) / 2 _, ax = plt.subplots(figsize=(12.8, 7)) for zyg in zygosity: ys = np.zeros((n_gid, n_hemi)) items = [f'{zyg}_lh', f'{zyg}_rh'] labels = [] for gid_idx, gid in enumerate(gids): ys[gid_idx] = df.loc[f'G{gid}', items] labels.append(f'G{gid}_{zyg}') ys = ys / np.array([df.loc[limit_name, items]]) plot_stacked_bar(x + width * offset, ys, width, label=labels, ec=zyg2color[zyg], fc='w', hatch=hatchs, ax=ax) offset += 1 print(f'{zyg}_{hemis}:\n', ys) print(f'{zyg}_{hemis}:\n', np.sum(ys, 0)) ax.set_xticks(x) ax.set_xticklabels(hemis) ax.set_ylabel('the ratio of twins') ax.legend() plt.tight_layout() plt.show()
def plot_bar(): import numpy as np import pickle as pkl from scipy.stats import sem from matplotlib import pyplot as plt from nibrain.util.plotfig import auto_bar_width # inputs hemis = ('lh', 'rh') seeds = ('pFus-face', 'mFus-face') seed2color = {'pFus-face': 'limegreen', 'mFus-face': 'cornflowerblue'} rsfc_file = pjoin(work_dir, 'rsfc_mpm2Cole_{hemi}.pkl') n_hemi = len(hemis) n_seed = len(seeds) x = np.arange(n_hemi) width = auto_bar_width(x, n_seed) plt.figure() ax = plt.gca() hemi2meas = { 'lh': pkl.load(open(rsfc_file.format(hemi='lh'), 'rb')), 'rh': pkl.load(open(rsfc_file.format(hemi='rh'), 'rb')) } offset = -(n_seed - 1) / 2 for seed in seeds: y = np.zeros(n_hemi) y_err = np.zeros(n_hemi) for hemi_idx, hemi in enumerate(hemis): meas = np.mean(hemi2meas[hemi][seed], 1) meas = meas[~np.isnan(meas)] y[hemi_idx] = np.mean(meas) y_err[hemi_idx] = sem(meas) ax.bar(x + width * offset, y, width, yerr=y_err, label=seed.split('-')[0], color=seed2color[seed]) offset += 1 ax.set_xticks(x) ax.set_xticklabels(hemis) ax.set_ylabel('RSFC') ax.set_ylim(0.1) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.legend() plt.tight_layout() plt.show()
def plot_gdist(): import numpy as np import pandas as pd from scipy.stats import sem from matplotlib import pyplot as plt from nibrain.util.plotfig import auto_bar_width hemis = ('lh', 'rh') items = ('pFus-mFus', ) data_file = pjoin(work_dir, 'gdist_peak.csv') n_hemi = len(hemis) n_item = len(items) df = pd.read_csv(data_file) _, ax = plt.subplots() x = np.arange(n_hemi) width = auto_bar_width(x, n_item) offset = -(n_item - 1) / 2 for item in items: ys = np.zeros(n_hemi) yerrs = np.zeros(n_hemi) for hemi_idx, hemi in enumerate(hemis): col = hemi + '_' + item data = np.array(df[col]) data = data[~np.isnan(data)] print(f'#{col}: {len(data)}') ys[hemi_idx] = np.mean(data) yerrs[hemi_idx] = sem(data) ax.bar(x + width * offset, ys, width, yerr=yerrs, label=item) offset += 1 ax.legend() ax.set_xticks(x) ax.set_xticklabels(hemis) ax.set_ylabel('geodesic distance (mm)') ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) plt.tight_layout() plt.show()
def plot_retest_reliability_corr(): import numpy as np import pickle as pkl from scipy.stats.stats import sem from nibrain.util.plotfig import auto_bar_width # inputs figsize = (6.4, 4.8) hemis = ('lh', 'rh') rois = ('pFus', 'mFus') retest_dir = pjoin(proj_dir, 'analysis/s2/1080_fROI/' 'refined_with_Kevin/retest') atlas_names = ('MPM', 'ROIv3') meas2file = { 'thickness': pjoin(retest_dir, 'reliability/thickness_{atlas}_corr_rm-subj.pkl'), 'myelin': pjoin(retest_dir, 'reliability/myelin_{atlas}_corr_rm-subj.pkl'), 'activ': pjoin(retest_dir, 'reliability/activ_{atlas}_corr_rm-subj.pkl'), 'rsfc': pjoin(retest_dir, 'rfMRI/{atlas}_rm-subj_corr.pkl'), } meas2title = { 'thickness': 'thickness', 'myelin': 'myelination', 'activ': 'face selectivity', 'rsfc': 'RSFC' } atlas2color = { 'MPM': (0.33, 0.33, 0.33, 1), 'ROIv3': (0.66, 0.66, 0.66, 1) } # outputs out_file = pjoin(work_dir, 'retest_reliabilty_corr.jpg') # prepare n_hemi = len(hemis) n_roi = len(rois) n_atlas = len(atlas_names) n_meas = len(meas2file) x = np.arange(n_roi) # plot _, axes = plt.subplots(n_meas, n_hemi, figsize=figsize) offset = -(n_atlas - 1) / 2 width = auto_bar_width(x, n_atlas) for atlas_idx, atlas_name in enumerate(atlas_names): for meas_idx, meas_name in enumerate(meas2file.keys()): fpath = meas2file[meas_name].format(atlas=atlas_name) data = pkl.load(open(fpath, 'rb')) for hemi_idx, hemi in enumerate(hemis): ax = axes[meas_idx, hemi_idx] ys = np.zeros(n_roi) yerrs = np.zeros(n_roi) for roi_idx, roi in enumerate(rois): k = f'{hemi}_{roi}' ys[roi_idx] = np.mean(data[k]) yerrs[roi_idx] = sem(data[k]) ax.bar(x + width * offset, ys, width, yerr=yerrs, label=atlas_name, color=atlas2color[atlas_name]) if atlas_idx == 1: ax.set_title(meas2title[meas_name]) # ax.legend() ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_xticks(x) ax.set_xticklabels(rois) if hemi_idx == 0: ax.set_ylabel('pearson R') offset += 1 plt.tight_layout() plt.savefig(out_file)
def plot_bar(): import numpy as np import pandas as pd from scipy.stats.stats import sem from matplotlib import pyplot as plt from nibrain.util.plotfig import auto_bar_width # inputs gids = (1, 2) hemis = ('lh', 'rh') rois = ('pFus', 'mFus') sessions = ('test', 'retest') subj_file_45 = pjoin(proj_dir, 'data/HCP/wm/analysis_s2/' 'retest/subject_id') subj_file_1080 = pjoin(proj_dir, 'analysis/s2/subject_id') ses2gid_file = { 'test': pjoin( proj_dir, 'analysis/s2/1080_fROI/refined_with_Kevin/' 'grouping/group_id_{}.npy'), 'retest': pjoin( proj_dir, 'analysis/s2/1080_fROI/refined_with_Kevin/' 'retest/grouping/group_id_{}.npy') } data_file = pjoin(work_dir, 'activ-{ses1}_ROI-{ses2}.csv') gid2name = {1: 'two-C', 2: 'two-S'} # prepare n_gid = len(gids) n_ses = len(sessions) subj_ids_45 = open(subj_file_45).read().splitlines() subj_ids_1080 = open(subj_file_1080).read().splitlines() retest_idx_in_1080 = [subj_ids_1080.index(i) for i in subj_ids_45] ses_hemi2gid_vec = {} for ses in sessions: for hemi in hemis: gid_file = ses2gid_file[ses].format(hemi) gid_vec = np.load(gid_file) if ses == 'test': gid_vec = gid_vec[retest_idx_in_1080] ses_hemi2gid_vec[f'{ses}_{hemi}'] = gid_vec # plot x = np.arange(len(hemis) * len(rois)) width = auto_bar_width(x, n_gid) _, axes = plt.subplots(n_ses, n_ses * n_ses) col_idx = -1 for ses1 in sessions: for ses2 in sessions: col_idx += 1 fpath = data_file.format(ses1=ses1, ses2=ses2) data = pd.read_csv(fpath) for ses3_idx, ses3 in enumerate(sessions): ax = axes[ses3_idx, col_idx] offset = -(n_gid - 1) / 2 for gid in gids: y = [] yerr = [] xticklabels = [] for hemi in hemis: gid_vec = ses_hemi2gid_vec[f'{ses3}_{hemi}'] gid_idx_vec = gid_vec == gid print(f'#{ses3}_{hemi}_{gid}:', np.sum(gid_idx_vec)) for roi in rois: column = f'{hemi}_{roi}' meas_vec = np.array(data[column])[gid_idx_vec] print(np.sum(np.isnan(meas_vec))) meas_vec = meas_vec[~np.isnan(meas_vec)] y.append(np.mean(meas_vec)) yerr.append(sem(meas_vec)) xticklabels.append(column) ax.bar(x + width * offset, y, width, yerr=yerr, label=gid2name[gid]) offset += 1 ax.set_xticks(x) ax.set_xticklabels(xticklabels) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) if col_idx == 0: ax.set_ylabel('face selectivity') if ses3_idx == 0: ax.legend() if ses3_idx == 0: fname = os.path.basename(fpath) ax.set_title(fname.rstrip('.csv')) plt.tight_layout() plt.show()
def plot_SEM_h2_RSFC(): import numpy as np import pandas as pd from matplotlib import pyplot as plt from nibrain.util.plotfig import auto_bar_width df_file = pjoin(work_dir, 'AE-h2estimate_rsfc.csv') hemis = ('lh', 'rh') rois = ('pFus', 'mFus', 'pFus_mFus') roi2color = { 'pFus': 'limegreen', 'mFus': 'cornflowerblue', 'pFus_mFus': 'black' } trg_config_file = '/nfs/p1/atlases/ColeAnticevicNetPartition/'\ 'network_labelfile.txt' n_hemi = len(hemis) n_roi = len(rois) df = pd.read_csv(df_file) rf = open(trg_config_file) trg_names = [] trg_labels = [] while True: trg_name = rf.readline() if trg_name == '': break trg_names.append(trg_name.rstrip('\n')) trg_labels.append(int(rf.readline().split(' ')[0])) indices_sorted = np.argsort(trg_labels) trg_names = np.array(trg_names)[indices_sorted].tolist() trg_labels = np.array(trg_labels)[indices_sorted].tolist() n_trg = len(trg_names) print(trg_names) x = np.arange(n_hemi * n_trg) width = auto_bar_width(x, n_roi) / 1.5 offset = -(n_roi - 1) / 2 for roi in rois: cols = [ f'{roi}_trg{trg_lbl}_{hemi}' for trg_lbl in trg_labels for hemi in hemis ] data = np.array(df[cols]) y = data[1] low_err = y - data[0] high_err = data[2] - y yerr = np.array([low_err, high_err]) plt.bar(x + width * offset, y, width, yerr=yerr, label=roi, ec=roi2color[roi], fc='w', hatch='//') offset += 1 plt.xticks(x, hemis * n_trg) plt.ylabel('heritability') plt.legend() plt.tight_layout() plt.show()
def plot_pattern_corr2(): import numpy as np import pandas as pd from scipy.stats import ttest_ind from matplotlib import pyplot as plt from nibrain.util.plotfig import auto_bar_width from commontool.stats import EffectSize rois = ('pFus', 'mFus') roi2color = {'pFus': 'limegreen', 'mFus': 'cornflowerblue'} meas_names = ('thickness', 'myelin', 'activ') meas2ylabel = { 'thickness': 'thickness', 'myelin': 'myelin', 'activ': 'face-avg' } hemis = ('lh', 'rh') n_roi = len(rois) df_file = pjoin(work_dir, 'twins_pattern-corr_{}.csv') x = np.arange(len(hemis)) width = auto_bar_width(x, n_roi) _, axes = plt.subplots(2, len(meas_names)) es = EffectSize() for meas_idx, meas_name in enumerate(meas_names): print(f'---{meas2ylabel[meas_name]}---') ax1 = axes[0, meas_idx] ax2 = axes[1, meas_idx] df = pd.read_csv(df_file.format(meas_name)) offset = -(n_roi - 1) / 2 for roi in rois: ts = [] ps = [] ds = [] for hemi in hemis: col = f'{hemi}_{roi}' s1 = np.array(df.loc[df['zygosity'] == 'MZ', col]) s2 = np.array(df.loc[df['zygosity'] == 'DZ', col]) t, p = ttest_ind(s1, s2) d = es.cohen_d(s1, s2) ts.append(t) ps.append(p) ds.append(d) print(f'P value of {col}:', p) ax1.bar(x + width * offset, ts, width, label=roi, color=roi2color[roi]) ax2.bar(x + width * offset, ds, width, label=roi, color=roi2color[roi]) offset += 1 ax1.set_ylabel(meas2ylabel[meas_name]) ax1.set_xticks(x) ax1.set_xticklabels(hemis) ax1.spines['top'].set_visible(False) ax1.spines['right'].set_visible(False) ax2.set_ylabel(meas2ylabel[meas_name]) ax2.set_xticks(x) ax2.set_xticklabels(hemis) ax2.spines['top'].set_visible(False) ax2.spines['right'].set_visible(False) if meas_idx == 1: # ax1.legend() ax1.set_title('t-value') ax2.set_title("Cohen's D") plt.tight_layout() plt.show()
def plot_Falconer_h2(): """ thickness, myelin, face-avg, mean RSFC """ import numpy as np import pandas as pd from matplotlib import pyplot as plt from nibrain.util.plotfig import auto_bar_width # figsize = (9, 4) figsize = (7, 4.8) out_file = pjoin(work_dir, 'Falconer_h2.jpg') meas2file = { 'thickness': pjoin(work_dir, 'heritability_icc_TMA.csv'), 'myelin': pjoin(work_dir, 'heritability_icc_TMA.csv'), 'activ': pjoin(work_dir, 'heritability_icc_TMA.csv'), 'rsfc': pjoin(work_dir, 'heritability_icc_rsfc-mean.csv') } zygosity = ('mz', 'dz') hemis = ('lh', 'rh') # rois = ('pFus', 'mFus', 'pFus_mFus') rois = ('pFus', 'mFus') roi2color = { 'pFus': 'limegreen', 'mFus': 'cornflowerblue', 'pFus_mFus': 'black' } roi2label = {'pFus': 'pFus', 'mFus': 'mFus', 'pFus_mFus': 'pFus-mFus'} meas2title = { 'thickness': 'thickness', 'myelin': 'myelination', 'activ': 'face selectivity', 'va': 'surface area', 'rsfc': 'RSFC' } meas2str = { 'thickness': '_thickness_', 'myelin': '_myelin_', 'activ': '_activ_', 'va': '_va_', 'rsfc': '_' } n_roi = len(rois) n_hemi = len(hemis) n_meas = len(meas2file) x = np.arange(n_hemi) _, axes = plt.subplots(2, n_meas, figsize=figsize) # plot ICC n_item0 = 2 * n_roi width0 = auto_bar_width(x, n_item0) for meas_idx, meas_name in enumerate(meas2file.keys()): df = pd.read_csv(meas2file[meas_name], index_col=0) ax = axes[0, meas_idx] offset = -(n_item0 - 1) / 2 for roi in rois: cols = [f'{roi}{meas2str[meas_name]}{hemi}' for hemi in hemis] for zyg in zygosity: lbl = roi2label[roi] + '_' + zyg y = np.array(df.loc[f'r_{zyg}', cols]) low_err = y - np.array(df.loc[f'r_{zyg}_lb', cols]) high_err = np.array(df.loc[f'r_{zyg}_ub', cols]) - y yerr = np.array([low_err, high_err]) if zyg == 'mz': ax.bar(x + width0 * offset, y, width0, yerr=yerr, label=lbl, ec=roi2color[roi], fc='w', hatch='//') # ax.plot(x + width0 * offset, # np.array(df.loc['ICC_MZ', cols]), # linestyle='', marker='*', ms=10, # markeredgecolor='k', markerfacecolor='w') else: ax.bar(x + width0 * offset, y, width0, yerr=yerr, label=lbl, ec=roi2color[roi], fc='w', hatch='\\') # ax.plot(x + width0 * offset, # np.array(df.loc['ICC_DZ', cols]), # linestyle='', marker='*', ms=10, # markeredgecolor='k', markerfacecolor='w') offset += 1 ax.set_title(meas2title[meas_name]) ax.set_xticks(x) ax.set_xticklabels(hemis) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) if meas_idx == 0: ax.set_ylabel('ICC') if meas_idx == 1: ax.legend() # plot heritability n_item1 = n_roi width1 = auto_bar_width(x, n_item1) for meas_idx, meas_name in enumerate(meas2file.keys()): df = pd.read_csv(meas2file[meas_name], index_col=0) ax = axes[1, meas_idx] offset = -(n_item1 - 1) / 2 for roi in rois: cols = [f'{roi}{meas2str[meas_name]}{hemi}' for hemi in hemis] y = np.array(df.loc['h2', cols]) low_err = y - np.array(df.loc['h2_lb', cols]) high_err = np.array(df.loc['h2_ub', cols]) - y yerr = np.array([low_err, high_err]) # ax.bar(x + width1 * offset, y, width1, yerr=yerr, # label=roi2label[roi], ec=roi2color[roi], fc='w', # hatch='//') ax.bar(x + width1 * offset, y, width1, yerr=yerr, label=roi2label[roi], color=roi2color[roi]) # ax.plot(x + width1 * offset, # np.array(df.loc['H2', cols]), # linestyle='', marker='*', ms=10, # markeredgecolor='k', markerfacecolor='w') offset += 1 ax.set_xticks(x) ax.set_xticklabels(hemis) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) if meas_idx == 0: ax.set_ylabel('heritability') # if meas_idx == 1: # ax.legend() plt.tight_layout() plt.savefig(out_file)
def plot_twinsID_distribution_G0G1G2(): import numpy as np import pandas as pd from matplotlib import pyplot as plt from nibrain.util.plotfig import auto_bar_width, plot_stacked_bar hemis = ('lh', 'rh') hemi2color = {'lh': (0.33, 0.33, 0.33, 1), 'rh': (0.66, 0.66, 0.66, 1)} gids = (0, 1, 2) zygosity = ('MZ', 'DZ') twins_gid_file = pjoin(work_dir, 'twins_gid_1080.csv') gid_file = pjoin( proj_dir, 'analysis/s2/1080_fROI/refined_with_Kevin/' 'grouping/group_id_{hemi}.npy') n_hemi = len(hemis) n_gid = len(gids) n_zyg = len(zygosity) df = pd.read_csv(twins_gid_file) zyg2indices = {} for zyg in zygosity: zyg2indices[zyg] = df['zygosity'] == zyg x = np.arange(n_zyg) width = auto_bar_width(x, n_hemi) offset = -(n_hemi - 1) / 2 _, axes = plt.subplots(1, 2, figsize=(12.8, 7)) for hemi in hemis: cols = [f'twin1_gid_{hemi}', f'twin2_gid_{hemi}'] ys = np.zeros((n_gid, n_zyg)) gid_vec = np.load(gid_file.format(hemi=hemi)) n_subjs = np.zeros((n_gid, 1)) for zyg_idx, zyg in enumerate(zygosity): data = np.array(df.loc[zyg2indices[zyg], cols]) for gid_idx, gid in enumerate(gids): ys[gid_idx, zyg_idx] = np.sum(data == gid) if zyg_idx == 0: n_subjs[gid_idx, 0] = np.sum(gid_vec == gid) x_tmp = x + width * offset offset += 1 labels = [f'G0_{hemi}', f'G1_{hemi}', f'G2_{hemi}'] face_colors = ['w', 'w', hemi2color[hemi]] hatchs = ['//', '*', None] ax = axes[0] plot_stacked_bar(x_tmp, ys, width, label=labels, ec=hemi2color[hemi], fc=face_colors, hatch=hatchs, ax=ax) ax = axes[1] plot_stacked_bar(x_tmp, ys / n_subjs, width, label=labels, ec=hemi2color[hemi], fc=face_colors, hatch=hatchs, ax=ax) axes[0].set_xticks(x) axes[0].set_xticklabels(zygosity) axes[0].set_ylabel('the number of subjects') axes[1].set_xticks(x) axes[1].set_xticklabels(zygosity) axes[1].set_ylabel('the ratio of subjects') axes[1].legend() plt.tight_layout() plt.show()
def plot(): """ 把整组(-1)的和分组(1, 2)的都画到一起 """ import numpy as np import pickle as pkl from scipy.stats import sem from nibrain.util.plotfig import auto_bar_width from cxy_hcp_ffa.lib.predefine import roi2color gids = [-1, 1, 2] files1 = [ pjoin(work_dir, 'structure/rois_v3_{hemi}_thickness.pkl'), pjoin(work_dir, 'structure/rois_v3_{hemi}_myelin.pkl'), pjoin(work_dir, 'split/tfMRI/activ_{hemi}.pkl'), pjoin(work_dir, 'structure/rois_v3_{hemi}_va.pkl') ] files2 = [ pjoin(work_dir, 'grouping/structure/individual_G{gid}_thickness_{hemi}.pkl'), pjoin(work_dir, 'grouping/structure/individual_G{gid}_myelin_{hemi}.pkl'), pjoin(work_dir, 'grouping/split/tfMRI/G{gid}_activ_{hemi}.pkl'), pjoin(work_dir, 'grouping/structure/individual_G{gid}_va_{hemi}.pkl') ] trg_file = pjoin(trg_dir, 'TMAV_diff.svg') ylims = [2.7, 1.3, 2, 200] ylabels = ['thickness', 'myelination', 'face selectivity', 'surface area'] hemis = ['lh', 'rh'] n_hemi = len(hemis) rois = ['pFus-face', 'mFus-face'] n_roi = len(rois) x = np.arange(n_hemi) _, axes = plt.subplots(len(gids), len(ylabels)) for gid_idx, gid in enumerate(gids): files = files1 if gid == -1 else files2 for f_idx, file in enumerate(files): ax = axes[gid_idx, f_idx] if gid == -1: hemi2meas = { 'lh': pkl.load(open(file.format(hemi='lh'), 'rb')), 'rh': pkl.load(open(file.format(hemi='rh'), 'rb')) } else: hemi2meas = { 'lh': pkl.load(open(file.format(gid=gid, hemi='lh'), 'rb')), 'rh': pkl.load(open(file.format(gid=gid, hemi='rh'), 'rb')) } width = auto_bar_width(x, n_roi) offset = -(n_roi - 1) / 2 for roi in rois: y = np.zeros(n_hemi) y_err = np.zeros(n_hemi) for hemi_idx, hemi in enumerate(hemis): roi_idx = hemi2meas[hemi]['roi'].index(roi) meas = hemi2meas[hemi]['meas'][roi_idx] meas = meas[~np.isnan(meas)] y[hemi_idx] = np.mean(meas) y_err[hemi_idx] = sem(meas) ax.bar(x + width * offset, y, width, yerr=y_err, label=roi.split('-')[0], color=roi2color[roi]) offset += 1 ax.set_xticks(x) ax.set_xticklabels(hemis) ax.set_ylabel(ylabels[f_idx]) ax.set_ylim(ylims[f_idx]) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) # if 'myelin' in file and gid_idx == 0: # ax.legend() plt.tight_layout() plt.savefig(trg_file)