def test_create_initvalues_typeerror(self): """ Tests Protocol creation with invalid initial values. """ with self.assertRaises(TypeError): protocol = pk.Protocol(inital_dose='a') with self.assertRaises(TypeError): protocol = pk.Protocol(time_span='b') # noqa: F841
def test_type_error(self): """ Tests that an invalid input type returns a type error. """ model = pk.Protocol(5, 'error test') with self.assertRaises(TypeError): error_protocol = pk.Protocol(7, 'error').dose(3)
def test_create(self): """ Tests Protocol creation. """ with self.assertRaises(TypeError): protocol = pk.Protocol('string') protocol = pk.Protocol() self.assertEqual(protocol.name, 'Protocol-initial_dose=1.0-time_span=1.0')
def test_dose_function(self): """ Tests dose function creation. """ protocol = pk.Protocol('intravenous', 'instantaneous', [5], T=[10]) # noqa self.assertAlmostEqual(protocol.create_dose_function(1), 0) protocol = pk.Protocol('subcutaneous', 'continuous', 7, T=[10], absorption_rate=0.7) # noqa self.assertAlmostEqual(protocol.create_dose_function(4), 7)
def test_make_continuous(self): dosing = pk.Protocol() self.assertEqual(dosing.continuous, False) self.assertEqual(dosing.continuous_period, [0, 0]) dosing.make_continuous(4, 10) self.assertEqual(dosing.continuous, True) self.assertEqual(dosing.continuous_period, [4, 10])
def test_subcutaneous_comp_function(self): """ Tests subcutaneous compartment function creation. """ protocol = pk.Protocol('intravenous', 'instantaneous', [5], T=[10]) # noqa self.assertAlmostEqual( protocol.create_subcutaneous_comp_function(0, 0.5), 0) # noqa protocol = pk.Protocol('subcutaneous', 'continuous', 7, T=[10], absorption_rate=0.7) # noqa self.assertAlmostEqual( protocol.create_subcutaneous_comp_function(0, 0.5), 0.35) # noqa
def test_create_defaults(self): """ Tests Protocol creation default values. """ protocol = pk.Protocol() self.assertEqual(protocol.initial_dose, 1.0) self.assertEqual(protocol.time_span, 1.0)
def test_create_initvalues(self): """ Tests Protocol creation with initial values. """ protocol = pk.Protocol(initial_dose=1.1, time_span=1.2) self.assertEqual(protocol.initial_dose, 1.1) self.assertEqual(protocol.time_span, 1.2)
def test_create(self): """ Tests Solution creation. """ with self.assertRaises(TypeError): pk.Solution(0, pk.Protocol('subcutaneous', 'continuous', 7, T=[10]), 1000, 1) # noqa with self.assertRaises(TypeError): pk.Solution(pk.Model(0, 1, [1], [0], 1), 0, 1000, 1)
def test_errors(self): """ Tests error raises """ with self.assertRaises(TypeError): # check if error is raised when dose is not list # for instantaneous case pk.Protocol('subcutaneous', 'instantaneous', 3) with self.assertRaises(TypeError): # check if error is raised when injection time is not list # for instantaneous case pk.Protocol('subcutaneous', 'instantaneous', [2], T=3) with self.assertRaises(ValueError): # check if error is raised when length of dose list is equal to length of injection time # noqa # for instantaneous case pk.Protocol('intravenous', 'instantaneous', [2, 5], T=[10]) with self.assertRaises(TypeError): # check if error is raised when dose is not float or integer # for continuous case pk.Protocol('intravenous', 'continuous', [2]) with self.assertRaises(KeyError): # check if error is raised when the dosing pattern is spelled wrongly # noqa pk.Protocol('subcutaneous', 'instataneous', [2]) with self.assertRaises(KeyError): # check if error is raised when the dosing type is spelled wrongly pk.Protocol('intravanous', 'continuous', 2)
def test_solve(self): """ Tests solve function """ # Test the situation when Dose(t) is constant at 0, then the solution will remain at 0 # noqa test_model = pk.Model(0, 1, [1], [0], 1) test_protocol = pk.Protocol('intravenous', 'continuous', 0) test_solution = pk.Solution(test_model, test_protocol, 10, 10) test_sol = test_solution.solve npt.assert_array_almost_equal(test_sol.y, np.zeros((3, 10)), decimal=2) # Test the situation when there is only the central compartment test_model = pk.Model(1, 1, [], [], 0) test_protocol = pk.Protocol('intravenous', 'continuous', 1) test_solution = pk.Solution(test_model, test_protocol, 10, 10) test_sol = test_solution.solve npt.assert_array_almost_equal(test_sol.y, np.vstack((1 - np.exp(-test_sol.t), np.zeros((1, 10)))), decimal=2) # noqa
def test_create_dose(self): """ Tests Protocol dose function creation. """ protocol = pk.Protocol() dose = protocol.dose self.assertEqual(str(type(dose)), "<class 'function'>") dose_func_in = lambda y, t: 0 protocol.add_dose_function(func=dose_func_in) dose_func_out = protocol.dose self.assertEqual(dose_func_in, dose_func_out) with self.assertRaises(TypeError): dose_func = lambda z: 0 protocol.add_dose_function(func=dose_func)
def test_compare_pipeline_separate(self): """ Tests the whole pipeline; - make model x2 - make protocol x2 - make solution x2 - plot and compare the solutions using the separate for an intravenous dosing protocol. """ model = pk.Model(Vc=2., Vps=[3, 1], Qps=[1, 3], 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) model2 = pk.Model(Vc=2., Vps=[4, 5], Qps=[8, 9], CL=3.) dosing2 = pk.Protocol(dose_amount=15, subcutaneous=True, k_a=0.3, continuous=True, continuous_period=[0.3, 0.35], instantaneous=True, dose_times=[0, .1, .2, .3]) solution2 = pk.Solution(model=model2, protocol=dosing2) sol_fig = solution.generate_plot(compare=solution2, separate=True) self.assertIsInstance(sol_fig, matplotlib.figure.Figure)
def test_both_dose(self): dosing = pk.Protocol(dose_amount=10, subcutaneous=True, k_a=0.3, continuous=True, continuous_period=[1, 2], instantaneous=True, instant_doses=[10, 20], dose_times=[0.5, 1.5]) dose_0 = dosing.dose_time_function(0.5) dose_1 = dosing.dose_time_function(1.5) self.assertLessEqual(dose_0 - (10 / (0.02 * np.sqrt(2 * np.pi))), 0.0001) self.assertLessEqual(dose_1 - (10 + 20 / (0.02 * np.sqrt(2 * np.pi))), 0.0001)
def test_add_dose(self): dosing = pk.Protocol(instantaneous=False, instant_doses=[], dose_times=[]) self.assertEqual(dosing.instantaneous, False) self.assertEqual(dosing.dose_times, []) self.assertEqual(dosing.instant_doses, []) dosing.add_dose(1, 1) self.assertEqual(dosing.instantaneous, True) self.assertEqual(dosing.dose_times, [1]) self.assertEqual(dosing.instant_doses, [1]) dosing.add_dose(5, 1) self.assertEqual(dosing.instantaneous, True) self.assertEqual(dosing.dose_times, [1, 5]) self.assertEqual(dosing.instant_doses, [1, 1])
def test_dose_function(self): dosing = pk.Protocol(dose_amount=10, subcutaneous=True, k_a=0.3, continuous=True, continuous_period=[1, 2], instantaneous=False) dose_0 = dosing.dose_time_function(0) dose_1 = dosing.dose_time_function(0.5) dose_2 = dosing.dose_time_function(1.1) dose_3 = dosing.dose_time_function(1.8) dose_4 = dosing.dose_time_function(4) self.assertEqual(dose_0, 0) self.assertEqual(dose_1, 0) self.assertEqual(dose_2, 10) self.assertEqual(dose_3, 10) self.assertEqual(dose_4, 0)
def test_create(self): """ Tests Protocol creation. """ dosing = pk.Protocol(dose_amount=10, subcutaneous=True, k_a=0.3, continuous=True, continuous_period=[1, 2], instantaneous=True, dose_times=[0, 1, 2, 3]) self.assertEqual(dosing.dose_amount, 10) self.assertEqual(dosing.subcutaneous, True) self.assertEqual(dosing.k_a, 0.3) self.assertEqual(dosing.continuous, True) self.assertEqual(dosing.continuous_period, [1, 2]) self.assertEqual(dosing.instantaneous, True) self.assertEqual(dosing.dose_times, [0, 1, 2, 3])
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_encapsulation(self): """ Tests Protocol values are encapsulated. """ protocol = pk.Protocol() with self.assertRaises(AttributeError): protocol.Name with self.assertRaises(AttributeError): protocol.Initial_dose with self.assertRaises(AttributeError): protocol.Time_span with self.assertRaises(AttributeError): protocol._Name with self.assertRaises(AttributeError): protocol._Initial_dose with self.assertRaises(AttributeError): protocol._Time_span with self.assertRaises(AttributeError): protocol.__Name with self.assertRaises(AttributeError): protocol.__Initial_dose with self.assertRaises(AttributeError): protocol.__Time_span
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_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_discrete_tuple(self): """ Tests discrete dosing protocol with tuple. """ model = pk.Protocol(5, (2,3,4)) self.assertEqual(round(model.dose(2), 2), 1.67)
def test_continuous_int(self): """ Tests continuous dosing protocol with int value. """ model = pk.Protocol(1, 2) self.assertEqual(model.dose(2), 0.5)
t_0 = 0 t_end = 1 precison = 1000 model1_args = { 'name': 'model1', 'Q_p1': 3.0, 'V_c': 2.0, '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']]
def test_change_dose(self): dosing = pk.Protocol() self.assertEqual(dosing.dose_amount, 1) dosing.change_dose(100) self.assertEqual(dosing.dose_amount, 100)
def test_modify_dose_type(self): dosing = pk.Protocol() self.assertEqual(dosing.subcutaneous, False) dosing.modify_dose_type(True, 0.3) self.assertEqual(dosing.subcutaneous, True) self.assertEqual(dosing.k_a, 0.3)
def test_create(self): """ Tests Protocol creation. """ model = pk.Protocol() self.assertEqual(model.value, 43)
def test_discrete_list(self): """ Tests discrete dosing protocol with list. """ model = pk.Protocol(7, [1,2,3]) self.assertEqual(round(model.dose(1), 2), 2.33)
def test_create(self): """ Tests Protocol creation. """ protocol = pk.Protocol() self.assertEqual(protocol.dose_amount, 1)
def test_continuous_float(self): """ Tests continuous dosing protocol with float value. """ model = pk.Protocol(9, 2.25) self.assertEqual(model.dose(3), 0)