def test_progress_nested(make_napari_viewer): """Test nested progress bars are added with QtProgressBarGroup""" viewer = make_napari_viewer(show=SHOW) assert not qt_viewer_has_pbar(viewer) with progress(range(10)) as pbr: assert qt_viewer_has_pbar(viewer) pbr2 = progress(range(2), nest_under=pbr) prog_groups = get_progress_groups(viewer.window._qt_viewer) assert len(prog_groups) == 1 # two progress bars + separator assert prog_groups[0].layout().count() == 3 pbr2.close() assert not prog_groups[0].isVisible()
def try_thresholds(): """Tries each threshold, and adds result to viewer.""" if 'Binarised' in viewer.layers: del viewer.layers['Binarised'] thresholded_nuclei = [] # we wrap our iterable with `progress` # this will automatically add a progress bar to our activity dock for threshold_func in progress(all_thresholds): current_threshold = threshold_func(cell_nuclei) binarised_im = cell_nuclei > current_threshold thresholded_nuclei.append(binarised_im) # uncomment if processing is too fast # sleep(0.5) # working with a wrapped iterable, the progress bar will be closed # as soon as the iteration is complete binarised_nuclei = np.stack(thresholded_nuclei) viewer.add_labels( binarised_nuclei, color={1: 'lightgreen'}, opacity=0.7, name="Binarised", blending='translucent', )
def iterable(): """using progress as a wrapper for iterables """ my_stacked_volume = np.random.random((5, 4, 500, 500)) # we can wrap any iterable object in `progress` and see a progress # bar in the viewer for im_slice in progress(my_stacked_volume): process(im_slice)
def test_closing_viewer_no_error(make_napari_viewer): """Closing viewer with active progress doesn't cause RuntimeError""" viewer = make_napari_viewer(show=SHOW) assert not qt_viewer_has_pbar(viewer) with progress(range(100)): assert qt_viewer_has_pbar(viewer) viewer.close()
def test_progress_with_context(make_napari_viewer): """Test adding/removing of progress bar with context manager""" viewer = make_napari_viewer(show=SHOW) with assert_pbar_added_to(viewer): with progress(range(100)) as prog: pbar = get_qt_labeled_progress_bar(prog, viewer) assert pbar.qt_progress_bar.maximum() == prog.total == 100
def test_progress_with_iterable(): """Test typical iterable is correctly built""" r = range(100) pbr = progress(r, desc='iterable') assert pbr.iterable is r assert pbr.n == 0 with assert_progress_added_to_all(pbr): pbr.close()
def test_progress_set_description(): """Test setting description works as expected""" pbr = progress(total=5) pbr.set_description("Test") assert pbr.desc == "Test: " pbr.close() assert pbr not in progress._all_instances
def iterable_w_context(): """using progress with a context manager """ my_stacked_volume = np.random.random((5, 4, 500, 500)) # progress provides a context manager we can use for automatic # teardown of our widget once iteration is complete. Wherever # possible, we should *always* use progress within a context with progress(my_stacked_volume) as pbr: for i, im_slice in enumerate(pbr): # using a context manager also allows us to manipulate # the progress object e.g. by setting a description pbr.set_description(f"Slice {i}") # we can group progress bars together in the viewer # by passing a parent progress bar to new progress # objects' nest_under attribute for channel in progress(im_slice, nest_under=pbr): process(channel)
def test_progress_set_description(make_napari_viewer): viewer = make_napari_viewer(show=SHOW) prog = progress(total=5) prog.set_description("Test") pbar = get_qt_labeled_progress_bar(prog, viewer) assert pbar.description_label.text() == "Test: " prog.close()
def test_progress_with_total(): """Test progress with total not iterable, and manual updates""" pbr = progress(total=5, desc='total') assert pbr.n == 0 pbr.update(1) assert pbr.n == 1 with assert_progress_added_to_all(pbr): pbr.close()
def test_progress_with_ndarray(): """Test 2D ndarray is correctly built""" iter_ = np.random.random((100, 100)) pbr = progress(iter_, desc='ndarray') assert pbr.iterable is iter_ assert pbr.n == 0 with assert_progress_added_to_all(pbr): pbr.close()
def test_progress_indicator(make_napari_viewer): viewer = make_napari_viewer(show=SHOW) activity_dialog = viewer.window._qt_viewer.window()._activity_dialog # it's not clear why, but using the context manager here # causes test to fail, so we make the assertions explicitly assert not qt_viewer_has_pbar(viewer) with progress(range(10)): assert qt_viewer_has_pbar(viewer) assert activity_button_shows_indicator(activity_dialog)
def indeterminate(): """By passing a total of 0, we can have an indeterminate progress bar """ # note progress(total=0) is equivalent to progress() with progress(total=0) as pbr: x = 0 while x != 42: pbr.set_description(f"Processing {x}") x = choice(range(100)) sleep(0.05)
def test_activity_dialog_holds_progress(make_napari_viewer): """Progress gets added to dialog & once finished it gets removed""" viewer = make_napari_viewer(show=SHOW) with assert_pbar_added_to(viewer): r = range(100) prog = progress(r) pbar = get_qt_labeled_progress_bar(prog, viewer) assert pbar is not None assert pbar.progress is prog assert pbar.qt_progress_bar.maximum() == prog.total prog.close() assert not pbar.isVisible()
def test_progress_update(): """Test update with different values""" pbr = progress(total=10, desc='update') assert pbr.n == 0 pbr.update(1) pbr.refresh() # not sure why this has to be called manually here assert pbr.n == 1 pbr.update(2) pbr.refresh() assert pbr.n == 3 with assert_progress_added_to_all(pbr): pbr.close()
def arbitrary_steps(): """We can manually control updating the value of the progress bar. """ with progress(total=4) as pbr: sleep(3) pbr.set_description("Step 1 Complete") # manually updating the progress bar by 1 pbr.update(1) sleep(1) pbr.set_description("Step 2 Complete") pbr.update(1) sleep(2) pbr.set_description("Processing Complete!") # we can manually update by any number of steps pbr.update(2) # sleeping so we can see full completion sleep(1)
def process_ims(): """ First performs thresholding, then segmentation on our image. Manually updates a `progress` object. """ if 'Binarised' in viewer.layers: del viewer.layers['Binarised'] if 'Segmented' in viewer.layers: del viewer.layers['Segmented'] # we instantiate a manually controlled `progress` object # by just passing a total with no iterable with progress(total=2) as pbar: pbar.set_description("Thresholding") try_thresholds() # once one processing step is complete, we increment # the value of our progress bar pbar.update(1) pbar.set_description("Segmenting") segment_binarised_ims() pbar.update(1)
def segment_binarised_ims(): """Segments each of the binarised ims. Uses `progress` within a context manager allowing us to manipulate the progress bar within the loop """ if 'Binarised' not in viewer.layers: raise TypeError("Cannot segment before thresholding") if 'Segmented' in viewer.layers: del viewer.layers['Segmented'] binarised_data = viewer.layers['Binarised'].data segmented_nuclei = [] # using the `with` keyword we can use `progress` inside a context manager # `progress` inherits from tqdm and therefore provides the same API # e.g. we can provide the miniters argument if we want to see the # progress bar update with each iteration with progress(binarised_data, miniters=0) as pbar: for i, binarised_cells in enumerate(pbar): # this allows us to manipulate the pbar object within the loop # e.g. setting the description. pbar.set_description(all_thresholds[i].__name__.split("_")[1]) labelled_im = label(binarised_cells) segmented_nuclei.append(labelled_im) # uncomment if processing is too fast # sleep(0.5) # progress bar is still automatically closed segmented_nuclei = np.stack(segmented_nuclei) viewer.add_labels( segmented_nuclei, name="Segmented", blending='translucent', ) viewer.layers['Binarised'].visible = False
def test_progress_with_context(): """Test context manager works as expected""" with progress(range(100), desc='context') as pbr: assert pbr in progress._all_instances assert pbr.n == 0 assert pbr not in progress._all_instances
def test_progrange(): """Test progrange shorthand for progress(range(n))""" with progrange(10) as pbr: with progress(range(10)) as pbr2: assert pbr.iterable == pbr2.iterable assert pbr not in progress._all_instances