def test_read_task(self):
        filename = os.path.join(self.dirname, 'model.bngl')
        with open(filename, 'w') as file:
            file.write('ignored before content\n')
            file.write('\n')
            file.write('begin model\n')
            file.write('begin  parameters\n')
            file.write('    k_1  0.0\n')
            file.write('    # k_2 0.0\n')
            file.write('end  parameters\n')
            file.write('end model\n')
            file.write('\n')
            file.write('ode\n')

        with pytest.warns(IgnoredBnglFileContentWarning,
                          match='outside content blocks were ignored'):
            task = read_task(filename)
        self.assertTrue(
            task.model.is_equal(Model({'parameters':
                                       ModelBlock(['k_1 0.0'])})))
        task.actions = ['ode']

        read_task(
            os.path.join(os.path.dirname(__file__), 'fixtures', 'test.bngl'))
        read_task(
            os.path.join(os.path.dirname(__file__), 'fixtures', 'dolan.bngl'))
    def test_get_parameters_variables_outputs_for_simulation(self):
        fixtures_dirname = os.path.join(os.path.dirname(__file__), 'fixtures')
        for model_filename in [
                os.path.join(fixtures_dirname, 'test.bngl'),
                os.path.join(fixtures_dirname, 'LR_comp_resolved.bngl'),
        ]:
            changes, sim, variables, plots = get_parameters_variables_outputs_for_simulation(
                model_filename, None, UniformTimeCourseSimulation, None)

            task = read_task(model_filename)
            task.actions = []

            for change in changes:
                add_model_attribute_change_to_task(task, change)

            add_variables_to_model(task.model, variables)

            model_filename_2 = os.path.join(self.dirname, 'task.bngl')
            write_task(task, model_filename_2)

            changes_2, sim, variables_2, plots_2 = get_parameters_variables_outputs_for_simulation(
                model_filename_2, None, UniformTimeCourseSimulation, None)
            for change, change_2 in zip(changes, changes_2):
                self.assertTrue(change_2.is_equal(change))
            for variable, variable_2 in zip(variables, variables_2):
                self.assertTrue(variable_2.is_equal(variable))

            task_2 = read_task(model_filename_2)
            self.assertEqual(task_2.model, task.model)
Beispiel #3
0
    def test_add_model_attribute_change_to_task(self):
        model_filename = os.path.join(os.path.dirname(__file__), 'fixtures', 'test.bngl')

        task = read_task(model_filename)
        task.model['compartments'] = [
            'EC 3 vol_EC',
            'PM 2 sa_PM*eff_width EC',
        ]
        change = ModelAttributeChange(target='compartments.EC.size', new_value='0.5')
        add_model_attribute_change_to_task(task, change)
        self.assertEqual(task.model['compartments'], [
            'EC 3 0.5',
            'PM 2 sa_PM*eff_width EC',
        ])

        task = read_task(model_filename)
        change = ModelAttributeChange(target='functions.gfunc().expression', new_value='0.5')
        add_model_attribute_change_to_task(task, change)
        self.assertEqual(task.model['functions'], ['gfunc() = 0.5'])

        task = read_task(model_filename)
        change = ModelAttributeChange(target='functions.gfunc.expression', new_value='0.5')
        add_model_attribute_change_to_task(task, change)
        self.assertEqual(task.model['functions'], ['gfunc() = 0.5'])

        change = ModelAttributeChange(target='parameters.k_1.value', new_value='1.0')
        add_model_attribute_change_to_task(task, change)
        self.assertEqual(task.actions[-1], 'setParameter("k_1", 1.0)')

        change = ModelAttributeChange(target='species.GeneA_00().initialCount', new_value='1')
        add_model_attribute_change_to_task(task, change)
        self.assertEqual(task.actions[-1], 'setConcentration("GeneA_00()", 1)')

        # error: no compartment
        task = read_task(model_filename)
        task.model['compartments'] = [
            'EC 3 vol_EC',
            'PM 2 sa_PM*eff_width EC',
        ]
        change = ModelAttributeChange(target='compartments.CE.size', new_value='0.5')
        with self.assertRaisesRegex(ValueError, 'the model does not have a compartment'):
            add_model_attribute_change_to_task(task, change)

        # error: no function
        change = ModelAttributeChange(target='functions.hfunc.expression', new_value='0.5')
        with self.assertRaisesRegex(ValueError, 'the model does not have a function'):
            add_model_attribute_change_to_task(task, change)

        # error: no function
        change = ModelAttributeChange(target='functions.hfunc().expression', new_value='0.5')
        with self.assertRaisesRegex(ValueError, 'the model does not have a function'):
            add_model_attribute_change_to_task(task, change)

        # error: no function
        change = ModelAttributeChange(target='section.object.attribute', new_value='0.5')
        with self.assertRaisesRegex(NotImplementedError, 'is not a valid target'):
            add_model_attribute_change_to_task(task, change)
Beispiel #4
0
    def test_exec_bionetgen_task(self):
        model_filename = os.path.join(os.path.dirname(__file__), 'fixtures', 'test.bngl')
        task = read_task(model_filename)

        results = exec_bionetgen_task(task)

        self.assertEqual(set(results.index), set([
            'time',
            'Atot',
            'Btot',
            'GA00tot',
            'GA01tot',
            'GA10tot',
            'GB00tot',
            'GB01tot',
            'GB10tot',
        ]))

        self.assertFalse(numpy.any(numpy.isnan(results)))
        numpy.testing.assert_allclose(results.loc['time', :], numpy.linspace(0., 1000000., 1000 + 1))

        # error handling
        with mock.patch('subprocess.check_call', side_effect=ValueError('big error')):
            with self.assertRaisesRegex(ValueError, 'big error'):
                exec_bionetgen_task(task)
    def test_add_variables_to_task(self):
        model_filename = os.path.join(os.path.dirname(__file__), 'fixtures',
                                      'test.bngl')
        task = read_task(model_filename)
        task.model.pop('observables')

        variables = [
            Variable(id='Time', symbol=Symbol.time),
            Variable(id='A', target='species.A'),
            Variable(id='B', target='species.B.count'),
            Variable(id='Atot', target='molecules.A()'),
            Variable(id='GA00tot', target='molecules.GeneA_00().count'),
        ]
        add_variables_to_model(task.model, variables)

        self.assertEqual(task.model['observables'], [
            'Species A A',
            'Species B B',
            'Molecules Atot A()',
            'Molecules GA00tot GeneA_00()',
        ])

        task.model.move_to_end('functions')
        task.model.move_to_end('reaction rules')
        results = exec_bionetgen_task(task)

        self.assertEqual(set(results.index),
                         set([
                             'time',
                             'A',
                             'B',
                             'Atot',
                             'GA00tot',
                         ]))

        add_variables_to_model(task.model, [])
        self.assertEqual(task.model['observables'], [
            'Species A A',
            'Species B B',
            'Molecules Atot A()',
            'Molecules GA00tot GeneA_00()',
        ])

        # error handling
        with self.assertRaisesRegex(NotImplementedError,
                                    'symbols are not supported'):
            add_variables_to_model(task.model, [Variable(id='X', symbol='x')])

        with self.assertRaisesRegex(NotImplementedError,
                                    'targets are not supported'):
            add_variables_to_model(task.model, [Variable(id='X', target='x')])
    def test_write_task(self):
        task = Task()
        model = task.model = Model()
        model['parameters'] = ModelBlock([
            'k_1 0.0',
            'r_2 0.0',
        ])
        model['molecule types'] = ModelBlock([
            'A()',
            'B()',
            'C()',
        ])
        task.actions = ['ode', 'nf']

        filename = os.path.join(self.dirname, 'task.bngl')
        write_task(task, filename)

        with open(filename, 'r') as file:
            lines = file.readlines()

        self.assertEqual(lines, [
            'begin model\n',
            'begin parameters\n',
            '    k_1 0.0\n',
            '    r_2 0.0\n',
            'end parameters\n',
            'begin molecule types\n',
            '    A()\n',
            '    B()\n',
            '    C()\n',
            'end molecule types\n',
            'end model\n',
            'ode\n',
            'nf\n',
        ])

        task2 = read_task(filename)
        self.assertTrue(task2.is_equal(task))
    def test_read_task_error_handling(self):
        # no `begin model`
        filename = os.path.join(self.dirname, 'model.bngl')
        with open(filename, 'w') as file:
            file.write('\n')

        with self.assertRaisesRegex(ValueError, 'does not contain a model'):
            read_task(filename)

        # no `end model`
        filename = os.path.join(self.dirname, 'model.bngl')
        with open(filename, 'w') as file:
            file.write('begin model\n')

        with self.assertRaisesRegex(ValueError, 'has no termination'):
            read_task(filename)

        # no `begin model`
        filename = os.path.join(self.dirname, 'model.bngl')
        with open(filename, 'w') as file:
            file.write('begin parameters\n')

        with self.assertRaisesRegex(ValueError, 'has no termination'):
            read_task(filename)

        # two parameters blocks
        filename = os.path.join(self.dirname, 'model.bngl')
        with open(filename, 'w') as file:
            file.write('begin model\n')
            file.write('begin parameters\n')
            file.write('    k_1 0.0\n')
            file.write('end parameters\n')
            file.write('begin parameters\n')
            file.write('    k_2 0.0\n')
            file.write('end parameters\n')
            file.write('end model\n')

        with self.assertRaisesRegex(ValueError, 'has a second'):
            read_task(filename)

        # no triple nesting of block
        filename = os.path.join(self.dirname, 'model.bngl')
        with open(filename, 'w') as file:
            file.write('begin model\n')
            file.write('begin parameters\n')
            file.write('    begin parameters\n')
            file.write('    end parameters\n')
            file.write('end parameters\n')
            file.write('end model\n')

        with self.assertRaisesRegex(ValueError, 'is inappropriately nested'):
            read_task(filename)

        # mismatched ending
        filename = os.path.join(self.dirname, 'model.bngl')
        with open(filename, 'w') as file:
            file.write('begin model\n')
            file.write('begin parameters\n')
            file.write('end reactions\n')
            file.write('end model\n')

        with self.assertRaisesRegex(ValueError, 'incorrectly ends'):
            read_task(filename)

        # inappropriately nested content
        filename = os.path.join(self.dirname, 'model.bngl')
        with open(filename, 'w') as file:
            file.write('begin model\n')
            file.write('    k_1 0.0\n')
            file.write('end model\n')

        with pytest.warns(IgnoredBnglFileContentWarning,
                          match='outside content blocks were ignored'):
            read_task(filename)

        # inappropriately nested content
        filename = os.path.join(self.dirname, 'model.bngl')
        with open(filename, 'w') as file:
            file.write('begin parameters\n')
            file.write('begin parameters\n')
            file.write('    k_1 0.0\n')
            file.write('end parameters\n')
            file.write('end parameters\n')

        with self.assertRaisesRegex(ValueError, 'inappropriately nested'):
            read_task(filename)

        # no second model
        filename = os.path.join(self.dirname, 'model.bngl')
        with open(filename, 'w') as file:
            file.write('begin model\n')
            file.write('    k_1 0.0\n')
            file.write('end model\n')
            file.write('begin model\n')
            file.write('    k_1 0.0\n')
            file.write('end model\n')

        with self.assertRaisesRegex(ValueError, 'contains a second model'):
            read_task(filename)