def test_callbacks(self): cb1 = mock.create_autospec(ProgressHandler) cb2 = mock.create_autospec(ProgressHandler) callbacks = [cb1, cb2] def assert_call(expected_completion, expected_step, expected_msg): for m in callbacks: m.progress_update.assert_called_once() m.reset_mock() p = Progress(5) p.add_progress_handler(cb1) p.add_progress_handler(cb2) p.update(msg="First") assert_call(0.167, 1, "First") p.update(steps=2, msg="Second") assert_call(0.5, 3, "Second") p.update(msg="Third") assert_call(0.667, 4, "Third") p.update(msg="Last") assert_call(0.833, 5, "Last") p.mark_complete() assert_call(1.0, 6, "complete")
def execute_impl(img_num: int, partial_func: partial, cores: int, chunksize: int, progress: Progress, msg: str): task_name = f"{msg} {cores}c {chunksize}chs" progress = Progress.ensure_instance(progress, num_steps=img_num, task_name=task_name) indices_list = generate_indices(img_num) if multiprocessing_necessary(img_num, cores): with Pool(cores) as pool: for _ in pool.imap(partial_func, indices_list, chunksize=chunksize): progress.update(1, msg) else: for ind in indices_list: partial_func(ind) progress.update(1, msg) progress.mark_complete()
def test_context(self): p = Progress(num_steps=2) self.assertFalse(p.is_started()) self.assertFalse(p.is_completed()) self.assertEqual(len(p.progress_history), 1) self.assertEqual(p.completion(), 0.0) with p: p.update(msg="do a thing") self.assertTrue(p.is_started()) self.assertEqual(len(p.progress_history), 2) self.assertEqual(p.completion(), 0.5) self.assertTrue(p.is_completed()) self.assertEqual(len(p.progress_history), 3) self.assertEqual(p.completion(), 1.0)
def test_cancellation(self): p = Progress() p.add_estimated_steps(10) with p: for i in range(10): if i < 6: p.update() if i == 5: p.cancel("nope") self.assertTrue(p.should_cancel) else: with self.assertRaises(RuntimeError): p.update() self.assertFalse(p.is_completed()) self.assertTrue(p.should_cancel)
def test_basic_single_step(self): p = Progress(num_steps=2) self.assertFalse(p.is_started()) self.assertFalse(p.is_completed()) self.assertEqual(len(p.progress_history), 1) self.assertEqual(p.completion(), 0.0) p.update(msg="do a thing") self.assertTrue(p.is_started()) self.assertEqual(len(p.progress_history), 2) self.assertEqual(p.completion(), 0.5) p.mark_complete() self.assertTrue(p.is_completed()) self.assertEqual(len(p.progress_history), 3) self.assertEqual(p.completion(), 1.0)
def auto_find_minimisation_sqsum(self, slices: List[int], recon_params: ReconstructionParameters, initial_cor: Union[float, List[float]], progress: Progress) -> List[float]: """ :param slices: Slice indices to be reconstructed :param recon_params: Reconstruction parameters :param initial_cor: Initial COR for the slices. Will be used as the start for the minimisation. If a float is passed it will be used for all slices. If a list is passed, the COR will be retrieved for each slice. :param progress: Progress reporter """ # Ensure we have some sample data if self.images is None: return [0.0] if isinstance(initial_cor, list): assert len(slices) == len( initial_cor ), "A COR for each slice index being reconstructed must be provided" else: # why be efficient when you can be lazy? initial_cor = [initial_cor] * len(slices) reconstructor = get_reconstructor_for(recon_params.algorithm) progress = Progress.ensure_instance(progress, num_steps=len(slices)) progress.update(0, msg=f"Calculating COR for slice {slices[0]}") cors = [] for idx, slice in enumerate(slices): cor = reconstructor.find_cor(self.images, slice, initial_cor[idx], recon_params) cors.append(cor) progress.update(msg=f"Calculating COR for slice {slice}") return cors
def test_multi_step(self): # Default to estimating a single step p = Progress(num_steps=2) self.assertEqual(p.end_step, 2) self.assertEqual(len(p.progress_history), 1) self.assertEqual(p.completion(), 0.0) p.update(msg="Estimate how complex I am") self.assertEqual(p.end_step, 2) self.assertEqual(len(p.progress_history), 2) self.assertEqual(p.completion(), 0.5) # First step to execute may decide that more steps are required p.add_estimated_steps(10) self.assertEqual(p.end_step, 12) self.assertEqual(len(p.progress_history), 2) self.assertEqual(p.completion(), 0.083) p.update(steps=2, msg="Do two things") self.assertEqual(len(p.progress_history), 3) self.assertEqual(p.completion(), 0.25) p.update(steps=2, msg="Do two more things") self.assertEqual(len(p.progress_history), 4) self.assertEqual(p.completion(), 0.417) p.update(steps=3, msg="Do three things") self.assertEqual(len(p.progress_history), 5) self.assertEqual(p.completion(), 0.667) p.mark_complete() self.assertEqual(len(p.progress_history), 6) self.assertEqual(p.completion(), 1.0)