Example #1
0
    def test_model_hash_accuracy(self):
        """
        Test the accuracy of the JSON hash.
        """

        for model in self.models:
            # Create two two instances of the 'model' type.
            model_1 = model()
            model_2 = model()

            # Assert that the hash of the anonymized models are the same.
            self.assertEqual(model_1.to_anon().get_json_hash(),
                             model_2.to_anon().get_json_hash())

            # Create a test class and change the variable insertion order.
            model_1 = model()
            model_1.var1 = "Hello"
            model_1.var2 = "world"
            model_1.var3 = ["Hello world!"]

            model_2 = model()
            model_2.var3 = ["Hello world!"]
            model_2.var2 = "world"
            model_2.var1 = "Hello"

            # Generate the first model's translation table.
            translation_table = model_1.get_translation_table()

            # Assert that the JSON of the anonymized models are still the same.
            self.assertEqual(model_1.to_anon().to_json(),
                             model_2.to_anon().to_json())

            # Assert that the hash of the anonymized models are still the same.
            self.assertEqual(model_1.to_anon().get_json_hash(),
                             model_2.to_anon().get_json_hash())

            # Assert that the translation table is the same.
            self.assertEqual(model_1.get_translation_table().to_json(),
                             model_2.get_translation_table().to_json())

            # Assert that model_1's JSON is equivalent to model_2 -> anon -> json -> object -> named -> json.
            self.assertEqual(
                model_1.to_json(),
                Model.from_json(
                    model_2.to_anon().to_json()).to_named().to_json())

            # Assert that model_2's anon JSON hash is equivalent to model_2 -> anon -> json -> object -> json hash.
            self.assertEqual(
                model_1.to_anon().get_json_hash(),
                Model.from_json(model_2.to_anon().to_json()).get_json_hash())
Example #2
0
    def test_named_to_anon_accuracy(self):
        for model in self.models:
            model_1 = model()

            # For each model, check to see if we can convert its table to and from json accurately.
            # For each model, generate its translation table and convert it to JSON.
            translation_table = model_1.get_translation_table()
            translation_table_json = translation_table.to_json()

            # Convert the JSON back into a TranslationTable object.
            translation_table_from_json = TranslationTable.from_json(
                translation_table_json)

            # Assert that the two tables are still identical.
            self.assertEqual(translation_table, translation_table_from_json)

            # Anonymize and convert model_1 to JSON.
            model_1 = model()
            model_1_json = model_1.to_anon().to_json()

            # Convert the JSON back into a Model object.
            model_2 = Model.from_json(model_1_json)

            # Assert that the anonymized model_1 and the new model_2 are identical.
            self.assertEquals(model_1.to_anon().to_json(), model_2.to_json())

            # Convert the new model_2 to named.
            model_2 = model_2.to_named()

            # Assert that model_1 and model_2 are still the same.
            self.assertEquals(model_1.to_json(), model_2.to_json())
Example #3
0
    def test_equality_of_named_models(self):
        """
        Test that a model can be converted to JSON and back.
        """

        for model in self.models:
            target = model()

            self.assertEqual(target, Model.from_json(target.to_json()))
Example #4
0
    def test_equality_of_anon_models(self):
        """
        Test that an anonymous model can be converted to JSON and back.
        """

        for model in self.models:
            target = model()
            anon_target = target.to_anon()

            self.assertEqual(anon_target,
                             Model.from_json(anon_target.to_json()))
Example #5
0
def start_job():
    request_obj = StartJobRequest.parse_raw(request.json)

    if delegate.job_complete(request_obj.job_id):
        return ErrorResponse(
            msg=f"The job with id '{request_obj.job_id} has already completed."
        ).json(), 400

    if delegate.job_exists(request_obj.job_id):
        return StartJobResponse(
            job_id=request_obj.job_id,
            msg="The job has already been started.",
            status=f"/v1/job/{request_obj.job_id}/status").json(), 202

    model = Model.from_json(request_obj.model)
    kwargs = json.loads(request_obj.kwargs)

    from gillespy2.solvers import SSACSolver
    kwargs["solver"] = SSACSolver

    if not "number_of_trajectories" in kwargs:
        delegate.start_job(request_obj.job_id, Model.run, model, **kwargs)

    else:
        trajectories = kwargs["number_of_trajectories"]
        del kwargs["number_of_trajectories"]

        # Hacky, but it works for now.
        keys = [
            f"{request_obj.job_id}/trajectory_{i}"
            for i in range(0, trajectories)
        ]
        delegate.client.scatter([model, kwargs])

        dependencies = delegate.client.map(Model.run, [model] * trajectories,
                                           **kwargs,
                                           key=keys)

        def test_job(*args, **kwargs):
            data = []

            for result in args:
                data = data + result.data

            from gillespy2.core import Results
            return Results(data)

        delegate.start_job(request_obj.job_id, test_job, *dependencies)

    return StartJobResponse(
        job_id=request_obj.job_id,
        msg="The job has been successfully started.",
        status=f"/v1/job/{request_obj.job_id}/status").json(), 202