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, 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