Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
FabianPascher opened this issue Jan 26, 2022 · 1 comment
Open

Comments

@FabianPascher
Copy link

FabianPascher commented Jan 26, 2022

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+R
Q
(w1j)**n)) + (R2/(1+R2Q2*(w*1j)**n2))
UnboundLocalError: local variable 'R' referenced before assignment

How to fix this?

@FabianPascher
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant