def windowed_spectrum(mz_array, intensity_array, window_size=1.):
    mz_min = mz_array.min()
    mz_max = mz_array.max()

    step_size = window_size / 2.
    center_mz = mz_min + step_size
    center_i = get_nearest(mz_array, center_mz, 1)

    windows = []

    niter = 0

    while center_mz < mz_max:
        lo_mz = center_mz - step_size
        hi_mz = center_mz + step_size

        lo_i = get_nearest(mz_array, lo_mz, center_i)
        hi_i = get_nearest(mz_array, hi_mz, center_i)
        win = Window(mz_array[lo_i:hi_i + 1],
                     intensity_array[lo_i:hi_i + 1], lo_i, hi_i)
        win.center_mz = center_mz
        windows.append(win)

        center_mz = center_mz + window_size
        center_i = get_nearest(mz_array, center_mz, center_i)

        niter += 1

    return windows
Пример #2
0
 def find_intersects(self, fit_bin=0):
     starts = []
     ends = []
     bin_fit = self.binned_fits[fit_bin]
     for i, feat_fit in enumerate(self.fits):
         baseline = self.baselines[i]
         xs = feat_fit.xs
         ys = feat_fit.compute_fitted()
         center_ix = search.get_nearest(xs, bin_fit.center, 0)
         left_ix = self.find_left_intersect(ys, baseline, center_ix)
         right_ix = self.find_right_intersect(ys, baseline, center_ix)
         starts.append(xs[left_ix])
         ends.append(xs[right_ix])
     return starts, ends
Пример #3
0
 def find_intersects(self, fit_bin=0):
     starts = []
     ends = []
     bin_fit = self.binned_fits[fit_bin]
     for i, feat_fit in enumerate(self.fits):
         baseline = self.baselines[i]
         xs = feat_fit.xs
         ys = feat_fit.compute_fitted()
         center_ix = search.get_nearest(xs, bin_fit.center, 0)
         left_ix = self.find_left_intersect(ys, baseline, center_ix)
         right_ix = self.find_right_intersect(ys, baseline, center_ix)
         starts.append(xs[left_ix])
         ends.append(xs[right_ix])
     return starts, ends
Пример #4
0
    def set_up_peak_fit(self, ys=None, min_height=0, peak_count=0):
        xs = self.xs
        if ys is None:
            ys = self.ys

        params = self.shape_fitter.guess(xs, ys)
        params_dict = self.shape_fitter.params_to_dict(params)
        # If the guessed amplitude is less than 0, we are virtually guaranteed to never
        # improve.
        if params_dict['amplitude'] < 1:
            params_dict['amplitude'] = 1
        indices = peak_indices(ys, min_height)
        if len(indices) > 0:
            center = xs[max(indices, key=lambda x: ys[x])]
        else:
            center = xs[len(xs) // 2]
        params_dict['center'] = center

        fit = self.shape_fitter.leastsq(xs, ys, list(params_dict.values()))
        params = fit[0]
        params_dict = self.shape_fitter.wrap_parameters(params)
        self.params_dict_list.append(params_dict)

        residuals = self.shape_fitter.error(params, xs, ys)

        fitted_apex_index = search.get_nearest(xs, params_dict['center'], 0)
        fitted_apex = ys[fitted_apex_index]

        new_min_height = fitted_apex * self.relative_peak_height_threshold

        if new_min_height < min_height:
            min_height *= 0.85
        else:
            min_height = new_min_height

        indices = peak_indices(residuals, min_height)

        peak_count += 1
        if indices.size and peak_count < self.max_peaks:
            residuals, params_dict = self.set_up_peak_fit(
                residuals, min_height, peak_count=peak_count)

        return residuals, params_dict
Пример #5
0
    def set_up_peak_fit(self, ys=None, min_height=0, peak_count=0):
        xs = self.xs
        if ys is None:
            ys = self.ys
        params = self.shape_fitter.guess(xs, ys)
        params_dict = self.shape_fitter.params_to_dict(params)

        indices = peak_indices(ys, min_height)
        if len(indices) > 0:
            center = xs[max(indices, key=lambda x: ys[x])]
        else:
            center = xs[len(xs) / 2]
        params_dict['center'] = center

        fit = leastsq(self.shape_fitter.fit, params_dict.values(), (xs, ys))
        params = fit[0]
        params_dict = FittedPeakShape(self.shape_fitter.params_to_dict(params),
                                      self.shape_fitter)
        self.params_list.append(params)
        self.params_dict_list.append(params_dict)

        residuals = self.shape_fitter.fit(params, xs, ys)

        fitted_apex_index = search.get_nearest(xs, params_dict['center'], 0)
        fitted_apex = ys[fitted_apex_index]

        new_min_height = fitted_apex * 0.5

        if new_min_height < min_height:
            min_height *= 0.85
        else:
            min_height = new_min_height

        indices = peak_indices(residuals, min_height)

        peak_count += 1
        if indices.size and peak_count < self.max_peaks:
            residuals, params_dict = self.set_up_peak_fit(
                residuals, min_height, peak_count=peak_count)

        return residuals, params_dict
Пример #6
0
    def set_up_peak_fit(self, ys=None, min_height=0, peak_count=0):
        xs = self.xs
        if ys is None:
            ys = self.ys
        params = self.shape_fitter.guess(xs, ys)
        params_dict = self.shape_fitter.params_to_dict(params)

        indices = peak_indices(ys, min_height)
        if len(indices) > 0:
            center = xs[max(indices, key=lambda x: ys[x])]
        else:
            center = xs[len(xs) / 2]
        params_dict['center'] = center

        fit = leastsq(self.shape_fitter.fit,
                      params_dict.values(), (xs, ys))
        params = fit[0]
        params_dict = FittedPeakShape(self.shape_fitter.params_to_dict(params), self.shape_fitter)
        self.params_list.append(params)
        self.params_dict_list.append(params_dict)

        residuals = self.shape_fitter.fit(params, xs, ys)

        fitted_apex_index = search.get_nearest(xs, params_dict['center'], 0)
        fitted_apex = ys[fitted_apex_index]

        new_min_height = fitted_apex * 0.5

        if new_min_height < min_height:
            min_height *= 0.85
        else:
            min_height = new_min_height

        indices = peak_indices(residuals, min_height)

        peak_count += 1
        if indices.size and peak_count < self.max_peaks:
            residuals, params_dict = self.set_up_peak_fit(residuals, min_height, peak_count=peak_count)

        return residuals, params_dict
def average_profile_scans(scan_arrays, width=0.01):
    groups = [[arr.astype(float) for arr in group] for group in scan_arrays]

    mzs = set()
    for mz_array, intensity_array in groups:
        mzs.update(mz_array)

    mzs = sorted(mzs)
    mz_out = []
    intensity_out = []

    _abs = abs
    _len = len

    for mz in mzs:
        cluster_mzs = []
        cluster_intensities = []
        for group in groups:
            mz_array, intensity_array = group
            left_ix = search.get_nearest(mz_array, mz - width, 0)
            left_mz = mz_array[left_ix]
            err = (left_mz - (mz - width))
            abs_err = _abs(err)
            if abs_err > width:
                if err > 0 and left_ix != 0:
                    left_ix -= 1
                elif left_ix != _len(mz_array) - 1:
                    left_ix += 1

            left_mz = mz_array[left_ix]
            err = (left_mz - (mz - width))
            if _abs(err) > (2 * width):
                continue

            right_ix = search.get_nearest(mz_array, mz + width, 0)
            right_mz = mz_array[right_ix]
            err = (right_mz - (mz + width))
            abs_err = _abs(err)
            if abs_err > width:
                if err > 0:
                    right_ix -= 1
                elif right_ix != _len(mz_array) - 1:
                    right_ix += 1

            right_mz = mz_array[right_ix]
            err = (right_mz - (mz + width))
            abs_err = _abs(err)
            if abs_err > (2 * width):
                continue

            mz_values = mz_array[left_ix:(right_ix + 1)]
            intensity_values = intensity_array[left_ix:(right_ix + 1)]

            cluster_mzs.extend(mz_values)
            cluster_intensities.extend(intensity_values)

        cluster_mzs = np.array(cluster_mzs)
        cluster_intensities = np.array(cluster_intensities)
        ix = np.argsort(cluster_mzs)
        cluster_mzs = cluster_mzs[ix]
        cluster_intensities = cluster_intensities[ix]

        u = np.mean(cluster_mzs)
        sd = np.std(cluster_mzs)
        gauss_weights = np.exp(-((cluster_mzs - u) ** 2) / (2 * (sd ** 2)))
        intensity = (gauss_weights * cluster_intensities).sum() / \
            gauss_weights.sum()
        mz_out.append(mz)
        intensity_out.append(intensity)
    return np.array(mz_out, dtype=np.float64), np.array(intensity_out, dtype=np.float64)