def test_sampling_output(self): """ Check that output generated by the sampling postprocessing goes to the correct path.""" # Setup parameters. parameters = CancerSimulatorParameters( matrix_size=1000, number_of_generations=20, division_probability=1, adv_mutant_division_probability=1, death_probability=0.1, adv_mutant_death_probability=0.0, mutation_probability=1, adv_mutant_mutation_probability=1, adv_mutation_wait_time=10, number_of_initial_mutations=150, number_of_mutations_per_division=1, tumour_multiplicity=None, sampling_fraction=0.5, plot_tumour_growth=True, export_tumour=True, ) simulator = CancerSimulator(parameters=parameters, seed=1, outdir="casim_out") # Cleanup (remove test output). self._test_files.append(simulator.outdir) # Run the simulation. simulator.run() # Check sampling file was written. self.assertRegex(",".join(os.listdir(simulator._CancerSimulator__simdir)), re.compile(r"sample_out_[0-9]{3}_[0-9]{3}.txt")) self.assertRegex(",".join(os.listdir(simulator._CancerSimulator__simdir)), re.compile(r"sampleHistogram_[0-9]{3}_[0-9]{3}.pdf")) self.assertIn('growthCurve.pdf', os.listdir(simulator._CancerSimulator__simdir))
def test_init_step_increases(self): """ Check the the internal step counter is propely updated. """ parameters = CancerSimulatorParameters() cancer_sim = CancerSimulator(parameters, seed=1, outdir = mkdtemp()) self.assertEqual(cancer_sim._CancerSimulator__init_step, 0) cancer_sim.run() self.assertEqual(cancer_sim._CancerSimulator__init_step, 2)
def test_export_tumour_matrix(self): """ Test exporting the tumour matrix. """ parameters = CancerSimulatorParameters() cancer_sim = CancerSimulator(parameters, seed=1, outdir = mkdtemp()) cancer_sim.run() # Check files where created. listing = os.listdir(cancer_sim._CancerSimulator__simdir) for f in ['mtx.p', 'mut_container.p', 'mtx_VAF.txt']: self.assertIn(f, listing)
def test_high_sampling_fraction(self): """ Test run with sampling_fraction=0.9 """ # Setup parameters. parameters = CancerSimulatorParameters( matrix_size=1000, number_of_generations=20, division_probability=1, adv_mutant_division_probability=1, death_probability=0.1, adv_mutant_death_probability=0.0, mutation_probability=1, adv_mutant_mutation_probability=1, adv_mutation_wait_time=10, number_of_initial_mutations=150, number_of_mutations_per_division=50, tumour_multiplicity=None, read_depth=100, sampling_fraction=0.9, ) # Setup the simulator. simulator = CancerSimulator(parameters=parameters, seed=1, outdir=mkdtemp()) # Cleanup (remove test output). self._test_files.append(simulator.outdir) # Check run returns sanely. self.assertEqual(simulator.run(), 0)
def test_reference_data_1mut(self): """ Run a reference test and compare against reference data.""" """ 1 mutation per division.""" # Setup parameters. Values taken from casim/params.py. parameters = CancerSimulatorParameters( matrix_size=1000, number_of_generations=20, division_probability=1, adv_mutant_division_probability=1, death_probability=0.1, adv_mutant_death_probability=0.0, mutation_probability=1, adv_mutant_mutation_probability=1, adv_mutation_wait_time=10, number_of_initial_mutations=150, number_of_mutations_per_division=1, tumour_multiplicity=None, sampling_fraction=0.1, sampling_positions=[(501,502)], ) simulator = CancerSimulator(parameters=parameters, seed=1, outdir="reference_test_out") self._test_files.append('reference_test_out') simulator.run() ### Load results and reference data. # Reference data. ref_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'reference_test_data_1mut', 'cancer_1', 'simOutput') with open(os.path.join(ref_dir, 'mtx.p'), 'rb') as fp: ref_mtx = pickle.load(fp) with open(os.path.join(ref_dir, 'mut_container.p'), 'rb') as fp: ref_mutations = pickle.load(fp) run_dir = os.path.join('reference_test_out', 'cancer_1', 'simOutput') # Run data. with open(os.path.join(run_dir, 'mtx.p'), 'rb') as fp: run_mtx = pickle.load(fp) with open(os.path.join(run_dir, 'mut_container.p'), 'rb') as fp: run_mutations = pickle.load(fp) # Check data is equal. self.assertAlmostEqual(numpy.linalg.norm((ref_mtx - run_mtx).toarray()), 0.0) self.assertEqual(ref_mutations, run_mutations)
def test_sampling_positions(self): """ Check setting the sample positions generates the expected output.""" # Setup parameters. parameters = CancerSimulatorParameters( matrix_size=1000, number_of_generations=20, division_probability=1, adv_mutant_division_probability=1, death_probability=0.1, adv_mutant_death_probability=0.0, mutation_probability=1, adv_mutant_mutation_probability=1, adv_mutation_wait_time=10, number_of_initial_mutations=150, number_of_mutations_per_division=1, tumour_multiplicity=None, sampling_fraction=0.1, sampling_positions=[ [500,500], [450,550], [450,550], [550,450], [550,550], ], plot_tumour_growth=True, export_tumour=True, ) simulator = CancerSimulator(parameters=parameters, seed=1, outdir="casim_out") # Cleanup (remove test output). self._test_files.append(simulator.outdir) # Run the simulation. simulator.run() # Check sampling file was written. self.assertIn("sample_out_500_500.txt", os.listdir(simulator._CancerSimulator__simdir)) self.assertIn("sampleHistogram_500_500.pdf", os.listdir(simulator._CancerSimulator__simdir)) self.assertRegex(",".join(os.listdir(simulator._CancerSimulator__simdir)), re.compile(r"sampleHistogram_[0-9]{3}_[0-9]{3}.pdf"))
parameters = CancerSimulatorParameters( matrix_size=4096, number_of_generations=g, # vary division_probability=dp, # 1 => exp. growth adv_mutant_division_probability=0.3, death_probability=0.0, adv_mutant_death_probabilityability=0.0, mutation_rate=1.0, # 1 => adv_mutant_mutation_probability=0.8, mutations_per_division= 10, # if mutation event occurs, have this number of mutation adv_mutation_interval=30000, # large to not kick in number_of_initital_mutations= 2, # initial number of mutations in first cancer cell tumour_multiplicity='single', ) simulator = CancerSimulator(parameters, seed=None, outdir="%s" % (n_run)) t0 = time() simulator.run() T = time() - t0 with open(os.path.join(simulator.outdir, "log.txt"), 'a') as fp: fp.write("%s\t%f\t%d\t%e\n" % (simulator.seed, dp, g, T)) simulator.dump() sleep(1)