<commit_before>import numpy as np
import sys
import warnings
import emcee
import matplotlib.pyplot as plt
from bioscrape.types import Model, read_model_from_sbml
from bioscrape.simulator import ModelCSimInterface, DeterministicSimulator, SSASimulator

def import_timeseries(filename, time_column, value_column, properties = {}, plot_show = False):
    '''
    filename : csv file with columns for data values 
    (The column numbers start at 1)
    time_column : the column number in the file that has all the time series indexes that you want to import
    value_column : the column number in the file that has all the corresponding values that you want to import 
    properties : Optional dictionary to specify other properties that the imported data must satisfy. For example, 
    properties = {3 : 'abc'}, would only impor those rows that have column 3 value equal to 'abc'
    '''
    try:
        import csv
        from operator import itemgetter
        from itertools import groupby
        import math
    except:
        print('Packages not found. Make sure csv, operator, itertool, and math are installed.')

    with open(filename) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        data_dict = {}
        for row in csv_reader:
            if row and row[time_column - 1] and row[value_column - 1]:
                if properties:
                    for col, value in properties.items():
                        if row[col - 1] == str(value):
                            data_dict[float(row[time_column - 1])] = float(row[value_column - 1])
                        else:
                            break 
                else:
                    cell_t = row[time_column - 1]
                    cell_v = row[value_column - 1]
                    temp_str_t = cell_t.replace('.','',1).replace('e','',1).replace('-','',1)
                    temp_str_v = cell_v.replace('.','',1).replace('e','',1).replace('-','',1)
                    if temp_str_t.isdigit() and temp_str_v.isdigit():
                        data_dict[float(cell_t)] = float(cell_v)
        data_obj = Data(filename, 'timeseries', data_dict)
        time = list(data_obj.get_keys())
        values = list(data_obj.get_values())
        if plot_show:
            try:
                import matplotlib.pyplot as plt
            except:
                raise Exception('matplotlib not installed.')
            max_time = math.floor(max(time))
            index = []
            for i in range(len(time)):
                if int(math.floor(float(time[i]))) == max_time:
                    index.append(i)
            final_index = []
            for k, g in groupby(enumerate(index), lambda x:x[1]-x[0]):
                map_t = map(itemgetter(1), g)
                final_index.append(max(map_t)+1)
            init_time_index = 0
            for i in final_index:
                plt.plot(time[init_time_index:i],values[init_time_index:i])
                plt.show()
                init_time_index = i
    return data_obj
    
def import_distribution(filename, index_column, value_column, properties = {}, plot_show = False):
    '''
    TODO: To be implemented
    ''' 
    return

class Data:
    def __init__(self, name, type, data = {}):
        '''
        name : string representing the name of the data set 
        type : type of data set - whether time series (Use string 'timeseries') or distributional (use 'distrib')
        data : dictionary with key and value for the data
        '''
        self.name = name
        self.type = type
        self.data = data 

    def get_keys(self):
        '''
        Returns the key list of the data dictionary
        '''
        return list(self.data.keys())

    def get_values(self):
        '''
        Returns the values list of the data dictionary
        '''
        return list(self.data.values())

    
class StochasticInference(object):
    def __init__(self):
        self.m = None
        self.sbml = None
        self.params_to_estimate = {}
        self.prior = None
        self.nwalkers = 100
        self.nsteps = 1000
        self.nsamples = 500
        self.dimension = 0
        self.cost = 'L2norm'
        self.penalty = 1
        self.exp_data = None
        self.timepoints = []
        self.measurements = ['']
        self.species = {}
        self.parameters = {}
        return 

    def set_prior(self, prior):
        self.prior = prior
        return self.prior

    def get_parameters(self):
        return self.params_to_estimate

    def get_prior(self):
        return self.prior
   
    def log_prior(self, param_dict, prior):
        for key,value in param_dict.items():
            range = prior[key]
            if value > max(range) or value < min(range):
                return False
        return True
        
    def log_likelihood(self, log_params):
        measurements = self.measurements
        timepoints = self.timepoints
        nsamples = self.nsamples
        exp_data = self.exp_data
        cost = self.cost
        penalty = self.penalty
        param_dict = {}
        params_exp = np.exp(log_params)
        for key, p in zip(self.parameters.keys(),params_exp):
            param_dict[key] = p
        prior = self.get_prior()
        # Check prior
        if self.log_prior(param_dict, prior) == False:
            return -np.inf
        
        # Simulate for each sample in nsample and store the result for the desired output species in result array
        if self.sbml:
            try:
                import libsbml
            except:
                print('python-libsbml must be installed to use SBML models')
            filename = 'temp_simulate.xml'
            libsbml.writeSBML(self.sbml,filename)
            self.m = read_model_from_sbml(filename)
            m = self.m
            outputs = []
            for species in measurements:
                outputs.append(m.get_species_index(species))
#             print('outputs are')
#             print(outputs)
            results = np.zeros((len(timepoints), len(outputs), nsamples))
#             print('the shape of results is')
#             print(np.shape(results))
            for sample in range(nsamples):
                sim, m = self.simulate(timepoints, type = 'stochastic')
                for i in range(len(outputs)):
                    out = outputs[i]
                    results[:,i,sample] = sim[:,out]
#             print('the results have now been filled in after simulation')
#             print(results)
        else:<commit_msg>before pulling latest upstream<commit_after>import numpy as np
import sys
import warnings
import emcee
import matplotlib.pyplot as plt
from bioscrape.types import Model, read_model_from_sbml
from bioscrape.simulator import ModelCSimInterface, DeterministicSimulator, SSASimulator

def import_timeseries(filename, time_column, value_column, properties = {}, plot_show = False):
    '''
    filename : csv file with columns for data values 
    (The column numbers start at 1)
    time_column : the column number in the file that has all the time series indexes that you want to import
    value_column : the column number in the file that has all the corresponding values that you want to import 
    properties : Optional dictionary to specify other properties that the imported data must satisfy. For example, 
    properties = {3 : 'abc'}, would only impor those rows that have column 3 value equal to 'abc'
    '''
    try:
        import csv
        from operator import itemgetter
        from itertools import groupby
        import math
    except:
        print('Packages not found. Make sure csv, operator, itertool, and math are installed.')

    with open(filename) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        data_dict = {}
        for row in csv_reader:
            if row and row[time_column - 1] and row[value_column - 1]:
                if properties:
                    for col, value in properties.items():
                        if row[col - 1] == str(value):
                            data_dict[float(row[time_column - 1])] = float(row[value_column - 1])
                        else:
                            break 
                else:
                    cell_t = row[time_column - 1]
                    cell_v = row[value_column - 1]
                    temp_str_t = cell_t.replace('.','',1).replace('e','',1).replace('-','',1)
                    temp_str_v = cell_v.replace('.','',1).replace('e','',1).replace('-','',1)
                    if temp_str_t.isdigit() and temp_str_v.isdigit():
                        data_dict[float(cell_t)] = float(cell_v)
        data_obj = Data(filename, 'timeseries', data_dict)
        time = list(data_obj.get_keys())
        values = list(data_obj.get_values())
        if plot_show:
            try:
                import matplotlib.pyplot as plt
            except:
                raise Exception('matplotlib not installed.')
            max_time = math.floor(max(time))
            index = []
            for i in range(len(time)):
                if int(math.floor(float(time[i]))) == max_time:
                    index.append(i)
            final_index = []
            for k, g in groupby(enumerate(index), lambda x:x[1]-x[0]):
                map_t = map(itemgetter(1), g)
                final_index.append(max(map_t)+1)
            init_time_index = 0
            for i in final_index:
                plt.plot(time[init_time_index:i],values[init_time_index:i])
                plt.show()
                init_time_index = i
    return data_obj
    
def import_distribution(filename, index_column, value_column, properties = {}, plot_show = False):
    '''
    TODO: To be implemented
    ''' 
    return

class Data:
    def __init__(self, name, type, data = {}):
        '''
        name : string representing the name of the data set 
        type : type of data set - whether time series (Use string 'timeseries') or distributional (use 'distrib')
        data : dictionary with key and value for the data
        '''
        self.name = name
        self.type = type
        self.data = data 

    def get_keys(self):
        '''
        Returns the key list of the data dictionary
        '''
        return list(self.data.keys())

    def get_values(self):
        '''
        Returns the values list of the data dictionary
        '''
        return list(self.data.values())

    
class StochasticInference(object):
    def __init__(self):
        self.m = None
        self.params_to_estimate = {}
        self.prior = None
        self.nwalkers = 100
        self.nsteps = 1000
        self.nsamples = 500
        self.dimension = 0
        self.cost = 'L2norm'
        self.penalty = 1
        self.exp_data = None
        self.timepoints = []
        self.measurements = ['']
        self.species = {}
        self.parameters = {}
        return 

    def set_prior(self, prior):
        self.prior = prior
        return self.prior

    def get_parameters(self):
        return self.params_to_estimate

    def get_prior(self):
        return self.prior
   
    def log_prior(self, param_dict, prior):
        for key,value in param_dict.items():
            range = prior[key]
            if value > max(range) or value < min(range):
                return False
        return True
        
    def log_likelihood(self, log_params):
        measurements = self.measurements
        timepoints = self.timepoints
        nsamples = self.nsamples
        exp_data = self.exp_data
        cost = self.cost
        penalty = self.penalty
        param_dict = {}
        params_exp = np.exp(log_params)
        for key, p in zip(self.parameters.keys(),params_exp):
            param_dict[key] = p
        self.m.set_params(param_dict)
        prior = self.get_prior()
        # Check prior
        if self.log_prior(param_dict, prior) == False:
            return -np.inf
        
        # Simulate for each sample in nsample and store the result for the desired output species in result array
        if self.m:
            try:
                import libsbml
            except:
                print('python-libsbml must be installed to use SBML models')
            filename = 'temp_simulate.xml'
            libsbml.writeSBML(self.sbml,filename)
            self.m = read_model_from_sbml(filename)
            m = self.m
            outputs = []
            for species in measurements:
                outputs.append(m.get_species_index(species))
#             print('outputs are')
#             print(outputs)
            results = np.zeros((len(timepoints), len(outputs), nsamples))
#             print('the shape of results is')
#             print(np.shape(results))
            for sample in range(nsamples):
                sim, m = self.simulate(timepoints, type = 'stochastic')
                for i in range(len(outputs)):
                    out = outputs[i]
                    results[:,i,sample] = sim[:,out]
#             print('the results have now been filled in after simulation')
#             print(results)
        else:
{'fraction_non_alphanumeric': 0.0931047619047619, 'fraction_numerical': 0.005180952380952381, 'mean_word_length': 1.8343770244007773, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5097466', 'n_tokens_mistral': 3722, 'n_tokens_neox': 3544, 'n_words': 1035}