def test_progress_no_viewer(): assert list(progress(range(10))) == list(range(10)) with progress(total=5) as pbr: pbr.set_description('Test') assert pbr.desc == "Test: " pbr.update(3) assert pbr.n == 3
def test_progress_no_viewer(): assert list(progress(range(10))) == list(range(10)) with progress(total=5) as pbr: # TODO: debug segfaults if sys.platform != 'linux': pbr.set_description('Test') assert pbr.desc == "Test: " pbr.update(3) assert pbr.n == 3
def test_progress_nested(make_napari_viewer): viewer = make_napari_viewer(show=SHOW) with assert_pbar_added_to(viewer): with progress(range(10)) as pbr: 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()
def test_progress_indicator(make_napari_viewer): viewer = make_napari_viewer(show=SHOW) activity_dialog = viewer.window.qt_viewer.window()._activity_dialog with assert_pbar_added_to(viewer): with progress(range(10)): assert activity_button_shows_indicator(activity_dialog)
def test_progress_with_context(make_napari_viewer): viewer = make_napari_viewer() with assert_pbar_added_to(viewer): with progress(range(100)) as pbr: assert pbr.n == 0 assert pbr._pbar.pbar.maximum() == pbr.total == 100
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 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) # uncomment this line to see the 100% progress bar sleep(0.5)
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 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): make_napari_viewer() pbr = progress(total=5) pbr.set_description("Test") assert pbr.desc == "Test: " assert pbr._pbar.description_label.text() == "Test: " pbr.close()
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_progress_with_iterable(make_napari_viewer): viewer = make_napari_viewer(show=SHOW) with assert_pbar_added_to(viewer): r = range(100) pbr = progress(r) assert pbr.iterable is r assert pbr.n == 0 assert pbr._pbar.pbar.maximum() == pbr.total == 100 pbr.close()
def test_progress_with_ndarray(make_napari_viewer): viewer = make_napari_viewer() with assert_pbar_added_to(viewer): iter = np.random.random((100, 100)) pbr = progress(iter) assert pbr.iterable is iter assert pbr.n == 0 assert pbr._pbar.pbar.maximum() == pbr.total pbr.close()
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}") process(im_slice)
def test_progress_with_total(make_napari_viewer): viewer = make_napari_viewer() with assert_pbar_added_to(viewer): pbr = progress(total=5) assert pbr.n == 0 assert pbr._pbar.pbar.maximum() == pbr.total == 5 pbr.update(1) assert pbr.n == 1 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 test_progress_update(make_napari_viewer): make_napari_viewer() pbr = progress(total=10) assert pbr.n == 0 assert pbr._pbar.pbar.value() == 0 pbr.update(1) pbr.refresh() # not sure why this has to be called manually here assert pbr.n == 1 assert pbr._pbar.pbar.value() == 1 pbr.update(2) pbr.refresh() assert pbr.n == 3 assert pbr._pbar.pbar.value() == 3 pbr.close()
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_progrange(): with progrange(10) as pbr: with progress(range(10)) as pbr2: assert pbr.iterable == pbr2.iterable
def test_progrange(): assert progress(range(10)).iterable == progrange(10).iterable