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 math import os from IPython import embed import helper_functions as hf from params import * import itertools def fill_cbf_plot(matrix, x, t, i, time_point1, time_point2): matrix[i].extend(x[(t >= time_point1) & (t <= time_point2)]) return matrix def plot_changes_for_swim_through(x, y, dur, f): plt.plot(x, y) plt.title('dur: %.1f, %.1f' % (dur, f)) ax = plt.gca() ax.beautimechannelaxis() ax.timeaxis() plt.show() def chances_per_bin_filler(time_bins, x, t, cbf, cbf_counter): for idx, tb in enumerate(time_bins[:-1]): xx = x[(t >= tb) & (t <= time_bins[idx + 1])] if np.any(xx): cbf[0, idx, cbf_counter] = len(xx[xx < 0]) cbf[1, idx, cbf_counter] = len(xx[xx > 0]) cbf[2, idx, cbf_counter] = len(xx[xx == 0]) cbf_counter += 1 return cbf, cbf_counter def make_trajectory(x, y, kernel): smooth_y = np.convolve(y, kernel, 'valid') smooth_x = x[int(np.ceil(kernel_size / 2)):-int(np.floor(kernel_size / 2) - 1)] # interpolate fish_x = flat_x[np.where(flat_x == x_tickses[0])[0][0]:np.where(flat_x == x_tickses[-1])[0][0] + 1] trajectory = np.round(np.interp(fish_x, smooth_x, smooth_y)) return fish_x, trajectory if __name__ == '__main__': ################################################################################################################### # parameter and variables # plot params inch = 2.45 save_path = '../../thesis/Figures/Results/' kernel_size = 100 kernel = np.ones(kernel_size) / kernel_size c = 0 cc = 1 all_first = [] all_last = [] # time_bins in seconds time_factor = 60*60 time_bins = np.arange(0, 24 * time_factor + 1, bin_len) time_bins_recordings = np.zeros(len(time_bins)-1) # changes per bin of bin_len seconds for all fish cbf = np.full([3, len(time_bins) - 1, 166], np.nan) cbf_counter = 0 # percent roaming pr = [] swim_trough_list = [] trial_durs = [] transit_data = [] # average speed speed = [] # trajectory trajectories = [] trajec_x = [] std_trajecs = [] # for frequency vs. activity f = [] eigen = [] males = [] females = [] n = [] all_changes = [] ################################################################################################################### # load data ################################################################################################################### # load all the data of one day for index, filename_idx in enumerate([0, 1, 2, 3]): filename = sorted(os.listdir('../data/'))[filename_idx] aifl = np.load('../data/' + filename + '/aifl2.npy', allow_pickle=True) all_xticks = np.load('../data/' + filename + '/all_xtickses.npy', allow_pickle=True) all_Ctime_v = np.load('../data/' + filename + '/all_Ctime_v.npy', allow_pickle=True) all_max_ch_means = np.load('../data/' + filename + '/all_max_ch.npy', allow_pickle=True) power_means = np.load('../data/' + filename + '/power_means.npy', allow_pickle=True) freq = np.load('../data/' + filename + '/fish_freq_q10.npy', allow_pickle=True) all_hms = np.load('../data/' + filename + '/all_hms.npy', allow_pickle=True) names = np.load('../data/' + filename + '/fish_species.npy', allow_pickle=True) ############################################################################################################### # lists flat_x = np.unique(np.hstack(all_xticks)) # in which bins is this recording, fill time_bins_recordings flat_hms = np.unique(np.hstack(all_hms)) for idx, tb in enumerate(time_bins[:-1]): if np.any((flat_hms >= tb) & (flat_hms <= time_bins[idx + 1])): time_bins_recordings[idx] += 1 ############################################################################################################### # analysis of the changes and trajectories for fish_number in range(len(power_means)): if power_means[fish_number] >= -90.0: x_tickses = all_xticks[fish_number] max_ch_mean = all_max_ch_means[fish_number] # make trajectory fish_x, trajectory = make_trajectory(x_tickses, max_ch_mean, kernel) # trial_duration t_s = datetime.timedelta(np.diff([fish_x[0], fish_x[-1]])[0]) trial_dur = t_s.total_seconds() / 60 trial_durs.append(trial_dur) changes = np.sum(np.abs(np.diff(trajectory)) > 0.5) / trial_dur if changes > 6: continue # average changes per trial all_changes.append(changes) f.append(freq[fish_number, 2]) n.append(names[fish_number]) # percent roaming m = np.median(trajectory) percent = (len(trajectory[trajectory > m + 1]) + len(trajectory[trajectory < m - 1])) / len(trajectory) pr.append(percent) std_trajecs.append(np.std(trajectory)) # trajectories trajectories.append(trajectory) trajec_x.append(all_hms[fish_number]) # average speed speed.append(np.sum(np.abs(np.diff(trajectory))) / trial_dur) # activity vs. time t = all_hms[fish_number][:-1] x = np.diff(trajectory) if names[fish_number] == 'Eigenmannia': cbf, cbf_counter = chances_per_bin_filler(time_bins, x, t, cbf, cbf_counter) elif names[fish_number] == 'Apteronotus': cbf, cbf_counter = chances_per_bin_filler(time_bins, x, t, cbf, cbf_counter) # swim through first = fish_x[0] last = fish_x[-1] start15 = mdates.date2num(mdates.num2date(first) + datetime.timedelta(seconds=2 * 60)) stop15 = mdates.date2num(mdates.num2date(last) - datetime.timedelta(seconds=2 * 60)) if np.any(trajectory[fish_x < start15] >= 13) and np.any(trajectory[fish_x > stop15] <= 2): c += 1 swim_trough_list.append(True) transit_data.append(np.array([first, last, trial_dur])) # print(names[fish_number]) # plot_changes_for_swim_through(fish_x, trajectory, trial_dur, freq[fish_number, 2]) elif np.any(trajectory[fish_x < start15] <= 2) and np.any(trajectory[fish_x > stop15] >= 13): c += 1 swim_trough_list.append(True) transit_data.append(np.array([first, last, trial_dur])) # print(names[fish_number]) # plot_changes_for_swim_through(fish_x, trajectory, trial_dur, freq[fish_number, 2]) elif np.any(trajectory[fish_x < start15] <= 1) \ and np.any(trajectory[fish_x > stop15] >= 6) \ and np.any(trajectory[fish_x > stop15] <= 7): c += 1 swim_trough_list.append(True) transit_data.append(np.array([first, last, trial_dur])) # print(names[fish_number]) # plot_changes_for_swim_through(fish_x, trajectory, trial_dur, freq[fish_number, 2]) elif np.any(trajectory[fish_x < start15] >= 6) \ and np.any(trajectory[fish_x < start15] <= 7) \ and np.any(trajectory[fish_x > stop15] <= 1): c += 1 swim_trough_list.append(True) transit_data.append(np.array([first, last, trial_dur])) # print(names[fish_number]) # plot_changes_for_swim_through(fish_x, trajectory, trial_dur, freq[fish_number, 2]) elif np.any(trajectory[fish_x < start15] >= 14) \ and np.any(trajectory[fish_x > stop15] >= 8) \ and np.any(trajectory[fish_x > stop15] <= 9): c += 1 swim_trough_list.append(True) transit_data.append(np.array([first, last, trial_dur])) # print(names[fish_number]) # plot_changes_for_swim_through(fish_x, trajectory, trial_dur, freq[fish_number, 2]) elif np.any(trajectory[fish_x < start15] >= 8) \ and np.any(trajectory[fish_x < start15] <= 9) \ and np.any(trajectory[fish_x > stop15] >= 14): c += 1 swim_trough_list.append(True) transit_data.append(np.array([first, last, trial_dur])) # print(names[fish_number]) # plot_changes_for_swim_through(fish_x, trajectory, trial_dur, freq[fish_number, 2]) else: cc += 1 swim_trough_list.append(False) # np.save('../data/pr.npy', pr) # np.save('../data/stl.npy', swim_trough_list) # np.save('../data/speed.npy', speed) # np.save('../data/trial_dur.npy', trial_durs) # np.save('../data/trajectories'+str(bin_len)+'.npy', trajectories) # np.save('../data/trajec_x'+str(bin_len)+'.npy', trajec_x) # np.save('../data/std_trajec.npy', std_trajecs) # # np.save('../data/f.npy', f) # np.save('../data/all_changes.npy', all_changes) # np.save('../data/n.npy', n) # # np.save('../data/cbf_e'+str(bin_len)+'.npy', cbf_e) # np.save('../data/cbf_a'+str(bin_len)+'.npy', cbf_a) np.save('../data/cbf'+str(bin_len)+'.npy', cbf) embed() quit()