Repository navigation
Description
@jni mentioned this post, and that got me thinking ...
This would be quite a bit change, and some things (cough meta data) are more urgent. However, it might be that the required changes in metadata break compatibility in some ways, making it easier to roll these changes out together?
I'd like to consider a new way to specify plugins. These plugins will not be required to inherit from imageio.core.Format, nor will they need to be registered at runtime. This means that libs can specify imageio formats without needing to depend on imageio. If we use entriepoints to let libs advertise their "imageio formats", then imageio can detect the format even if the lib is not imported (though it must be installed).
When a user reads a file, imageio can query possible formats and auto-import these formats as needed. That way, imageio stays light, while the number of supported formats can be increased in a very scalable way (in terms of memory, but also maintenance).
Further, we can keep a dict of common formats specified in 3d party libraries, so that when a user tries to read a file type for which no format is available on the system, imageio can suggests the installation of a library.
Imageio can then spin off some of its plugins into separate libraries, and mark them as dependencies in setup.py. These formats are then still always available, but are only loaded when needed.
Note, that with a little bit of work, we can also delay the import of plugins as things currently are ...
Preferably, this format should be generic, so it can also be consumed by other tools. Not sure if this is feasible though.
Info that we need (based on the current format system):
- Name and docs
- Common extensions
- Supported image modes (2d, 3d, multi-image)
- A way to tell whether the plugin can read the data, based on the first n bytes
- a way to get meta data
- a way to read image data (and meta data)
- a way to write image and meta data
Example:
class FooFormat:
"""
Docs
"""
extensions = ".foo", ".bar"
modes = "ri", "rI", "wi" # read images and sequences, write single images
modes_alt = "r2", "r2m", "w2" # maybe use numbers for dimension, and 'm' for multi?
def __init__(self, request, mode=''):
# Imageio will pass a request object here, via which we can
# obtain a file object, or a local filename, whatever we need.
# To make this more generic, we should also support plain file
# objects.
if not can_handle(request.firstbytes, mode):
raise NotImplementedError("This format can not read/write this data")
def get_meta(self, index=None):
# Return the metadata of the file or the specified frame.
# Formats are expected to provide metadata with standardized
# fields.
pass
def read(self, index=None):
# Read image and meta data of that image. If index is None,
# read whatever is next. Raise StopIteration if there is no
# more data.
pass
def write(self, image, meta):
# Formats are expected to handle the meta data in the way that
# imageio defines it, e.g. discarting certain elements.
pass
def __iter__(self):
# Formats that want to support iteration, can simply do this
return FormatIterator(self)
class FormatIterator:
def __init__(self, format):
self.format = format
def __len__(self):
return sys.maxsize
def __next__(self):
return self.format.read()So, not too different from how things currently are ... but quite a bit simpler.
Activity
jni commentedon Nov 6, 2020
@almarklein
This is definitely my view!
Everything else seems great here.
You could have a look at the way napari defines IO plugins here:
https://napari.org/docs/dev/plugins/index.html
it's very similar to the above but also uses napari-plugin-engine, which is just a fork of (pytest's) pluggy, to discover and execute plugins.
FirefoxMetzger commentedon Jun 19, 2021
@almarklein Since we have the new API now, would you think this question has been solved? If not, what is still missing?
almarklein commentedon Jun 21, 2021
Yeah, it looks like you've covered this for the most part! Just a few question when comparing the above with https://github.com/imageio/imageio/blob/master/imageio/plugins/pillow.py#L85
open()function really necessary? If a plugin is successfully initialized, will the next step not always be to open the image?FirefoxMetzger commentedon Jun 21, 2021
Yep, after I am done with #648 I will go and write the docs for the v3 API and with that some docs for the pillow plugin.
I am a bit undecided on the supported extensions/formats part. In theory it is very good if we can provide that list; in practice, it is difficult to maintain, because are in constant motion which make our list backend-version dependent and quickly out-date it, too. My current idea is to have a big list of supported formats (see #648 "Supported Formats") and list each plugin/backend that supports it there. This would come with a disclaimer that, if in doubt, one should double-check with the backend docs regarding actual format support, due to the above mentioned concerns regarding backend versions.
The plugin selection logic is independent of this, as it is essentially just a re-ordering of plugins for speed. We still test all plugins each time, but stop as soon as we find a matching one. We try to have a smart search order based on file extension (and/or other features).
You mean
iio.imopen? I think it follows the same logic as having the genericopenbuild-in in python. Sometimes, you want to open the file and keep it around, e.g. because you are writing images to it after some processing, or you want to read certain images on demand without going through the overhead of talking to the OS to open a file each time.I think I might not understand the question correctly though x)
almarklein commentedon Jun 22, 2021
Yeah, but I mean for this to work, the plugin class needs to advertise the extension that it (likely) supports. Perhaps we can use this data to generate that table you mentioned. Yes it's some maintenance to keep these data up-to-date, but I won't expect most backends to change their supported extensions much. Plus its probably not a big deal if we're a bit behind.
I meant the
.open()method of the plugin :)''
FirefoxMetzger commentedon Jun 22, 2021
Fair point. I didn't think very deeply about the discovery of extensions yet. Thinking about it now, we likely want to have a way to maintain priority for plugins irrespective of the order in which they are loaded. Plugins aren't aware of other plugins so this is something core needs to take care of. That said, a solution might be to have a mechanism for core to query a plugin regarding its supported extensions.
I agree that it is worth maintaining. I'm not sure if the added effort of automating this will be worthwhile though, or if it won't be simpler to make the list once and then do incremental upgrades as new plugins become available or backends change their support.
Hm, yes, I don't think it is necessary to force a plugin to have it, and it won't be needed in most cases. That said, I don't see a downside if a plugin chooses to have it.
I added it in the pillow plugin before we decided to re-introduce the Request object which now handles most of the actual IO, so we could refactor it. Do you want to make a PR?
almarklein commentedon Jun 22, 2021
I meant it as a way to not duplicate these efforts. If each plugin has something like:
Then this is what the plugin selection logic could use, and what we could maintain. Then generate the table from these. Maybe you meant that just also keeping track of a table is less effort than writing/maintaining the code to generate it? In which case you could be right.
almarklein commentedon Jun 22, 2021
Ok, let's see how it works out as we add more plugins. I might play with it and create a PR, but at the moment I am swamped.
FirefoxMetzger commentedon Jun 22, 2021
Yep, that's what I was thinking.
FirefoxMetzger commentedon Jun 30, 2021
@almarklein You mentioned two open points further up in the issue
I couldn't quite work out if they are fully resolved or not. Is everything clear? If not, what actions are needed to make it clear?
almarklein commentedon Jun 30, 2021
Made #654 for
open(). The other issue is something we'll work out when the plugin selection logic is made smarter.