def explore_statistics(n_clusters_dir, items, ylabels, colors, val_fmt=''): stats_path = pjoin(n_clusters_dir, 'statistics.csv') with open(stats_path) as f: stats = f.read().splitlines() stats_items = stats[0].split(',') stats_content = [line.split(',') for line in stats[1:]] stats_content = list(zip(*stats_content)) stats_dict = {} for idx, item in enumerate(stats_items): stats_dict[item] = stats_content[idx] x = np.arange(len(stats) - 1) width = auto_bar_width(x) for idx, item in enumerate(items): plt.figure() y = [float(_) for _ in stats_dict[item]] rects = plt.bar(x, y, width, color=colors[idx]) show_bar_value(rects, val_fmt) plt.xlabel('subgroup label') plt.ylabel(ylabels[idx]) plt.title(item) plt.xticks(x, stats_dict['label']) ax = plt.gca() ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) plt.savefig(pjoin(n_clusters_dir, '{}.png'.format(item)))
def gender_diff_roi_mean_plot(roi_mean_file, items_m, items_f, xticklabels, ylabel=None, title=None): assert len(items_m) == len(items_f) roi_mean_dict = CsvReader(roi_mean_file).to_dict(axis=1) roi_means_list_m = [list(map(float, roi_mean_dict[item])) for item in items_m] roi_means_list_f = [list(map(float, roi_mean_dict[item])) for item in items_f] item_num = len(items_m) for i in range(item_num): print('{} vs. {}'.format(items_m[i], items_f[i]), ttest_ind(roi_means_list_m[i], roi_means_list_f[i])) fig, ax = plt.subplots() x = np.arange(item_num) width = auto_bar_width(x, 2) y_m = [np.mean(roi_means) for roi_means in roi_means_list_m] y_f = [np.mean(roi_means) for roi_means in roi_means_list_f] sems_m = [sem(roi_means) for roi_means in roi_means_list_m] sems_f = [sem(roi_means) for roi_means in roi_means_list_f] rects1 = ax.bar(x, y_m, width, color='b', alpha=0.5, yerr=sems_m, ecolor='blue') rects2 = ax.bar(x + width, y_f, width, color='r', alpha=0.5, yerr=sems_f, ecolor='red') # show_bar_value(rects1, '.3f') # show_bar_value(rects2, '.3f') ax.legend((rects1, rects2), ('male', 'female')) ax.set_xticks(x + width / 2.0) ax.set_xticklabels(xticklabels) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) if ylabel is not None: plt.ylabel(ylabel) if title is not None: plt.title(title) plt.tight_layout()
def test_bar_plot(): x = np.arange(3) y = [1, 2, 3] x_ticks = ['11', '22', '33'] width = auto_bar_width(x) plt.figure() rects = plt.bar(x, y, width, color='r') show_bar_value(rects) plt.xticks(x, x_ticks) x = np.arange(1) y = [1.3241] width = auto_bar_width(x) plt.figure() rects = plt.bar(x, y, width, color='g') show_bar_value(rects, '.3f')
def plot_rsfc(): import numpy as np import pandas as pd from matplotlib import pyplot as plt from commontool.algorithm.plot import auto_bar_width df = pd.read_csv('ACE-h2estimate_rsfc.csv') hemis = ('lh', 'rh') n_hemi = len(hemis) rois = ('pFus', 'mFus', 'pFus_mFus') roi2color = { 'pFus': 'limegreen', 'mFus': 'cornflowerblue', 'pFus_mFus': 'black' } n_roi = len(rois) trg_config_file = '/nfs/p1/atlases/ColeAnticevicNetPartition/network_labelfile.txt' 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]) plt.bar(x + width * offset, data[1], width, yerr=data[[0, 2]], label=roi, color=roi2color[roi]) offset += 1 plt.xticks(x, hemis * n_trg) plt.ylabel('heritability') plt.legend() plt.tight_layout() plt.show()
def plot_roi_info(): import numpy as np import pickle as pkl from matplotlib import pyplot as plt from commontool.algorithm.plot import show_bar_value, auto_bar_width roi_info_file = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_pattern/analysis/s2_rh/zscore/' \ 'HAC_ward_euclidean/100clusters/activation/ROIs/v3/rois_info.pkl' roi_infos = pkl.load(open(roi_info_file, 'rb')) # -plot n_group and n_subject- x_labels = list(roi_infos.keys()) n_roi = len(x_labels) x = np.arange(n_roi) width = auto_bar_width(x) # plot n_group y_n_group = [info['n_group'] for info in roi_infos.values()] rects_group = plt.bar(x, y_n_group, width, facecolor='white', edgecolor='black') show_bar_value(rects_group) plt.xticks(x, x_labels) plt.ylabel('#group') # plot n_subject plt.figure() y_n_subj = [info['n_subject'] for info in roi_infos.values()] rects_subj = plt.bar(x, y_n_subj, width, facecolor='white', edgecolor='black') show_bar_value(rects_subj) plt.xticks(x, x_labels) plt.ylabel('#subject') # -plot sizes- for roi, info in roi_infos.items(): plt.figure() sizes = info['sizes'] bins = np.linspace(min(sizes), max(sizes), 50) _, _, patches = plt.hist(sizes, bins, color='white', edgecolor='black') plt.xlabel('#vertex') plt.title(f'distribution of {roi} sizes') show_bar_value(patches, '.0f') plt.tight_layout() plt.show()
def compare_plot_bar(): import numpy as np import matplotlib.pyplot as plt from os.path import join as pjoin # https://www.statsmodels.org/dev/_modules/statsmodels/stats/multitest.html from statsmodels.stats.multitest import multipletests from commontool.io.io import CsvReader from commontool.algorithm.plot import auto_bar_width, show_bar_value stru_name = 'myelin' project_dir = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_clustering' stru_dir = pjoin(project_dir, 's2_25_zscore/HAC_ward_euclidean/2clusters/structure') compare_dir = pjoin(stru_dir, 'compare') compare_file = pjoin(compare_dir, stru_name) multi_test_corrected = True alpha = 0.001 compare_dict = CsvReader(compare_file).to_dict(1) ps = np.array(list(map(float, compare_dict['p']))) if multi_test_corrected: reject, ps, alpha_sidak, alpha_bonf = multipletests(ps, 0.05, 'fdr_bh') sample_names = [ name for idx, name in enumerate(compare_dict['sample_name']) if ps[idx] < alpha ] ps = [p for p in ps if p < alpha] print('\n'.join(list(map(str, zip(sample_names, ps))))) fig, ax = plt.subplots() x = np.arange(len(sample_names)) width = auto_bar_width(x) rects_p = ax.bar(x, ps, width, color='g', alpha=0.5) show_bar_value(rects_p, '.2f') ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_title(stru_name) ax.set_ylabel('p', color='g') ax.tick_params('y', colors='g') ax.axhline(0.05) ax.axhline(0.01) ax.axhline(0.001) ax.set_xticks(x) ax.set_xticklabels(sample_names) plt.setp(ax.get_xticklabels(), rotation=25, ha='right', rotation_mode='anchor') plt.tight_layout() plt.show()
def plot_compare(ps, sample_names, ts=None, title=''): """ :param ps: sequence a sequence of p values :param sample_names: sequence a sequence of sample names corresponding to the 'ps' :param ts: sequence a sequence of t values corresponding to the 'ps' :param title: str :returns: fig, ax when 'ts' is None :returns: fig, ax, ax_twin when 'ts' is not None """ sample_num = len(sample_names) assert sample_num != 0 assert len(ps) == sample_num fig, ax = plt.subplots() x = np.arange(sample_num) width = auto_bar_width(x) rects_p = ax.bar(x, ps, width, color='g', alpha=0.5) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_title(title) ax.set_ylabel('p', color='g') ax.tick_params('y', colors='g') ax.axhline(0.05) ax.axhline(0.01) ax.axhline(0.001) ax.set_xticks(x) ax.set_xticklabels(sample_names) plt.setp(ax.get_xticklabels(), rotation=-90, ha='left', rotation_mode='anchor') if ts is not None: assert len(ts) == sample_num ax_twin = ax.twinx() rects_t = ax_twin.bar(x, ts, width, color='b', alpha=0.5) ax_twin.legend([rects_t, rects_p], ['t', 'p']) ax_twin.set_ylabel('t', color='b') ax_twin.tick_params('y', colors='b') return fig, ax, ax_twin plt.tight_layout() return fig, ax
def plot_TMA(): import numpy as np import pandas as pd from matplotlib import pyplot as plt from commontool.algorithm.plot import auto_bar_width df = pd.read_csv('ACE-h2estimate_TMA.csv') hemis = ('lh', 'rh') n_hemi = len(hemis) rois = ('pFus', 'mFus', 'pFus_mFus') roi2color = { 'pFus': 'limegreen', 'mFus': 'cornflowerblue', 'pFus_mFus': 'black' } n_roi = len(rois) meas2title = { 'thickness': 'thickness', 'myelin': 'myelin', 'activ': 'face-avg' } n_meas = len(meas2title) x = np.arange(n_hemi) width = auto_bar_width(x, n_roi) fig, 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]) ax.bar(x + width * offset, data[1], width, yerr=data[[0, 2]], label=roi, color=roi2color[roi]) 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_compare(compare_file, label_ids=None, p_thr=None): file_name = os.path.basename(compare_file) compare_dict = CsvReader(compare_file).to_dict(1) if label_ids is None: label_ids = compare_dict['label_id'] else: label_ids = [str(i) for i in label_ids] if p_thr is not None: label_ids = [i for i in label_ids if float(compare_dict['p'][compare_dict['label_id'].index(i)]) < p_thr] fig, ax = plt.subplots() ax_twin = ax.twinx() if len(label_ids) > 0: x = np.arange(len(label_ids)) width = auto_bar_width(x) y_t = [float(compare_dict['t'][compare_dict['label_id'].index(i)]) for i in label_ids] y_p = [float(compare_dict['p'][compare_dict['label_id'].index(i)]) for i in label_ids] rects_t = ax.bar(x, y_t, width, color='b', alpha=0.5) rects_p = ax_twin.bar(x, y_p, width, color='g', alpha=0.5) ax.legend([rects_t, rects_p], ['t', 'p']) ax.set_xticks(x) xticklabels = [compare_dict['label_name'][compare_dict['label_id'].index(i)] for i in label_ids] ax.set_xticklabels(xticklabels) plt.setp(ax.get_xticklabels(), rotation=-90, ha='left', rotation_mode='anchor') ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_title(file_name) ax.set_ylabel('t', color='b') ax.tick_params('y', colors='b') ax_twin.set_ylabel('p', color='g') ax_twin.tick_params('y', colors='g') ax_twin.axhline(0.05) ax_twin.axhline(0.01) ax_twin.axhline(0.001) plt.tight_layout() plt.show()
def roi_mean_plot(roi_mean_file, ROIitems, colors, xticklabels, ylabel=None, title=None, plot_style='violin'): roi_mean_dict = CsvReader(roi_mean_file).to_dict(axis=1) roi_means_list = [list(map(float, roi_mean_dict[ROIitem])) for ROIitem in ROIitems] ROIitem_num = len(ROIitems) for i in range(ROIitem_num): for j in range(i+1, ROIitem_num): print('{} vs. {}'.format(ROIitems[i], ROIitems[j]), ttest_ind(roi_means_list[i], roi_means_list[j])) plt.figure() if plot_style == 'violin': violin_parts = plt.violinplot(roi_means_list, showmeans=True) for idx, pc in enumerate(violin_parts['bodies']): # https://stackoverflow.com/questions/26291479/changing-the-color-of-matplotlibs-violin-plots pc.set_color(colors[idx]) plt.xticks(range(1, ROIitem_num + 1), xticklabels) elif plot_style == 'bar': x = np.arange(ROIitem_num) y = [np.mean(roi_means) for roi_means in roi_means_list] sems = [sem(roi_means) for roi_means in roi_means_list] width = auto_bar_width(x) rects = plt.bar(x, y, width, edgecolor=colors[0], yerr=sems, facecolor='white') show_bar_value(rects, '.2f') plt.xticks(x, xticklabels) else: raise RuntimeError("Invalid plot style: {}".format(plot_style)) if ylabel is not None: plt.ylabel(ylabel) if title is not None: plt.title(title) ax = plt.gca() ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) plt.tight_layout()
def plot_mean_sem(mean_sem_files, items=None, label_ids=None, xlabel='', ylabel=''): fig, ax = plt.subplots() x = None width = None xticklabels = None rects_list = [] item_num = len(mean_sem_files) for idx, mean_sem_file in enumerate(mean_sem_files): mean_sem_dict = CsvReader(mean_sem_file).to_dict(1) if label_ids is None: label_ids = mean_sem_dict['label_id'] else: label_ids = [str(i) for i in label_ids] if x is None: xticklabels = [mean_sem_dict['label_name'][mean_sem_dict['label_id'].index(i)] for i in label_ids] x = np.arange(len(label_ids)) width = auto_bar_width(x, item_num) y = [float(mean_sem_dict['mean'][mean_sem_dict['label_id'].index(i)]) for i in label_ids] sems = [float(mean_sem_dict['sem'][mean_sem_dict['label_id'].index(i)]) for i in label_ids] rects = ax.bar(x+width*idx, y, width, color='k', alpha=1./((idx+1)/2+0.5), yerr=sems) rects_list.append(rects) if items is not None: assert item_num == len(items) ax.legend(rects_list, items) ax.set_xticks(x+width/2.0*(item_num-1)) ax.set_xticklabels(xticklabels) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) plt.setp(ax.get_xticklabels(), rotation=-90, ha='left', rotation_mode='anchor') # plt.ylim(bottom=5.5) plt.tight_layout() plt.show()
def different_activation_gender_roi2allsubgroup(b_dict): # file paths maps_file = pjoin( project_dir, 'data/HCP_1080/S1200_1080_WM_cope19_FACE-AVG_s2_MSMAll_32k_fs_LR.dscalar.nii' ) cluster_num_dir = pjoin(project_dir, 's2_25_zscore/HAC_ward_euclidean/2clusters') roi_files = pjoin(cluster_num_dir, 'activation/{hemi}{label}_FFA.nii.gz') group_labels_file = pjoin(cluster_num_dir, 'group_labels') with open(group_labels_file) as f: group_labels = np.array(f.read().split(' '), dtype=np.uint16) cifti_reader = CiftiReader(maps_file) lmaps = cifti_reader.get_data('CIFTI_STRUCTURE_CORTEX_LEFT', True) rmaps = cifti_reader.get_data('CIFTI_STRUCTURE_CORTEX_RIGHT', True) # get gender labels subjects = np.array(b_dict['Subject']) genders = np.array(b_dict['Gender']) subjects_m = subjects[genders == 'M'] subjects_f = subjects[genders == 'F'] map2subject = [name.split('_')[0] for name in cifti_reader.map_names()] gender_labels = np.zeros((len(map2subject), ), dtype=np.str) for idx, subj_id in enumerate(map2subject): if subj_id in subjects_m: gender_labels[idx] = 'M' elif subj_id in subjects_f: gender_labels[idx] = 'F' means_m = [] means_f = [] sems_m = [] sems_f = [] for roi in rois: roi_file = roi_files.format(roi[:-1]) roi_mask = nib.load(roi_file).get_data().ravel() roi_vertices = np.where(roi_mask == int(roi[-1]))[0] if roi[0] == 'l': roi_maps = lmaps[:, roi_vertices] elif roi[0] == 'r': roi_maps = rmaps[:, roi_vertices] else: raise RuntimeError("invalid roi name: {}".format(roi)) male_indices = np.logical_and(gender_labels == 'M', group_labels == roi[1]) female_indices = np.logical_and(gender_labels == 'F', group_labels == roi[1]) roi_map_means_m = np.mean(roi_maps[male_indices], 1) roi_map_means_f = np.mean(roi_maps[female_indices], 1) # print('the number of males about {0}: {1}'.format(roi, roi_map_means_m.shape[0])) # print('the number of females about {0}: {1}'.format(roi, roi_map_means_f.shape[0])) print('{0}_male vs. {0}_female: p={1}'.format( roi, ttest_ind(roi_map_means_m, roi_map_means_f)[1])) means_m.append(np.mean(roi_map_means_m)) means_f.append(np.mean(roi_map_means_f)) sems_m.append(sem(roi_map_means_m)) sems_f.append(sem(roi_map_means_f)) x = np.arange(len(rois)) fig, ax = plt.subplots() width = auto_bar_width(x, 2) rects1 = ax.bar(x, means_m, width, color='b', alpha=0.5, yerr=sems_m, ecolor='blue') rects2 = ax.bar(x + width, means_f, width, color='r', alpha=0.5, yerr=sems_f, ecolor='red') # show_bar_value(rects1, '.3f') # show_bar_value(rects2, '.3f') ax.legend((rects1, rects2), ('male', 'female')) ax.set_xticks(x + width / 2.0) ax.set_xticklabels(rois) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_ylabel('activation (z-stat)') plt.tight_layout() plt.show()
def different_activation_gender_1080(b_dict): # file paths maps_file = pjoin( project_dir, 'data/HCP_1080/S1200_1080_WM_cope19_FACE-AVG_s2_MSMAll_32k_fs_LR.dscalar.nii' ) lFFA_file = pjoin(project_dir, 'data/HCP_1080/face-avg_s2/label/lFFA_25.label') rFFA_file = pjoin(project_dir, 'data/HCP_1080/face-avg_s2/label/rFFA_25.label') # get maps cifti_reader = CiftiReader(maps_file) lFFA = nib.freesurfer.read_label(lFFA_file) rFFA = nib.freesurfer.read_label(rFFA_file) lFFA_maps = cifti_reader.get_data('CIFTI_STRUCTURE_CORTEX_LEFT', True)[:, lFFA] rFFA_maps = cifti_reader.get_data('CIFTI_STRUCTURE_CORTEX_RIGHT', True)[:, rFFA] # get subjects' ids subjects = np.array(b_dict['Subject']) genders = np.array(b_dict['Gender']) subjects_m = subjects[genders == 'M'] subjects_f = subjects[genders == 'F'] map2subject = [name.split('_')[0] for name in cifti_reader.map_names()] gender_labels = np.zeros((lFFA_maps.shape[0], ), dtype=np.str) for idx, subj_id in enumerate(map2subject): if subj_id in subjects_m: gender_labels[idx] = 'M' elif subj_id in subjects_f: gender_labels[idx] = 'F' lFFA_maps_mean = np.mean(lFFA_maps, 1) rFFA_maps_mean = np.mean(rFFA_maps, 1) lFFA_maps_mean_m = np.mean(lFFA_maps[gender_labels == 'M'], 1) lFFA_maps_mean_f = np.mean(lFFA_maps[gender_labels == 'F'], 1) rFFA_maps_mean_m = np.mean(rFFA_maps[gender_labels == 'M'], 1) rFFA_maps_mean_f = np.mean(rFFA_maps[gender_labels == 'F'], 1) print('lFFA vs. rFFA: p={}'.format( ttest_ind(lFFA_maps_mean, rFFA_maps_mean)[1])) print('lFFA_male vs. lFFA_female: p={}'.format( ttest_ind(lFFA_maps_mean_m, lFFA_maps_mean_f)[1])) print('rFFA_male vs. rFFA_female: p={}'.format( ttest_ind(rFFA_maps_mean_m, rFFA_maps_mean_f)[1])) print('lFFA_male vs. rFFA_male: p={}'.format( ttest_ind(lFFA_maps_mean_m, rFFA_maps_mean_m)[1])) print('lFFA_female vs. rFFA_female: p={}'.format( ttest_ind(lFFA_maps_mean_f, rFFA_maps_mean_f)[1])) l_mean = np.mean(lFFA_maps_mean) r_mean = np.mean(rFFA_maps_mean) l_sem = sem(lFFA_maps_mean) r_sem = sem(rFFA_maps_mean) l_m_mean = np.mean(lFFA_maps_mean_m) l_f_mean = np.mean(lFFA_maps_mean_f) r_m_mean = np.mean(rFFA_maps_mean_m) r_f_mean = np.mean(rFFA_maps_mean_f) l_m_sem = sem(lFFA_maps_mean_m) l_f_sem = sem(lFFA_maps_mean_f) r_m_sem = sem(rFFA_maps_mean_m) r_f_sem = sem(rFFA_maps_mean_f) x = np.arange(2) fig, ax = plt.subplots() width = auto_bar_width(x, 3) rects1 = ax.bar(x, [l_mean, r_mean], width, color='g', alpha=0.5, yerr=[l_sem, r_sem], ecolor='green') rects2 = ax.bar(x - width, [l_m_mean, r_m_mean], width, color='b', alpha=0.5, yerr=[l_m_sem, r_m_sem], ecolor='blue') rects3 = ax.bar(x + width, [l_f_mean, r_f_mean], width, color='r', alpha=0.5, yerr=[l_f_sem, r_f_sem], ecolor='red') # show_bar_value(rects1, '.3f') # show_bar_value(rects2, '.3f') # show_bar_value(rects3, '.3f') ax.legend((rects1, rects2, rects3), ('both', 'male', 'female')) ax.set_xticks(x) ax.set_xticklabels(['lFFA_2mm', 'rFFA_2mm']) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_ylabel('activation (z-stat)') plt.tight_layout() plt.show()
def plot_mean_sem(mean_sem_files, items=None, sample_names=None, xlabel='', ylabel=''): """ :param mean_sem_files: sequence a sequence of file paths which are generated from 'calc_mean_sem' :param items: sequence a sequence of item names corresponding to the 'mean_sem_files' :param sample_names: collection a collection of sample names of interested :param xlabel: str :param ylabel: str :returns: fig, ax """ fig, ax = plt.subplots() x = None width = None rects_list = [] item_num = len(mean_sem_files) for idx, mean_sem_file in enumerate(mean_sem_files): mean_sem_dict = CsvReader(mean_sem_file).to_dict(1) if sample_names is None: sample_names = mean_sem_dict['sample_name'] if x is None: x = np.arange(len(sample_names)) width = auto_bar_width(x, item_num) y = [ float(mean_sem_dict['mean'][mean_sem_dict['sample_name'].index(i)]) for i in sample_names ] sems = [ float(mean_sem_dict['sem'][mean_sem_dict['sample_name'].index(i)]) for i in sample_names ] rects = ax.bar(x + width * idx, y, width, color='k', alpha=1. / ((idx + 1) / 2 + 0.5), yerr=sems) rects_list.append(rects) if items is not None: assert item_num == len(items) ax.legend(rects_list, items) ax.set_xticks(x + width / 2.0 * (item_num - 1)) ax.set_xticklabels(sample_names) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) plt.setp(ax.get_xticklabels(), rotation=-90, ha='left', rotation_mode='anchor') plt.tight_layout() return fig, ax
def explore_label_dice(n_clusters_dir): import nibabel as nib from commontool.algorithm.tool import calc_overlap c1_r = nib.freesurfer.read_label( pjoin(n_clusters_dir, 'cluster1_ROI_z2.3.label')) c2_r = nib.freesurfer.read_label( pjoin(n_clusters_dir, 'cluster2_ROI_z2.3.label')) c3_r = nib.freesurfer.read_label( pjoin(n_clusters_dir, 'cluster3_ROI_z2.3.label')) c4_r1 = nib.freesurfer.read_label( pjoin(n_clusters_dir, 'cluster4_ROI1_z2.3.label')) c4_r2 = nib.freesurfer.read_label( pjoin(n_clusters_dir, 'cluster4_ROI2_z2.3.label')) c5_r1 = nib.freesurfer.read_label( pjoin(n_clusters_dir, 'cluster5_ROI1_z2.3.label')) c5_r2 = nib.freesurfer.read_label( pjoin(n_clusters_dir, 'cluster5_ROI2_z2.3.label')) c6_r1 = nib.freesurfer.read_label( pjoin(n_clusters_dir, 'cluster6_ROI1_z2.3.label')) c6_r2 = nib.freesurfer.read_label( pjoin(n_clusters_dir, 'cluster6_ROI2_z2.3.label')) c1_6_acti_top10 = nib.load( pjoin(n_clusters_dir, 'top_acti_ROIs_percent10.0.nii.gz')).get_data() c123_r_z = [c1_r, c2_r, c3_r] c123_r_top = c1_6_acti_top10[:3] c123_dice = [] c123_xticks = [ 'c1_c2_z2.3', 'c1_c3_z2.3', 'c2_c3_z2.3', 'c1_c2_top10', 'c1_c3_top10', 'c2_c3_top10' ] c456_r_z = [ np.concatenate((c4_r1, c4_r2)), np.concatenate((c5_r1, c5_r2)), np.concatenate((c6_r1, c6_r2)) ] c456_r_top = c1_6_acti_top10[3:] c456_dice = [] c456_xticks = [ 'c4_c5_z2.3', 'c4_c6_z2.3', 'c5_c6_z2.3', 'c4_c5_top10', 'c4_c6_top10', 'c5_c6_top10' ] for idx, i in enumerate(c123_r_z[:-1]): for j in c123_r_z[idx + 1:]: c123_dice.append(calc_overlap(i, j)) for idx, i in enumerate(c123_r_top[:-1]): for j in c123_r_top[idx + 1:]: c123_dice.append(calc_overlap(i, j, 1, 1)) for idx, i in enumerate(c456_r_z[:-1]): for j in c456_r_z[idx + 1:]: c456_dice.append(calc_overlap(i, j)) for idx, i in enumerate(c456_r_top[:-1]): for j in c456_r_top[idx + 1:]: c456_dice.append(calc_overlap(i, j, 1, 1)) x = np.arange(6) width = auto_bar_width(x) plt.figure() rects = plt.bar(x, c123_dice, width, color='b') show_bar_value(rects, '.2%') plt.ylabel('dice') plt.xticks(x, c123_xticks) ax = plt.gca() ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) plt.figure() rects = plt.bar(x, c456_dice, width, color='b') show_bar_value(rects, '.2%') plt.ylabel('dice') plt.xticks(x, c456_xticks) ax = plt.gca() ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False)
def explore_roi_stats(n_clusters_dir): roi_path = pjoin(n_clusters_dir, 'mean_map_ROIs.nii.gz') stats_path = pjoin(n_clusters_dir, 'statistics.csv') roi_maps = read_nifti(roi_path) stats_reader = CsvReader(stats_path) row_dict = stats_reader.to_dict(keys=['#subjects']) numb_items = ['1', '2'] numb_dict = OrderedDict() for item in numb_items: numb_dict[item] = 0 type_items = ['r_pFFA', 'r_mFFA', 'both', 'unknown'] type_dict = OrderedDict() for item in type_items: type_dict[item] = 0 for idx, roi_map in enumerate(roi_maps): map_set = set(roi_map) subjects_num = int(row_dict['#subjects'][idx]) if 0 not in map_set: raise RuntimeError('Be careful! There is no zero in one roi_map') if len(map_set) == 2: numb_dict['1'] += subjects_num if 1 in map_set: type_dict['r_pFFA'] += subjects_num elif 2 in map_set: type_dict['r_mFFA'] += subjects_num elif 3 in map_set: type_dict['unknown'] += subjects_num else: raise RuntimeError( 'Be careful! the only one ROI label is not in (1, 2, 3)') elif len(map_set) == 3: numb_dict['2'] += subjects_num if 1 in map_set and 2 in map_set: type_dict['both'] += subjects_num else: raise RuntimeError( 'Be careful! the two ROI labels are not 1 and 2') else: raise RuntimeError( 'Be careful! the number of ROI labels is not 1 or 2') plt.figure() x = np.arange(len(numb_items)) width = auto_bar_width(x) rects = plt.bar(x, numb_dict.values(), width, color='b') show_bar_value(rects) plt.ylabel('#subjects') plt.xticks(x, numb_dict.keys()) ax = plt.gca() ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) plt.savefig(pjoin(n_clusters_dir, 'numb_count.png')) plt.figure() x = np.arange(len(type_items)) width = auto_bar_width(x) rects = plt.bar(x, type_dict.values(), width, color='b') show_bar_value(rects) plt.ylabel('#subjects') plt.xticks(x, type_dict.keys()) ax = plt.gca() ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) plt.savefig(pjoin(n_clusters_dir, 'type_count.png'))
subgroup_ids = subjects_1080[group_labels == label] behavior_dict = get_behavior_dict(subject_dict, behavior_names, subgroup_ids) male_num, female_num = explore_Gender(behavior_dict, label) male_nums.append(male_num) female_nums.append(female_num) for item in float_items: float_data_dict[item].append( explore_float_data(behavior_dict, item, label)) # plot x = np.arange(label_num) width = auto_bar_width(x, 2) plt.figure() ax = plt.gca() rects1 = ax.bar(x, male_nums, width, color='b') rects2 = ax.bar(x + width, female_nums, width, color='r') show_bar_value(rects1) show_bar_value(rects2) ax.legend((rects1[0], rects2[1]), ('male', 'female')) ax.set_xticks(x + width / 2.0) ax.set_xticklabels(labels) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_title('Gender') ax.set_xlabel('subgroup label') ax.set_ylabel('count') # plt.savefig(pjoin(cluster_num_dir, 'Gender.png'))
def plot_mean_sem(): import numpy as np import pandas as pd import matplotlib.pyplot as plt from os.path import join as pjoin from commontool.algorithm.plot import auto_bar_width, show_bar_value stru_name = 'myelin' project_dir = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_clustering' stru_dir = pjoin(project_dir, 's2_25_zscore/HAC_ward_euclidean/2clusters/structure') mean_sem_file = pjoin(stru_dir, 'mean_sem/{}'.format(stru_name)) mean_sems = pd.read_csv(mean_sem_file) intra_inter_pairs = np.array( [[ 'G1_acti_corr_G1_{}_lFFA'.format(stru_name), 'G1_acti_corr_G2_{}_lFFA'.format(stru_name) ], [ 'G2_acti_corr_G2_{}_lFFA'.format(stru_name), 'G2_acti_corr_G1_{}_lFFA'.format(stru_name) ], [ 'G1_acti_corr_G1_{}_rFFA'.format(stru_name), 'G1_acti_corr_G2_{}_rFFA'.format(stru_name) ], [ 'G2_acti_corr_G2_{}_rFFA'.format(stru_name), 'G2_acti_corr_G1_{}_rFFA'.format(stru_name) ]]) names = mean_sems['names'].to_list() means = list(map(float, mean_sems['means'])) sems = list(map(float, mean_sems['sems'])) fig, ax = plt.subplots() x = np.arange(intra_inter_pairs.shape[0]) item_num = intra_inter_pairs.shape[1] width = auto_bar_width(x, item_num) for idx in range(item_num): sub_means = [means[names.index(i)] for i in intra_inter_pairs[:, idx]] sub_sems = [sems[names.index(i)] for i in intra_inter_pairs[:, idx]] rects = ax.bar(x + width * idx, sub_means, width, color='k', alpha=1. / ((idx + 1) / 2 + 0.5), yerr=sub_sems) show_bar_value(rects, '.3f') xticklabels1 = [name for name in intra_inter_pairs[:, 0]] xticklabels2 = [name for name in intra_inter_pairs[:, 1]] xticklabels = xticklabels1 + xticklabels2 ax.set_xticks(np.r_[x, x + width * (item_num - 1)]) ax.set_xticklabels(xticklabels) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_ylabel('pearsonr') plt.setp(ax.get_xticklabels(), rotation=25, ha='right', rotation_mode='anchor') plt.tight_layout() plt.show()
metric0 = assessment_metrics[0] x = np.arange(n_labels) x_labels = np.array([len(set(labels)) for labels in labels_list]) vline_plotter_holder = [] for metric in assessment_metrics: # plot assessment curve v_plotter = ClusteringVlineMoverPlotter(FFA_patterns, labels_list, subject_ids, subproject_dir) if metric0 == 'dice': y = np.mean(assessments_dict[metric0], 1) sem = stats.sem(assessments_dict[metric0], 1) v_plotter.axes[0].plot(x, y, 'b.-') v_plotter.axes[0].fill_between(x, y - sem, y + sem, alpha=0.5) elif 'elbow' in metric0: width = auto_bar_width(x) y1 = assessments_dict[metric0] y2 = [y1[i] - y1[i + 1] for i in x[:-1]] y3 = [y2[i] - y2[i + 1] for i in x[:-2]] # v_plotter.axes[0].bar(x, y1, width, color='cyan') v_plotter.axes[0].plot(x, y1, 'b.-') fig2, ax2 = plt.subplots() ax2.plot(x[:-1], y2, 'b.-') ax2.set_title('assessment for #subgroups') ax2.set_xlabel('#subgroups') ax2.set_ylabel(metric0 + "'") if len(x[:-1]) > 20: middle_idx = int(len(x[:-1]) / 2) ax2.set_xticks(x[:-1][[0, middle_idx, -1]]) ax2.set_xticklabels(x_labels[1:][[0, middle_idx, -1]]) else: