def fake_raw_annot(rawfile, why=0):
    cba = rawlist.rawcb(rawfile, why=why)
    annot = []
    for items in cba[0]:
        grp, act, strm = rawevt(items[1])
        if grp == 4:
            if rawlist.cstring(items[3]) != '':
                annot.append((items[2], rawlist.cstring(items[3])))
    return annot
def update_eyes(fobj):
    try:
        fdraw = open(fobj.fname[:-3] + 'raw', 'rb')
    except:
        return {}
    frq = rawlist.rawheaderfield(fdraw, 'CLOCKRATE')
    cbxx = rawlist.rawcb('', rawfd=fdraw)
    frq = rawlist.rawheaderfield(fdraw, 'CLOCKRATE')
    cbxx = rawlist.rawcb('', rawfd=fdraw)

    #must fix error in saving summary data status of eyes
    eoec = {}
    for cb in cbxx[0]:
        g, a, s = rawlist.unpack_action(cb[1])
        if g == 14 and a == 0:
            vv = struct.unpack('ff', cb[3])
            if vv[0] == 5.0:
                cls = int(vv[1])
                if vv[1] == 2.0:
                    cls = 2
                sec = cb[2] / frq
                eoec[sec] = cls
    return eoec
def _rawrewards(fd):
    rawannot, ans = rawlist.rawcb('', rawfd=fd)
    td = {}
    for r in rawannot:
        # items are length of item, actioncode,time in frames,associated data
        # first, decode actioncode
        ac = r[1]
        grp = ((ac >> 12) & 0x0f)
        act = ((ac >> 4) & 0xff)
        strm = (ac & 0x0f)
        # for now, only interested in rewards and F11 and Alt-F11
        if grp == 0:  # GROUP_DC
            if act == 1:  #ACTION_KEY
                # now decode data
                dta = struct.unpack('=LL', r[3])
                if dta[0] == 0x7a:  # VK_F11
                    if dta[1] == 0 or dta[1] == 4:
                        key = r[2]
                        txt = 'F11 '
                        if dta[1] == 4:
                            txt = 'Alt-F11'
                        lst = td.get(key, [])
                        lst.append((r[2], txt, 0))
                        td[key] = lst
            elif act == 6:  #thresh
                key = r[2]
                txt = 'Thresh'
                lst = td.get(key, [])
                lst.append((r[2], txt, 0))
                td[key] = lst

                pass  # what to do???
        elif grp == 15:  #GROUP_EVENT
            if act == 15:  #action_reward
                key = r[2]
                txt = 'REWARD %d' % strm
                lst = td.get(key, [])
                lst.append((r[2], txt, 0))
                td[key] = lst
            elif act == 16 or act == 17:  #fback/Pause
                key = r[2]
                txt = ('FBACK', 'PAUSE')[act - 16]
                lst = td.get(key, [])
                lst.append((r[2], txt, 0))
                td[key] = lst
    return td
def refilter(root, fn, edict=None, recompute=0):

    if root:
        bob = BusyBox.BusyBox(root, 'Busy processing raw data')
    ans = rawlist.rawinfo(fn)
    if ans[0] == None:
        if root:
            bob.destroy()  # oops
        return None  # couldn't open
    # must also insert the site and eyes open/close changes
    cbxx = rawlist.rawcb(fn)
    cbl = cbxx[0]
    true_length = ans[0]
    if len(cbl):
        ans = _handle_other_events(ans, cbl)
    # now figure out the periods
    plist = _decode_evlist(ans)

    fobj = sumfile.get_sum_file(fn[:-4] + '.sum')
    evd = sumcalc.make_all_events(fobj)
    get_freqset(edict)

    dofilts.Load_Filter_File(edict)
    dofilts.Load_Raw_File(fn)
    d = {}
    per = 1
    for itm in plist:
        # get the starting sample, and length
        end = itm[1]
        if end == 0:
            end = true_length
        begin = itm[0]
        if recompute:
            start = begin
        else:
            start = end - 30 * 256
            if start < begin:
                start = begin
            if start < 0:
                start = 0
        length = (end - start) / 256.0
        for chan in range(len(itm[2])):
            # compute them
            clamp = _getclamp(evd, chan, start, end)
            move = dofilts.Get_Average_Value(chan,
                                             freqlims,
                                             start,
                                             length,
                                             trueaverage=1,
                                             clamp=clamp,
                                             useartifact=avalue)
            if len(move) == 0:
                continue
            ##print itm,chan,move
            ##print start,length,true
            v = Value()
            v.theta = move[0][0]
            v.alpha = move[0][1]
            v.beta = move[0][2]
            v.hibeta = move[0][3]
            v.values = move[0][:]  # for newer method
            v.period = per
            v.site = itm[2]
            v.eyes = itm[3]
            v.rewards = itm[-1]
            v.freqlims = freqlims
            # following for debug only
            v.tx = move[2]
            v.secs = length
            v.sums = move[3]
            v.count = move[4]
            d['%2d%02d' % (per, chan)] = v
        if len(itm[2]) > 1:
            d['%2dxx' % (per, )] = len(itm[2])
        per += 1

    if root:
        bob.destroy()
    dofilts.Unload_Filter_File()
    return d
Ejemplo n.º 5
0
def raw2edf(rawfile, destination_dir, debug=0):
    ''' Convert RAW file to EDF '''

    global SAMPLING_RATE
    global MAX_CHAN_LENGTH

    rawFD = open(rawfile, 'rb')  # open RAW file for reading
    rawfile_info = rawlist.rawinfo(rawfile)
    rawfile_subset = rawlist.rawheaderfield(rawFD, 'SUBSET')
    rawfile_formatstring = rawlist.rawheaderfield(rawFD, 'FORMATSTRING')
    # get base name for use with GetClientFile
    basename = "\\".join(rawfile.split("\\")[:-2])
    client = clients.GetClientFile(basename)

    # get patient identification, date and time from raw file
    datetime = rawlist.rawdate(rawfile)
    if debug > 1:
        print 'debugging'

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

    patient = client['client.clientid']
    rec_date = datetime[8:10] + '-' + MONTHS[int(datetime[5:7]) -
                                             1] + '-' + datetime[0:4]
    startdate = datetime[8:10] + '.' + datetime[5:7] + '.' + datetime[2:4]
    starttime = datetime[11:13] + '.' + datetime[14:16] + '.00'

    # Combine directory for edf file and name of files that's going to be created
    rsplit = os.path.split(rawfile)
    rdir = os.path.split(rsplit[0])
    actname = rsplit[1].split('.')
    output_file = destination_dir + '\\' + actname[0] + rdir[1][
        0]  # add E,B,A for category

    c_scales = rawlist.rawparams(rawFD)[1:]  # list of channel scales
    c_channels = []  # list of channel data
    peripheral = []  # peripheral data

    # there will be up to 3 scales (2 for channel a and b, 1 for peripheral)
    c_channels.append(rawlist.rawread(rawFD, CHAN_A)[0])
    c_channels.append(rawlist.rawread(rawFD, CHAN_B)[0])
    if rawfile_formatstring[2] == 'C':
        # must be 4 chan
        if debug > 1:
            print 'aha- 4 channels'
        c_channels.append(rawlist.rawread(rawFD, CHAN_C)[0])
        c_channels.append(rawlist.rawread(rawFD, CHAN_D)[0])

    ####!!!!
    # check for peripheral data (NOT INCORPORATED INTO EDF FILE YET)
    #if len(c_scales) > 2:
    #    try:
    #        peripheral.append( rawlist.rawread(rawFD, PERIPH)[0] )
    #    except:
    #        pass

    # set 1) Sampling Rate, 2) Max channel length
    SAMPLING_RATE = rawfile_info[1]
    MAX_CHAN_LENGTH = ((len(max(c_channels)) / 2) / 256)
    if debug > 1:
        print SAMPLING_RATE, MAX_CHAN_LENGTH

    # get annotations
    annotations = []
    initial_site = []
    initial_site.append(rawfile_info[len(rawfile_info) - 1][1:])
    numchannels = 0
    if rawfile_subset >= 2:
        #sumfilepath = rawfile.split(".")[0] + ".SUM"
        #if os.path.exists(sumfilepath):
        #    summary = sumfile.get_sum_file(sumfilepath, 0, 0)
        #    if summary:
        #        annotations = get_annotation_list(summary)
        #        numchannels=summary.number_streams
        #if numchannels == 0:
        #    # do it the hard way
        #    while numchannels < 16:
        #        dta=rawlist.readrawamplvalues(fd,numchannels,countonly=1)
        #        if len(dta):
        #            numchannels += 1
        #        else:
        #            break
        for astr in range(len(rawfile_formatstring)):
            dta = rawlist.rawreadampl(rawFD, numchannels, prt=0)
            if len(dta):
                c_channels.append(dta[0][:])
                if debug > 1:
                    print 'achan ', len(c_channels[-1]), numchannels
                c_scales.append(0.01)
                numchannels += 1
    startinfo = (pid, patient, rec_date, startdate, starttime, initial_site,
                 numchannels, rawfile_formatstring)

    rawevents, ans = rawlist.rawcb(rawfile)
    if ans == 0:
        # now merge in the desired raw event data
        annotations = merge_rawsum(rawevents, annotations)

    #if numchannels:
    #    for ch in range(numchannels):
    #        c_scales.append(0.0)

    if debug > 1:
        print 'write2file', len(c_channels)
        for i in range(len(c_channels)):
            print i, len(c_channels[i])
    write2file(startinfo, c_channels, c_scales, annotations, output_file,
               debug)
    rawFD.close()

    return 1