def test_pol2cart(): theta = np.pi/3. phi = np.pi/3. x = 1/2. * np.sqrt(3/4.) y = 3/4. z = 1/2. res = pol2cart(np.array([theta, phi])) assert_almost_equal(np.array((x,y,z)), res)
def random_pars(mu=None, kappa=None, verbose=False): if mu == None: theta = np.random.uniform(low = -np.pi/2., high = np.pi/2.) phi = np.random.uniform(low = -np.pi, high = np.pi) if verbose: print "theta = %.3f, phi = %.3f" % (theta, phi) mu = pol2cart(np.array([theta, phi])) if kappa == None: kappa = np.random.uniform(low=0., high=5.) return mu, kappa
def test_coordinate_conversions(): n_tests = int(1e3) for i in xrange(n_tests): theta = np.random.uniform(low = 0., high = np.pi) phi = np.random.uniform(low = 0., high = 2 * np.pi) cart = pol2cart(np.array([theta, phi])) theta_prime, phi_prime = cart2pol(cart) assert_almost_equal(np.array((theta, phi % (2 * np.pi))), np.array((theta_prime, phi_prime % (2 * np.pi))))
def _map_data(data, task, pd, resolution=100): ''' Parameters ---------- data : ndarray shape (ntime, ntask) task : ndarray shape (ntask, 6) pd : ndarray shape (3,) Returns ------- mapped_data : ndarray shape (ntime, resolution, resolution) Notes ----- need to incorporate PD so that it lies at center of plot ''' if data.shape[-1] != task.shape[0]: raise ValueError('last axis of data (len %d) must be same length as\n' 'first axis of task (len %d)' % \ (data.shape[-1], data.shape[0])) # generate theta, phi grid # or rather map x,y grid to nearest of 26 targets thetas = np.linspace(0., np.pi, resolution) phis = np.linspace(0., 2 * np.pi, resolution) theta_grid = np.tile(thetas[:,None], (1, resolution)) phi_grid = np.tile(phis[None,:], (resolution, 1)) # convert polar to cartesian co-ordinates tp_grid = np.concatenate((theta_grid[None], phi_grid[None]), axis=0) xyz_grid = pol2cart(tp_grid, axis=0) # calculate direction of each task start = task[:,:3] stop = task[:,3:] direction_task = unitvec(stop - start, axis=-1) # rotate task directions until pd points towards 0,0,1 rotated_toz = rotate_targets(direction_task, cart2pol(pd)) # now rotate again to point pd towards 0,1,0 rotated_toy = rotate_targets(rotated_toz, np.array([np.pi/2., 0])) # calculate angle between each grid square and each direction angles = np.arccos(np.tensordot(rotated_toy, xyz_grid, [1,0])) # get index of closest direction for each grid square # = get argmin along 0th axis nearest = np.argmin(angles, axis=0) mapped_data = data[..., nearest] return mapped_data