示例#1
1
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
示例#2
0
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
示例#3
0
def plotPrior(result, 
              lineWidth = 2, 
              lineColor = np.array([0,105,170])/255,
              markerSize = 30):
    
    """
    This function creates the plot illustrating the priors on the different 
    parameters
    """
    
    data = result['data']

    if np.size(result['options']['stimulusRange']) <= 1:
        result['options']['stimulusRange'] = np.array([min(data[:,0]), max(data[:,0])])
        stimRangeSet = False
    else:
        stimRangeSet = True
        
    stimRange = result['options']['stimulusRange']
    r = stimRange[1] - stimRange[0]
    
    # get borders for width
    # minimum = minimal difference of two stimulus levels
    
    if len(np.unique(data[:,0])) > 1 and not(stimRangeSet):
        widthmin = min(np.diff(np.sort(np.unique(data[:,0]))))
    else:
        widthmin = 100*np.spacing(stimRange[1])
    # maximum = spread of the data

    # We use the same prior as we previously used... e.g. we use the factor by
    # which they differ for the cumulative normal function
    Cfactor = (my_norminv(.95,0,1) - my_norminv(.05,0,1))/          \
            (my_norminv(1-result['options']['widthalpha'], 0,1) -   \
             my_norminv(result['options']['widthalpha'], 0,1))
    widthmax = r
    
    steps = 10000
    theta = np.empty(5)
    for itheta in range(0,5):
        if itheta == 0:
            x = np.linspace(stimRange[0]-.5*r, stimRange[1]+.5*r, steps)
        elif itheta == 1:
            x = np.linspace(min(result['X1D'][itheta]), max(result['X1D'][1],),steps)
        elif itheta == 2:
            x = np.linspace(0,.5,steps)
        elif itheta == 3:
            x = np.linspace(0,.5,steps)
        elif itheta == 4:                
            x = np.linspace(0,1,steps)
        
        y = result['options']['priors'][itheta](x)
        theta[itheta] = np.sum(x*y)/np.sum(y)
        
    if result['options']['expType'] == 'equalAsymptote':
        theta[3] = theta[2]
    if result['options']['expType'] == 'nAFC':
        theta[3] = 1/result['options']['expN']
        
    # get limits for the psychometric function plots
    xLimit = [stimRange[0] - .5*r , stimRange[1] +.5*r]
    
    """ threshold """
    
    xthresh = np.linspace(xLimit[0], xLimit[1], steps )
    ythresh = result['options']['priors'][0](xthresh)
    wthresh = convn(np.diff(xthresh), .5*np.array([1,1])) 
    cthresh = np.cumsum(ythresh*wthresh)
    
    plt.subplot(2,3,1)
    plt.plot(xthresh,ythresh, lw = lineWidth, c= lineColor)
    plt.hold(True)
    plt.xlim(xLimit)
    plt.title('Threshold', fontsize = 18)
    plt.ylabel('Density',  fontsize = 18)
    
    plt.subplot(2,3,4)    
    plt.plot(data[:,0], np.zeros(data[:,0].shape), 'k.', ms = markerSize*.75 )
    plt.hold(True)
    plt.ylabel('Percent Correct', fontsize = 18)
    plt.xlim(xLimit)
    
    for idot in range(0,5):
        if idot == 0:
            xcurrent = theta[0]
            color = 'k'
        elif idot == 1:
            xcurrent = min(xthresh)
            color = [1,200/255,0]
        elif idot == 2:
            tix = cthresh[cthresh >=.25].size
            xcurrent = xthresh[-tix]
            color = 'r'
        elif idot == 3:
            tix = cthresh[cthresh >= .75].size
            xcurrent = xthresh[-tix]
            color = 'b'
        elif idot == 4:
            xcurrent = max(xthresh)
            color = 'g'
        y = 100*(theta[3]+(1-theta[2])-theta[3])*result['options']['sigmoidHandle'](x,xcurrent, theta[1])
        
        plt.subplot(2,3,4)
        plt.plot(x,y, '-', lw=lineWidth,c=color )
        plt.subplot(2,3,1)
        plt.plot(xcurrent, result['options']['priors'][0](xcurrent), '.',c=color, ms = markerSize)
    
    """ width"""
    xwidth = np.linspace(widthmin, 3/Cfactor*widthmax, steps)
    ywidth = result['options']['priors'][1](xwidth)
    wwidth = convn(np.diff(xwidth), .5*np.array([1,1]))
    cwidth = np.cumsum(ywidth*wwidth)

    plt.subplot(2,3,2)
    plt.plot(xwidth,ywidth,lw=lineWidth,c=lineColor)
    plt.hold(True)
    plt.xlim([widthmin,3/Cfactor*widthmax])
    plt.title('Width',fontsize=18)

    plt.subplot(2,3,5)
    plt.plot(data[:,0],np.zeros(data[:,0].size),'k.',ms =markerSize*.75)
    plt.hold(True)
    plt.xlim(xLimit)
    plt.xlabel('Stimulus Level',fontsize=18)

    x = np.linspace(xLimit[0],xLimit[1],steps)
    for idot in range(0,5):
        if idot == 0:
            xcurrent = theta[1]
            color = 'k'
        elif idot == 1:
            xcurrent = min(xwidth)
            color = [1,200/255,0]
        elif idot == 2:
            wix = cwidth[cwidth >= .25].size
            xcurrent = xwidth[-wix]
            color = 'r'
        elif idot == 3:
            wix = cwidth[cwidth >= .75].size
            xcurrent = xwidth[-wix]
            color = 'b'
        elif idot ==4:
            xcurrent = max(xwidth)
            color = 'g'
    
        y = 100*(theta[3]+ (1-theta[2] -theta[3])* result['options']['sigmoidHandle'](x,theta[0],xcurrent))
        plt.subplot(2,3,5)
        plt.plot(x,y,'-',lw = lineWidth, c= color)
        plt.subplot(2,3,2)
        plt.plot(xcurrent,result['options']['priors'][1](xcurrent),'.',c = color,ms=markerSize)

    """ lapse """

    xlapse = np.linspace(0,.5,steps)
    ylapse = result['options']['priors'][2](xlapse)
    wlapse = convn(np.diff(xlapse),.5*np.array([1,1]))
    clapse = np.cumsum(ylapse*wlapse)
    plt.subplot(2,3,3)
    plt.plot(xlapse,ylapse,lw=lineWidth,c=lineColor)
    plt.hold(True)
    plt.xlim([0,.5])
    plt.title('\lambda',fontsize=18)

    plt.subplot(2,3,6)
    plt.plot(data[:,0],np.zeros(data[:,0].size),'k.',ms=markerSize*.75)
    plt.hold(True)
    plt.xlim(xLimit)


    x = np.linspace(xLimit[0],xLimit[1],steps)
    for idot in range(0,5):
        if idot == 0:
            xcurrent = theta[2]
            color = 'k'
        elif idot == 1:
            xcurrent = 0
            color = [1,200/255,0]
        elif idot == 2:
            lix = clapse[clapse >= .25].size
            xcurrent = xlapse[-lix]
            color = 'r'
        elif idot == 3:
            lix = clapse[clapse >= .75].size
            xcurrent = xlapse[-lix]
            color = 'b'
        elif idot ==4:
            xcurrent = .5
            color = 'g'
        y = 100*(theta[3]+ (1-xcurrent-theta[3])*result['options']['sigmoidHandle'](x,theta[0],theta[1]))
        plt.subplot(2,3,6)
        plt.plot(x,y,'-',lw=lineWidth,c=color)
        plt.subplot(2,3,3)
        plt.plot(xcurrent,result['options']['priors'][2](xcurrent),'.',c=color,ms=markerSize)


    a_handle = plt.gca()
    a_handle.set_position([200,300,1000,600])
    fig, ax = plt.subplots()
    
    for item in [fig, ax]:
        item.patch.set_visible(False)