Beispiel #1
0
def qualitative_times(
    df,
    ax=None,
    x="relative_date",
    y="weight",
    unit="Animal_id",
    condition="treatment",
    err_style="unit_traces",
    order=None,
    bp_style=True,
    save_as='',
    legend_title=False,
    palette=QUALITATIVE_COLORSET,
    renames={},
    model='',
    print_model=False,
    print_anova=False,
    anova_type=3,
    groups=None,
    ci=95,
):
    """Plot a timecourse based on qualitative times (e.g. sessions).
	"""

    if bp_style:
        plt.style.use(u'seaborn-darkgrid')
        plt.style.use('ggplot')

    if renames:
        for key in renames:
            for subkey in renames[key]:
                df.loc[df[key] == subkey, key] = renames[key][subkey]

    ax = sns.pointplot(
        x=x,
        y=y,
        units=unit,
        data=df,
        hue=condition,
        dodge=True,
        palette=sns.color_palette(palette),
        order=order,
        ax=ax,
        ci=ci,
    )

    ax.set_ylabel(y)

    if not legend_title:
        legend_title = ax.legend().set_title('')

    if save_as:
        plt.savefig(path.abspath(path.expanduser(save_as)),
                    bbox_inches='tight')
Beispiel #2
0
def plot_roi_per_session(
    df,
    x='Session',
    y='Mean t-Statistic',
    condition='treatment',
    unit='subject',
    ci=90,
    palette=["#56B4E9", "#E69F00"],
    dodge=True,
    order=[],
    feature_map=True,
    roi_left=0.02,
    roi_bottom=0.74,
    roi_width=0.3,
    roi_height=0.2,
    roi_anat="/usr/share/mouse-brain-atlases/dsurqec_40micron_masked.nii",
    roi_threshold=None,
    cut_coords=None,
    samri_style=True,
    renames=[],
    save_as='',
    ax=None,
    fig=None,
):
    """Plot a ROI t-values over the session timecourse
	"""

    if samri_style:
        plt.style.use(u'seaborn-darkgrid')
        plt.style.use('ggplot')

    try:
        df = path.abspath(path.expanduser(df))
    except AttributeError:
        pass

    # definitions for the axes
    height = rcParams['figure.subplot.top']
    bottom = rcParams['figure.subplot.bottom']
    left = rcParams['figure.subplot.left']
    width = rcParams['figure.subplot.right']

    session_coordinates = [left, bottom, width, height]

    roi_coordinates = [
        left + roi_left, bottom + roi_bottom, roi_width, roi_height
    ]

    if not fig:
        fig = plt.figure(1)

    if renames:
        for key in renames:
            for subkey in renames[key]:
                df.loc[df[key] == subkey, key] = renames[key][subkey]

    if not ax:
        ax1 = plt.axes(session_coordinates)
    else:
        ax1 = ax
    ax = sns.pointplot(
        x=x,
        y=y,
        units=unit,
        data=df,
        hue=condition,
        dodge=dodge,
        palette=sns.color_palette(palette),
        order=order,
        ax=ax1,
        ci=ci,
    )
    ax.set_ylabel(y)

    if isinstance(feature_map, str):
        ax2 = plt.axes(roi_coordinates)
        if roi_threshold and cut_coords:
            maps.stat(
                feature,
                cut_coords=cut_coords,
                template=roi_anat,
                annotate=False,
                scale=0.3,
                show_plot=False,
                interpolation=None,
                threshold=roi_threshold,
                draw_colorbar=False,
                ax=ax2,
            )
        else:
            maps.atlas_label(
                feature_map,
                scale=0.3,
                color="#E69F00",
                ax=ax2,
                annotate=False,
                alpha=0.8,
            )
    elif feature_map:
        try:
            features = df['feature'].unique()
        except KeyError:
            pass
        else:
            if len(features) > 1:
                print(
                    'WARNING: The features list contains more than one feature. We will highlight the first one in the list. This may be incorrect.'
                )
            feature = features[0]
            ax2 = plt.axes(roi_coordinates)
            if path.isfile(feature):
                if roi_threshold and cut_coords:
                    maps.stat(
                        stat_maps=feature,
                        cut_coords=cut_coords,
                        template=roi_anat,
                        annotate=False,
                        scale=0.3,
                        show_plot=False,
                        interpolation=None,
                        threshold=roi_threshold,
                        draw_colorbar=False,
                        ax=ax2,
                    )
                else:
                    maps.atlas_label(
                        feature,
                        scale=0.3,
                        color="#E69F00",
                        ax=ax2,
                        annotate=False,
                        alpha=0.8,
                    )
            else:
                atlas = df['atlas'].unique()[0]
                mapping = df['mapping'].unique()[0]
                if isinstance(feature, str):
                    feature = [feature]
                maps.atlas_label(
                    atlas,
                    scale=0.3,
                    color="#E69F00",
                    ax=ax2,
                    mapping=mapping,
                    label_names=feature,
                    alpha=0.8,
                    annotate=False,
                )

    if save_as:
        plt.savefig(path.abspath(path.expanduser(save_as)),
                    bbox_inches='tight')

    return fig, ax
Beispiel #3
0
               var_name='Peak sunlight hours', value_name='Month')

sun2.head()
sun2 = pd.melt(sunlight, id_vars=['City', 'Country or US State'],
               value_vars=months,
               var_name='Month', value_name='Peak sunlight hours')

sun2.head()
australia_rows = sun2['Country or US State'] == 'Australia'
sun2oz = sun2.loc[australia_rows]
import seaborn.apionly as sns
get_ipython().magic('pinfo sns.pointplot')
sun2['Month'] = pd.to_datetime(sun2['Month'])
sns.barplot(data=sun2, x='Month', y='Peak sunlight hours', hue='City')
sns.barplot(data=sun2oz, x='Month', y='Peak sunlight hours',hue='City')
sns.pointplot(data=sun2oz, x='Month', y='Peak sunlight hours',hue='City')
from bokeh import plotting
TOOLS="hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,tap,save,box_select,poly_select,lasso_select,"
plot = plotting.figure(tools=TOOLS)
get_ipython().magic('pinfo plot.scatter')
get_ipython().magic('pinfo plot.line')
plt.color('C1')
get_ipython().magic('pinfo plt.cm.colors')
plt.cm.colors('C0')
plt.cm.colors.to_hex('C0')
for i, (city, data) in enumerate(sun2oz.groupby('City')):
    c = plt.cm.colors.to_hex(f'C{i}')
    plot.line(source=data, x='Month', y='Peak sunlight hours',
              color=c)

plotting.output_file('psh.html')
    _ = pm.traceplot(
        traces_signoise[-1000:],
        figsize=(12, len(traces_signoise.varnames) * 1.5),
        lines={k: v["mean"] for k, v in pm.df_summary(traces_signoise[-1000:]).iterrows()},
    )
    plt.show()

    ## fancy plot stuff
    outlier_melt = pd.melt(
        pd.DataFrame(traces_signoise["is_outlier", -1000:], columns=["[{}]".format(int(d)) for d in dfhoggs.index]),
        var_name="datapoint_id",
        value_name="is_outlier",
    )
    ax0 = sns.pointplot(
        y="datapoint_id", x="is_outlier", data=outlier_melt, kind="point", join=False, ci=None, size=4, aspect=2
    )

    _ = ax0.vlines([0, 1], 0, 19, ["b", "r"], "--")

    _ = ax0.set_xlim((-0.1, 1.1))
    _ = ax0.set_xticks(np.arange(0, 1.1, 0.1))
    _ = ax0.set_xticklabels(["{:.0%}".format(t) for t in np.arange(0, 1.1, 0.1)])

    _ = ax0.yaxis.grid(True, linestyle="-", which="major", color="w", alpha=0.4)
    _ = ax0.set_title("Prop. of the trace where datapoint is an outlier")
    _ = ax0.set_xlabel("Prop. of the trace where is_outlier == 1")
    plt.show()

    cutoff = 5
    dfhoggs["outlier"] = np.percentile(traces_signoise[-1000:]["is_outlier"], cutoff, axis=0)
Beispiel #5
0
def plot_roi_per_session(subjectdf,
                         voxeldf,
                         legend_loc="best",
                         figure="per-participant",
                         tabref="tab",
                         xy_label=[],
                         color="#E69F00",
                         save_as=False):
    """Plot a ROI t-values over the session timecourse

	figure : {"per-participant", "per-voxel", "both"}
	At what level to resolve the t-values. Per-participant compares participant means, per-voxel compares all voxel values, both creates two plots covering the aforementioned cases.

	"""

    names_for_plotting = {
        "ofM": u"naïve",
        "ofM_aF": "acute",
        "ofM_cF1": "chronic (2w)",
        "ofM_cF2": "chronic (4w)",
        "ofM_pF": "post"
    }
    voxeldf = voxeldf.replace({"session": names_for_plotting})
    subjectdf = subjectdf.replace({"session": names_for_plotting})

    if figure == "per-voxel":
        ax = sns.pointplot(x="session",
                           y="t",
                           hue="subject",
                           data=voxeldf,
                           ci=90,
                           dodge=True,
                           jitter=True,
                           legend_out=False,
                           units="voxel")
        if xy_label:
            ax.set(xlabel=xy_label[0], ylabel=xy_label[1])
    elif figure == "per-participant":
        ax = sns.pointplot(x="session",
                           y="t",
                           data=subjectdf,
                           ci=68.3,
                           dodge=True,
                           jitter=True,
                           legend_out=False,
                           units="subject",
                           color=color)
        if xy_label:
            ax.set(xlabel=xy_label[0], ylabel=xy_label[1])
    elif figure == "both":
        f, (ax1, ax2) = plt.subplots(1, 2)
        ax1 = sns.pointplot(x="session",
                            y="t",
                            hue="subject",
                            data=voxeldf,
                            ci=68.3,
                            dodge=True,
                            jitter=True,
                            legend_out=False,
                            units="subject",
                            ax=ax1)
        ax2 = sns.pointplot(x="session",
                            y="t",
                            data=subjectdf,
                            ci=68.3,
                            dodge=True,
                            jitter=True,
                            legend_out=False,
                            units="subject",
                            ax=ax2,
                            color=color)
        if xy_label:
            ax1.set(xlabel=xy_label[0], ylabel=xy_label[1])
            ax2.set(xlabel=xy_label[0], ylabel=xy_label[1])

    if (save_as):
        plt.savefig(path.abspath(path.expanduser(save_as)))
Beispiel #6
0
def analytic_pattern_per_session(
    substitutions,
    analytic_pattern,
    legend_loc="best",
    t_file_template="~/ni_data/ofM.dr/l1/{l1_dir}/sub-{subject}/ses-{session}/sub-{subject}_ses-{session}_trial-{scan}_tstat.nii.gz",
    roi_mask_normalize="",
    figure="per-participant",
    tabref="tab",
    xy_label=[],
    obfuscate=False,
    color="#E69F00",
    saveas=False,
):
    """Plot a ROI t-values over the session timecourse

	analytic_pattern : str
	Path to the analytic pattern by which to multiply the per-participant per-session t-statistic maps.

		roi_mask_normalize : str
	Path to a ROI mask by the mean of whose t-values to normalite the t-values in roi_mask.
	"""

    if isinstance(analytic_pattern, str):
        analytic_pattern = path.abspath(path.expanduser(analytic_pattern))
    analytic_pattern = nib.load(analytic_pattern)
    pattern_data = analytic_pattern.get_data()

    if figure == "per-participant":
        voxels = False
    else:
        voxels = True

    n_jobs = mp.cpu_count() - 2
    roi_data = Parallel(n_jobs=n_jobs, verbose=0, backend="threading")(map(
        delayed(add_pattern_data),
        substitutions,
        [t_file_template] * len(substitutions),
        [pattern_data] * len(substitutions),
        [voxels] * len(substitutions),
    ))
    subject_dfs, voxel_dfs = zip(*roi_data)
    subjectdf = pd.concat(subject_dfs)

    return subjectdf

    if obfuscate:
        obf_session = {
            "ofM": "_pre",
            "ofM_aF": "t1",
            "ofM_cF1": "t2",
            "ofM_cF2": "t3",
            "ofM_pF": "post"
        }
        subjectdf = subjectdf.replace({"session": obf_session})
        subjectdf.to_csv("~/MixedLM_data.csv")

    model = smf.mixedlm("t ~ session", subjectdf, groups=subjectdf["subject"])
    fit = model.fit()
    report = fit.summary()

    # create a restriction for every regressor - except intercept (first) and random effects (last)
    omnibus_tests = np.eye(len(fit.params))[1:-1]
    anova = fit.f_test(omnibus_tests)

    names_for_plotting = {
        "ofM": u"naïve",
        "ofM_aF": "acute",
        "ofM_cF1": "chronic (2w)",
        "ofM_cF2": "chronic (4w)",
        "ofM_pF": "post"
    }
    if voxels:
        voxeldf = pd.concat(voxel_dfs)
        voxeldf = voxeldf.replace({"session": names_for_plotting})
    subjectdf = subjectdf.replace({"session": names_for_plotting})

    ax = sns.pointplot(x="session",
                       y="t",
                       data=subjectdf,
                       ci=68.3,
                       dodge=True,
                       jitter=True,
                       legend_out=False,
                       units="subject",
                       color=color)
    if xy_label:
        ax.set(xlabel=xy_label[0], ylabel=xy_label[1])

    if (saveas):
        plt.savefig(saveas)

    return fit, anova
Beispiel #7
0
def forced_swim_timecourse(
    df,
    bp_style=True,
    colorset=QUALITATIVE_COLORSET,
    datacolumn_label="Immobility Ratio",
    legend_loc="best",
    plotstyle="tsplot",
    rename_treatments={},
    time_label="interval [1 min]",
    save_as=False,
):
    """Plot timecourse of forced swim measurements.

	Parameters
	----------

	df : {pandas.Dataframe, string}
	Pandas Dataframe containing the experimental data, or path pointing to a csv containing such data.

	bp_style : bool, optional
	Whether to apply the default behaviopy style.

	datacolumn_label : string, optional
	A column name from df, the values in which column give the data to plot.

	legend_loc : string, optional
	Where to place the legend on the figure.

	plotstyle : {"pointplot", "tsplot"}
	Dictionary with strings as keys and values used to map treatment names onto new stings.

	rename_treatments : dict, optional
	Dictionary with strings as keys and values used to map treatment names onto new stings.

	time_label : dict, optional
	A column name from df, the values in which column give the time pointd of the data.
	"""

    try:
        if isinstance(df, basestring):
            df = path.abspath(path.expanduser(df))
            df = pd.read_csv(df)
    except NameError:
        if isinstance(df, str):
            df = path.abspath(path.expanduser(df))
            df = pd.read_csv(df)

    for key in rename_treatments:
        df.loc[df["Treatment"] == key, "Treatment"] = rename_treatments[key]
    df = control_first_reordering(df, "Treatment")
    if bp_style:
        sns.set_style("white", {'legend.frameon': True})
        plt.style.use(u'seaborn-darkgrid')
        plt.style.use(u'ggplot')
    if plotstyle == "tsplot":
        myplot = sns.tsplot(time=time_label,
                            value=datacolumn_label,
                            condition="Treatment",
                            unit="Identifier",
                            data=df,
                            err_style="unit_traces",
                            color=sns.color_palette(colorset))
        myplot.set_xticks(list(set(df[time_label])))
    elif plotstyle == "pointplot":
        sns.pointplot(x=time_label,
                      y=datacolumn_label,
                      hue="Treatment",
                      data=df,
                      palette=sns.color_palette(colorset),
                      legend_out=False,
                      dodge=0.1)
    plt.legend(loc=legend_loc, frameon=True)

    if save_as:
        plt.savefig(path.abspath(path.expanduser(save_as)),
                    bbox_inches='tight')
Beispiel #8
0
def roi_per_session(
    substitutions,
    roi_mask,
    legend_loc="best",
    t_file_template="~/ni_data/ofM.dr/l1/{l1_dir}/sub-{subject}/ses-{session}/sub-{subject}_ses-{session}_trial-{scan}_tstat.nii.gz",
    roi_mask_normalize="",
    figure="per-participant",
    tabref="tab",
    xy_label=[],
    obfuscate=False,
    color="#E69F00",
):
    """Plot a ROI t-values over the session timecourse

	roi_mask : str
	Path to the ROI mask for which to select the t-values.

	figure : {"per-participant", "per-voxel", "both"}
	At what level to resolve the t-values. Per-participant compares participant means, per-voxel compares all voxel values, both creates two plots covering the aforementioned cases.

	roi_mask_normalize : str
	Path to a ROI mask by the mean of whose t-values to normalite the t-values in roi_mask.
	"""

    if isinstance(roi_mask, str):
        roi_mask = path.abspath(path.expanduser(roi_mask))
    masker = NiftiMasker(mask_img=roi_mask)

    n_jobs = mp.cpu_count() - 2
    roi_data = Parallel(n_jobs=n_jobs, verbose=0, backend="threading")(map(
        delayed(add_roi_data),
        [t_file_template] * len(substitutions),
        [masker] * len(substitutions),
        substitutions,
    ))
    subject_dfs, voxel_dfs = zip(*roi_data)
    subjectdf = pd.concat(subject_dfs)
    voxeldf = pd.concat(voxel_dfs)
    if roi_mask_normalize:
        figure = "per-participant"
        if isinstance(roi_mask_normalize, str):
            mask_normalize = path.abspath(path.expanduser(roi_mask_normalize))
        masker_normalize = NiftiMasker(mask_img=mask_normalize)
        roi_data = Parallel(n_jobs=n_jobs, verbose=0, backend="threading")(map(
            delayed(add_roi_data),
            [t_file_template] * len(substitutions),
            [masker_normalize] * len(substitutions),
            substitutions,
        ))
        subject_dfs_normalize, _ = zip(*roi_data)
        subjectdf_normalize = pd.concat(subject_dfs_normalize)

        subjectdf['t'] = subjectdf['t'] / subjectdf_normalize['t']
        #this is a nasty hack to mitigate +/-inf values appearing if the normalization ROI mean is close to 0
        subjectdf_ = deepcopy(subjectdf)
        subjectdf_ = subjectdf_.replace([np.inf, -np.inf],
                                        np.nan).dropna(subset=["t"], how="all")
        subjectdf = subjectdf.replace([-np.inf],
                                      subjectdf_[['t']].min(axis=0)[0])
        subjectdf = subjectdf.replace([np.inf],
                                      subjectdf_[['t']].max(axis=0)[0])

    if obfuscate:
        obf_session = {
            "ofM": "_pre",
            "ofM_aF": "t1",
            "ofM_cF1": "t2",
            "ofM_cF2": "t3",
            "ofM_pF": "post"
        }
        subjectdf = subjectdf.replace({"session": obf_session})
        subjectdf.to_csv("~/MixedLM_data.csv")

    model = smf.mixedlm("t ~ session", subjectdf, groups=subjectdf["subject"])
    fit = model.fit()
    report = fit.summary()

    # create a restriction for every regressor - except intercept (first) and random effects (last)
    omnibus_tests = np.eye(len(fit.params))[1:-1]
    anova = fit.f_test(omnibus_tests)

    names_for_plotting = {
        "ofM": u"naïve",
        "ofM_aF": "acute",
        "ofM_cF1": "chronic (2w)",
        "ofM_cF2": "chronic (4w)",
        "ofM_pF": "post"
    }
    voxeldf = voxeldf.replace({"session": names_for_plotting})
    subjectdf = subjectdf.replace({"session": names_for_plotting})

    if figure == "per-voxel":
        ax = sns.pointplot(x="session",
                           y="t",
                           hue="subject",
                           data=voxeldf,
                           ci=90,
                           dodge=True,
                           jitter=True,
                           legend_out=False,
                           units="voxel")
        if xy_label:
            ax.set(xlabel=xy_label[0], ylabel=xy_label[1])
    elif figure == "per-participant":
        ax = sns.pointplot(x="session",
                           y="t",
                           data=subjectdf,
                           ci=68.3,
                           dodge=True,
                           jitter=True,
                           legend_out=False,
                           units="subject",
                           color=color)
        if xy_label:
            ax.set(xlabel=xy_label[0], ylabel=xy_label[1])
    elif figure == "both":
        f, (ax1, ax2) = plt.subplots(1, 2)
        ax1 = sns.pointplot(x="session",
                            y="t",
                            hue="subject",
                            data=voxeldf,
                            ci=68.3,
                            dodge=True,
                            jitter=True,
                            legend_out=False,
                            units="subject",
                            ax=ax1)
        ax2 = sns.pointplot(x="session",
                            y="t",
                            data=subjectdf,
                            ci=68.3,
                            dodge=True,
                            jitter=True,
                            legend_out=False,
                            units="subject",
                            ax=ax2,
                            color=color)
        if xy_label:
            ax1.set(xlabel=xy_label[0], ylabel=xy_label[1])
            ax2.set(xlabel=xy_label[0], ylabel=xy_label[1])

    return fit, anova
raw_half = 0.5 * (np.fliplr(raw_diff[:, :5]) + raw_diff[:, -5:])
fits_half = 0.5 * (np.fliplr(fits_diff[:, :100]) + fits_diff[:, -100:])

# %% Plot difference in two conditions
rcParams['font.sans-serif'] = "Arial"

c3 = '#332288'
ms = 5
plt.figure(figsize=(8.7 / 2.54, 2.5))
angles = 20 * [1.25, 2.5, 5, 10, 20]
angles += list(np.arange(1.25, 30, 1.25))
angles += 20 * [30]
effect = list(raw_half.ravel()) + 23 * [None] + list(raw_half.mean(1))
data = DataFrame(data={'angles': angles, 'effect': effect})
sns.swarmplot('angles', 'effect', data=data, palette=[c3], marker='^', size=3)
sns.pointplot('angles', 'effect', data=data, color=c3, join=False, markers ='^', scale=.1, capsize=.6, errwidth=1)
plt.plot([0, 1, 3, 7, 15], raw_half.mean(0), marker='^', mfc='w', mec=c3,
         markersize=ms, lw=0, zorder=100)
plt.plot([23], raw_half.mean(0).mean(0), marker='^', mfc='w', mec=c3,
         markersize=ms, lw=0, zorder=100)
plt.xticks([0, 1, 3, 7, 15, 23], [1.25, 2.5, 5, 10, 20, 25])
plt.gca().set_xticklabels(xlabels + ['Mean'])
plt.xlabel(u'Auditory Separation (°)')
plt.ylabel('Performance Improvement \n (% correct)')

plt.xlim([-1, 25])
plt.tight_layout()
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
plt.subplots_adjust(left=0.175, right=1, top=0.95, bottom=0.2)
plt.savefig(path + 'data_diff.pdf', dpi=600)
Beispiel #10
0
meltedpop = pd.melt(pop, id_vars=['Country Name', 'Country Code'],
                    value_vars=all_years,
                    value_name='population',
                    var_name='year')
                    
meltedpop.head()
china_or_india_rows = ((meltedpop['Country Name'] == 'China') |
                       (meltedpop['Country Name'] == 'India'))
                       
china_or_india_rows.head()
popcnin = meltedpop.loc[china_or_india_rows]
popcnin.shape
popcnin.head()
import seaborn.apionly as sns
get_ipython().magic('pinfo sns.pointplot')
sns.pointplot(data=popcnin, x='year', y='population')
sns.pointplot(data=popcnin, x='year', y='population', hue='Country Name')
_32
45
_34
_32.figure
_32.figure.legends
_32.legend_
get_ipython().magic('pinfo _32.legend_.draggable')
_32.legend_.draggable(True)
_32.get_xticklabels()
ipython_green = np.array([1, 77, 1]) / 255
import matplotlib.pyplot as plt
plt.cm.colors.rgb2hex(ipython_green)
get_ipython().magic('pinfo plt.cm.colors.rgb2hex')
type(meltedpop['Country Name'])