Example #1
0
def getCanoHRF(duration=25,
               dt=.6,
               hrf_from_spm=True,
               delay_of_response=6.,
               delay_of_undershoot=16.,
               dispersion_of_response=1.,
               dispersion_of_undershoot=1.,
               ratio_resp_under=6.,
               delay=0.):
    """Compute the canonical HRF.

    Parameters
    ----------
    duration : int or float, optional
        time lenght of the HRF in seconds
    dt : float, optional
        time resolution of the HRF in seconds
    hrf_from_spm : bool, optional
        if True, use the SPM formula to compute the HRF, if False, use the hard
        coded values and resample if necessary. It is strongly advised to use
        True.
    delay_of_response : float, optional
        delay of the first peak response in seconds
    delay_of_undershoot : float, optional
        delay of the second undershoot peak in seconds
    dispersion_of_response : float, optional
    dispersion_of_undershoot : float, optional
    ratio_resp_under : float, optional
        ratio between the response peak and the undershoot peak
    delay : float, optional
        delay of the HRF

    Returns
    -------
    time_axis : ndarray, shape (round(duration/dt)+1,)
        time axis of the HRF in seconds
    HRF : ndarray, shape (round(duration/dt)+1,)

    """

    time_axis = np.arange(0, duration + dt, dt)
    axis = np.arange(len(time_axis))

    if hrf_from_spm:
        hrf_cano = (scipy.stats.gamma.pdf(
            axis - delay / dt, delay_of_response / dispersion_of_response, 0,
            dispersion_of_response / dt) - scipy.stats.gamma.pdf(
                axis - delay / dt, delay_of_undershoot /
                dispersion_of_undershoot, 0, dispersion_of_undershoot / dt) /
                    ratio_resp_under)
        hrf_cano[-1] = 0
        hrf_cano /= np.linalg.norm(hrf_cano)
    else:
        hrf_cano = resampleToGrid(time_axisHCano, hCano.copy(), time_axis)
        hrf_cano[-1] = 0.
        assert len(hrf_cano) == len(time_axis)
        hrf_cano /= (hrf_cano**2).sum()**.5

    return time_axis, hrf_cano
Example #2
0
File: hrf.py Project: Solvi/pyhrf
def getCanoHRF(duration=25, dt=.6):

    tAxis = np.arange(0, duration+dt, dt)
    h = resampleToGrid(tAxisHCano, hCano.copy(), tAxis)
    h[-1] = 0.
    assert len(h) == len(tAxis)
    h /= (h**2).sum()**.5
    return tAxis, h
Example #3
0
    def testLargerTargetGrid(self):

        import numpy
        size = 10

        x = numpy.concatenate(
            ([0.], numpy.sort(numpy.random.rand(size)), [1.]))
        y = numpy.concatenate(
            ([0.], numpy.sort(numpy.random.rand(size)), [1.]))
        grid = numpy.arange(-0.2, 1.1, 0.01)
        ny = resampleToGrid(x, y, grid)
Example #4
0
    def testLargerTargetGrid(self):

        import numpy
        size = 10

        x = numpy.concatenate(
            ([0.], numpy.sort(numpy.random.rand(size)), [1.]))
        y = numpy.concatenate(
            ([0.], numpy.sort(numpy.random.rand(size)), [1.]))
        grid = numpy.arange(-0.2, 1.1, 0.01)
        ny = resampleToGrid(x, y, grid)
Example #5
0
def compute_roc_labels(mlabels, true_labels, dthres=0.005, lab_ca=1, lab_ci=0,
                false_pos=2, false_neg=3):
    thresholds = np.arange(0,1/dthres) * dthres
    nconds = true_labels.shape[0]
    oneMinusSpecificity = np.zeros((nconds, len(thresholds)))
    sensitivity = np.zeros((nconds, len(thresholds)))
    for it,thres in enumerate(thresholds):
	#print 'thres:',thres
        labs = threshold_labels(mlabels,thres)
        tlabels = labs.copy()
        labs = mark_wrong_labels(labs,true_labels,lab_ca, lab_ci, false_pos, false_neg)
        for cond in xrange(nconds):
            if 0 and cond == 1:
                print "**cond %d **" %cond
                print 'thresh:', thres
                print 'mlabels (activ class):'
                print mlabels[1,cond,:80]
                print 'thresholded labels:'
                print tlabels[cond,:80]
                print 'simulated labels:'
                print true_labels[cond,:80]
                print 'marked labels:'
                print labs[cond,:80]
            counts = np.bincount(labs[cond,:])
            nbTrueNeg = counts[0]
            nbTruePos = counts[1] if len(counts)>1 else 0

            fp = false_pos
            nbFalsePos = counts[fp] if len(counts)>fp else 0

            fn = false_neg
            nbFalseNeg = counts[fn] if len(counts)>fn else 0

            #if cond == 1 or cond == 2:
	      #print 'cond ', cond
	      #print 'nbTrueNeg=',nbTrueNeg
	      #print 'nbTruePos=',nbTruePos
	      #print 'nbFalsePos=',nbFalsePos
	      #print 'nbFalseNeg=',nbFalseNeg

            if 0 and cond == 1:
                print 'TN :', nbTrueNeg
                print 'TP :', nbTruePos
                print 'FP :', nbFalsePos
                print 'FN :', nbFalseNeg
            if nbTruePos == 0:
                sensitivity[cond,it] = 0
            else:
                sensitivity[cond,it] = nbTruePos /                    \
                                       (nbTruePos+nbFalseNeg+0.0)
            spec = nbTrueNeg/(nbTrueNeg+nbFalsePos+0.0)
            oneMinusSpecificity[cond,it] = 1-spec
            if 0 and cond == 1:
                print '-> se = ', sensitivity[cond, it]
                print '-> 1-sp = ', oneMinusSpecificity[cond,it]

    if 1:
        spGrid = np.arange(0.,1.,0.0005)
        omspec = np.zeros((nconds, len(spGrid)))
        sens = np.zeros((nconds, len(spGrid)))
        for cond in xrange(nconds):
            order = np.argsort(oneMinusSpecificity[cond,:])
            if oneMinusSpecificity[cond,order][0] != 0.:
                osp = np.concatenate(([0.],oneMinusSpecificity[cond,order]))
                se = np.concatenate(([0.],sensitivity[cond,order]))
            else:
                osp = oneMinusSpecificity[cond,order]
                se = sensitivity[cond,order]

            if osp[-1] != 1.:
                osp = np.concatenate((osp,[1.]))
                se = np.concatenate((se,[1.]))

            sens[cond,:] = resampleToGrid(osp, se, spGrid)
            omspec[cond, :] = spGrid
            if 0 and cond == 1:
                print '-> (se,1-spec) :'
                print zip(se,osp)
                print '-> se resampled :'
                print sens[cond,:]
                print 'spec grid :'
                print spGrid


    else:
        sens = sensitivity
        omspec = oneMinusSpecificity

    auc = np.array([trapz(sens[j,:], omspec[j,:])
                    for j in xrange(nconds)])

    #Computing the area under ROC curve (with John Walkenbach formula) (SAME AS auc)
    #area_under_ROC_curve = np.zeros(nconds, dtype=float)
    #for cond in xrange(nconds):
      #for i in xrange(len(spGrid)-1):
	#if sens[cond,i]*sens[cond,i+1] >= 0.:
	  #area_under_ROC_curve[cond] += ((sens[cond,i+1] + sens[cond,i])/2.) * (omspec[cond,i+1] - omspec[cond,i])
	#else:
	  #area_under_ROC_curve[cond] += ((sens[cond,i+1]**2 + sens[cond,i]**2)/((sens[cond,i+1] - sens[cond,i])/2.)) * (omspec[cond,i+1] - omspec[cond,i])

    return sens, omspec, auc #, area_under_ROC_curve