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

Sourcery Starbot ⭐ refactored leshaker/python_integration #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SourceryAI
Copy link

Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨

Here's your pull request refactoring your most popular Python repo.

If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.

Review changes via command line

To manually merge these changes, make sure you're on the master branch, then run:

git fetch https://github.com/sourcery-ai-bot/python_integration master
git merge --ff-only FETCH_HEAD
git reset HEAD^

def parseSym(model_dict):
def parseSym(model_dict):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parseSym refactored with the following changes:

Comment on lines -108 to +124
fname_def = model_name+'_define.c'
fid = open('./includes/'+fname_def, 'w')
define_str = "#define NPARS %d\t\t/* number of parameters */\n" % (len(ps))
define_str = define_str+"#define NEQ %d\t\t/* number of equations */\n" % len(fs)
fid.write(define_str)
fid.close()

fname_init = model_name+'_initialize.c'
fid = open('./includes/'+fname_init, 'w')
if (type(atol)!=list):
atol = [atol for x in range(len(fs))]

init_str = "hmin = RCONST(%e);\t\t/* minimal stepsize */\n" % hmin
init_str = init_str + "hmax = RCONST(%e);\t\t/* maximal stepsize */\n" % hmax
init_str = init_str + "mxsteps = RCONST(%e);\t\t/* maximal number of steps */\n" % mxsteps
init_str = init_str + "reltol = RCONST(%e);\t\t/* scalar relative tolerance */\n" % rtol
for i in range(len(fs)):
init_str = init_str + "Ith(abstol,%d) = RCONST(%e);\t\t/* vector absolute tolerance components */\n" % (i+1, atol[i])
fid.write(init_str)
fid.close()

fname_def = f'{model_name}_define.c'
with open(f'./includes/{fname_def}', 'w') as fid:
define_str = "#define NPARS %d\t\t/* number of parameters */\n" % (len(ps))
define_str = define_str+"#define NEQ %d\t\t/* number of equations */\n" % len(fs)
fid.write(define_str)
fname_init = f'{model_name}_initialize.c'
with open(f'./includes/{fname_init}', 'w') as fid:
if (type(atol)!=list):
atol = [atol for _ in range(len(fs))]

init_str = "hmin = RCONST(%e);\t\t/* minimal stepsize */\n" % hmin
init_str = init_str + "hmax = RCONST(%e);\t\t/* maximal stepsize */\n" % hmax
init_str = init_str + "mxsteps = RCONST(%e);\t\t/* maximal number of steps */\n" % mxsteps
init_str = init_str + "reltol = RCONST(%e);\t\t/* scalar relative tolerance */\n" % rtol
for i in range(len(fs)):
init_str = init_str + "Ith(abstol,%d) = RCONST(%e);\t\t/* vector absolute tolerance components */\n" % (i+1, atol[i])
fid.write(init_str)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function writeInitSundials refactored with the following changes:

Comment on lines -137 to +133
if checknegative:
checknegative_c = "TRUE"
else:
checknegative_c = "FALSE"
checknegative_c = "TRUE" if checknegative else "FALSE"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function writeOdeSundials refactored with the following changes:

This removes the following comments ( why? ):

# write parameter definitions
# write header

Comment on lines -238 to +289

# write parameter definitions
par_def = "\t/* Extract needed constants from data */\n"
par_def = par_def+"\tdata = (UserData) user_data;\n"
par_def = par_def+"\tdouble p[NPARS];\n"
par_def = par_def+"\tfor (i=0; i<NPARS; i++) p[i] = data->p[i];\n\n"
fid.write(par_def)

# write parameter definitions
for i, p in enumerate(ps):
tmp_str = '\trealtype %s = p[%d];\n' % (p, i)
fid.write(tmp_str)
fid.write('\n')

#write variables definitions
for i, x in enumerate(xs):
tmp_str = '\trealtype %s = Ith(x,%d);\n' % (x, i+1)
fid.write(tmp_str)
fid.write('\n')
fname_jac = f'{model_name}_ode_jac.c'
with open(f'./includes/{fname_jac}', 'w') as fid:
header = (
"static int Jac(long int N, realtype t, N_Vector x, N_Vector fx, DlsMat J, void *user_data,\nN_Vector tmp1, N_Vector tmp2, N_Vector tmp3)\n"
+ "{\n\n"
)

header += "\tint i, j;\n"
header += "\tUserData data;\n"
fid.write(header)

par_def = (
"\t/* Extract needed constants from data */\n"
+ "\tdata = (UserData) user_data;\n"
)

par_def += "\tdouble p[NPARS];\n"
par_def += "\tfor (i=0; i<NPARS; i++) p[i] = data->p[i];\n\n"
fid.write(par_def)

# write parameter definitions
for i, p in enumerate(ps):
tmp_str = '\trealtype %s = p[%d];\n' % (p, i)
fid.write(tmp_str)
fid.write('\n')

#write alg eqs
for alg in alg_dict:
tmp_str = '\t%s = %s;\n' % (alg,alg_dict[alg])
fid.write(tmp_str)
fid.write('\n')

# write jacobian
for i in range(dfdx.shape[0]):
for j in range(dfdx.shape[1]):
if not dfdx[i,j]==0:
# convert formula to c-style
dfdx_c = convToCstr([dfdx[i,j]])
tmp_str = '\tIJth(J,%d,%d) = %s;\n' % (i+1,j+1, dfdx_c[0])
fid.write(tmp_str)
fid.write('\n')
#write variables definitions
for i, x in enumerate(xs):
tmp_str = '\trealtype %s = Ith(x,%d);\n' % (x, i+1)
fid.write(tmp_str)
fid.write('\n')

# write variables definitions
for i, x in enumerate(xs):
if x in xs_alg:
tmp_str = '\tIth(x,%d) = %s;\n' % (i+1, x)
#write alg eqs
for alg in alg_dict:
tmp_str = '\t%s = %s;\n' % (alg,alg_dict[alg])
fid.write(tmp_str)
fid.write('\n')

# write footer
footer = '\treturn(0);\n}\n'
fid.write(footer)
fid.close()
# write jacobian
for i in range(dfdx.shape[0]):
for j in range(dfdx.shape[1]):
if dfdx[i, j] != 0:
# convert formula to c-style
dfdx_c = convToCstr([dfdx[i,j]])
tmp_str = '\tIJth(J,%d,%d) = %s;\n' % (i+1,j+1, dfdx_c[0])
fid.write(tmp_str)
fid.write('\n')

# write variables definitions
for i, x in enumerate(xs):
if x in xs_alg:
tmp_str = '\tIth(x,%d) = %s;\n' % (i+1, x)
fid.write(tmp_str)
fid.write('\n')

# write footer
footer = '\treturn(0);\n}\n'
fid.write(footer)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function writeJacSundials refactored with the following changes:

This removes the following comments ( why? ):

# write parameter definitions
# write header

Comment on lines -455 to +450
rm_cmd = "rm -rf %s" % os.path.join('./bin/',bin)
rm_cmd = f"rm -rf {os.path.join('./bin/', bin)}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function writeModelFiles refactored with the following changes:

Comment on lines -455 to +460
model_dict = {}
model_dict['name'] = model_name
model_dict['odes'] = dict(zip(x_names,dxdt))
model_dict['vars'] = x_names
model_dict['pars'] = p_names

if x0 == []:
model_dict['initvars'] = dict(zip(x_names, 0.1*np.ones([len(x_names)])))
else:
model_dict['initvars'] = dict(zip(x_names, x0))
model_dict = {
'name': model_name,
'odes': dict(zip(x_names, dxdt)),
'vars': x_names,
'pars': p_names,
'initvars': dict(zip(x_names, 0.1 * np.ones([len(x_names)])))
if x0 == []
else dict(zip(x_names, x0)),
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function convToDict refactored with the following changes:

Comment on lines -18 to -25
dxdt = []
dxdt = [
'- kon * Epo * EpoR + koff * EpoEpoR + kex * EpoEpoRi',
'- kon * Epo * EpoR + koff * EpoEpoR + kt * EpoR0 - kt * EpoR + kex * EpoEpoRi',
'kon * Epo * EpoR - koff * EpoEpoR - ke * EpoEpoR',
'ke * EpoEpoR - kex * EpoEpoRi - kdi * EpoEpoRi - kde * EpoEpoRi',
'kdi * EpoEpoRi',
'kde * EpoEpoRi',
]

dxdt.append('- kon * Epo * EpoR + koff * EpoEpoR + kex * EpoEpoRi')
dxdt.append('- kon * Epo * EpoR + koff * EpoEpoR + kt * EpoR0 - kt * EpoR + kex * EpoEpoRi')
dxdt.append('kon * Epo * EpoR - koff * EpoEpoR - ke * EpoEpoR')
dxdt.append('ke * EpoEpoR - kex * EpoEpoRi - kdi * EpoEpoRi - kde * EpoEpoRi')
dxdt.append('kdi * EpoEpoRi')
dxdt.append('kde * EpoEpoRi')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function EpoEpoR refactored with the following changes:

Comment on lines -41 to -45
dxdt = []
dxdt = [
'- k1 * A + k2 * B - k3 * A * B + k4 * C',
'+ k1 * A - k2 * B - k3 * A * B + k4 * C',
'+ k3 * A * B - k4 * C',
]

dxdt.append('- k1 * A + k2 * B - k3 * A * B + k4 * C')
dxdt.append('+ k1 * A - k2 * B - k3 * A * B + k4 * C')
dxdt.append('+ k3 * A * B - k4 * C')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ToyModel refactored with the following changes:

Comment on lines -61 to -65
dxdt = []
dxdt = [
'- k1 * log ( A ) + k2 * B ** k3 * log ( A * B ) + C**k4',
'+ k1 * A - k2 * B - k3 * A * B + k4 * C',
'+ k3 * A * B - k4 * C',
]

dxdt.append('- k1 * log ( A ) + k2 * B ** k3 * log ( A * B ) + C**k4')
dxdt.append('+ k1 * A - k2 * B - k3 * A * B + k4 * C')
dxdt.append('+ k3 * A * B - k4 * C')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ToyModel2 refactored with the following changes:

Comment on lines -97 to +110
dxdt = [v[2]+"-"+v[1], # MKKK
v[1]+"-"+v[2], # MKKKp
v[6]+"-"+v[3], # MKK
v[3]+"+"+v[5]+"-"+v[4]+"-"+v[6], # MKKp
v[4]+"-"+v[5], # MKKpp
v[10]+"-"+v[7], # MAPK
v[7]+"+"+v[9]+"-"+v[8]+"-"+v[10], # MAPKp
v[8]+"-"+v[9]] # MAPKpp
dxdt = [
f"{v[2]}-{v[1]}",
f"{v[1]}-{v[2]}",
f"{v[6]}-{v[3]}",
f"{v[3]}+{v[5]}-{v[4]}-{v[6]}",
f"{v[4]}-{v[5]}",
f"{v[10]}-{v[7]}",
f"{v[7]}+{v[9]}-{v[8]}-{v[10]}",
f"{v[8]}-{v[9]}",
]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MAPK refactored with the following changes:

This removes the following comments ( why? ):

# MAPKp
# MKKK
# MAPKpp
# MKKpp
# MKKKp
# MAPK
# MKK
# MKKp

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

Successfully merging this pull request may close these issues.

1 participant