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

Base ImageIOReader on FramesSequenceND #320

Merged
merged 10 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
build/*
dist/*
pims.egg-info/*
PIMS.egg-info/*
pims/version.py
pims/loci_tools.jar
examples/.ipynb_checkpoints
Expand Down
25 changes: 25 additions & 0 deletions pims/base_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,28 @@ def __repr__(self):
s += "Axis '{0}' size: {1}\n".format(dim, self._sizes[dim])
s += """Pixel Datatype: {dtype}""".format(dtype=self.pixel_type)
return s

def _init_axis_if_exists(self, axis, size, min_size=1):
rbnvrw marked this conversation as resolved.
Show resolved Hide resolved
if size >= min_size:
self._init_axis(axis, size)

def _guess_default_iter_axis(self):
"""
Guesses the default axis to iterate over based on axis sizes.
Returns:
the axis to iterate over
"""
priority = ['t', 'z', 'c', 'v']
rbnvrw marked this conversation as resolved.
Show resolved Hide resolved
found_axes = []
for axis in priority:
try:
current_size = self.sizes[axis]
except KeyError:
continue

if current_size > 1:
return axis

found_axes.append(axis)

return found_axes[0]
28 changes: 26 additions & 2 deletions pims/imageio_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import six

from pims.base_frames import FramesSequence
from pims.base_frames import FramesSequenceND
from pims.frame import Frame

try:
Expand All @@ -16,8 +16,12 @@ def available():
return imageio is not None


class ImageIOReader(FramesSequence):
class ImageIOReader(FramesSequenceND):
class_priority = 6

propagate_attrs = ['frame_shape', 'pixel_type', 'metadata',
'get_metadata_raw', 'reader_class_name']

@classmethod
def class_exts(cls):
return {'tiff', 'bmp', 'cut', 'dds', 'exr', 'g3', 'hdr', 'iff', 'j2k',
Expand All @@ -31,6 +35,9 @@ def class_exts(cls):
def __init__(self, filename, **kwargs):
if imageio is None:
raise ImportError('The ImageIOReader requires imageio to work.')

super(self.__class__, self).__init__()

self.reader = imageio.get_reader(filename, **kwargs)
self.filename = filename
self._len = self.reader.get_length()
Expand All @@ -39,6 +46,23 @@ def __init__(self, filename, **kwargs):
self._shape = first_frame.shape
self._dtype = first_frame.dtype

self._setup_axes()
self._register_get_frame(self.get_frame, 'tyx')
rbnvrw marked this conversation as resolved.
Show resolved Hide resolved

def _setup_axes(self):
"""Setup the xyctz axes, iterate over t axis by default

"""
self._init_axis_if_exists('x', self._shape[1])
self._init_axis_if_exists('y', self._shape[0])
self._init_axis_if_exists('t', self._len)

if len(self.sizes) == 0:
raise EmptyFileError("No axes were found for this file.")

# provide the default
self.iter_axes = self._guess_default_iter_axis()

def get_frame(self, i):
frame = self.reader.get_data(i)
rbnvrw marked this conversation as resolved.
Show resolved Hide resolved
return Frame(frame, frame_no=i, metadata=frame.meta)
Expand Down