Exemple #1
0
def _generate_observed_trajectories():
    timepoints = np.arange(0, 20, 0.5)

    observed_trajectories = [
        Trajectory(timepoints, [
            301., 290.1919552, 280.58701279, 272.03059275, 264.39179184,
            257.55906225, 251.43680665, 245.94267027, 241.0053685,
            236.56293031, 232.56126687, 228.95299703, 225.69647718,
            222.75499485, 220.09609439, 217.69101017, 215.51418738,
            213.54287512, 211.75677913, 210.13776407, 208.6695974,
            207.33772795, 206.12909392, 205.03195581, 204.03575051,
            203.13096347, 202.30901645, 201.5621687, 200.88342959,
            200.26648137, 199.70561061, 199.19564728, 198.73191049,
            198.3101601, 197.92655342, 197.57760651, 197.26015951,
            196.97134545, 196.70856234, 196.46944793
        ], Moment([1], symbol='y_0')),
        Trajectory(timepoints, [
            0., 20.10320788, 35.54689328, 47.51901615, 56.88242563,
            64.26983231, 70.14921364, 74.86943532, 78.69244584, 81.81623963,
            84.39139622, 86.53309942, 88.32994353, 89.85043353, 91.1478162,
            92.26369294, 93.23073689, 94.07474712, 94.81620873, 95.47148295,
            96.05371852, 96.57355199, 97.03964777, 97.45911583, 97.83783557,
            98.18070779, 98.49185119, 98.77475594, 99.03240415, 99.26736458,
            99.48186728, 99.67786273, 99.85706877, 100.02100803, 100.17103799,
            100.3083751, 100.43411443, 100.54924568, 100.65466632, 100.75119251
        ], Moment([2], symbol='yx1'))
    ]

    return observed_trajectories
Exemple #2
0
 def test_trajectory_with_sensitivities_serialisation(self):
     term = Moment([1, 0, 0], 'x')
     x = Trajectory([1, 2, 3], [3, 2, 1], SensitivityTerm(term, 'x'))
     y = Trajectory([1, 2, 3], [7, 8, 9], SensitivityTerm(term, 'y'))
     t = TrajectoryWithSensitivityData([1, 2, 3], [-1, -2, -3],
                                       term,
                                       sensitivity_data=[x, y])
     self._roundtrip(t)
Exemple #3
0
    def test_equality_treats_equal_things_as_equal(self):
        """
        Given two Trajectories that were equal, they should be comparable with ==.
        """

        t1 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'))
        t2 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'))

        self.assertEqual(t1, t2)
Exemple #4
0
    def test_single_traj_to_file(self):

        trajectory = Trajectory([1, 2, 3, 4, 5, 6], [3, 2, 1,5, 2, 4], Moment([1], symbol='description'))
        file = tempfile.mktemp(suffix=".csv")
        try:
            with open(file,"w") as out:
                trajectory.to_csv(out)
        finally:
            os.unlink(file)
Exemple #5
0
    def test_different_descriptions_make_trajectories_different(self):
        """
        Given two Trajectories that differ only by values, they should be treated as different
        """

        t1 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'))
        t2 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'))

        self.assertNotEqual(t1, t2)
Exemple #6
0
    def test_equality_treats_equal_things_as_equal(self):
        """
        Given two Trajectories that were equal, they should be comparable with ==.
        """
        t_sensitivity_1 = Trajectory([1,2,3], [3, 2, 1], Moment([1], symbol='description'))
        t_sensitivity_2 = Trajectory([1,2, 3], [-5, -9, -1], Moment([1], symbol='description'))

        t1 = TrajectoryWithSensitivityData([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'), [t_sensitivity_1, t_sensitivity_2])
        t2 = TrajectoryWithSensitivityData([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'), [t_sensitivity_1, t_sensitivity_2])

        self.assertEqual(t1, t2)
Exemple #7
0
    def test_traj_collection_to_file(self):

        tr2 = Trajectory([1, 2, 3, 4, 5, 6], [3, 2, 1, 5, 2, 4], Moment([1], symbol='y_1'))
        tr1 = Trajectory([1, 2, 3, 4, 5, 6], [3, 2, 1, 5, 2, 4], Moment([1], symbol='y_2'))
        tc = TrajectoryCollection([tr1,tr2])

        file = tempfile.mktemp(suffix=".csv")
        try:
            with open(file,"w") as out:
                tc.to_csv(out)
        finally:
            os.unlink(file)
Exemple #8
0
    def test_different_timepoints_make_trajectories_different(self):
        """
        Given two TrajectoriesWithSensitivityData that differ only by sensitivity data
        they should be reported as different
        """

        t_sensitivity_1 = Trajectory([1,2,3], [3, 2, 1], Moment([1], symbol='sensitivity1'))
        t_sensitivity_2 = Trajectory([1,2, 3], [-5, -9, -1], Moment([1], symbol='sensitivity2'))
        t_sensitivity_3 = Trajectory([1,2,3], [-5, -9, -100], Moment([1], symbol='sensitivity2'))

        t1 = TrajectoryWithSensitivityData([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'), [t_sensitivity_1, t_sensitivity_2])
        t2 = TrajectoryWithSensitivityData([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'), [t_sensitivity_1, t_sensitivity_3])

        self.assertNotEqual(t1, t2)
Exemple #9
0
    def _test_trajectory_wrapping_works_correctly(self):
        """
        Given a set of returned values from a solver, _results_to_trajectories should be able to
        wrap those results into :class:`~means.simulation.trajectory.Trajectory` objects nicely.
        """

        descriptions = [ODETermBase('x'), ODETermBase('y')]

        simulated_timepoints = [1,2,3]
        simulated_values = np.array([[-3, 1], [-2, 2], [-1, 3]])

        trajectory1, trajectory2 = _wrap_results_to_trajectories(simulated_timepoints, simulated_values, descriptions)

        correct_trajectory1 = Trajectory(simulated_timepoints, [-3, -2, -1], descriptions[0])
        correct_trajectory2 = Trajectory(simulated_timepoints, [1, 2, 3], descriptions[1])

        self.assertEqual(correct_trajectory1, trajectory1)
        self.assertEqual(correct_trajectory2, trajectory2)
Exemple #10
0
def _sample_inference():

    r = Inference(problem=_sample_problem(),
                  starting_parameters=[1, 2, 3, 4, 5, 6, 7],
                  starting_conditions=[1, 2, 3],
                  variable_parameters=['c_0', 'c_1'],
                  observed_trajectories=[
                      Trajectory([1, 2], [2, 3], Moment([1, 0, 0], 'x'))
                  ],
                  distance_function_type='gamma',
                  maxh=0.01)  # Some simulation kwargs
    return r
Exemple #11
0
    def _test_trajectory_with_sensitivity_wrapping_works_correctly(self):
        """
        Given a set of already pre-processed trajectories, results from sensitivity calculations,
        and the parameters of model, these should be wrapped nicely into TrajectoryWithSensitivityData objects
        """
        time_points = [1, 2, 3]

        t0 = Trajectory(time_points, [-1, 2, -3], ODETermBase('x'))
        t1 = Trajectory(time_points, [-1, -2, -3], ODETermBase('y'))

        parameters = sympy.symbols(['p', 'z'])

        raw_sensitivity_results = np.array([[[np.nan, np.nan], [1, 2], [3, 4]],
                                            [[np.nan, np.nan], [-1, -3], [-2, -4]]])

        actual_t1, actual_t2 = _add_sensitivity_data_to_trajectories([t0, t1], raw_sensitivity_results, parameters)

        sensitivity_data_1 = [Trajectory(time_points, [np.nan, 1, 3], SensitivityTerm(t0.description, parameters[0])),
                              Trajectory(time_points, [np.nan, 2, 4], SensitivityTerm(t1.description, parameters[1]))]
        expected_t1 = TrajectoryWithSensitivityData.from_trajectory(t0, sensitivity_data_1)

        sensitivity_data_2 = [Trajectory(time_points, [np.nan, -1, -2], SensitivityTerm(t0.description, parameters[0])),
                              Trajectory(time_points, [np.nan, -3, -4], SensitivityTerm(t1.description, parameters[1]))]
        expected_t2 = TrajectoryWithSensitivityData.from_trajectory(t0, sensitivity_data_2)

        self.assertEqual(actual_t1, expected_t1)
        self.assertEqual(actual_t2, expected_t2)
Exemple #12
0
        def check_initialisation(variable_parameters,
                                 expected_parameters_with_variability,
                                 expected_initial_conditions_with_variability,
                                 expected_constraints):
            p = Inference(
                self.dimer_problem, parameters, initial_conditions,
                variable_parameters,
                [Trajectory([1, 2, 3], [1, 2, 3], Moment([1], symbol='x'))])

            self.assertEquals(p.starting_parameters_with_variability,
                              expected_parameters_with_variability)
            self.assertEqual(p.starting_conditions_with_variability,
                             expected_initial_conditions_with_variability)
            self.assertEqual(p.constraints, expected_constraints)
Exemple #13
0
    def test_trajectory_collection_serialisation(self):

        t1 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1, 2, 3], 'x'))
        t2 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1, 0, 0], 'y'))
        tc = TrajectoryCollection([t1, t2])
        self._roundtrip(tc)
Exemple #14
0
 def test_trajectory_serialisation(self):
     t = Trajectory([1, 2, 3], [3, 2, 1], Moment([1, 2, 3], 'x'))
     self._roundtrip(t)
Exemple #15
0
    def test_moment_from_traj(self):
        dummy = StochasticProblem(MODEL_LOTKA_VOLTERRA)
        ssa = SSASimulation(dummy, 1)

        all_trajectories = [
            [
                Trajectory([0, 1, 2], [3, 4, 4], Moment([0,1],"a")),
                Trajectory([0, 1, 2], [7, 9, 10], Moment([1,0],"b"))
            ],
            [
                Trajectory([0, 1, 2], [1, 2, 5], Moment([0,1],"a")),
                Trajectory([0, 1, 2], [2, 3, 3], Moment([1,0],"b"))
            ],
            [
                Trajectory([0, 1, 2], [13,7,5], Moment([0,1],"a")),
                Trajectory([0, 1, 2], [22,3,1], Moment([1,0],"b"))
            ],
            [
                Trajectory([0, 1, 2], [9,21, 3], Moment([0,1],"a")),
                Trajectory([0, 1, 2], [8, 7, 4], Moment([1,0],"b"))
            ]
        ]

        mean_trajectories = [sum(trajs)/float(len(trajs)) for trajs in zip(*all_trajectories)]

        variance_a_result = ssa._compute_one_moment(all_trajectories, mean_trajectories,
                                                    Moment([2,0],sympy.Symbol("V_a")))
        covar_result= ssa._compute_one_moment(all_trajectories, mean_trajectories,
                                              Moment([1,1],sympy.Symbol("Cov_a_b")))

        variance_a_expected = Trajectory([0.0, 1.0, 2.0],[22.75, 55.25, 0.6875],
                                         Moment([2, 0], sympy.Symbol("V_a")))

        covar_expected = Trajectory([0.0, 1.0, 2.0], [31.875, 5.75, -1.125],
                                    Moment([1, 1], sympy.Symbol("Cov_a_b")))

        skew_a_result = ssa._compute_one_moment(all_trajectories, mean_trajectories,
                                                    Moment([3,0],sympy.Symbol("V_a")))

        skew_a_expected = Trajectory([0.0, 1.0, 2.0], [20.25,   396.0,  -0.28125],
                                     Moment([3, 0], symbol="V_a"))


        self.assertEqual(variance_a_result , variance_a_expected)
        self.assertEqual(covar_result, covar_expected)
        self.assertEqual(skew_a_expected, skew_a_result)