Пример #1
0
def set_axis_layout(E, ax, bin_type, labelsize=8):
    # if bin_type.endswith('bins'):
    #     # if bin_type == '12bins'
    #     major_xticks, minor_xticks = (4, 2)  
    #     # elif bin_type == '24bins':
    #     #     major_xticks, minor_xticks = (24, 1)
    # else:
    #     stop

    # formatter = '%d' if not E.BIN_SHIFT else '%1.1f'
    # ax.xaxis.set_major_locator(MultipleLocator(major_xticks))
    # ax.xaxis.set_major_formatter(FormatStrFormatter(formatter))
    # ax.xaxis.set_minor_locator(MultipleLocator(minor_xticks))
    xticks, xlabels = get_CT_ticks_labels(bin_type, SHIFT=E.BIN_SHIFT, tshift=E.binTimeShift)
    rotation = 0
    if E.BIN_SHIFT:
        labelsize = 2
        if bin_type.endswith('bins'):
            rotation = 90
        
    ax.set_xticks(xticks)
    ax.set_xticklabels(xlabels, rotation=rotation)

    ax.set_xlim([xticks[0]-.5, xticks[-1]+.5])
    ax.set_ylim([0, ax.get_ylim()[1]])

    ax.tick_params(axis='both', which='both', labelsize=labelsize, direction='out')
    # DC grey background
    ax.axvspan(12, 24, fc='.9', ec='.9', zorder=-1)     
    ax_cleanup(ax)
Пример #2
0
def set_panel_layout(E, fig, ax, feats, num_subplot, ymaxs, num_bins=12):
    num_days = len(E.daysToUse)
    ax.set_xlim([0, num_days * num_bins + 1])
    if ymaxs is not None:
        ax.set_ylim([0, ymaxs[num_subplot]])

    xticks = np.arange(0, num_days * num_bins + 1, 6)
    ax.set_xticks(xticks)
    ax.set_xticklabels(xticks * 2)
    ylims = np.array(ax.get_ylim())
    if feats[num_subplot] == 'ASP':
        ylims = [0, 1]
    ax.set_ylim([0, ylims[1]])

    for x in xticks[::2]:
        ax.axvline(x=x, ymax=ylims[1], color='.7', zorder=0)

    cnt = E.daysToUse[0]
    for x in xticks[1::2]:
        ax.text(x,
                .9 * ylims[1],
                'day%d' % cnt,
                fontsize=6,
                color='.7',
                ha='center')
        cnt += 1

    ax.tick_params(axis='both', which='both', labelsize=7)
    ax.set_aspect('auto')
    ax_cleanup(ax)

    plt.subplots_adjust(left=0.1, right=0.88, hspace=0.6)  #, wspace=0.15)
Пример #3
0
def plot_features_distribution_binned_and_avgs_by_strain(E, bin_type='24HDCLC', NORMED=False):
    features = E.features
    _, _, _, _, md_data, md_labels = E.load_feature_vectors(features, bin_type) 

    if bin_type == '24HDCLC':
        bins = ['24H', 'DC', 'LC']
    elif bin_type == '12bins':
        bins = ['CT%d-%d' %(x, x+2) for x in range(6, 26, 2)] + \
                    ['CT%d-%d' %(x, x+2) for x in range(2, 6, 2)]

    level = 'mouseday'    
    hist_bins = 50
    for b, tbin in enumerate(bins[:1]):
        dirname_out = os.path.join(E.figures_dir_subpar, 'features/vectors/distributions/%s/strain_breakdown/%s/' %(bin_type, tbin))
        if not os.path.isdir(dirname_out): os.makedirs(dirname_out) 
        for strain in xrange(E.num_strains):
            idx = md_labels[:, 0] == strain
            data = md_data
            fig, axes = plt.subplots(8, 3, figsize=(8, 12))
            for f, feat in enumerate(features):
                ax = axes.flatten()[f]
                arr = data[f, idx, b]

                # set xmax
                x_max = np.mean(data[f]) + 5*np.std(data[f])
                hist_bins = np.linspace(0, x_max, 50)
                weights = np.ones_like(arr)
                if NORMED:
                    weights /=len(arr)
                color = E.plot_settings_dict[features[f]]['color']

                y, _, _ = ax.hist(arr, bins=hist_bins,  weights=weights, color=color)
                ax.set_xlim(0, x_max)
                ax.tick_params(axis='both', labelsize=6)
                plt.subplots_adjust(left=0.1, right=0.88, hspace=0.6)
                ax.set_title(feat, fontsize=10)
                plotting_utils.ax_cleanup(ax)

            fig.text(0.05, 0.95, E.strain_names[strain], fontsize=16)
            fig.suptitle('Features distributions\nbinned and averages\nbinned%s %s' %(tbin, level))
            fname = dirname_out + "%s_all_features_distribution_binavgs_%s_strain%d.pdf" %(level, tbin, strain)
            fig.savefig(fname, bbox_inches='tight')
            print "saved to:", fname
            plt.close()
Пример #4
0
def plot_features_distribution_all_data(E, NORMED=False, GENERATE=False, VERBOSE=False):
    """24H and mouseday only
    """
    features = E.features[:3]
    # features dictionary each key (feature) is a list - 16 strains - 
    # of lists - all feature values during days -
    all_data = {}
    for feat in features:
        all_data[feat] = E.generate_feature_arrays(feat, GENERATE, VERBOSE)
    return all_data
        # # manipulate the list
        # arr_list = [list(x) for x in feat_arrays]
        # data.append(np.array(list(itertools.chain(*arr_list))))
    
    hist_bins = 50
    # fig, ax = plt.subplots(figsize=(8, 12))
    fig, axes = plt.subplots(8, 3, figsize=(8, 12))
    for f, feat in enumerate(features):
        ax = axes.flatten()[f]
        arr = data[f]
        # set xmax
        x_max = np.mean(data[f]) + 5*np.std(data[f])
        hist_bins = np.linspace(0, x_max, 50)
        weights = np.ones_like(arr)
        if NORMED:
            weights /=len(arr)
        color = E.plot_settings_dict[features[f]]['color']

        y, _, _ = ax.hist(arr, bins=hist_bins,  weights=weights, color=color)
        ax.set_xlim(0, x_max)
        ax.tick_params(axis='both', labelsize=6)
        plt.subplots_adjust(left=0.1, right=0.88, hspace=0.6)
        ax.set_title(feat, fontsize=10)
        plotting_utils.ax_cleanup(ax)

    fig.suptitle('Features distributions\nall data 24H  from all mousedays')
    dirname_out = os.path.join(E.figures_dir_subpar, 'features/vectors/distributions/')
    if not os.path.isdir(dirname_out): os.makedirs(dirname_out)  
    fname = dirname_out + "mousedays_all_features_distribution_all_data_24H.pdf" 
    fig.savefig(fname, bbox_inches='tight')
    print "saved to:", fname
    plt.close()

    return feat_arrays
Пример #5
0
def set_panel_layout(E, fig, ax, feats, num_subplot, ymaxs):
    # xticks = E.daysToUse
    ax.set_xlim([xticks[0] - 1, xticks[-1] + 1])
    if ymaxs is not None:
        ax.set_ylim([0, ymaxs[num_subplot]])

    if len(E.daysToUse) > 20:
        xticks = E.daysToUse[::2]
    ax.set_xticks(xticks)
    ax.set_xticklabels(xticks)
    ylims = np.array(ax.get_ylim())
    if feats[num_subplot] == 'ASP':
        ylims = [0, 1]
    ax.set_ylim([0, ylims[1]])
    ax.tick_params(axis='both', which='both', labelsize=7)
    ax.set_aspect('auto')
    ax_cleanup(ax)

    plt.subplots_adjust(left=0.1, right=0.88, hspace=0.6)  #, wspace=0.15)
Пример #6
0
def plot_explained_variance_ratio(experiment, X, pca, level, lastcomp, fname,
                                  EXCLUDE_SS):
    E = experiment

    components = pca.components_
    num_components = pca.n_components_
    expl_variance = pca.explained_variance_ratio_

    figsize, nrows, ncols, _, _ = get_subplot_specs(E, num_subplots=2)
    fig, (ax0, ax1) = plt.subplots(nrows=nrows, ncols=ncols, figsize=figsize)
    ax0.plot(range(len(components)), expl_variance, color='.4')
    ax1.plot(range(len(components))[-lastcomp:],
             expl_variance[-lastcomp:],
             color='.4')

    ax0.set_ylim([0, .35])
    ax1.set_ylim([0, .0006])

    ax1.xaxis.set_major_locator(MultipleLocator(1))
    for ax in [ax0, ax1]:
        ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
        ax_cleanup(ax)

    # annotate close-up
    text = 'close up,\nlast %d components' % lastcomp
    ax1.text(.5, .7, text, fontsize=8, transform=ax1.transAxes)

    text = '%d strains, ' %E.num_strains if not EXCLUDE_SS \
            else '%d strains (no AKR, CAST, CZECH), ' %(E.num_strains-3)
    level_ = 'mice' if level == 'mouse' else level
    fig_title = 'Explained variance ratio, %d principal components\n%s%d %s, 24H avg data' % (
        num_components, text, X.shape[0], level_)
    subplot_labels(E,
                   fig,
                   xlabel='Principal component',
                   ylabel='explained variance',
                   fig_title=fig_title)
    plt.subplots_adjust(bottom=.15, wspace=.4)
    save_plot(fname, fig)
Пример #7
0
def plot_features_panel_LC_DC_ratio(experiment,
                                    level='group',
                                    bin_type='3cycles',
                                    err_type='sem',
                                    plot_type='features',
                                    ADD_SOURCE=True):
    E = experiment

    print "%s, level: %s, bin_type: %s, err_type: %s, days: %s" % (
        plot_features_panel_LC_DC_ratio.__name__, level, bin_type, err_type,
        E.daysToUse)

    features = E.features_by_activity
    num_features = len(features)
    days_to_use = E.daysToUse

    dirname = E.figures_dir + '%s/vectors/panels_expdays/%s/%s_days/' % (
        plot_type, bin_type, E.use_days)
    if not os.path.isdir(dirname): os.makedirs(dirname)
    fname = dirname + '%s_panel_expdays_LC_DC_ratio' % (plot_type
                                                        )  #s, err_type)

    # using mouseday value
    # ratios = E.generate_feature_vectors_LC_DC_ratio_expdays(features, level, bin_type, err_type)

    # using group averages value
    data_ = E.generate_feature_vectors_expdays(features, level, bin_type,
                                               err_type)
    ratios_ = data_[:, :, :, 2, 0] / data_[:, :, :, 1, 0]  # LC/DC, avgs

    figsize = (14.4, 4.8)
    nrows, ncols = 4, 7
    fig, axes = plt.subplots(nrows, ncols, figsize=figsize, sharex=True)
    xs = days_to_use

    cnt = 0
    for n in xrange(nrows):
        for m in xrange(ncols):
            ax = axes.flatten()[n * ncols + m]
            if n == 0 and m >= 3:
                ax.axis('off')
                continue

            feature = features[cnt]
            colors = get_two_group_features_colors(E, feature)
            subtitle = '%s %s' % (feature,
                                  E.plot_settings_dict[feature]['unit'])

            for g in xrange(E.num_strains):
                # # using mouseday values
                # tkw = {'fmt':'o-', 'ms':2, 'elinewidth':.5, 'capsize':1}
                # ax.errorbar(xs, ratios[g, cnt, :, 0], yerr=ratios[g, cnt, :, 1],
                #     color=colors[g], mec=colors[g], **tkw)

                # using group averages value
                tkw = {'marker': 'o', 'ms': 2, 'zorder': 0}
                ax.plot(xs,
                        ratios_[g, cnt, :],
                        color=colors[g],
                        mec=colors[g],
                        **tkw)
                # use when plotting all for comparison
                # colors_ = ['r', 'y']
                # ax.plot(xs, ratios_[g, cnt, :], color=colors_[g], mec=colors_[g], **tkw)

            if E.short_name == 'HFD2':
                d_day = E.dietChangeDayNumbers[0]
                ax.axvspan(xmin=d_day,
                           xmax=days_to_use[-1],
                           color='.9',
                           zorder=0)

            xlims = ax.get_xlim()
            ax.set_xlim((4, xlims[1]))
            ax.set_ylim((0, ax.get_ylim()[1]))
            ax_cleanup(ax)
            ax.set_title(subtitle, fontsize=10)

            cnt += 1

    fig.text(.6,
             .9,
             '0: %s, control, 24H (grayscale)' % E.strain_names[0],
             fontsize=8)
    fig.text(.6,
             .85,
             '1: %s, knock-out, 24H (colors)' % E.strain_names[1],
             fontsize=8)
    fig.text(.6, .8, 'gray background: diet change', fontsize=8)

    xlabel = 'Experiment Day'
    add_xylabels(fig, xlabel=xlabel, xypad=-.02, labelsize=10)

    days_string = E.get_days_to_use_text()
    fig_title = '%s Experiment\nFeatures vs days, LC/DC ratio %s avg$\pm$%s\nacross %s days: %s' % (
        E.short_name, level, err_type, E.use_days.replace(
            '_', '-').title(), days_string)

    add_titles_notes(E, fig, title=fig_title)

    plt.subplots_adjust(hspace=.6, wspace=.34)
    save_plot(fname, fig, ADD_SOURCE=ADD_SOURCE, sypad=-.02)
Пример #8
0
def plot_features_panel_DC_LC(experiment,
                              level='group',
                              bin_type='3cycles',
                              err_type='sem',
                              plot_type='features',
                              ADD_SOURCE=True):

    E = experiment

    print "%s, level: %s, bin_type: %s, err_type: %s, days: %s" % (
        plot_features_panel_DC_LC.__name__, level, bin_type, err_type,
        E.daysToUse)

    features = E.features_by_activity
    num_features = len(features)
    days_to_use = E.daysToUse

    dirname = E.figures_dir + '%s/vectors/panels_expdays/%s/%s_days/' % (
        plot_type, bin_type, E.use_days)
    if not os.path.isdir(dirname): os.makedirs(dirname)
    fname = dirname + '%s_panel_expdays_DC_LC_group_%s' % (plot_type, err_type)

    # groups, features, days, cycles, avg/err
    data = E.generate_feature_vectors_expdays(features, level, bin_type,
                                              err_type)
    new_data = data[:, :, :, 1:, :]

    figsize = (14.4, 4.8)
    nrows, ncols = 4, 7
    fig, axes = plt.subplots(nrows, ncols, figsize=figsize, sharex=True)
    xs = days_to_use

    cnt = 0
    for n in xrange(nrows):
        for m in xrange(ncols):
            ax = axes.flatten()[n * ncols + m]
            if n == 0 and m >= 3:
                ax.axis('off')
                continue

            feature = features[cnt]
            # get colors
            if feature.startswith('AS'):
                color1, color2 = [E.fcolors['AS'].values()[x] for x in [1, 3]]
            elif feature.startswith('T'):
                color1, color2 = [
                    E.fcolors[feature[1:]].values()[x] for x in [1, 3]
                ]
            else:
                color1, color2 = [
                    E.fcolors[feature[:1]].values()[x] for x in [1, 3]
                ]

            colors = ['.4', '.7', color1, color2]

            subtitle = '%s %s' % (feature,
                                  E.plot_settings_dict[feature]['unit'])

            for g in xrange(E.num_strains):
                tkw = {'fmt': 'o-', 'ms': 2, 'elinewidth': .5, 'capsize': 1}
                # DC
                mec = '0' if g == 0 else colors[2 * g]
                ax.errorbar(xs,
                            new_data[g, cnt, :, 0, 0],
                            yerr=new_data[g, cnt, :, 0, 1],
                            color=colors[2 * g],
                            mec=mec,
                            **tkw)
                #LC
                mc = '0' if g == 0 else colors[2 * g + 1]
                ax.errorbar(xs,
                            new_data[g, cnt, :, 1, 0],
                            yerr=new_data[g, cnt, :, 1, 1],
                            color=colors[2 * g + 1],
                            mec=mec,
                            **tkw)

            if E.short_name == 'HFD2':
                d_day = E.dietChangeDayNumbers[0]
                ax.axvspan(xmin=d_day,
                           xmax=days_to_use[-1],
                           color='.9',
                           zorder=0)

            xlims = ax.get_xlim()
            ax.set_xlim((4, xlims[1]))
            ax.set_ylim([0, ax.get_ylim()[1]])
            ax_cleanup(ax)
            ax.set_title(subtitle, fontsize=10)

            cnt += 1

    fig.text(.6,
             .9,
             '0: %s, control (grayscale)\nDC:black, LC: gray' %
             E.strain_names[0],
             fontsize=8)
    fig.text(.6,
             .83,
             '1: %s, knock-out (colors)\nDC: dark, LC: light ' %
             E.strain_names[1],
             fontsize=8)
    fig.text(.6, .78, 'gray background: diet change', fontsize=8)

    xlabel = 'Experiment Day'
    add_xylabels(fig, xlabel=xlabel, xypad=-.02, labelsize=10)

    days_string = E.get_days_to_use_text()
    fig_title = '%s Experiment\nFeatures, DC vs. LC, %s avg and %s\nacross %s days: %s' % (
        E.short_name, level, err_type, E.use_days.replace(
            '_', '-').title(), days_string)

    add_titles_notes(E, fig, title=fig_title)

    plt.subplots_adjust(hspace=.6, wspace=.34)
    save_plot(fname, fig, ADD_SOURCE=ADD_SOURCE, sypad=-.02)
Пример #9
0
def plot_features_panel_12bins(experiment,
                               level='group',
                               bin_type='12bins',
                               err_type='sem',
                               plot_type='features',
                               ADD_SOURCE=True):

    E = experiment

    print "%s, level: %s, bin_type: %s, err_type: %s, days: %s" % (
        plot_features_panel_12bins.__name__, level, bin_type, err_type,
        E.daysToUse)

    features = E.features_by_activity
    num_features = len(features)
    days_to_use = E.daysToUse
    num_bins = int(bin_type.strip('bins'))
    tbins = my_utils.get_tbins_string(bin_type)

    dirname = E.figures_dir + '%s/vectors/panels_expdays/%s/%s_days/' % (
        plot_type, bin_type, E.use_days)
    if not os.path.isdir(dirname): os.makedirs(dirname)

    data = E.generate_feature_vectors_expdays(features, level, bin_type,
                                              err_type)
    ymaxs = get_ymaxs(data)

    # stop
    for b in xrange(num_bins):
        new_data = data[:, :, :, b, :]
        figsize = (14.4, 4.8)
        nrows, ncols = 4, 7
        fig, axes = plt.subplots(nrows, ncols, figsize=figsize, sharex=True)
        xs = days_to_use

        cnt = 0
        for n in xrange(nrows):
            for m in xrange(ncols):
                ax = axes.flatten()[n * ncols + m]
                if n == 0 and m >= 3:
                    ax.axis('off')
                    continue

                feature = features[cnt]
                colors = get_two_group_features_colors(E, feature)
                subtitle = '%s %s' % (feature,
                                      E.plot_settings_dict[feature]['unit'])

                for g in xrange(E.num_strains):
                    tkw = {
                        'fmt': 'o-',
                        'ms': 2,
                        'elinewidth': .5,
                        'capsize': 1
                    }
                    ax.errorbar(xs,
                                new_data[g, cnt, :, 0],
                                yerr=new_data[g, cnt, :, 1],
                                color=colors[g],
                                mec=colors[g],
                                **tkw)

                if E.short_name == 'HFD2':
                    d_day = E.dietChangeDayNumbers[0]
                    ax.axvspan(xmin=d_day,
                               xmax=days_to_use[-1],
                               color='.9',
                               zorder=0)

                # xmin, xmax = ax.get_xlim()
                # ax.set_xlim((xmin-.5, xmax+.5))
                # ax.set_ylim([0, ymaxs[cnt]])
                ax_cleanup(ax)
                ax.set_title(subtitle, fontsize=10)

                cnt += 1

        xmin, xmax = ax.get_xlim()
        ax.set_xlim((xmin - .5, xmax + .5))

        fig.text(.6,
                 .9,
                 '0: %s, control, %s (grayscale)' %
                 (E.strain_names[0], tbins[b]),
                 fontsize=8)
        fig.text(.6,
                 .85,
                 '1: %s, knock-out, %s (colors)' %
                 (E.strain_names[1], tbins[b]),
                 fontsize=8)
        fig.text(.6, .8, 'gray background: diet change', fontsize=8)

        xlabel = 'Experiment Day'
        add_xylabels(fig, xlabel=xlabel, xypad=-.02, labelsize=10)

        days_string = E.get_days_to_use_text()
        fig_title = '%s Experiment\nFeatures, %s %s avg$\pm$%s, %s\nacross %s days: %s' % (
            E.short_name, bin_type, level, err_type, tbins[b],
            E.use_days.replace('_', '-').title(), days_string)

        add_titles_notes(E, fig, title=fig_title)

        plt.subplots_adjust(hspace=.6, wspace=.34)
        fname = dirname + '%s_panel_expdays_%s_group_%s_bin%d' % (
            plot_type, bin_type, err_type, b)

        save_plot(fname, fig, ADD_SOURCE=ADD_SOURCE, sypad=-.02)
Пример #10
0
def plot_HCM_system_time(E, ttype='maintenance', ADD_SOURCE=True):
    """plots system_on_off 
    """
    if ttype == 'maintenance':
        HCM_time, labels = E.get_HCM_maintenance_time() 
        xlims = [2, 8]
        xticks = range(2, 10+1, 2)
        xticklabels = xticks

    elif ttype == 'recording':
        HCM_time, labels = E.get_HCM_recording_time() 
        xlims = [2, 34]
        xticks = range(4, 32+1, 4)
        xticklabels = [str(x) for x in range(4, 24+1, 4)] \
                    + [str(x) for x in range(4, 10+1, 4)]
        
    print "%s, %s days.." %(plot_HCM_system_time.__name__, E.use_days)

    figsize, nrows, ncols, sharex, sharey = \
            get_subplot_specs(E, num_subplots=len(E.groups), plot_type='subplot')
    fig, axes = plt.subplots(nrows, ncols, figsize=figsize, sharex=sharex, sharey=sharey)

    for strain in range(nrows * ncols):
        ax = axes.flatten()[strain] if type(axes) == np.ndarray else axes
        idx = labels [:, 0] == strain
        data = HCM_time[idx] / 3600. - 7
        c = 0
        for row in data:
            ax.plot([row[0], row[1]], [c, c], lw=0.5, color='0.5')
            c += 1

        ax.set_xlim(xlims)
        ax.set_xticks(xticks)
        ax.set_xticklabels(xticklabels)
        
        if ttype == 'maintenance':
            text = 'avg:\n%1.1fmin' %(np.diff(data).mean() * 60)
        
        elif ttype == 'recording':
            text = 'avg:\n%1.2fhrs' %np.diff(data).mean()
            ax.axvline(x=12, linestyle='--', color='k')
            ax.axvline(x=24, linestyle='--', color='k')

        ax.text(.9, .9, text, fontsize=10, 
                ha='center', transform=ax.transAxes)  
        

        ax.set_xlabel('Circadian Time')
        ax.set_ylabel('Obs. days', labelpad=10)
        ax.set_title('group%d: %s' %(strain, E.strain_names[strain]), y=1.05)  

        ax_cleanup(ax, labelsize=10)


    title = '%sExp\n%s days\nDaily %s Time' %(
                E.short_name, E.use_days.replace('_', '-').title(), ttype.title())
    add_titles_notes(E, fig, 
                    title=title,
                    typad=.18,
                    tlxpad=-.1,
                    tlypad=.15,
                    HP_TEXT=False)

    plt.subplots_adjust(wspace=0.4)
    
    # save
    dirname_out = E.figures_dir + 'HCM_recording_time/%s_days/' %E.use_days
    if not os.path.isdir(dirname_out): os.makedirs(dirname_out)
    fname = dirname_out + 'HCMsystem_%s_time_%s_days' %(ttype, E.use_days)
    save_plot(fname, fig, ADD_SOURCE=ADD_SOURCE, sypad=-.1)        
Пример #11
0
def plot_loadings(experiment,
                  X,
                  pca_components,
                  feature_names,
                  level,
                  comps_idx,
                  fname,
                  plot_type,
                  EXCLUDE_SS,
                  ADD_SOURCE=True):
    E = experiment

    num_items = X.shape[0]
    num_components = len(pca_components)
    num_features = len(feature_names)
    assert num_features == num_components, 'wtf'

    # select components
    selected_comps = pca_components[comps_idx]

    fig, ax = plt.subplots(figsize=(7, 3))
    bar_width = .03 if len(comps_idx) == num_components else .1
    colors = [E.plot_settings_dict[feat]['color'] for feat in feature_names]

    if plot_type == 'bar':
        for c, comp in enumerate(selected_comps):
            ind = np.arange(num_features) + c * bar_width
            alpha_step = .03 * comp if len(
                comps_idx) == num_components else .2 * comp
            tkw = {
                'lw': .2,
                'alpha': (1 - alpha_step),
                'color': colors,
                'edgecolor': None,
                'label': 'PC%d' % comp
            }

            ax.bar(ind, pca_components[-(comp + 1)], bar_width, **tkw)

    elif plot_type == 'line':
        colors = get_16_colors()[1::4]
        tkw = {
            'numpoints': 1,
            'fontsize': 6,
            'handletextpad': 1.2,
            'labelspacing': .8,
            'borderaxespad': 1.5,
            'frameon': False,
            'bbox_to_anchor': (1., 1.2),
            'bbox_transform': ax.transAxes
        }
        for c, comp in enumerate(selected_comps):
            ax.plot(comp, color=colors[c], label='comp%d' % (comps_idx[c]))
        leg = ax.legend(**tkw)

    ax.set_xticks(range(num_features))
    ax.set_xticklabels([])
    ax.set_ylim(-1, 1)

    ax.axhline(xmin=0, xmax=num_features, lw=.5, color='k')
    ax.spines['bottom'].set_position('zero')
    ax.spines['bottom'].set_visible(False)
    ax_cleanup(ax)

    # display feature names on top of bars
    xpos = np.arange(num_features) + .2
    heights = selected_comps.max(axis=0) + .04
    heights[5] = heights[4] + .05  # correct FBASR
    if plot_type == 'line': heights += .1
    for p, feat in enumerate(feature_names):
        ax.text(xpos[p],
                heights[p],
                feat,
                fontsize=6,
                ha='center',
                transform=ax.transData)

    # annotate
    if plot_type == 'hist':
        annotation = 'Color-code by feature type.\n' \
                    + 'AS:purple, Feeding:orange, Drinking:blue, Move:green.\n' \
                    + 'For each feature, components from %d (opaque) to\n' %comps_idx[0] \
                    + '%dth (transparent) are shown on a sliding\ntransparency scale.' %comps_idx[-1]
        ax.text(13.8,
                ax.get_ylim()[0],
                annotation,
                fontsize=6,
                va='center',
                transform=ax.transData)

    # title
    text = '%d strains, ' %E.num_strains if not EXCLUDE_SS \
            else '%d strains (no AKR, CAST, CZECH), ' %(E.num_strains-3)
    level_ = 'mice' if level == 'mouse' else level
    fig_title = 'Principal Component loadings, components: %s of %d total\n%s%d %s, 24H avg data\n%s days:%dto%d' % (
        comps_idx, num_components, text, num_items, level_, E.use_days,
        E.daysToUse[0], E.daysToUse[-1])
    subplot_labels(E, fig, xlabel='', ylabel='Loading', fig_title=fig_title)
    save_plot(fname, fig, ADD_SOURCE=ADD_SOURCE)
Пример #12
0
def plot_component_loadings(E,
                            X,
                            pca_components,
                            feature_names,
                            level,
                            fname,
                            EXCLUDE_SS=False,
                            BY_STRAIN=False,
                            strain=None,
                            ADD_SOURCE=True):

    num_items = X.shape[0]
    num_components = len(pca_components)
    num_features = len(feature_names)

    figsize, nrows, ncols, sharex, sharey = get_subplot_specs(
        E, num_subplots=num_components)
    fig, axes = plt.subplots(nrows,
                             ncols,
                             figsize=figsize,
                             sharex=sharex,
                             sharey=sharey)

    xs = range(1, num_components + 1)
    for c, comp in enumerate(pca_components):
        ax = axes.flatten()[c]
        ax.plot(xs, comp, color='.4')
        ax.set_xlim(0, ax.get_xlim()[1])
        ax.set_xticks(range(num_features))
        ax.set_xticklabels([])

        ax.set_title('PC%d' % c, fontsize=8, position=(.5, 1.15), ha='center')
        ax.axhline(xmin=0, xmax=num_features, lw=.5, color='k')
        ax.spines['bottom'].set_position('zero')
        ax.spines['bottom'].set_visible(False)
        ax_cleanup(ax)

        # display feature names on top
        heights = list(np.linspace(1.05, .55, 4)) * 8
        for p, feat in enumerate(feature_names):
            ax.text(xs[p],
                    heights[p],
                    feat,
                    fontsize=4,
                    ha='center',
                    transform=ax.transData)
            ax.axvline(x=xs[p], ymin=.4, ymax=heights[p], lw=.2, color='.5')

    # title
    text = '%d strains,' %E.num_strains if not EXCLUDE_SS \
            else '%d strains (no AKR, CAST, CZECH),' %(E.num_strains-3)
    level_ = 'mice' if level == 'mouse' else level
    fig_title = 'Principal Component loadings, %d components\n' %num_components \
                + '%s %d %s, 24H avg data\n%s days:%dto%d' %(
                text, num_items, level_, E.use_days, E.daysToUse[0], E.daysToUse[-1])
    if BY_STRAIN:
        fig_title = 'Principal Component loadings, %d components\n' %num_components \
                    + '%s %d %s, 24H avg data\ngroup%d: %s\n%s days:%dto%d' %(
                    text, num_items, level_, strain, E.strain_names[strain],
                    E.use_days, E.daysToUse[0], E.daysToUse[-1])
    subplot_labels(E, fig, xlabel='', ylabel='Loading', fig_title=fig_title)
    plt.subplots_adjust(hspace=.4, wspace=.3)
    save_plot(fname, fig, ADD_SOURCE=ADD_SOURCE)