def snap_print(root=None, fname=None, pname=None, edict=None, prt=0): "display/print one file, return 1 if destroy selected, -1 if error" if not os.path.exists(fname): return -1 dta = snap_read(fname, 0, prt) if not dta: return -1 name = dta['name'] if edict: cl = clients.GetClientFile(edict['PROGRAMPATH'] + '/CLIENTS/' + dta['code']) if cl: name = clients.HIPAA(cl, edict['SETTINGS'].get('HipTitle', 'All')) container = _win_common(root, 'Display/print snapshots', fname, prt) fr = container.fr Label(fr, text=name + ' ' + dta['time'] + ' (' + dta['sessiontime'] + ')', font=('Tahoma', 16), background='white').pack(side=TOP) create_graphics(fr, dta) return _win_work(container, _printme, pname, fname, prt)
def autoexport(root, edict, fsum, fraw, proto, prt=0): if len(fsum) == 0: if prt: print 'no summary files found' return # get root filename(s) fx = os.path.basename(fsum[0]) cname = fx[:-10] cd = clients.GetClientFile(edict['DATAPATH'] + '\\' + cname) if not cd: if prt: print 'no client file' return flst = [] # make list for f in fsum: rootname = os.path.basename(f) flst.append((0, 1, 0, edict['DATAPATH'] + '\\' + cname + '\\' + proto + '\\' + rootname)) if int(edict['SETTINGS']['ConsultRaw']): for f in fraw: rootname = os.path.basename(f) flst.append((0, 1, 0, edict['DATAPATH'] + '\\' + cname + '\\' + proto + '\\' + rootname)) # export ans, path = _consultexport(root, edict, flst, cd, prt) return ans
def getname(filename, edict): "gets just the nameinfo from file" dta = snap_read(filename, 1) if dta: name = dta['name'] if edict: cl = clients.GetClientFile(edict['PROGRAMPATH'] + '/CLIENTS/' + dta['code']) if cl: name = clients.HIPAA(cl, edict['SETTINGS'].get('HipSelect', 'All')) nm = name + ' ' + dta['time'] return nm return None
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
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