<commit_before># -*- coding: utf-8 -*-
"""
Created on Sat Aug 06 17:18:46 2016

@author: Pjer
"""


import numpy as np
import string
#from matplotlib import pyplot as plt

#from plugin import Plugin


class bio_simulation:
    def __init__(self,str_coefs,str_eqs,str_init):
        print('INFO: simulation: \n\t%r\n\t%r\n\t%r' % (str_coefs, str_eqs, str_init))
        self.coefs = str_coefs
        
        # parse equations to str arrays for further use
        self.eqs_str_arr= str.split(str_eqs,'\n')
        self.eqs_arr = self.eqs_str_arr[1:len(self.eqs_str_arr)-1] 
        self.init_str_arr= str.split(str_init,'\n')
        self.init_arr = self.init_str_arr[1:len(self.eqs_str_arr)-1]        
        
        # define defualt coef        
        start_t = 0
        end_t = 1
        step_l = 0.005
        
        # user input coef    
        exec(self.coefs)
        
        self.start_t = start_t
        self.end_t = end_t
        self.step_l = step_l
        #print self.eqs_arr
        
    
    def run_sim(self):
        self.t_range = np.linspace(self.start_t,self.end_t,((self.end_t-self.start_t)/self.step_l))
        # generate varable name : y[1] y[2]......        
        var_all = map(lambda x:"y"+str(x) , range(1,len(self.eqs_arr)+1))
        self.data_all  = np.zeros((len(self.eqs_arr),len(self.t_range)+1))        
        y = np.zeros(len(self.eqs_arr))
        
        index_init = 0
        for init_single_line in self.init_arr:
            parse_init = str.split(init_single_line,'=')
            self.data_all[index_init,0] = parse_init[1]
            y[index_init] = parse_init[1]
            index_init = index_init + 1
            
        index_simu = 0
        for t in self.t_range:            
            # get variabls ready
            index_var = 0
            for var_y in var_all:
                step_func = str.split(self.eqs_arr[index_var],'=')
                #print step_func
                self.data_all[index_var,index_simu+1] = eval(step_func[1]) * self.step_l + self.data_all[index_var,index_simu]
                index_var += 1
            
            y = self.data_all[:,index_simu+1]
            index_simu += 1
        return 0
        
    def parse_data(self):
        return self.data_all
    '''
    def plot_data(self):
        plt.plot(self.t_range,self.data_all[0,0:len(self.data_all[0,:])-1])
        plt.plot(self.t_range,self.data_all[1,0:len(self.data_all[0,:])-1])
        plt.show()
    '''


def main():
    str_coefs="start_t=0;end_t=200;step=0.005;"
    str_eqs ="""# equations (one equation one line)
    dy0dt = 0.01*y[0] +0.005*y[1]
    dy1dt = 0.05*y[1]*(1-y[1]/2)
    # end line """
    str_init = """# init values
    y0 = 0.2
    y1 = 0.1
    # end line"""
    sim = bio_simulation(str_coefs,str_eqs, str_init);
    sim.run_sim()
    print(repr(list(map(list, sim.data_all))))
if __name__=="plugins.simulation":
    main()
'''
class Simulation(Plugin):
    name = 'simulation'
    def process(self, request):
        str_coefs = "start_t=0;end_t=200;step=0.005;"
        str_eqs = request['eqs']
        str_init = request['init']
        str_coefs="start_t=0;end_t=200;step=0.005;"
        str_eqs ="""# equations (one equation one line)
        dy0dt = 0.01*y[0] +0.005*y[1]
        dy1dt = 0.05*y[1]*(1-y[1]/2)
        # end line """
        str_init = """# init values
        y0 = 0.2
        y1 = 0.1
        # end line"""
        sim = bio_simulation(str_coefs, str_eqs, str_init)
        sim.run_sim()
        print(repr(list(map(list, sim.data_all))))
        return dict(result=repr(list(map(list, sim.data_all))))

__plugin__ = Simulation()
'''
<commit_msg>fix the bug<commit_after># -*- coding: utf-8 -*-
"""
Created on Sat Aug 06 17:18:46 2016

@author: Pjer
"""


import numpy as np
import string
from matplotlib import pyplot as plt

#from plugin import Plugin


class bio_simulation:
    def __init__(self,str_eqs,str_init):
        # parse equations to str arrays for further use
        self.eqs_str_arr= str.split(str_eqs,'\n')
        self.eqs_arr = self.eqs_str_arr[1:len(self.eqs_str_arr)-1] 
        self.init_str_arr= str.split(str_init,'\n')
        self.init_arr = self.init_str_arr[1:len(self.eqs_str_arr)-1]        
        
        # define defualt coef        
        start_t = 0
        end_t = 200
        step_l = 0.005
        
        self.start_t = start_t
        self.end_t = end_t
        self.step_l = step_l
        #print self.eqs_arr
        
    
    def run_sim(self):
        self.t_range = np.linspace(self.start_t,self.end_t,((self.end_t-self.start_t)/self.step_l))
        # generate varable name : y[1] y[2]......        
        var_all = list(map(lambda x:"y"+str(x) , range(1,len(self.eqs_arr)+1)))
        self.data_all  = np.zeros((len(self.eqs_arr),len(self.t_range)+1))        
        y = np.zeros(len(self.eqs_arr))
        
        index_init = 0
        for init_single_line in self.init_arr:
            parse_init = str.split(init_single_line,'=')
            self.data_all[index_init,0] = parse_init[1]
            y[index_init] = parse_init[1]
            index_init = index_init + 1
            
        index_simu = 0
        for t in self.t_range:            
            # get variabls ready
            index_var = 0
            for var_y in var_all:
                step_func = str.split(self.eqs_arr[index_var],'=')
                #print step_func
                self.data_all[index_var,index_simu+1] = eval(step_func[1]) * self.step_l + self.data_all[index_var,index_simu]
                index_var += 1
            
            y = self.data_all[:,index_simu+1]
            index_simu += 1
        return 0
        
    def parse_data(self):
        return self.data_all

    def plot_data(self):
        plt.plot(self.t_range,self.data_all[0,0:len(self.data_all[0,:])-1])
        plt.plot(self.t_range,self.data_all[1,0:len(self.data_all[0,:])-1])
        plt.show()



def main():
    str_eqs ="""# equations (one equation one line)
    dy0dt = 0.01*y[0] +0.005*y[1]
    dy1dt = 0.05*y[1]*(1-y[1]/2)
    # end line """
    str_init = """# init values
    y0 = 0.2
    y1 = 0.1
    # end line"""
    sim = bio_simulation(str_eqs, str_init)
    sim.run_sim()
    print(repr(list(map(list, sim.data_all))))
    sim.plot_data()
if __name__ in ("plugins.simulation", '__main__'):
    main()
'''
class Simulation(Plugin):
    name = 'simulation'
    def process(self, request):
        str_eqs = request['eqs']
        str_init = request['init']
        str_eqs ="""# equations (one equation one line)
        dy0dt = 0.01*y[0] +0.005*y[1]
        dy1dt = 0.05*y[1]*(1-y[1]/2)
        # end line """
        str_init = """# init values
        y0 = 0.2
        y1 = 0.1
        # end line"""
        sim = bio_simulation(str_eqs, str_init)
        sim.run_sim()
        print(repr(list(map(list, sim.data_all))))
        return dict(result=repr(list(map(list, sim.data_all))))

__plugin__ = Simulation()
'''

{'fraction_non_alphanumeric': 0.13890481651376146, 'fraction_numerical': 0.03153669724770642, 'mean_word_length': 2.073568281938326, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3060702', 'n_tokens_mistral': 2727, 'n_tokens_neox': 2608, 'n_words': 451}