from pathlib import Path
from rich import print
from pyinspect._colors import mocassin, orange

print(f'[{mocassin}]Generating [{orange}]figures[/{orange}][{mocassin}]...\n')

metadata = load_yaml('figures/composed_figures.yml')
figs_folder = Path('figures')

for figname, figs in metadata.items():
    if len(figs) < 2:
        ncols, nros = 1, 1
    elif len(figs) < 3:
        ncols, nrows = 2, 1
    elif len(figs) < 5:
        ncols, nrows = 2, 2
    elif len(figs) < 7:
        ncols, nrows = 3, 2

    f, axarr = plt.subplots(ncols=ncols, nrows=nrows, figsize=(18, 13))
    f.suptitle(figname)
    for ax in axarr.flatten():
        ax.axis('off')

    for ax, (title, fig) in zip(axarr.flatten(), figs):
        ax.imshow(mpimg.imread(figs_folder / fig))
        ax.set(title=title)

    f.tight_layout()
    save_figure(f, figs_folder / figname, verbose=False)
Пример #2
0
            xticks=[],
            xlim=[x.min(), x.max()],
        )
        clean_axes(f)

        axes, titles = tag_axes, ('stim', 'start', 'run', 'shelter', 'stop')
        for ax, ttl in zip(tag_axes, titles):
            ax.set(title=ttl)
            ax.set(xticks=[0, 30, 60],
                   xticklabels=[-1, 0, 1],
                   xlabel='s',
                   yticks=[],
                   ylim=axarr[1, 0].get_ylim())

            ax.spines['left'].set_visible(False)
            ax.axvline(30, color=[.2, .2, .2], lw=3)

        save_figure(
            f,
            fld /
            f'{sess["mouse"]}_{sess["date"]}__{roi}_active_{ACTIVE["active"][-1]}',
            verbose=False)
        # plt.close(f)
        break
    break
pd.DataFrame(ACTIVE).to_hdf('ACTIVE_ROIS.h5', key='hdf')

# %%

# %%
Пример #3
0
        #     ax.plot(_time, signals[n][stim-frames_pre:at_shelter], c=signal_color, alpha=.3)

    # Clean up figure
    ttls = ['Shelter distance', 'speed', 'angular velocity']
    for n, ax in enumerate(axarr):
        ax.axvline(0, lw=2, ls='--', color=[.4, .4, .4])

        if n < len(ttls):
            ttl = ttls[n]
        else:
            ttl = 'signal'
        ax.set(ylabel=ttl)

    axarr[-1].set(xlabel='frames')
    clean_axes(f)
    save_figure(f, str(save_fld / f'{mouse} {sess} behav traces'))

    # break

# %%
"""
        Plot each trials baseline as a scatter plot + scatter of PCA
"""


def fit_lin_regrs(R, X, S):
    """
        predicts a ROI's mean baseline activity at each trial (R)
        with the mean X position (X) and mean speed (S) at each trial
        with a linear regression + OLS
    """
Пример #4
0
        overal_accuracies[name] = accuracy

    # Clean axes
    axarr[s].legend()
    s += 1

    # break
#

axarr[-1].axis('off')

clean_axes(f)
set_figure_subplots_aspect(hspace=.6, top=.9, wspace=.25)
save_figure(
    f,
    str(save_fld /
        f'Shelter representation {"stationary" if KEEP_ONLY_STATIONARY else ""} {PREDICT}'
        ))

# %%
# Fit SVM to individual ROIs
f, axarr = plt.subplots(ncols=3, nrows=3, figsize=(16, 16))
axarr = axarr.flatten()
s = 0
f.suptitle(f'individual ROI SVM accuracy (shelter detection)')

for mouse, sess, name in tqdm(mouse_sessions):
    if pcas[name] is None: continue

    # Prep data
    N = 500
Пример #5
0
            scatter_trial_baseline(axarr[n, 1], WHOLE_TRACE_DFF[:n_frames_pre], count, len(tags))

            # Plot signals
            axarr[n, 0].plot(RAW, color='skyblue', lw=2, alpha=.6)
            axarr[n, 1].plot(WHOLE_TRACE_DFF, color='salmon', lw=2, alpha=.6)
            axarr[n, 2].plot(DORIC_CHUNK_DFF, color='seagreen', lw=2, alpha=.6)
            axarr[n, 3].plot(BASELINE_DFF, color='magenta', lw=2, alpha=.6)


        # Mark tag onset
        for ax in axarr[n, :]:
            ax.axvline(n_frames_pre, lw=2, color='r')

        # make titles, axis labels etc
        ts = [f' RAW Tag: {tag_type} ', ' WHOLE SESS DFF', ' DORIC CHUNK DFF', ' PRE STIM BASELINE DFF']
        ttls = [rid + t for t in ts]
        ylbs = ['raw', 'dff', 'dff', 'dff']

        for a, ax in enumerate(axarr[n, :]):
            ax.set(title=ttls[a], ylabel=ylbs[a])
    
    # Set X labels
    for ax in axarr[-1, :]:
        ax.set(xlabel='frames')
    
    # clean up and save
    clean_axes(f)
    f.tight_layout()
    save_figure(f, fld / (sessname))

Пример #6
0
                                 sig[stim_frame],
                                 color='k',
                                 s=100,
                                 zorder=99)

        axarr[n].legend()
        axarr[n].set(title=rid, ylabel='raw signal')

        # break

    # cleanup and save
    axarr[-1].set(xlabel='frames')

    clean_axes(f)
    f.tight_layout()
    save_figure(f, fld / (sessname + '_doric_chunks_baselines'))
    break

# %%
"""
    Plot each ROI's signal with the chunk smoothed by a rolling mean filter
"""
window = 600
for mouse, sess, sessname in track(mouse_sessions,
                                   description='plotting smoothed chunks'):
    if not DO['plot_chunks_smoothed']: break

    # Get data
    tracking, ang_vel, speed, shelter_distance, dffs, signals, nrois, is_rec, roi_ids = \
                        get_mouse_session_data(mouse, sess, sessions, hanning_window=hanning)
    tags = get_tags_by(mouse=mouse,
    # fix figure
    for n, ax in enumerate(axarr[2:]):
        if n >= len(signals):
            ax.axis('off')
        else:
            make_signal_ax(ax, f'ROI {n}', start, end)

    axarr[0].set(title='tracking', xlabel='X (px)', ylabel='Y (px)')
    axarr[0].legend(handles=legend_elements)
    axarr[1].set(title='tracking', xlabel='speed (cm/s)', ylabel='X (px) ')

    clean_axes(f)
    set_figure_subplots_aspect(right=.85, top=.9, wspace=.4)
    save_figure(
        f,
        str(fld /
            f'{sessname}_tag_{tag_type[-1]}_{"".join(event_type)}_mean_{HIGHLIGHT_MEAN}'
            ),
        svg=False)

    # break

with open(str(fld / '00_info.txt'), 'w') as out:
    out.write('INFO')
    out.write(f'''IMAGES GENERATED WITH PARAMS:
                        fps = {fps }
                        n_sec_pre = {n_sec_pre}
                        n_sec_post = {n_sec_post}
                        tag_type = {tag_type}
                        event_type = {event_type}
                        NORMALIZE = {NORMALIZE}
                        FILTER = {FILTER}
Пример #8
0
                keys = []

                plot_model(axes['escape_peak_speed'][4],
                           ols_behav_traces_escape_peak_speed, *args)
                plot_model(axes['escape_onset'][4],
                           ols_behav_traces_escape_onset, *args)
                plot_model(axes['stim_onset'][4], ols_behav_traces_escape_stim,
                           *args)
                plot_model(axes['shelter_arrival'][4],
                           ols_behav_traces_shelter_arrival, *args)
                plot_model(axes['homing'][4], ols_behav_traces_homing_onset,
                           *args)
                plot_model(axes['homing_peak_speed'][4],
                           ols_behav_traces_hominst_peak_speed, *args)

            # Refine figure
            f.suptitle(
                f"{mouse} - {sess} - {roi_ids[n]} - {tot_trials} trials")
            for ax in f.axes:
                adjust_ticks(ax, frames_pre, frames_post, fps, every=1)
                ax.set(xlim=[xmin, xmax])

            # Save and close
            save_figure(f, f_path, verbose=True)
            if not DEBUG: plt.close(f)

            if DEBUG: break
        if DEBUG: break
    if DEBUG: break
plt.show()
Пример #9
0
                      xlim=[-600, 600])
    meanspeedsax.set(title='Bout mean ang vel and speed',
                     xlabel='speed (px/s)',
                     ylabel='angular velocity (deg/s)',
                     ylim=[-100, 100],
                     xlim=[-600, 600])
    meanspeedsax.axhline(0, color='k', alpha=.5, lw=4, ls='--')
    meanspeedsax.axvline(0, color='k', alpha=.5, lw=4, ls='--')
    meanspeedsax.legend()

    framespeedsax.axhline(0, color='k', alpha=.5, lw=4, ls='--')
    framespeedsax.axvline(0, color='k', alpha=.5, lw=4, ls='--')
    framespeedsax.legend()

    speedsax.set(title='Avg bout speed distribution',
                 xlabel='speed',
                 ylabel='density')
    speedsax.legend()

    # Clean and save figure
    set_figure_subplots_aspect(wspace=.4, hspace=.4, top=.9, bottom=.15)
    clean_axes(f)
    f.suptitle(f'{mouse} summary.', fontsize=22)
    save_figure(
        f,
        os.path.join(output_fld,
                     f'{mouse}_highspeed_{high_speed_bouts}_bouts_summary'))
    # break

# %%
Пример #10
0
        ax.plot(X, speed, lw=3, color=salmon_dark, label='seed', alpha=.5)

        ax.plot(X,
                smoothed - smoothed[rec_off],
                lw=3,
                color=light_blue_dark,
                label='DFF',
                alpha=.5)

        ax.legend()
        ax.set(title=rid, xlabel='frames', ylabel='norm.speed and dff')

        clean_axes(f)
        f.tight_layout()
        save_figure(f, sfld / f'{rid}_dff_vs_speed')
    #     break
    # break

# %%
'''
    Make histogram of pearson correlations between filtered variables
'''

speed_th = .2
window = 30

pvals = [[], []]
speed_coeffs, dist_coffs, speed_dist_coeffs, pc_speed_coeffs = [[], []
                                                                ], [[], []
                                                                    ], [[], []
Пример #11
0
    axarr[0].axhline(liftoff_th, lw=6, color='k', alpha=.2, ls='--')
    axarr[0].axhline(0, lw=2, color='k', zorder=-1)
    axarr[0].legend()
    axarr[1].legend()

    axarr[0].set(ylabel='$W_{pc}$')

    axarr[1].set(ylabel='$\\frac{d W_{pc}}{dt}$',
                 **xkwargs,
                 xlabel='time (ms)',
                 xlim=[-2, 2])

    # save figure
    clean_axes(fig)
    save_figure(fig,
                str(save_fld / f"trial_plot_{trial['name']}"),
                verbose=False)

    if i > 0:
        plt.close(fig)

    # if i == 2:
    #         break

save_figure(main_fig, str(save_fld / f"all_trials"), verbose=False)
if len(step_start_frames) == len(data):
    data['step_start_frames'] = step_start_frames

# %%
from sklearn.decomposition import PCA
from einops import rearrange
Пример #12
0
        clean_axes(f)
        for row in range(3):
            maxy = np.max([ax.get_ylim()[1] for ax in axarr[row, :]])
            miny = np.min([ax.get_ylim()[0] for ax in axarr[row, :]])

            for n, ax in enumerate(axarr[row, :]):
                ax.set(ylim=[miny, maxy])
                if n > 0:
                    ax.spines['left'].set_visible(False)
                    ax.set(yticks=[])

                if row < 2:
                    ax.spines['bottom'].set_visible(False)

        save_figure(f,
                    fld / f'{sess["mouse"]}_{sess["date"]}__{roi}',
                    verbose=False)
        plt.close(f)
    del rois

    # break
    # break
# %%
# ''' Plot the mean trace aligned to each tag for each ROI and the first PCA component'''

# f, axarr = plt.subplots(ncols=5, nrows=2, figsize=(16, 9))

# for sessn, sess in enumerate(Sessions.fetch(as_dict=True)):
#     print(sess['mouse'], sess['date'])
#     data, rois = get_session_data(sess['mouse'], sess['date'], roi_data_type='raw')
#     if data is None: continue
Пример #13
0
    points = cont['coordinates']
    axarr[0].plot(*points.T, color=color, lw=2)
    axarr[1].plot(*points.T, color=color, lw=2, alpha=alpha)

    axarr[1].text(cont['CoM'][1],
                  cont['CoM'][0],
                  str(n),
                  color=txtcolor,
                  zorder=100)

axarr[2].imshow(1 - np.sum(new_masks, 2), cmap="gray_r")

axarr[0].set(title='A', xticks=[], yticks=[])
axarr[1].set(title=f'Masks - threhsold {mask_th}', xticks=[], yticks=[])

save_figure(f, str(fld / 'contours'))

# Save new masks and contours to file
np.save(str(fld / 'masks.npy'), new_masks)
pd.DataFrame(contours).to_hdf(str(fld / 'contours.h5'), key='hdf')

# %%

# ---------------------------------------------------------------------------- #
#                                EXTRACT TRACES                                #
# ---------------------------------------------------------------------------- #
n_frames = video.shape[0]
traces = np.zeros((n_frames, n_components))

for compn in track(range(n_components)):
    traces[:, compn] = get_component_trace_from_video(compn, masks, n_frames,