def test_empty(self): peaks = [] x, y = reprofile(peaks) repicked = pick_peaks(x, y) assert len(repicked) == 0 peaks = self.make_data() peaks[0].full_width_at_half_max = 10 x, y = reprofile(peaks, max_fwhm=1.0) repicked = pick_peaks(x, y) assert len(repicked) == 0
def make_scan(self): peaks = [] tids, ions = self.make_tids() list(map(peaks.extend, tids)) peaks.sort(key=lambda x: x.mz) mz = np.array([0]) intensity = np.array([0]) fpeaks = [] for p in peaks: fpeaks.append(FittedPeak( mz=p.mz, intensity=p.intensity, signal_to_noise=p.intensity, peak_count=-1, index=-1, full_width_at_half_max=fwhm, area=p.intensity)) mz, intensity = reprofile(fpeaks) scan = common.Scan( { "id": "test-scan", "index": 0, "m/z array": mz, "intensity array": intensity, "ms level": 1, "scan time": 0.0, "profile spectrum": "", "negative scan": "", }, mzml.MzMLDataInterface()) return scan
def _scan_arrays(self, scan): if scan.is_combined(): mzs, intensities = self.read_spectrum( scan.frame.id, scan.start_scan, scan.end_scan) if len(mzs) == 0: return np.array([], dtype=float), np.array([], dtype=float) sort_mask = np.argsort(mzs) mzs = mzs[sort_mask] intensities = intensities[sort_mask] centroids = pick_peaks(mzs, intensities, peak_mode="centroid") if centroids is None: return np.array([], dtype=float), np.array([], dtype=float) mzs, intensities = reprofile( centroids, dx=self._scan_merging_parameters['dx'], override_fwhm=self._scan_merging_parameters['fwhm']) return mzs, intensities else: mzs, intensities = self.read_spectrum(scan.frame.id, scan.start_scan, scan.end_scan) return mzs, intensities
def reprofile(self, max_fwhm=0.2, dx=0.01, model_cls=None): """Use the picked peaks in :attr:`peak_set` to create a new profile mass spectrum using a peak shape model. Parameters ---------- max_fwhm : float, optional Maximum peak width above which peaks will be ignored dx : float, optional The distance between each new point in m/z space in the reprofiled spectrum model_cls : ms_peak_picker.peak_statistics.PeakShapeModel, optional The peak shape model to use to generate the profile data from the centroided peaks. Defaults a Gaussian model Returns ------- Scan A shallow copy of this scan with its :attr:`arrays` replaced with the new reprofiled arrays Raises ------ ValueError A scan that has not been centroided and is already in profile mode must have its peaks picked before it can be reprofiled. """ if self.peak_set is None and self.is_profile: raise ValueError( "Cannot reprofile a scan that has not been centroided") elif self.peak_set is None and not self.is_profile: self.pick_peaks() if not self.peak_set: arrays = (np.array([], dtype=float), np.array([], dtype=float)) else: arrays = reprofile(self.peak_set, max_fwhm, dx, model_cls) scan = WrappedScan(self._data, self.source, arrays, list(self.product_scans), is_profile=True, annotations=self._external_annotations) return scan
def reprofile(self, max_fwhm=0.2, dx=0.01, model_cls=None): """Use the picked peaks in :attr:`peak_set` to create a new profile mass spectrum using a peak shape model. Parameters ---------- max_fwhm : float, optional Maximum peak width above which peaks will be ignored dx : float, optional The distance between each new point in m/z space in the reprofiled spectrum model_cls : ms_peak_picker.peak_statistics.PeakShapeModel, optional The peak shape model to use to generate the profile data from the centroided peaks. Defaults a Gaussian model Returns ------- Scan A shallow copy of this scan with its :attr:`arrays` replaced with the new reprofiled arrays Raises ------ ValueError A scan that has not been centroided and is already in profile mode must have its peaks picked before it can be reprofiled. """ if self.peak_set is None and self.is_profile: raise ValueError( "Cannot reprofile a scan that has not been centroided") elif self.peak_set is None and not self.is_profile: self.pick_peaks() if not self.peak_set: arrays = (np.array([], dtype=float), np.array([], dtype=float)) else: arrays = reprofile(self.peak_set, max_fwhm, dx, model_cls) scan = WrappedScan( self._data, self.source, arrays, list(self.product_scans), is_profile=True, annotations=self._external_annotations) return scan
def make_data(self): peak = make_peak(200, 1e4) x = np.arange(0, 1000, 0.01) y = (np.random.random(x.size) + 1) * 100 y[19700:20300] += reprofile([peak])[1] return x, y
def test_reprofile(self): peaks = self.make_data() x, y = reprofile(peaks) repicked = pick_peaks(x, y) diff = repicked[0].mz - peaks[0].mz assert abs(diff) < 1e-3