fix Nvidia shit and compute event time correlations that have been added. event time analysis now include analysis on chirpt times/quantity during or relative to chasings. this about this again.
This commit is contained in:
		
							parent
							
								
									fa6ed5b3b3
								
							
						
					
					
						commit
						ea9d5f22f8
					
				@ -182,110 +182,63 @@ def relative_rate_progression(all_event_t, title=''):
 | 
			
		||||
    print(f'Progression {title}: pearson-r={r:.2f} p={p:.3f}')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def chase_time_progression(all_ag_on_t, all_ag_off_t):
 | 
			
		||||
    stop_t = 3*60*60
 | 
			
		||||
    snippet_len = 15*60
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main(base_path):
 | 
			
		||||
    if not os.path.exists(os.path.join(os.path.split(__file__)[0], 'figures', 'event_meta')):
 | 
			
		||||
        os.makedirs(os.path.join(os.path.split(__file__)[0], 'figures', 'event_meta'))
 | 
			
		||||
 | 
			
		||||
    if not os.path.exists(os.path.join(os.path.split(__file__)[0], 'figures', 'event_time_corr')):
 | 
			
		||||
        os.makedirs(os.path.join(os.path.split(__file__)[0], 'figures', 'event_time_corr'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    trial_summary = pd.read_csv(os.path.join(base_path, 'trial_summary.csv'), index_col=0)
 | 
			
		||||
    chirp_notes = pd.read_csv(os.path.join(base_path, 'chirp_notes.csv'), index_col=0)
 | 
			
		||||
    trial_mask = chirp_notes['good'] == 1
 | 
			
		||||
    # trial_summary = trial_summary[chirp_notes['good'] == 1]
 | 
			
		||||
 | 
			
		||||
    all_rise_times_lose = []
 | 
			
		||||
    all_rise_times_win = []
 | 
			
		||||
    all_chirp_times_lose = []
 | 
			
		||||
    all_chirp_times_win = []
 | 
			
		||||
    win_sex = []
 | 
			
		||||
    lose_sex = []
 | 
			
		||||
 | 
			
		||||
    all_contact_t = []
 | 
			
		||||
    all_ag_on_t = []
 | 
			
		||||
    all_ag_off_t = []
 | 
			
		||||
 | 
			
		||||
    for index, trial in trial_summary.iterrows():
 | 
			
		||||
        print(index, len(trial_summary))
 | 
			
		||||
        got_boris = False
 | 
			
		||||
 | 
			
		||||
        trial_path = os.path.join(base_path, trial['recording'])
 | 
			
		||||
 | 
			
		||||
        if trial['group'] < 3:
 | 
			
		||||
    snippet_starts = np.arange(0, stop_t, snippet_len)
 | 
			
		||||
    all_snippet_chase_dur = []
 | 
			
		||||
    for a_on, a_off in zip(all_ag_on_t, all_ag_off_t):
 | 
			
		||||
        if len(a_on) == 0:
 | 
			
		||||
            continue
 | 
			
		||||
        if trial['draw'] == 1:
 | 
			
		||||
            continue
 | 
			
		||||
        if os.path.exists(os.path.join(trial_path, 'led_idxs.csv')):
 | 
			
		||||
            got_boris = True
 | 
			
		||||
        if os.path.exists(os.path.join(trial_path, 'LED_frames.npy')):
 | 
			
		||||
            got_boris = True
 | 
			
		||||
 | 
			
		||||
        ids = np.load(os.path.join(trial_path, 'analysis', 'ids.npy'))
 | 
			
		||||
        times = np.load(os.path.join(trial_path, 'times.npy'))
 | 
			
		||||
        sorter = -1 if trial['win_ID'] != ids[0] else 1
 | 
			
		||||
 | 
			
		||||
        ### event times --> BORIS behavior
 | 
			
		||||
        if got_boris:
 | 
			
		||||
            contact_t_GRID, ag_on_off_t_GRID, led_idx, led_frames = \
 | 
			
		||||
                load_and_converete_boris_events(trial_path, trial['recording'], sr=20_000)
 | 
			
		||||
            all_contact_t.append(contact_t_GRID)
 | 
			
		||||
            all_ag_on_t.append(ag_on_off_t_GRID[:, 0])
 | 
			
		||||
            all_ag_off_t.append(ag_on_off_t_GRID[:, 1])
 | 
			
		||||
        mean_chase_dur = np.mean(a_off - a_on)
 | 
			
		||||
        snippet_chase_dur = []
 | 
			
		||||
        for s0 in snippet_starts:
 | 
			
		||||
            snippet_mask = (a_on > s0) & (a_on <= s0+snippet_len)
 | 
			
		||||
            if np.any(snippet_mask):
 | 
			
		||||
                snippet_chase_dur.append(np.mean(a_off[snippet_mask] - a_on[snippet_mask]))
 | 
			
		||||
            else:
 | 
			
		||||
            all_contact_t.append(np.array([]))
 | 
			
		||||
            all_ag_on_t.append(np.array([]))
 | 
			
		||||
            all_ag_off_t.append(np.array([]))
 | 
			
		||||
                snippet_chase_dur.append(np.nan)
 | 
			
		||||
 | 
			
		||||
        ### communication
 | 
			
		||||
        if not os.path.exists(os.path.join(trial_path, 'chirp_times_cnn.npy')):
 | 
			
		||||
            continue
 | 
			
		||||
        chirp_t = np.load(os.path.join(trial_path, 'chirp_times_cnn.npy'))
 | 
			
		||||
        chirp_ids = np.load(os.path.join(trial_path, 'chirp_ids_cnn.npy'))
 | 
			
		||||
        chirp_times = [chirp_t[chirp_ids == trial['win_ID']], chirp_t[chirp_ids == trial['lose_ID']]]
 | 
			
		||||
        all_snippet_chase_dur.append(np.array(snippet_chase_dur) / mean_chase_dur)
 | 
			
		||||
    all_snippet_chase_dur = np.array(all_snippet_chase_dur)
 | 
			
		||||
 | 
			
		||||
    fig = plt.figure(figsize=(20/2.54, 12/2.54))
 | 
			
		||||
    gs = gridspec.GridSpec(1, 1, left=.1, bottom=.1, right=0.95, top=0.95)
 | 
			
		||||
    ax = fig.add_subplot(gs[0, 0])
 | 
			
		||||
 | 
			
		||||
    plot_t = np.repeat(snippet_starts, 2)
 | 
			
		||||
    plot_t[1::2] += snippet_len
 | 
			
		||||
 | 
			
		||||
    for trial_snippet_chase_dur in all_snippet_chase_dur:
 | 
			
		||||
        plot_ratios = np.repeat(trial_snippet_chase_dur, 2)
 | 
			
		||||
        ax.plot(plot_t / 3600, plot_ratios, color='grey', lw=1, alpha=0.5)
 | 
			
		||||
        # ax.plot(snippet_starts + snippet_len/2, event_ratios)
 | 
			
		||||
    mean_ratio = np.nanmean(all_snippet_chase_dur, axis=0)
 | 
			
		||||
    plot_mean_ratio = np.repeat(mean_ratio, 2)
 | 
			
		||||
    ax.plot(plot_t / 3600, plot_mean_ratio, color='k', lw=3)
 | 
			
		||||
    ax.plot(plot_t / 3600, np.ones_like(plot_t), linestyle='dotted', lw=2, color='k')
 | 
			
		||||
 | 
			
		||||
    ax.set_xlabel('time [h]', fontsize=12)
 | 
			
		||||
    ax.set_ylabel('chase duration / mean(chase duration)', fontsize=12)
 | 
			
		||||
    ax.set_title('progression chase duration ')
 | 
			
		||||
    ax.tick_params(labelsize=10)
 | 
			
		||||
 | 
			
		||||
    ax.set_xlim(0, 3)
 | 
			
		||||
    ax.set_ylim(0, 5)
 | 
			
		||||
 | 
			
		||||
    x = np.hstack(all_snippet_chase_dur)
 | 
			
		||||
    y = np.hstack(np.tile(snippet_starts, (all_snippet_chase_dur.shape[0], 1)))
 | 
			
		||||
 | 
			
		||||
    r, p = scp.pearsonr(x[~np.isnan(x)], y[~np.isnan(x)])
 | 
			
		||||
 | 
			
		||||
    print(f'Progression chase duration: pearson-r={r:.2f} p={p:.3f}')
 | 
			
		||||
 | 
			
		||||
    plt.savefig(os.path.join(os.path.split(__file__)[0], 'figures', 'event_meta', 'chase_duration_progression.png'), dpi=300)
 | 
			
		||||
    plt.close()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        rise_idx = np.load(os.path.join(trial_path, 'analysis', 'rise_idx.npy'))[::sorter]
 | 
			
		||||
        rise_idx_int = [np.array(rise_idx[i][~np.isnan(rise_idx[i])], dtype=int) for i in range(len(rise_idx))]
 | 
			
		||||
        rise_times = [times[rise_idx_int[0]], times[rise_idx_int[1]]]
 | 
			
		||||
 | 
			
		||||
        all_rise_times_lose.append(rise_times[1])
 | 
			
		||||
        all_rise_times_win.append(rise_times[0])
 | 
			
		||||
 | 
			
		||||
        if trial_mask[index]:
 | 
			
		||||
            all_chirp_times_lose.append(chirp_times[1])
 | 
			
		||||
            all_chirp_times_win.append(chirp_times[0])
 | 
			
		||||
        else:
 | 
			
		||||
            all_chirp_times_lose.append(np.array([]))
 | 
			
		||||
            all_chirp_times_win.append(np.array([]))
 | 
			
		||||
 | 
			
		||||
        win_sex.append(trial['sex_win'])
 | 
			
		||||
        lose_sex.append(trial['sex_lose'])
 | 
			
		||||
 | 
			
		||||
    win_sex = np.array(win_sex)
 | 
			
		||||
    lose_sex = np.array(lose_sex)
 | 
			
		||||
 | 
			
		||||
    iei_analysis(all_chirp_times_lose, win_sex, lose_sex, kernal_w=1, title=r'chirps$_{lose}$')
 | 
			
		||||
    iei_analysis(all_chirp_times_win,  win_sex, lose_sex, kernal_w=1, title=r'chirps$_{win}$')
 | 
			
		||||
    iei_analysis(all_rise_times_lose, win_sex, lose_sex, kernal_w=5, title=r'rises$_{lose}$')
 | 
			
		||||
    iei_analysis(all_rise_times_win, win_sex, lose_sex, kernal_w=50, title=r'rises$_{win}$')
 | 
			
		||||
 | 
			
		||||
    print('')
 | 
			
		||||
    relative_rate_progression(all_chirp_times_lose, title=r'chirp$_{lose}$')
 | 
			
		||||
    relative_rate_progression(all_chirp_times_win, title=r'chirp$_{win}$')
 | 
			
		||||
    relative_rate_progression(all_rise_times_lose, title=r'rises$_{lose}$')
 | 
			
		||||
    relative_rate_progression(all_rise_times_win, title=r'rises$_{win}$')
 | 
			
		||||
 | 
			
		||||
    relative_rate_progression(all_contact_t, title=r'contact')
 | 
			
		||||
    relative_rate_progression(all_ag_on_t, title=r'chasing')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    #############################################################################
 | 
			
		||||
    for all_event_t, event_name in zip([all_chirp_times_lose, all_chirp_times_win, all_rise_times_lose, all_rise_times_win],
 | 
			
		||||
                                       [r'chirps$_{lose}$', r'chirps$_{win}$', r'rises$_{lose}$', r'rises$_{win}$']):
 | 
			
		||||
def event_category_signal(all_event_t, all_contact_t, all_ag_on_t, all_ag_off_t, win_sex, lose_sex, event_name):
 | 
			
		||||
    print('')
 | 
			
		||||
    all_pre_chase_event_mask = []
 | 
			
		||||
    all_chase_event_mask = []
 | 
			
		||||
@ -385,7 +338,6 @@ def main(base_path):
 | 
			
		||||
        t, p = scp.ttest_rel(x, y)
 | 
			
		||||
        print(f'{event_name} {name}: t={t:.2f} p={p:.3f}')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    fig = plt.figure(figsize=(20 / 2.54, 12 / 2.54))
 | 
			
		||||
    gs = gridspec.GridSpec(1, 2, left=0.1, bottom=0.15, right=0.95, top=0.9)
 | 
			
		||||
    ax = fig.add_subplot(gs[0, 0])
 | 
			
		||||
@ -396,7 +348,8 @@ def main(base_path):
 | 
			
		||||
                all_end_chase_event_ratio / all_end_chase_time_ratio,
 | 
			
		||||
                all_after_chase_event_ratio / all_after_chase_time_ratio,
 | 
			
		||||
                all_before_countact_event_ratio / all_before_countact_time_ratio,
 | 
			
		||||
                    all_after_countact_event_ratio/all_after_countact_time_ratio], positions=np.arange(6), sym='', zorder=2)
 | 
			
		||||
                all_after_countact_event_ratio / all_after_countact_time_ratio], positions=np.arange(6), sym='',
 | 
			
		||||
               zorder=2)
 | 
			
		||||
 | 
			
		||||
    ylim = list(ax.get_ylim())
 | 
			
		||||
    ylim[0] = -.1 if ylim[0] < -.1 else ylim[0]
 | 
			
		||||
@ -414,30 +367,36 @@ def main(base_path):
 | 
			
		||||
            print('error')
 | 
			
		||||
            embed()
 | 
			
		||||
            quit()
 | 
			
		||||
            values = np.array(all_pre_chase_event_ratio/all_pre_chase_time_ratio)[(video_trial_win_sex == sex_w) & (video_trial_lose_sex == sex_l)]
 | 
			
		||||
            ax.plot(np.ones_like(values) * 0, values, marker=marker, linestyle='None', color=c, mec=mec, markersize=8, zorder=1)
 | 
			
		||||
        values = np.array(all_pre_chase_event_ratio / all_pre_chase_time_ratio)[
 | 
			
		||||
            (video_trial_win_sex == sex_w) & (video_trial_lose_sex == sex_l)]
 | 
			
		||||
        ax.plot(np.ones_like(values) * 0, values, marker=marker, linestyle='None', color=c, mec=mec, markersize=8,
 | 
			
		||||
                zorder=1)
 | 
			
		||||
 | 
			
		||||
            values = np.array(all_chase_event_ratio/all_chase_time_ratio)[(video_trial_win_sex == sex_w) & (video_trial_lose_sex == sex_l)]
 | 
			
		||||
            ax.plot(np.ones_like(values) * 1, values, marker=marker, linestyle='None', color=c, mec=mec, markersize=8, zorder=1)
 | 
			
		||||
        values = np.array(all_chase_event_ratio / all_chase_time_ratio)[
 | 
			
		||||
            (video_trial_win_sex == sex_w) & (video_trial_lose_sex == sex_l)]
 | 
			
		||||
        ax.plot(np.ones_like(values) * 1, values, marker=marker, linestyle='None', color=c, mec=mec, markersize=8,
 | 
			
		||||
                zorder=1)
 | 
			
		||||
 | 
			
		||||
            values = np.array(all_end_chase_event_ratio/all_end_chase_time_ratio)[(video_trial_win_sex == sex_w) & (video_trial_lose_sex == sex_l)]
 | 
			
		||||
            ax.plot(np.ones_like(values) * 2, values, marker=marker, linestyle='None', color=c, mec=mec, markersize=8, zorder=1)
 | 
			
		||||
        values = np.array(all_end_chase_event_ratio / all_end_chase_time_ratio)[
 | 
			
		||||
            (video_trial_win_sex == sex_w) & (video_trial_lose_sex == sex_l)]
 | 
			
		||||
        ax.plot(np.ones_like(values) * 2, values, marker=marker, linestyle='None', color=c, mec=mec, markersize=8,
 | 
			
		||||
                zorder=1)
 | 
			
		||||
 | 
			
		||||
            values = np.array(all_after_chase_event_ratio/all_after_chase_time_ratio)[(video_trial_win_sex == sex_w) & (video_trial_lose_sex == sex_l)]
 | 
			
		||||
            ax.plot(np.ones_like(values) * 3, values, marker=marker, linestyle='None', color=c, mec=mec, markersize=8, zorder=1)
 | 
			
		||||
        values = np.array(all_after_chase_event_ratio / all_after_chase_time_ratio)[
 | 
			
		||||
            (video_trial_win_sex == sex_w) & (video_trial_lose_sex == sex_l)]
 | 
			
		||||
        ax.plot(np.ones_like(values) * 3, values, marker=marker, linestyle='None', color=c, mec=mec, markersize=8,
 | 
			
		||||
                zorder=1)
 | 
			
		||||
 | 
			
		||||
            values = np.array(all_before_countact_event_ratio/all_before_countact_time_ratio)[(video_trial_win_sex == sex_w) & (video_trial_lose_sex == sex_l)]
 | 
			
		||||
            ax.plot(np.ones_like(values) * 4, values, marker=marker, linestyle='None', color=c, mec=mec, markersize=8, zorder=1)
 | 
			
		||||
        values = np.array(all_before_countact_event_ratio / all_before_countact_time_ratio)[
 | 
			
		||||
            (video_trial_win_sex == sex_w) & (video_trial_lose_sex == sex_l)]
 | 
			
		||||
        ax.plot(np.ones_like(values) * 4, values, marker=marker, linestyle='None', color=c, mec=mec, markersize=8,
 | 
			
		||||
                zorder=1)
 | 
			
		||||
 | 
			
		||||
            values = np.array(all_after_countact_event_ratio/all_after_countact_time_ratio)[(video_trial_win_sex == sex_w) & (video_trial_lose_sex == sex_l)]
 | 
			
		||||
            ax.plot(np.ones_like(values) * 5, values, marker=marker, linestyle='None', color=c, mec=mec, markersize=8, zorder=1)
 | 
			
		||||
        values = np.array(all_after_countact_event_ratio / all_after_countact_time_ratio)[
 | 
			
		||||
            (video_trial_win_sex == sex_w) & (video_trial_lose_sex == sex_l)]
 | 
			
		||||
        ax.plot(np.ones_like(values) * 5, values, marker=marker, linestyle='None', color=c, mec=mec, markersize=8,
 | 
			
		||||
                zorder=1)
 | 
			
		||||
 | 
			
		||||
        # ax.plot(np.ones_like(all_pre_chase_event_ratio) * 0, all_pre_chase_event_ratio/all_pre_chase_time_ratio, 'ok')
 | 
			
		||||
        # ax.plot(np.ones_like(all_chase_event_ratio) * 1, all_chase_event_ratio/all_chase_time_ratio, 'ok')
 | 
			
		||||
        # ax.plot(np.ones_like(all_end_chase_event_ratio) * 2, all_end_chase_event_ratio/all_end_chase_time_ratio, 'ok')
 | 
			
		||||
        # ax.plot(np.ones_like(all_after_chase_event_ratio) * 3, all_after_chase_event_ratio/all_after_chase_time_ratio, 'ok')
 | 
			
		||||
 | 
			
		||||
        # ax.plot(np.ones_like(all_before_countact_event_ratio) * 4, all_before_countact_event_ratio/all_before_countact_time_ratio, 'ok')
 | 
			
		||||
    ##############################################################################
 | 
			
		||||
 | 
			
		||||
    ax.plot(np.arange(7) - 1, np.ones(7), linestyle='dotted', lw=2, color='k')
 | 
			
		||||
@ -446,11 +405,10 @@ def main(base_path):
 | 
			
		||||
 | 
			
		||||
    ax.set_ylabel(r'rel. count$_{event}$ / rel. time$_{event}$', fontsize=12)
 | 
			
		||||
    ax.set_xticks(np.arange(6))
 | 
			
		||||
        ax.set_xticklabels([r'chase$_{before}$', r'chasing', r'chase$_{end}$', r'chase$_{after}$', 'contact$_{before}$', 'contact$_{after}$'], rotation=45)
 | 
			
		||||
    ax.set_xticklabels([r'chase$_{before}$', r'chasing', r'chase$_{end}$', r'chase$_{after}$', 'contact$_{before}$',
 | 
			
		||||
                        'contact$_{after}$'], rotation=45)
 | 
			
		||||
    ax.tick_params(labelsize=10)
 | 
			
		||||
        # ax.set_title(event_name)
 | 
			
		||||
    fig.suptitle(f'{event_name}: n={len(np.hstack(all_event_t))}')
 | 
			
		||||
        # plt.show()
 | 
			
		||||
 | 
			
		||||
    ###############################################
 | 
			
		||||
    flat_pre_chase_event_mask = np.hstack(all_pre_chase_event_mask)
 | 
			
		||||
@ -475,7 +433,8 @@ def main(base_path):
 | 
			
		||||
    event_context_values.append(1 - np.sum(event_context_values))
 | 
			
		||||
 | 
			
		||||
    time_context_values = [np.sum(all_pre_chase_time), np.sum(all_chase_time), np.sum(all_end_chase_time),
 | 
			
		||||
                               np.sum(all_after_chase_time), np.sum(all_before_contact_time), np.sum(all_after_contact_time)]
 | 
			
		||||
                           np.sum(all_after_chase_time), np.sum(all_before_contact_time),
 | 
			
		||||
                           np.sum(all_after_contact_time)]
 | 
			
		||||
 | 
			
		||||
    time_context_values.append(len(all_pre_chase_time) * 3 * 60 * 60 - np.sum(time_context_values))
 | 
			
		||||
    time_context_values /= np.sum(time_context_values)
 | 
			
		||||
@ -490,25 +449,202 @@ def main(base_path):
 | 
			
		||||
 | 
			
		||||
    ax_pie.set_title(r'event context')
 | 
			
		||||
    legend_elements = [Patch(facecolor='tab:red', edgecolor='w', label='%.1f' % (event_context_values[0] * 100) + '%'),
 | 
			
		||||
                           Patch(facecolor='tab:orange', edgecolor='w', label='%.1f' % (event_context_values[1] * 100) + '%'),
 | 
			
		||||
                       Patch(facecolor='tab:orange', edgecolor='w',
 | 
			
		||||
                             label='%.1f' % (event_context_values[1] * 100) + '%'),
 | 
			
		||||
                       Patch(facecolor='yellow', edgecolor='w', label='%.1f' % (event_context_values[2] * 100) + '%'),
 | 
			
		||||
                           Patch(facecolor='tab:green', edgecolor='w', label='%.1f' % (event_context_values[3] * 100) + '%'),
 | 
			
		||||
                       Patch(facecolor='tab:green', edgecolor='w',
 | 
			
		||||
                             label='%.1f' % (event_context_values[3] * 100) + '%'),
 | 
			
		||||
                       Patch(facecolor='k', edgecolor='w', label='%.1f' % (event_context_values[4] * 100) + '%'),
 | 
			
		||||
                           Patch(facecolor='tab:brown', edgecolor='w', label='%.1f' % (event_context_values[5] * 100) + '%'),
 | 
			
		||||
                           Patch(facecolor='tab:red', alpha=0.6, edgecolor='w', label='%.1f' % (time_context_values[0] * 100) + '%'),
 | 
			
		||||
                           Patch(facecolor='tab:orange', alpha=0.6, edgecolor='w', label='%.1f' % (time_context_values[1] * 100) + '%'),
 | 
			
		||||
                           Patch(facecolor='yellow', alpha=0.6, edgecolor='w', label='%.1f' % (time_context_values[2] * 100) + '%'),
 | 
			
		||||
                           Patch(facecolor='tab:green', alpha=0.6, edgecolor='w', label='%.1f' % (time_context_values[3] * 100) + '%'),
 | 
			
		||||
                           Patch(facecolor='k', alpha=0.6, edgecolor='w', label='%.1f' % (time_context_values[4] * 100) + '%'),
 | 
			
		||||
                           Patch(facecolor='tab:brown', alpha=0.6, edgecolor='w', label='%.1f' % (time_context_values[5] * 100) + '%')]
 | 
			
		||||
                       Patch(facecolor='tab:brown', edgecolor='w',
 | 
			
		||||
                             label='%.1f' % (event_context_values[5] * 100) + '%'),
 | 
			
		||||
                       Patch(facecolor='tab:red', alpha=0.6, edgecolor='w',
 | 
			
		||||
                             label='%.1f' % (time_context_values[0] * 100) + '%'),
 | 
			
		||||
                       Patch(facecolor='tab:orange', alpha=0.6, edgecolor='w',
 | 
			
		||||
                             label='%.1f' % (time_context_values[1] * 100) + '%'),
 | 
			
		||||
                       Patch(facecolor='yellow', alpha=0.6, edgecolor='w',
 | 
			
		||||
                             label='%.1f' % (time_context_values[2] * 100) + '%'),
 | 
			
		||||
                       Patch(facecolor='tab:green', alpha=0.6, edgecolor='w',
 | 
			
		||||
                             label='%.1f' % (time_context_values[3] * 100) + '%'),
 | 
			
		||||
                       Patch(facecolor='k', alpha=0.6, edgecolor='w',
 | 
			
		||||
                             label='%.1f' % (time_context_values[4] * 100) + '%'),
 | 
			
		||||
                       Patch(facecolor='tab:brown', alpha=0.6, edgecolor='w',
 | 
			
		||||
                             label='%.1f' % (time_context_values[5] * 100) + '%')]
 | 
			
		||||
 | 
			
		||||
        ax_pie.legend(handles=legend_elements, loc='lower right', ncol=2, bbox_to_anchor=(1.15, -0.25), frameon=False, fontsize=9)
 | 
			
		||||
    ax_pie.legend(handles=legend_elements, loc='lower right', ncol=2, bbox_to_anchor=(1.15, -0.25), frameon=False,
 | 
			
		||||
                  fontsize=9)
 | 
			
		||||
 | 
			
		||||
        plt.savefig(os.path.join(os.path.split(__file__)[0], 'figures', 'event_time_corr', f'{event_name}_categories.png'), dpi=300)
 | 
			
		||||
    plt.savefig(os.path.join(os.path.split(__file__)[0], 'figures', 'event_time_corr', f'{event_name}_categories.png'),
 | 
			
		||||
                dpi=300)
 | 
			
		||||
    plt.close()
 | 
			
		||||
    # plt.show()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main(base_path):
 | 
			
		||||
    if not os.path.exists(os.path.join(os.path.split(__file__)[0], 'figures', 'event_meta')):
 | 
			
		||||
        os.makedirs(os.path.join(os.path.split(__file__)[0], 'figures', 'event_meta'))
 | 
			
		||||
 | 
			
		||||
    if not os.path.exists(os.path.join(os.path.split(__file__)[0], 'figures', 'event_time_corr')):
 | 
			
		||||
        os.makedirs(os.path.join(os.path.split(__file__)[0], 'figures', 'event_time_corr'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    trial_summary = pd.read_csv(os.path.join(base_path, 'trial_summary.csv'), index_col=0)
 | 
			
		||||
    chirp_notes = pd.read_csv(os.path.join(base_path, 'chirp_notes.csv'), index_col=0)
 | 
			
		||||
    trial_mask = chirp_notes['good'] == 1
 | 
			
		||||
 | 
			
		||||
    ### data processing #######################
 | 
			
		||||
    all_rise_times_lose = []
 | 
			
		||||
    all_rise_times_win = []
 | 
			
		||||
    all_chirp_times_lose = []
 | 
			
		||||
    all_chirp_times_win = []
 | 
			
		||||
    win_sex = []
 | 
			
		||||
    lose_sex = []
 | 
			
		||||
 | 
			
		||||
    all_contact_t = []
 | 
			
		||||
    all_ag_on_t = []
 | 
			
		||||
    all_ag_off_t = []
 | 
			
		||||
 | 
			
		||||
    for index, trial in trial_summary.iterrows():
 | 
			
		||||
        print(index, len(trial_summary))
 | 
			
		||||
        got_boris = False
 | 
			
		||||
 | 
			
		||||
        trial_path = os.path.join(base_path, trial['recording'])
 | 
			
		||||
 | 
			
		||||
        if trial['group'] < 3:
 | 
			
		||||
            continue
 | 
			
		||||
        if trial['draw'] == 1:
 | 
			
		||||
            continue
 | 
			
		||||
        if os.path.exists(os.path.join(trial_path, 'led_idxs.csv')):
 | 
			
		||||
            got_boris = True
 | 
			
		||||
        if os.path.exists(os.path.join(trial_path, 'LED_frames.npy')):
 | 
			
		||||
            got_boris = True
 | 
			
		||||
 | 
			
		||||
        ids = np.load(os.path.join(trial_path, 'analysis', 'ids.npy'))
 | 
			
		||||
        times = np.load(os.path.join(trial_path, 'times.npy'))
 | 
			
		||||
        sorter = -1 if trial['win_ID'] != ids[0] else 1
 | 
			
		||||
 | 
			
		||||
        ### event times --> BORIS behavior
 | 
			
		||||
        if got_boris:
 | 
			
		||||
            contact_t_GRID, ag_on_off_t_GRID, led_idx, led_frames = \
 | 
			
		||||
                load_and_converete_boris_events(trial_path, trial['recording'], sr=20_000)
 | 
			
		||||
            all_contact_t.append(contact_t_GRID)
 | 
			
		||||
            all_ag_on_t.append(ag_on_off_t_GRID[:, 0])
 | 
			
		||||
            all_ag_off_t.append(ag_on_off_t_GRID[:, 1])
 | 
			
		||||
        else:
 | 
			
		||||
            all_contact_t.append(np.array([]))
 | 
			
		||||
            all_ag_on_t.append(np.array([]))
 | 
			
		||||
            all_ag_off_t.append(np.array([]))
 | 
			
		||||
 | 
			
		||||
        ### communication
 | 
			
		||||
        if not os.path.exists(os.path.join(trial_path, 'chirp_times_cnn.npy')):
 | 
			
		||||
            continue
 | 
			
		||||
        chirp_t = np.load(os.path.join(trial_path, 'chirp_times_cnn.npy'))
 | 
			
		||||
        chirp_ids = np.load(os.path.join(trial_path, 'chirp_ids_cnn.npy'))
 | 
			
		||||
        chirp_times = [chirp_t[chirp_ids == trial['win_ID']], chirp_t[chirp_ids == trial['lose_ID']]]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        rise_idx = np.load(os.path.join(trial_path, 'analysis', 'rise_idx.npy'))[::sorter]
 | 
			
		||||
        rise_idx_int = [np.array(rise_idx[i][~np.isnan(rise_idx[i])], dtype=int) for i in range(len(rise_idx))]
 | 
			
		||||
        rise_times = [times[rise_idx_int[0]], times[rise_idx_int[1]]]
 | 
			
		||||
 | 
			
		||||
        all_rise_times_lose.append(rise_times[1])
 | 
			
		||||
        all_rise_times_win.append(rise_times[0])
 | 
			
		||||
 | 
			
		||||
        if trial_mask[index]:
 | 
			
		||||
            all_chirp_times_lose.append(chirp_times[1])
 | 
			
		||||
            all_chirp_times_win.append(chirp_times[0])
 | 
			
		||||
        else:
 | 
			
		||||
            all_chirp_times_lose.append(np.array([]))
 | 
			
		||||
            all_chirp_times_win.append(np.array([]))
 | 
			
		||||
 | 
			
		||||
        win_sex.append(trial['sex_win'])
 | 
			
		||||
        lose_sex.append(trial['sex_lose'])
 | 
			
		||||
 | 
			
		||||
    win_sex = np.array(win_sex)
 | 
			
		||||
    lose_sex = np.array(lose_sex)
 | 
			
		||||
 | 
			
		||||
    ### inter event intervalls ###
 | 
			
		||||
    iei_analysis(all_chirp_times_lose, win_sex, lose_sex, kernal_w=1, title=r'chirps$_{lose}$')
 | 
			
		||||
    iei_analysis(all_chirp_times_win,  win_sex, lose_sex, kernal_w=1, title=r'chirps$_{win}$')
 | 
			
		||||
    iei_analysis(all_rise_times_lose, win_sex, lose_sex, kernal_w=5, title=r'rises$_{lose}$')
 | 
			
		||||
    iei_analysis(all_rise_times_win, win_sex, lose_sex, kernal_w=50, title=r'rises$_{win}$')
 | 
			
		||||
 | 
			
		||||
    ### event progressions ###
 | 
			
		||||
    print('')
 | 
			
		||||
    relative_rate_progression(all_chirp_times_lose, title=r'chirp$_{lose}$')
 | 
			
		||||
    relative_rate_progression(all_chirp_times_win, title=r'chirp$_{win}$')
 | 
			
		||||
    relative_rate_progression(all_rise_times_lose, title=r'rises$_{lose}$')
 | 
			
		||||
    relative_rate_progression(all_rise_times_win, title=r'rises$_{win}$')
 | 
			
		||||
 | 
			
		||||
    relative_rate_progression(all_contact_t, title=r'contact')
 | 
			
		||||
    relative_rate_progression(all_ag_on_t, title=r'chasing')
 | 
			
		||||
 | 
			
		||||
    chase_time_progression(all_ag_on_t, all_ag_off_t)
 | 
			
		||||
 | 
			
		||||
    ### event category signals ###
 | 
			
		||||
    for all_event_t, event_name in zip([all_chirp_times_lose, all_chirp_times_win, all_rise_times_lose, all_rise_times_win],
 | 
			
		||||
                                       [r'chirps$_{lose}$', r'chirps$_{win}$', r'rises$_{lose}$', r'rises$_{win}$']):
 | 
			
		||||
        event_category_signal(all_event_t, all_contact_t, all_ag_on_t, all_ag_off_t, win_sex, lose_sex, event_name)
 | 
			
		||||
 | 
			
		||||
    embed()
 | 
			
		||||
    quit()
 | 
			
		||||
 | 
			
		||||
    chase_dur = []
 | 
			
		||||
    chase_chirp_count = []
 | 
			
		||||
    dt_start_first_chirp = []
 | 
			
		||||
    dt_end_first_chirp = []
 | 
			
		||||
    for ag_on_t, ag_off_t, chirp_times_lose in zip(all_ag_on_t, all_ag_off_t, all_chirp_times_lose):
 | 
			
		||||
        if len(chirp_times_lose) == 0:
 | 
			
		||||
            continue
 | 
			
		||||
        for a_on, a_off in zip(ag_on_t, ag_off_t):
 | 
			
		||||
            chase_dur.append(a_off - a_on)
 | 
			
		||||
            chirp_t_oi = chirp_times_lose[(chirp_times_lose > a_on) & (chirp_times_lose <= a_off)]
 | 
			
		||||
            chase_chirp_count.append(len(chirp_t_oi))
 | 
			
		||||
            if len(chirp_t_oi) >= 1:
 | 
			
		||||
                dt_start_first_chirp.append(chirp_t_oi[0] - a_on)
 | 
			
		||||
                dt_end_first_chirp.append(a_off - chirp_t_oi[0])
 | 
			
		||||
            else:
 | 
			
		||||
                dt_start_first_chirp.append(np.nan)
 | 
			
		||||
                dt_end_first_chirp.append(np.nan)
 | 
			
		||||
 | 
			
		||||
    fig = plt.figure(figsize=(21/2.54, 19/2.54))
 | 
			
		||||
    gs = gridspec.GridSpec(2, 2, left=.15, bottom=0.1, right=0.95, top=0.95)
 | 
			
		||||
    ax = []
 | 
			
		||||
    ax.append(fig.add_subplot(gs[0, 0]))
 | 
			
		||||
    ax.append(fig.add_subplot(gs[0, 1], sharex=ax[0]))
 | 
			
		||||
    ax.append(fig.add_subplot(gs[1, 0], sharex=ax[0]))
 | 
			
		||||
    ax.append(fig.add_subplot(gs[1, 1], sharex=ax[0]))
 | 
			
		||||
 | 
			
		||||
    ax[0].plot(chase_dur, chase_chirp_count, '.')
 | 
			
		||||
    ax[1].plot(chase_dur, np.array(chase_chirp_count) / np.array(chase_dur), '.')
 | 
			
		||||
    ax[2].plot(chase_dur, dt_start_first_chirp, '.')
 | 
			
		||||
    ax[3].plot(chase_dur, dt_end_first_chirp, '.')
 | 
			
		||||
 | 
			
		||||
    ax[2].plot([0, 60], [0, 60], '-k', lw=1)
 | 
			
		||||
    ax[3].plot([0, 60], [0, 60], '-k', lw=1)
 | 
			
		||||
 | 
			
		||||
    ax[2].set_xlabel(r'chase$_{duration}$ [s]', fontsize=12)
 | 
			
		||||
    ax[3].set_xlabel(r'chase$_{duration}$ [s]', fontsize=12)
 | 
			
		||||
    ax[0].set_ylabel('chirps [n]', fontsize=12)
 | 
			
		||||
    ax[1].set_ylabel('chirp rate [Hz]', fontsize=12)
 | 
			
		||||
    ax[2].set_ylabel(r'$\Delta$t chase$_{on}$ - chirp$_{0}$', fontsize=12)
 | 
			
		||||
    ax[3].set_ylabel(r'$\Delta$t chirp$_{0}$ - chase$_{off}$', fontsize=12)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    chase_chirp_count = np.array(chase_chirp_count)
 | 
			
		||||
    chase_dur = np.array(chase_dur)
 | 
			
		||||
 | 
			
		||||
    chirp_rate = chase_chirp_count / chase_dur
 | 
			
		||||
 | 
			
		||||
    r, p = scp.pearsonr(chase_dur[chase_chirp_count >= 3], chase_chirp_count[chase_chirp_count >= 3])
 | 
			
		||||
    ax[0].text(1, 1, f'r= {r:.2f} p={p:.3f}', transform=ax[0].transAxes, ha='right', va='bottom')
 | 
			
		||||
 | 
			
		||||
    r, p = scp.pearsonr(chase_dur[chase_chirp_count >= 3], chirp_rate[chase_chirp_count >= 3])
 | 
			
		||||
    ax[1].text(1, 1, f'r= {r:.2f} p={p:.3f}', transform=ax[1].transAxes, ha='right', va='bottom')
 | 
			
		||||
    plt.show()
 | 
			
		||||
 | 
			
		||||
    fig, ax = plt.subplots()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    main(sys.argv[1])
 | 
			
		||||
 | 
			
		||||
@ -279,6 +279,9 @@ def main(base_path):
 | 
			
		||||
    win_rises_centered_on_lose_chirps = []
 | 
			
		||||
    win_rises_count = []
 | 
			
		||||
 | 
			
		||||
    ag_off_centered_on_ag_on = []
 | 
			
		||||
    ag_count = []
 | 
			
		||||
 | 
			
		||||
    sex_win = []
 | 
			
		||||
    sex_lose = []
 | 
			
		||||
 | 
			
		||||
@ -362,6 +365,9 @@ def main(base_path):
 | 
			
		||||
        win_rises_centered_on_lose_chirps.append(event_centered_times(chirp_times[1], rise_times[0]))
 | 
			
		||||
        win_rises_count.append(len(rise_times[0]))
 | 
			
		||||
 | 
			
		||||
        ag_off_centered_on_ag_on.append(event_centered_times(ag_on_off_t_GRID[:, 0], ag_on_off_t_GRID[:, 1]))
 | 
			
		||||
        ag_count.append(len(ag_on_off_t_GRID))
 | 
			
		||||
 | 
			
		||||
        sex_win.append(trial['sex_win'])
 | 
			
		||||
        sex_lose.append(trial['sex_lose'])
 | 
			
		||||
 | 
			
		||||
@ -399,7 +405,9 @@ def main(base_path):
 | 
			
		||||
             [win_rises_centered_on_ag_off_t, win_rises_count, r'rise$_{win}$ on chase$_{off}$'],
 | 
			
		||||
             [win_rises_centered_on_ag_on_t, win_rises_count, r'rise$_{win}$ on chase$_{on}$'],
 | 
			
		||||
             [win_rises_centered_on_contact_t, win_rises_count, r'rise$_{win}$ on contact'],
 | 
			
		||||
             [win_rises_centered_on_lose_chirps, win_rises_count, r'rise$_{win}$ on chirp$_{lose}$']]:
 | 
			
		||||
             [win_rises_centered_on_lose_chirps, win_rises_count, r'rise$_{win}$ on chirp$_{lose}$'],
 | 
			
		||||
 | 
			
		||||
             [ag_off_centered_on_ag_on, ag_count,  r'chase$_{off}$ on chase$_{on}$']]:
 | 
			
		||||
 | 
			
		||||
        save_str = title.replace('$', '').replace('{', '').replace('}', '').replace(' ', '_')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user