Beispiel #1
0
 def this_op(ylms):
     #print('ylms shape: ', ylms.shape)
     assert n_chan == ylms.shape[-1], 'Got the wrong number of channels'
     batch_sz, hrm_blk_sz = ylms.shape[:-1]
     rslt = np.zeros((batch_sz, N_BALL_SAMPS, n_chan))
     for idx_batch in range(batch_sz):
         samp_offset = 0
         hrm_offset = 0
         for _, _, l in full_chain:
             samp_dim1, samp_dim2 = dims_from_l(l)
             samp_blk_sz = samp_dim1 * samp_dim2
             if l in layer_list:
                 hrm_dim1, hrm_dim2, hrm_dim3 = hrm_dims_from_l(l)
                 hrm_blk_sz = hrm_dim1 * hrm_dim2 * hrm_dim3
                 #print('hrm_blk_sz:', hrm_blk_sz)
                 for idx_chan in range(n_chan):
                     hrm_blk = ylms[idx_batch,
                                    hrm_offset:hrm_offset + hrm_blk_sz,
                                    idx_chan]
                     nodes, weights = l_dict[l]
                     samp_blk = psh.MakeGridGLQ(
                         tf.reshape(hrm_blk,
                                    [hrm_dim1, hrm_dim2, hrm_dim3]), nodes)
                     #print('samp_blk shape: ', samp_blk.shape)
                     rslt[idx_batch, samp_offset:samp_offset + samp_blk_sz,
                          idx_chan] = samp_blk.flat
                 hrm_offset += hrm_blk_sz
             samp_offset += samp_blk_sz
     return rslt
Beispiel #2
0
def random_rotate_ball_data(vals_to_rotate, full_chain, l_dict):
    r0, r1, r2 = np.random.random(size=3)
    theta = np.arccos((2.0 * r0) - 1.0)
    phi = 2.0 * np.pi * r1
    alpha = 2.0 * np.pi * r2
    z = np.cos(theta)
    x = np.sin(theta) * np.cos(phi)
    y = np.sin(theta) * np.sin(phi)
    vecM = np.asarray([x, y, z]).reshape((3, 1))
    rot = Quaternion.fromAxisAngle(vecM, alpha).toTransform()
    theta0, theta1, theta2 = transToEulerRzRyRz(rot)
    thetaArr = np.array([-theta2, -theta1, -theta0])

    sampOffset = 0
    rslt = np.zeros_like(vals_to_rotate)
    for _, _, l in full_chain:
        sampDim1, sampDim2 = dims_from_l(l)
        sampBlkSz = sampDim1 * sampDim2
        #if l in [MAX_L]:  # to only output one layer
        if True:
            sampBlk = vals_to_rotate[sampOffset:sampOffset + sampBlkSz]
            nodes, weights, rotMtx = l_dict[l]
            hrmBlk = psh.SHExpandGLQ(sampBlk.reshape((sampDim1, sampDim2)),
                                     weights, nodes)
            hrmRotBlk = psh.SHRotateRealCoef(hrmBlk, thetaArr, rotMtx)
            sampRotBlk = psh.MakeGridGLQ(hrmRotBlk, nodes)
            rslt[sampOffset:sampOffset + sampBlkSz] = sampRotBlk.flat
        sampOffset += sampBlkSz
    return rslt
Beispiel #3
0
def extract_and_pair_single(images, full_chain, l_dict, top_l, layers):
    outDim1, outDim2 = dims_from_l(top_l)
    top_nodes, top_weights, _ = l_dict[top_l]
    sampOffset = 0
    layerOffset = 0
    rslt = np.zeros((len(layers), outDim1, outDim2))
    for _, _, l in full_chain:
        sampDim1, sampDim2 = dims_from_l(l)
        sampBlkSz = sampDim1 * sampDim2
        if l in layers:
            sampBlk = images[sampOffset:sampOffset + sampBlkSz]
            if l == top_l:
                rslt[layerOffset, :, :] = sampBlk.reshape((sampDim1, sampDim2))
            else:
                nodes, weights, rotMtx = l_dict[l]
                hrmBlk = psh.SHExpandGLQ(sampBlk.reshape((sampDim1, sampDim2)),
                                         weights, nodes)
                padHrmBlk = np.zeros((2, top_l + 1, top_l + 1))
                hrmD0, hrmD1, hrmD2 = hrmBlk.shape
                padHrmBlk[:, :hrmD1, :hrmD2] = hrmBlk
                padSampBlk = psh.MakeGridGLQ(padHrmBlk, top_nodes)
                rslt[layerOffset, :, :] = padSampBlk
            layerOffset += 1
        sampOffset += sampBlkSz
    return rslt
Beispiel #4
0
 def reconstructSamples(self, harmonics, maxL=None):
     if maxL is None:
         maxL = self.maxL
     thetaglq, phiglq, nodes, weights = self.prepL(maxL)  # @UnusedVariable
     assert harmonics.shape[1] == nodes.shape[0], 'maxL does not match'
     return psh.MakeGridGLQ(harmonics, nodes)
def TimingAccuracyGLQ():
    # ---- input parameters ----
    maxdeg = 2800
    ls = np.arange(maxdeg + 1)
    beta = 1.5
    print('Driscoll-Healy (real)')

    # ---- create mask to filter out m<=l ----
    mask = np.zeros((2, maxdeg + 1, maxdeg + 1), dtype=np.bool)
    mask[0, 0, 0] = True
    for l in ls:
        mask[:, l, :l + 1] = True
    mask[1, :, 0] = False

    # ---- create Gaussian powerlaw coefficients ----
    print('creating {:d} random coefficients'.format(2 * (maxdeg + 1) *
                                                     (maxdeg + 1)))
    cilm = np.random.normal(loc=0., scale=1., size=(2, maxdeg + 1, maxdeg + 1))
    cilm[:, 1:, :] *= np.sqrt((ls[1:]**beta) / (2. * ls[1:] + 1.))[None, :,
                                                                   None]
    old_power = shtools.SHPowerSpectrum(cilm)
    new_power = 1. / (1. + ls)**beta  # initialize degrees > 0 to power-law
    cilm[:, :, :] *= np.sqrt(new_power / old_power)[None, :, None]
    cilm[~mask] = 0.

    # ---- time spherical harmonics transform for lmax set to increasing
    # ---- powers of 2
    lmax = 2
    print('lmax    maxerror    rms        tprecompute    tinverse    tforward')
    while lmax <= maxdeg:
        # trim coefficients to lmax
        cilm_trim = cilm[:, :lmax + 1, :lmax + 1]
        mask_trim = mask[:, :lmax + 1, :lmax + 1]

        # precompute grid nodes and associated Legendre functions
        tstart = time.time()
        zeros, weights = shtools.SHGLQ(lmax)
        tend = time.time()
        tprecompute = tend - tstart

        # synthesis / inverse
        tstart = time.time()
        grid = shtools.MakeGridGLQ(cilm_trim, zeros)
        tend = time.time()
        tinverse = tend - tstart

        # analysis / forward
        tstart = time.time()
        cilm2_trim = shtools.SHExpandGLQ(grid, weights, zeros)
        tend = time.time()
        tforward = tend - tstart

        # compute error
        err = np.abs(cilm_trim[mask_trim] - cilm2_trim[mask_trim]) / \
            np.abs(cilm_trim[mask_trim])
        maxerr = err.max()
        rmserr = np.mean(err**2)

        print('{:4d}    {:1.2e}    {:1.2e}    {:1.1e}s       {:1.1e}s    '
              '{:1.1e}s'.format(lmax, maxerr, rmserr, tprecompute, tinverse,
                                tforward))
        lmax = lmax * 2