cremerlab / hplc-py

A Python utility for the processing and quantification of chromatography data
https://cremerlab.github.io/hplc-py/
GNU General Public License v3.0
34 stars 5 forks source link

ValueError: `x0` is infeasible. error on fit peaks #15

Closed sardnar closed 3 months ago

sardnar commented 7 months ago

Hello,

I keep getting this error message when trying to use fit_peaks:

ValueError: x0 is infeasible.

I am able to run signal.find_peaks correctly like here. https://cremerlab.github.io/hplc-py/methodology/peak_detection.html

Thanks!

gchure commented 7 months ago

Hi @sardnar, that sounds like there are some bounds issues showing up (also discussed in #14 ). Could you provide more information about the Chromatogram? Could you share a minimal failing example for the error?

sardnar commented 6 months ago

Hi @gchure,

sorry for late replay. I tried to turn off baseline correction but still didn't work. I am attaching csv file here if you may help. This data is from LCMS

raw3.csv

gchure commented 6 months ago

Thanks for sharing. I took a quick look at the chromatogram and it seems severely undersampled--plotting the chromatogram yields this:

Screenshot 2024-05-07 at 12 30 17

There look to be ≈ 20 peaks, but only 73 measurements. Playing around with the peak detection algorithm yields peak windows that are only 3 timepoints wide. I suspect this is where the x0 is infeasible shows up -- the initial guesses for the parameters (I would bet for the width) end up in contradiction between what is present in the data.

Unfortunately, I do not think that hplc-py can be used to fit data this sparsely sampled. I would bet that other software applied to this data would also fail, or worse, return an integrated peak area.

sardnar commented 6 months ago

@gchure my bad. Actually this one is almost a blank. I am uploading a better peak. The raised error now is:

scipy\optimize\_lsq\least_squares.py", line 816, in least_squares
    raise ValueError("Each lower bound must be strictly less than each "

raw3.csv

gchure commented 6 months ago

Thanks, that looks more reasonable. This is a very complex chromatogram with lots of narrow, overlapping peaks. What are you looking to quantify here? Are you after the major peaks, like the one at 1.75? Or are you after getting the small peaks as well?

When I adjust the prominence and expand the amplitude bounds, I can fit the major peak at 1.75

Screenshot 2024-05-07 at 13 33 27

I think there's a general problem here at how the parameter bounds are tuned for each window when the peaks are very short lived (in terms of number of time points). I suspect this can be dealt with in part by adjusting the parameter bounds (through the param_bounds keyword argument in fit_peaks), though it seems to be fickle.

sardnar commented 6 months ago

@gchure thank you for looking into that .I don't understand param_bounds, what is it and how it could it adjusted?

sardnar commented 6 months ago

reproducible example to what to do is appreciated

gchure commented 6 months ago

The bounding on the parameters are described here in the documentation: https://cremerlab.github.io/hplc-py/methodology/fitting.html#Default-settings-for-initial-guesses-of-parameters

You can also read about fit_peaks here: https://cremerlab.github.io/hplc-py/quant.html#hplc.quant.Chromatogram.fit_peaks

See the section that explains what param_bounds does.

For the chromatogram I shared above, here is the code used:


import numpy as np 
import pandas as pd 
from hplc.quant import Chromatogram
data = pd.read_csv('./raw3.csv')
chrom = Chromatogram(data, cols={'time':'RT', 'signal':'Intensity'})
peaks = chrom.fit_peaks(correct_baseline=False,
                        param_bounds={'amplitude':[0.001, 100],
                                      'scale':[0, np.inf]})
gchure commented 3 months ago

Hi @sardnar, hope you're doing well! I wanted to let you know that I pushed a new version of hplc-py (v0.2.7) which should address this problem. Note that your data is still a bit undersampled, with peaks not having a lot of direct measurements.

I'm going to close this issue for now, but please reopen if you need more help or if the new version doesn't help your issues.