<commit_before>import unittest
import os

import scipy

from SloppyCell.ReactionNetworks import *

file_path = os.path.join('SBML_files',
                         'algebraicRules-assignment_in_algebraic.xml')
base_net = IO.from_SBML_file(file_path,
                               'algebraicRules-assignment_in_algebraic')
base_net.remove_component('event0')
base_net.set_var_constant('k1', False)

dyn_var = 'X1'
opt_var = 'Keq'
eps = 1e-4
times = [0, 10]

def do_sens_and_fd(net):
    params = net.GetParameters()
    plus_params = params.copy()
    plus_params.set(opt_var, plus_params.get(opt_var) + eps)

    sens_traj = Dynamics.integrate_sensitivity(net, times, params, 
                                               fill_traj=False)

    traj_central = Dynamics.integrate(net, times, rtol=1e-10, params=params,
                                      fill_traj=False)
    traj_plus = Dynamics.integrate(net, times, rtol=1e-10, 
                                   params=plus_params, fill_traj=False)

    return sens_traj, traj_central, traj_plus

class test_SensitivityEvents(unittest.TestCase):
    def assert_over_all_vars(self, sens_traj, traj_central, traj_plus):
        for dyn_var in sens_traj.dynamicVarKeys:
            y_plus = traj_plus.get_var_traj(dyn_var)
            y_central = traj_central.get_var_traj(dyn_var)
            diff_sens = (y_plus-y_central)/eps

            sens_val = sens_traj.get_var_val_index((dyn_var, opt_var), -1)
            self.assertAlmostEqual(diff_sens[-1], sens_val, 4, 
                                   'failed for %s, %g vs %s' % (dyn_var, diff_sens[01], sens_val))

    def test_basic(self):
        net = base_net.copy('test')
        net.add_event('event 1', 'gt(time, 4)', {'k1': 4.3})
        sens_traj, traj_central, traj_plus = do_sens_and_fd(net)
        self.assert_over_all_vars(sens_traj, traj_central, traj_plus)

    def test_basic_delay(self):
        net = base_net.copy('test')
        net.add_event('event 1', 'gt(time, 5)', 
                      {'k1': 2}, delay = 3)
        sens_traj, traj_central, traj_plus = do_sens_and_fd(net)<commit_msg>Nicer error reporting. Removed extraneous scipy import<commit_after>import unittest
import os

from SloppyCell.ReactionNetworks import *

file_path = os.path.join('SBML_files',
                         'algebraicRules-assignment_in_algebraic.xml')
base_net = IO.from_SBML_file(file_path,
                               'algebraicRules-assignment_in_algebraic')
base_net.remove_component('event0')
base_net.set_var_constant('k1', False)

dyn_var = 'X1'
opt_var = 'Keq'
eps = 1e-4
times = [0, 10]

def do_sens_and_fd(net):
    params = net.GetParameters()
    plus_params = params.copy()
    plus_params.set(opt_var, plus_params.get(opt_var) + eps)

    sens_traj = Dynamics.integrate_sensitivity(net, times, params, 
                                               fill_traj=False)

    traj_central = Dynamics.integrate(net, times, rtol=1e-10, params=params,
                                      fill_traj=False)
    traj_plus = Dynamics.integrate(net, times, rtol=1e-10, 
                                   params=plus_params, fill_traj=False)

    return sens_traj, traj_central, traj_plus

class test_SensitivityEvents(unittest.TestCase):
    def assert_over_all_vars(self, sens_traj, traj_central, traj_plus):
        for dyn_var in sens_traj.dynamicVarKeys:
            y_plus = traj_plus.get_var_traj(dyn_var)
            y_central = traj_central.get_var_traj(dyn_var)
            diff_sens = (y_plus-y_central)/eps

            sens_val = sens_traj.get_var_val_index((dyn_var, opt_var), -1)
            self.assertAlmostEqual(diff_sens[-1], sens_val, 4, 
                                   'failed for (%s, %s), %g vs %g'
                                   % (dyn_var, opt_var, diff_sens[-1],
                                      sens_val))

    def test_basic(self):
        net = base_net.copy('test')
        net.add_event('event 1', 'gt(time, 4)', {'k1': 4.3})
        sens_traj, traj_central, traj_plus = do_sens_and_fd(net)
        self.assert_over_all_vars(sens_traj, traj_central, traj_plus)

    def test_basic_delay(self):
        net = base_net.copy('test')
        net.add_event('event 1', 'gt(time, 5)', 
                      {'k1': 2}, delay = 3)
        sens_traj, traj_central, traj_plus = do_sens_and_fd(net)
{'fraction_non_alphanumeric': 0.10590397427061797, 'fraction_numerical': 0.013094417643004824, 'mean_word_length': 2.43646408839779, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4101782', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1470, 'n_words': 251}