Skip to content

Commit

Permalink
Merge pull request #6 from DragaDoncila/improve-reliability
Browse files Browse the repository at this point in the history
Slice opens file each time and gets correct timepoint
  • Loading branch information
DragaDoncila authored Nov 8, 2020
2 parents b6c7167 + 45586ce commit bd92fe4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
63 changes: 38 additions & 25 deletions nd2_dask/nd2_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ def get_pims_nd2_vol(nd2_data, c, frame):
return v


def get_nd2reader_nd2_vol(nd2_data, c, frame):
nd2_data.default_coords['c']=c
nd2_data.bundle_axes = ('z', 'y', 'x')
v = nd2_data.get_frame(frame)
v = np.array(v)
def get_nd2reader_nd2_vol(path, c, frame):
with ND2Reader(path) as nd2_data:
nd2_data.default_coords['c']=c
nd2_data.bundle_axes = ('z', 'y', 'x')
v = nd2_data.get_frame(frame)
v = np.array(v)
return v

@napari_hook_implementation(specname="napari_get_reader")
# @napari_hook_implementation(specname="napari_get_reader")
def napari_get_pims_reader(path):
"""Returns reader if path is valid .nd2 file, otherwise None
Expand Down Expand Up @@ -82,7 +83,7 @@ def napari_get_nd2_reader(path):
return nd2_reader


def pims_reader(path):
def pims_reader(path, max_retries=30):
"""Take a path or list of paths and return a list of LayerData tuples.
Readers are expected to return data as a list of tuples, where each tuple
Expand All @@ -104,14 +105,26 @@ def pims_reader(path):
Both "meta", and "layer_type" are optional. napari will default to
layer_type=="image" if not provided
"""
nd2_data = ND2_Reader(path)
channels = [nd2_data.metadata[f"plane_{i}"]["name"] for i in range(0, nd2_data.sizes['c'])]
n_timepoints = nd2_data.sizes['t']
frame_shape = nd2_data.frame_shape
frame_dtype = nd2_data.pixel_type
nd2vol = tz.curry(get_pims_nd2_vol)
channel_dict = dict(zip(channels, [[] for _ in range(len(channels))]))
layer_list = get_layer_list(channels, nd2vol, nd2_data, frame_shape, frame_dtype, n_timepoints)

tries = 0
while tries < max_retries:
try:
nd2_data = ND2_Reader(path)
channels = [nd2_data.metadata[f"plane_{i}"]["name"] for i in range(0, nd2_data.sizes['c'])]
n_timepoints = nd2_data.sizes['t']
frame_shape = nd2_data.frame_shape
frame_dtype = nd2_data.pixel_type
nd2vol = tz.curry(get_pims_nd2_vol)
channel_dict = dict(zip(channels, [[] for _ in range(len(channels))]))
layer_list = get_layer_list(channels, nd2vol, nd2_data, frame_shape, frame_dtype, n_timepoints)

for (data, _, _) in layer_list:
shape = np.asarray(data[0]).shape
break
except Exception as e:
tries += 1
if tries > max_retries:
raise e

return layer_list

Expand All @@ -138,14 +151,14 @@ def nd2_reader(path):
Both "meta", and "layer_type" are optional. napari will default to
layer_type=="image" if not provided
"""
nd2_data = ND2Reader(path)
channels = nd2_data.metadata['channels']
n_timepoints = nd2_data.sizes['t']
z_depth = nd2_data.sizes['z']
frame_shape = (z_depth, *nd2_data.frame_shape)
frame_dtype = nd2_data._dtype
nd2vol = tz.curry(get_nd2reader_nd2_vol)
layer_list = get_layer_list(channels, nd2vol, nd2_data, frame_shape, frame_dtype, n_timepoints)
with ND2Reader(path) as nd2_data:
channels = nd2_data.metadata['channels']
n_timepoints = nd2_data.sizes['t']
z_depth = nd2_data.sizes['z']
frame_shape = (z_depth, *nd2_data.frame_shape)
frame_dtype = nd2_data._dtype
nd2vol = tz.curry(get_nd2reader_nd2_vol)
layer_list = get_layer_list(channels, nd2vol, path, frame_shape, frame_dtype, n_timepoints)

return layer_list

Expand All @@ -159,12 +172,12 @@ def get_layer_list(channels, nd2_func, nd2_data, frame_shape, frame_dtype, n_tim
dtype=frame_dtype)
for j in range(n_timepoints)]
)
channel_dict[channel] = dask.optimize(arr)
channel_dict[channel] = dask.optimize(arr)[0]

layer_list = []
for channel_name, channel in channel_dict.items():
add_kwargs = {
"scale": [1, 1, 1, 4],
"scale": [1, 1, 1, 1],
"name": channel_name,
"visible": channel_name == "Alxa 647"
}
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
napari-plugin-engine>=0.1.4
numpy
nd2reader
pims-nd2
dask
toolz
toolz

0 comments on commit bd92fe4

Please sign in to comment.