diff --git a/code/eventchirpsplots.py b/code/eventchirpsplots.py index 986910a..e1de80d 100644 --- a/code/eventchirpsplots.py +++ b/code/eventchirpsplots.py @@ -198,7 +198,8 @@ def main(datapath: str): time_before_event = 30 time_after_event = 60 dt = 0.01 - width = 1.5 # width of kernel, currently gaussian kernel + width = 1.5 # width of kernel for all recordings, currently gaussian kernel + recording_width = 1 # width of kernel for each recording time = np.arange(-time_before_event, time_after_event, dt) ##### Chirps around events, all fish, all recordings ##### @@ -220,11 +221,11 @@ def main(datapath: str): physical_contacts = nrecording_physicals[i] # Chirps around chasing onsets - _, centered_chasing_onset_chirps, _ = event_triggered_chirps(chasing_onsets, chirps, time_before_event, time_after_event, dt, width) + _, centered_chasing_onset_chirps, cc_chasing_onset_chirps = event_triggered_chirps(chasing_onsets, chirps, time_before_event, time_after_event, dt, recording_width) # Chirps around chasing offsets - _, centered_chasing_offset_chirps, _ = event_triggered_chirps(chasing_offsets, chirps, time_before_event, time_after_event, dt, width) + _, centered_chasing_offset_chirps, cc_chasing_offset_chirps = event_triggered_chirps(chasing_offsets, chirps, time_before_event, time_after_event, dt, recording_width) # Chirps around physical contacts - _, centered_physical_chirps, _ = event_triggered_chirps(physical_contacts, chirps, time_before_event, time_after_event, dt, width) + _, centered_physical_chirps, cc_physical_chirps = event_triggered_chirps(physical_contacts, chirps, time_before_event, time_after_event, dt, recording_width) nrecording_centered_onset_chirps.append(centered_chasing_onset_chirps) nrecording_centered_offset_chirps.append(centered_chasing_offset_chirps) @@ -235,20 +236,73 @@ def main(datapath: str): nshuffled_offset_chirps = [] nshuffled_physical_chirps = [] - for i in tqdm(range(nbootstrapping)): + for j in tqdm(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) + _, _, cc_shuffled_onset_chirps = event_triggered_chirps(chasing_onsets, shuffled_chirps, time_before_event, time_after_event, dt, recording_width) nshuffled_onset_chirps.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) + _, _, cc_shuffled_offset_chirps = event_triggered_chirps(chasing_offsets, shuffled_chirps, time_before_event, time_after_event, dt, recording_width) nshuffled_offset_chirps.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) + _, _, cc_shuffled_physical_chirps = event_triggered_chirps(physical_contacts, shuffled_chirps, time_before_event, time_after_event, dt, recording_width) nshuffled_physical_chirps.append(cc_shuffled_physical_chirps) + + rec_shuffled_q5_onset, rec_shuffled_median_onset, rec_shuffled_q95_onset = np.percentile( + nshuffled_onset_chirps, (5, 50, 95), axis=0) + rec_shuffled_q5_offset, rec_shuffled_median_offset, rec_shuffled_q95_offset = np.percentile( + nshuffled_offset_chirps, (5, 50, 95), axis=0) + rec_shuffled_q5_physical, rec_shuffled_median_physical, rec_shuffled_q95_physical = np.percentile( + nshuffled_physical_chirps, (5, 50, 95), axis=0) + + #### Recording plots #### + fig, ax = plt.subplots(1, 3, figsize=(28*ps.cm, 16*ps.cm, ), constrained_layout=True, sharey='all') + 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=ps.yellow, zorder=2) + ax0 = ax[0].twinx() + ax0.eventplot(centered_chasing_onset_chirps, linelengths=0.2, colors=ps.gray, alpha=0.25, zorder=1) + ax0.vlines(0, 0, 1.5, ps.white, 'dashed') + ax[0].set_zorder(ax0.get_zorder()+1) + ax[0].patch.set_visible(False) + ax0.set_yticklabels([]) + ax0.set_yticks([]) + ax[0].fill_between(time, rec_shuffled_q5_onset, rec_shuffled_q95_onset, color=ps.gray, alpha=0.5) + ax[0].plot(time, rec_shuffled_median_onset, color=ps.black) + + # Plot chasing offets + ax[1].set_xlabel('Time[s]') + ax[1].plot(time, cc_chasing_offset_chirps, color=ps.orange, zorder=2) + ax1 = ax[1].twinx() + ax1.eventplot(centered_chasing_offset_chirps, linelengths=0.2, colors=ps.gray, alpha=0.25, zorder=1) + ax1.vlines(0, 0, 1.5, ps.white, 'dashed') + ax[1].set_zorder(ax1.get_zorder()+1) + ax[1].patch.set_visible(False) + ax1.set_yticklabels([]) + ax1.set_yticks([]) + ax[1].fill_between(time, rec_shuffled_q5_offset, rec_shuffled_q95_offset, color=ps.gray, alpha=0.5) + ax[1].plot(time, rec_shuffled_median_offset, color=ps.black) + + # Plot physical contacts + ax[2].set_xlabel('Time[s]') + ax[2].plot(time, cc_physical_chirps, color=ps.maroon, zorder=2) + ax2 = ax[2].twinx() + ax2.eventplot(centered_physical_chirps, linelengths=0.2, colors=ps.gray, alpha=0.25, zorder=1) + ax2.vlines(0, 0, 1.5, ps.white, 'dashed') + ax[2].set_zorder(ax2.get_zorder()+1) + ax[2].patch.set_visible(False) + ax2.set_yticklabels([]) + ax2.set_yticks([]) + ax[2].fill_between(time, rec_shuffled_q5_physical, rec_shuffled_q95_physical, color=ps.gray, alpha=0.5) + ax[2].plot(time, rec_shuffled_median_physical, ps.black) + fig.suptitle(f'Recording: {i}') + plt.show() + # plt.close() nrecording_shuffled_convolved_onset_chirps.append(nshuffled_onset_chirps) nrecording_shuffled_convolved_offset_chirps.append(nshuffled_offset_chirps) @@ -290,45 +344,49 @@ def main(datapath: str): fig, ax = plt.subplots(1, 3, figsize=(28*ps.cm, 16*ps.cm, ), constrained_layout=True, sharey='all') # offsets = np.arange(1,28,1) ax[0].set_xlabel('Time[s]') + # Plot chasing onsets ax[0].set_ylabel('Chirp rate [Hz]') ax[0].plot(time, all_onset_chirps_convolved, color=ps.yellow, zorder=2) ax0 = ax[0].twinx() nrecording_centered_onset_chirps = np.asarray(nrecording_centered_onset_chirps, dtype=object) ax0.eventplot(np.array(nrecording_centered_onset_chirps), linelengths=0.5, colors=ps.gray, alpha=0.25, zorder=1) - ax0.vlines(0, 0, 1.5, ps.black, 'dashed') + ax0.vlines(0, 0, 1.5, ps.white, 'dashed') ax[0].set_zorder(ax0.get_zorder()+1) ax[0].patch.set_visible(False) ax0.set_yticklabels([]) ax0.set_yticks([]) ax[0].fill_between(time, shuffled_q5_onset, shuffled_q95_onset, color=ps.gray, alpha=0.5) ax[0].plot(time, shuffled_median_onset, color=ps.black) + # Plot chasing offets ax[1].set_xlabel('Time[s]') ax[1].plot(time, all_offset_chirps_convolved, color=ps.orange, zorder=2) ax1 = ax[1].twinx() nrecording_centered_offset_chirps = np.asarray(nrecording_centered_offset_chirps, dtype=object) ax1.eventplot(np.array(nrecording_centered_offset_chirps), linelengths=0.5, colors=ps.gray, alpha=0.25, zorder=1) - ax1.vlines(0, 0, 1.5, ps.black, 'dashed') + ax1.vlines(0, 0, 1.5, ps.white, 'dashed') ax[1].set_zorder(ax1.get_zorder()+1) ax[1].patch.set_visible(False) ax1.set_yticklabels([]) ax1.set_yticks([]) ax[1].fill_between(time, shuffled_q5_offset, shuffled_q95_offset, color=ps.gray, alpha=0.5) ax[1].plot(time, shuffled_median_offset, color=ps.black) + # Plot physical contacts ax[2].set_xlabel('Time[s]') ax[2].plot(time, all_physical_chirps_convolved, color=ps.maroon, zorder=2) ax2 = ax[2].twinx() nrecording_centered_physical_chirps = np.asarray(nrecording_centered_physical_chirps, dtype=object) ax2.eventplot(np.array(nrecording_centered_physical_chirps), linelengths=0.5, colors=ps.gray, alpha=0.25, zorder=1) - ax2.vlines(0, 0, 1.5, ps.black, 'dashed') + ax2.vlines(0, 0, 1.5, ps.white, 'dashed') ax[2].set_zorder(ax2.get_zorder()+1) ax[2].patch.set_visible(False) ax2.set_yticklabels([]) ax2.set_yticks([]) ax[2].fill_between(time, shuffled_q5_physical, shuffled_q95_physical, color=ps.gray, alpha=0.5) ax[2].plot(time, shuffled_median_physical, ps.black) + fig.suptitle('All recordings') plt.show() # plt.close() @@ -351,7 +409,7 @@ def main(datapath: str): # fish2 = chirps[chirps_fish_ids == fish_ids[1]] # fish = [len(fish1), len(fish2)] - # Concolution over all recordings + # Convolution over all recordings # Rasterplot for each recording