line_tracking_of_fish_movement/plot_direction.py

150 lines
6.4 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.colors as mcolors
import matplotlib.gridspec as gridspec
from IPython import embed
from scipy import stats
import os
from IPython import embed
import helper_functions as hf
from params import *
import itertools
from statisitic_functions import *
def get_recording_number_in_time_bins(time_bins):
"""
Calculates the number of the recordings in the time bins
:param time_bins: numpy array with borders of the time bins
:return: time_bins_recording: numpy array with the number of recordings to that specific time bin
"""
# variables
time_bins_recordings = np.zeros(len(time_bins) - 1)
# load data
for index, filename_idx in enumerate([0, 1, 2, 3]):
filename = sorted(os.listdir('../data/'))[filename_idx]
time_points = np.load('../data/' + filename + '/all_hms.npy', allow_pickle=True)
# in which bins is this recording, fill time_bins_recordings
unique_time_points = np.unique(np.hstack(time_points))
for idx, tb in enumerate(time_bins[:-1]):
if np.any((unique_time_points >= tb) & (unique_time_points <= time_bins[idx + 1])):
time_bins_recordings[idx] += 1
return time_bins_recordings
if __name__ == '__main__':
###################################################################################################################
# parameter and variables
# plot params
inch = 2.45
save_path = '../../thesis/Figures/Results/'
kernel_size = 120
c = 0
cc = 1
all_changes = []
all_first = []
all_last = []
# time_bins 5 min
time_factor = 60 * 60
time_bins = np.arange(0, 24 * time_factor + 1, bin_len)
# changes per bin of 5min for all fish
cbf = np.full([3, len(time_bins) - 1, 300], np.nan)
cbf_counter = 0
# kernel
kernel = np.ones(kernel_size) / kernel_size
###################################################################################################################
# load data
###################################################################################################################
# load all the data of one day
cbf = np.load('../data/cbf5.npy', allow_pickle=True)
# embed()
# quit()
N_rec_time_bins = get_recording_number_in_time_bins(time_bins)
###################################################################################################################
# direction and act plot
no = np.full(len(time_bins) - 1, np.nan)
down = np.full(len(time_bins) - 1, np.nan)
up = np.full(len(time_bins) - 1, np.nan)
for i in range(len(up)):
up[i] = np.nansum(cbf[1, i]) # * (60/bin_len)
down[i] = np.nansum(cbf[0, i]) # * (60/bin_len)
no[i] = np.nansum(cbf[2, i]) # * (60/bin_len)
conv_direct = np.convolve(up - down, kernel, 'same')
conv_up = np.convolve(up, kernel, 'same')
conv_down = np.convolve(down, kernel, 'same')
conv_N_rec_tb = np.convolve(N_rec_time_bins, kernel, 'same')
# rolled time axis for nicer plot midnight in the middle start noon
rolled_time = np.roll(time_bins[:-1] / time_factor, int(len(time_bins[:-1]) / 2))
rolled_up = np.roll(up / N_rec_time_bins, int(len(time_bins[:-1]) / 2))
rolled_down = np.roll(down * -1 / N_rec_time_bins, int(len(time_bins[:-1]) / 2))
rolled_conv_dir = np.roll(conv_direct / conv_N_rec_tb, int(len(time_bins[:-1]) / 2))
rolled_conv_up = np.roll(conv_up / conv_N_rec_tb, int(len(time_bins[:-1]) / 2))
rolled_conv_down = np.roll(conv_down * -1 / conv_N_rec_tb, int(len(time_bins[:-1]) / 2))
###################################################################################################################
# time zones
time_edges = np.array([4.5, 6.5, 16.5, 18.5]) * time_factor
day = (time_bins[:-1] >= time_edges[1]) & (time_bins[:-1] <= time_edges[2])
dusk = (time_bins[:-1] >= time_edges[2]) & (time_bins[:-1] <= time_edges[3])
night = (time_bins[:-1] <= time_edges[0]) | (time_bins[:-1] >= time_edges[3])
dawn = (time_bins[:-1] >= time_edges[0]) & (time_bins[:-1] <= time_edges[1])
###################################################################################################################
# activity up down over time
fig2, ax2 = plt.subplots(1, 1, figsize=(15 / inch, 8 / inch))
fig2.subplots_adjust(left=0.1, bottom=0.15, right=0.95, top=0.98)
ax2.plot(rolled_up, color=color2[2])
ax2.plot(rolled_down, color=color2[3])
ax2.plot(rolled_conv_dir, 'k', lw=3)
ax2.plot(rolled_conv_up, color=color2[0], lw=3, label='upstream')
ax2.plot(rolled_conv_down, color=color2[1], lw=3, label='downstream')
ax2.plot([16.5*time_factor/5, 6.5*time_factor/bin_len], [5.5, 5.5], color=color_diffdays[0], lw=7)
ax2.plot([16.5*time_factor/5, 18.5*time_factor/bin_len], [5.5, 5.5], color=color_diffdays[3], lw=7)
ax2.plot([4.5*time_factor/5, 6.5*time_factor/bin_len], [5.5, 5.5], color=color_diffdays[3], lw=7)
ax2.set_xlim([0, 24 * time_factor / bin_len])
ax2.set_xticks(np.arange(0, 25, 3) * time_factor / bin_len)
ax2.set_xticklabels(['12:00', '15:00', '18:00', '21:00', '00:00', '03:00', '06:00', '09:00', '12:00'])
ax2.make_nice_ax()
# ax2.set_yticks([-7., -5., -2.5, 0, 2.5, 5., 7.])
ax2.set_xlabel('Time', fontsize=fs)
ax2.set_ylabel('Activity [Changes/ ' + str(bin_len) + ' s]', fontsize=fs)
plt.legend()
# fig2.savefig(save_path+'activ_updown_time.png')
fig2.savefig(save_path + 'activ_updown_time.pdf')
fig2.savefig(save_path_pres + 'activ_updown_time.pdf')
plt.show()
###################################################################################################################
# statistics
# print('night down: ', np.nanmedian(conv_down[night]/conv_N_rec_tb[night]))
# print('day down: ', np.nanmedian(conv_down[day]/conv_N_rec_tb[day]))
print('night', stats.pearsonr(conv_down[night]/conv_N_rec_tb[night], conv_up[night]/conv_N_rec_tb[night]))
print('day', stats.pearsonr(conv_down[day]/conv_N_rec_tb[day], conv_up[day]/conv_N_rec_tb[day]))
print('dusk', stats.pearsonr(conv_down[dusk]/conv_N_rec_tb[dusk], conv_up[dusk]/conv_N_rec_tb[dusk]))
print('dawn', stats.pearsonr(conv_down[dawn]/conv_N_rec_tb[dawn], conv_up[dawn]/conv_N_rec_tb[dawn]))
# print('night up: ', np.nanmedian(conv_up[night]/conv_N_rec_tb[night]))
# print('day up: ', np.nanmedian(conv_up[day]/conv_N_rec_tb[day]))
embed()
quit()