import numpy as np import matplotlib.pyplot as plt from IPython import embed from scipy import stats from helper_functions import fill_aifl import os # from tqdm import tqdm def connect_fish(aifl, faifl, uni_ident_list, matrix_fish_counter, jumper): ''' function that connects all ids of the channels with the next channels and can be varied in the check distance by jumper Parameters ---------- aifl: 3-D array all identity fish list; 1_D: channel; 2_D: fish number; 3_D: fish identities faifl: 2-D array false all identity fish list; 1_D: number of how many false enteries in the aifl exist 2_D: channels and fish number that are falsely connected; 4 columns: Ch, fishN0, Ch_connect, fishN1 uni_ident_list: 2-D array array of all unique identities per channel; 1_D: channel; 2_D: identities matrix_fish_counter: int counter where the new fish should be registered jumper: int defines which channel should be compared with which next channel; channel+jumper Returns ------- aifl: 3-D array updated aifl faifl 2-D array updated faifl matrix_fish_counter: int updated counter ''' for channel in range(len(uni_ident_list) - jumper): print(channel) connect_channel = channel + jumper # find for each identity the first and last time stamp in the given channel and the next # channel 0 t_Ch0 = np.empty((len(uni_ident_list[channel]), 2)) for index in range(len(uni_ident_list[channel])): id0 = uni_ident_list[channel][index] t = timeidx[channel][ident[channel] == id0] t_Ch0[index][0] = t[0] t_Ch0[index][1] = t[-1] # channel 1 t_Ch1 = np.empty((len(uni_ident_list[connect_channel]), 2)) for index in range(len(uni_ident_list[connect_channel])): id1 = uni_ident_list[connect_channel][index] t1 = timeidx[connect_channel][ident[connect_channel] == id1] t_Ch1[index][0] = t1[0] t_Ch1[index][1] = t1[-1] # parameters jitter_time = 61 tolerance_time = jitter_time*2 false_count = 0 true_count = 0 for i in range(len(t_Ch0)): for j in range(len(t_Ch1)): id0 = uni_ident_list[channel][i] t0 = timeidx[channel][ident[channel] == id0] f0 = freq[channel][ident[channel] == id0] id1 = uni_ident_list[connect_channel][j] t1 = timeidx[connect_channel][ident[connect_channel] == id1] f1 = freq[connect_channel][ident[connect_channel] == id1] t00 = t_Ch0[i][0] - jitter_time t0n = t_Ch0[i][1] + jitter_time t10 = t_Ch1[j][0] - jitter_time t1n = t_Ch1[j][1] + jitter_time # time sorting sort_it = False if (t00 <= t10) and (t00 <= t1n) and (t0n >= t1n): sort_it = True elif (t10 <= t00) and (t10 <= t0n) and (t1n >= t0n): sort_it = True elif (t00 <= t10) and (t0n >= t10) and (t0n <= t1n): sort_it = True elif (t10 <= t00) and (t1n >= t00) and (t1n <= t0n): sort_it = True else: false_count += 1 pass # frequency sorting window_times = sorted(np.array([t00, t0n, t10, t1n]))[1:3] # only where they are overlapping if sort_it: true_count += 1 if window_times[0] < 0: window_times[0] = 0.0 f0_box_min = np.median(f0[np.isclose(t0, window_times[0], atol=tolerance_time)]) f0_box_max = np.median(f0[np.isclose(t0, window_times[1], atol=tolerance_time)]) f1_box_min = np.median(f1[np.isclose(t1, window_times[0], atol=tolerance_time)]) f1_box_max = np.median(f1[np.isclose(t1, window_times[1], atol=tolerance_time)]) if f0_box_min.size > 0 and f1_box_min.size > 0: fdiff0 = abs(f0_box_min - f1_box_min) if fdiff0 <= 2: matrix_fish_counter, aifl, faifl = fill_aifl(id0, id1, aifl, channel, connect_channel, matrix_fish_counter, timeidx, freq, ident, faifl) else: continue elif f0_box_max.size > 0 and f1_box_max.size > 0: fdiff1 = abs(f0_box_max - f1_box_max) if fdiff1 <= 2: matrix_fish_counter, aifl, faifl = fill_aifl(id0, id1, aifl, channel, connect_channel, matrix_fish_counter, timeidx, freq, ident, faifl) else: continue return aifl, faifl, matrix_fish_counter if __name__ == '__main__': ################################################################################################################### # load data ################################################################################################################### # load all the data of one day filename = sorted(os.listdir('../../../data/'))[6] ident = np.load('../../../data/' + filename + '/all_ident_v.npy', allow_pickle=True) freq = np.load('../../../data/' + filename + '/all_fund_v.npy', allow_pickle=True) timeidx = np.load('../../../data/' + filename + '/all_idx_v.npy', allow_pickle=True) ################################################################################################################### # Parameters ################################################################################################################### # find me for each channel the unique identities uni_ident_list = [] for index in range(len(ident)): uni_ident = np.unique(ident[index][~np.isnan(ident[index])]) uni_ident_list.append(uni_ident) # all_identity_fish_list # dim z, y, x, dim 3, 1, 2 aifl = np.empty([16, 500, 100]) aifl.fill(np.nan) faifl = np.empty([1, 4]) faifl.fill(np.nan) matrix_fish_counter = 0 for jump in [1, 2, 3]: print('Jump ', jump) aifl, faifl, matrix_fish_counter = connect_fish(aifl, faifl, uni_ident_list, matrix_fish_counter, jump) oofl = [] counter = 0 for idx in range(len(uni_ident_list)): for j in range(len(uni_ident_list[idx])): if not np.any(aifl[idx] == uni_ident_list[idx][j]): oofl.append([idx, uni_ident_list[idx][j]]) counter = counter + 1 ################################################################################################################### # save if filename not in os.listdir('../data/'): os.mkdir('../data/'+filename) np.save('../data/' + filename + '/aifl.npy', aifl) np.save('../data/' + filename + '/faifl.npy', faifl) np.save('../data/' + filename + '/oofl.npy', oofl) embed() quit()