def get_o2_init_class_count(self): n_1st_order_bins = 2 * self.n_class initial_v0_class = class_index_abs_log(self.initial_v0, self.v_log_edges) initial_v1_class = class_index_abs_log(self.initial_v1, self.v_log_edges) init_class_count = np.zeros(n_1st_order_bins * n_1st_order_bins) for i in range(len(initial_v0_class)): class_number_2d = find_2d_bin(initial_v0_class[i], initial_v1_class[i], n_1st_order_bins) init_class_count[class_number_2d] += 1 return init_class_count
def count_matrix_with_freq_one_trajectory_kang(transition_count_matrix, lag, v_array, freq_array, v_log_edges): """ Count matrix in time using kang bins """ if not len(v_array): return inds_temp = class_index_abs_log(v_array, v_log_edges) idx_cum1 = 1 idx_cum2 = 1 + lag cumsum_temp = np.cumsum(freq_array) last_index = cumsum_temp[-1] while idx_cum2 < last_index: [idx1, idx2] = np.digitize([idx_cum1, idx_cum2], cumsum_temp, right=True) if idx1 != idx2: #in this case they are not in the same cumsum_bin hence no repetition #current bin is idx1 transition_count_matrix[inds_temp[idx2], inds_temp[idx1]] += 1 idx_cum1 += 1 idx_cum2 += 1 else: #they are in a bin with repetition might happen n_repeating_pair = (cumsum_temp[idx1] - idx_cum2) + 1 current_bin = inds_temp[idx1] transition_count_matrix[current_bin, current_bin] += n_repeating_pair idx_cum1 += n_repeating_pair idx_cum2 += n_repeating_pair
def get_init_class_count(self): new_v, new_f = remove_duplicate(self.initial_v0, self.initial_f0) v_log_edges = self.v_log_edges v_class_number = class_index_abs_log(new_v, v_log_edges) init_count = self.mapping.find_3d_class_number(v_class_number, new_f) init_class_count = np.zeros(self.mapping.n_3d_classes) for i in init_count: init_class_count[i] += 1 return init_class_count
def test_mapping_with_theta(): main_folder = os.path.dirname(os.path.dirname(__file__)) input_folder = os.path.join(main_folder, 'test_related_files','particle_tracking_results') dt = 50.0 n_realz = 1 big_v_array, big_freq_array, pointer_list, initial_v0, initial_v1 = make_input_for_binning_with_freq(input_folder, n_realz, dt) new_v, new_f = remove_duplicate(big_v_array, big_freq_array) #make random angles new_theta = np.random.randn(len(new_v))*np.pi n_abs_log_class = 8 n_theta_classes = 8 abs_log_v_edges = make_1d_abs_vel_bins(new_v, n_abs_log_class, n_slow_classes = 1) theta_bin_edges = make_theta_bins_linear(n_theta_classes) v_class_number = class_index_abs_log(new_v, abs_log_v_edges) mapping = mapping_v_theta_repeat(abs_log_v_edges, theta_bin_edges, new_v, new_theta, new_f) # find 1d v and theta bins, calculate the 2d bins, calculate the 1d bins from 2d bins v_idx = np.digitize(np.log(np.abs(new_v)), abs_log_v_edges) fix_out_of_bound(v_idx, abs_log_v_edges) v_idx -= 1 theta_idx = np.digitize(new_theta, theta_bin_edges) fix_out_of_bound(theta_idx, theta_bin_edges) theta_idx -= 1 idx_2d = mapping.class_index_2d_vtheta(new_v, new_theta) v_idx2, theta_idx2 = mapping.class_index_1d_v_theta_from_2d(idx_2d) assert (np.all(v_idx2 == v_idx)) assert (np.all(theta_idx2 == theta_idx)) # test inverse mapping class_3d_array = range(mapping.n_3d_classes) for class_3d_test in class_3d_array: abs_v, sgn_v, freq = mapping.find_v_theta_freq(class_3d_test) # test draw velocity v_log_edges = mapping.v_log_edges # for i in range(len(v_log_edges)-1): for i in range(mapping.n_abs_v_classes): class_2d = i v1 = mapping.draw_from_class_velocity(class_2d) assert (np.log(v1) > v_log_edges[class_2d]) assert (np.log(v1) < v_log_edges[class_2d + 1]) # test find_3d_class_number for i in range(mapping.n_2d_classes): class_2d = i cumsum_n_subclass = mapping.cumsum_n_subclass freq_array = mapping.sub_classes_nrepeat[class_2d] index_2d = class_2d * np.ones(len(freq_array), dtype=np.int) class_3d = mapping.find_3d_class_number(index_2d, freq_array) assert (np.all(class_3d == (cumsum_n_subclass[index_2d] + range(len(freq_array))))) freq_array_2 = np.zeros(len(class_3d)) for j in range(len(class_3d)): v, theta, freq = mapping.find_v_theta_freq(class_3d[j]) assert (freq == freq_array[j]) inv_class_2d = mapping.class_index_2d_vtheta(v, theta) assert (inv_class_2d == class_2d)
def test_mapping_both_ways(): main_folder = os.path.dirname(os.path.dirname(__file__)) input_folder = os.path.join(main_folder, 'test_related_files', 'particle_tracking_results') dt = 50.0 n_realz = 1 big_v_array, big_freq_array, pointer_list, initial_v0, initial_v1 = make_input_for_binning_with_freq( input_folder, n_realz, dt) new_v, new_f = remove_duplicate(big_v_array, big_freq_array) n_abs_log_class = 8 abs_log_v_edges = make_1d_abs_vel_bins(new_v, n_abs_log_class, n_slow_classes=1) v_class_number = class_index_abs_log(new_v, abs_log_v_edges) sub_classes_nrepeat = [] n_subclass = [] place_holder = np.array([1], dtype=np.int) for i in range(2 * n_abs_log_class): possible_f_vals = np.unique(new_f[v_class_number == i]) if not len(possible_f_vals): possible_f_vals = copy(place_holder) sub_classes_nrepeat.append(sorted(possible_f_vals)) n_subclass.append(len(possible_f_vals)) modified_n_sub_class = np.array(n_subclass) cumsum_n_subclass = np.hstack((0, np.cumsum(modified_n_sub_class))) mapping = mapping_v_sgn_repeat(abs_log_v_edges, cumsum_n_subclass, sub_classes_nrepeat) #test draw velocity v_log_edges = mapping.v_log_edges # for i in range(len(v_log_edges)-1): for i in range(mapping.n_abs_v_classes): class_2d = i v1 = mapping.draw_from_class_velocity(class_2d) v_log_edges = mapping.v_log_edges assert (np.log(v1) > v_log_edges[class_2d]) assert (np.log(v1) < v_log_edges[class_2d + 1]) #test find_3d_class_number for i in range(len(v_log_edges) - 1): class_2d = i cumsum_n_subclass = mapping.cumsum_n_subclass freq_array = mapping.sub_classes_nrepeat[class_2d] index_2d = class_2d * np.ones(len(freq_array), dtype=np.int) class_3d = mapping.find_3d_class_number(index_2d, freq_array) assert (np.all(class_3d == (cumsum_n_subclass[index_2d] + range(len(freq_array)))))
def get_trans_matrix(self, lag): n_3d_class = self.mapping.n_3d_classes v_log_edges = self.v_log_edges i_list = [] j_list = [] ij_list = set([]) val_list = [] time_step = self.time_step print 'extracting trans matrix...' for j in range(self.n_total_realz): print 'realization number: ', j file_name = "real_" + str(j) + ".pkl" input_file = os.path.join(self.input_folder, file_name) with open(input_file, 'rb') as input: dataHolder = pickle.load(input) dx = np.diff(dataHolder.x_array) dt = np.diff(dataHolder.t_array) lastIdx = dataHolder.last_idx_array vMatrix = np.divide(dx, dt) m = dx.shape[0] assert (np.all(vMatrix[:, 0] > 0.0)) for i in range(m): # get the time process for each velocity cutOff = lastIdx[i] dx_time, freq_temp = get_time_dx_array_with_frequency( dt[i, :cutOff], vMatrix[i, :cutOff], time_step) v_temp = np.array(dx_time) / time_step if len(dx_time) > 1: new_v, new_f = remove_duplicate(v_temp, freq_temp) #get the velocity class number from abs(v) and sgn(v) v_class_number = class_index_abs_log(new_v, v_log_edges) class_3d_array = self.mapping.find_3d_class_number( v_class_number, new_f) fill_one_trajectory_sparse_cython(lag, class_3d_array, i_list, j_list, ij_list, val_list) print 'done' return csc_matrix((val_list, (i_list, j_list)), shape=(n_3d_class, n_3d_class))
def generate_mapping(self): ''' :param self: :return: mapping ''' print "generating map..." new_v, new_f = remove_duplicate(self.big_v_array, self.big_freq_array) v_log_edges = self.v_log_edges v_class_number = class_index_abs_log(new_v, v_log_edges) sub_classes_nrepeat = [] n_subclass = [] place_holder = np.array([1], dtype=np.int) for i in range(2 * self.n_absv_class): possible_f_vals = np.unique(new_f[v_class_number == i]) if not len(possible_f_vals): possible_f_vals = copy(place_holder) sub_classes_nrepeat.append(sorted(possible_f_vals)) n_subclass.append(len(possible_f_vals)) modified_n_sub_class = np.array(n_subclass) cumsum_n_subclass = np.hstack((0, np.cumsum(modified_n_sub_class))) mapping = mapping_v_sgn_repeat(v_log_edges, cumsum_n_subclass, sub_classes_nrepeat) print "done" return mapping