def generateODEScript(modelfile, mtype='ASM', solver='RK4', timestep=1, endtime=21600, lowerbound='0;0', upperbound='1e-3;1e-3', odefile='odescript.py'): '''! Function to generate Python ODE script from a given model specification file. Usage: python astools.py genODE --modelfile=models/asm/glycolysis.modelspec --mtype=ASM --solver=RK4 --timestep=1 --endtime=21600 --lowerbound=0;0 --upperbound=1e-3;1e-3 --odefile=glycolysis.py @param modelfile String: Name of model specification file in models folder. This assumes that the model file is not in models folder. @param mtype String: Type of model specification file. Allowable types are 'ASM' (AdvanceSyn Model Specification). Default = 'ASM'. @param solver String: Type of solver to use. Allowable types are 'Euler', 'Heun' (Runge-Kutta 2nd method or Trapezoidal), 'RK3' (third order Runge-Kutta), 'RK4' (fourth order Runge-Kutta), 'RK38' (fourth order Runge-Kutta method, 3/8 rule), 'CK4' (fourth order Cash-Karp), 'CK5' (fifth order Cash-Karp), 'RKF4' (fourth order Runge-Kutta-Fehlberg), 'RKF5' (fifth order Runge-Kutta-Fehlberg), 'DP4' (fourth order Dormand-Prince), and 'DP5' (fifth order Dormand-Prince). Default = 'RK4'. @param timestep Float: Time step interval for simulation. Default = 1.0. @param endtime Float: Time to end simulation - the simulation will run from 0 to end time. Default = 21600. @param lowerbound String: Define lower boundary of objects. For example, "1;2" means that when the value of the object hits 1, it will be bounced back to 2. Default = 0;0; that is, when the value of the object goes to negative, it will be bounced back to zero. @param upperbound String: Define upper boundary of objects. For example, "10;9" means that when the value of the object hits 1, it will be pushed down to 9. Default = 1e-3;1e-3; that is, when the value of the object above 1e-3, it will be pushed back to 1e-3. @param odefile String: Python ODE script file to write out. This file will be written into odescript folder. Default = odescript.py. @return: A list containing the Python ODE script (one element = one line). ''' modelfile = os.path.abspath(modelfile) (spec, modelobj) = modelReader(modelfile, mtype, 'extended') datalist = ASModeller.generate_ODE(spec, modelobj, solver, timestep, endtime, lowerbound, upperbound) filepath = fileWriter(datalist, 'odescript', odefile) return datalist
def generateODEScript(modelfile, mtype='ASM', solver='RK4', timestep=1, endtime=21600, odefile='odescript.py'): '''! Function to generate Python ODE script from a given model specification file. Usage: python astools.py genODE --modelfile=asm/glycolysis.modelspec --mtype=ASM --solver=RK4 --timestep=1 --endtime=21600 --odefile=glycolysis.py @param modelfile String: Name of model specification file in models folder. This assumes that the model file is in models folder. @param mtype String: Type of model specification file. Allowable types are 'ASM' (AdvanceSyn Model Specification). Default = 'ASM'. @param solver String: Type of solver to use. Allowable types are 'Euler', 'Heun' (Runge-Kutta 2nd method or Trapezoidal), 'RK3' (third order Runge-Kutta), 'RK4' (fourth order Runge-Kutta), 'RK38' (fourth order Runge-Kutta method, 3/8 rule), 'CK4' (fourth order Cash-Karp), 'CK5' (fifth order Cash-Karp), 'RKF4' (fourth order Runge-Kutta-Fehlberg), 'RKF5' (fifth order Runge-Kutta-Fehlberg), 'DP4' (fourth order Dormand-Prince), and 'DP5' (fifth order Dormand-Prince). Default = 'RK4'. @param timestep Float: Time step interval for simulation. Default = 1.0. @param endtime Float: Time to end simulation - the simulation will run from 0 to end time. Default = 21600. @param odefile String: Python ODE script file to write out. This file will be written into odescript folder. Default = odescript.py. @return: A list containing the Python ODE script (one element = one line). ''' modelfile = 'models\\' + modelfile (spec, modelobj) = modelReader(modelfile, mtype, 'extended') datalist = ASModeller.generate_ODE(spec, modelobj, solver, timestep, endtime) filepath = fileWriter(datalist, 'odescript', odefile) return datalist
def localSensitivity(modelfile, multiple=100, prefix='', mtype='ASM', solver='RK4', timestep=1, endtime=21600, cleanup=True, outfmt='reduced', sampling=100, resultfile='sensitivity_analysis.csv'): '''! Function to perform local sensitivity analysis using OFAT/OAT (one factor at a time) method where the last data time (end time) simulation results are recorded into results file. Usage: python astools.py LSA --modelfile=models/asm/glycolysis.modelspec --prefix=sen01 --mtype=ASM --multiple=100 --solver=RK4 --timestep=1 --endtime=21600 --cleanup=True --outfmt=reduced --resultfile=sensitivity_analysis.csv @param modelfile String: Name of model specification file in models folder. This assumes that the model file is not in models folder. @param multiple Integer: Multiples to change each variable value. Default = 100 (which will multiple the original parameter value by 100). @param prefix String: A prefixing string for the set of new model specification for identification purposes. Default = ''. @param mtype String: Type of model specification file. Allowable types are 'ASM' (AdvanceSyn Model Specification). Default = 'ASM'. @param solver String: Type of solver to use. Allowable types are 'Euler', 'Heun' (Runge-Kutta 2nd method or Trapezoidal), 'RK3' (third order Runge-Kutta), 'RK4' (fourth order Runge-Kutta), 'RK38' (fourth order Runge-Kutta method, 3/8 rule), 'CK4' (fourth order Cash-Karp), 'CK5' (fifth order Cash-Karp), 'RKF4' (fourth order Runge-Kutta-Fehlberg), 'RKF5' (fifth order Runge-Kutta-Fehlberg), 'DP4' (fourth order Dormand-Prince), and 'DP5' (fifth order Dormand-Prince). Default = 'RK4'. @param timestep Float: Time step interval for simulation. Default = 1.0. @param endtime Float: Time to end simulation - the simulation will run from 0 to end time. Default = 21600. @param cleanup String: Flag to determine whether to remove all generated temporary models and ODE code files. Default = True. @param outfmt String: Output format. Allowable types are 'reduced' (only the final result will be saved into resultfile) and 'full' (all data, depending on sampling, will be saved into resultfile). @param sampling Integer: Sampling frequency. If 100, means only every 100th simulation result will be written out. The first (start) and last (end) result will always be written out. Default = 100. @param resultfile String: Relative or absolute file path to write out sensitivity results. Default = 'sensitivity_analysis.csv' ''' MSF = sensitivityGenerator(modelfile, multiple, prefix, mtype) outfmt = str(outfmt).lower() sampling = int(sampling) resultfile = os.path.abspath(resultfile) resultfile = open(resultfile, 'w') header = False msf_count = len(MSF) model_count = 0 for param in MSF: model_count = model_count + 1 # Generate ODE codes from model (spec, modelobj) = modelReader(MSF[param]['ASM'], mtype, 'extended') print('Processing model %s of %s: %s' % \ (model_count, msf_count, MSF[param]['ASM'])) ODECode = ASModeller.generate_ODE(spec, modelobj, solver, timestep, endtime) odefile = re.sub('\.', '_', param) filepath = fileWriter(ODECode, 'models/temp', odefile + '.py') MSF[param]['ODE'] = filepath # Simulate ODE codes m = importlib.import_module('models.temp.'+ odefile) if header == False: labels = ['Parameter', 'Change'] + m.labels resultfile.write(','.join(labels) + '\n') header = True simData = [] data_row_count = 0 for data in m.model: if outfmt == "reduced": simData = [str(x) for x in data] elif outfmt == "full": if (data_row_count % sampling) == 0: simData.append([str(x) for x in data]) data_row_count = data_row_count + 1 MSF[param]['Data'] = simData # Write out sensitivity results to resultfile if outfmt == "reduced": data = [param, MSF[param]['Change']] + MSF[param]['Data'] data = [str(x) for x in data] resultfile.write(','.join(data) + '\n') elif outfmt == "full": for datarow in MSF[param]['Data']: data = [param, MSF[param]['Change']] + datarow data = [str(x) for x in data] resultfile.write(','.join(data) + '\n') resultfile.close() if str(cleanup).upper() == 'TRUE': for param in MSF: ASMfile = MSF[param]['ASM'] ODEfile = MSF[param]['ODE'] os.remove(ASMfile) os.remove(ODEfile)
def localSensitivity(modelfile, multiple=100, prefix='', mtype='ASM', solver='RK4', timestep=1, endtime=21600, cleanup=True, resultfile='sensitivity_analysis.csv'): '''! Function to perform local sensitivity analysis using OFAT/OAT (one factor at a time) method where the last data time (end time) simulation results are recorded into results file. Usage: python astools.py LSA --modelfile=asm/glycolysis.modelspec --prefix=sen01 --mtype=ASM --multiple=100 --solver=RK4 --timestep=1 --endtime=21600 --cleanup=True --resultfile=sensitivity_analysis.csv @param modelfile String: Name of model specification file in models folder. @param multiple Integer: Multiples to change each variable value. Default = 100 (which will multiple the original parameter value by 100). @param prefix String: A prefixing string for the set of new model specification for identification purposes. Default = ''. @param mtype String: Type of model specification file. Allowable types are 'ASM' (AdvanceSyn Model Specification). Default = 'ASM'. @param solver String: Type of solver to use. Allowable types are 'Euler', 'Heun' (Runge-Kutta 2nd method or Trapezoidal), 'RK3' (third order Runge-Kutta), 'RK4' (fourth order Runge-Kutta), 'RK38' (fourth order Runge-Kutta method, 3/8 rule), 'CK4' (fourth order Cash-Karp), 'CK5' (fifth order Cash-Karp), 'RKF4' (fourth order Runge-Kutta-Fehlberg), 'RKF5' (fifth order Runge-Kutta-Fehlberg), 'DP4' (fourth order Dormand-Prince), and 'DP5' (fifth order Dormand-Prince). Default = 'RK4'. @param timestep Float: Time step interval for simulation. Default = 1.0. @param endtime Float: Time to end simulation - the simulation will run from 0 to end time. Default = 21600. @param cleanup String: Flag to determine whether to remove all generated temporary models and ODE code files. Default = True. @param resultfile String: Relative or absolute file path to write out sensitivity results. Default = 'sensitivity_analysis.csv' ''' MSF = sensitivityGenerator(modelfile, multiple, prefix, mtype) for param in MSF: # Generate ODE codes from model (spec, modelobj) = modelReader(MSF[param]['ASM'], mtype, 'extended') print('Processing model: %s' % MSF[param]['ASM']) ODECode = ASModeller.generate_ODE(spec, modelobj, solver, timestep, endtime) odefile = re.sub('\.', '_', param) filepath = fileWriter(ODECode, 'models/temp', odefile + '.py') MSF[param]['ODE'] = filepath # Simulate ODE codes m = importlib.import_module('models.temp.' + odefile) labels = [param, 'Change'] + m.labels for data in m.model: simData = [str(x) for x in data] MSF[param]['Data'] = simData resultfile = os.path.abspath(resultfile) resultfile = open(resultfile, 'w') resultfile.write(','.join(labels) + '\n') for param in MSF: data = [param, MSF[param]['Change']] + MSF[param]['Data'] data = [str(x) for x in data] resultfile.write(','.join(data) + '\n') resultfile.close() if str(cleanup).upper() == 'TRUE': for param in MSF: ASMfile = MSF[param]['ASM'] ODEfile = MSF[param]['ODE'] os.remove(ASMfile) os.remove(ODEfile)