示例#1
0
    def test_add_smoldyn_output_files_for_sed_variables(self):
        filename = os.path.join(self.EXAMPLES_DIRNAME, 'S1_intro',
                                'bounce1.txt')
        config = smoldyn.biosimulators.combine.read_smoldyn_simulation_configuration(
            filename)
        smoldyn.biosimulators.combine.disable_smoldyn_graphics_in_simulation_configuration(
            config)

        filename2 = os.path.join(self.dirname, 'config.txt')
        smoldyn.biosimulators.combine.write_smoldyn_simulation_configuration(
            config, filename2)

        sim = smoldyn.biosimulators.combine.init_smoldyn_simulation_from_configuration_file(
            filename2)
        sim.setGraphics('none')

        var = Variable(id='time', symbol=Symbol.time.value)
        variable_output_cmd_map = smoldyn.biosimulators.combine.validate_variables(
            [var])
        smoldyn_output_files = smoldyn.biosimulators.combine.add_smoldyn_output_files_for_sed_variables(
            self.dirname, [var], variable_output_cmd_map, sim)
        self.assertEqual(set(smoldyn_output_files.keys()), set(['molcount']))

        var.symbol = 'undefined'
        with self.assertRaises(ValueError):
            smoldyn.biosimulators.combine.validate_variables([var])

        var.symbol = None
        var.target = 'undefined '
        with self.assertRaises(NotImplementedError):
            smoldyn.biosimulators.combine.validate_variables([var])
示例#2
0
    def test_get_variables_results_from_observable_results(self):
        bionetgen_path = Config().bionetgen_path
        model_filename = os.path.join(os.path.dirname(__file__), 'fixtures', 'test.bngl')
        subprocess.check_call([bionetgen_path, model_filename, '--outdir', self.dirname])

        obs_results = read_simulation_results(os.path.join(self.dirname, 'test.gdat'))
        variables = [
            Variable(id='Time', symbol=Symbol.time),
            Variable(id='Atot', target='A()'),
            Variable(id='GA00tot', target='GeneA_00()'),
        ]
        var_results = get_variables_results_from_observable_results(obs_results, variables)

        self.assertEqual(set(var_results.keys()), set([
            'Time',
            'Atot',
            'GA00tot',
        ]))

        for values in var_results.values():
            self.assertFalse(numpy.any(numpy.isnan(values)))
        numpy.testing.assert_allclose(var_results['Time'], numpy.linspace(0., 1000000., 1000 + 1))

        # handle errors
        variables.append(Variable(id='X', symbol='x'))
        with self.assertRaisesRegex(NotImplementedError, 'symbols are not supported'):
            get_variables_results_from_observable_results(obs_results, variables)

        variables[-1].symbol = None
        variables[-1].target = 'undefined'
        with self.assertRaisesRegex(ValueError, 'could not be recorded'):
            get_variables_results_from_observable_results(obs_results, variables)
示例#3
0
    def test_exec_sed_task_negative_initial_time(self):
        task = Task(
            id='task',
            model=Model(
                id='model',
                source=os.path.join(self.EXAMPLES_DIRNAME, 'S1_intro',
                                    'bounce1.txt'),
                language=ModelLanguage.Smoldyn.value,
            ),
            simulation=UniformTimeCourseSimulation(
                initial_time=-0.01,
                output_start_time=-0.01,
                output_end_time=0.09,
                number_of_points=10,
                algorithm=Algorithm(kisao_id='KISAO_0000057', )),
        )

        variables = [
            Variable(id='time', symbol=Symbol.time.value, task=task),
            Variable(id='red', target='molcount red', task=task),
            Variable(id='green', target='molcount green', task=task),
        ]

        results, _ = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables)

        self.assertEqual(set(results.keys()), set(['time', 'red', 'green']))
        numpy.testing.assert_allclose(results['time'],
                                      numpy.linspace(-0.01, 0.09, 11),
                                      rtol=5e-6)
示例#4
0
    def test_get_parameters_variables_for_simulation_compartmentalized(self):
        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.COMP_FIXTURE_FILENAME, None, UniformTimeCourseSimulation, None)

        self.assertTrue(params[0].is_equal(ModelAttributeChange(
            id='value_parameter_NaV',
            name='Value of parameter "NaV"',
            target='parameters.NaV.value',
            new_value='6.02e8',
        )))
        self.assertTrue(params[2].is_equal(ModelAttributeChange(
            id='value_parameter_Vec',
            name='Value of parameter "Vec"',
            target='parameters.Vec.value',
            new_value='1000*Vcell',
        )))
        self.assertTrue(params[11].is_equal(ModelAttributeChange(
            id='initial_size_compartment_EC',
            name='Initial size of 3-D compartment "EC"',
            target='compartments.EC.size',
            new_value='1000000',
        )))
        self.assertTrue(params[12].is_equal(ModelAttributeChange(
            id='initial_size_compartment_PM',
            name='Initial size of 2-D compartment "PM"',
            target='compartments.PM.size',
            new_value='10',
        )))
        self.assertTrue(params[14].is_equal(ModelAttributeChange(
            id='initial_amount_species__EC_L_r_',
            name='Initial amount of species "@EC:L(r)"',
            target='species.@EC:L(r).initialCount',
            new_value='L0',
        )))
        self.assertEqual(len(params), 16)

        self.assertTrue(vars[0].is_equal(Variable(
            id='time',
            name='Time',
            symbol=Symbol.time.value,
        )))
        self.assertTrue(vars[1].is_equal(Variable(
            id='amount_molecule_L_r_',
            name='Dynamics of molecule "L(r)"',
            target='molecules.L(r).count',
        )))
        self.assertTrue(vars[3].is_equal(Variable(
            id='amount_species__EC_L_r_',
            name='Dynamics of species "@EC:L(r)"',
            target='species.@EC:L(r).count',
        )))
        self.assertTrue(vars[5].is_equal(Variable(
            id='amount_molecule_L_r_1__R_l_1_',
            name='Dynamics of molecule "L(r!1).R(l!1)"',
            target='molecules.L(r!1).R(l!1).count',
        )))
        self.assertEqual(len(vars), 7)
示例#5
0
    def test_get_variable_results_2d(self):
        filename = os.path.join(self.EXAMPLES_DIRNAME, 'S4_molecules',
                                'isotropic', 'diffi.txt')
        config = smoldyn.biosimulators.combine.read_smoldyn_simulation_configuration(
            filename)
        smoldyn.biosimulators.combine.disable_smoldyn_graphics_in_simulation_configuration(
            config)

        filename2 = os.path.join(self.dirname, 'config.txt')
        smoldyn.biosimulators.combine.write_smoldyn_simulation_configuration(
            config, filename2)

        sim = smoldyn.biosimulators.combine.init_smoldyn_simulation_from_configuration_file(
            filename2)
        sim.setGraphics('none')

        vars = [
            Variable(id='time', symbol=Symbol.time.value),
            Variable(
                id='greenHist',
                target='molcountspace2d green(all) z -10 10 30 -10 10 40 -10 10'
            )
        ]
        variable_output_cmd_map = smoldyn.biosimulators.combine.validate_variables(
            vars)
        smoldyn_output_files = smoldyn.biosimulators.combine.add_smoldyn_output_files_for_sed_variables(
            self.dirname, vars, variable_output_cmd_map, sim)

        sim.run(start=0.,
                stop=0.2,
                dt=0.01,
                overwrite=True,
                display=False,
                quit_at_end=False)

        results = smoldyn.biosimulators.combine.get_variable_results(
            10, vars, variable_output_cmd_map, smoldyn_output_files)
        self.assertEqual(set(results.keys()), set(['time', 'greenHist']))
        numpy.testing.assert_allclose(results['time'],
                                      numpy.linspace(0.1, 0.2, 11))

        self.assertEqual(results['greenHist'].shape, (11, 30, 40))
        self.assertFalse(numpy.any(numpy.isnan(results['greenHist'])))
    def test_get_parameters_variables_for_simulation_version_2_native_ids_data_types(
            self):
        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.V2_FIXTURE_FILENAME,
            None,
            UniformTimeCourseSimulation,
            None,
            native_ids=True,
            native_data_types=True)
        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.V2_FIXTURE_FILENAME,
            None,
            OneStepSimulation,
            None,
            native_ids=True,
            native_data_types=True)

        self.assertEqual(len(params), 1)
        self.assertEqual(len(vars), 3)

        namespaces = {
            'cellml': 'http://www.cellml.org/cellml/2.0#',
        }

        print(params[0].id)
        print(params[0].new_value)
        self.assertTrue(params[0].is_equal(
            ModelAttributeChange(
                id='level2_component.cosine',
                name=None,
                target=("/cellml:model"
                        "/cellml:component[@name='level2_component']"
                        "/cellml:variable[@name='cosine']"
                        "/@initial_value"),
                target_namespaces=namespaces,
                new_value=0.,
            )))

        self.assertEqual(len(sims), 1)

        sim = sims[0]
        self.assertIsInstance(sim, OneStepSimulation)

        self.assertTrue(vars[0].is_equal(
            Variable(
                id='level2_component.time',
                name=None,
                target=("/cellml:model"
                        "/cellml:component[@name='level2_component']"
                        "/cellml:variable[@name='time']"),
                target_namespaces=namespaces,
            )))
    def test_get_parameters_variables_for_simulation_version_1_native_ids_data_types(
            self):
        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.V1_FIXTURE_FILENAME,
            None,
            OneStepSimulation,
            None,
            native_ids=True,
            native_data_types=True)
        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.V1_FIXTURE_FILENAME,
            None,
            UniformTimeCourseSimulation,
            None,
            native_ids=True,
            native_data_types=True)

        self.assertEqual(len(params), 13)
        self.assertEqual(len(vars), 54)

        namespaces = {
            'cellml': 'http://www.cellml.org/cellml/1.0#',
        }

        self.assertTrue(params[0].is_equal(
            ModelAttributeChange(
                id='total_cytoplasmic_Ca_flux.F',
                name=None,
                target=("/cellml:model"
                        "/cellml:component[@name='total_cytoplasmic_Ca_flux']"
                        "/cellml:variable[@name='F']"
                        "/@initial_value"),
                target_namespaces=namespaces,
                new_value=96.5,
            )))

        self.assertEqual(len(sims), 1)

        sim = sims[0]
        self.assertIsInstance(sim, UniformTimeCourseSimulation)

        self.assertTrue(vars[0].is_equal(
            Variable(
                id='environment.time',
                name=None,
                target=("/cellml:model"
                        "/cellml:component[@name='environment']"
                        "/cellml:variable[@name='time']"),
                target_namespaces=namespaces,
            )))
    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_get_parameters_variables_for_simulation_version_2(self):
        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.V2_FIXTURE_FILENAME, None, UniformTimeCourseSimulation, None)
        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.V2_FIXTURE_FILENAME, None, OneStepSimulation, None)

        self.assertEqual(len(params), 1)
        self.assertEqual(len(vars), 3)

        namespaces = {
            'cellml': 'http://www.cellml.org/cellml/2.0#',
        }

        self.assertTrue(params[0].is_equal(
            ModelAttributeChange(
                id='initial_value_component_level2_component_variable_cosine',
                name=
                'Initial value of variable "cosine" of component "level2_component"',
                target=("/cellml:model"
                        "/cellml:component[@name='level2_component']"
                        "/cellml:variable[@name='cosine']"
                        "/@initial_value"),
                target_namespaces=namespaces,
                new_value='0',
            )))

        self.assertEqual(len(sims), 1)

        sim = sims[0]
        self.assertIsInstance(sim, OneStepSimulation)

        self.assertTrue(vars[0].is_equal(
            Variable(
                id='value_component_level2_component_variable_time',
                name='Value of variable "time" of component "level2_component"',
                target=("/cellml:model"
                        "/cellml:component[@name='level2_component']"
                        "/cellml:variable[@name='time']"),
                target_namespaces=namespaces,
            )))
    def tezst_get_parameters_variables_for_simulation_native_ids_data_types(
            self):
        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.FIXTURE_FILENAME,
            None,
            UniformTimeCourseSimulation,
            None,
            native_ids=True,
            native_data_types=True)

        self.assertTrue(params[0].is_equal(
            ModelAttributeChange(
                id='red',
                name=None,
                target='difc red',
                new_value=3,
            )))
        self.assertTrue(params[1].is_equal(
            ModelAttributeChange(
                id='green',
                name=None,
                target='difc green',
                new_value=1,
            )))

        self.assertEqual(len(sims), 1)

        sim = sims[0]
        self.assertEqual(sim.initial_time, 0.0)
        self.assertEqual(sim.output_start_time, 0.0)
        self.assertEqual(sim.output_end_time, 100.0)
        self.assertEqual(sim.number_of_steps, 10000)
        self.assertEqual(sim.algorithm.kisao_id, 'KISAO_0000057')

        self.assertTrue(vars[0].is_equal(
            Variable(
                id=None,
                name=None,
                symbol=Symbol.time.value,
            )))
        self.assertTrue(vars[1].is_equal(
            Variable(
                id='red',
                name=None,
                target='molcount red',
            )))
        self.assertTrue(vars[2].is_equal(
            Variable(
                id='green',
                name=None,
                target='molcount green',
            )))

        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.COMP_FIXTURE_FILENAME,
            None,
            UniformTimeCourseSimulation,
            None,
            native_ids=True,
            native_data_types=True)

        self.assertTrue(vars[0].is_equal(
            Variable(
                id=None,
                name=None,
                symbol=Symbol.time.value,
            )))
        self.assertTrue(vars[1].is_equal(
            Variable(
                id='red',
                name=None,
                target='molcount red',
            )))
        self.assertTrue(vars[2].is_equal(
            Variable(
                id='red.middle',
                name=None,
                target='molcountincmpt red middle',
            )))
        self.assertTrue(vars[3].is_equal(
            Variable(
                id='red.walls',
                name=None,
                target='molcountonsurf red walls',
            )))
    def test_exec_sedml_docs_in_archive_without_log(self):
        archive = CombineArchive(contents=[
            CombineArchiveContent(
                location='sim.sedml',
                format='http://identifiers.org/combine.specifications/sed-ml',
            ),
            CombineArchiveContent(
                location='model.xml',
                format='http://identifiers.org/combine.specifications/sbml',
            ),
        ], )

        sed_doc = SedDocument()
        model = Model(id='model_1',
                      source='model.xml',
                      language=ModelLanguage.SBML.value)
        sed_doc.models.append(model)
        sim = UniformTimeCourseSimulation(
            id='sim_1',
            initial_time=0.,
            output_start_time=0.,
            output_end_time=10.,
            number_of_points=10,
            algorithm=Algorithm(kisao_id='KISAO_0000019'))
        sed_doc.simulations.append(sim)
        task = Task(id='task_1', model=model, simulation=sim)
        sed_doc.tasks.append(task)
        sed_doc.data_generators.append(
            DataGenerator(
                id='data_gen_1',
                variables=[
                    Variable(
                        id='var_1',
                        target=
                        "/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='Trim']",
                        target_namespaces={
                            'sbml': 'http://www.sbml.org/sbml/level2/version4'
                        },
                        task=task)
                ],
                math='var_1',
            ))
        sed_doc.data_generators.append(
            DataGenerator(
                id='data_gen_2',
                variables=[
                    Variable(
                        id='var_2',
                        target=
                        "/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='Clb']",
                        target_namespaces={
                            'sbml': 'http://www.sbml.org/sbml/level2/version4'
                        },
                        task=task)
                ],
                math='var_2',
            ))
        report = Report(id='output_1')
        sed_doc.outputs.append(report)
        report.data_sets.append(
            DataSet(id='data_set_1',
                    label='data_set_1',
                    data_generator=sed_doc.data_generators[0]))
        report.data_sets.append(
            DataSet(id='data_set_2',
                    label='data_set_2',
                    data_generator=sed_doc.data_generators[1]))

        archive_dirname = os.path.join(self.tmp_dir, 'archive')
        os.makedirs(archive_dirname)

        shutil.copyfile(
            os.path.join(os.path.dirname(__file__), '..', 'fixtures',
                         'BIOMD0000000297.xml'),
            os.path.join(archive_dirname, 'model.xml'))
        SedmlSimulationWriter().run(sed_doc,
                                    os.path.join(archive_dirname, 'sim.sedml'))

        archive_filename = os.path.join(self.tmp_dir, 'archive.omex')
        CombineArchiveWriter().run(archive, archive_dirname, archive_filename)

        def sed_task_executer(task, variables, log=None, config=None):
            if log:
                log.algorithm = task.simulation.algorithm.kisao_id
                log.simulator_details = {
                    'attrib': 'value',
                }

            return VariableResults({
                'var_1':
                numpy.linspace(0., 10., task.simulation.number_of_points + 1),
                'var_2':
                numpy.linspace(10., 20., task.simulation.number_of_points + 1),
            }), log

        def sed_task_executer_error(task, variables, log=None, config=None):
            raise ValueError('Big error')

        out_dir = os.path.join(self.tmp_dir, 'outputs')

        config = get_config()
        config.REPORT_FORMATS = []
        config.VIZ_FORMATS = []
        config.COLLECT_COMBINE_ARCHIVE_RESULTS = True
        config.LOG = True

        # with log
        sed_doc_executer = functools.partial(sedml_exec.exec_sed_doc,
                                             sed_task_executer)
        results, log = exec.exec_sedml_docs_in_archive(
            sed_doc_executer,
            archive_filename,
            out_dir,
            apply_xml_model_changes=False,
            config=config)
        self.assertEqual(set(results.keys()), set(['sim.sedml']))
        self.assertEqual(set(results['sim.sedml'].keys()), set(['output_1']))
        self.assertEqual(set(results['sim.sedml']['output_1'].keys()),
                         set(['data_set_1', 'data_set_2']))
        numpy.testing.assert_allclose(
            results['sim.sedml']['output_1']['data_set_1'],
            numpy.linspace(0., 10., 11))
        numpy.testing.assert_allclose(
            results['sim.sedml']['output_1']['data_set_2'],
            numpy.linspace(10., 20., 11))
        self.assertEqual(log.exception, None)
        self.assertEqual(
            log.sed_documents['sim.sedml'].tasks['task_1'].algorithm,
            task.simulation.algorithm.kisao_id)
        self.assertEqual(
            log.sed_documents['sim.sedml'].tasks['task_1'].simulator_details,
            {'attrib': 'value'})

        sed_doc_executer = functools.partial(sedml_exec.exec_sed_doc,
                                             sed_task_executer_error)
        results, log = exec.exec_sedml_docs_in_archive(
            sed_doc_executer,
            archive_filename,
            out_dir,
            apply_xml_model_changes=False,
            config=config)
        self.assertIsInstance(log.exception, CombineArchiveExecutionError)

        config.DEBUG = True
        sed_doc_executer = functools.partial(sedml_exec.exec_sed_doc,
                                             sed_task_executer_error)
        with self.assertRaisesRegex(ValueError, 'Big error'):
            exec.exec_sedml_docs_in_archive(sed_doc_executer,
                                            archive_filename,
                                            out_dir,
                                            apply_xml_model_changes=False,
                                            config=config)

        # without log
        config.COLLECT_COMBINE_ARCHIVE_RESULTS = False
        config.LOG = False
        config.DEBUG = False

        sed_doc_executer = functools.partial(sedml_exec.exec_sed_doc,
                                             sed_task_executer)
        results, log = exec.exec_sedml_docs_in_archive(
            sed_doc_executer,
            archive_filename,
            out_dir,
            apply_xml_model_changes=False,
            config=config)
        self.assertEqual(results, None)
        self.assertEqual(log, None)

        sed_doc_executer = functools.partial(sedml_exec.exec_sed_doc,
                                             sed_task_executer_error)
        with self.assertRaisesRegex(CombineArchiveExecutionError, 'Big error'):
            exec.exec_sedml_docs_in_archive(sed_doc_executer,
                                            archive_filename,
                                            out_dir,
                                            apply_xml_model_changes=False,
                                            config=config)

        config.DEBUG = True
        sed_doc_executer = functools.partial(sedml_exec.exec_sed_doc,
                                             sed_task_executer_error)
        with self.assertRaisesRegex(ValueError, 'Big error'):
            exec.exec_sedml_docs_in_archive(sed_doc_executer,
                                            archive_filename,
                                            out_dir,
                                            apply_xml_model_changes=False,
                                            config=config)
示例#12
0
    def test_exec_sed_task_with_changes(self):
        task = Task(
            id='task',
            model=Model(
                id='model',
                source=os.path.join(os.path.dirname(__file__), 'fixtures',
                                    'lotvolt.txt'),
                language=ModelLanguage.Smoldyn.value,
            ),
            simulation=UniformTimeCourseSimulation(
                initial_time=0.,
                output_start_time=0.,
                output_end_time=0.1,
                number_of_points=10,
                algorithm=Algorithm(kisao_id='KISAO_0000057',
                                    changes=[
                                        AlgorithmParameterChange(
                                            kisao_id='KISAO_0000488',
                                            new_value='10'),
                                    ])),
        )
        model = task.model
        sim = task.simulation

        variable_ids = ['rabbit', 'fox']

        variables = []
        for variable_id in variable_ids:
            variables.append(
                Variable(id=variable_id,
                         target='molcount ' + variable_id,
                         task=task))

        preprocessed_task = smoldyn.biosimulators.combine.preprocess_sed_task(
            task, variables)
        results, _ = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables, preprocessed_task=preprocessed_task)
        with self.assertRaises(AssertionError):
            for variable_id in variable_ids:
                numpy.testing.assert_allclose(
                    results[variable_id][0:int(sim.number_of_points / 2 + 1)],
                    results[variable_id][-int(sim.number_of_points / 2 + 1):])

        # check simulation is repeatable
        preprocessed_task = smoldyn.biosimulators.combine.preprocess_sed_task(
            task, variables)
        results2, _ = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables, preprocessed_task=preprocessed_task)
        for variable_id in variable_ids:
            numpy.testing.assert_allclose(results2[variable_id],
                                          results[variable_id])

        # check simulation is repeatable in two steps
        sim.output_end_time = sim.output_end_time / 2
        sim.number_of_points = int(sim.number_of_points / 2)

        model.changes = []
        for variable_id in variable_ids:
            model.changes.append(
                ModelAttributeChange(target='fixmolcount ' + variable_id,
                                     new_value=None))
        preprocessed_task = smoldyn.biosimulators.combine.preprocess_sed_task(
            task, variables)
        model.changes = []
        results2, _ = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables, preprocessed_task=preprocessed_task)
        for variable_id in variable_ids:
            numpy.testing.assert_allclose(
                results2[variable_id],
                results[variable_id][0:sim.number_of_points + 1])

        for variable_id in variable_ids:
            model.changes.append(
                ModelAttributeChange(target='fixmolcount ' + variable_id,
                                     new_value=results2[variable_id][-1]))
        results3, _ = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables, preprocessed_task=preprocessed_task)
        for variable_id in variable_ids:
            numpy.testing.assert_allclose(
                results3[variable_id],
                results[variable_id][-(sim.number_of_points + 1):])

        # check model change modifies simulation
        model.changes = []
        for variable_id in variable_ids:
            model.changes.append(
                ModelAttributeChange(target='fixmolcount ' + variable_id,
                                     new_value=None))
        preprocessed_task = smoldyn.biosimulators.combine.preprocess_sed_task(
            task, variables)
        model.changes = []
        results2, _ = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables, preprocessed_task=preprocessed_task)
        for variable_id in variable_ids:
            numpy.testing.assert_allclose(
                results2[variable_id],
                results[variable_id][0:sim.number_of_points + 1])

        for variable_id in variable_ids:
            model.changes.append(
                ModelAttributeChange(target='fixmolcount ' + variable_id,
                                     new_value=results2[variable_id][-1] + 1))
        results3, _ = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables, preprocessed_task=preprocessed_task)
        with self.assertRaises(AssertionError):
            for variable_id in variable_ids:
                numpy.testing.assert_allclose(
                    results3[variable_id],
                    results[variable_id][-(sim.number_of_points + 1):])

        # check model change modifies simulation
        model.changes = []
        for variable_id in variable_ids:
            model.changes.append(
                ModelAttributeChange(target='killmol ' + variable_id,
                                     new_value=None))
        preprocessed_task = smoldyn.biosimulators.combine.preprocess_sed_task(
            task, variables)
        model.changes = []
        results2, _ = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables, preprocessed_task=preprocessed_task)
        for variable_id in variable_ids:
            numpy.testing.assert_allclose(
                results2[variable_id],
                results[variable_id][0:sim.number_of_points + 1])

        for variable_id in variable_ids:
            model.changes.append(
                ModelAttributeChange(target='killmol ' + variable_id,
                                     new_value=0))
        results3, _ = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables, preprocessed_task=preprocessed_task)
        with self.assertRaises(AssertionError):
            for variable_id in variable_ids:
                numpy.testing.assert_allclose(
                    results3[variable_id],
                    results[variable_id][-(sim.number_of_points + 1):])

        # preprocessing-time change
        model.changes = []
        for variable_id in variable_ids:
            model.changes.append(
                ModelAttributeChange(target='define K_1', new_value=10))
        results2, _ = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables)
        for variable_id in variable_ids:
            numpy.testing.assert_allclose(
                results2[variable_id],
                results[variable_id][0:sim.number_of_points + 1])

        model.changes = []
        for variable_id in variable_ids:
            model.changes.append(
                ModelAttributeChange(target='define K_1', new_value=10))
        preprocessed_task = smoldyn.biosimulators.combine.preprocess_sed_task(
            task, variables)
        with self.assertRaisesRegex(
                NotImplementedError,
                'can only be changed during simulation preprocessing'):
            smoldyn.biosimulators.combine.exec_sed_task(
                task, variables, preprocessed_task=preprocessed_task)
        model.changes = []
        results2, _ = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables, preprocessed_task=preprocessed_task)
        for variable_id in variable_ids:
            numpy.testing.assert_allclose(
                results2[variable_id],
                results[variable_id][0:sim.number_of_points + 1])

        model.changes = []
        for variable_id in variable_ids:
            model.changes.append(
                ModelAttributeChange(target='define K_1', new_value=0))
        preprocessed_task = smoldyn.biosimulators.combine.preprocess_sed_task(
            task, variables)
        model.changes = []
        results2, _ = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables, preprocessed_task=preprocessed_task)
        with self.assertRaises(AssertionError):
            for variable_id in variable_ids:
                numpy.testing.assert_allclose(
                    results2[variable_id],
                    results[variable_id][0:sim.number_of_points + 1])
示例#13
0
    def test_exec_sed_task(self):
        task = Task(
            id='task',
            model=Model(
                id='model',
                source=os.path.join(self.EXAMPLES_DIRNAME, 'S1_intro',
                                    'bounce1.txt'),
                language=ModelLanguage.Smoldyn.value,
            ),
            simulation=UniformTimeCourseSimulation(
                initial_time=0.,
                output_start_time=0.1,
                output_end_time=0.2,
                number_of_points=10,
                algorithm=Algorithm(kisao_id='KISAO_0000057',
                                    changes=[
                                        AlgorithmParameterChange(
                                            kisao_id='KISAO_0000488',
                                            new_value='10'),
                                    ])),
        )

        variables = [
            Variable(id='time', symbol=Symbol.time.value, task=task),
            Variable(id='red', target='molcount red', task=task),
            Variable(id='green', target='molcount green', task=task),
        ]

        results, log = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables)

        self.assertEqual(set(results.keys()), set(['time', 'red', 'green']))
        numpy.testing.assert_allclose(results['time'],
                                      numpy.linspace(0.1, 0.2, 11))
        for result in results.values():
            self.assertEqual(result.shape, (11, ))
            self.assertFalse(numpy.any(numpy.isnan(result)))

        self.assertEqual(log.algorithm, 'KISAO_0000057')
        self.assertEqual(
            log.simulator_details, {
                'class': 'smoldyn.Simulation',
                'instanceAttributes': {
                    'setRandomSeed': 10
                },
                'method': 'run',
                'methodArguments': {
                    'start': 0.,
                    'stop': 0.2,
                    'dt': 0.01,
                },
            })

        task.simulation.algorithm.changes.append(
            AlgorithmParameterChange('KISAO_0000254', '5'))
        results, log = smoldyn.biosimulators.combine.exec_sed_task(
            task, variables)
        self.assertEqual(
            log.simulator_details, {
                'class': 'smoldyn.Simulation',
                'instanceAttributes': {
                    'setRandomSeed': 10
                },
                'method': 'run',
                'methodArguments': {
                    'start': 0.,
                    'stop': 0.2,
                    'dt': 0.01,
                    'accuracy': 5.,
                },
            })

        task.simulation.algorithm.kisao_id = 'KISAO_0000437'
        with self.assertRaises(NotImplementedError):
            smoldyn.biosimulators.combine.exec_sed_task(task, variables)
示例#14
0
def export_sed_doc(sed_doc_specs):
    """ Export the specifications of SED-ML document to SED-ML

    Args:
        sed_doc_specs (``SedDocument``)

    Returns:
        :obj:`SedDocument`
    """
    sed_doc = SedDocument(
        level=sed_doc_specs['level'],
        version=sed_doc_specs['version'],
    )

    # add styles to SED-ML document
    style_id_map = {}
    for style_spec in sed_doc_specs['styles']:
        style = Style(
            id=style_spec.get('id'),
            name=style_spec.get('name', None),
        )
        sed_doc.styles.append(style)
        style_id_map[style.id] = style

        if style_spec.get('line', None) is not None:
            style.line = LineStyle(
                type=style_spec['line'].get('type', None),
                color=style_spec['line'].get('color', None),
                thickness=style_spec['line'].get('thickness', None),
            )
            if style_spec['line'].get('type', None) is not None:
                style.line.type = LineStyleType[style_spec['line']['type']]
            if style_spec['line'].get('color', None) is not None:
                style.line.color = Color(style_spec['line']['color'])

        if style_spec.get('marker', None) is not None:
            style.marker = MarkerStyle(
                type=style_spec['marker'].get('type', None),
                size=style_spec['marker'].get('size', None),
                line_color=style_spec['marker'].get('lineColor', None),
                line_thickness=style_spec['marker'].get('lineThickness', None),
                fill_color=style_spec['marker'].get('fillColor', None),
            )
            if style_spec['marker'].get('type', None) is not None:
                style.marker.type = MarkerStyleType[style_spec['marker']
                                                    ['type']]
            if style_spec['marker'].get('lineColor', None) is not None:
                style.marker.line_color = Color(
                    style_spec['marker']['lineColor'])
            if style_spec['marker'].get('fillColor', None) is not None:
                style.marker.fill_color = Color(
                    style_spec['marker']['fillColor'])

        if style_spec.get('fill', None) is not None:
            style.fill = FillStyle(color=style_spec['fill'].get('color',
                                                                None), )
            if style_spec['fill'].get('color', None) is not None:
                style.fill.color = Color(style_spec['fill']['color'])

    for style_spec, style in zip(sed_doc_specs['styles'], sed_doc.styles):
        if style_spec.get('base', None) is not None:
            style.base = style_id_map.get(style_spec['base'], None)
            if style.base is None:
                raise BadRequestException(
                    title='Base style `{}` for style `{}` does not exist'.
                    format(style_spec['base'], style.id),
                    instance=ValueError('Style does not exist'),
                )

    # add models to SED-ML document
    model_id_map = {}
    for model_spec in sed_doc_specs['models']:
        model = Model(
            id=model_spec.get('id'),
            name=model_spec.get('name', None),
            language=model_spec.get('language'),
            source=model_spec.get('source'),
        )
        sed_doc.models.append(model)
        model_id_map[model.id] = model

        for change_spec in model_spec['changes']:
            if change_spec['_type'] == 'SedModelAttributeChange':
                change = ModelAttributeChange(
                    new_value=change_spec.get('newValue'), )

            elif change_spec['_type'] == 'SedAddElementModelChange':
                change = AddElementModelChange(
                    new_elements=change_spec.get('newElements'), )

            elif change_spec['_type'] == 'SedReplaceElementModelChange':
                change = ReplaceElementModelChange(
                    new_elements=change_spec.get('newElements'), )

            elif change_spec['_type'] == 'SedRemoveElementModelChange':
                change = RemoveElementModelChange()

            elif change_spec['_type'] == 'SedComputeModelChange':
                change = ComputeModelChange(
                    parameters=[],
                    variables=[],
                    math=change_spec.get('math'),
                )
                for parameter_spec in change_spec.get('parameters', []):
                    change.parameters.append(
                        Parameter(
                            id=parameter_spec.get('id'),
                            name=parameter_spec.get('name', None),
                            value=parameter_spec.get('value'),
                        ))
                for variable_spec in change_spec.get('variables', []):
                    change.variables.append(
                        Variable(
                            id=variable_spec.get('id'),
                            name=variable_spec.get('name', None),
                            model=variable_spec.get('model', None),
                            target=variable_spec.get('target',
                                                     {}).get('value', None),
                            target_namespaces={
                                namespace['prefix']: namespace['uri']
                                for namespace in variable_spec.get(
                                    'target', {}).get('namespaces', [])
                            },
                            symbol=variable_spec.get('symbol', None),
                            task=variable_spec.get('task', None),
                        ))

            else:
                raise BadRequestException(
                    title='Changes of type `{}` are not supported'.format(
                        change_spec['_type']),
                    instance=NotImplementedError('Invalid change'))

            change.target = change_spec.get('target').get('value')
            for ns in change_spec.get('target').get('namespaces', []):
                change.target_namespaces[ns.get('prefix', None)] = ns['uri']

            model.changes.append(change)

    # add simulations to SED-ML document
    simulation_id_map = {}
    for sim_spec in sed_doc_specs['simulations']:
        if sim_spec['_type'] == 'SedOneStepSimulation':
            sim = OneStepSimulation(
                id=sim_spec.get('id'),
                name=sim_spec.get('name', None),
                step=sim_spec.get('step'),
            )
        elif sim_spec['_type'] == 'SedSteadyStateSimulation':
            sim = SteadyStateSimulation(
                id=sim_spec.get('id'),
                name=sim_spec.get('name', None),
            )
        elif sim_spec['_type'] == 'SedUniformTimeCourseSimulation':
            sim = UniformTimeCourseSimulation(
                id=sim_spec.get('id'),
                name=sim_spec.get('name', None),
                initial_time=sim_spec.get('initialTime'),
                output_start_time=sim_spec.get('outputStartTime'),
                output_end_time=sim_spec.get('outputEndTime'),
                number_of_steps=sim_spec.get('numberOfSteps'),
            )
        else:
            raise BadRequestException(
                title='Simulations of type `{}` are not supported'.format(
                    sim_spec['_type']),
                instance=NotImplementedError('Invalid simulation')
            )  # pragma: no cover: unreachable due to schema validation

        alg_spec = sim_spec.get('algorithm')
        sim.algorithm = Algorithm(kisao_id=alg_spec.get('kisaoId'))
        for change_spec in alg_spec.get('changes'):
            sim.algorithm.changes.append(
                AlgorithmParameterChange(
                    kisao_id=change_spec.get('kisaoId'),
                    new_value=change_spec.get('newValue'),
                ))

        sed_doc.simulations.append(sim)
        simulation_id_map[sim.id] = sim

    # add tasks to SED-ML document
    task_id_map = {}
    for task_spec in sed_doc_specs['tasks']:
        if task_spec['_type'] == 'SedTask':
            model_id = task_spec.get('model')
            sim_id = task_spec.get('simulation')
            model = model_id_map.get(model_id, None)
            sim = simulation_id_map.get(sim_id, None)

            if not model:
                raise BadRequestException(
                    title='Model `{}` for task `{}` does not exist'.format(
                        model_id, task_spec.get('id')),
                    instance=ValueError('Model does not exist'),
                )
            if not sim:
                raise BadRequestException(
                    title='Simulation `{}` for task `{}` does not exist'.
                    format(sim_id, task_spec.get('id')),
                    instance=ValueError('Simulation does not exist'),
                )

            task = Task(
                id=task_spec.get('id'),
                name=task_spec.get('name', None),
                model=model,
                simulation=sim,
            )
        else:
            # TODO: support repeated tasks
            raise BadRequestException(
                title='Tasks of type `{}` are not supported'.format(
                    task_spec['_type']),
                instance=NotImplementedError('Invalid task')
            )  # pragma: no cover: unreachable due to schema validation

        sed_doc.tasks.append(task)
        task_id_map[task.id] = task

    # add data generators to SED-ML document
    data_gen_id_map = {}
    for data_gen_spec in sed_doc_specs['dataGenerators']:
        data_gen = DataGenerator(
            id=data_gen_spec.get('id'),
            name=data_gen_spec.get('name', None),
            math=data_gen_spec.get('math'),
        )

        for var_spec in data_gen_spec['variables']:
            task_id = var_spec.get('task')
            task = task_id_map.get(task_id, None)

            if not task:
                raise BadRequestException(
                    title='Task `{}` for variable `{}` does not exist'.format(
                        task_id, var_spec.get('id')),
                    instance=ValueError('Task does not exist'),
                )

            var = Variable(
                id=var_spec.get('id'),
                name=var_spec.get('name', None),
                task=task,
                symbol=var_spec.get('symbol', None),
            )

            target_spec = var_spec.get('target', None)
            if target_spec:
                var.target = target_spec['value']
                for ns in target_spec.get('namespaces', []):
                    var.target_namespaces[ns.get('prefix', None)] = ns['uri']

            data_gen.variables.append(var)

        sed_doc.data_generators.append(data_gen)
        data_gen_id_map[data_gen.id] = data_gen

    # add outputs to SED-ML document
    for output_spec in sed_doc_specs['outputs']:
        if output_spec['_type'] == 'SedReport':
            output = Report(
                id=output_spec.get('id'),
                name=output_spec.get('name', None),
            )
            for data_set_spec in output_spec['dataSets']:
                data_gen_id = data_set_spec['dataGenerator']
                data_gen = data_gen_id_map.get(data_gen_id, None)

                if not data_gen:
                    raise BadRequestException(
                        title=
                        'Data generator `{}` for output `{}` does not exist'.
                        format(data_gen_id, output_spec.get('id')),
                        instance=ValueError('Data generator does not exist'),
                    )

                data_set = DataSet(
                    id=data_set_spec.get('id'),
                    name=data_set_spec.get('name', None),
                    label=data_set_spec.get('label', None),
                    data_generator=data_gen,
                )
                output.data_sets.append(data_set)

        elif output_spec['_type'] == 'SedPlot2D':
            output = Plot2D(
                id=output_spec.get('id'),
                name=output_spec.get('name', None),
            )
            for curve_spec in output_spec['curves']:
                x_data_gen_id = curve_spec['xDataGenerator']
                y_data_gen_id = curve_spec['yDataGenerator']
                style_id = curve_spec.get('style', None)
                x_data_gen = data_gen_id_map.get(x_data_gen_id, None)
                y_data_gen = data_gen_id_map.get(y_data_gen_id, None)
                style = style_id_map.get(style_id, None)

                if not x_data_gen:
                    raise BadRequestException(
                        title=
                        'X data generator `{}` for curve `{}` does not exist'.
                        format(x_data_gen_id, output_spec.get('id')),
                        instance=ValueError('Data generator does not exist'),
                    )
                if not y_data_gen:
                    raise BadRequestException(
                        title=
                        'Y data generator `{}` for curve `{}` does not exist'.
                        format(y_data_gen_id, output_spec.get('id')),
                        instance=ValueError('Data generator does not exist'),
                    )
                if style_id is not None and style is None:
                    raise BadRequestException(
                        title='Style `{}` for curve `{}` does not exist'.
                        format(style_id, output_spec.get('id')),
                        instance=ValueError('Style does not exist'),
                    )

                curve = Curve(
                    id=curve_spec.get('id'),
                    name=curve_spec.get('name', None),
                    x_data_generator=x_data_gen,
                    y_data_generator=y_data_gen,
                    x_scale=AxisScale[output_spec['xScale']],
                    y_scale=AxisScale[output_spec['yScale']],
                    style=style,
                )
                output.curves.append(curve)

        elif output_spec['_type'] == 'SedPlot3D':
            output = Plot3D(
                id=output_spec.get('id'),
                name=output_spec.get('name', None),
            )
            for surface_spec in output_spec['surfaces']:
                x_data_gen_id = surface_spec['xDataGenerator']
                y_data_gen_id = surface_spec['yDataGenerator']
                z_data_gen_id = surface_spec['zDataGenerator']
                style_id = surface_spec.get('style', None)
                x_data_gen = data_gen_id_map.get(x_data_gen_id, None)
                y_data_gen = data_gen_id_map.get(y_data_gen_id, None)
                z_data_gen = data_gen_id_map.get(z_data_gen_id, None)
                style = style_id_map.get(style_id, None)

                if not x_data_gen:
                    raise BadRequestException(
                        title=
                        'X data generator `{}` for surface `{}` does not exist'
                        .format(x_data_gen_id, output_spec.get('id')),
                        instance=ValueError('Data generator does not exist'),
                    )
                if not y_data_gen:
                    raise BadRequestException(
                        title=
                        'Y data generator `{}` for surface `{}` does not exist'
                        .format(y_data_gen_id, output_spec.get('id')),
                        instance=ValueError('Data generator does not exist'),
                    )
                if not z_data_gen:
                    raise BadRequestException(
                        title=
                        'X data generator `{}` for surface `{}` does not exist'
                        .format(z_data_gen_id, output_spec.get('id')),
                        instance=ValueError('Data generator does not exist'),
                    )
                if style_id is not None and style is None:
                    raise BadRequestException(
                        title='Style `{}` for surface `{}` does not exist'.
                        format(style_id, output_spec.get('id')),
                        instance=ValueError('Style does not exist'),
                    )

                surface = Surface(
                    id=surface_spec.get('id'),
                    name=surface_spec.get('name', None),
                    x_data_generator=x_data_gen,
                    y_data_generator=y_data_gen,
                    z_data_generator=z_data_gen,
                    x_scale=AxisScale[output_spec['xScale']],
                    y_scale=AxisScale[output_spec['yScale']],
                    z_scale=AxisScale[output_spec['zScale']],
                    style=style,
                )
                output.surfaces.append(surface)

        else:
            raise BadRequestException(
                title='Outputs of type `{}` are not supported'.format(
                    output_spec['_type']),
                instance=NotImplementedError('Invalid output')
            )  # pragma: no cover: unreachable due to schema validation

        sed_doc.outputs.append(output)

    # deserialize references
    model_map = {}
    for model in sed_doc.models:
        model_map[model.id] = model

    task_map = {}
    for task in sed_doc.tasks:
        task_map[task.id] = task

    for model in sed_doc.models:
        for change in model.changes:
            if isinstance(change, ComputeModelChange):
                for variable in change.variables:
                    if variable.model:
                        variable.model = model_map[variable.model]
                    if variable.task:
                        variable.task = task_map[variable.task]

    return sed_doc
    def test_get_parameters_variables_for_simulation(self):
        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.FIXTURE_FILENAME, None, UniformTimeCourseSimulation, None)

        self.assertTrue(params[0].is_equal(
            ModelAttributeChange(
                id='difc_red',
                name='Diffusion coefficient of species "red"',
                target='difc red',
                new_value='3',
            )))
        self.assertTrue(params[1].is_equal(
            ModelAttributeChange(
                id='difc_green',
                name='Diffusion coefficient of species "green"',
                target='difc green',
                new_value='1',
            )))

        self.assertEqual(len(sims), 1)

        sim = sims[0]
        self.assertEqual(sim.initial_time, 0.0)
        self.assertEqual(sim.output_start_time, 0.0)
        self.assertEqual(sim.output_end_time, 100.0)
        self.assertEqual(sim.number_of_steps, 10000)
        self.assertEqual(sim.algorithm.kisao_id, 'KISAO_0000057')

        self.assertTrue(vars[0].is_equal(
            Variable(
                id='time',
                name='Time',
                symbol=Symbol.time.value,
            )))
        self.assertTrue(vars[1].is_equal(
            Variable(
                id='count_species_red',
                name='Count of species "red"',
                target='molcount red',
            )))
        self.assertTrue(vars[2].is_equal(
            Variable(
                id='count_species_green',
                name='Count of species "green"',
                target='molcount green',
            )))

        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.COMP_FIXTURE_FILENAME, None, UniformTimeCourseSimulation,
            None)

        self.assertTrue(vars[0].is_equal(
            Variable(
                id='time',
                name='Time',
                symbol=Symbol.time.value,
            )))
        self.assertTrue(vars[1].is_equal(
            Variable(
                id='count_species_red',
                name='Count of species "red"',
                target='molcount red',
            )))
        self.assertTrue(vars[2].is_equal(
            Variable(
                id='count_species_red_compartment_middle',
                name='Count of species "red" in compartment "middle"',
                target='molcountincmpt red middle',
            )))
        self.assertTrue(vars[3].is_equal(
            Variable(
                id='count_species_red_surface_walls',
                name='Count of species "red" in surface "walls"',
                target='molcountonsurf red walls',
            )))

        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.COMP_FIXTURE_FILENAME_2,
            None,
            UniformTimeCourseSimulation,
            None,
            native_data_types=True)

        self.assertTrue(params[2].is_equal(
            ModelAttributeChange(
                id='red',
                name='Membrane diffusion coefficient of species "red"',
                target='difm red',
                new_value=[1., 0., 0., 2.],
            )))
        self.assertTrue(params[3].is_equal(
            ModelAttributeChange(
                id='green',
                name='Membrane diffusion coefficient of species "green"',
                target='difm green',
                new_value=[1., 0., 0., 2.],
            )))
示例#16
0
    def _build_combine_archive(self):
        task = Task(
            id='task',
            model=Model(
                id='model',
                source='bounce1.txt',
                language=ModelLanguage.Smoldyn.value,
            ),
            simulation=UniformTimeCourseSimulation(
                id='sim',
                initial_time=0.,
                output_start_time=0.1,
                output_end_time=0.2,
                number_of_points=10,
                algorithm=Algorithm(kisao_id='KISAO_0000057',
                                    changes=[
                                        AlgorithmParameterChange(
                                            kisao_id='KISAO_0000488',
                                            new_value='10'),
                                    ])),
        )

        variables = [
            Variable(id='time', symbol=Symbol.time.value, task=task),
            Variable(id='red', target='molcount red', task=task),
            Variable(id='green', target='molcount green', task=task),
        ]

        doc = SedDocument(
            models=[task.model],
            simulations=[task.simulation],
            tasks=[task],
            data_generators=[
                DataGenerator(
                    id='data_gen_time',
                    variables=[
                        Variable(id='var_time',
                                 symbol=Symbol.time.value,
                                 task=task)
                    ],
                    math='var_time',
                ),
                DataGenerator(
                    id='data_gen_red',
                    variables=[
                        Variable(id='var_red',
                                 target='molcount red',
                                 task=task)
                    ],
                    math='var_red',
                ),
                DataGenerator(
                    id='data_gen_green',
                    variables=[
                        Variable(id='var_green',
                                 target='molcount green',
                                 task=task)
                    ],
                    math='var_green',
                ),
            ],
        )
        doc.outputs.append(
            Report(id='report',
                   data_sets=[
                       DataSet(id='data_set_time',
                               label='time',
                               data_generator=doc.data_generators[0]),
                       DataSet(id='data_set_red',
                               label='red',
                               data_generator=doc.data_generators[1]),
                       DataSet(id='data_set_green',
                               label='green',
                               data_generator=doc.data_generators[2]),
                   ]))

        archive_dirname = os.path.join(self.dirname, 'archive')
        os.makedirs(archive_dirname)
        shutil.copyfile(
            os.path.join(self.EXAMPLES_DIRNAME, 'S1_intro', 'bounce1.txt'),
            os.path.join(archive_dirname, 'bounce1.txt'))
        sim_filename = os.path.join(archive_dirname, 'sim_1.sedml')
        SedmlSimulationWriter().run(doc, sim_filename)

        archive = CombineArchive(contents=[
            CombineArchiveContent('bounce1.txt',
                                  CombineArchiveContentFormat.Smoldyn.value),
            CombineArchiveContent('sim_1.sedml',
                                  CombineArchiveContentFormat.SED_ML.value),
        ], )
        archive_filename = os.path.join(self.dirname, 'archive.omex')
        CombineArchiveWriter().run(archive, archive_dirname, archive_filename)

        return doc, archive_filename
示例#17
0
    def test_get_parameters_variables_for_simulation(self):
        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(self.FIXTURE_FILENAME, None, OneStepSimulation, None)
        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.FIXTURE_FILENAME, None, UniformTimeCourseSimulation, None)

        self.assertTrue(params[0].is_equal(ModelAttributeChange(
            id='value_parameter_k_1',
            name='Value of parameter "k_1"',
            target='parameters.k_1.value',
            new_value='0.0',
        )))
        self.assertTrue(params[7].is_equal(ModelAttributeChange(
            id='value_parameter_fa',
            name='Value of parameter "fa"',
            target='parameters.fa.value',
            new_value='1E-5',
        )))
        self.assertTrue(params[9].is_equal(ModelAttributeChange(
            id='initial_amount_species_GeneA_00__',
            name='Initial amount of species "GeneA_00()"',
            target='species.GeneA_00().initialCount',
            new_value='1',
        )))
        self.assertTrue(params[17].is_equal(ModelAttributeChange(
            id='expression_function_gfunc',
            name='Expression of function "gfunc()"',
            target='functions.gfunc.expression',
            new_value='(0.5*(Atot^2))/(10+(Atot^2))',
        )))

        self.assertEqual(len(sims), 5)
        self.assertIsInstance(sims[0], UniformTimeCourseSimulation)
        expected_sim = UniformTimeCourseSimulation(
            id='simulation_0',
            initial_time=0.,
            output_start_time=0.,
            output_end_time=1000000.,
            number_of_steps=1000,
            algorithm=Algorithm(
                kisao_id='KISAO_0000029',
                changes=[
                    AlgorithmParameterChange(
                        kisao_id='KISAO_0000488',
                        new_value='2',
                    )
                ]
            )
        )
        self.assertTrue(sims[0].is_equal(expected_sim))

        self.assertTrue(vars[0].is_equal(Variable(
            id='time',
            name='Time',
            symbol=Symbol.time.value,
        )))
        self.assertTrue(vars[1].is_equal(Variable(
            id='amount_molecule_A__',
            name='Dynamics of molecule "A()"',
            target='molecules.A().count',
        )))
        self.assertTrue(vars[9].is_equal(Variable(
            id='amount_species_GeneA_00__',
            name='Dynamics of species "GeneA_00()"',
            target='species.GeneA_00().count',
        )))
        self.assertEqual(len(vars), 17)
示例#18
0
    def test_get_parameters_variables_for_simulation_native_data_types(self):
        params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
            self.FIXTURE_FILENAME, None, UniformTimeCourseSimulation, None,
            native_ids=True, native_data_types=True)

        self.assertTrue(params[0].is_equal(ModelAttributeChange(
            id='k_1',
            name=None,
            target='parameters.k_1.value',
            new_value=0.0,
        )))
        self.assertTrue(params[7].is_equal(ModelAttributeChange(
            id='fa',
            name=None,
            target='parameters.fa.value',
            new_value=1E-5,
        )))
        self.assertTrue(params[9].is_equal(ModelAttributeChange(
            id='GeneA_00()',
            name=None,
            target='species.GeneA_00().initialCount',
            new_value=1,
        )))
        self.assertTrue(params[17].is_equal(ModelAttributeChange(
            id='gfunc',
            name=None,
            target='functions.gfunc.expression',
            new_value='(0.5*(Atot^2))/(10+(Atot^2))',
        )))

        self.assertEqual(len(sims), 5)
        self.assertIsInstance(sims[0], UniformTimeCourseSimulation)
        expected_sim = UniformTimeCourseSimulation(
            id='simulation_0',
            initial_time=0.,
            output_start_time=0.,
            output_end_time=1000000.,
            number_of_steps=1000,
            algorithm=Algorithm(
                kisao_id='KISAO_0000029',
                changes=[
                    AlgorithmParameterChange(
                        kisao_id='KISAO_0000488',
                        new_value=2,
                    )
                ]
            )
        )

        self.assertTrue(sims[0].is_equal(expected_sim))

        self.assertTrue(vars[0].is_equal(Variable(
            id=None,
            name=None,
            symbol=Symbol.time.value,
        )))
        self.assertTrue(vars[1].is_equal(Variable(
            id='A()',
            name=None,
            target='molecules.A().count',
        )))
        self.assertTrue(vars[9].is_equal(Variable(
            id='GeneA_00()',
            name=None,
            target='species.GeneA_00().count',
        )))
        self.assertEqual(len(vars), 17)
示例#19
0
    def test_get_variable_results(self):
        filename = os.path.join(self.EXAMPLES_DIRNAME, 'S1_intro',
                                'bounce1.txt')
        config = smoldyn.biosimulators.combine.read_smoldyn_simulation_configuration(
            filename)
        smoldyn.biosimulators.combine.disable_smoldyn_graphics_in_simulation_configuration(
            config)

        filename2 = os.path.join(self.dirname, 'config.txt')
        smoldyn.biosimulators.combine.write_smoldyn_simulation_configuration(
            config, filename2)

        sim = smoldyn.biosimulators.combine.init_smoldyn_simulation_from_configuration_file(
            filename2)
        sim.setGraphics('none')

        vars = [
            Variable(id='time', symbol=Symbol.time.value),
            Variable(id='red', target='molcount red'),
            Variable(id='green', target='molcountspecies green'),
            Variable(id='greenBox', target='molcountinbox green 0 10'),
            Variable(id='greenHist', target='molcountspace green x 0 100 20')
        ]
        variable_output_cmd_map = smoldyn.biosimulators.combine.validate_variables(
            vars)
        smoldyn_output_files = smoldyn.biosimulators.combine.add_smoldyn_output_files_for_sed_variables(
            self.dirname, vars, variable_output_cmd_map, sim)

        sim.run(start=0.,
                stop=0.2,
                dt=0.01,
                overwrite=True,
                display=False,
                quit_at_end=False)

        results = smoldyn.biosimulators.combine.get_variable_results(
            10, vars, variable_output_cmd_map, smoldyn_output_files)
        self.assertEqual(
            set(results.keys()),
            set(['time', 'red', 'green', 'greenBox', 'greenHist']))
        numpy.testing.assert_allclose(results['time'],
                                      numpy.linspace(0.1, 0.2, 11))

        self.assertEqual(results['red'].shape, (11, ))
        self.assertFalse(numpy.any(numpy.isnan(results['red'])))

        self.assertEqual(results['green'].shape, (11, ))
        self.assertFalse(numpy.any(numpy.isnan(results['green'])))

        self.assertEqual(results['greenBox'].shape, (11, ))
        self.assertFalse(numpy.any(numpy.isnan(results['greenBox'])))

        self.assertEqual(results['greenHist'].shape, (11, 20))
        self.assertFalse(numpy.any(numpy.isnan(results['greenHist'])))

        vars = [
            Variable(id='time', symbol='undefined'),
        ]
        with self.assertRaises(ValueError):
            variable_output_cmd_map = smoldyn.biosimulators.combine.validate_variables(
                vars)
            smoldyn.biosimulators.combine.get_variable_results(
                10, vars, variable_output_cmd_map, smoldyn_output_files)

        vars = [
            Variable(id='red', target='molcount blue'),
        ]
        variable_output_cmd_map = smoldyn.biosimulators.combine.validate_variables(
            vars)
        with self.assertRaises(ValueError):
            smoldyn.biosimulators.combine.get_variable_results(
                10, vars, variable_output_cmd_map, smoldyn_output_files)

        vars = [
            Variable(id='red', target='undefined blue'),
        ]
        with self.assertRaises(NotImplementedError):
            variable_output_cmd_map = smoldyn.biosimulators.combine.validate_variables(
                vars)
            smoldyn.biosimulators.combine.get_variable_results(
                10, vars, variable_output_cmd_map, smoldyn_output_files)
    def test_get_parameters_variables_for_simulation(self):
        # BNGL
        filename = os.path.join(self.FIXTURES_DIRNAME, 'bngl', 'valid.bngl')
        params, sims, vars, plots = model_utils.get_parameters_variables_outputs_for_simulation(
            filename, ModelLanguage.BNGL, UniformTimeCourseSimulation, None)
        self.assertTrue(vars[0].is_equal(
            Variable(
                id='time',
                name='Time',
                symbol=Symbol.time.value,
            )))

        # CellML
        filename = os.path.join(self.FIXTURES_DIRNAME, 'cellml',
                                'albrecht_colegrove_friel_2002.xml')
        params, sims, vars, plots = model_utils.get_parameters_variables_outputs_for_simulation(
            filename, ModelLanguage.CellML, UniformTimeCourseSimulation, None)
        self.assertTrue(vars[0].is_equal(
            Variable(
                id='value_component_environment_variable_time',
                name='Value of variable "time" of component "environment"',
                target=("/cellml:model"
                        "/cellml:component[@name='environment']"
                        "/cellml:variable[@name='time']"),
                target_namespaces={
                    'cellml': 'http://www.cellml.org/cellml/1.0#'
                },
            )))

        # RBA
        filename = os.path.join(self.FIXTURES_DIRNAME, 'rba',
                                'Escherichia-coli-K12-WT.zip')
        params, sims, vars, plots = model_utils.get_parameters_variables_outputs_for_simulation(
            filename, ModelLanguage.RBA, SteadyStateSimulation, None)
        self.assertTrue(vars[0].is_equal(
            Variable(
                id='objective',
                name='Value of objective',
                target='objective',
            )))

        # SBML
        filename = os.path.join(self.FIXTURES_DIRNAME, 'BIOMD0000000297.xml')
        params, sims, vars, plots = model_utils.get_parameters_variables_outputs_for_simulation(
            filename, ModelLanguage.SBML, UniformTimeCourseSimulation,
            'KISAO_0000019')
        self.assertTrue(vars[0].is_equal(
            Variable(
                id='time',
                name='Time',
                symbol=Symbol.time.value,
            )))

        # Smoldyn
        filename = os.path.join(self.FIXTURES_DIRNAME, 'smoldyn',
                                'bounce1.txt')
        params, sims, vars, plots = model_utils.get_parameters_variables_outputs_for_simulation(
            filename, ModelLanguage.Smoldyn, UniformTimeCourseSimulation, None)
        self.assertTrue(params[0].is_equal(
            ModelAttributeChange(
                id='difc_red',
                name='Diffusion coefficient of species "red"',
                target='difc red',
                new_value='3',
            )))

        # XPP
        filename = os.path.join(self.FIXTURES_DIRNAME, 'xpp',
                                'wilson-cowan.ode')
        params, sims, vars, plots = model_utils.get_parameters_variables_outputs_for_simulation(
            filename, ModelLanguage.XPP, UniformTimeCourseSimulation, None)
        self.assertTrue(params[0].is_equal(
            ModelAttributeChange(
                id='parameter_aee',
                name='Value of parameter "aee"',
                target='aee',
                new_value='10.0',
            )))