예제 #1
0
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()
예제 #2
0
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()