def diagnostics(data, params, nafc=2, sigmoid="logistic", core="ab", cuts=None, gammaislambda=False):
    # here we need to hack stuff, since data can be either 'real' data, or just
    # a list of intensities, or just an empty sequence

    # in order to remain compatible with psipy we must check for an empty
    # sequence here, and return a specially crafted return value in that case.
    # sorry..
    if op.isSequenceType(data) and len(data) == 0:
        pmf, nparams = sfu.make_pmf(
            sfr.PsiData([0], [0], [0], 1), nafc, sigmoid, core, None, gammaislambda=gammaislambda
        )
        thres = np.array([pmf.getThres(params, cut) for cut in sfu.get_cuts(cuts)])
        slope = np.array([pmf.getSlope(params, th) for th in thres])
        return np.array([]), np.array([]), 0.0, thres, np.nan, np.nan

    shape = np.shape(np.array(data))
    intensities_only = False
    if len(shape) == 1:
        # just intensities, make a dataset with k and n all zero
        k = n = [0] * shape[0]
        data = [[xx, kk, nn] for xx, kk, nn in zip(data, k, n)]
        intensities_only = True
    else:
        # data is 'real', just do nothing
        pass

    dataset, pmf, nparams = sfu.make_dataset_and_pmf(data, nafc, sigmoid, core, None, gammaislambda=gammaislambda)
    cuts = sfu.get_cuts(cuts)
    params = sfu.get_params(params, nparams)
    predicted = np.array([pmf.evaluate(intensity, params) for intensity in dataset.getIntensities()])

    if intensities_only:
        return predicted
    else:
        deviance_residuals = pmf.getDevianceResiduals(params, dataset)
        deviance = pmf.deviance(params, dataset)
        thres = np.array([pmf.getThres(params, cut) for cut in cuts])
        slope = np.array([pmf.getSlope(params, th) for th in thres])
        rpd = pmf.getRpd(deviance_residuals, params, dataset)
        rkd = pmf.getRkd(deviance_residuals, dataset)
        return predicted, deviance_residuals, deviance, thres, slope, rpd, rkd
Exemple #2
0
def diagnostics(data,
                params,
                nafc=2,
                sigmoid='logistic',
                core='ab',
                cuts=None,
                gammaislambda=False):
    """ Some diagnostic statistics for a psychometric function fit.

    This function is a bit messy since it has three functions depending on the
    type of the `data` argument.

    Parameters
    ----------

    data : variable
        real data : A list of lists or an array of data.
            The first column should be stimulus intensity, the second column should
            be number of correct responses (in 2AFC) or number of yes- responses (in
            Yes/No), the third column should be number of trials. See also: the examples
            section below.
        intensities : sequence of floats
            The x-values of the psychometric function, then we obtain only the
            predicted values.
        no data : empty sequence
            In this case we evaluate the psychometric function at the cuts. All
            other return values are then irrelevant.

    params : sequence of len nparams
        parameter vector at which the diagnostic information should be evaluated

    nafc : int
        Number of responses alternatives for nAFC tasks. If nafc==1 a Yes/No task is
        assumed.

    sigmoid : string
        Name of the sigmoid to be fitted. Valid sigmoids include:
                logistic
                gauss
                gumbel_l
                gumbel_r
        See `swignifit.utility.available_sigmoids()` for all available sigmoids.

    core : string
        \"core\"-type of the psychometric function. Valid choices include:
                ab       (x-a)/b
                mw%g     midpoint and width
                linear   a+bx
                log      a+b log(x)
        See `swignifit.utility.available_cores()` for all available sigmoids.

    cuts : sequence of floats
        Cuts at which thresholds should be determined.  That is if cuts =
        (.25,.5,.75), thresholds (F^{-1} ( 0.25 ), F^{-1} ( 0.5 ), F^{-1} ( 0.75
        )) are returned.  Here F^{-1} denotes the inverse of the function
        specified by sigmoid. If cuts==None, this is modified to cuts=[0.5].

    Output
    ------

    (predicted, deviance_residuals, deviance, thres, Rpd, Rkd)

    predicted : numpy array of length nblocks
        predicted values associated with the respective stimulus intensities

    deviance_residuals : numpy array of length nblocks
        deviance residuals of the data

    deviance float
        deviance of the data

    thres : numpy array length ncuts
        the model prediction at the cuts

    Rpd : float
        correlation between predicted performance and deviance residuals

    Rkd : float
        correlation between block index and deviance residuals

    Example
    -------
    >>> x = [float(2*k) for k in xrange(6)]
    >>> k = [34,32,40,48,50,48]
    >>> n = [50]*6
    >>> d = [[xx,kk,nn] for xx,kk,nn in zip(x,k,n)]
    >>> prm = [2.75, 1.45, 0.015]
    >>> pred,di,D,thres,slope,Rpd,Rkd = diagnostics(d,prm)
    >>> D
    8.0748485860836254
    >>> di[0]
    1.6893279652591433
    >>> Rpd
    -0.19344675783032755

    """

    # here we need to hack stuff, since data can be either 'real' data, or just
    # a list of intensities, or just an empty sequence

    # in order to remain compatible with psipy we must check for an empty
    # sequence here, and return a specially crafted return value in that case.
    # sorry..
    # TODO after removal of psipy we can probably change this.
    if op.isSequenceType(data) and len(data) == 0:
        pmf, nparams = sfu.make_pmf(sfr.PsiData([0], [0], [0], 1),
                                    nafc,
                                    sigmoid,
                                    core,
                                    None,
                                    gammaislambda=gammaislambda)
        thres = np.array(
            [pmf.getThres(params, cut) for cut in sfu.get_cuts(cuts)])
        slope = np.array([pmf.getSlope(params, th) for th in thres])
        return np.array([]), np.array([]), 0.0, thres, np.nan, np.nan

    shape = np.shape(np.array(data))
    intensities_only = False
    if len(shape) == 1:
        # just intensities, make a dataset with k and n all zero
        k = n = [0] * shape[0]
        data = [[xx, kk, nn] for xx, kk, nn in zip(data, k, n)]
        intensities_only = True
    else:
        # data is 'real', just do nothing
        pass

    dataset, pmf, nparams = sfu.make_dataset_and_pmf(
        data, nafc, sigmoid, core, None, gammaislambda=gammaislambda)
    cuts = sfu.get_cuts(cuts)
    params = sfu.get_params(params, nparams)
    predicted = np.array([
        pmf.evaluate(intensity, params)
        for intensity in dataset.getIntensities()
    ])

    if intensities_only:
        return predicted
    else:
        deviance_residuals = pmf.getDevianceResiduals(params, dataset)
        deviance = pmf.deviance(params, dataset)
        thres = np.array([pmf.getThres(params, cut) for cut in cuts])
        slope = np.array([pmf.getSlope(params, th) for th in thres])
        rpd = pmf.getRpd(deviance_residuals, params, dataset)
        rkd = pmf.getRkd(deviance_residuals, dataset)
        return predicted, deviance_residuals, deviance, thres, slope, rpd, rkd
def diagnostics(data, params, nafc=2, sigmoid='logistic', core='ab', cuts=None, gammaislambda=False):
    """ Some diagnostic statistics for a psychometric function fit.

    This function is a bit messy since it has three functions depending on the
    type of the `data` argument.

    Parameters
    ----------

    data : variable
        real data : A list of lists or an array of data.
            The first column should be stimulus intensity, the second column should
            be number of correct responses (in 2AFC) or number of yes- responses (in
            Yes/No), the third column should be number of trials. See also: the examples
            section below.
        intensities : sequence of floats
            The x-values of the psychometric function, then we obtain only the
            predicted values.
        no data : empty sequence
            In this case we evaluate the psychometric function at the cuts. All
            other return values are then irrelevant.

    params : sequence of len nparams
        parameter vector at which the diagnostic information should be evaluated

    nafc : int
        Number of responses alternatives for nAFC tasks. If nafc==1 a Yes/No task is
        assumed.

    sigmoid : string
        Name of the sigmoid to be fitted. Valid sigmoids include:
                logistic    (1+exp(-x))**-1 [Default]
                gauss       Phi(x)
                gumbel_l    1 - exp(-exp(x))
                gumbel_r    exp(-exp(-x))
                exponential x>0: 1 - exp(-x); else: 0
                cauchy      atan(x)/pi + 0.5
                id          x; only useful in conjunction with NakaRushton core
        See `swignifit.utility.available_sigmoids()` for all available sigmoids.

    core : string
        \"core\"-type of the psychometric function. Valid choices include:
                ab          (x-a)/b [Default]
                mw%g        midpoint and width, with "%g" a number larger than 0 and less than 0.5. 
                            mw%g corresponds to a parameterization in terms of midpoint and width of
                            the rising part of the sigmoid. This width is defined as the length of the
                            interval on which the sigmoidal part reaches from "%g" to 1-"%g".
                linear      a+b*x
                log         a+b*log(x)
                weibull     2*s*m*(log(x)-log(m))/log(2) + log(log(2)) 
                            This will give you a weibull if combined with the gumbel_l sigmoid and a
                            reverse weibull if combined with the gumbel_r sigmoid.
                poly        (x/a)**b   Will give you a weibull if combined with an exp sigmoid
                NakaRushton The Naka-Rushton nonlinearity; should only be used with an id core
        See `swignifit.utility.available_cores()` for all available cores.

    cuts : sequence of floats
        Cuts at which thresholds should be determined.  That is if cuts =
        (.25,.5,.75), thresholds (F^{-1} ( 0.25 ), F^{-1} ( 0.5 ), F^{-1} ( 0.75
        )) are returned.  Here F^{-1} denotes the inverse of the function
        specified by sigmoid. If cuts==None, this is modified to cuts=[0.5].

    Output
    ------

    (predicted, deviance_residuals, deviance, thres, Rpd, Rkd)

    predicted : numpy array of length nblocks
        predicted values associated with the respective stimulus intensities

    deviance_residuals : numpy array of length nblocks
        deviance residuals of the data

    deviance float
        deviance of the data

    thres : numpy array length ncuts
        the model prediction at the cuts

    Rpd : float
        correlation between predicted performance and deviance residuals

    Rkd : float
        correlation between block index and deviance residuals

    Example
    -------
    >>> x = [float(2*k) for k in xrange(6)]
    >>> k = [34,32,40,48,50,48]
    >>> n = [50]*6
    >>> d = [[xx,kk,nn] for xx,kk,nn in zip(x,k,n)]
    >>> prm = [2.75, 1.45, 0.015]
    >>> pred,di,D,thres,slope,Rpd,Rkd = diagnostics(d,prm)
    >>> D
    8.0748485860836254
    >>> di[0]
    1.6893279652591433
    >>> Rpd
    -0.19344675783032755

    """

    # here we need to hack stuff, since data can be either 'real' data, or just
    # a list of intensities, or just an empty sequence

    # in order to remain compatible with psipy we must check for an empty
    # sequence here, and return a specially crafted return value in that case.
    # sorry..
    # TODO after removal of psipy we can probably change this.
    if op.isSequenceType(data) and len(data) == 0:
        pmf, nparams =  sfu.make_pmf(sfr.PsiData([0],[0],[0],1), nafc, sigmoid, core, None, gammaislambda=gammaislambda )
        thres = np.array([pmf.getThres(params, cut) for cut in sfu.get_cuts(cuts)])
        slope = np.array([pmf.getSlope(params, th ) for th in thres])
        return np.array([]), np.array([]), 0.0, thres, np.nan, np.nan

    shape = np.shape(np.array(data))
    intensities_only = False
    if len(shape) == 1:
        # just intensities, make a dataset with k and n all zero
        k = n = [0] * shape[0]
        data  = [[xx,kk,nn] for xx,kk,nn in zip(data,k,n)]
        intensities_only = True
    else:
        # data is 'real', just do nothing
        pass

    dataset, pmf, nparams = sfu.make_dataset_and_pmf(data, nafc, sigmoid, core, None, gammaislambda=gammaislambda)
    cuts = sfu.get_cuts(cuts)
    params = sfu.get_params(params, nparams)
    predicted = np.array([pmf.evaluate(intensity, params) for intensity in
            dataset.getIntensities()])

    if intensities_only:
        return predicted
    else:
        deviance_residuals = pmf.getDevianceResiduals(params, dataset)
        deviance = pmf.deviance(params, dataset)
        thres = np.array([pmf.getThres(params, cut) for cut in cuts])
        slope = np.array([pmf.getSlope(params, th ) for th in thres])
        rpd = pmf.getRpd(deviance_residuals, params, dataset)
        rkd = pmf.getRkd(deviance_residuals, dataset)
        return predicted, deviance_residuals, deviance, thres, slope, rpd, rkd