def __blockadeDepthFit(self): """ """ # Get first order estimates of the open channel and blocked state currents # We will use these to guide the final fits. pre = self.__currentEstimate( self.eventData[:self.eStartEstimate - self.datPad], min( 50, int( len(self.eventData[:self.eStartEstimate - self.datPad]) / 10.0))) post = self.__currentEstimate( self.eventData[self.eEndEstimate + self.datPad:], min( 50, int( len(self.eventData[self.eEndEstimate + self.datPad:]) / 10.0))) bcCurrEst = self.__meanBlockedCurrent( self.eventData[self.eStartEstimate - self.datPad:self.eEndEstimate + self.datPad], 2.0) if self.mdProcessingStatus == 'normal': ocCurrEst = util.flat2([pre, post]) self.__fitsumgauss(ocCurrEst + bcCurrEst, util.avg(ocCurrEst), np.std(ocCurrEst), util.avg(bcCurrEst))
def _levelchange(self, dat, sMean, sSD, nSD, blksz): for i in range(int(blksz / 2.0), len(dat) - int(blksz / 2.0)): if abs( util.avg(dat[i - int(blksz / 2.0):i + int(blksz / 2.0)]) - sMean) > nSD * sSD: return [i + 1, util.avg(dat[i:i + blksz])] raise InvalidEvent()
def __blockadeDepthFit(self): """ """ # Get first order estimates of the open channel and blocked state currents # We will use these to guide the final fits. pre=self.__currentEstimate(self.eventData[:self.eStartEstimate-self.datPad], min(50, int(len(self.eventData[:self.eStartEstimate-self.datPad])/10.0)) ) post=self.__currentEstimate(self.eventData[self.eEndEstimate+self.datPad:], min(50, int(len(self.eventData[self.eEndEstimate+self.datPad:])/10.0)) ) bcCurrEst=self.__meanBlockedCurrent(self.eventData[self.eStartEstimate-self.datPad:self.eEndEstimate+self.datPad], 2.0) if self.mdProcessingStatus=='normal': ocCurrEst=util.flat2( [pre, post] ) self.__fitsumgauss( ocCurrEst+bcCurrEst, util.avg(ocCurrEst), np.std(ocCurrEst), util.avg(bcCurrEst) )
def __fitsumgauss(self, cleandat, oc, ocsd, bc): """ """ np.seterr(invalid='raise', over='raise') # keep track of the current polarity sign=np.sign(util.avg(cleandat)) # use the absolute current value to simplify the calc. # We put the sign back in the current when returning the results evntabs=np.abs(cleandat) # Histogram and fit try: # PDF of event data ehist,bins=np.histogram( evntabs, bins=np.arange(np.min(evntabs)-self.histPad,np.max(evntabs)+self.histPad, self.histBinSz), density=True ) popt, pcov = scipy.optimize.curve_fit( self.__sumgauss, [ b+(0.5*self.histBinSz) for b in bins[:-1] ], ehist, p0=[np.max(ehist), np.abs(bc), np.abs(ocsd), np.max(ehist), np.abs(oc), np.abs(ocsd)], maxfev=self.maxFitIters ) except BaseException, err: #print oc, ocsd, bc, len(cleandat) # reject event if it can't be fit self.rejectEvent('eBDFit') return
def __FitEvent(self): try: dt = 1000. / self.Fs # time-step in ms. # edat=np.asarray( np.abs(self.eventData), dtype='float64' ) edat = self.dataPolarity * np.asarray(self.eventData, dtype='float64') # control numpy error reporting np.seterr(invalid='ignore', over='ignore', under='ignore') ts = np.array([t * dt for t in range(0, len(edat))], dtype='float64') # estimate initial guess for events initguess = self._characterizeevent(edat, np.abs(util.avg(edat[:10])), self.baseSD, self.InitThreshold, 6.) self.nStates = len(initguess) - 1 # setup fit params params = Parameters() for i in range(1, len(initguess)): params.add('a' + str(i - 1), value=initguess[i][0] - initguess[i - 1][0]) params.add('mu' + str(i - 1), value=initguess[i][1] * dt) params.add('tau' + str(i - 1), value=dt * 7.5) params.add('b', value=initguess[0][0]) optfit = Minimizer(self.__objfunc, params, fcn_args=( ts, edat, )) optfit.prepare_fit() optfit.leastsq(xtol=self.FitTol, ftol=self.FitTol, maxfev=self.FitIters) if optfit.success: self.__recordevent(optfit) else: #print optfit.message, optfit.lmdif_message self.rejectEvent('eFitConvergence') except KeyboardInterrupt: self.rejectEvent('eFitUserStop') raise except InvalidEvent: self.rejectEvent('eInvalidEvent') except: self.rejectEvent('eFitFailure') raise
def __FitEvent(self): try: dt = 1000./self.Fs # time-step in ms. # edat=np.asarray( np.abs(self.eventData), dtype='float64' ) edat=self.dataPolarity*np.asarray( self.eventData, dtype='float64' ) # control numpy error reporting np.seterr(invalid='ignore', over='ignore', under='ignore') ts = np.array([ t*dt for t in range(0,len(edat)) ], dtype='float64') # estimate initial guess for events initguess=self._characterizeevent(edat, np.abs(util.avg(edat[:10])), self.baseSD, self.InitThreshold, 6.) self.nStates=len(initguess)-1 # setup fit params params=Parameters() for i in range(1, len(initguess)): params.add('a'+str(i-1), value=initguess[i][0]-initguess[i-1][0]) params.add('mu'+str(i-1), value=initguess[i][1]*dt) params.add('tau'+str(i-1), value=dt*7.5) params.add('b', value=initguess[0][0]) optfit=Minimizer(self.__objfunc, params, fcn_args=(ts,edat,)) optfit.prepare_fit() optfit.leastsq(xtol=self.FitTol,ftol=self.FitTol,maxfev=self.FitIters) if optfit.success: self.__recordevent(optfit) else: #print optfit.message, optfit.lmdif_message self.rejectEvent('eFitConvergence') except KeyboardInterrupt: self.rejectEvent('eFitUserStop') raise except InvalidEvent: self.rejectEvent('eInvalidEvent') except: self.rejectEvent('eFitFailure') raise
def __fitsumgauss(self, cleandat, oc, ocsd, bc): """ """ np.seterr(invalid='raise', over='raise') # keep track of the current polarity sign = np.sign(util.avg(cleandat)) # use the absolute current value to simplify the calc. # We put the sign back in the current when returning the results evntabs = np.abs(cleandat) # Histogram and fit try: # PDF of event data ehist, bins = np.histogram(evntabs, bins=np.arange( np.min(evntabs) - self.histPad, np.max(evntabs) + self.histPad, self.histBinSz), density=True) popt, pcov = scipy.optimize.curve_fit( self.__sumgauss, [b + (0.5 * self.histBinSz) for b in bins[:-1]], ehist, p0=[ np.max(ehist), np.abs(bc), np.abs(ocsd), np.max(ehist), np.abs(oc), np.abs(ocsd) ], maxfev=self.maxFitIters) except BaseException, err: #print oc, ocsd, bc, len(cleandat) # reject event if it can't be fit self.rejectEvent('eBDFit') return
def __init__(self, dat): self.data = dat self.mean = util.avg(dat) self.sd = util.sd(dat)
def __init__(self, dat): self.data=dat self.mean=util.avg(dat) self.sd=util.sd(dat)
def _levelchange(self, dat, sMean, sSD, nSD, blksz): for i in range(int(blksz/2.0), len(dat)-int(blksz/2.0)): if abs(util.avg(dat[i-int(blksz/2.0):i+int(blksz/2.0)])-sMean) > nSD * sSD: return [i+1, util.avg(dat[i:i+blksz])] raise InvalidEvent()
def _eventsegment(self): """ Cut up a trajectory into individual events. This algorithm uses simple thresholding. By working with absolute values of currents, we handle positive and negative potentials without switches. When the current drops below 'thrCurr', mark the start of the event. When the current returns to the baseline (obtained by averaging the open channel current immediately preceeding the start of the event), mark the event end. Pad the event by 'eventPad' points and hand off to the event processing algorithm. """ try: while(1): t=self.currData.popleft() self.globalDataIndex+=1 # store the latest point in a fixed buffer if not self.eventstart: self.preeventdat.append(t) # Mark the start of the event if abs(t) < self.thrCurr: #print "event", self.eventstart=True self.eventdat=[] self.eventdat.append(t) self.dataStart=self.globalDataIndex-len(self.preeventdat)-1 if self.eventstart: mean=abs(util.avg(self.preeventdat)) while(abs(t)<mean): t=self.currData.popleft() self.eventdat.append(t) self.globalDataIndex+=1 # end of event. Reset the flag self.eventstart=False # Check if there are enough data points to pad the event. If not pop more. if len(self.currData) < self.eventPad: self.currData.extend(list(self.trajDataObj.popdata(self.nPoints))) # Cleanup event pad data before adding it to the event. We look for: # 1. the start of a second event # 2. Outliers # The threshold for accepting a point is eventThreshold/2.0 eventpaddat = util.selectS( [ self.currData[i] for i in range(self.eventPad) ], self.eventThreshold/2.0, self.meanOpenCurr, self.sdOpenCurr ) #print self.trajDataObj.FsHz, self.windowOpenCurrentMean, self.sdOpenCurr, self.slopeOpenCurr if len(self.eventdat)>=self.minEventLength: self.eventcount+=1 # print "i=", self.eventcount #sys.stderr.write('event mean curr={0:0.2f}, len(preeventdat)={1}\n'.format(sum(self.eventdat)/len(self.eventdat),len(self.preeventdat))) #print list(self.preeventdat) + self.eventdat + [ self.currData[i] for i in range(self.eventPad) ] #print "ecount=", self.eventcount, self.eventProcHnd # print "eventProcSettings", self.eventProcSettingsDict self._processEvent( self.eventProcHnd( list(self.preeventdat) + self.eventdat + eventpaddat, self.FsHz, eventstart=len(self.preeventdat)+1, # event start point eventend=len(self.preeventdat)+len(self.eventdat)+1, # event end point baselinestats=[ self.meanOpenCurr, self.sdOpenCurr, self.slopeOpenCurr ], algosettingsdict=self.eventProcSettingsDict.copy(), savets=self.writeEventTS, absdatidx=self.dataStart ) ) self.preeventdat.clear() except IndexError: return