def test_default_values_positivity(self, input, expected): ''' Test ValueErrors are raised for invalid model params ''' with self.assertRaises(expected): pk.Model(clearance_rate=input) with self.assertRaises(expected): pk.Model(vol_c=input)
def test_model_params_valid_type(self, input, expected): ''' Test TypeErrors are raised for invalid model params ''' # __init__ input params with self.assertRaises(expected): pk.Model(clearance_rate=input) with self.assertRaises(expected): pk.Model(vol_c=input) with self.assertRaises(expected): pk.Model(dose=input)
def test_subcutaneous_compartment_equals_absorption(self): ''' Test subcutaneous compartment equals absorption rate ''' test_model_default = pk.Model() test_model = pk.Model() assert (test_model.subcutaneous_compartment is None) assert (test_model_default.subcutaneous_compartment is None) test_model_default.add_subcutaneous_compartment() assert (test_model_default.subcutaneous_compartment == 1) abs_rate = 10 test_model.add_subcutaneous_compartment(absorption_rate=abs_rate) assert (test_model.subcutaneous_compartment == abs_rate)
def test_add_compartment(self): """ Tests add_compartment function """ model = pk.Model(Vc=2, Vps=[], Qps=[], CL=3) model.add_compartment() self.assertEqual(model.size, 2)
def test_model_instantiation(self): """ Tests Model instantiation. """ test_args_sc3 = { 'name': 'test_model', 'V_c': 1.0, 'Q_p1': 2.0, 'V_p1': 3.0, 'CL': 4.0, 'k_a':5.0 } test_args_iv1 = { 'name': 'test_model', 'V_c': 1.0, 'CL': 2.0 } test_args_nve = { 'name': 'test_model', 'V_c': -1.0, 'CL': 2.0 } # Negative value in the component properties with self.assertRaises(AssertionError): obj = pk.Model(1,test_args_nve,'iv',0) # Unavailable type of injection with self.assertRaises(ValueError): obj = pk.Model(0,test_args_iv1,'jk',0) # Too few components for subcutaneous injections with self.assertRaises(ValueError): obj = pk.Model(1,test_args_iv1,'sc',0) # Number of components and model keys do not match # sc with self.assertRaises(ValueError): obj = pk.Model(2,test_args_sc3,'sc',0) # iv with self.assertRaises(ValueError): obj = pk.Model(3,test_args_iv1,'iv',0)
def test_properties(self): """ Tests Vc, Vps, Qps, and CL properties """ model = pk.Model(Vc=2., Vps=[], Qps=[], CL=3.) model.add_compartment() self.assertEqual(len(model.Vps), model.size - 1) self.assertEqual(len(model.Qps), model.size - 1) self.assertIsInstance(model.Vc, float) self.assertIsInstance(model.CL, float)
def test_create(self): """ Tests Model creation. """ model = pk.Model(Vc=2., Vps=[], Qps=[], CL=3.) self.assertIsInstance(model, pk.Model) self.assertIsInstance(model.Vps, list) self.assertIsInstance(model.Qps, list) self.assertIsInstance(model.CL, float) self.assertEqual(model.size, 1)
def test_model_definition(self): """ Tests Model definition. """ test_args_sc3 = { 'name': 'test_model', 'V_c': 1.0, 'Q_p1': 2.0, 'V_p1': 3.0, 'CL': 4.0, 'k_a':5.0 } test_args_iv1 = { 'name': 'test_model', 'V_c': 1.0, 'CL': 2.0 } # test correct definition of 1 comp iv obj1 = pk.Model(1,test_args_iv1,'iv',[0]) args = obj1.make_args() assert len(args[0]) == 1 , 'Volumes incorrectly defined' assert len(args[1]) == 0 , 'Transitions incorrectly defined' assert type(args[2]) == float , 'CL incorrectly defined' rates = obj1.rhs(t = 0, y = [0], args = args) assert len(rates) == 1, 'iv ODE system incorrectly defined' # test correct definition of 3 comp sc obj1 = pk.Model(3,test_args_sc3,'sc',[0]) args = obj1.make_args() assert type(args[0]) == float , 'k_a incorrectly defined' assert len(args[1]) == 2 , 'Volumes incorrectly defined' assert len(args[2]) == 1 , 'Transitions incorrectly defined' assert type(args[3]) == float , 'CL incorrectly defined' rates = obj1.rhs(t = 0, y = [0,0,0],args = args) assert len(rates) == 3, 'sc ODE system incorrectly defined'
def test_add_subcutaneous_compartments_increases_compartments(self): ''' Test add_subcutaneous_compartments adds 1 to object.no_of_compartments ''' test_model = pk.Model() initial_no_of_compartments = test_model.number_of_compartments test_model.add_subcutaneous_compartment() assert ( test_model.number_of_compartments == initial_no_of_compartments + 1)
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_cannot_add_more_than_1_sc_compartment(self): ''' Adding more than one SC compartment should raise Attribute Error. ''' test_model = pk.Model() assert (test_model.subcutaneous_compartment is None) test_model.add_subcutaneous_compartment() assert (test_model.subcutaneous_compartment is not None) with self.assertRaises(AttributeError): test_model.add_subcutaneous_compartment()
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_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_vol_and_quantity_peripheral_compartments(self): ''' Test that the vol & drug quantity in a peripheral compartment are uploaded correctly ''' test_model = pk.Model() vol_p, q_p = 10, 20 test_model.add_peripheral_compartment(vol_p=vol_p, q_p=q_p) assert (test_model.peripheral_compartments[0]["vol_p"] == vol_p) assert (test_model.peripheral_compartments[0]["q_p"] == q_p) vol_p, q_p = 30, 40 test_model.add_peripheral_compartment(vol_p=vol_p, q_p=q_p) assert (test_model.peripheral_compartments[1]["vol_p"] == vol_p) assert (test_model.peripheral_compartments[1]["q_p"] == q_p)
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_default_init_attribute_values(self): """ Tests default values in initialising model are correct. """ test_model = pk.Model() default_values = { "clearance_rate": 1, "vol_c": 1, "subcutaneous_compartment": None, "peripheral_compartments": [], "number_of_compartments": 1, "number_of_peripheral_compartments": 0 } assert (test_model.clearance_rate == default_values["clearance_rate"]) assert (test_model.vol_c == default_values["vol_c"]) assert (test_model.subcutaneous_compartment == default_values["subcutaneous_compartment"]) assert (test_model.peripheral_compartments == default_values["peripheral_compartments"]) assert (test_model.number_of_compartments == default_values["number_of_compartments"]) assert (test_model.number_of_peripheral_compartments == default_values["number_of_peripheral_compartments"])
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_add_peripheral_compartments_increases_compartments(self): """ Tests add_peripheral_compartments adds 1 to object.no_of_peripheral_compartments and object.no_of_compartments """ test_model = pk.Model() initial_compartments = [ test_model.number_of_peripheral_compartments, test_model.number_of_compartments ] assert (len(test_model.peripheral_compartments) == test_model.number_of_peripheral_compartments) test_model.add_peripheral_compartment() new_compartments = [ test_model.number_of_peripheral_compartments, test_model.number_of_compartments ] assert (len(test_model.peripheral_compartments) == test_model.number_of_peripheral_compartments) for i in range(len(initial_compartments)): assert (new_compartments[i] == initial_compartments[i] + 1)
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 Model creation. """ # Creates a viable model model = pk.Model(0, 1, [1], [0], 1) self.assertEqual(model.parameters, (0, 1, [1], [0], 1)) # Test wrong dimensions with self.assertRaises(ValueError): pk.Model(0, 1, [1, 1], [0], 1) with self.assertRaises(ValueError): pk.Model(0, 1, [1], [0], 2) with self.assertRaises(ValueError): pk.Model(0, 1, [1], [0], -1) with self.assertRaises(ValueError): pk.Model(0, 0, [1], [0], 1) with self.assertRaises(ValueError): pk.Model(0, 1, [0], [0], 1)
def test_call(self): """ Tests Model calling function. """ model = pk.Model(0, 1, [1], [0], 1) self.assertEqual(str(model), "new_model")
def test_create(self): """ Tests Model creation. """ model = pk.Model() self.assertEqual(model.value, 42)
def test_peripherals(self): """ Tests that model has correct number of peripherals """ model = pk.Model([[0, 1], [2, 4], [3, 5]]) self.assertEqual(len(model.peripherals), 2)
def test_central(self): """ Tests that model has a central compartment """ model = pk.Model([[0, 1], [2, 4], [3, 5]]) self.assert_(model.central)
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']] path = [] for model in models: path.append(os.path.join('data', model.model_args['name']))
def test_parameters(self): """ Tests Model Parameter calling function. """ model = pk.Model(0, 1, [1], [0], 1) self.assertEqual(model.parameters, (0, 1, [1], [0], 1))