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 __init__(self, datapdf, index): '''returns sensor locations from an index provided sensors.locationsbyindex(datapdf, ch.channelindexcfg)''' #--20090707--danc--read config directly to keep from infinite looping #pdfinstance=pdf.read(datapdf) if os.path.isfile(os.path.dirname(datapdf)+'/config')==True: #print 'found config file in same dir. Reading config' cfg=config.read(os.path.dirname(datapdf)+'/config'); self.chlpos = array([]);self.chupos=array([]) self.chldir = array([]);self.chudir=array([]) self.chname = array([]) #--20090817--danc--flipped lower and upper position and direction, as I had it wrong before now. for i in index: self.chupos = append(self.chupos,cfg.channel_data[i].device_data.loop_data[1].position) self.chlpos = append(self.chlpos,cfg.channel_data[i].device_data.loop_data[0].position) self.chudir = append(self.chudir,cfg.channel_data[i].device_data.loop_data[1].direction) self.chldir = append(self.chldir,cfg.channel_data[i].device_data.loop_data[0].direction) self.chname = append(self.chname,cfg.channel_data[i].name) self.chupos=self.chupos.reshape(size(self.chupos)/3,3) self.chlpos=self.chlpos.reshape(size(self.chlpos)/3,3) self.chudir=self.chudir.reshape(size(self.chudir)/3,3) self.chldir=self.chldir.reshape(size(self.chldir)/3,3)
def getdata(self, start, end, chindex=None):#chanindex): from meg import functions '''d.getdata(0, d.pnts_in_file, chindex=[1,2]) or chind=ch.channelindexhdr[ch.channelsortedlabels == 'A63'] d.getdata(0, d.pnts_in_file, chindex=chind)''' self.fid.seek(start*self.time_slice_size, os.SEEK_SET) self.fid_start_pos = self.fid.tell() data_block = fread(self.fid, self.total_chans*(end-start), self.dataprecision, self.dataprecision, 1); self.fid_end_pos = self.fid.tell() data_blockreshape=(data_block.reshape([ (end-start),self.total_chans])) if self.format == 1: print 'WARNING, bug in scalefactor order here. NEED TO FIX' print 'short format, multiplying units_per_bit from cfg' if os.path.isfile(os.path.dirname(self.datapdf)+'/config')==True: print 'found config file in same dir. Reading config' self.cfg=config.read(os.path.dirname(self.datapdf)+'/config'); cfgchlist = [] for t in range(0, self.cfg.data_total_chans): cfgchlist.append(self.cfg.channel_data[t].chan_no) self.scalefact = [] for c in range(0, self.hdr.header_data.total_chans): scalefact = self.cfg.channel_data[cfgchlist.index(self.hdr.channel_ref_data[c].chan_no)].units_per_bit self.scalefact = append(self.scalefact, scalefact) data_blockreshape = data_blockreshape*scalefact else: print "can't read config from same directory" if chindex!=None: self.data_block = data_blockreshape[:,chindex] self.scalefact = 1 else: try: self.data_block = data_blockreshape[:,self.channels.indexlist] except AttributeError: print 'no channels specified' print "try something like... obj.setchannels('meg')" print "or something like... obj.data.getdata(0,p.data.pnts_in_file, chindex=obj.data.channels.channelsind)" return self.fid.close self.numofepochs = self.hdr.header_data.total_epochs[0] #self.pnts_in_file/self.time_slice_size #lasttimepnt = (self.pnts_in_file / self.numofepochs)/\ #(1/self.hdr.header_data.sample_period) #self.wintime = arange(0,lasttimepnt,self.hdr.header_data.sample_period)[:-1] lasttimepnt = self.hdr.header_data.sample_period * self.pnts_in_file self.wintime = arange(0,lasttimepnt,self.hdr.header_data.sample_period) self.epoch_size = self.pnts_in_file / self.numofepochs self.eventtime = self.wintime[0:self.epoch_size] - self.hdr.event_data[0].start_lat self.numofchannels = size(self.data_block,1) self.srate = 1/self.hdr.header_data.sample_period[0] self.frames = self.data_block.shape[0] / self.numofepochs print 'DATA DEBUG', shape(self.data_block)
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