# this is really too complex for thesis and it is straight python code . . . 
    bintime=.0020         # size of bin, in seconds
    matchinterval=stimdur+.02

    oo=LoadSpikeData(brainname, repname)
    numpats=max(keystimpat)+1 
    starttime=0.0
    mp=[]       # 'match patterns' for a pattern number
    endtime=starttime+stiminterval
    nbins=int(matchinterval/bintime)
    numcells=len(oo)
    print "bins per match template: "+`nbins`+", each is", bintime, \
        "s, for a match interval of", matchinterval, "s, #cells",numcells
    print "numpats",numpats
    for i in range(0, numpats):
        ar=zeros((numcells, nbins))
        mp.append(ar)

    # Now construct the template based on the part of the stimpattern for
    # each stim.  Remember that each template really is a number of cells
    # wide and we match on all of them.
    # The M match template will usually be the last of the training
    # runs for each pattern pair number.
    ts=0.0
    te=stiminterval
    for (t, n) in prfstimpat:
        if t=='M':
            print "found a match template for pattern", n, "at time", \
                ts,"duration",matchinterval
            celln=0
            for cellsp in oo: 
                matchend=ts+matchinterval
                # matchinterval, since only looking at part of stiminterval
                a=[s-ts for s in cellsp if s >= ts and s < matchend]
                # now we have a list of spike times in the output for that cell,
                # increment the appropriate bin: 
                ar=mp[n]        # later, check for multiple match patterns
                for spiketime in a:
                    binnum=int(spiketime/bintime)-1
                    ar[celln,binnum]+=1
                celln+=1
        ts=te
        te=ts+stiminterval

    # at this point, mp[n] contains the match pattern (what is tagged
    # in prfstimpat as 'M') for pattern n.  This is independent of the
    # order that each stim pattern might appear in the
    # actual prfstimpat.  mp[n][c] is the array for cell #c
    # Now match the test patterns with the templates:
    ts=0.0
    te=stiminterval
    correct={}
    correct['BK']=0; correct['BD']=0; correct['AK']=0; correct['AD']=0
    numposs=0

    for (t, n) in prfstimpat:
    # t is template type (BK is before training with key as input,
    # AD after training with data as input
        if t=='BK':
            numposs+=1
        if t=='BK' or t=='BD' or t=='AK' or t=='AD':
            tar=zeros((numcells, nbins))
            #print "got test template for stim pattern", n
            celln=0
            for cellsp in oo: 
                matchend=ts+matchinterval
                a=[s-ts for s in cellsp if s >= ts and s < matchend]
                # now we have a list of spike times in the output
                # for that cell, increment the appropriate bin: 
                for spiketime in a:
                    binnum=int(spiketime/bintime)-1
                    #print "time", spiketime, "in bin", binnum
                    tar[celln,binnum]+=1
                celln+=1
            ss=[]
            # extra bins on either side of target bin to check
            # for spikes to count as hit
            binwin=2
            for p in range(0, numpats):
                #print "pattern", p
                s=0.0
                mar=mp[p]
                for c in range(0, numcells):
                    mm=0.0
                    p=0
                    # mar is the match profile from last training run
                    # tar is the result from the run we are trying to match
                    # to a profile
                    ms=mar[c]; ts=tar[c]
                    lm=len(ms)      # had better be same as len(ts)!
                    unmatched=0
                    for p in range(0, lm):      # for each bin
                        if ts[p] > 0:
                            # got spike in sequence we are trying to match to mp
                            # profile, # so check for spike in match profile
                            # within +- binwin 
                            lp=p-binwin; rp=p+binwin
                            if lp < 0: lp=0
                            if rp >= lm: rp=lm-1
                            gotmatch=False
                            for tp in range(lp, rp):
                                if ms[tp]>=1:
                                    mm+=1.0
                                    gotmatch=True
                                    break;  # only get credit for one match!
                            if not gotmatch: unmatched+=1
                    s+=mm
                # s is the sum of all cells' max correlations
                ss.append(s)
                #print s
            # ss is the list of (sums of cells' correlations),
            # indexed by pattern number
            sm=max(ss)
            si=ss.index(sm)
            print "this template",n,"is most like pattern",si,\
                "with a match sum of", sm
            if n==si:
                ans="+ "+`n`+"=="+`si`
                correct[t]+=1
            else: ans="  "+`n`+"!="+`si`
            print t, ans

        ts=te
        te=ts+stiminterval

    b='BD'; a='AD'; print b, correct[b], "  ", a, correct[a] 
    b='BK'; a='AK'; print b, correct[b], "  ", a, correct[a] 

    # here we assume numpossible correct is same for all cases
    return (correct['BD'], correct['AD'], correct['BK'], correct['AK'], numposs)
