diff --git a/code/eventchirpsplots.py b/code/eventchirpsplots.py index 0627f2d..d638a31 100644 --- a/code/eventchirpsplots.py +++ b/code/eventchirpsplots.py @@ -1,6 +1,7 @@ import os import numpy as np +import pandas as pd import matplotlib.pyplot as plt from IPython import embed @@ -182,10 +183,10 @@ def main(datapath: str): duration = offset - onset chasing_durations.append(duration) - fig, ax = plt.subplots() - ax.boxplot(chasing_durations) - plt.show() - plt.close() + # fig, ax = plt.subplots() + # ax.boxplot(chasing_durations) + # plt.show() + # plt.close() # Get fish ids fish_ids = np.unique(chirps_fish_ids) @@ -217,73 +218,150 @@ def main(datapath: str): 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) + # 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() + # 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) # Plot all events with all shuffled - fig, ax = plt.subplots(1, 3, figsize=(50 / 2.54, 15 / 2.54), constrained_layout=True, sharey='all') + # 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() + # plt.close() + + + + #### Chirps around events, winner VS loser, one recording #### + # Load file with fish ids and winner/loser info + meta = pd.read_csv('../data/mount_data/order_meta.csv') + current_recording = meta[meta.index == 43] + fish1 = current_recording['rec_id1'].values + fish2 = current_recording['rec_id2'].values + # Implement check if fish_ids from meta and chirp detection are the same??? + winner = current_recording['winner'].values + + if winner == fish1: + loser = fish2 + elif winner == fish2: + loser = fish1 + + winner_chirps = chirps[chirps_fish_ids == winner] + loser_chirps = chirps[chirps_fish_ids == loser] + + # Event triggered winner chirps + _, winner_centered_onset, winner_cc_onset = event_triggered_chirps(chasing_onsets, winner_chirps, time_before_event, time_after_event, dt, width) + _, winner_centered_offset, winner_cc_offset = event_triggered_chirps(chasing_offsets, winner_chirps, time_before_event, time_after_event, dt, width) + _, winner_centered_physical, winner_cc_physical = event_triggered_chirps(physical_contacts, winner_chirps, time_before_event, time_after_event, dt, width) + + # Event triggered loser chirps + _, loser_centered_onset, loser_cc_onset = event_triggered_chirps(chasing_onsets, loser_chirps, time_before_event, time_after_event, dt, width) + _, loser_centered_offset, loser_cc_offset = event_triggered_chirps(chasing_offsets, loser_chirps, time_before_event, time_after_event, dt, width) + _, loser_centered_physical, loser_cc_physical = event_triggered_chirps(physical_contacts, loser_chirps, time_before_event, time_after_event, dt, width) + + fig, ax = plt.subplots(2, 3, figsize=(50 / 2.54, 15 / 2.54), constrained_layout=True, sharey='row') 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) + ax[1][0].set_xlabel('Time[s]') + ax[1][1].set_xlabel('Time[s]') + ax[1][2].set_xlabel('Time[s]') + # Plot winner chasing onsets + ax[0][0].set_ylabel('Chirp rate [Hz]') + ax[0][0].plot(time, winner_cc_onset, color='tab:blue', zorder=100) + ax0 = ax[0][0].twinx() + ax0.eventplot(np.array([winner_centered_onset]), lineoffsets=offset, linelengths=0.1, colors=['tab:green'], alpha=0.25, zorder=-100) + ax0.set_ylabel('Event') 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) + # Plot winner chasing offets + ax[0][1].plot(time, winner_cc_offset, color='tab:blue', zorder=100) + ax1 = ax[0][1].twinx() + ax1.eventplot(np.array([winner_centered_offset]), 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) + # Plot winner physical contacts + ax[0][2].plot(time, winner_cc_physical, color='tab:blue', zorder=100) + ax2 = ax[0][2].twinx() + ax2.eventplot(np.array([winner_centered_physical]), 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') + # Plot loser chasing onsets + ax[1][0].set_ylabel('Chirp rate [Hz]') + ax[1][0].plot(time, loser_cc_onset, color='tab:blue', zorder=100) + ax3 = ax[1][0].twinx() + ax3.eventplot(np.array([loser_centered_onset]), lineoffsets=offset, linelengths=0.1, colors=['tab:green'], alpha=0.25, zorder=-100) + ax3.vlines(0, 0, 1.5, 'tab:grey', 'dashed') + ax3.set_yticklabels([]) + ax3.set_yticks([]) + # Plot loser chasing offsets + ax[1][1].plot(time, loser_cc_offset, color='tab:blue', zorder=100) + ax4 = ax[1][1].twinx() + ax4.eventplot(np.array([loser_centered_offset]), lineoffsets=offset, linelengths=0.1, colors=['tab:purple'], alpha=0.25, zorder=-100) + ax4.vlines(0, 0, 1.5, 'tab:grey', 'dashed') + ax4.set_yticklabels([]) + ax4.set_yticks([]) + # Plot loser physical contacts + ax[1][2].plot(time, loser_cc_physical, color='tab:blue', zorder=100) + ax5 = ax[1][2].twinx() + ax5.eventplot(np.array([loser_centered_physical]), lineoffsets=offset, linelengths=0.1, colors=['tab:red'], alpha=0.25, zorder=-100) + ax5.vlines(0, 0, 1.5, 'tab:grey', 'dashed') + ax5.set_yticklabels([]) + ax5.set_yticks([]) plt.show() + plt.close() + embed() exit() - - - - - + - #### 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] @@ -291,10 +369,6 @@ def main(datapath: str): #### Chirps around events, only losers, one recording #### - - embed() - exit() - if __name__ == '__main__':