somexlab / fastddm

Python library for Differential Dynamic Microscopy analysis
https://fastddm.readthedocs.io/
GNU General Public License v3.0
3 stars 0 forks source link

polyfit for azimuthal average does not work in noise_est.py #162

Closed moyuanchen closed 1 year ago

moyuanchen commented 1 year ago

Description Using the 'polyfit' mode in fastddm.noise_est.estimate_camera_noise for an azimuthalaverage dataclass gives the following error

To Reproduce Steps to reproduce the behavior:

  1. Load an AzimuthalAverage dataclass
  2. Run estimate_camera_noise(AzimuthalAverage, mode = 'polyfit')
  3. See error:
  4. estimate_camera_noise(azi_210, mode='polyfit') ~:318: RuntimeWarning: divide by zero encountered in divide ~\AppData\Local\anaconda3\envs\fastddm\lib\site-packages\numpy\lib\polynomial.py:667: RuntimeWarning: invalid value encountered in divide lhs /= scale Traceback (most recent call last):

    Cell In[223], line 1 estimate_camera_noise(azimuthal_average, mode='polyfit')

    File c:\users\mchen\desktop\cerbino rotation\code\fastddm\untitled2.py:775 in estimate_camera_noise

    File c:\users\mchen\desktop\cerbino rotation\code\fastddm\untitled2.py:318 in _noise_polyfit_az_avg

    File <__array_function__ internals>:180 in polyfit

    File ~\AppData\Local\anaconda3\envs\fastddm\lib\site-packages\numpy\lib\polynomial.py:668 in polyfit c, resids, rank, s = lstsq(lhs, rhs, rcond)

    File <__array_function__ internals>:180 in lstsq

    File ~\AppData\Local\anaconda3\envs\fastddm\lib\site-packages\numpy\linalg\linalg.py:2300 in lstsq x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj)

    File ~\AppData\Local\anaconda3\envs\fastddm\lib\site-packages\numpy\linalg\linalg.py:101 in _raise_linalgerror_lstsq raise LinAlgError("SVD did not converge in Linear Least Squares")

LinAlgError: SVD did not converge in Linear Least Squares

Intel MKL ERROR: Parameter 4 was incorrect on entry to DGELSD.

Expected behavior Expected returning (noise, err)

Desktop (please complete the following information):

Additional context 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz 2.80 GHz 64 bit

enrico-lattuada commented 1 year ago

Could you please provide a representation of your azimuthal average object?

enrico-lattuada commented 1 year ago

The bug was due to the presence of zeros in the _err of the AzimuthalAverage object. This happens when just 1 point is present in the bin (e.g., in k=0), so the _err is initialized and the contribution to its value is 0. However, the variance/standard deviation is infinite or not defined for just 1 point. Therefore, we decide to fix it to nan.

The check for nans is then performed in the noise_est functions.

enrico-lattuada commented 1 year ago

Closed by PR #163