kbknudsen / PyEIS

PyEIS: A Python-based Electrochemical Impedance Spectroscopy simulator and analyzer
Apache License 2.0
177 stars 61 forks source link

UnboundLocalError: local variable 'R' referenced before assignment #16

Open FabianPascher opened 2 years ago

FabianPascher commented 2 years ago

I use the R-RQ-RQ Fit Function [cir_RsRQRQ()] with

fit_params=Parameters() fit_params.add('Rs', value=190, min=200, max=2000) fit_params.add('R', value=150, min=100, max=4000) fit_params.add('Q', value=10-5, min=10-7, max=10-3) fit_params.add('n', value=.8, min=.5, max=1) fit_params.add('fs', value=10-4, min=10-6, max=10-3) fit_params.add('R2', value=2500, min=1000, max=4000) fit_params.add('Q2', value=10-4, min=10-6, max=10-3) fit_params.add('n2', value=.8, min=.7, max=1) fit_params.add('fs2', value=10-5, min=10-7, max=10-3)

What I receive is:

Traceback (most recent call last): File "C:\Users------------.py", line 73, in Lennard_HIAT_C_2_5DC_80.EIS_fit(circuit='R-RQ-RQ', params=fit_params, weight_func='modulus') File "C:\Users\fab54611\Anaconda3\lib\site-packages\PyEIS\PyEIS.py", line 4028, in EIS_fit self.Fit.append(minimize(leastsq_errorfunc, params, method='leastsq', args=(self.df[i].w.values, self.df[i].re.values, self.df[i].im.values, circuit, weight_func), nan_policy=nan_policy, maxfev=9999990)) File "C:\Users--\Anaconda3\lib\site-packages\lmfit\minimizer.py", line 2583, in minimize return fitter.minimize(method=method) File "C:\Users--\Anaconda3\lib\site-packages\lmfit\minimizer.py", line 2352, in minimize return function(kwargs) File "C:\Users--\Anaconda3\lib\site-packages\lmfit\minimizer.py", line 1689, in leastsq lsout = scipy_leastsq(self.__residual, variables, lskws) File "C:\Users--\Anaconda3\lib\site-packages\scipy\optimize\minpack.py", line 410, in leastsq shape, dtype = _check_func('leastsq', 'func', func, x0, args, n) File "C:\Users--\Anaconda3\lib\site-packages\scipy\optimize\minpack.py", line 24, in _check_func res = atleast_1d(thefunc(((x0[:numinputs],) + args))) File "C:\Users--\Anaconda3\lib\site-packages\lmfit\minimizer.py", line 587, in __residual out = self.userfcn(params, self.userargs, self.userkws) File "C:\Users--\Anaconda3\lib\site-packages\PyEIS\PyEIS.py", line 2128, in leastsq_errorfunc re_fit = cir_RsRQRQ_fit(params, w).real File "C:\Users--\Anaconda3\lib\site-packages\PyEIS\PyEIS.py", line 1249, in cir_RsRQRQ_fit return Rs + (R/(1+RQ(w*1j)*n)) + (R2/(1+R2Q2(w1j)n2)) UnboundLocalError: local variable 'R' referenced before assignment

How to fix this?

FabianPascher commented 2 years ago

The solution is pretty easy. I made the mistake, that I defined the summit frequencies fs as well as the constant phase elements Q. This resulted in an overdetermined system. For my case, it makes sense to leave the Q and Q2 parameters out. In general, you have to use three of the parameters in systems that contain R, Q, n and fs. For more details, see the Equivalent Circuit Fitting Chapter in the very well written kbknudsen/PyEIS/blob/master/Tutorials/PyEIS_simulation_tutorial.ipynb Thanks a lot for the library!