def __init__(self, datapdf): '''d=data.read(datapdf)''' self.datapdf=datapdf self.hdr=header.read(datapdf) self.ext = 'pymdat' self.filename = datapdf self.total_chans=self.hdr.header_data.total_chans[0] #self.pnts_in_file=self.hdr.epoch_data[0].pts_in_epoch[0]*self.hdr.header_data.input_epochs #print 'pnts in file ', self.pnts_in_file self.format=self.hdr.header_data.data_format[0] self.fid=open(datapdf, "r") print 'fidtell', self.fid.tell() if self.format == 1: self.dataprecision='h'; self.time_slice_size = 2 * self.total_chans; if self.format == 2: self.dataprecision='i'; self.time_slice_size = 4 * self.total_chans; if self.format == 3: self.dataprecision='f'; self.time_slice_size = 4 * self.total_chans; if self.format == 4: self.dataprecision='d'; self.time_slice_size = 8 * self.total_chans; print 'time_slice_size ',self.time_slice_size print 'precision ', self.dataprecision self.pnts_in_file = self.hdr.header_offset / self.time_slice_size
def __init__(self, datapdf): if os.path.isfile(datapdf)==True: print 'reading pdf header', os.path.abspath(datapdf) self.hdr=header.read(datapdf); if os.path.isfile(os.path.dirname(datapdf)+'/config')==True: print 'found config file in same dir. Reading config' self.cfg=config.read(os.path.dirname(datapdf)+'/config'); if os.path.isfile(os.path.dirname(datapdf)+'/hs_file')==True: print 'found headshape file in same dir. Reading headshape' self.hs=headshape.read(os.path.dirname(datapdf)+'/hs_file'); ##conf=config.read('file') ##h=header.read('file') ## ## ## os.path.dirname(pdf) ## ## ##j=[]; ## ##for i in range(0, len(h.channel_ref_data)): ## j.append(h.channel_ref_data[i].chan_label) ## ## ##dd={} ## for x in range(0, len(j)): ## dd[j[x]]=int(ind[x])
def run(): stage = os.environ['STAGE'] #path = stage+'/data/' x=os.walk(stage+'/data/') datapaths = x.next()[1] listofscans = [] dirofscans = {} for i in datapaths: x=os.walk(stage+'/data/'+i) dirs = x.next()[1] if size(dirs) > 0: for ii in dirs: xx = os.walk(stage+'/data/'+i+'/'+ii) scan = xx.next()[1] #print 'scans',scan if size(scan) > 0: for iii in scan: xx = os.walk(stage+'/data/'+i+'/'+ii+'/'+iii) #added 090324 to fix bug session = xx.next()[1] #print 'scan',iii,'sessions',session for iiii in session: #added 090324 to fix bug #print iiii #if size(session) > 0: #removed 090324 to fix bug xx = os.walk(stage+'/data/'+i+'/'+ii+'/'+iii+'/'+iiii) #added 090324 to fix bug run = xx.next()[1] #print 'run',run if size(run) > 0: data = xx.next() #print 'data',data if size(data[2]) > 0: for d in data[2]: #try and read #print data[0]+'/'+d try: h = header.read(data[0]+'/'+d) if h.header_offset[0] > 0: dirofscans[iii] = data[0]+'/'+d #print 'dirofscans',dirofscans except IOError: pass except MemoryError: pass except OverflowError: pass except AttributeError: print 'Attribute Error in dbscan' except IndexError: pass #dirofscans[iii] = data[0]+data[2][0] #listofscans.extend(iii) #listofids.extend(dirs) uscans = unique(listofscans) return dirofscans
def __init__(self, datapdf, epoch, starttime, endtime): ''' give epoch number and start and end times and returns sample indexes. starttime and endtime in seconds. ''' h=header.read(datapdf) h.header_data.total_epochs[0] self.epochlength=h.epoch_data[0].pts_in_epoch[0] self.startind=round(starttime/h.header_data.sample_period[0]+(h.epoch_data[0].pts_in_epoch[0]*(epoch-1))) self.endind=round(endtime/h.header_data.sample_period[0]+(h.epoch_data[0].pts_in_epoch[0]*(epoch-1)))
def __init__(self, datapdf): if os.path.isfile(datapdf)==True: print('reading pdf', os.path.abspath(datapdf)) self.data = data.read(datapdf) print('reading pdf header', os.path.abspath(datapdf)) self.hdr=header.read(datapdf); #reading withing data module if os.path.isfile(os.path.dirname(datapdf)+'/config')==True: print('found config file in same dir. Reading config') self.cfg=config.read(os.path.dirname(datapdf)+'/config'); if os.path.isfile(os.path.dirname(datapdf)+'/hs_file')==True: print('found headshape file in same dir. Reading headshape') self.hs=headshape.read(os.path.dirname(datapdf)+'/hs_file'); #self.results = self.__class__ else: print('no file found')
def __init__(self, datapdf, type=None, labels=None): '''Returns the channelindex for TYPE channels type = type of channel (meg | eeg | ref | trig | ext | derived | utility | shorted)''' '''sortedindtype----is the index to channels of type. use for extracting those meg channels ex. fn = '/path/to/file' ch = channel.index(fn, 'meg') #return meg channels ''' self.datapdf = datapdf self.type = type if os.path.isfile(datapdf)==True: self.hdr=header.read(datapdf); if os.path.isfile(os.path.dirname(datapdf)+'/config')==True: self.cfg=config.read(os.path.dirname(datapdf)+'/config'); if os.path.isfile(os.path.dirname(datapdf)+'/hs_file')==True: headshape.read self.hs=headshape.read(os.path.dirname(datapdf)+'/hs_file'); else: print 'no headshape file' if type != None: self.labellist, self.indexlist = self.getchindexfromtype(type) if labels != None: self.indexlist, self.labellist = self.getchindexfromlabelsinhdr(labels) if type == 'derived': self.indexlist, self.labellist = self.getchindexfromchannuminhdr('derived') try: self.chanlocs = self.locs2D(self.labellist) except: print 'something wrong with new chanloc method' try: if self.device_types.__contains__(1): #has meg channel data print 'Data contains MEG channels. Making 2D chanlocs map' self.chanlocs = self.locs2D(self.labellist)#, self.indexlist) except AttributeError: pass self.reverseindex = []; sortedindex = sort(self.indexlist) for i in sortedindex: #reverse index self.reverseindex.append(self.indexlist.index(i))
def __init__(self, datapdf): '''d=data.read(datapdf)''' try: self.datapdf=datapdf self.hdr=header.read(datapdf) self.ext = 'pymdat' self.filepath = datapdf pathlist = self.filepath.split('/') self.filename = pathlist.pop() self.run = pathlist.pop() self.session = pathlist.pop() self.scan = pathlist.pop() self.pid = pathlist.pop() self.filedir = self.filepath.replace(self.filepath.split('/')[-1],'') self.total_chans=self.hdr.header_data.total_chans[0] self.format=self.hdr.header_data.data_format[0] self.fid=open(datapdf, "r") #print 'fidtell', self.fid.tell() except IndexError: print 'probably not a 4D MEG file' pass if self.format == 1: self.dataprecision='h'; self.time_slice_size = 2 * self.total_chans; if self.format == 2: self.dataprecision='i'; self.time_slice_size = 4 * self.total_chans; if self.format == 3: self.dataprecision='f'; self.time_slice_size = 4 * self.total_chans; if self.format == 4: self.dataprecision='d'; self.time_slice_size = 8 * self.total_chans; print 'time_slice_size ',self.time_slice_size print 'precision ', self.dataprecision self.pnts_in_file = self.hdr.header_offset / self.time_slice_size
def __init__(self, datapdf, type): '''Returns the channelindex for TYPE channels type = type of channel (meg | eeg | ref | trig | ext | derived | utility | shorted)''' '''sortedindtype----is the index to channels of type. use for extracting those meg channels ex. fn = '/path/to/file' ch = channel.index(fn[0], 'meg') #return meg channels ''' self.datapdf = datapdf self.type = type if os.path.isfile(datapdf)==True: self.hdr=header.read(datapdf); if os.path.isfile(os.path.dirname(datapdf)+'/config')==True: self.cfg=config.read(os.path.dirname(datapdf)+'/config'); if os.path.isfile(os.path.dirname(datapdf)+'/hs_file')==True: headshape.read self.hs=headshape.read(os.path.dirname(datapdf)+'/hs_file'); else: print 'no headshape file' cind=[]; chlabellist=[]#=empty chnumber=zeros([len(self.hdr.channel_ref_data)], dtype=int) #channel label, number and index array chindex=zeros([len(self.hdr.channel_ref_data)], dtype=int) #channel label, number and index array cfgchname=[] cfgchtype=zeros([len(self.cfg.channel_data)], dtype=int) #channel label, number and index array cfgchannumber=zeros([len(self.cfg.channel_data)], dtype=int) #channel label, number and index array for i in range(0, len(self.hdr.channel_ref_data)): cind.append(self.hdr.channel_ref_data[i].chan_label) chlabellist.append(self.hdr.channel_ref_data[i].chan_label) chnumber[i]=(self.hdr.channel_ref_data[i].chan_no) chindex[i]=(self.hdr.channel_ref_data[i].index) chlabel=array(chlabellist) self.chlabel=chlabel self.chnumber=chnumber self.chindex=chindex for i in range(0, len(self.cfg.channel_data)): cfgchname.append(self.cfg.channel_data[i].name) cfgchtype[i]=(self.cfg.channel_data[i].type) cfgchannumber[i]=(self.cfg.channel_data[i].chan_no) cfgchname=array(cfgchname) self.cfgchname=cfgchname self.cfgchtype=cfgchtype self.cfgchannumber=cfgchannumber def indexsorted(sortedch, chlisttoindex): chlist=list(chlisttoindex) sortind=[]; for i in sortedch: sortind.append(chlist.index(i)) return sortind '''sort channels and return indexing array''' self.unsortedch=chlabel; if type == 'derived': print 'cant sort derived channels' return self.sortedch=array(sorted(self.unsortedch)) self.sortedind=array(indexsorted(self.sortedch, self.chlabel)) self.sortedcharray=array(chlabel[self.sortedind]) '''EXTRACT CHANNELS''' if type == 'meg': device_data=[1] elif type == 'ref': device_data=[3] elif type == 'eeg': device_data=[2] elif type == 'ext': device_data=[4] elif type == 'trig': device_data=[5] elif type == 'util': device_data=[6] elif type == 'derived': device_data=[7] elif type == 'shorted': device_data=[8] elif type == 'all': device_data=unique(cfgchtype) else: print 'unknown channel type', type device_data=[1000] dd = device_data self.chanbool=cfgchtype[chnumber-1]==dd #cfgchtype[chnumber-1] returns boolean to unsorted channel of type self.unsortedlabeltype=self.unsortedch[self.cfgchtype[self.chnumber-1]==dd] #unsorted of only device type self.sortedlabeltype=array(sorted(self.unsortedlabeltype)) #sorted labels self.sortedindtype=array(indexsorted(self.sortedlabeltype, self.chlabel)) #sorted index of certain type self.channelsind=self.chindex[self.chanbool] self.channelsname=self.chlabel[self.chanbool] self.allfromconfig=cfgchname[cfgchtype[cfgchannumber<1000-1]==dd] #get all channels from config less than number value=1000 (excludes weird types) '''return index to config''' try: self.ind2cfg=array(indexsorted(self.sortedlabeltype, self.cfgchname)) except ValueError: print 'names in config don\'t match header. no sort' return #--20090928--danc--fixing bug from previous channel locs method which picked up unsorted channel labels and used that to index sorted coordinates, which produced wrong results. chfound=[] #empty array cfglabels = [] import readline if type == 'meg': #build meg channel 2d locations #chaninfo = read_array(os.path.dirname(__file__)+'/configs/megchannels.cfg') chaninfo = loadtxt(os.path.dirname(__file__)+'/configs/megchannels.cfg') for l in chaninfo[0]: #print l cfglabels.append('A'+str(int(l))) chanlocs248 = chaninfo[1:4] for ch in cfglabels: chfound.append(ch in self.sortedlabeltype) if ch in self.sortedlabeltype: pass else: print 'Chanloc Maker Warning:',ch, 'not found' self.chfound=array(chfound); self.chanlocs=chanlocs248[:,self.chfound]; self.channelindexhdr=self.sortedindtype self.channelindexcfg=self.ind2cfg self.channelsortedlabels=self.sortedlabeltype self.channeltype = type del self.channelsind,self.chanbool,self.unsortedlabeltype,self.unsortedch del self.cfgchannumber,self.cfgchname,self.cfgchtype,self.cfg,self.allfromconfig del self.channelsname,self.chindex,self.chlabel,self.chnumber del self.hdr,self.sortedcharray,self.sortedlabeltype,self.ind2cfg