def run_simulation(sample, sampleId, K, f):

    import modeltools
    sim_time = 1e9
    mp = modeltools.ModelProcessor()
    # pdb.set_trace()
    res = mp.simulate(sample.S0, sim_time, sample.F, K)

    f.write("CONC_{}={}\n\n".format(sampleId, res.finalState.tolist()))
    if not res.success:
        print bcolors.FAIL\
         + "Simulation failed with flag {} for sample {}"\
         .format(res.exitcode, sampleId)\
         + bcolors.ENDC
        return []
    else:
        return res.finalState

    return
def run_simulation_test(model, sim_time_vector):

    from model import PybiosEqs
    import modeltools

    process_times = Odict()
    real_times = Odict()
    try:
        if (sys.argv[1] == "True" or sys.argv[1] == "true"):
            print "Running simulations with steady state as a start"
            flag_next = False
        else:
            print "Running normal simulations"
            flag_next = True
    except IndexError:
        print "Running normal simulations"
        flag_next = True

    flag_first = True
    for sim_time in sim_time_vector:
        try:
            model_raw = PybiosEqs(0.000000,
                                  sim_time, [model],
                                  modParPath=[model],
                                  filePrefix=model)
        except TypeError:  # for older models
            model_raw = PybiosEqs(0.000000, sim_time, model)

        mp = modeltools.ModelProcessor()
        S = model_raw.S
        F = model_raw.F
        K = model_raw.K

        step = sim_time / 100
        cctime = np.arange(0, float(sim_time) + 1, step, dtype=np.float)
        if flag_first:
            start_time = time.time()
            start_ptime = time.clock()
            not_in_steady_state = False
            try:
                profiler_start("simulate_normal_" +
                               model[model[:model.rfind("/")].rfind("/") +
                                     1:-1] + ".log")
                res_ccompile = mp.simulate_time_course(S, cctime, F, K)
                S_time = res_ccompile.timeCourse[-1]
                profiler_stop()
            except MemoryError:
                print "Memory Error"

            process_times[sim_time] = time.clock() - start_ptime
            real_times[sim_time] = time.time() - start_time
            print "T({}) Process time normal: {}".format(
                sim_time, process_times[sim_time])
            print "T({}) Real time normal: {}".format(sim_time,
                                                      real_times[sim_time])

            flag_first = flag_next  # change this if running with or without steady state (true normal, false ss)
            S_time = res_ccompile.timeCourse[-1]
            not_in_steady_state = check_steady_state(res_ccompile, cctime,
                                                     not_in_steady_state)
            if not_in_steady_state:
                print "SPECIES NOT IN STEADY STATE"
        else:
            start_ptime = time.clock()
            start_time = time.time()
            try:
                profiler_start("simulate_short_" +
                               model[model[:model.rfind("/")].rfind("/") +
                                     1:-1] + ".log")
                res_ccompile = mp.simulate_time_course(S_time, cctime, F, K)
                profiler_stop()
            except MemoryError:
                print "Memory Error"
            process_times[sim_time] = time.clock() - start_ptime
            real_times[sim_time] = time.time() - start_time
            print "T({}) Process time short: {}".format(
                sim_time, process_times[sim_time])
            print "T({}) Real time short: {}".format(sim_time,
                                                     real_times[sim_time])

            S_time = res_ccompile.timeCourse[-1]

            not_in_steady_state = check_steady_state(res_ccompile, cctime,
                                                     not_in_steady_state)
            if not_in_steady_state:
                print "SPECIES NOT IN STEADY STATE RUNNING AGAIN WITH NORMAL SIMULATIONS"
                res_ccompile = mp.simulate_time_course(S, cctime, F, K)

    return res_ccompile, process_times, real_times, len(S)
Exemple #3
0
def main():

    # Take the config file from command line
    args = parser.parse_args()

    model_path = CONFIG.get('PYBIOS_MODEL', section='DATA_INPUT')
    if model_path[-1] != "/":
        model_path += "/"
    ids_file = model_path + "identifiers.py"
    model_id = model_path[model_path[:-1].rindex("/") + 1:-1]
    # Get options
    cost_func = CONFIG.get('CONTROL_TREATMENT_FILE', section='DATA_INPUT')
    exp_table = CONFIG.get('EXPERIMENT_TABLE', section='DATA_INPUT')
    c_model = CONFIG.get('CCOMPILED_MODEL', section='DATA_INPUT')

    # import c-compiled model
    sys.path.append(c_model)
    import modeltools

    mp = modeltools.ModelProcessor(maxStepNum=5000)
    # prepare time and range for simulation
    sim_time = 1e9
    step = sim_time / 100
    cctime = numpy.arange(0, float(sim_time) + 1, step, dtype=numpy.float)

    # initialize the model and random inital parameter vector
    model = PyBiosModel(model_id, ids_file, exp_table, cost_func)
    bounds = (-1, 1)
    varKParVect =  numpy.power(10, bounds[0] + \
            numpy.random.rand(len(model.variable_indexK_arr) + \
                model.dimKCD)*(bounds[1] - bounds[0]) )
    # varKParVect = bounds[0] + \
    # numpy.random.rand(len(model.variable_indexK_arr)+ \
    # model.dimKCD)*(bounds[1] - bounds[0])
    process_times = Odict()
    real_times = Odict()

    fixed_Ki = numpy.setdiff1d(xrange(model.dimK), model.variable_indexK_arr)
    K = numpy.zeros(model.dimK)
    if len(model.variable_indexK_arr) > 0:
        K[model.variable_indexK_arr] = varKParVect
    # sample_states = {}
    # import pdb; pdb.set_trace()
    cost_func_sample_ids = set(itertools.chain(*model.conditionDict.keys()))

    # for each sample in the cost function run formward simulation
    # print the run time
    for sampleId in cost_func_sample_ids:
        sample = model.samples[sampleId]
        if len(fixed_Ki) > 0:
            K[fixed_Ki] = sample.K[fixed_Ki]
            start_time = time.time()
            start_ptime = time.clock()
            res = mp.simulate(sample.S0, sim_time, sample.F, K)
            if res.success:
                process_times[sampleId] = time.clock() - start_ptime
                # real_times[sampleId] = time.time() - start_time
                print "({}) Process time normal: \t\t\t {}".\
                    format(sampleId, process_times[sampleId])
                # print "({}) Real time normal: {}".format(sampleId, real_times[sampleId])
            else:
                raise RuntimeError("Simulation time course failed with flag %s for sample %s" \
                         %( res.exitcode, sampleId))

    # import pdb; pdb.set_trace()
    print sum(process_times.values())
    # pprint.pprint(process_times)

    process_times = Odict()
    ss_dict = Odict()
    cost_func_sample_ids_sorted = sorted(
        list(set(itertools.chain(*model.conditionDict.keys()))))
    # import pdb; pdb.set_trace()
    # run the same simulations now with using the previous steady state as initial
    for sampleId in cost_func_sample_ids_sorted:
        sample = model.samples[sampleId]
        if len(fixed_Ki) > 0:
            K[fixed_Ki] = sample.K[fixed_Ki]
            # import pdb; pdb.set_trace()
            if any(string in sampleId for string in ss_dict.keys()):
                S0 = [ss_dict[i] for i in ss_dict.keys() \
                             if sampleId.startswith(i)][0]
            else:
                S0 = sample.S0
            start_time = time.time()
            start_ptime = time.clock()
            res = mp.simulate(S0, sim_time, sample.F, K)
            # res = mp.simulate_time_course(sample.S0, cctime, sample.F, K)
            if res.success:
                process_times[sampleId] = time.clock() - start_ptime
                # real_times[sampleId] = time.time() - start_time
                print "({}) Process time ss: \t\t\t {}".\
                        format(sampleId, process_times[sampleId])
                # print "({}) Real time normal: {}".format(sampleId, real_times[sampleId])
                if "CONTROL" in sampleId or not "_" in sampleId:
                    ss_dict[sampleId] = res.finalState
                    # ss_dict[sampleId] = res.timeCourse[-1]
            else:
                raise RuntimeError("Simulation time course failed with flag %s for sample %s" \
                         %( res.exitcode, sampleId))

    # print process_times
    print sum(process_times.values())
Exemple #4
0
def test_param_sensitivity(model_path, numb_vect, opti_flag, perc):

	model_input = model_path+"inputs/"
	model_output = model_path+"outputs/"
	sim_vect_file = model_output+"simulated_vectors_file.txt"
	sys.path.append(model_input)

	import modeltools 
	mp = modeltools.ModelProcessor(maxStepNum=100000)
	total_results = Odict()
	
	_, _, ids_vector, opti_vector, _ = parse_simulation_file(sim_vect_file)
	n_param = len([i for i in ids_vector if "{" in i])
	n_kCD = len([i for i in ids_vector if i.startswith("k")])
	
	if opti_flag:
		n_repeats = 1 
		i_size = len(opti_vector[-1])
		isrand =  'opti_'
	else:
		# n_param = 4072
		# n_kCD = 16
		i_size = n_param + n_kCD
		n_repeats = numb_vect
		isrand = 'rand_'
	tmp_opti = {}
	# pdb.set_trace()	
	for r in xrange(0, n_repeats):
		# tmp_progress_trace_rand0.txt
		org_rand_sim_file = tmp_progress_trace_dir + "tmp_progress_trace_" + isrand + str(r) +".txt"
		tmp_opti[r] = []
		if opti_flag:
			for i in range(i_size):
			 	tmp_opti_vect = list(opti_vector[-1]) # clean way to copy a list
			 	tmp_opti_vect[i] = tmp_opti_vect[i] + tmp_opti_vect[i]*perc/100.0 
			 	# pdb.set_trace()
				tmp_opti[r].append(tmp_opti_vect)
		else:
			# use the next 2 lines to generate new random vector
			# tmp_opti_vect = np.concatenate((np.random.rand(n_kCD), np.random.uniform(-1,1,n_param))) 
			# tmp_opti[r].append(tmp_opti_vect)
			# use the next 2 lines to use existing random vector
			_, _, _, tmp_opti_vect, _  = parse_simulation_file(org_rand_sim_file)
			tmp_opti[r].append(tmp_opti_vect[0])
			# pdb.set_trace()
			for i in xrange(i_size):
				# pdb.set_trace()
				tmp_mod = list(tmp_opti[r][0]) 
				tmp_mod[i] = tmp_mod[i] + tmp_mod[i]*perc/100.0 
				tmp_opti[r].append(tmp_mod)
			# pdb.set_trace()	
	# pdb.set_trace()	
	for k, vect in tmp_opti.iteritems():
		sufx = '_rand_'+str(k) if not opti_flag else '_opti_' + str(k)
		if perc>0:
			direct = 'incr_'
		else:
			direct = 'decr_'
		tmp_opti_dir = tmp_progress_trace_dir + 'Model_Mai2017_sim_' + str(abs(perc)) + direct + isrand + str(k)+'_best3/'
		tmp_prog_file = tmp_progress_trace_file(tmp_progress_trace_dir, 
												ids_vector, 
												vect,
												sufx+direct)
		# set the random progres trace file in the config file
		config_file = modify_config_file(config_path, 
										["VectorsFile","JobId"],
										[tmp_prog_file, tmp_opti_dir])
		# pdb.set_trace()
		simtools_exe = simtools_dir + "simcontrol.sh"
		subprocess.call([simtools_exe, 'startjob', config_file])
		# set the config file back to "VectorsFile"

		# print "Finished simulation at {}".\
		# format(datetime.datetime.strftime(datetime.datetime.now(), "%H:%M %d-%m-%Y"))
		# pdb.set_trace()
		res_dir, res_file = get_config_data(config_file)


		_, _, ids_vector2, _, fitness2 = parse_simulation_file(res_file)

		if not opti_flag:
			ids_vector2 = ['orig_fitness'] + ids_vector2

		change = [el/fitness2[0] for el in fitness2]

		res = pd.DataFrame(data={'IDS': ids_vector2, 'FITNESS': fitness2, 'CHANGE': change})
		res.sort_values(by=['CHANGE'], inplace=True)
		res.to_csv(res_dir+'results_'+str(abs(perc)) + direct + isrand + str(k), sep='\t', index=False)

		config_file = modify_config_file(config_path, 
										[tmp_prog_file, tmp_opti_dir], 
										["VectorsFile","JobId"])	
		shutil.move(tmp_prog_file, res_dir)				
	return 
# size_S = 1228
# size_F = 168
# size_K = 4686

size_S = 9217
size_F = 1235
size_K = 29797

sys.path.append(ccompiled)
sys.path.append(sap)

# import sap and cython libs
import modeltools
import model_solver

mp = modeltools.ModelProcessor(abstol=1.0E-9, reltol=1.0E-5, maxStepNum=5000)
# bdfMaxOrder=5, stabLimDet=1):
sp = model_solver.SolverWrapper()
# 'initial_step_size' : 1.0E-4,
# 'abs_tolerance'     : 1.0E-9,
# 'rel_tolerance'     : 1.0E-5,
# 'min_step_size'     : 1E-8,
# 'max_step_size'     : 1E8, !!!!!
# self.solver = SapOdeSolver.Solver(model_name=self.model_name)
sp.set_ode_parameters(1.0E-4, 1.0E-9, 1.49e-8, 1E-8, 10000)
# set_ode_parameters(h, abs_tol, rel_tol, min_step_size, max_step_size):
# h initial step size

# set model parameters
if rand:
    S = np.random.rand(size_S)