normal TypeError with the python script do_fit.py

  • Hot
  • Hot's Avatar Topic Author
  • Visitor
4 years 4 months ago #8363 by Hot
I am doing CG simulation and initial guess for angle bending parameters,according to http://cgmartini.nl/index.php/tutorials-general-introduction-gmx5/bilayers-gmx5#Adv-refinehttp://cgmartini.nl/index.php/tutorials-general-introduction-gmx5/bilayers-gmx5#Adv-refine .
The python script do_fit.py was used, and the command is as follows:
python do_fit.py -f ANGLES/cga_0-distr.xvg -x0 100 -k 400 --harmonic --radians

Returned results:
Parameter Value Standard Deviation
par_0 1.055004e+02 4.039648e-01
par_1 8.000000e+02 1.338068e+02
Status message CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL
Number of iterations 4
Objective <symfit.core.objectives.LeastSquares object at 0x2ae29ab84810>
Minimizer <symfit.core.minimizers.LBFGSB object at 0x2ae29ab67650>

Goodness of fit qualifiers:
chi_squared 0.631567762071
objective_value 0.315783881035
r_squared 0.679987456214
Traceback (most recent call last):
File "do_fit.py", line 135, in <module>
plt.plot(xs, model(x=xs, **result.params), color='r')
File "/home/stjt/miniconda3/envs/do-fit/lib/python2.7/site-packages/symfit/core/operators.py", line 90, in call
bound_arguments = signature.bind(*values, **relevant_named_values)
File "/home/stjt/miniconda3/envs/do-fit/lib/python2.7/site-packages/funcsigs/__init__.py", line 792, in bind
return args[0]._bind(args[1:], kwargs)
File "/home/stjt/miniconda3/envs/do-fit/lib/python2.7/site-packages/funcsigs/__init__.py", line 717, in _bind
raise TypeError(msg)
TypeError: 'var_0' parameter lacking default value

Where need to modify?

Please Log in or Create an account to join the conversation.

More
4 years 4 months ago #8365 by peterkroon
Replied by peterkroon on topic TypeError with the python script do_fit.py
Hi,

to be honest, I haven't looked at or maintained that script in quite some time.

First, upgrade to Python3. After January 1 2020 Python2 will no longer be supported by anyone in the world.
Then, for the script:
In lines 110-112:
x = Variable() * args.factor
x0 = Parameter(value=args.x0, min=np.min(xs), max=np.max(xs)) * args.factor
k = Parameter(value=args.k, min=args.mink, max=args.maxk)
Should be:
x = Variable('x') * args.factor
x0 = Parameter('x0', value=args.x0, min=np.min(xs), max=np.max(xs)) * args.factor
k = Parameter('k', value=args.k, min=args.mink, max=args.maxk)

Line 135:
plt.plot(xs, model(x=xs, **result.params), color='r')
should be
plt.plot(xs, model(x=xs, **result.params)[0], color='r')

Let me know if it works, so I can update the tutorial files.

Please Log in or Create an account to join the conversation.

  • Hot
  • Hot's Avatar Topic Author
  • Visitor
4 years 4 months ago #8366 by Hot
I have modified the script following your prompt,and run it with Python2 and Python3 respectively. The error still exists.
Python2:
/home/miniconda3/envs/do-fit/lib/python2.7/site-packages/symfit/core/fit_results.py:114: RuntimeWarning: invalid value encountered in sqrt
return np.sqrt(self.variance(param))

Parameter Value Standard Deviation
par_0 9.800000e+01 nan
par_1 5.000000e+01 3.055364e+01
Status message CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL
Number of iterations 2
Objective <symfit.core.objectives.LeastSquares object at 0x2b15e24577d0>
Minimizer <symfit.core.minimizers.LBFGSB object at 0x2b15e243b610>

Goodness of fit qualifiers:
chi_squared 5.44559526318
objective_value 2.72279763159
r_squared -1.75925862157
Traceback (most recent call last):
File "do_fit.py", line 135, in <module>
plt.plot(xs, model(x=xs, **result.params), color='r')
File "/home/stjt/miniconda3/envs/do-fit/lib/python2.7/site-packages/symfit/core/operators.py", line 90, in call
bound_arguments = signature.bind(*values, **relevant_named_values)
File "/home/stjt/miniconda3/envs/do-fit/lib/python2.7/site-packages/funcsigs/__init__.py", line 792, in bind
return args[0]._bind(args[1:], kwargs)
File "/home/stjt/miniconda3/envs/do-fit/lib/python2.7/site-packages/funcsigs/__init__.py", line 717, in _bind
raise TypeError(msg)
TypeError: 'var_0' parameter lacking default value

Python3:
do_fit.py:92: DeprecationWarning: inspect.getargspec() is deprecated since Python 3.0, use inspect.signature() or inspect.getfullargspec()
if 'n' not in inspect.getargspec(args.func).args and hasattr(args, 'n'):
do_fit.py:95: DeprecationWarning: inspect.getargspec() is deprecated since Python 3.0, use inspect.signature() or inspect.getfullargspec()
if not hasattr(args, 'n') and 'n' in inspect.getargspec(args.func).args:
do_fit.py:110: DeprecationWarning: It is recommended to provide names to Variable explicitly as automatic generation of names will be dropped in future `symfit` versions.
x = Variable() * args.factor
qt.qpa.xcb: QXcbConnection: XCB error: 145 (Unknown), sequence: 177, resource id: 0, major code: 139 (Unknown), minor code: 20
/home/stjt/miniconda3/lib/python3.7/site-packages/symfit/core/fit_results.py:114: RuntimeWarning: invalid value encountered in sqrt
return np.sqrt(self.variance(param))

Parameter Value Standard Deviation
par_0 9.800000e+01 nan
par_1 5.000000e+01 3.055364e+01
Status message b'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL'
Number of iterations 2
Objective <symfit.core.objectives.LeastSquares object at 0x2aca22deaf90>
Minimizer <symfit.core.minimizers.LBFGSB object at 0x2aca22deaad0>

Goodness of fit qualifiers:
chi_squared 5.4455952631815085
objective_value 2.7227976315907543
r_squared -1.759258621573057
Traceback (most recent call last):
File "do_fit.py", line 135, in <module>
plt.plot(xs, model(x=xs, **result.params), color='r')
File "/home/stjt/miniconda3/lib/python3.7/site-packages/symfit/core/operators.py", line 90, in call
bound_arguments = signature.bind(*values, **relevant_named_values)
File "/home/stjt/miniconda3/lib/python3.7/inspect.py", line 3015, in bind
return args[0]._bind(args[1:], kwargs)
File "/home/stjt/miniconda3/lib/python3.7/inspect.py", line 2930, in _bind
raise TypeError(msg) from None
TypeError: missing a required argument: 'var_0'

Please Log in or Create an account to join the conversation.

More
4 years 4 months ago #8371 by peterkroon
Replied by peterkroon on topic TypeError with the python script do_fit.py
Interesting :)

While I start digging, could you get me the output of
pip show symfit
?

And could you double check you made all the modifications?
File "do_fit.py", line 135, in <module>
plt.plot(xs, model(x=xs, **result.params), color='r')
still looks like the old code.

Please Log in or Create an account to join the conversation.

More
4 years 4 months ago #8372 by peterkroon
Replied by peterkroon on topic TypeError with the python script do_fit.py
Ok, I made some modifications and get the following output:
$ python3 -V
Python 3.6.8

$ pip3 show symfit
Name: symfit
Version: 0.5.2
...

$ python3 do_fit.py -f ANGLES/cga_0-distr.xvg -x0 135 -k 35 --harmonic --radians

Parameter Value        Standard Deviation
k         1.878271e+01 4.340405e-01
x0        1.317686e+02 2.847421e-01
Status message         b'CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR*EPSMCH'
Number of iterations   10
Objective              <symfit.core.objectives.LeastSquares object at 0x7feff9425a58>
Minimizer              <symfit.core.minimizers.LBFGSB object at 0x7feff942eba8>

Goodness of fit qualifiers:
chi_squared            0.4169061723299059
objective_value        0.20845308616495295
r_squared              0.9715937509657963
And for Python2:
$ python2 -V
Python 2.7.15+

$ pip2 show symfit
Name: symfit
Version: 0.5.2
...

$ python2 do_fit.py -f ANGLES/cga_0-distr.xvg -x0 135 -k 35 --harmonic --radians

Parameter Value        Standard Deviation
k         1.878271e+01 4.340405e-01
x0        1.317686e+02 2.847421e-01
Status message         CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR*EPSMCH
Number of iterations   10
Objective              <symfit.core.objectives.LeastSquares object at 0x7f373456f350>
Minimizer              <symfit.core.minimizers.LBFGSB object at 0x7f375fb9fcd0>

Goodness of fit qualifiers:
chi_squared            0.41690617233
objective_value        0.208453086165
r_squared              0.971593750966


The changes I made:
$ diff do_fit_original.py do_fit.py
8c8
< from symfit.api import exp, cos, Variable, Parameter, Fit
---
> from symfit import exp, cos, Variable, Parameter, Fit, Model
110,112c110,113
< x = Variable() * args.factor
< x0 = Parameter(value=args.x0, min=np.min(xs), max=np.max(xs)) * args.factor
< k = Parameter(value=args.k, min=args.mink, max=args.maxk)
---
> y = Variable('y')
> x = Variable('x') * args.factor
> x0 = Parameter('x0', value=args.x0, min=np.min(xs), max=np.max(xs)) * args.factor
> k = Parameter('k', value=args.k, min=args.mink, max=args.maxk)
117c118
<     n = Parameter(value=args.n, min=0, max=args.n * 2, fixed=True)
---
>     n = Parameter('n', value=args.n, min=0, max=args.n * 2, fixed=True)
122c123
< model = harmonic_distribution(**argument_dict)
---
> model = Model({y: harmonic_distribution(**argument_dict)})
124c125
< guess = symfit.contrib.interactive_guess.interactive_guess.InteractiveGuess2D(model, xs, ys) 
---
> guess = symfit.contrib.interactive_guess.interactive_guess.InteractiveGuess2D(model, x=xs, y=ys) 
129c130
< fit = Fit(model, xs, ys)
---
> fit = Fit(model, x=xs, y=ys)
135c136
< plt.plot(xs, model(x=xs, **result.params), color='r')
---
> plt.plot(xs, model(x=xs, **result.params).y, color='r')

Could you give these a try? If this doesn't work for you there's something very strange going on, and I'll have to dig deeper. If it works for you I'll update the tutorial.

Please Log in or Create an account to join the conversation.

  • Hot
  • Hot's Avatar Topic Author
  • Visitor
4 years 4 months ago #8373 by Hot
I have modified do-fit.py and get the output:
symfit 0.5.2
Python3.7.3
do_fit.py:92: DeprecationWarning: inspect.getargspec() is deprecated since Python 3.0, use inspect.signature() or inspect.getfullargspec()
if 'n' not in inspect.getargspec(args.func).args and hasattr(args, 'n'):
do_fit.py:95: DeprecationWarning: inspect.getargspec() is deprecated since Python 3.0, use inspect.signature() or inspect.getfullargspec()
if not hasattr(args, 'n') and 'n' in inspect.getargspec(args.func).args:
Traceback (most recent call last):
File "do_fit.py", line 122, in <module>
model = Model({y: harmonic_distribution(**argument_dict)})
File "do_fit.py", line 43, in boltzmann
return exp(-V(*args, **kwargs)/kbT)
File "do_fit.py", line 47, in harmonic
return 0.5 * k * (x - x0)**2
TypeError: unsupported operand type(s) for *: 'float' and '_StoreAction'

Python2.7
Traceback (most recent call last):
File "do_fit.py", line 122, in <module>
model = Model({y: harmonic_distribution(**argument_dict)})
File "do_fit.py", line 43, in boltzmann
return exp(-V(*args, **kwargs)/kbT)
File "do_fit.py", line 47, in harmonic
return 0.5 * k * (x - x0)**2
TypeError: unsupported operand type(s) for *: 'float' and '_StoreAction'

The result is different from yours. I don't know what the problem is.

Please Log in or Create an account to join the conversation.

More
4 years 4 months ago #8381 by peterkroon
Replied by peterkroon on topic TypeError with the python script do_fit.py
Very strange.

It also works for me on Python 3.7.4 (although I do get the deprecation warning about inspect.signature).
Could you try `pip install --force-reinstall symfit matplotlib`? My guess is your sympy version got too old (1.4 here; `pip show sympy`).

I'll put my full code below, just to be sure.


Warning: Spoiler! [ Click to expand ]

Please Log in or Create an account to join the conversation.

Time to create page: 0.112 seconds