)

sns.set(font_scale=0.4)
fig_dir = Path('~/duguidlab/visuomotor_control/figures/srf_grant')

if rates_not_rasters:
    firing_rates = exp.align_trials(
        ActionLabels.rewarded_push,
        Events.back_sensor_open,
        'spike_rate',
        duration=duration,
    )

    # IPN
    ses = exp[ses_ipn_gpi].name
    spike_rate.per_unit_spike_rate(firing_rates[ses_ipn_gpi][ipn], ci='sd')
    plt.suptitle(f'IPN - per-unit across-trials firing rate (aligned to push)')
    utils.save(fig_dir / f'IPN_unit_spike_rate_{duration}s_{ses}.png')

    # GPi
    spike_rate.per_unit_spike_rate(firing_rates[ses_ipn_gpi][gpi], ci='sd')
    plt.suptitle(f'GPi - per-unit across-trials firing rate (aligned to push)')
    utils.save(fig_dir / f'GPi_unit_spike_rate_{duration}s_{ses}.png')

    # MTh
    ses = exp[ses_m1_mth].name
    spike_rate.per_unit_spike_rate(firing_rates[ses_m1_mth][mth], ci='sd')
    plt.suptitle(f'Motor thalamus - per-unit across-trials firing rate (aligned to push)')
    utils.save(fig_dir / f'MTh_unit_spike_rate_{duration}s_{ses}.png')

else:
#   utils.save(fig_dir / f'unit_spike_rate_cued_reach_{duration}s_{area}_{name}')

    m2 = stim_all[session][0]
    ppc = stim_all[session][1]

    for unit in m2:
        firing_rate = unit.mean()
        firing_rate = firing_rate.iloc[0: 1000]
        baseline = firing_rate.iloc[-500: -100]
        firing_rate = firing_rate - baseline
        if firing_rate.max() < abs(firing_rate.min()):
            np.where(firing_rate == firing_rate.min())
        else:
            np.where(firing_rate == firing_rate.max())
        
    fig = spike_rate.per_unit_spike_rate(stim_all[session][rec_num], ci=ci)
    name = exp[session].name
    plt.suptitle(f'Session {name} - per-unit across-trials firing rate (aligned to all visual stimulations)')
    utils.save(fig_dir / f'unit_spike_rate_all_visual_stim_{duration}s_{area}_{name}')

    #fig = spike_rate.per_unit_spike_rate(stim_short[session][rec_num], ci=ci)
    #name = exp[session].name
    #plt.suptitle(f'Session {name} - per-unit across-trials firing rate (aligned to short visual stimulations)')
    #utils.save(fig_dir / f'unit_spike_rate_short_visual_stim_{duration}s_{area}_{name}')

    #fig = spike_rate.per_unit_spike_rate(stim_long[session][rec_num], ci=ci)
    #name = exp[session].name
    #plt.suptitle(f'Session {name} - per-unit across-trials firing rate (aligned to long visual stimulations)')
    #utils.save(fig_dir / f'unit_spike_rate_long_visual_stim_{duration}s_{area}_{name}')

    #fig = spike_rate.per_unit_spike_rate(stim_left[session][rec_num], ci=ci)
)

pulls = exp.align_trials(
    ActionLabels.rewarded_pull_good_mi,
    Events.motion_index_onset,
    'spike_rate',
    duration=duration,
    units=units,
)

# Only one recording
rec_num = 0

# I wanted to really check that the same units list is seen in all variables
all_unit_ids = [u for s in units for r in s for u in r]
ps_units = pushes.columns.get_level_values('unit').unique().values.copy()
pl_units = pulls.columns.get_level_values('unit').unique().values.copy()
ps_units.sort()
pl_units.sort()
assert all(ps_units == np.unique(all_unit_ids))
assert all(pl_units == np.unique(all_unit_ids))

# Plot
for i, session in enumerate(exp):
    name = session.name

    subplots = spike_rate.per_unit_spike_rate(pushes[i][rec_num], ci='sd')
    spike_rate.per_unit_spike_rate(pulls[i][rec_num], ci='sd', subplots=subplots)
    plt.suptitle(f'Pushes + pulls - per-unit firing rate (aligned to MI onset)')
    utils.save(fig_dir / f'pushpulls_unit_spike_rate_{duration}s_{name}.pdf')
    'spike_rate',
    duration=duration,
    uncurated=True,
)

pulls = exp.align_trials(
    ActionLabels.rewarded_pull,
    Events.front_sensor_open,
    'spike_rate',
    duration=duration,
    uncurated=True,
)

for session in range(len(exp)):
    # per unit
    fig = spike_rate.per_unit_spike_rate(pushes[session][rec_num], ci='sd')
    name = exp[session].name
    plt.suptitle(f'Session {name} - per-unit across-trials firing rate (aligned to cued push)')
    utils.save(fig_dir / f'unit_spike_rate_cued_push_{duration}s_{name}_sd')

    fig = spike_rate.per_unit_spike_rate(pulls[session][rec_num], ci='sd')
    name = exp[session].name
    plt.suptitle(f'Session {name} - per-unit across-trials firing rate (aligned to cued pull)')
    utils.save(fig_dir / f'unit_spike_rate_cued_pull_{duration}s_{name}_sd')

    # per trial
    fig = spike_rate.per_trial_spike_rate(pushes[session][rec_num], ci='sd')
    name = exp[session].name
    plt.suptitle(f'Session {name} - per-trial across-units firing rate (aligned to cued push)')
    utils.save(fig_dir / f'trial_spike_rate_cued_push_{duration}s_{name}_sd')