def _alignData(self): """Re-calibrate data using theoretical envelope.""" # split data to raster and intensities xAxis, yAxis = numpy.hsplit(self.data, 2) raster = xAxis.flatten() intensities = yAxis.flatten() # model profiles models, exchanged = self._makeModels(raster) # fit current data fit = self._leastSquare(intensities, models, iterLimit=len(self.models)) # get all isotopes for all used models isotopes = [] for i, abundance in enumerate(fit): pattern = self.models[exchanged[i]][1] isotopes += [(p[0], p[1]*abundance) for p in pattern] # check isotopes if not isotopes: return # make total envelope profile = mod_pattern.profile(isotopes, fwhm=self.fwhm, points=10, model=self.peakShape) # label peaks in profile peaklist = mod_peakpicking.labelscan(profile, pickingHeight=0.95, relThreshold=0.01) # find useful calibrants calibrants = [] tolerance = self.fwhm/1.5 for peak in peaklist: for point in self.data: error = point[0] - peak.mz if abs(error) <= tolerance: if calibrants and calibrants[-1][0] == peak.mz and calibrants[-1][1] < point[1]: calibrants[-1] = (point[0], peak.mz) else: calibrants.append((point[0], peak.mz)) elif error > tolerance: break # calc calibration if len(calibrants) > 3: model, params, chi = mod_calibration.calibration(calibrants, model='quadratic') elif len(calibrants) > 1: model, params, chi = mod_calibration.calibration(calibrants, model='linear') else: return # apply calibration to data for x in range(len(self.data)): self.data[x][0] = model(params, self.data[x][0]) for x in range(len(self.spectrum)): self.spectrum[x][0] = model(params, self.spectrum[x][0])
def labelscan( self, pickingHeight=0.75, absThreshold=0.0, relThreshold=0.0, snThreshold=0.0, baselineWindow=0.1, baselineOffset=0.0, smoothMethod=None, smoothWindow=0.2, smoothCycles=1, ): """Label centroides in current scan. pickingHeight (float) - peak picking height for centroiding absThreshold (float) - absolute intensity threshold relThreshold (float) - relative intensity threshold snThreshold (float) - signal to noise threshold baselineWindow (float) - noise calculation window (in %/100) baselineOffset (float) - baseline offset, relative to noise width (in %/100) smoothMethod (None, MA, GA or SG) - smoothing method smoothWindow (float) - m/z window size for smoothing smoothCycles (int) - number of smoothing cycles """ # get baseline baseline = self.baseline(window=baselineWindow, offset=baselineOffset) # pre-smooth profile profile = self.profile if smoothMethod: profile = mod_signal.smooth(signal=profile, method=smoothMethod, window=smoothWindow, cycles=smoothCycles) # label peaks peaklist = mod_peakpicking.labelscan( signal=profile, pickingHeight=pickingHeight, absThreshold=absThreshold, relThreshold=relThreshold, snThreshold=snThreshold, baseline=baseline, ) # check peaklist if peaklist == None: return False # update peaklist self.peaklist = peaklist return True
def labelscan(self, pickingHeight=0.75, absThreshold=0., relThreshold=0., snThreshold=0., baselineWindow=0.1, baselineOffset=0., smoothMethod=None, smoothWindow=0.2, smoothCycles=1): """Label centroides in current scan. pickingHeight (float) - peak picking height for centroiding absThreshold (float) - absolute intensity threshold relThreshold (float) - relative intensity threshold snThreshold (float) - signal to noise threshold baselineWindow (float) - noise calculation window (in %/100) baselineOffset (float) - baseline offset, relative to noise width (in %/100) smoothMethod (None, MA, GA or SG) - smoothing method smoothWindow (float) - m/z window size for smoothing smoothCycles (int) - number of smoothing cycles """ # get baseline baseline = self.baseline(window=baselineWindow, offset=baselineOffset) # pre-smooth profile profile = self.profile if smoothMethod: profile = mod_signal.smooth(signal=profile, method=smoothMethod, window=smoothWindow, cycles=smoothCycles) # label peaks peaklist = mod_peakpicking.labelscan(signal=profile, pickingHeight=pickingHeight, absThreshold=absThreshold, relThreshold=relThreshold, snThreshold=snThreshold, baseline=baseline) # check peaklist if peaklist == None: return False # update peaklist self.peaklist = peaklist return True
def tospectrum(self, signal, fwhm=0.1, forceFwhm=True, autoAlign=True, iterLimit=None, relThreshold=0., pickingHeight=0.90, baseline=None): """Fit modeled profiles to spectrum using tmp peaklist. signal (numpy array) - m/z / intensity pairs fwhm (float) - defaut fwhm forceFwhm (bool) - use default fwhm autoAlign (bool) - automatic m/z shift iterLimit (int) - maximum number of iterations pickingHeight (float) - peak picking height for centroiding relThreshold (float) - relative intensity threshold baseline (numpy array) - signal baseline """ # crop signal to relevant m/z range i1 = mod_signal.locate(signal, self.mzrange[0]) i2 = mod_signal.locate(signal, self.mzrange[1]) signal = signal[i1:i2] # get peaklist from signal peaklist = mod_peakpicking.labelscan(signal=signal, pickingHeight=pickingHeight, relThreshold=relThreshold, baseline=baseline) # remove shoulder peaks peaklist.remshoulders(fwhm=fwhm) # correct signal baseline if baseline != None: self.spectrum = mod_signal.subbase(signal, baseline) # fit to peaklist return self.topeaklist( peaklist=peaklist, fwhm=fwhm, forceFwhm=forceFwhm, autoAlign=autoAlign, iterLimit=iterLimit, relThreshold=relThreshold, )
def tospectrum(self, signal, fwhm=0.1, forceFwhm=True, autoAlign=True, iterLimit=None, relThreshold=0., pickingHeight=0.90, baseline=None): """Fit modeled profiles to spectrum using tmp peaklist. signal (numpy array) - m/z / intensity pairs fwhm (float) - defaut fwhm forceFwhm (bool) - use default fwhm autoAlign (bool) - automatic m/z shift iterLimit (int) - maximum number of iterations pickingHeight (float) - peak picking height for centroiding relThreshold (float) - relative intensity threshold baseline (numpy array) - signal baseline """ # crop signal to relevant m/z range i1 = mod_signal.locate(signal, self.mzrange[0]) i2 = mod_signal.locate(signal, self.mzrange[1]) signal = signal[i1:i2] # get peaklist from signal peaklist = mod_peakpicking.labelscan( signal = signal, pickingHeight = pickingHeight, relThreshold = relThreshold, baseline = baseline ) # remove shoulder peaks peaklist.remshoulders(fwhm=fwhm) # correct signal baseline if baseline != None: self.spectrum = mod_signal.subbase(signal, baseline) # fit to peaklist return self.topeaklist( peaklist = peaklist, fwhm = fwhm, forceFwhm = forceFwhm, autoAlign = autoAlign, iterLimit = iterLimit, relThreshold = relThreshold, )
def _alignData(self): """Re-calibrate data using theoretical envelope.""" # split data to raster and intensities xAxis, yAxis = numpy.hsplit(self.data, 2) raster = xAxis.flatten() intensities = yAxis.flatten() # model profiles models, exchanged = self._makeModels(raster) # fit current data fit = self._leastSquare(intensities, models, iterLimit=len(self.models)) # get all isotopes for all used models isotopes = [] for i, abundance in enumerate(fit): pattern = self.models[exchanged[i]][1] isotopes += [(p[0], p[1] * abundance) for p in pattern] # check isotopes if not isotopes: return # make total envelope profile = mod_pattern.profile(isotopes, fwhm=self.fwhm, points=10, model=self.peakShape) # label peaks in profile peaklist = mod_peakpicking.labelscan(profile, pickingHeight=0.95, relThreshold=0.01) # find useful calibrants calibrants = [] tolerance = self.fwhm / 1.5 for peak in peaklist: for point in self.data: error = point[0] - peak.mz if abs(error) <= tolerance: if calibrants and calibrants[-1][ 0] == peak.mz and calibrants[-1][1] < point[1]: calibrants[-1] = (point[0], peak.mz) else: calibrants.append((point[0], peak.mz)) elif error > tolerance: break # calc calibration if len(calibrants) > 3: model, params, chi = mod_calibration.calibration(calibrants, model='quadratic') elif len(calibrants) > 1: model, params, chi = mod_calibration.calibration(calibrants, model='linear') else: return # apply calibration to data for x in range(len(self.data)): self.data[x][0] = model(params, self.data[x][0]) for x in range(len(self.spectrum)): self.spectrum[x][0] = model(params, self.spectrum[x][0])