fooof-tools / fooof

Parameterizing neural power spectra into periodic & aperiodic components.
https://fooof-tools.github.io/
Apache License 2.0
345 stars 93 forks source link

Performance "regression" in 2.0.0rc2 #326

Open toni-neurosc opened 2 weeks ago

toni-neurosc commented 2 weeks ago

Hi, first of all, thank you very much for developing and maintaining this package. I'm one of the contributors of py_neuromodulation, a Python tool for feature extraction from neurophysiology recordings. We include FOOOF as one of the available features that we can calculate during data analysis, and recently I came across the new renamed version SpecParam. Although we don't have a strong reason to update I wanted to give the new version a try, since the numerical methods used by FOOOF are computationally expensive and difficult to run in real-time when we want to run our analysis online.

However when I replaced the FOOOF class with SpectralModel, I noticed I was getting different output values and also that it was running 2x as slow as FOOOF. I started digging and figured out this only happens with the newer release candidate 2.0.0rc2, which is the version that pip is currently installing, while with rc1 I get the exact same results as with the FOOOF package v1.1.

It seems that the reason for the change in fitted parameters and speed is that in rc2 SciPy's curve_fit is now being called with the default xtol, ftol and gtol parameters, as opposed to the 1E-5 set in #299 . Once I add back the tolerance to curve_fit, the results and the computation time come much closer, and once the jacobian_gauss is added back, the results are identical (the remaining missing parameter check_finite=False makes no difference)

Is there a reason for this change? I understand that there might be a concern with the precision of the model fit, but the doubling in the computation time is a bit expensive in use-cases like ours when we would want to be able to output real-time feature calculations. Of course, one could argue that for online analysis spectral parametrization is just not a viable metric, but I was hoping that perhaps with future performance increases it would come closer to real-time.

So, to sum up:

EDIT: one more question

TomDonoghue commented 5 days ago

hey @toni-neurosc - thanks for detailed check in here, and sorry for a slow (and currently a bit limited) answer as I'm currently traveling.

I believe what has happened here is that you have discovered an accidental mistake in me merging things together as work for specparam2.0 became spread across multiple branches! At some point I had a bit of a complicated merge & refactor and I was pretty sure I didn't miss anything, but you are right that current rc2 is missing some changes from #299, and that is not on purpose! I will make sure it all gets merged back in - though it might be another week or two before I get to it - I realize now I need to do a bit of an audit across the branches and check through and figure out where the mistake is and double check everything does come back to merge all together, with all the updates.

Apologies for the confusion here, and thank you for the deep dive that helped figure this out! I'll follow up on this issue once I get the chance to work through and get things fixed!