def test_plotting(self): """ Tests Solution for the ability to create separate plots for solutions. """ mock_model = Mock() mock_model.size = 2 mock_model.Vc = 1 mock_model.CL = 1 mock_model.Qps = [1] mock_model.Vps = [1] mock_protocol = Mock() mock_protocol.subcutaneous = False mock_protocol.dose_time_function.return_value = 1 solution = pk.Solution(model=mock_model, protocol=mock_protocol) sol_fig = solution.generate_plot() self.assertIsInstance(sol_fig, matplotlib.figure.Figure)
def test_intravenous(self): """ Tests Solution creation for intravenous protocol. """ mock_model = Mock() mock_model.size = 2 mock_model.Vc = 1 mock_model.CL = 1 mock_model.Qps = [1] mock_model.Vps = [1] mock_protocol = Mock() mock_protocol.subcutaneous = False mock_protocol.dose_time_function.return_value = 1 solution = pk.Solution(model=mock_model, protocol=mock_protocol) self.assertEqual(solution.sol.y.shape[0], solution.model.size) self.assertEqual(solution.sol.y.shape[1], solution.t_eval.shape[0]) self.assertIsInstance(solution.t_eval, np.ndarray) self.assertIsInstance(solution.y0, np.ndarray)
def test_analyse_models(self, mock_class): """ Test creation of a list containing a solution of each model. """ # Create a Solution object model1 = mock_class model2 = mock_class mock_class.configure_mock(components=2) mock_class.configure_mock(model_args={'name': 'test_model'}) model_list = [model1, model2] t_eval = [0, 1, 2, 3, 4, 5] y0 = [[0, 0], [1, 1]] solution = pk.Solution(model_list, t_eval, y0) # Set mock functions and properties solution._integrate = Mock(return_value=mock_class) t = PropertyMock(return_value='t') type(mock_class).t = t solution._save_to_csv = Mock(return_value='save_csv') sol_list = solution.analyse_models() self.assertEqual(sol_list, [mock_class, mock_class])
def test_plot_pipeline(self): """ Tests the whole pipeline; - make model - make protocol - make solution - plot solution for an intravenous dosing protocol. """ model = pk.Model(Vc=2., Vps=[], Qps=[], CL=3.) dosing = pk.Protocol(dose_amount=10, subcutaneous=True, k_a=0.3, continuous=True, continuous_period=[0.2, 0.6], instantaneous=True, dose_times=[0, .1, .2, .3]) solution = pk.Solution(model=model, protocol=dosing) sol_fig = solution.generate_plot() self.assertIsInstance(sol_fig, matplotlib.figure.Figure)
def test_all_zeros(self): """ Tests the whole pipeline; - make model - make protocol - make solution for an intravenous dosing protocol. """ model = pk.Model(Vc=2., Vps=[], Qps=[], CL=3.) dosing = pk.Protocol(dose_amount=0, subcutaneous=True, k_a=0.3, continuous=False, continuous_period=[], instantaneous=True, dose_times=[]) solution = pk.Solution(model=model, protocol=dosing) sol = solution.sol assert (not ((sol.y != 0).any()))
def test_integrate(self, mock_class): """ Test integration of a given model. """ def test_solution(t): """ Create the analytical solution of the test_rhs model. """ y_sol = -1 * math.exp(-3 * t / 2) return y_sol def test_rhs(t, y, args): """ A test model for the ODE solver. """ x, z = y dx_dt = -x + 1 / 4 * z dz_dt = x - z return [dx_dt, dz_dt] # Create a Solution object y_0 = [[-1, 2]] test_model = mock_class test_model.configure_mock(components=2) t_eval = np.linspace(0, 100, 1000) testsol = pk.Solution([test_model], t_eval, y_0) # Set the behaviour of the mock rhs method to the test model test_model.rhs.side_effect = test_rhs args = [-1, 1 / 4] sol = testsol._integrate(test_model, args, np.array(y_0[0])) # Obtain y values for the analytical solution y_sol = np.array([test_solution(x) for x in t_eval]) self.assertIsNone( np.testing.assert_almost_equal(sol.y[0], y_sol, decimal=3))
def test_build_sc(self): """ Tests the whole pipeline can be built; - make model - make protocol - make solution for an subcataneous dosing protocol. """ model = pk.Model(Vc=2., Vps=[1, 2], Qps=[3, 4], CL=3.) dosing = pk.Protocol(dose_amount=10, subcutaneous=True, k_a=0.3, continuous=True, continuous_period=[0.2, 0.6], instantaneous=True, dose_times=[0, .1, .2, .3]) solution = pk.Solution(model=model, protocol=dosing) self.assertEqual(solution.sol.y.shape[0], solution.model.size + 1) self.assertEqual(solution.sol.y.shape[1], solution.t_eval.shape[0]) self.assertIsInstance(solution.t_eval, np.ndarray) self.assertIsInstance(solution.y0, np.ndarray)
def test_create(self): """ Tests Solution creation. """ model = pk.Solution() self.assertEqual(model.value, 44)
'V_p1': 0.10, 'CL': 2.0 } model2_args = {'name': 'iv-2 comp', 'V_c': 1.0, 'CL': 1.0, 'k_a': 5} # Protocol trial_protocol = pk.Protocol(quantity=2, t_start=t_0, t_end=t_end, n=precison) X = trial_protocol.linear_dose() # Model trial1 = pk.Model(2, model1_args, 'iv', X) trial2 = pk.Model(2, model2_args, 'sc', X) models = [trial1, trial2] # Solution trial_sol = pk.Solution(models, t_eval=np.linspace(t_0, t_end, precison), y0=[[0, 0], [0, 0]]) trial_sol.analyse_models() # Visualisation labels = [model1_args['name'], model2_args['name']] path = [] for model in models: path.append(os.path.join('data', model.model_args['name'])) trial_vis = pk.Visualisation(path, labels, [trial1.dose_type, trial2.dose_type]) trial_vis.visualise(labels, os.path.join('data/plots', 'testplot.png'))