getWeights.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:python-psignifit 作者: wichmann-lab 项目源码 文件源码
def getWeights(X1D):

    d = len(X1D)

    ''' puts the X values in their respective dimensions to use bsxfun for
    evaluation'''
    Xreshape = []
    Xreshape.append(X1D[0].reshape(-1,1))
    if d >= 2:
        Xreshape.append(X1D[1].reshape([1,-1]))

    for idx in range (2,d):
        dims = [1]*idx
        dims.append(-1)
        Xreshape.append(reshape(X1D[idx], dims))


    # to find and handle singleton dimensions
    sizes = [X1D[ix].size for ix in range(0,d)]
    Xlength = array(sizes)

    ''' calculate weights '''
    #1) calculate mass/volume for each cuboid
    weight = 1
    for idx in range(0,d):
        if Xlength[idx] > 1:
            if idx > 1:
                weight = multiply(weight[...,newaxis],diff(Xreshape[idx], axis=idx))
            else:
                weight = multiply(weight,diff(Xreshape[idx], axis=idx))
        else:
            weight = weight[...,newaxis]
    #2) sum to get weight for each point
    if d > 1:
        dims = tile(2,[1,d])
        dims[0,where(Xlength == 1)] = 1
        d = sum(Xlength > 1)
        weight = (2.**(-d))*convn(weight, ones(dims.ravel()), mode='full')
    else:
        weight = (2.**(-1))*convolve(weight, array([[1],[1]]))

    return weight
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号