Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVP of napari viewer #30

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions examples/example_viewer.py
Original file line number Diff line number Diff line change
@@ -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()
95 changes: 95 additions & 0 deletions mantis/acquisition/viewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import os
import time

import click
import napari

from napari.qt import thread_worker
from pycromanager import Dataset

_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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be slightly faster to check the key with something like:

if LF_LAYER_NAME not in viewer.layers:
    viewer.add_image(...)
else:
    ...
if LS_LAYER_NAME not in viewer.layers:
    ...
else:
    ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these be logging.debug instead?

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()
ls_dataset.close()


@click.command()
@click.argument(
"dataset_path",
type=click.Path(exists=True, file_okay=False),
)
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()


if __name__ == '__main__':
run_viewer()