From b688b78d691852533e97c81550bebaf94ece3070 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 30 Apr 2023 13:49:48 -0700 Subject: [PATCH 1/4] viewer prototype --- examples/example_viewer.py | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 examples/example_viewer.py diff --git a/examples/example_viewer.py b/examples/example_viewer.py new file mode 100644 index 00000000..1c95ac2e --- /dev/null +++ b/examples/example_viewer.py @@ -0,0 +1,64 @@ +from pycromanager import Dataset +import numpy as np +import napari +import time +from napari.qt import thread_worker + +LF_LAYER_NAME = 'label-free data' +LS_LAYER_NAME = 'light-sheet data' + +lf_dataset_path = r'Z:\rawdata\mantis\2023_04_20 HEK RAC1 PCNA\1timepoint_test_1\1timepoint_test_labelfree_1' +ls_dataset_path = r'Z:\rawdata\mantis\2023_04_20 HEK RAC1 PCNA\1timepoint_test_1\1timepoint_test_lightsheet_1' + +viewer = napari.Viewer() +lf_dataset = Dataset(lf_dataset_path) +ls_dataset = Dataset(ls_dataset_path) + +t_start = time.time() + +def update_layers(data): + """ + update the napari layer with the new image + """ + lf_data = data[0] + ls_data = data[1] + + if lf_data is not None and ls_data is not None: + # update data + try: + viewer.layers[LF_LAYER_NAME].data = lf_data + viewer.layers[LS_LAYER_NAME].data = ls_data + # layers do not exist, create display + except KeyError: + viewer.add_image(lf_data, name=LF_LAYER_NAME) + viewer.add_image(ls_data, name=LS_LAYER_NAME) + +@thread_worker(connect={'yielded': update_layers}) +def napari_signaller(lf_dataset, ls_dataset): + """ + Monitor for signals that Acqusition has a new image ready, and when that happens + update napari appropriately + """ + while True: + if time.time() - t_start < 60: + print('waiting 1 sec') + time.sleep(1) + elif time.time() - t_start < 5 * 60: + print('waiting 15 sec') + time.sleep(15) + elif time.time() - t_start < 60 * 60: + print('waiting 2 min') + time.sleep(2 * 60) + else: + print('waiting 10 min') + time.sleep(10 * 60) + + lf_data, ls_data = None, None + if lf_dataset is not None and ls_dataset is not None: + lf_data = lf_dataset.as_array() + ls_data = ls_dataset.as_array() + + yield (lf_data, ls_data) + +napari_signaller(lf_dataset, ls_dataset) +napari.run() \ No newline at end of file From 5d2bd5dead6bc51e6b2ca0957eb7320b964a4ad4 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 1 May 2023 19:18:19 -0700 Subject: [PATCH 2/4] add viewer module --- mantis/acquisition/viewer.py | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 mantis/acquisition/viewer.py diff --git a/mantis/acquisition/viewer.py b/mantis/acquisition/viewer.py new file mode 100644 index 00000000..f52792b1 --- /dev/null +++ b/mantis/acquisition/viewer.py @@ -0,0 +1,79 @@ +import click +from pycromanager import Dataset +import napari +import time +from napari.qt import thread_worker + +_verbose = False +LF_LAYER_NAME = 'label-free data' +LS_LAYER_NAME = 'light-sheet data' + +viewer = napari.Viewer() +t_start = time.time() + +def update_layers(data): + """ + update the napari layer with the new image + """ + lf_data = data[0] + ls_data = data[1] + + if lf_data is not None and ls_data is not None: + # update data + try: + viewer.layers[LF_LAYER_NAME].data = lf_data + viewer.layers[LS_LAYER_NAME].data = ls_data + # layers do not exist, create display + except KeyError: + viewer.add_image(lf_data, name=LF_LAYER_NAME) + viewer.add_image(ls_data, name=LS_LAYER_NAME) + +@thread_worker(connect={'yielded': update_layers}) +def napari_signaller(lf_dataset_path, ls_dataset_path): + """ + Monitor for signals that Acquisition has a new image ready, and when that happens + update napari appropriately + """ + while True: + lf_dataset = Dataset(lf_dataset_path) + ls_dataset = Dataset(ls_dataset_path) + + lf_data = lf_dataset.as_array() + ls_data = ls_dataset.as_array() + if _verbose: print(f'LF data shape: {lf_data.shape}, LF data shape: {ls_data.shape}') + + yield (lf_data, ls_data) + + if time.time() - t_start < 60: + if _verbose: print('waiting 1 sec') + time.sleep(5) + elif time.time() - t_start < 5 * 60: + if _verbose: print('waiting 15 sec') + time.sleep(15) + elif time.time() - t_start < 60 * 60: + if _verbose: print('waiting 2 min') + time.sleep(2 * 60) + else: + if _verbose: print('waiting 10 min') + time.sleep(10 * 60) + + if _verbose: print('Closing datasets') + lf_dataset.close() + lf_dataset.close() + + +@click.command() +@click.argument( + "lf_dataset_path", + type=click.Path(exists=True, file_okay=False), +) +@click.argument( + "ls_dataset_path", + type=click.Path(exists=True, file_okay=False), +) +def run_viewer(lf_dataset_path, ls_dataset_path): + napari_signaller(lf_dataset_path, ls_dataset_path) + napari.run() + +if __name__ == '__main__': + run_viewer() From 25527b78dbe829d111475b5859d2de6f62306c82 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 2 May 2023 14:58:34 -0700 Subject: [PATCH 3/4] refactor cli --- mantis/acquisition/viewer.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/mantis/acquisition/viewer.py b/mantis/acquisition/viewer.py index f52792b1..7a8b94fd 100644 --- a/mantis/acquisition/viewer.py +++ b/mantis/acquisition/viewer.py @@ -1,4 +1,5 @@ import click +import os from pycromanager import Dataset import napari import time @@ -59,19 +60,23 @@ def napari_signaller(lf_dataset_path, ls_dataset_path): if _verbose: print('Closing datasets') lf_dataset.close() - lf_dataset.close() + ls_dataset.close() @click.command() @click.argument( - "lf_dataset_path", - type=click.Path(exists=True, file_okay=False), -) -@click.argument( - "ls_dataset_path", + "dataset_path", type=click.Path(exists=True, file_okay=False), ) -def run_viewer(lf_dataset_path, ls_dataset_path): +def run_viewer(dataset_path): + dirname, basename = os.path.split(dataset_path) + + lf_dataset_name = '_'.join(basename.split('_')[:-1]) + '_labelfree_1' + lf_dataset_path = os.path.join(dirname, basename, lf_dataset_name) + + ls_dataset_name = '_'.join(basename.split('_')[:-1]) + '_lightsheet_1' + ls_dataset_path = os.path.join(dirname, basename, ls_dataset_name) + napari_signaller(lf_dataset_path, ls_dataset_path) napari.run() From 2597e9e9ba66209306d5b1b6ea203a82e0adab3f Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Tue, 2 May 2023 15:03:52 -0700 Subject: [PATCH 4/4] style --- mantis/acquisition/viewer.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/mantis/acquisition/viewer.py b/mantis/acquisition/viewer.py index 7a8b94fd..051eb656 100644 --- a/mantis/acquisition/viewer.py +++ b/mantis/acquisition/viewer.py @@ -1,9 +1,11 @@ -import click import os -from pycromanager import Dataset -import napari import time + +import click +import napari + from napari.qt import thread_worker +from pycromanager import Dataset _verbose = False LF_LAYER_NAME = 'label-free data' @@ -12,6 +14,7 @@ viewer = napari.Viewer() t_start = time.time() + def update_layers(data): """ update the napari layer with the new image @@ -29,6 +32,7 @@ def update_layers(data): viewer.add_image(lf_data, name=LF_LAYER_NAME) viewer.add_image(ls_data, name=LS_LAYER_NAME) + @thread_worker(connect={'yielded': update_layers}) def napari_signaller(lf_dataset_path, ls_dataset_path): """ @@ -41,24 +45,30 @@ def napari_signaller(lf_dataset_path, ls_dataset_path): lf_data = lf_dataset.as_array() ls_data = ls_dataset.as_array() - if _verbose: print(f'LF data shape: {lf_data.shape}, LF data shape: {ls_data.shape}') + if _verbose: + print(f'LF data shape: {lf_data.shape}, LF data shape: {ls_data.shape}') yield (lf_data, ls_data) if time.time() - t_start < 60: - if _verbose: print('waiting 1 sec') + if _verbose: + print('waiting 1 sec') time.sleep(5) elif time.time() - t_start < 5 * 60: - if _verbose: print('waiting 15 sec') + if _verbose: + print('waiting 15 sec') time.sleep(15) elif time.time() - t_start < 60 * 60: - if _verbose: print('waiting 2 min') + if _verbose: + print('waiting 2 min') time.sleep(2 * 60) else: - if _verbose: print('waiting 10 min') + if _verbose: + print('waiting 10 min') time.sleep(10 * 60) - if _verbose: print('Closing datasets') + if _verbose: + print('Closing datasets') lf_dataset.close() ls_dataset.close() @@ -80,5 +90,6 @@ def run_viewer(dataset_path): napari_signaller(lf_dataset_path, ls_dataset_path) napari.run() + if __name__ == '__main__': run_viewer()