From 49c071e8205b76851dd0c4f54a96adc8af656067 Mon Sep 17 00:00:00 2001 From: Till Raab Date: Mon, 26 Jun 2023 14:34:44 +0200 Subject: [PATCH] chirp distribution in chasing events are not so clear. maybe detect chirp bursts and calculate time correlations with their on/offsets --- event_time_analysis.py | 125 ++++++++++++++++++++++++++++++------- event_time_correlations.py | 120 ++++++++++++++++++++++------------- 2 files changed, 182 insertions(+), 63 deletions(-) diff --git a/event_time_analysis.py b/event_time_analysis.py index ab64dbc..cccbfc9 100644 --- a/event_time_analysis.py +++ b/event_time_analysis.py @@ -585,13 +585,16 @@ def main(base_path): [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 = [] + + dt_start_all_chirp = [] + dt_end_all_chirp = [] + chase_dur_all_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 @@ -602,49 +605,129 @@ def main(base_path): 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]) + + dt_start_all_chirp.extend(chirp_t_oi - a_on) + dt_end_all_chirp.extend(a_off - chirp_t_oi) + chase_dur_all_chirp.extend(np.ones_like(chirp_t_oi) * (a_off - a_on)) 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) + dt_start_first_chirp = np.array(dt_start_first_chirp) + dt_end_first_chirp = np.array(dt_end_first_chirp) + + dt_start_all_chirp = np.array(dt_start_all_chirp) + dt_end_all_chirp = np.array(dt_end_all_chirp) + chase_dur_all_chirp = np.array(chase_dur_all_chirp) + + chase_chirp_count = np.array(chase_chirp_count) + chase_dur = np.array(chase_dur) + chirp_rate = chase_chirp_count / chase_dur + + chase_dur_per_chirp_count = [] + positions = np.arange(np.max(chase_chirp_count)+1) + for cc in positions: + chase_dur_per_chirp_count.append([]) + chase_dur_per_chirp_count[-1].extend(chase_dur[chase_chirp_count == cc]) + + ################################################ + + chase_dur_pct99 = np.percentile(chase_dur, 99) + chase_dur_bins = np.arange(0, chase_dur_pct99+1, 2.5) + chase_dur_count_above_th = np.zeros_like(chase_dur_bins[:-1]) + + for enu, chase_dur_th in enumerate(chase_dur_bins[:-1]): + chase_dur_count_above_th[enu] = len(chase_dur[chase_dur >= chase_dur_th]) + + ################################################ + + fig = plt.figure(figsize=(21/2.54, 28/2.54)) + gs = gridspec.GridSpec(3, 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.append(fig.add_subplot(gs[2, 0], sharex=ax[0])) + ax.append(fig.add_subplot(gs[2, 1], sharex=ax[0])) + ax[0].plot(chase_dur, chase_chirp_count, '.') + ax[0].boxplot(chase_dur_per_chirp_count, positions=positions, vert=False, sym='') 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) + n, _ = np.histogram(dt_start_first_chirp, bins=chase_dur_bins) + n = n / np.sum(n) / (chase_dur_bins[1] - chase_dur_bins[0]) + n = n / chase_dur_count_above_th + n = n / np.max(n) * chase_dur_pct99 + ax[2].barh(chase_dur_bins[:-1] + (chase_dur_bins[1] - chase_dur_bins[0]) / 2, n, + height=(chase_dur_bins[1] - chase_dur_bins[0]) * 0.8, color='firebrick', alpha=0.5, zorder=2) + n, _ = np.histogram(dt_end_first_chirp, bins=chase_dur_bins) + n = n / np.sum(n) / (chase_dur_bins[1] - chase_dur_bins[0]) + n = n / chase_dur_count_above_th + n = n / np.max(n) * chase_dur_pct99 + ax[3].barh(chase_dur_bins[:-1] + (chase_dur_bins[1] - chase_dur_bins[0]) / 2, n, + height=(chase_dur_bins[1] - chase_dur_bins[0]) * 0.8, color='firebrick', alpha=0.5, zorder=2) + ax[3].invert_yaxis() + ax[2].set_xlim(right=chase_dur_pct99 + 2) + ax[2].set_ylim(top=chase_dur_pct99 + 2) + ax[3].set_xlim(right=chase_dur_pct99 + 2) + ax[3].set_ylim(bottom=chase_dur_pct99 + 2) - chase_chirp_count = np.array(chase_chirp_count) - chase_dur = np.array(chase_dur) - chirp_rate = chase_chirp_count / chase_dur + ax[4].plot(chase_dur_all_chirp, dt_start_all_chirp, '.') + ax[5].plot(chase_dur_all_chirp, dt_end_all_chirp, '.') + ax[4].plot([0, 60], [0, 60], '-k', lw=1) + ax[5].plot([0, 60], [0, 60], '-k', lw=1) + + n, _ = np.histogram(dt_start_all_chirp, bins=chase_dur_bins) + n = n / np.sum(n) / (chase_dur_bins[1] - chase_dur_bins[0]) + n = n / chase_dur_count_above_th + n = n / np.max(n) * chase_dur_pct99 + ax[4].barh(chase_dur_bins[:-1] + (chase_dur_bins[1] - chase_dur_bins[0])/2, n, height=(chase_dur_bins[1] - chase_dur_bins[0])*0.8, color='firebrick', alpha=0.5, zorder=2) + + n, _ = np.histogram(dt_end_all_chirp, bins=chase_dur_bins) + n = n / np.sum(n) / (chase_dur_bins[1] - chase_dur_bins[0]) + n = n / chase_dur_count_above_th + n = n / np.max(n) * chase_dur_pct99 + ax[5].barh(chase_dur_bins[:-1] + (chase_dur_bins[1] - chase_dur_bins[0])/2, n, height=(chase_dur_bins[1] - chase_dur_bins[0])*0.8, color='firebrick', alpha=0.5, zorder=2) + ax[5].invert_yaxis() + + ax[4].set_xlim(right=chase_dur_pct99+2) + ax[4].set_ylim(top=chase_dur_pct99+2) + ax[5].set_xlim(right=chase_dur_pct99+2) + ax[5].set_ylim(bottom=chase_dur_pct99+2) - 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() + ax[4].set_xlabel(r'chase$_{duration}$ [s]', fontsize=12) + ax[5].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) + ax[4].set_ylabel(r'$\Delta$t chase$_{on}$ - chirps', fontsize=12) + ax[5].set_ylabel(r'$\Delta$t chirps - chase$_{off}$', fontsize=12) + + # embed() + # quit() + + plt.show() + + # fig, ax = plt.subplots() + # n, bin_edges = np.histogram(dt_start_first_chirp[~np.isnan(dt_start_first_chirp)] / chase_dur[~np.isnan(dt_start_first_chirp)], bins = np.arange(0, 1.05, .05)) + # ax.bar(bin_edges[:-1] + (bin_edges[1]-bin_edges[0])/2, n/np.sum(n)/(bin_edges[1] - bin_edges[0]), width=0.8*(bin_edges[1]-bin_edges[0])) + # + # n, bin_edges = np.histogram(dt_end_first_chirp[~np.isnan(dt_end_first_chirp)] / chase_dur[~np.isnan(dt_end_first_chirp)], bins = np.arange(0, 1.05, .05)) + # ax.bar(bin_edges[:-1] + (bin_edges[1]-bin_edges[0])/2, n/np.sum(n)/(bin_edges[1] - bin_edges[0]), width=0.8*(bin_edges[1]-bin_edges[0])) if __name__ == '__main__': main(sys.argv[1]) diff --git a/event_time_correlations.py b/event_time_correlations.py index fa6561f..fea7fcf 100644 --- a/event_time_correlations.py +++ b/event_time_correlations.py @@ -257,7 +257,6 @@ def main(base_path): lose_chrips_centered_on_win_rises = [] lose_chrips_centered_on_win_chirp = [] lose_chirps_centered_on_lose_rises = [] - lose_chirp_count = [] win_chrips_centered_on_ag_off_t = [] win_chrips_centered_on_ag_on_t = [] @@ -265,22 +264,25 @@ def main(base_path): win_chrips_centered_on_lose_rises = [] win_chrips_centered_on_lose_chirp = [] win_chirps_centered_on_win_rises = [] - win_chirp_count = [] lose_rises_centered_on_ag_off_t = [] lose_rises_centered_on_ag_on_t = [] lose_rises_centered_on_contact_t = [] lose_rises_centered_on_win_chirps = [] - lose_rises_count = [] win_rises_centered_on_ag_off_t = [] win_rises_centered_on_ag_on_t = [] win_rises_centered_on_contact_t = [] win_rises_centered_on_lose_chirps = [] - win_rises_count = [] ag_off_centered_on_ag_on = [] - ag_count = [] + + lose_chirp_count = [] + win_chirp_count = [] + lose_rises_count = [] + win_rises_count = [] + chase_count = [] + contact_count = [] sex_win = [] sex_lose = [] @@ -366,7 +368,8 @@ def main(base_path): 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)) + chase_count.append(len(ag_on_off_t_GRID)) + contact_count.append(len(contact_t_GRID)) sex_win.append(trial['sex_win']) sex_lose.append(trial['sex_lose']) @@ -381,34 +384,63 @@ def main(base_path): conv_t = cp.arange(-max_dt, max_dt+conv_t_dt, conv_t_dt) conv_t_numpy = cp.asnumpy(conv_t) - # kde_array = kde(np.hstack(lose_chrips_centered_on_ag_off_t), conv_t, kernal_w = 1, kernal_h = 1) - for centered_times, event_counts, title in \ - [[lose_chrips_centered_on_ag_off_t, lose_chirp_count, r'chirp$_{lose}$ on chase$_{off}$'], - [lose_chrips_centered_on_ag_on_t, lose_chirp_count, r'chirp$_{lose}$ on chase$_{on}$'], - [lose_chrips_centered_on_contact_t, lose_chirp_count, r'chirp$_{lose}$ on contact'], - [lose_chrips_centered_on_win_rises, lose_chirp_count, r'chirp$_{lose}$ on rise$_{win}$'], - [lose_chrips_centered_on_win_chirp, lose_chirp_count, r'chirp$_{lose}$ on chirp$_{win}$'], - [lose_chirps_centered_on_lose_rises, lose_chirp_count, r'chirp$_{lose}$ on rises$_{lose}$'], - - [win_chrips_centered_on_ag_off_t, win_chirp_count, r'chirp$_{win}$ on chase$_{off}$'], - [win_chrips_centered_on_ag_on_t, win_chirp_count, r'chirp$_{win}$ on chase$_{on}$'], - [win_chrips_centered_on_contact_t, win_chirp_count, r'chirp$_{win}$ on contact'], - [win_chrips_centered_on_lose_rises, win_chirp_count, r'chirp$_{win}$ on rise$_{lose}$'], - [win_chrips_centered_on_lose_chirp, win_chirp_count, r'chirp$_{win}$ on chirp$_{lose}$'], - [win_chirps_centered_on_win_rises, win_chirp_count, r'chirp$_{win}$ on rises$_{win}$'], - - [lose_rises_centered_on_ag_off_t, lose_rises_count, r'rise$_{lose}$ on chase$_{off}$'], - [lose_rises_centered_on_ag_on_t, lose_rises_count, r'rise$_{lose}$ on chase$_{on}$'], - [lose_rises_centered_on_contact_t, lose_rises_count, r'rise$_{lose}$ on contact'], - [lose_rises_centered_on_win_chirps, lose_rises_count, r'rise$_{lose}$ on chirp$_{win}$'], - - [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}$'], - - [ag_off_centered_on_ag_on, ag_count, r'chase$_{off}$ on chase$_{on}$']]: + # embed() + # quit() + + # for centered_times, event_counts, title in \ + # [[lose_chrips_centered_on_ag_off_t, lose_chirp_count, r'chirp$_{lose}$ on chase$_{off}$'], + # [lose_chrips_centered_on_ag_on_t, lose_chirp_count, r'chirp$_{lose}$ on chase$_{on}$'], + # [lose_chrips_centered_on_contact_t, lose_chirp_count, r'chirp$_{lose}$ on contact'], + # [lose_chrips_centered_on_win_rises, lose_chirp_count, r'chirp$_{lose}$ on rise$_{win}$'], + # [lose_chrips_centered_on_win_chirp, lose_chirp_count, r'chirp$_{lose}$ on chirp$_{win}$'], + # [lose_chirps_centered_on_lose_rises, lose_chirp_count, r'chirp$_{lose}$ on rises$_{lose}$'], + # + # [win_chrips_centered_on_ag_off_t, win_chirp_count, r'chirp$_{win}$ on chase$_{off}$'], + # [win_chrips_centered_on_ag_on_t, win_chirp_count, r'chirp$_{win}$ on chase$_{on}$'], + # [win_chrips_centered_on_contact_t, win_chirp_count, r'chirp$_{win}$ on contact'], + # [win_chrips_centered_on_lose_rises, win_chirp_count, r'chirp$_{win}$ on rise$_{lose}$'], + # [win_chrips_centered_on_lose_chirp, win_chirp_count, r'chirp$_{win}$ on chirp$_{lose}$'], + # [win_chirps_centered_on_win_rises, win_chirp_count, r'chirp$_{win}$ on rises$_{win}$'], + # + # [lose_rises_centered_on_ag_off_t, lose_rises_count, r'rise$_{lose}$ on chase$_{off}$'], + # [lose_rises_centered_on_ag_on_t, lose_rises_count, r'rise$_{lose}$ on chase$_{on}$'], + # [lose_rises_centered_on_contact_t, lose_rises_count, r'rise$_{lose}$ on contact'], + # [lose_rises_centered_on_win_chirps, lose_rises_count, r'rise$_{lose}$ on chirp$_{win}$'], + # + # [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}$'], + # + # [ag_off_centered_on_ag_on, chase_count, r'chase$_{off}$ on chase$_{on}$']]: + + for centered_times, event_counts, title in \ + [[lose_chrips_centered_on_ag_off_t, chase_count, r'chirp$_{lose}$ on chase$_{off}$'], + [lose_chrips_centered_on_ag_on_t, chase_count, r'chirp$_{lose}$ on chase$_{on}$'], + [lose_chrips_centered_on_contact_t, contact_count, r'chirp$_{lose}$ on contact'], + [lose_chrips_centered_on_win_rises, win_rises_count, r'chirp$_{lose}$ on rise$_{win}$'], + [lose_chrips_centered_on_win_chirp, win_chirp_count, r'chirp$_{lose}$ on chirp$_{win}$'], + [lose_chirps_centered_on_lose_rises, lose_rises_count, r'chirp$_{lose}$ on rises$_{lose}$'], + + [win_chrips_centered_on_ag_off_t, chase_count, r'chirp$_{win}$ on chase$_{off}$'], + [win_chrips_centered_on_ag_on_t, chase_count, r'chirp$_{win}$ on chase$_{on}$'], + [win_chrips_centered_on_contact_t, contact_count, r'chirp$_{win}$ on contact'], + [win_chrips_centered_on_lose_rises, lose_rises_count, r'chirp$_{win}$ on rise$_{lose}$'], + [win_chrips_centered_on_lose_chirp, lose_chirp_count, r'chirp$_{win}$ on chirp$_{lose}$'], + [win_chirps_centered_on_win_rises, win_rises_count, r'chirp$_{win}$ on rises$_{win}$'], + + [lose_rises_centered_on_ag_off_t, chase_count, r'rise$_{lose}$ on chase$_{off}$'], + [lose_rises_centered_on_ag_on_t, chase_count, r'rise$_{lose}$ on chase$_{on}$'], + [lose_rises_centered_on_contact_t, contact_count, r'rise$_{lose}$ on contact'], + [lose_rises_centered_on_win_chirps, win_chirp_count, r'rise$_{lose}$ on chirp$_{win}$'], + + [win_rises_centered_on_ag_off_t, chase_count, r'rise$_{win}$ on chase$_{off}$'], + [win_rises_centered_on_ag_on_t, chase_count, r'rise$_{win}$ on chase$_{on}$'], + [win_rises_centered_on_contact_t, contact_count, r'rise$_{win}$ on contact'], + [win_rises_centered_on_lose_chirps, lose_chirp_count, r'rise$_{win}$ on chirp$_{lose}$'], + + [ag_off_centered_on_ag_on, chase_count, r'chase$_{off}$ on chase$_{on}$']]: save_str = title.replace('$', '').replace('{', '').replace('}', '').replace(' ', '_') ########################################################################################################### @@ -491,15 +523,19 @@ def main(base_path): fig = plt.figure(figsize=(20/2.54, 12/2.54)) gs = gridspec.GridSpec(1, 1, left=0.1, bottom=0.1, right=0.95, top=0.95) ax = fig.add_subplot(gs[0, 0]) - # ax.fill_between(conv_t_numpy, perm_p1/np.sum(event_counts), perm_p99/np.sum(event_counts), color='cornflowerblue', alpha=.8) - # ax.plot(conv_t_numpy, perm_p50/np.sum(event_counts), color='dodgerblue', alpha=1, lw=3) - ax.fill_between(conv_t_numpy, perm_p1/len(np.hstack(centered_times)), perm_p99/len(np.hstack(centered_times)), color='cornflowerblue', alpha=.8) - ax.plot(conv_t_numpy, perm_p50/len(np.hstack(centered_times)), color='dodgerblue', alpha=1, lw=3) - - # ax.fill_between(conv_t_numpy, jk_p1/np.sum(event_counts)/jack_pct, jk_p99/np.sum(event_counts)/jack_pct, color='tab:red', alpha=.8) - # ax.plot(conv_t_numpy, jk_p50/np.sum(event_counts)/jack_pct, color='firebrick', alpha=1, lw=3) - ax.fill_between(conv_t_numpy, jk_p1/len(np.hstack(centered_times))/jack_pct, jk_p99/len(np.hstack(centered_times))/jack_pct, color='tab:red', alpha=.8) - ax.plot(conv_t_numpy, jk_p50/len(np.hstack(centered_times))/jack_pct, color='firebrick', alpha=1, lw=3) + + # ax.fill_between(conv_t_numpy, perm_p1/len(np.hstack(centered_times)), perm_p99/len(np.hstack(centered_times)), color='cornflowerblue', alpha=.8) + # ax.plot(conv_t_numpy, perm_p50/len(np.hstack(centered_times)), color='dodgerblue', alpha=1, lw=3) + # + # ax.fill_between(conv_t_numpy, jk_p1/len(np.hstack(centered_times))/jack_pct, jk_p99/len(np.hstack(centered_times))/jack_pct, color='tab:red', alpha=.8) + # ax.plot(conv_t_numpy, jk_p50/len(np.hstack(centered_times))/jack_pct, color='firebrick', alpha=1, lw=3) + + ax.fill_between(conv_t_numpy, perm_p1/np.nansum(event_counts), perm_p99/np.nansum(event_counts), color='cornflowerblue', alpha=.8) + ax.plot(conv_t_numpy, perm_p50/np.nansum(event_counts), color='dodgerblue', alpha=1, lw=3) + + ax.fill_between(conv_t_numpy, jk_p1/np.nansum(event_counts)/jack_pct, jk_p99/np.nansum(event_counts)/jack_pct, color='tab:red', alpha=.8) + ax.plot(conv_t_numpy, jk_p50/np.nansum(event_counts)/jack_pct, color='firebrick', alpha=1, lw=3) + ax_m = ax.twinx() for enu, centered_events in enumerate(centered_times):