<jupyter_start><jupyter_text>Graphical view of param sweep results (Figure 3A-C, G)<jupyter_code>%matplotlib inline
from copy import deepcopy as copy
import json
import os

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from disp import set_font_size, set_n_x_ticks, set_n_y_ticks
from replay import analysis

cc = np.concatenate


HMAP_PARAMS = {
    'FR_NTRJ_MIN_BLOWUP': 1.5,  # Hz
    'FR_NTRJ_TRJ_BLOWUP_RATIO': .3,  # frac
    'MIN_EVTS_ONE_WAY_FRAC_CALC': 5,
    'MIN_EVTS_SPD_CALC': 5,
    'MIN_SPD_SPD_CALC': 10,  # m/s
}

SCT_SIZE = 150
W_SCALE_PLOT = .05/.0005  # T_M_PC/DT

def evt_frq_hmap(f_name, v_mins, v_maxs, x_ticks, c_ticks, hmap_params):
    """Make heatmaps of spontaneous replay event frequency given param sweep file."""
    
    df, rslts, header = analysis.make_df(f_name)
    params_varied = header['params_varied']
    assert len(params_varied) == 2
    
    n_trials = header['sweep_params']['N_TRIALS']
    dur = header['s_params']['schedule']['D_SMLN'] - header['a_params']['MIN_START']
    
    # get data into numpy arrays
    p_0 = np.array(df[params_varied[0]])
    p_1 = np.array(df[params_varied[1]])
    
    if params_varied[0].startswith('W_PC'):
        p_0 = p_0 * W_SCALE_PLOT
        
    if params_varied[1].startswith('W_PC'):
        p_1 = p_1 * W_SCALE_PLOT
    
    p_0_min = np.min(p_0)
    p_0_max = np.max(p_0)
    p_0_range = p_0_max - p_0_min
    
    p_1_min = np.min(p_1)
    p_1_max = np.max(p_1)
    p_1_range = p_1_max - p_1_min
    
    fr_trj = np.array(df['FR_TRJ'])
    fr_ntrj = np.array(df['FR_NTRJ'])
    
    print('Min fr_trj = ', np.nanmin(fr_trj), 'Hz')
    print('Max fr_trj = ', np.nanmax(fr_trj), 'Hz')
    print('Min fr_ntrj = ', np.nanmin(fr_ntrj), 'Hz')
    print('Max fr_ntrj = ', np.nanmax(fr_ntrj), 'Hz')
    
    evt_ct = np.array(df['EVT_CT'])
    evt_frq = evt_ct / dur
    
    print('Min evt_ct = ', np.min(evt_ct))
    print('Max evt_ct = ', np.max(evt_ct))
    print('Min evt_frq = ', np.min(evt_frq), 'Hz')
    print('Max evt_frq = ', np.max(evt_frq), 'Hz')
    
    one_way_ct = np.array(df['ONE_WAY_CT'])
    one_way_frq = one_way_ct / dur
    
    print('Min one_way_ct', np.min(one_way_ct))
    print('Max one_way_ct', np.max(one_way_ct))
    
    # make plots
    fig, axs = plt.subplots(3, 2, figsize=(12.5, 15), tight_layout=True)
    
    ## fr trj
    im_00 = axs[0, 0].scatter(p_0, p_1, SCT_SIZE, c=fr_trj, marker='s', vmin=v_mins['fr_trj'], vmax=v_maxs['fr_trj'])
    axs[0, 0].set_title('LTP-IE-tagged firing rates')
    cb_00 = fig.colorbar(im_00, ax=axs[0, 0])
    cb_00.set_label('Firing rate (Hz)', fontsize=16)

    ## fr ntrj
    im_01 = axs[0, 1].scatter(p_0, p_1, SCT_SIZE, c=fr_ntrj, marker='s', vmin=v_mins['fr_ntrj'], vmax=v_maxs['fr_ntrj'])
    axs[0, 1].set_title('Non-LTP-IE-tagged firing rates')
    cb_01 = fig.colorbar(im_01, ax=axs[0, 1])
    cb_01.set_label('Firing rate (Hz)', fontsize=16)
    
    ## evt frq
    im_10 = axs[1, 0].scatter(p_0, p_1, SCT_SIZE, c=evt_frq, marker='s', vmin=v_mins['evt_frq'], vmax=v_maxs['evt_frq'])
    axs[1, 0].set_title('Event frequency')
    cb_10 = fig.colorbar(im_10, ax=axs[1, 0])
    cb_10.set_label('Event frequency (Hz)', fontsize=16)
    
    ## replay zones
    ### plot event freq
    im_11 = axs[1, 1].scatter(
        p_0, p_1, SCT_SIZE, c=evt_frq, marker='s', vmin=v_mins['evt_frq'], vmax=v_maxs['evt_frq'], cmap='plasma', zorder=0)
    axs[1, 1].set_title('Replay regimes')
    cb_11 = fig.colorbar(im_11, ax=axs[1, 1], ticks=c_ticks)
    cb_11.set_label('Event frequency (Hz)', fontsize=16)
    
    ### set blowup vals to green
    mask = \
        (fr_ntrj >= hmap_params['FR_NTRJ_MIN_BLOWUP']) \
        | (fr_ntrj / fr_trj >= hmap_params['FR_NTRJ_TRJ_BLOWUP_RATIO'])
    axs[1, 1].scatter(
        p_0[mask], p_1[mask], SCT_SIZE+2, c='k', marker='s', zorder=2)
        
    c_tick_labels = [str(c_tick) for c_tick in c_ticks]
    c_tick_labels[-1] = '>{}'.format(c_ticks[-1])

    cb_11.ax.set_yticklabels(c_tick_labels)
    
    ## plot frequency of one-way events
    im_20 = axs[2, 0].scatter(p_0, p_1, SCT_SIZE, c=one_way_frq, marker='s', vmin=v_mins['evt_frq'], vmax=v_maxs['evt_frq'])
    axs[2, 0].set_title('One-way frequency')
    cb_20 = fig.colorbar(im_20, ax=axs[2, 0])
    cb_20.set_label('One-way frequency (Hz)', fontsize=16)
    
    ## plot fraction of one-way events
    ### put trial-wise total event counts into np array
    evt_cts_trials = np.transpose([np.array(df['EVT_CT_{}'.format(ctr)]) for ctr in range(n_trials)])
    ### put trial-wise one-way event counts into np array
    one_way_cts_trials = np.transpose([np.array(df['ONE_WAY_CT_{}'.format(ctr)]) for ctr in range(n_trials)])
    
    ### get trial-wise one-way count fractions
    #### set evt cts to nan if less than min evts to use for one-way frac calc
    evt_cts_trials[evt_cts_trials < hmap_params['MIN_EVTS_ONE_WAY_FRAC_CALC']]
    #### divide
    one_way_fracs_trials = one_way_cts_trials / evt_cts_trials
    #### take mean across trials
    one_way_fracs = np.nanmean(one_way_fracs_trials, axis=1)
    
    ## plot one-way event fractions
    im_21 = axs[2, 1].scatter(p_0, p_1, SCT_SIZE, c=one_way_fracs, vmin=0, vmax=1, marker='s', cmap='cool')
    axs[2, 1].set_title('Unidirectional replay')
    cb_21 = fig.colorbar(im_21, ax=axs[2, 1])
    cb_21.set_label('Fraction unidirectional', fontsize=16)
    
    ## set blowup vals to green
    mask = \
        (fr_ntrj >= hmap_params['FR_NTRJ_MIN_BLOWUP']) \
        | (fr_ntrj / fr_trj >= hmap_params['FR_NTRJ_TRJ_BLOWUP_RATIO'])
    axs[2, 1].scatter(
        p_0[mask], p_1[mask], SCT_SIZE+2, c='k', marker='s', zorder=2)
    
    for ax in axs.flatten():
        
        ax.set_xlim(p_0_min - .05*p_0_range, p_0_max + .05*p_0_range)
        ax.set_ylim(p_1_min - .05*p_1_range, p_1_max + .05*p_1_range)
        
        if x_ticks is not None:
            ax.set_xticks(x_ticks)
        
        ax.set_xlabel(params_varied[0])
        ax.set_ylabel(params_varied[1])
        
        ax.set_facecolor((.8, .8, .8))
        set_font_size(ax, 16)
        
    set_font_size(axs[2, 1], 20)
        
    for cb in [cb_00, cb_01, cb_10, cb_11]:
        
        set_font_size(cb.ax, 16)
    
    set_font_size(cb_21.ax, 20)
    
    return df, rslts, header<jupyter_output><empty_output><jupyter_text>Dynamics vs $\lambda^{PC,PC}$ and $w^{PC,PC}$ (Figure 3B)<jupyter_code>v_mins = {'fr_trj': 0, 'fr_ntrj': 0, 'evt_frq': 0}
v_maxs = {'fr_trj': 10, 'fr_ntrj': 10, 'evt_frq': 4}
x_ticks = [2, 2.5, 3, 3.5, 4]
c_ticks = [0, 1, 2, 3, 4]

dfs, rslts, header = evt_frq_hmap(
    'PARAM_SWEEP_RESULTS_0_W_PC_PC_L_PC_PC.txt',
    v_mins, v_maxs, x_ticks, c_ticks,
    HMAP_PARAMS)<jupyter_output>/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/fromnumeric.py:2920: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/_methods.py:85: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)<jupyter_text>Dynamics vs $w^{PC,PC}$ and $w^{PC,INH}$ (Figure 3A)<jupyter_code>v_mins = {'fr_trj': 0, 'fr_ntrj': 0, 'evt_frq': 0}
v_maxs = {'fr_trj': 10, 'fr_ntrj': 10, 'evt_frq': 4}
x_ticks = [0.0, .02, 0.04, 0.06]
c_ticks = [0, 1, 2, 3, 4]

dfs, rslts, header = evt_frq_hmap(
    'PARAM_SWEEP_RESULTS_1_W_PC_PC_W_PC_INH.txt',
    v_mins, v_maxs, x_ticks, c_ticks,
    HMAP_PARAMS)<jupyter_output>/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/fromnumeric.py:2920: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/_methods.py:85: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)<jupyter_text>Additional param sweep (not in manuscript)<jupyter_code>v_mins = {'fr_trj': 0, 'fr_ntrj': 0, 'evt_frq': 0}
v_maxs = {'fr_trj': 10, 'fr_ntrj': 10, 'evt_frq': 4}
x_ticks = [2, 2.5, 3, 3.5, 4]
c_ticks = [0, 1, 2, 3, 4]

dfs, rslts, header = evt_frq_hmap(
    'PARAM_SWEEP_RESULTS_2_W_PC_PC_SGM_MAX.txt',
    v_mins, v_maxs, x_ticks, c_ticks,
    HMAP_PARAMS)<jupyter_output>/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/fromnumeric.py:2920: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/_methods.py:85: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)<jupyter_text>Additional param sweep (not in manuscript)<jupyter_code>v_mins = {'fr_trj': 0, 'fr_ntrj': 0, 'evt_frq': 0}
v_maxs = {'fr_trj': 10, 'fr_ntrj': 10, 'evt_frq': 4}
x_ticks = [0.0, .02, 0.04, 0.06]
c_ticks = [0, 1, 2, 3, 4]

dfs, rslts, header = evt_frq_hmap(
    'PARAM_SWEEP_RESULTS_3_L_PC_PC_W_PC_INH.txt',
    v_mins, v_maxs, x_ticks, c_ticks,
    HMAP_PARAMS)<jupyter_output>/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/fromnumeric.py:2920: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/_methods.py:85: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)<jupyter_text>Dynamics vs $r^G$ and $\sigma_{max}$ (Figure 3C)<jupyter_code>v_mins = {'fr_trj': 0, 'fr_ntrj': 0, 'evt_frq': 0}
v_maxs = {'fr_trj': 10, 'fr_ntrj': 10, 'evt_frq': 4}
x_ticks = None  # [0.0, .0002, 0.0004, 0.0006]
c_ticks = [0, 1, 2, 3, 4]

dfs, rslts, header = evt_frq_hmap(
    'PARAM_SWEEP_RESULTS_4_R_G_SGM_MAX.txt',
    v_mins, v_maxs, x_ticks, c_ticks,
    HMAP_PARAMS)<jupyter_output>/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/fromnumeric.py:2920: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/_methods.py:85: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)<jupyter_text>Additional param sweep (not in manuscript)<jupyter_code>v_mins = {'fr_trj': 0, 'fr_ntrj': 0, 'evt_frq': 0}
v_maxs = {'fr_trj': 10, 'fr_ntrj': 10, 'evt_frq': 6}
x_ticks = None  # [0.0, .0002, 0.0004, 0.0006]
c_ticks = [0, 1, 2, 3, 4]

dfs, rslts, header = evt_frq_hmap(
    'PARAM_SWEEP_RESULTS_5_R_G_W_PC_INH.txt',
    v_mins, v_maxs, x_ticks, c_ticks,
    HMAP_PARAMS)<jupyter_output>/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/fromnumeric.py:2920: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/_methods.py:85: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)<jupyter_text>Additional param sweep (not in manuscript)<jupyter_code>v_mins = {'fr_trj': 0, 'fr_ntrj': 0, 'evt_frq': 0}
v_maxs = {'fr_trj': 10, 'fr_ntrj': 10, 'evt_frq': 6}
x_ticks = None  # [0.0, .0002, 0.0004, 0.0006]
c_ticks = [0, 1, 2, 3, 4]

dfs, rslts, header = evt_frq_hmap(
    'PARAM_SWEEP_RESULTS_6_R_G_W_PC_G.txt',
    v_mins, v_maxs, x_ticks, c_ticks,
    HMAP_PARAMS)<jupyter_output>/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/fromnumeric.py:2920: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/home/rkp/miniconda3/envs/sci/lib/python3.5/site-packages/numpy/core/_methods.py:85: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)<jupyter_text>Replay speed param sweep (Figure 3G)<jupyter_code>def plot_speed_hmap(f_name, v_min, v_max, fig_w=6.5, n_x_ticks=5):
    """Plot heatmap of triggered replay speeds."""
    # load dataframe
    df, rslts, header = analysis.make_df_ext(f_name)
    
    fig, ax = plt.subplots(1, 1, figsize=(fig_w, 5.5), tight_layout=True)
    mask = (df['EVT_CT'] >= HMAP_PARAMS['MIN_EVTS_SPD_CALC']) & (df['SPD_MIN'] >= HMAP_PARAMS['MIN_SPD_SPD_CALC'])
    
    df_valid = df[mask]
    
    param_0, param_1 = header['params_varied']
    p_0 = df_valid[param_0]
    p_1 = df_valid[param_1]
    
    if param_0.startswith('W_PC'):
        p_0 = p_0 * W_SCALE_PLOT
        
    if param_1.startswith('W_PC'):
        p_1 = p_1 * W_SCALE_PLOT
    
    im = ax.scatter(
        p_0, p_1, c=df_valid['SPD_MED'], vmin=v_min, vmax=v_max,
        s=SCT_SIZE, marker='s')
    
    cb = fig.colorbar(im, ax=ax)
    cb.set_label('Speed (m/s)')
    
    print('PARAMS:', header['m_params'])
    
    ax.set_xlim(p_0.min(), p_0.max())
    ax.set_ylim(p_1.min(), p_1.max())
    
    ax.set_facecolor((.8, .8, .8))
    
    set_n_x_ticks(ax, n_x_ticks)
    ax.set_xlabel(param_0)
    ax.set_ylabel(param_1)
    
    ax.set_title('Replay speed')
    
    set_font_size(ax, 20)
    set_font_size(cb.ax, 20)
plot_speed_hmap('PARAM_SWEEP_RESULTS_EXT_0_W_PC_PC_L_PC_PC.txt', v_min=None, v_max=None, fig_w=6.5)<jupyter_output>PARAMS: {'N_PC': 3000, 'E_L_INH': -0.06, 'R_G': 125, 'S_PC_G': 0, 'N_INH': 300, 'W_PC_INH': 0.0002, 'S_PC_PL': 0, 'T_M_PC': 0.05, 'E_L_PC': -0.068, 'E_I': -0.08, 'V_R_PC': -0.068, 'T_E': 0.002, 'T_M_INH': 0.005, 'L_PL': 0.15, 'T_I': 0.002, 'T_R_INH': 0.002, 'T_R_PC': 0.008, 'W_PC_PC': 0.026, 'W_MIN_PC_PC': 0.001, 'W_INH_PC': 0.003, 'W_PC_PL': 0.02, 'B_SGM': 1, 'V_TH_INH': -0.05, 'E_E': 0, 'V_R_INH': -0.06, 'L_PC_PC': 0.053, 'V_TH_PC': -0.036, 'W_PC_G': 0.008216, 'R_SGM': 10, 'SGM_MAX': 2, 'R_MAX': 20}<jupyter_text>Additional param sweep for replay speed (not in manuscript)<jupyter_code>plot_speed_hmap('PARAM_SWEEP_RESULTS_EXT_1_W_PC_PC_W_PC_INH.txt', v_min=10, v_max=None, fig_w=7, n_x_ticks=4)<jupyter_output>PARAMS: {'N_PC': 3000, 'E_L_INH': -0.06, 'S_PC_PL': 0, 'S_PC_G': 0, 'N_INH': 300, 'T_M_PC': 0.05, 'V_R_PC': -0.068, 'W_PC_INH': 0.0002, 'E_L_PC': -0.068, 'E_I': -0.08, 'V_TH_INH': -0.05, 'R_G': 125, 'T_M_INH': 0.005, 'T_E': 0.002, 'W_PC_PL': 0.02, 'L_PL': 0.15, 'T_I': 0.002, 'T_R_INH': 0.002, 'T_R_PC': 0.008, 'W_PC_PC': 0.026, 'W_INH_PC': 0.003, 'B_SGM': 1, 'W_MIN_PC_PC': 0.001, 'E_E': 0, 'V_R_INH': -0.06, 'L_PC_PC': 0.053, 'V_TH_PC': -0.036, 'W_PC_G': 0.008216, 'R_SGM': 10, 'SGM_MAX': 2, 'R_MAX': 20}
{'fraction_non_alphanumeric': 0.16445774547527434, 'fraction_numerical': 0.051874020236568334, 'mean_word_length': 3.9576121511833273, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '5845', 'license': 'MIT', 'n_tokens_mistral': 6965, 'n_tokens_neox': 6533, 'n_words': 1116, 'path': '3D_param_regimes_sweep_stats.ipynb', 'repo_name': 'rkp8000/seq_speak', 'size': '14034'}