def test_sphere_distance(): # make a circle, go around... radius = 3.2 n = 5000 n2 = n / 2 # pi at point n2 in array angles = np.linspace(0, np.pi*2, n, endpoint=False) x = np.sin(angles) * radius y = np.cos(angles) * radius # dists around half circle, including pi half_x = x[:n2+1] half_y = y[:n2+1] half_dists = np.sqrt(np.diff(half_x)**2 + np.diff(half_y)**2) # approximate distances from 0 to pi (not including 0) csums = np.cumsum(half_dists) # concatenated with distances from pi to 0 again cdists = np.r_[0, csums, csums[-2::-1]] # check approximation close to calculated sph_d = sphere_distance([0, radius], np.c_[x, y]) yield assert_array_almost_equal, cdists, sph_d # Now check with passed radius sph_d = sphere_distance([0, radius], np.c_[x, y], radius=radius) yield assert_array_almost_equal, cdists, sph_d # Check points not on surface raises error when asked for yield assert_raises, ValueError, sphere_distance, [1, 0], [0, 2] # Not when check is disabled sph_d = sphere_distance([1, 0], [0, 2], None, False) # Error when radii don't match passed radius yield assert_raises, ValueError, sphere_distance, [1, 0], [0, 1], 2.0
def direction_to_classification(sphere, next_dir, include_stop=False, last_is_stop=False, stop_values=None): # code adapted from Benou "DeepTract",exi # https://github.com/itaybenou/DeepTract/blob/master/utils/train_utils.py sl_len = len(next_dir) loop_len = sl_len - 1 if include_stop and last_is_stop else sl_len l = len(sphere.theta) + 1 if include_stop else len(sphere.theta) classification_output = np.zeros((sl_len, l)) for i in range(loop_len): if not (next_dir[i, 0] == 0.0 and next_dir[i, 1] == 0.0 and next_dir[i, 2] == 0.0): labels_odf = np.exp( -1 * sphere_distance(next_dir[i, :], np.asarray([sphere.x, sphere.y, sphere.z]).T, radius=1, check_radius=False) * 10) if include_stop: classification_output[i][:-1] = labels_odf / np.sum(labels_odf) classification_output[i, -1] = 0.0 else: classification_output[i] = labels_odf / np.sum(labels_odf) if include_stop and last_is_stop: classification_output[-1, -1] = 1 # stop condition or if include_stop and stop_values is not None: classification_output[:, -1] = stop_values # stop values return classification_output
def find_viewpoint_id(sphere_points, point): # print(np.array(point)) # distances = np.linalg.norm(sphere_points - point, axis=1) distances = sphere_distance(sphere_points, np.array(point), radius=1) viewpoint_index = np.argmin(distances) # print(distances[viewpoint_index]) return viewpoint_index
def smooth_labels(directions, Num_outputs=725): smoothed_labels = np.zeros((directions.shape[0], directions.shape[1], Num_outputs), np.float32) sphere_points = get_sphere('repulsion724') for i in range(directions.shape[0]): aux_bool = 0 for j in range(directions.shape[1]): if not (directions[i, j, 0] == 0.0 and directions[i, j, 0] == 0.0 and directions[i, j, 0] == 0.0): idx = sphere_points.find_closest(directions[i, j, :]) labels_odf = np.exp(-1 * sphere_distance(directions[i, j, :], np.asarray( [sphere_points.x, sphere_points.y, sphere_points.z]).T, radius=1.0) / 0.1) labels_odf = labels_odf / np.sum(labels_odf) smoothed_labels[i, j, :-1] = labels_odf smoothed_labels[i, j, -1] = 0.0 elif aux_bool == 0: smoothed_labels[i, j, -1] = 1.0 aux_bool = 1 return smoothed_labels