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,7 +182,301 @@ 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
|
||||
|
||||
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
|
||||
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:
|
||||
snippet_chase_dur.append(np.nan)
|
||||
|
||||
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()
|
||||
|
||||
|
||||
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 = []
|
||||
all_end_chase_event_mask = []
|
||||
all_after_chase_event_mask = []
|
||||
all_before_contact_event_mask = []
|
||||
all_after_contact_event_mask = []
|
||||
|
||||
all_pre_chase_time = []
|
||||
all_chase_time = []
|
||||
all_end_chase_time = []
|
||||
all_after_chase_time = []
|
||||
all_before_contact_time = []
|
||||
all_after_contact_time = []
|
||||
|
||||
video_trial_win_sex = []
|
||||
video_trial_lose_sex = []
|
||||
|
||||
time_tol = 5
|
||||
|
||||
for enu, contact_t, ag_on_t, ag_off_t, event_times in zip(
|
||||
np.arange(len(all_contact_t)), all_contact_t, all_ag_on_t, all_ag_off_t, all_event_t):
|
||||
|
||||
if len(ag_on_t) == 0:
|
||||
continue
|
||||
|
||||
if len(event_times) == 0:
|
||||
continue
|
||||
|
||||
pre_chase_event_mask = np.zeros_like(event_times)
|
||||
chase_event_mask = np.zeros_like(event_times)
|
||||
end_chase_event_mask = np.zeros_like(event_times)
|
||||
after_chase_event_mask = np.zeros_like(event_times)
|
||||
|
||||
video_trial_win_sex.append(win_sex[enu])
|
||||
video_trial_lose_sex.append(lose_sex[enu])
|
||||
|
||||
for chase_on_t, chase_off_t in zip(ag_on_t, ag_off_t):
|
||||
pre_chase_event_mask[(event_times >= chase_on_t - time_tol) & (event_times < chase_on_t)] = 1
|
||||
chase_event_mask[(event_times >= chase_on_t) & (event_times < chase_off_t - time_tol)] = 1
|
||||
end_chase_event_mask[(event_times >= chase_off_t - time_tol) & (event_times < chase_off_t)] = 1
|
||||
after_chase_event_mask[(event_times >= chase_off_t) & (event_times < chase_off_t + time_tol)] = 1
|
||||
|
||||
all_pre_chase_event_mask.append(pre_chase_event_mask)
|
||||
all_chase_event_mask.append(chase_event_mask)
|
||||
all_end_chase_event_mask.append(end_chase_event_mask)
|
||||
all_after_chase_event_mask.append(after_chase_event_mask)
|
||||
|
||||
all_pre_chase_time.append(len(ag_on_t) * time_tol)
|
||||
chasing_dur = (ag_off_t - ag_on_t) - time_tol
|
||||
chasing_dur[chasing_dur < 0] = 0
|
||||
all_chase_time.append(np.sum(chasing_dur))
|
||||
all_end_chase_time.append(len(ag_on_t) * time_tol)
|
||||
all_after_chase_time.append(len(ag_on_t) * time_tol)
|
||||
|
||||
before_countact_event_mask = np.zeros_like(event_times)
|
||||
after_countact_event_mask = np.zeros_like(event_times)
|
||||
for ct in contact_t:
|
||||
before_countact_event_mask[(event_times >= ct - time_tol) & (event_times < ct)] = 1
|
||||
after_countact_event_mask[(event_times >= ct) & (event_times < ct + time_tol)] = 1
|
||||
all_before_contact_event_mask.append(before_countact_event_mask)
|
||||
all_after_contact_event_mask.append(after_countact_event_mask)
|
||||
|
||||
all_before_contact_time.append(len(contact_t) * time_tol)
|
||||
all_after_contact_time.append(len(contact_t) * time_tol)
|
||||
|
||||
all_pre_chase_time = np.array(all_pre_chase_time)
|
||||
all_chase_time = np.array(all_chase_time)
|
||||
all_end_chase_time = np.array(all_end_chase_time)
|
||||
all_after_chase_time = np.array(all_after_chase_time)
|
||||
all_before_contact_time = np.array(all_before_contact_time)
|
||||
all_after_contact_time = np.array(all_after_contact_time)
|
||||
|
||||
video_trial_win_sex = np.array(video_trial_win_sex)
|
||||
video_trial_lose_sex = np.array(video_trial_lose_sex)
|
||||
|
||||
all_pre_chase_time_ratio = all_pre_chase_time / (3 * 60 * 60)
|
||||
all_chase_time_ratio = all_chase_time / (3 * 60 * 60)
|
||||
all_end_chase_time_ratio = all_end_chase_time / (3 * 60 * 60)
|
||||
all_after_chase_time_ratio = all_after_chase_time / (3 * 60 * 60)
|
||||
all_before_countact_time_ratio = all_before_contact_time / (3 * 60 * 60)
|
||||
all_after_countact_time_ratio = all_after_contact_time / (3 * 60 * 60)
|
||||
|
||||
all_pre_chase_event_ratio = np.array(list(map(lambda x: np.sum(x) / len(x), all_pre_chase_event_mask)))
|
||||
all_chase_event_ratio = np.array(list(map(lambda x: np.sum(x) / len(x), all_chase_event_mask)))
|
||||
all_end_chase_event_ratio = np.array(list(map(lambda x: np.sum(x) / len(x), all_end_chase_event_mask)))
|
||||
all_after_chase_event_ratio = np.array(list(map(lambda x: np.sum(x) / len(x), all_after_chase_event_mask)))
|
||||
all_before_countact_event_ratio = np.array(list(map(lambda x: np.sum(x) / len(x), all_before_contact_event_mask)))
|
||||
all_after_countact_event_ratio = np.array(list(map(lambda x: np.sum(x) / len(x), all_after_contact_event_mask)))
|
||||
|
||||
for x, y, name in [[all_pre_chase_event_ratio, all_pre_chase_time_ratio, 'pre chase'],
|
||||
[all_chase_event_ratio, all_chase_time_ratio, 'while chase'],
|
||||
[all_end_chase_event_ratio, all_end_chase_time_ratio, 'end chase'],
|
||||
[all_after_chase_event_ratio, all_after_chase_time_ratio, 'after chase'],
|
||||
[all_before_countact_event_ratio, all_before_countact_time_ratio, 'pre contact'],
|
||||
[all_after_countact_event_ratio, all_after_countact_time_ratio, 'post contact']]:
|
||||
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])
|
||||
ax_pie = fig.add_subplot(gs[0, 1])
|
||||
|
||||
ax.boxplot([all_pre_chase_event_ratio / all_pre_chase_time_ratio,
|
||||
all_chase_event_ratio / all_chase_time_ratio,
|
||||
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)
|
||||
|
||||
ylim = list(ax.get_ylim())
|
||||
ylim[0] = -.1 if ylim[0] < -.1 else ylim[0]
|
||||
ylim[1] = 1.1 if ylim[1] < 1.1 else ylim[1]
|
||||
##############################################################################
|
||||
for sex_w, sex_l in itertools.product(['m', 'f'], repeat=2):
|
||||
mec = 'k' if sex_w == sex_l else 'None'
|
||||
if 'lose' in event_name:
|
||||
marker = 'o'
|
||||
c = male_color if sex_l == 'm' else female_color
|
||||
elif "win" in event_name:
|
||||
marker = 'p'
|
||||
c = male_color if sex_w == 'm' else female_color
|
||||
else:
|
||||
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_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_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_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.arange(7) - 1, np.ones(7), linestyle='dotted', lw=2, color='k')
|
||||
ax.set_xlim(-0.5, 5.5)
|
||||
ax.set_ylim(ylim[0], ylim[1])
|
||||
|
||||
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.tick_params(labelsize=10)
|
||||
fig.suptitle(f'{event_name}: n={len(np.hstack(all_event_t))}')
|
||||
|
||||
###############################################
|
||||
flat_pre_chase_event_mask = np.hstack(all_pre_chase_event_mask)
|
||||
flat_chase_event_mask = np.hstack(all_chase_event_mask)
|
||||
flat_end_chase_event_mask = np.hstack(all_end_chase_event_mask)
|
||||
flat_after_chase_event_mask = np.hstack(all_after_chase_event_mask)
|
||||
flat_before_countact_event_mask = np.hstack(all_before_contact_event_mask)
|
||||
flat_after_countact_event_mask = np.hstack(all_after_contact_event_mask)
|
||||
|
||||
flat_pre_chase_event_mask[(flat_before_countact_event_mask == 1) | (flat_after_countact_event_mask == 1)] = 0
|
||||
flat_chase_event_mask[(flat_before_countact_event_mask == 1) | (flat_after_countact_event_mask == 1)] = 0
|
||||
flat_end_chase_event_mask[(flat_before_countact_event_mask == 1) | (flat_after_countact_event_mask == 1)] = 0
|
||||
flat_after_chase_event_mask[(flat_before_countact_event_mask == 1) | (flat_after_countact_event_mask == 1)] = 0
|
||||
|
||||
event_context_values = [np.sum(flat_pre_chase_event_mask) / len(flat_pre_chase_event_mask),
|
||||
np.sum(flat_chase_event_mask) / len(flat_chase_event_mask),
|
||||
np.sum(flat_end_chase_event_mask) / len(flat_end_chase_event_mask),
|
||||
np.sum(flat_after_chase_event_mask) / len(flat_after_chase_event_mask),
|
||||
np.sum(flat_before_countact_event_mask) / len(flat_before_countact_event_mask),
|
||||
np.sum(flat_after_countact_event_mask) / len(flat_after_countact_event_mask)]
|
||||
|
||||
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)]
|
||||
|
||||
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)
|
||||
|
||||
# fig, ax = plt.subplots(figsize=(12/2.54,12/2.54))
|
||||
size = 0.3
|
||||
outer_colors = ['tab:red', 'tab:orange', 'yellow', 'tab:green', 'k', 'tab:brown', 'tab:grey']
|
||||
ax_pie.pie(event_context_values, radius=1, colors=outer_colors,
|
||||
wedgeprops=dict(width=size, edgecolor='w'), startangle=90, center=(0, 1))
|
||||
ax_pie.pie(time_context_values, radius=1 - size, colors=outer_colors,
|
||||
wedgeprops=dict(width=size, edgecolor='w', alpha=.6), startangle=90, center=(0, 1))
|
||||
|
||||
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='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='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) + '%')]
|
||||
|
||||
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.close()
|
||||
# plt.show()
|
||||
|
||||
|
||||
def main(base_path):
|
||||
@ -196,8 +490,8 @@ def main(base_path):
|
||||
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]
|
||||
|
||||
### data processing #######################
|
||||
all_rise_times_lose = []
|
||||
all_rise_times_win = []
|
||||
all_chirp_times_lose = []
|
||||
@ -268,11 +562,13 @@ def main(base_path):
|
||||
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}$')
|
||||
@ -282,232 +578,72 @@ def main(base_path):
|
||||
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}$']):
|
||||
print('')
|
||||
all_pre_chase_event_mask = []
|
||||
all_chase_event_mask = []
|
||||
all_end_chase_event_mask = []
|
||||
all_after_chase_event_mask = []
|
||||
all_before_contact_event_mask = []
|
||||
all_after_contact_event_mask = []
|
||||
|
||||
all_pre_chase_time = []
|
||||
all_chase_time = []
|
||||
all_end_chase_time = []
|
||||
all_after_chase_time = []
|
||||
all_before_contact_time = []
|
||||
all_after_contact_time = []
|
||||
|
||||
video_trial_win_sex = []
|
||||
video_trial_lose_sex = []
|
||||
|
||||
time_tol = 5
|
||||
|
||||
for enu, contact_t, ag_on_t, ag_off_t, event_times in zip(
|
||||
np.arange(len(all_contact_t)), all_contact_t, all_ag_on_t, all_ag_off_t, all_event_t):
|
||||
|
||||
if len(ag_on_t) == 0:
|
||||
continue
|
||||
|
||||
if len(event_times) == 0:
|
||||
continue
|
||||
|
||||
pre_chase_event_mask = np.zeros_like(event_times)
|
||||
chase_event_mask = np.zeros_like(event_times)
|
||||
end_chase_event_mask = np.zeros_like(event_times)
|
||||
after_chase_event_mask = np.zeros_like(event_times)
|
||||
|
||||
video_trial_win_sex.append(win_sex[enu])
|
||||
video_trial_lose_sex.append(lose_sex[enu])
|
||||
|
||||
for chase_on_t, chase_off_t in zip(ag_on_t, ag_off_t):
|
||||
pre_chase_event_mask[(event_times >= chase_on_t - time_tol) & (event_times < chase_on_t)] = 1
|
||||
chase_event_mask[(event_times >= chase_on_t) & (event_times < chase_off_t - time_tol)] = 1
|
||||
end_chase_event_mask[(event_times >= chase_off_t - time_tol) & (event_times < chase_off_t)] = 1
|
||||
after_chase_event_mask[(event_times >= chase_off_t) & (event_times < chase_off_t + time_tol)] = 1
|
||||
|
||||
all_pre_chase_event_mask.append(pre_chase_event_mask)
|
||||
all_chase_event_mask.append(chase_event_mask)
|
||||
all_end_chase_event_mask.append(end_chase_event_mask)
|
||||
all_after_chase_event_mask.append(after_chase_event_mask)
|
||||
|
||||
all_pre_chase_time.append(len(ag_on_t) * time_tol)
|
||||
chasing_dur = (ag_off_t - ag_on_t) - time_tol
|
||||
chasing_dur[chasing_dur < 0 ] = 0
|
||||
all_chase_time.append(np.sum(chasing_dur))
|
||||
all_end_chase_time.append(len(ag_on_t) * time_tol)
|
||||
all_after_chase_time.append(len(ag_on_t) * time_tol)
|
||||
|
||||
before_countact_event_mask = np.zeros_like(event_times)
|
||||
after_countact_event_mask = np.zeros_like(event_times)
|
||||
for ct in contact_t:
|
||||
before_countact_event_mask[(event_times >= ct-time_tol) & (event_times < ct)] = 1
|
||||
after_countact_event_mask[(event_times >= ct) & (event_times < ct+time_tol)] = 1
|
||||
all_before_contact_event_mask.append(before_countact_event_mask)
|
||||
all_after_contact_event_mask.append(after_countact_event_mask)
|
||||
|
||||
all_before_contact_time.append(len(contact_t) * time_tol)
|
||||
all_after_contact_time.append(len(contact_t) * time_tol)
|
||||
|
||||
all_pre_chase_time = np.array(all_pre_chase_time)
|
||||
all_chase_time = np.array(all_chase_time)
|
||||
all_end_chase_time = np.array(all_end_chase_time)
|
||||
all_after_chase_time = np.array(all_after_chase_time)
|
||||
all_before_contact_time = np.array(all_before_contact_time)
|
||||
all_after_contact_time = np.array(all_after_contact_time)
|
||||
|
||||
video_trial_win_sex = np.array(video_trial_win_sex)
|
||||
video_trial_lose_sex = np.array(video_trial_lose_sex)
|
||||
|
||||
all_pre_chase_time_ratio = all_pre_chase_time / (3*60*60)
|
||||
all_chase_time_ratio = all_chase_time / (3*60*60)
|
||||
all_end_chase_time_ratio = all_end_chase_time / (3*60*60)
|
||||
all_after_chase_time_ratio = all_after_chase_time / (3*60*60)
|
||||
all_before_countact_time_ratio = all_before_contact_time / (3*60*60)
|
||||
all_after_countact_time_ratio = all_after_contact_time / (3*60*60)
|
||||
|
||||
all_pre_chase_event_ratio = np.array(list(map(lambda x: np.sum(x)/len(x), all_pre_chase_event_mask)))
|
||||
all_chase_event_ratio = np.array(list(map(lambda x: np.sum(x)/len(x), all_chase_event_mask)))
|
||||
all_end_chase_event_ratio = np.array(list(map(lambda x: np.sum(x)/len(x), all_end_chase_event_mask)))
|
||||
all_after_chase_event_ratio = np.array(list(map(lambda x: np.sum(x)/len(x), all_after_chase_event_mask)))
|
||||
all_before_countact_event_ratio = np.array(list(map(lambda x: np.sum(x)/len(x), all_before_contact_event_mask)))
|
||||
all_after_countact_event_ratio = np.array(list(map(lambda x: np.sum(x)/len(x), all_after_contact_event_mask)))
|
||||
|
||||
for x, y, name in [[all_pre_chase_event_ratio, all_pre_chase_time_ratio, 'pre chase'],
|
||||
[all_chase_event_ratio, all_chase_time_ratio, 'while chase'],
|
||||
[all_end_chase_event_ratio, all_end_chase_time_ratio, 'end chase'],
|
||||
[all_after_chase_event_ratio, all_after_chase_time_ratio, 'after chase'],
|
||||
[all_before_countact_event_ratio, all_before_countact_time_ratio, 'pre contact'],
|
||||
[all_after_countact_event_ratio, all_after_countact_time_ratio, 'post contact']]:
|
||||
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])
|
||||
ax_pie = fig.add_subplot(gs[0, 1])
|
||||
|
||||
ax.boxplot([all_pre_chase_event_ratio/all_pre_chase_time_ratio,
|
||||
all_chase_event_ratio/all_chase_time_ratio,
|
||||
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)
|
||||
|
||||
ylim = list(ax.get_ylim())
|
||||
ylim[0] = -.1 if ylim[0] < -.1 else ylim[0]
|
||||
ylim[1] = 1.1 if ylim[1] < 1.1 else ylim[1]
|
||||
##############################################################################
|
||||
for sex_w, sex_l in itertools.product(['m', 'f'], repeat=2):
|
||||
mec = 'k' if sex_w == sex_l else 'None'
|
||||
if 'lose' in event_name:
|
||||
marker='o'
|
||||
c = male_color if sex_l == 'm' else female_color
|
||||
elif "win" in event_name:
|
||||
marker='p'
|
||||
c = male_color if sex_w == 'm' else female_color
|
||||
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:
|
||||
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_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_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_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')
|
||||
ax.set_xlim(-0.5, 5.5)
|
||||
ax.set_ylim(ylim[0], ylim[1])
|
||||
|
||||
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.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)
|
||||
flat_chase_event_mask = np.hstack(all_chase_event_mask)
|
||||
flat_end_chase_event_mask = np.hstack(all_end_chase_event_mask)
|
||||
flat_after_chase_event_mask = np.hstack(all_after_chase_event_mask)
|
||||
flat_before_countact_event_mask = np.hstack(all_before_contact_event_mask)
|
||||
flat_after_countact_event_mask = np.hstack(all_after_contact_event_mask)
|
||||
|
||||
flat_pre_chase_event_mask[(flat_before_countact_event_mask == 1) | (flat_after_countact_event_mask == 1)] = 0
|
||||
flat_chase_event_mask[(flat_before_countact_event_mask == 1) | (flat_after_countact_event_mask == 1)] = 0
|
||||
flat_end_chase_event_mask[(flat_before_countact_event_mask == 1) | (flat_after_countact_event_mask == 1)] = 0
|
||||
flat_after_chase_event_mask[(flat_before_countact_event_mask == 1) | (flat_after_countact_event_mask == 1)] = 0
|
||||
|
||||
event_context_values = [np.sum(flat_pre_chase_event_mask) / len(flat_pre_chase_event_mask),
|
||||
np.sum(flat_chase_event_mask) / len(flat_chase_event_mask),
|
||||
np.sum(flat_end_chase_event_mask) / len(flat_end_chase_event_mask),
|
||||
np.sum(flat_after_chase_event_mask) / len(flat_after_chase_event_mask),
|
||||
np.sum(flat_before_countact_event_mask) / len(flat_before_countact_event_mask),
|
||||
np.sum(flat_after_countact_event_mask) / len(flat_after_countact_event_mask)]
|
||||
|
||||
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)]
|
||||
|
||||
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)
|
||||
|
||||
# fig, ax = plt.subplots(figsize=(12/2.54,12/2.54))
|
||||
size = 0.3
|
||||
outer_colors = ['tab:red', 'tab:orange', 'yellow', 'tab:green', 'k','tab:brown', 'tab:grey']
|
||||
ax_pie.pie(event_context_values, radius=1, colors=outer_colors,
|
||||
wedgeprops=dict(width=size, edgecolor='w'), startangle=90, center=(0, 1))
|
||||
ax_pie.pie(time_context_values, radius=1-size, colors=outer_colors,
|
||||
wedgeprops=dict(width=size, edgecolor='w', alpha=.6), startangle=90, center=(0, 1))
|
||||
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')
|
||||
|
||||
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='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='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) + '%')]
|
||||
|
||||
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.close()
|
||||
# plt.show()
|
||||
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__':
|
||||
|
@ -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