for trial in range(len(session_trial[day])): beh_file_name = 'mouse_' + f'{mouse}' + '_session_' + f'{session}' + '_trial_' + \ f'{session_trial[day][trial]}' + '_likelihood_0.75.npy' beh_path = behaviour_path + beh_file_name if not os.path.isfile(beh_path): print('ERROR: Behaviour file not found:' + beh_path) else: tracking = np.load(beh_path) init_trial = int(timeline[trial * 2]) end_trial = int(timeline[trial * 2 + 1]) duration = np.min((tracking.shape[0], end_trial - init_trial)) cm = np.zeros((tracking.shape[0], 2)) body_part = 1 x = tracking[:, body_part * 2] y = tracking[:, body_part * 2 + 1] x_new, y_new = beh_func.interpolate_positions(x, y) cm[:, 0] = signal.medfilt(x_new, 7) cm[:, 1] = signal.medfilt(y_new, 7) cm_vector[init_trial:init_trial + cm.shape[0], :] = cm[ 0:cm_vector[init_trial:init_trial + cm.shape[0], :].shape[0], :] output_tracking_file = 'mouse_' + f'{mouse}' + '_session_' + f'{session}' + '_day_' + \ f'{day + 1}' + '_likelihood_0.75.npy' output_tracking_path = category_path + output_tracking_file np.save(output_tracking_path, cm_vector) day = day + 1
beh_file_name = 'mouse_' + f'{mouse}' + '_session_' + f'{session}' + '_trial_' + \ f'{session_trial[day][trial]}' + '_likelihood_0.75.npy' beh_path = behaviour_path + beh_file_name if not os.path.isfile(beh_path): print('ERROR: Behaviour file not found:' + beh_path) else: tracking = np.load(beh_path) init_trial = int(timeline[trial * 2]) end_trial = int(timeline[trial * 2 + 1]) duration = np.min((tracking.shape[0], end_trial - init_trial)) x_positions_pre_nose = tracking[:, 0].T y_positions_pre_nose = tracking[:, 1].T x_positions_inter_nose, y_positions_inter_nose = beh_func.interpolate_positions( x_positions_pre_nose, y_positions_pre_nose) x_positions_nose = x_positions_inter_nose # gaussian_filter(x_positions_inter_nose, sigma=2) y_positions_nose = y_positions_inter_nose # gaussian_filter(y_positions_inter_nose, sigma=2) x_positions_pre_head = tracking[:, 2].T y_positions_pre_head = tracking[:, 3].T x_positions_inter_head, y_positions_inter_head = beh_func.interpolate_positions( x_positions_pre_head, y_positions_pre_head) x_positions_head = x_positions_inter_head # gaussian_filter(x_positions_inter_head, sigma=2) y_positions_head = y_positions_inter_head # gaussian_filter(y_positions_inter_head, sigma=2) position = np.array([x_positions_head, y_positions_head]).T position_nose = np.array([x_positions_nose, y_positions_nose]).T vx = np.zeros((x_positions_head.shape[0], 1)) vy = np.zeros((y_positions_head.shape[0], 1))
def get_parameters(tracking, coordinates1, coordinates2): positions_list = [ beh_func.interpolate_positions(signal1=tracking[:, i * 2].T, signal2=tracking[:, i * 2 + 1].T) for i in range(int(tracking.shape[1] / 2)) ] positions = np.array(positions_list) cm = np.mean(positions, axis=0) d1 = [distance(cm[:, i], coordinates1[i, :]) for i in range(cm.shape[1])] d2 = [distance(cm[:, i], coordinates2[i, :]) for i in range(cm.shape[1])] d1 = np.array(d1) d2 = np.array(d2) vx = np.zeros((cm.shape[1], 1)) vy = np.zeros((cm.shape[1], 1)) vx[1:, 0] = np.diff(cm[0, :]) vy[1:, 0] = np.diff(cm[1, :]) speed = np.squeeze(np.sqrt(vx * vx + vy * vy)) hd_list = [ head_direction(positions[:, :, i]) for i in range(positions.shape[2]) ] hd = np.array(hd_list) ## get object one direction x_difference = [ coordinates1[i, 0] - tracking[i, 0] for i in range(tracking.shape[0]) ] y_difference = [ coordinates1[i, 1] - tracking[i, 1] for i in range(tracking.shape[0]) ] direction = np.array([np.array(x_difference), np.array(y_difference)]).T direction = direction / npalg.norm(direction) angle1_list = [ angle_between(hd[i], direction[i, :]) for i in range(len(hd)) ] angle1 = np.array(angle1_list) x_difference = [ coordinates2[i, 0] - tracking[i, 0] for i in range(tracking.shape[0]) ] y_difference = [ coordinates2[i, 1] - tracking[i, 1] for i in range(tracking.shape[0]) ] direction2 = np.array([np.array(x_difference), np.array(y_difference)]).T direction2 = direction2 / npalg.norm(direction2) angle2_list = [ angle_between(hd[i], direction2[i, :]) for i in range(len(hd)) ] angle2 = np.array(angle2_list) parameters = { 'cm': cm, 'head_direction': hd, 'speed': speed, 'angle1': angle1, 'angle2': angle2, 'dist_obj1': d1, 'dist_obj2': d2 } return parameters