user6688825 user6688825 - 3 months ago 13
Python Question

Complete python newbie...init unexpected keyword argument python

I'm a complete python/programming novice just trying to run this script for chemistry research.

In case I didn't copy the script correctly here's the link to download the files:
http://pubs.acs.org/doi/suppl/10.1021/acs.analchem.5b02258

This instruction may be helpful:
"fsolve_withPT.py takes two command line arguments: input file name of “MamPol2_titration_data.txt” and output file name of “Kaps_result.txt”"

When I run the script on ipython I get this error message: init() got an unexpected keyword argument 'step_max'

Thank you in advance!

#University of California San Francisco
#Supplemental for
#
#A model for specific and nonspecific binding of ligand to multi-protein complexes by native mass spectrometry
#
#Shenheng Guan, et al
#2015
#

import sys
import math
import numpy
import warnings
from scipy.optimize import fsolve,fmin
import matplotlib.pyplot as plt
warnings.filterwarnings('ignore')

input_fn='MamPol2_titration_data.txt'
output_fn='Kaps_result1.txt'
##input_fn=sys.argv[1]
##output_fn=sys.argv[1]

fid=open(input_fn,'r')
line=fid.readline()
line=line.strip('\n')
no_mam=[float(x) for x in line.split('\t')[1:]]
line=fid.readline()
data=[]
conc=[]
for line in fid:
line=line.strip('\n')
tmp0=line.split('\t')
conc.append(float(tmp0[0]))
tmp1=[float(x) for x in tmp0[1:]]
data.append(tmp1)
fid.close()

class fsolve_withPT:
def __init__(self,conc,data):
self.conc = conc
self.data = data

def ff(self, x, Kas, LT, PT):#x[0]:[P];x[1]:[PL]...;x[n]:[PLn];x[-1]:[L] n+2 (10)
fc=[]
for j in range(0,len(x)-2):#setup equilibrium equations
fc.append(Kas[j]*x[j]*x[-1]-x[j+1])

#mass conservation for P
tmpP=0.0#[P]
for j in range(0,len(x)-1):#x[0] to x[8] or [P] to [PL8]
tmpP=tmpP+x[j]
fc.append(tmpP-PT)#PT equals to all P species combined

#mass conservation for L
tmpL=x[-1]#[L]
for j in range(1,len(x)-1):
tmpL=tmpL+j*x[j]
fc.append(tmpL-LT)

return fc

def error(self,w):
Kas=w[:-1]
PT=w[-1]
mySum=0.0
for m in range(0,len(self.conc)):#over conc (LT)
#print Kas,self.conc[m],PT
F=fsolve(self.ff, [1.0]*10, args=(Kas, self.conc[m], PT))
myPT=sum(F[:-1])
for k in range(0, len(no_mam)):#over # of Mam
mySum=mySum+(F[k]/myPT-self.data[m][k])**2
return mySum

w0=[8,7,6,5,4,3,2,1,0]
w0=numpy.array(w0)
w0=w0*3.01e4
w0[-1]=5.e-6

myFclass=fsolve_withPT(conc,data)

w, fopt, iter, funcalls, warnflag = fmin(myFclass.error, w0, maxiter=2000,
maxfun=2000, full_output=True,disp=True)

#http://nullege.com/codes/show/src@n@u@Numdifftools-0.6.0@numdifftools@speed_comparison@run_benchmarks.py/73/numdifftools.Hessian
import numdifftools as nd
#my_step_nom=[1.0e3]*8+[1.0e-6]*1
my_step_nom=w#*1.0e-3
hessian = nd.Hessian(myFclass.error,step_max=1.0e-2,step_nom=my_step_nom)#, step_max=1.0, step_nom=numpy.abs(w))
H = hessian(w)
covH=numpy.linalg.inv(H)

conc0=conc#numpy.linspace(0.0,6.0E-05,num=101).tolist()
y0=[]
for tmp in conc0:
F=fsolve(myFclass.ff, [1.0]*10, args=(w[:-1], tmp,w[-1]))
y0.append(F)
#y0=myFunc(conc0,w)

fid=open(output_fn,'w')
fid.write('Calculated complex conc. (M)\t'+str(w[-1])+'\n')
fid.write('# of Mam in Complex\t')
for j in no_mam:
fid.write(str(j)+'\t')
fid.write('\n')
fid.write('Associate constants (Kas)\t\t')
for j in no_mam[:-1]:
fid.write(str(w[j])+'\t')
fid.write('\n')
fid.write('Mam Conc. (M)\tSimulated abundances\n')
for k in range(0,len(y0)):
fid.write(str(conc0[k])+'\t')
yc=y0[k]
tmp=sum(yc[:-1])
for j in range(0,len(yc)-2):
fid.write(str(yc[j]/tmp)+'\t')
fid.write(str(yc[-2])+'\n')
fid.close()

from scipy import stats
SS=fopt
DF=len(data)*len(data[0])-len(w)
t_factor=stats.t.ppf(0.95, DF)

SE=[]
dw=[]

for j in range(0,len(w)):
SE.append(numpy.sqrt(SS/DF*numpy.abs(covH[j,j])))
for j in range(0,len(w)):
dw.append(SE[j]*t_factor)

Aur Aur
Answer

you are unable to run this code becuase the paper's code is incorrect. I even downloaded the code from the link you posted to make sure I had the correct code. I was also able to reproduce your error. I'll try to explain what is going on and what you might be able to do in light of this.

The error init() got an unexpected keyword argument 'step_max' essentially means that the code is telling python to create an object with some initial parameters, but python does not recognize the 'step_max' field.

The culprit line in the code is

hessian = nd.Hessian(myFclass.error,step_max=1.0e-2,step_nom=my_step_nom)

You can see that is is trying to tell python to create a nd.Hessian object given three initial parameters: myFclass.error, step_max=1.0e-2, and step_nom=my_step_nom. The problem here is that the nd.Hessian initializer does not take parameters called step_max and step_nom.

So then, what does the nd.Hessian initializer take? nd.Hessian is the Hessian object from the numdifftools package, so I took a look at the source code. Sure enough, this is the source code for initializing a nd.Hessian object:

class Hessian(_Derivative):
    def __init__(self, f, step=None, method='central', full_output=False):

Take a look at the __init__. You can see that it takes f, step, method, and full_output. If it had taken in step_max and step_nom, those fields would have been included in the __init__.

One option is to try to use the np.Hessian object correctly and use the step parameter and figure out what step you want to use.

For example, if you replace the

hessian = nd.Hessian(myFclass.error,step_max=1.0e-2,step_nom=my_step_nom)

with

hessian = nd.Hessian(myFclass.error,step=1.0e-2)

You will be able to run the code. It might not give the same results as the paper though, you'll never really know what exact code they ran to get their results.

If you want to continue using this code and want to use the numdifftools package, I suggest taking a look at the source code that has nice explanations and comments and examples.

Comments