From b1b9d9d3f18a31c80c50df7f1f0b2fe851ae65ea Mon Sep 17 00:00:00 2001
From: sprause <sprause95@gmail.com>
Date: Mon, 23 Jan 2023 15:58:22 +0100
Subject: [PATCH] implemented bootstrapping, q5 of physical still weird

---
 code/eventchirpsplots.py | 133 +++++++++++++++++++++++++--------------
 1 file changed, 85 insertions(+), 48 deletions(-)

diff --git a/code/eventchirpsplots.py b/code/eventchirpsplots.py
index 7162387..0627f2d 100644
--- a/code/eventchirpsplots.py
+++ b/code/eventchirpsplots.py
@@ -195,64 +195,101 @@ def main(datapath: str):
     # fish2 = chirps[chirps_fish_ids == fish_ids[1]]
     # fish = [len(fish1), len(fish2)]
 
-    # Define time window for chirp around event analysis
+    # Define time window for chirps around event analysis
     time_before_event = 30
     time_after_event = 60
     dt = 0.01
     width = 1
+    time = np.arange(-time_before_event, time_after_event, dt)
+
+
+    ##### Chirps around events, all fish, one recording #####
+    # Chirps around chasing onsets
+    _, centered_chasing_onset_chirps, cc_chasing_onset_chirps = event_triggered_chirps(chasing_onsets, chirps, time_before_event, time_after_event, dt, width)
+    # Chirps around chasing offsets
+    _, centered_chasing_offset_chirps, cc_chasing_offset_chirps = event_triggered_chirps(chasing_offsets, chirps, time_before_event, time_after_event, dt, width)
+    # Chirps around physical contacts
+    _, centered_physical_chirps, cc_physical_chirps = event_triggered_chirps(physical_contacts, chirps, time_before_event, time_after_event, dt, width)
+
+    ## Shuffled chirps ##
+    nbootstrapping = 1000
+    nshuffled_chirps_onset = []
+    nshuffled_chirps_offset = []
+    nshuffled_chirps_physical = []
+
+    for i in range(nbootstrapping):
+    # Calculate interchirp intervals; add first chirp timestamp in beginning to get equal lengths
+        interchirp_intervals = np.append(np.array([chirps[0]]), np.diff(chirps))
+        np.random.shuffle(interchirp_intervals)
+        shuffled_chirps = np.cumsum(interchirp_intervals)
+        # Shuffled chasing onset chirps
+        _, _, cc_shuffled_onset_chirps = event_triggered_chirps(chasing_onsets, shuffled_chirps, time_before_event, time_after_event, dt, width)
+        nshuffled_chirps_onset.append(cc_shuffled_onset_chirps)
+        # Shuffled chasing offset chirps
+        _, _, cc_shuffled_offset_chirps = event_triggered_chirps(chasing_offsets, shuffled_chirps, time_before_event, time_after_event, dt, width)
+        nshuffled_chirps_offset.append(cc_shuffled_offset_chirps)
+        # Shuffled physical contact chirps
+        _, _, cc_shuffled_physical_chirps = event_triggered_chirps(physical_contacts, shuffled_chirps, time_before_event, time_after_event, dt, width)
+        nshuffled_chirps_physical.append(cc_shuffled_physical_chirps)
+    
+    shuffled_q5_onset, shuffled_median_onset, shuffled_q95_onset = np.percentile(nshuffled_chirps_onset, (5, 50, 95), axis=0)
+    shuffled_q5_offset, shuffled_median_offset, shuffled_q95_offset = np.percentile(nshuffled_chirps_offset, (5, 50, 95), axis=0)
+    shuffled_q5_physical, shuffled_median_physical, shuffled_q95_physical = np.percentile(nshuffled_chirps_physical, (5, 50, 95), axis=0)
+
+    embed()
+
+    # Plot all events with all shuffled
+    fig, ax = plt.subplots(1, 3, figsize=(50 / 2.54, 15 / 2.54), constrained_layout=True, sharey='all')
+    offset = [1.35]
+    ax[0].set_xlabel('Time[s]')
+    # Plot chasing onsets
+    ax[0].set_ylabel('Chirp rate [Hz]')
+    ax[0].plot(time, cc_chasing_onset_chirps, color='tab:blue', zorder=100)
+    ax0 = ax[0].twinx()
+    ax0.eventplot(np.array([centered_chasing_onset_chirps]), lineoffsets=offset, linelengths=0.1, colors=['tab:green'], alpha=0.25, zorder=-100)
+    ax0.vlines(0, 0, 1.5, 'tab:grey', 'dashed')
+    ax0.set_yticklabels([])
+    ax0.set_yticks([])
+    ax[0].fill_between(time, shuffled_q5_onset, shuffled_q95_onset, color='tab:gray', alpha=0.5)
+    ax[0].plot(time, shuffled_median_onset, color='k')
+    # Plot chasing offets
+    ax[1].set_xlabel('Time[s]')
+    ax[1].plot(time, cc_chasing_offset_chirps, color='tab:blue', zorder=100)
+    ax1 = ax[1].twinx()
+    ax1.eventplot(np.array([centered_chasing_offset_chirps]), lineoffsets=offset, linelengths=0.1, colors=['tab:purple'], alpha=0.25, zorder=-100)
+    ax1.vlines(0, 0, 1.5, 'tab:grey', 'dashed')
+    ax1.set_yticklabels([])
+    ax1.set_yticks([])
+    ax[1].fill_between(time, shuffled_q5_offset, shuffled_q95_offset, color='tab:gray', alpha=0.5)
+    ax[1].plot(time, shuffled_median_offset, color='k')
+    # Plot physical contacts
+    ax[2].set_xlabel('Time[s]')
+    ax[2].plot(time, cc_physical_chirps, color='tab:blue', zorder=100)
+    ax2 = ax[2].twinx()
+    ax2.eventplot(np.array([centered_physical_chirps]), lineoffsets=offset, linelengths=0.1, colors=['tab:red'], alpha=0.25, zorder=-100)
+    ax2.vlines(0, 0, 1.5, 'tab:grey', 'dashed')
+    ax2.set_yticklabels([])
+    ax2.set_yticks([])
+    ax[2].fill_between(time, shuffled_q5_physical, shuffled_q95_physical, color='tab:gray', alpha=0.5)
+    ax[2].plot(time, shuffled_median_physical, color='k')
+    plt.show()
+
+    embed()
+    exit()
+    
+
+
+
+
+
 
-    #### Loop crashes at concatenate in function ####
+    #### Chirps around events, only winners, one recording ####
     for i in range(len(fish_ids)):
         fish = fish_ids[i]
         chirps_temp = chirps[chirps_fish_ids == fish]
         print(fish)
 
-        ##### Chirps around events #####
-        time = np.arange(-time_before_event, time_after_event, dt)
-
-        # Chirps around chasing onsets
-        _, centered_chasing_onset_chirps, cc_chasing_onset_chirps = event_triggered_chirps(chasing_onsets, chirps_temp, time_before_event, time_after_event, dt, width)
-        # Chirps around chasing offsets
-        _, centered_chasing_offset_chirps, cc_chasing_offset_chirps = event_triggered_chirps(chasing_offsets, chirps_temp, time_before_event, time_after_event, dt, width)
-        # Chirps around physical contacts
-        _, centered_physical_chirps, cc_physical_chirps = event_triggered_chirps(physical_contacts, chirps_temp, time_before_event, time_after_event, dt, width)
-
-        fig, ax = plt.subplots(1, 3, figsize=(50 / 2.54, 15 / 2.54), constrained_layout=True, sharey='all')
-        offset = [0.25]
-        ax[0].set_xlabel('Time[s]')
-        # Plot chasing onsets
-        ax[0].set_ylabel('Chirp rate [Hz]')
-        ax[0].plot(time, cc_chasing_onset_chirps, color='tab:blue')
-        ax0 = ax[0].twinx()
-        ax0.eventplot(np.array([centered_chasing_onset_chirps]), lineoffsets=offset, linelengths=0.1, colors=['tab:green'])
-        ax0.vlines(0, 0, 1.5, 'tab:grey', 'dashed')
-        ax0.set_yticklabels([])
-        ax0.set_yticks([])
-        # Plot chasing offets
-        ax[1].set_xlabel('Time[s]')
-        ax[1].plot(time, cc_chasing_offset_chirps, color='tab:blue')
-        ax1 = ax[1].twinx()
-        ax1.eventplot(np.array([centered_chasing_offset_chirps]), lineoffsets=offset, linelengths=0.1, colors=['tab:purple'])
-        ax1.vlines(0, 0, 1.5, 'tab:grey', 'dashed')
-        ax1.set_yticklabels([])
-        ax1.set_yticks([])
-        # Plot physical contacts
-        ax[2].set_xlabel('Time[s]')
-        ax[2].plot(time, cc_physical_chirps, color='tab:blue')
-        ax2 = ax[2].twinx()
-        ax2.eventplot(np.array([centered_physical_chirps]), lineoffsets=offset, linelengths=0.1, colors=['tab:red'])
-        ax2.vlines(0, 0, 1.5, 'tab:grey', 'dashed')
-        ax2.set_yticklabels([])
-        ax2.set_yticks([])
-        plt.show()
-
-    ### Plots:
-    # 1. All recordings, all fish, all chirps
-        # One CTC, one PTC
-    # 2. All recordings, only winners
-        # One CTC, one PTC
-    # 3. All recordings, all losers
-        # One CTC, one PTC
+    #### Chirps around events, only losers, one recording ####
 
     
     embed()