示例#1
0
    def test_hdf5_response(self):
        endpoint = '/run/run'
        archive_filename = os.path.join(
            self.FIXTURES_DIRNAME,
            'Caravagna-J-Theor-Biol-2010-tumor-suppressive-oscillations.omex')
        data = MultiDict([
            ('_type', 'SimulationRun'),
            ('archiveUrl', 'https://web.site/archive.omex'),
            ('simulator', 'copasi'),
        ])
        with app.app.app.test_client() as client:
            with open(archive_filename, 'rb') as archive_file:
                with mock.patch('requests.get',
                                return_value=mock.Mock(
                                    raise_for_status=lambda: None,
                                    content=archive_file.read())):
                    response = client.post(
                        endpoint,
                        data=data,
                        content_type="multipart/form-data",
                        headers={'Accept': 'application/x-hdf'})

        self.assertEqual(response.status_code, 200, response.json)
        h5_filename = os.path.join(self.tmp_dirname, 'reports.h5')
        with open(h5_filename, 'wb') as file:
            file.write(response.data)

        self.assertEqual([
            'BIOMD0000000912_sim.sedml/BIOMD0000000912_report',
            'BIOMD0000000912_sim.sedml/plot_1'
        ],
                         ReportReader().get_ids(self.tmp_dirname))

        archive_dirname = os.path.join(self.tmp_dirname, 'archive')
        CombineArchiveReader().run(archive_filename, archive_dirname)
        sed_doc = SedmlSimulationReader().run(
            os.path.join(archive_dirname, 'BIOMD0000000912_sim.sedml'))
        report = next(output for output in sed_doc.outputs
                      if output.id == 'BIOMD0000000912_report')

        data_set_results = ReportReader().run(
            report, self.tmp_dirname,
            'BIOMD0000000912_sim.sedml/BIOMD0000000912_report')

        self.assertEqual(
            set(data_set_results.keys()),
            set([
                'data_set_time',
                'data_set_T',
                'data_set_E',
                'data_set_I',
            ]),
        )

        numpy.testing.assert_allclose(data_set_results['data_set_time'],
                                      numpy.linspace(0., 1000., 5001))
        for values in data_set_results.values():
            self.assertEqual(values.shape, (5001, ))
            self.assertFalse(numpy.any(numpy.isnan(values)))
    def _assert_combine_archive_outputs(self, doc, out_dir):
        self.assertEqual(set(['reports.h5', 'reports.zip', 'sim_1.sedml']).difference(set(os.listdir(out_dir))), set())

        report = doc.outputs[0]

        # check HDF report
        report_results = ReportReader().run(report, out_dir, 'sim_1.sedml/report_1', format=report_data_model.ReportFormat.h5)

        self.assertEqual(sorted(report_results.keys()), sorted([d.id for d in doc.outputs[0].data_sets]))

        sim = doc.tasks[0].simulation
        self.assertEqual(len(report_results[report.data_sets[0].id]), sim.number_of_points + 1)
        numpy.testing.assert_allclose(
            report_results[report.data_sets[0].id],
            numpy.linspace(sim.output_start_time, sim.output_end_time, sim.number_of_points + 1),
        )

        for data_set_result in report_results.values():
            self.assertFalse(numpy.any(numpy.isnan(data_set_result)))

        # check CSV report
        report_results = ReportReader().run(report, out_dir, 'sim_1.sedml/report_1', format=report_data_model.ReportFormat.csv)

        self.assertEqual(sorted(report_results.keys()), sorted([d.id for d in doc.outputs[0].data_sets]))

        sim = doc.tasks[0].simulation
        self.assertEqual(len(report_results[report.data_sets[0].id]), sim.number_of_points + 1)
        numpy.testing.assert_allclose(
            report_results[report.data_sets[0].id],
            numpy.linspace(sim.output_start_time, sim.output_end_time, sim.number_of_points + 1),
        )

        for data_set_result in report_results.values():
            self.assertFalse(numpy.any(numpy.isnan(data_set_result)))
示例#3
0
    def _assert_combine_archive_outputs(self, doc, out_dir):
        results = ReportReader().run(doc.outputs[0],
                                     out_dir,
                                     os.path.join('sim_1.sedml',
                                                  doc.outputs[0].id),
                                     format=ReportFormat.h5)

        self.assertEqual(
            set(results.keys()),
            set(['data_set_time', 'data_set_red', 'data_set_green']))
        numpy.testing.assert_allclose(results['data_set_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)))
    def test_more_complex_archive(self):
        archive_filename = os.path.join(os.path.dirname(__file__), 'fixtures', 'BIOMD0000000297.edited.omex')

        config = get_config()
        config.REPORT_FORMATS = [report_data_model.ReportFormat.h5, report_data_model.ReportFormat.csv]
        config.VIZ_FORMATS = []
        config.BUNDLE_OUTPUTS = True
        config.KEEP_INDIVIDUAL_OUTPUTS = True

        _, log = core.exec_sedml_docs_in_combine_archive(archive_filename, self.dirname, config=config)
        if log.exception:
            raise log.exception

        self.assertEqual(set(['reports.zip', 'reports.h5', 'ex1', 'ex2']).difference(set(os.listdir(self.dirname))), set())
        self.assertEqual(set(os.listdir(os.path.join(self.dirname, 'ex1'))), set(['BIOMD0000000297.sedml']))
        self.assertEqual(set(os.listdir(os.path.join(self.dirname, 'ex2'))), set(['BIOMD0000000297.sedml']))
        self.assertEqual(set(os.listdir(os.path.join(self.dirname, 'ex1', 'BIOMD0000000297.sedml'))),
                         set(['two_species.csv', 'three_species.csv']))
        self.assertEqual(set(os.listdir(os.path.join(self.dirname, 'ex2', 'BIOMD0000000297.sedml'))),
                         set(['one_species.csv', 'four_species.csv']))

        archive = ArchiveReader().run(os.path.join(self.dirname, 'reports.zip'))

        self.assertEqual(
            sorted(file.archive_path for file in archive.files),
            sorted([
                'ex1/BIOMD0000000297.sedml/two_species.csv',
                'ex1/BIOMD0000000297.sedml/three_species.csv',
                'ex2/BIOMD0000000297.sedml/one_species.csv',
                'ex2/BIOMD0000000297.sedml/four_species.csv',
            ]),
        )

        report = sedml_data_model.Report(
            data_sets=[
                sedml_data_model.DataSet(id='data_set_time_two_species', label='time'),
                sedml_data_model.DataSet(id='data_set_Cln4', label='Cln4'),
                sedml_data_model.DataSet(id='data_set_Swe13', label='Swe13'),
            ]
        )

        report_results = ReportReader().run(report, self.dirname,
                                            'ex1/BIOMD0000000297.sedml/two_species',
                                            format=report_data_model.ReportFormat.h5)
        self.assertEqual(sorted(report_results.keys()), sorted(['data_set_time_two_species', 'data_set_Cln4', 'data_set_Swe13']))
        numpy.testing.assert_allclose(report_results['data_set_time_two_species'], numpy.linspace(0., 1., 10 + 1))
示例#5
0
    def assert_outputs_created(self, dirname):
        self.assertTrue(os.path.isfile(os.path.join(dirname, 'reports.h5')))

        report = sedml_data_model.Report(data_sets=[
            sedml_data_model.DataSet(id='time', label='time'),
        ])

        report_results = ReportReader().run(
            report,
            dirname,
            'simulation_1.sedml/simulation_1',
            format=report_data_model.ReportFormat.h5)

        self.assertEqual(len(report_results[report.data_sets[0].id]), 100 + 1)
        numpy.testing.assert_almost_equal(
            report_results[report.data_sets[0].id],
            numpy.linspace(0., 100., 100 + 1),
        )

        for data_set_result in report_results.values():
            self.assertFalse(numpy.any(numpy.isnan(data_set_result)))
示例#6
0
    def _assert_combine_archive_outputs(self, doc, out_dir):
        # read the report of the results of the execution of the COMBINE archive
        report = doc.outputs[0]
        report_results = ReportReader().run(
            report,
            out_dir,
            'simulation_1.sedml/simulation_1',
            format=report_data_model.ReportFormat.h5)

        # assert that the execution of the archive produced the expected results
        expected_report_results = report_data_model.DataSetResults({})
        for data_set in report.data_sets:
            numpy.testing.assert_almost_equal(
                report_results[data_set.id],
                expected_report_results[data_set.id])