-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Conversation
def parseSym(model_dict): | ||
def parseSym(model_dict): |
There was a problem hiding this comment.
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:
- Replace identity comprehension with call to collection constructor [×2] (
identity-comprehension
)
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) |
There was a problem hiding this comment.
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:
- Use f-string instead of string concatenation [×4] (
use-fstring-for-concatenation
) - Use
with
when opening file to ensure closure [×2] (ensure-file-closed
) - Replace unused for index with underscore (
for-index-underscore
)
if checknegative: | ||
checknegative_c = "TRUE" | ||
else: | ||
checknegative_c = "FALSE" | ||
checknegative_c = "TRUE" if checknegative else "FALSE" |
There was a problem hiding this comment.
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:
- Replace if statement with if expression (
assign-if-exp
) - Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
) - Use
with
when opening file to ensure closure (ensure-file-closed
) - Replace assignment and augmented assignment with single assignment [×2] (
merge-assign-and-aug-assign
) - Replace assignment with augmented assignment [×7] (
aug-assign
) - Simplify logical expression using De Morgan identities (
de-morgan
) - Replace multiple comparisons of same variable with
in
operator (merge-comparisons
)
This removes the following comments ( why? ):
# write parameter definitions
# write header
|
||
# 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) |
There was a problem hiding this comment.
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:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
) - Use
with
when opening file to ensure closure (ensure-file-closed
) - Replace assignment and augmented assignment with single assignment [×2] (
merge-assign-and-aug-assign
) - Replace assignment with augmented assignment [×6] (
aug-assign
) - Simplify logical expression using De Morgan identities (
de-morgan
)
This removes the following comments ( why? ):
# write parameter definitions
# write header
rm_cmd = "rm -rf %s" % os.path.join('./bin/',bin) | ||
rm_cmd = f"rm -rf {os.path.join('./bin/', bin)}" |
There was a problem hiding this comment.
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:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
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)), | ||
} |
There was a problem hiding this comment.
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:
- Merge dictionary assignment with declaration [×5] (
merge-dict-assign
) - Replace if statement with if expression (
assign-if-exp
)
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') |
There was a problem hiding this comment.
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:
- Merge append into list declaration [×6] (
merge-list-append
)
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') |
There was a problem hiding this comment.
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:
- Merge append into list declaration [×3] (
merge-list-append
)
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') |
There was a problem hiding this comment.
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:
- Merge append into list declaration [×3] (
merge-list-append
)
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]}", | ||
] | ||
|
There was a problem hiding this comment.
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:
- Use f-string instead of string concatenation [×24] (
use-fstring-for-concatenation
)
This removes the following comments ( why? ):
# MAPKp
# MKKK
# MAPKpp
# MKKpp
# MKKKp
# MAPK
# MKK
# MKKp
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: