Skip to content

Design for a new public API, take 2 #569

Closed

Description

@almarklein

In #563 I made a first proposal for a new API. There were some interesting discussions, which have lead to some new insights, e.g. that we may be able to pull this off without the mode argument. Here's a the tweaked version.

The base API is similar to what I proposed earlier. It provides access to a plugin object, providing complete functionality in a concise way, and supports keeping the file open to support random/lazy access. As a consequence, use of the with-statement is recommended.

# Get a read object (this is an instance of the actual plugin class for the appropriate format)
r = imopen(filename, mode='r/w')   # default mode is 'r'

# Read an image
with imopen(...) as r:
    im = r.read()

# Write an image
with imopen(..., mode='w') as w:
    w.write(im)

# Read image and meta data
with imopen(...) as r:
    meta = r.get_meta()
    im = r.read()

# Read an image from a series (sampling in the first dimension)
# Some formats (e.g. video) may require the user to either this or use the iterator
with imopen(...) as r:
    im = r.read(index)

# Iterate over the images in a sequence
with imopen(...) as r:
    for im, meta in r.iter():
        pass

Since forcing users to use a with statement everywhere does not seem like a good idea, so let's add convenient function(s):

# This one at the least. 
im = imread(...)

# Perhaps this too, but already more special.
for im in imiter(...):
    pass  # do stuff with the image

# Is this common enough to warrant a convenience function?
imwrite(filename, im)

With regards to backwards compatibility, we have a bit of a problem because we're introducing a new imread function with a different behavior. Also the API is very similar, but still different in subtle ways.

At the very least we should put the API in a new namespace, and since we're also contemplating a new plugin system, it may be worth considering creating a brand new library.

Thoughts?

cc @jni

Activity

  1. FirefoxMetzger commented on Nov 20, 2020

    @FirefoxMetzger
    Contributor

    Two thoughts:

    1. Would bumping the version major suffice instead of having to do a brand new library? At first glance, it seems possible to write wrapper code for both, the old API calling the new one (with deprecation warning), and the new API calling legacy plugins (not 100% sure on this one) and then successively update the codebase.

    2. @lschr has proposed a sliceable API in Add sliceable interface class akin to PIMS #564 and the decision there is/was that this should live in his package for now, with imageio referring to it. Could the same thought apply to the simplified / no with-statement API and skimage? skimage already has discussions on writing a (razor-)thin wrapper around imageio. (sneakily looking at @jni )

    The new imopen API seems like a library mainly targeting software engineers for use in packages; not so much by people scripting or hacking something together. In this context enforcing code quality and the use of the with statement to make resource usage explicit actually seems like a good idea. It is a little more verbose, but being explicit here may help with long-term maintenance (and allows lazy-loading, etc.).

    If you are primarily scripting/hacking instead of (software) engineering, then your focus likely isn't I/O anyway, and it is but a means to enable other code (likely another library/package) to consume an image. That package will then know how to best expose and consume the image. To me, it seems logical to let that package depend on imageio and decide how to expose the reading/writing mechanism similar to what Ischr is doing already.

  2. almarklein commented on Nov 20, 2020

    @almarklein
    MemberAuthor
    1. Would bumping the version major suffice instead of having to do a brand new library?

    I started writing a response with pro's and con's, but I'm starting to feel that the answer is: Yes.

    The thing is, I mentioned that the fact that we're changing the behavior of imread is a problem for backwards compatibility. However, the current implementation of imread() can be considered broken, because it may sometimes not read all the available data. Unless I'm mistaken, the only cases where the new imread would behave differently from the old, is in such cases where data would be omitted. So we should change it anyway!

    I'm not 100% sure whether we can make the other API functions behave exactly the same, but probably close enough for by far the most cases.

    1. [...] Could the same thought [moving a higher level API outside of imageio] apply to the simplified / no with-statement API and skimage?

    In theory yes, but I think that having a simple API in imageio is valuable, otherwise there is a risk that each image processing lib implements its own slightly different version of imread.

    The new imopen API seems like a library mainly targeting software engineers for use in packages

    Yes, and users who need a bit more, e.g. metadata. I feel this is another reason to keep the API's close together - to make it easy for someone using imread to transition to using imopen. That should not feel like a big step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions