def plotPrior(result,
              lineWidth=2,
              lineColor=np.array([0, 105, 170]) / 255,
              markerSize=30,
              showImediate=True):
    """
    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 = (_utils.my_norminv(.95,0,1) - _utils.my_norminv(.05,0,1))/          \
            (_utils.my_norminv(1-result['options']['widthalpha'], 0,1) -   \
             _utils.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)

    x = np.linspace(xLimit[0], xLimit[1], steps)
    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(np.array(xcurrent),
                 result['options']['priors'][2](np.array(xcurrent)),
                 '.',
                 c=color,
                 ms=markerSize)
    if (showImediate):
        plt.show(0)
def plotPrior(result, 
              lineWidth = 2, 
              lineColor = np.array([0,105,170])/255,
              markerSize = 30,
              showImediate   = True):
    
    """
    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 = (_utils.my_norminv(.95,0,1) - _utils.my_norminv(.05,0,1))/          \
            (_utils.my_norminv(1-result['options']['widthalpha'], 0,1) -   \
             _utils.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)
    
    x = np.linspace(xLimit[0],xLimit[1],steps)
    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(np.array(xcurrent),result['options']['priors'][2](np.array(xcurrent)),'.',c=color,ms=markerSize)
    if (showImediate):
        plt.show(0)