Exemplo n.º 1
0
def _getdata(pathname):
    try:
        fd = open(pathname, 'rb')
    except:
        if whazup:
            print 'open failed ', pathname
        return []
    rp = rawlist.rawparams(fd)  # get num chans and scales
    dta = []
    for ch in range(rp[0]):
        ans = rawlist.rawreadvalues(fd, ch)
        if ans[0] == None:
            if whazup:
                print 'error', ans[1]
            fd.close()
            return []  # some error
        dta.append(ans)
    fd.close()
    return dta
Exemplo n.º 2
0
def _getdata(pathname):
    fd = open(pathname, 'rb')
    rp = rawlist.rawparams(fd)  # get num chans and scales
    dta = []
    TC = 0.5
    part = 1 / (TC * 256)
    most = 1 - part
    for ch in range(rp[0]):
        ans = rawlist.rawreadvalues(fd, ch)
        if ans[0] == None:
            ##print 'error',ans[1]
            return []  # some error
        vdata = []
        if 0:
            intg = ans[0]
            for sample in ans:
                intg = intg * most + abs(sample) * 3.14159 * part
                vdata.append(intg)
            dta.append(vdata)
        else:
            dta.append(ans)
    fd.close()
    ##print len(dta),rp
    return dta
def smdumpfile(fname, fmtcheck=0):
    # get # channels of raw
    svals = []
    try:
        fd = open(fname, 'rb')
    except:
        return None, None
    # make sure new format
    hasextradata = 0
    subset = rawlist.rawheaderfield(fd, 'SUBSET')
    if subset > 1:
        hasextradata = subset
    if fmtcheck:
        if hasextradata == 0:
            fd.close()
            return None, None
    delta = 0
    if hasextradata >= 3:
        delta = 1

    fstr = rawlist.rawheaderfield(fd, 'FORMATSTRING')
    maxchan = 0
    for c in fstr:
        if c == 'C':
            maxchan += 1
    # get data list for each channel
    datalist = []
    for ch in range(maxchan):
        dta = rawlist.rawreadvalues(fd, ch)
        if dta[0] == None:
            break
        datalist.append(dta)
    # get filtered data
    filtlist = []
    if hasextradata:
        for ch in range(14 + delta):
            dta = rawlist.rawreadamplvalues(fd, ch)
            if dta == []:
                break
            filtlist.append(dta)
    # get reward list
    rwddict = _rawrewards(fd)
    datecode = rawlist.rawheaderfield(fd, 'DATECODE')
    timecode = rawlist.rawheaderfield(fd, 'TIMECODE')
    dt = mx.DateTime.DateTimeFromAbsDateTime(int(datecode), int(timecode))
    datetime = (dt.year, dt.month, dt.day, dt.hour, dt.minute, int(dt.second))
    threshdict = None
    if hasextradata:
        pos = rawlist.rawheaderfield(fd, 'SUMMARYOFFSET')
        if pos:
            threshdict = _getthresh(fd, pos)
    fd.close()
    if len(datalist) == 0:
        return None, None

    fmftext = None
    for frm in range(len(datalist[0])):
        data = []
        for dta in datalist:
            try:
                data.append(dta[frm])
            except:
                data.append(0.0)
        filt = []
        if hasextradata:
            for flst in filtlist:
                try:
                    filt.append(flst[frm])
                except:
                    filt.append(0.0)
        events = rwddict.get(frm, ())
        other = ''
        rwd = 0
        if len(events):
            for t, reason, x in events:
                if reason.find('REWARD') >= 0:
                    rwd = 1
                    #for testing
                    if len(other):
                        other = other + ','
                    other = other + reason
                else:
                    if len(other):
                        other = other + ','
                    other = other + reason
        if not fmftext:
            fmftext = 'Time'
            fmf = '%.4f'
            for i in range(len(data)):
                fmftext = fmftext + ',Chan%s' % 'ABCD'[i]
                fmf = fmf + ",%.3f"
            fmftext = fmftext + ',Reward'
            fmf = fmf + ',%d'
            if hasextradata:
                delta = 0
                if hasextradata >= 3:
                    delta = 1
                for i in range(len(filt) - delta):
                    fmftext = fmftext + ',%d' % i
                    fmf = fmf + ',%.2f'
                if delta:
                    fmftext = fmftext + ',OT'
                    fmf = fmf + ',%x'
                    filt[-1] = 100 * filt[-1]
            fmftext = fmftext + ',other'
            fmf = fmf + ',"%s"'
            if threshdict:
                tx = threshdict.keys()
                tx.sort()
                thr = threshdict[tx[0]]
                for i in range(len(thr)):
                    fmf = fmf + ',%.3f'
                    fmftext = fmftext + ',Thrsh' + '%d' % i
                currthresh = thr
            svals.append(fmftext)
        if 0:
            line = '%.4f' % (frm / 256.0)
            for i in range(len(data)):
                line = line + ',%.3f' % data[i]
            line = line + ',%d' % rwd
            for i in range(len(filt)):
                line = line + ',%.2f' % filt[i]
            line = line + ',"%s"' % other
        else:
            dx = [
                frm / 256.0,
            ]
            dx.extend(data)
            dx.append(rwd)
            if len(filt):
                dx.extend(filt)
            dx.append(other)
            if threshdict:
                currthresh = threshdict.get(frm, currthresh)
                dx.extend(currthresh)
            try:
                line = fmf % tuple(dx)
            except:
                print fmf
                print dx
                print fmftext
                raise

        svals.append(line)

    return '\n'.join(svals), datetime
def compute_data(fobj,prt=0):
    
    try:
        fdraw=open(fobj.fname[:-3]+'raw','rb')
    except:
        return []
    if prt:
        print fobj.fname
    rslt=csv_dump_out.csv_dump_out(None,fobj)
    seg=[]
    ampoffset=[0,0,0,0]
    throffset=[0,0,0,0]
    siteoffset=[0,0,0,0]
    
    frq=rawlist.rawheaderfield(fdraw,'CLOCKRATE')
    #cbxx=rawlist.rawcb('',rawfd=fdraw)
    #eoec=_update_eyes(cbxx[0],frq)
    if prt:
        print len(rslt)
    per=0
    # figure out which columns have data I want  period,eyes,ampl-0,ampl-1,thresh-0,thresh-1
    eyes=''
    for num,ln in enumerate(rslt):
        fld=ln.split(',')
        if not num:
            numraw=fobj.number_lowpass
            perslot=fld.index('"Period"')
            eyeslot=fld.index('"Eyes"')
            for i in range(numraw):
                try:
                    x=fld.index('"Ampl-%d"'%i)
                    ampoffset[i]=x
                except:
                    pass
                try:
                    x=fld.index('"Thresh-%d"'%i)
                    throffset[i]=x
                except:
                    pass
                try:
                    x=fld.index('"Chan-%c"' % chr(ord('A')+i))
                    siteoffset[i]=x
                except:
                    pass
            continue
                
        #find beg of per
        #if eoec.has_key(int(fld[0])):
        #    eyes=eoec[int(fld[0])]
        if int(fld[perslot]) == 0:
            if per == 0:
                ##if prt:
                ##    print 'not found first period ',num
                continue # not found end of first period
            if prt:
                print 'found end of period ',per,num
            #here to process previous period logic
            p=fobj.per[per-1]
            ssites=p.sitelist
            for i in range(numraw):
                for s in checksites:
                    look=s+'-a'
                    if fld[siteoffset[i]][1:-1].lower()[:4] == look:
                        # aha- this a a valid site
                        lnx=rslt[num-1].split(',')
                        seg.append((p.start,p.end,perstart,num-1,i,per,fld[eyeslot],fld[siteoffset[i]][1:-1]))
                        #if prt:
                        #    print 'match ',fld[siteoffset[i]][1:-1],look,i,numraw,len(ssites)
                        ##if prt:
                        ##    print 'no match ',ssites[j],look,i
                    else:
                        #if prt:
                        #    print 'no match ',fld[siteoffset[i]][1:-1],fld[siteoffset[i]][1:-1].lower()[:4],look,i
                        pass
            per=0
            continue
        if per== 0:
            perstart=num
            per=int(fld[perslot])
            if prt:
                print 'start of period ',per,num
        
    # get here with possibles in seg
    if prt:
        print 'found possible ',len(seg)
    ans=[]
    for start,end,pstart,pend,ch,per,eyes,site in seg:
        if prt:
            print "s,e,ps,pe,ch,per",start,end,pstart,pend,ch,per
        if end-start < 30:
            if prt:
                print ' less than 30 seconds'
            continue  # too short
        for i in range(numraw):
            if i != ch:
                #if prt:
                #    print "not this chan"
                continue
            ans.append([start,end,pstart,pend,ch+100,per,eyes,site])
                
            thrv=[]
            amplv=[]
            for k in range(pstart,pend):
                fld=rslt[k].split(',')
                thrv.append(fld[throffset[i]])
                amplv.append(fld[ampoffset[i]])
            beg=pstart
            org=None
            lstart=lend=0
            poss=[]
            while beg < pend:
                if thrv[beg-pstart] > amplv[beg-pstart]:
                    if org:
                        if beg-org >= 30:
                            poss.append((beg-org,org,beg-1))
                    org=None
                else:
                    if org == None:
                        org=beg
                beg += 1
            if org:
                if pend-org >= 30:
                    poss.append((pend-org,org,pend))
            else:
                if len(poss) == 0:
                    poss.append((pend-pstart,pstart,pend))
                
            #now find longest segment
            if prt:
                print poss
            beg=0
            for l,s,e in poss:
                if l > beg:
                    lstart=s
                    lend=e
                    beg=l
           
            # now have start end
            if beg:
                ans.append([start,end,lstart,lend,ch,per,eyes,site])
            if prt:
                print 'longest ',ch,per,start,end,lstart,lend
            # if not found, good sample
    # now do the fft stuff
    if prt:
        print 'found valid ',len(ans),' chans=',numraw
    final=[]
    dtaa=[]
    for i in range(numraw):
        dta=rawlist.rawreadvalues(fdraw,i)
        dtaa.append(dta)
    fdraw.close()
    extfft.initfft(frq)
    prevch=None
    prevper=0
    grp=[]
    for start,end,valid,pend,ch,per,eyes,site in ans:
        if prt:
            print ch,valid,pend,per,site
        if (ch%100) != prevch or prevper != per:
            if prevch == None:
                prevch = ch%100
                prevper=per
            else:
                final.append((prevch%100,site,prevper,eyes[1:-1],grp))
                grp=[]
                prevch = ch%100
                prevper=per
        dta=dtaa[ch% 100]
        #get the raw file
        span=pend-valid
        if ch < 100:
            if span > 60:
                span=60
        b=pend-span-10*frq
        if b < start:
            b=start
        sumr=[0 for x in range(640)]
        cntr=0
        for d in range((b+1)*frq,pend*frq,frq):
            x=extfft.compute_fft_array(dta[d-frq*10:d],frq*10,640)
            # take last 60 or less seconds of data and
            #    compute FFT in 0.1 Hz bins and average the bins
            # 
            for i in range(640):
                sumr[i] += x[i]
            cntr += 1
        biggie=0
        if cntr:
            for i in range(640):
                sumr[i] = sumr[i]/cntr
                if sumr[i] > biggie:
                    biggie=sumr[i]
        else:
            if prt:
                print 'no fft samples!',b+1,pend
        sumr.append(biggie)
        grp.append(sumr)
            
    if grp != []:
        final.append((ch%100,site,per,eyes[1:-1],grp))
        
    extfft.clean()
    #                             i    s      i,  s  65*f
    
    if prt:
        for item in final:
            print item[0],item[1],item[2],len(item[4]),item[4][0][-1],
            if len(item[4]) > 1:
                print item[4][1][-1],
            else:
                print 'None',
            print
            
            
    return final    #  array of (chan,site, per,eyes,64 bins of average+max)
def raw_export(dest, rawfile, why=0, hidename=0, justname=0, exporttree=None):

    rawFD = open(rawfile, 'rb')  # open RAW file for reading
    rawfile_info = rawlist.rawinfo(rawfile, why=why)
    rate = rawfile_info[1]
    if why > 1:
        print dest, rawfile
    # get base name for use with GetClientFile
    basename = "\\".join(os.path.normpath(rawfile).split("\\")[:-2])
    client = clients.GetClientFile(basename)
    if why > 1:
        print basename, client

    # get patient identification, date and time from raw file
    datetime = rawlist.rawdate(rawfile)

    try:
        pid = client['client.xguid']
    except:
        # for old version 4.0.3h and before
        pid = client['client.fullname']

    patient = client['client.clientid']
    fullname = client['client.fullname']

    # get annotations
    annotations = []
    initial_site = []
    sumfilepath = os.path.splitext(rawfile)[0] + ".SUM"
    tstart = []
    fake = 0
    if os.path.exists(sumfilepath):
        summary = sumfile.get_sum_file(sumfilepath, 0, 0)
        if summary:
            annotations = get_annotation_list(summary)
            if why > 3:
                print 'summary:', annotations
        else:
            annotations = fake_raw_annot(rawfile)
            fake = 1
            if why > 3:
                print 'fake sum', annotations
    else:
        ##isa=[]
        annotations = fake_raw_annot(rawfile, why=why)
        fake = 1
        if why > 3:
            print 'faked', annotations
        ##initial_site.extend( _expand_rawsites(rawfile_info) )
        ##tstart.append(0)

    temp_annot = []
    start_site = ''
    site_list = []

    TIME = 0
    TEXT = 1
    TYPE = 2

    # Break up annotations according to site change
    stime = 0
    for annot in annotations:
        if fake:
            tt = annot[0]
            if tt <= 256:
                tt = 0
            if start_site == '':
                start_site = annot[1]
                tstart.append(tt)
                site_list.append([annot[1]])
                stime = annot[tt]
            else:
                if stime == annot[0]:
                    site_list[-1].append(annot[1])
                else:
                    start_site = annot[1]
                    site_list.append([annot[1]])
                    tstart.append(annot[0])
                    stime = annot[0]
        else:
            if annot[TYPE] == 'SITE   ' and start_site == '':
                start_site = annot[TEXT]
                site_list.append([start_site])
                tstart.append(0)
                stime = 0
            elif annot[TYPE] == 'SITE   ':
                if stime == annot[TIME]:
                    site_list[-1].append(annot[TEXT])
                else:
                    if annot[TEXT] != start_site:
                        start_site = annot[TEXT]
                        site_list.append([start_site])
                        tstart.append(annot[TIME])
                        stime = annot[TIME]
    #print site_list,tstart
    # make up filename
    if dest == None:
        return  # for debug
    if exporttree:
        # make sure Brainchild folder exists in dest (which is the exporttree name
        bc = os.path.join(exporttree, 'Brainchild')
        if not os.path.exists(bc):
            os.mkdir(bc)
        # make sure client folder exists in brainchild
        bc2 = os.path.join(bc, patient)
        if not os.path.exists(bc2):
            os.mkdir(bc2)
        # make sure protocol class existss in client folder
        bcx = os.path.normpath(rawfile).split('\\')
        bc3 = os.path.join(bc2, bcx[-2])
        if not os.path.exists(bc3):
            os.mkdir(bc3)
        # dest dest to result
        dest = bc3
    else:
        dest = os.path.normpath(dest)
    bn = os.path.basename(rawfile)
    fdate = bn[-10:-4]
    if hidename:
        if hidename > 1:
            hname = clients.HIPAAstring(fullname, pid, 'First')
            asterisk = hname.find('*')
            if asterisk > 0:
                hname = hname[:asterisk]  # remove those trailing asterisks
            vfname = hname.replace(' ', '_')
            fout = os.path.join(dest, vfname + fdate)
        else:
            fout = os.path.join(dest, pid + fdate)
    else:
        fout = os.path.join(dest, patient + fdate)
    # get data (so I know how long it is)
    data = []
    for i in range(4):
        dta = rawlist.rawreadvalues(rawFD, i)
        if dta[0] == None:
            break
        data.append(dta)
    if why > 1:
        print 'data read ', len(data), len(tstart)
    # write the file
    for seg in range(len(tstart)):
        if seg + 1 == len(tstart):
            # here for the last segment
            seconds = len(data[0]) / rate - tstart[seg]
        else:
            seconds = tstart[seg + 1] - tstart[seg]
        if why > 1:
            print fout + segtable[seg]
        destname = fout + segtable[seg] + '.txt'
        fdo = open(destname, 'w')
        if justname:
            print >> fdo, 'Filename: %s' % os.path.basename(destname)
        else:
            print >> fdo, 'Filename: %s' % fout
        print >> fdo, 'Number of seconds: %d' % seconds
        print >> fdo, 'Sample Rate: %d' % rate
        print >> fdo, 'Multiplier: 1'
        fdo.write('Time')
        sl = site_list[seg]
        ##print site_list
        vinx, vsites = match_sites(sl)
        if why > 1:
            print vinx, vsites, sl, site_list
        xcntr = 1
        ##print len(data),len(sl),sl
        for isite in vsites:
            fdo.write(',')
            fdo.write(isite)
        fdo.write('\n')

        base = p = tstart[seg] * rate
        endpt = p + seconds * rate
        col = 0
        cntr = 0
        while p < endpt:
            fdo.write(str(p - base))
            while 1:
                if vinx[col] >= 0:
                    fdo.write(',%.3f' % data[vinx[col]][p])
                else:
                    fdo.write(',0')
                col += 1
                if col >= len(vinx):
                    break

            col = 0
            fdo.write('\n')
            p += 1
            cntr += 1

        while (cntr % rate):  # round up to next second
            fdo.write(str(p - base))
            for j in range(len(vinx)):
                fd.write(',0')
            fd.write('\n')
            p += 1
            cntr += 1

        fdo.close()
    rawFD.close()
    return 0