Skip to content

new public API - #574

Merged
almarklein merged 115 commits into
imageio:masterfrom
FirefoxMetzger:v3.0.0
Jun 15, 2021
Merged

almarklein merged 115 commits into
imageio:masterfrom
FirefoxMetzger:v3.0.0

Conversation

@FirefoxMetzger

@FirefoxMetzger FirefoxMetzger commented Dec 1, 2020 •

Copy link
Copy Markdown
Contributor

Closes: #569 (Discussion for a new public API in v3.0.0)

As a first step towards the new API, I am putting together a PR to test/demonstrate backward compatibility. The idea is to introduce both, the imopen factory as well as a legacy plugin. This plugin serves two functions:

  1. it is called by the old API for backward compatibility,
  2. it acts as a fallback to interface with legacy plugins while existing plugins are being updated to take advantage of the new API design.

We could introduce the new API in v2.10.0 and default to the old API with an optional switch into the new API. Starting with v3.0.0 the new API would replace the old one. How much of the old API we want to keep around at that point is open for discussion.

For future reference, a link to a comment in this issue that I keep citing: #574 (comment)

@FirefoxMetzger

Copy link
Copy Markdown
Contributor Author

At the moment, I just pipe everything through the new imopen.read() and imopen.write(). Tomorrow (or so) I will refactor the code further to have mimread and mvolread use imopen.iter().

@almarklein We didn't discuss the replacement for mvolwrite in the issue. Will we ask users to call .write() multiple times to write a list of images, or where should it live?

@FirefoxMetzger

Copy link
Copy Markdown
Contributor Author

There it is :) This should pump the entire public API through imopen and expose imopen with an API as discussed in #569

imopen is a glorified singleton factory (like FormatManager). When called it tries to find the correct plugin and returns an instance of the first plugin that can read and write the given format (wip).

If called with api="legacy" (maybe legacy=True is better?) it will skip the matching and instead instantiate a LegacyPlugin, which is a wrapper around the legacy backend. LegacyPlugin will then perform matching (if necessary).

@almarklein

Copy link
Copy Markdown
Member

Thanks! It's interesting to see the code move into the direction that we discussed!

@almarklein

Copy link
Copy Markdown
Member

I also like the idea of a wrapper plugin to be able to keep the old plugin system for the time being. Should help make the transition more smooth.

Comment thread imageio/core/format.py Resolved
Comment thread imageio/core/functional_api.py Resolved
Comment thread imageio/core/imopen.py Resolved

@rfezzani rfezzani left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here is my first round, very promising 😉! Thank you @FirefoxMetzger

Comment thread imageio/core/imopen.py Resolved
Comment thread imageio/core/imopen.py Resolved
Comment thread imageio/core/imopen.py Resolved
Comment thread imageio/core/imopen.py Resolved
Comment thread imageio/core/imopen.py Resolved
Comment thread imageio/core/imopen.py Resolved
@FirefoxMetzger

Copy link
Copy Markdown
Contributor Author

As far as I can see, there are 4 points we need to solve for this PR before we can seriously look at merging it:

  1. (discussion) We need to decide if we should use a plugin manager as @rfezzani suggested.
  2. (discussion) We need to decide if we should keep a Singleton or if we want to use class variables.
  3. (implementation) I would like to sink the legacy plugin deeper into the legacy codebase. I was thinking that it might be possible to remove FormatManager and wrap plugins directly. @almarklein You know the API best. If you think this is possible (and useful) could you give me a list of things I need to look at for that? I can do the implementation, but some guidance from your side will be useful here.
  4. (implementation) I would like to upgrade at least one plugin to the new API so that we can run and test those code paths, Anyy suggestions which one to migrate?

@almarklein

Copy link
Copy Markdown
Member

I would like to upgrade at least one plugin to the new API

The Pillow plugin would make sense, I think.

I wonder if we can take smaller steps. Doing the whole transition in a single PR would become such a big change that it wont be possible to oversee it. My suggestion is that in this PR the foundations are made to start the transition process. Then transform plugins one at a time in different PR's.

@FirefoxMetzger

Copy link
Copy Markdown
Contributor Author

@almarklein Absolutely. I don't intend to convert all plugins in this PR. My reasoning behind having one is that it might be good to have an example plugin (a) for reference, (b) to not "dream up" documentation on how to write plugins but to write it based on working code, and (c) to be able to write meaningful unit tests for the plugin manager.

@almarklein

Copy link
Copy Markdown
Member

Mmm, yeah that makes sense.

I updated the test to see if the problem really comes from the legacy reader being
destroyed. (It works fine in CPython, but breaks in pypy;
I don't have pypy)
@FirefoxMetzger

Copy link
Copy Markdown
Contributor Author

Solved :) It's not my prettiest bugfix, but I think it should do the trick.

What I'm doing now is that I pass uri and io_mode from imopen into legacy plugins in addition to the actual request object. I use the request object where possible, but for get_reader and get_writer specifically, I create a new (throw-away) request that can be closed by the reader/writer without affecting the plugin itself.

@FirefoxMetzger

FirefoxMetzger commented May 17, 2021 •

Copy link
Copy Markdown
Contributor Author

From my side, I am happy with this PR.

The codecov/patch test fails only because of legacy_pillow.py, which I only renamed (but git tracks it as a new file), so I am not concerned about it.

Functional/API documentation is written; however, more documentation and usage examples still need to be done. I would do these in a separate PR though, because this one is becoming so massive that it became hard to review. I'd rather add further refactoring and documentation in a series of small PRs...

@almarklein Let me know what you think, and what should be added from your side to get this PR merged.

Comment thread imageio/core/request.py Resolved
Comment thread imageio/plugins/pillow.py
arr = arr.astype(np.uint8)
mode = mode_base = "L"

except UnidentifiedImageError:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we can simply use the code from can_read and can_write here. As it is now, every image is opened twice.

Also, let's raise a more specific error when a plugin cannot process a request. NotImplementedError seems suited, or perhaps a custom error based on it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I ended up going with an exception instead of can_read/can_write, because you mentioned in a comment that plugins should raise an exception if they can't handle a request ( #574 (comment) ). It follows the pythonic mantra of "better ask for forgiveness than for permission". Personally, I like the idea from an API design perspective: A plugin just handles the request, and if that fails it screams .... loudly.

As it is now, every image is opened twice.

Maybe I misunderstood something, but in my mind the image file is handled by the stateful request object and will be kept open. We hand the same request to every plugin and, once bytes are read, they will remain in a memory buffer; this should be very fast to read.

Also, let's raise a more specific error when a plugin cannot process a request. NotImplementedError seems suited, or perhaps a custom error based on it.

I was thinking about this, too, when I was writing that code.

I first had a NotImplementedError, but I think doesn't match what is happening. It's not that the code to read/write/do-stuff isn't implemented - chances are it will never be implemented -, but rather that the request can not be handled (e.g. we are trying to open a .mp4 using pillow). Essentially, the input value passed to the constructor function is not valid, so I ended up choosing ValueError. I'm happy to raise a different error though if you think it makes more sense.

I have a personal thing against custom errors. I've never used them in a real project, and when I've encountered them they could have likely been refactored into a standard error + a good error string/message. We can introduce them, but I don't really understand how they add anything over StandardError("This is what went wrong.").

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, I just realized I may have misunderstood you. Do you mean using legacy_pillow's can_read/can_write to check if pillow is compatible?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry for not being more clear. I meant using the code in the old canread/canwrite to better deterine when to raise an error here :)

Maybe I misunderstood something, but in my mind the image file is handled by the stateful request object and will be kept open.

Yes you're right. I was triggered by the use of get_file. Ideally the code that determines whether or not a file can be dealt with should only use getfirstbytes. Think e.g. of non-seekable files.

In general I am opposed to custom errors too. But in this case we really want to distinguish the plugins I-cannot-handle-this signal from something actually going wrong. E.g. with the ValueError now, it's very easy for a bug in the plugin to raise ValueError and the format manager to therefore consider that the plugin cannot deal with the request. And the user will never know.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Regarding get_file, I can use can_read/can_write code instead. It feels a bit like we are digging in pillow's internals here, but I will see if I can refactor things.


Regarding the error, I'll change it to a custom error and we can see how things play out.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok, in this case I will leave the seek(0) behavior for another PR. I tried it just now and it looks like I need to dig into FreeImage and DICOM to see why their tests break if I reset the file before handing it over to LegacyPlugin. I would want to do this in a new PR that is a little more reviewable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've added the _can_write logic to pillow's constructor. There is one change I need to make: the elif should be an else. Afterward coverage should go up to 100% for pillow again. Otherwise, the changes of today should conclude this thread as well.

This seems to be the last open point for this PR. If there is anything else from your side @almarklein please feel free to comment, otherwise I'd say we summarize in the main thread and then get this thing merged :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I forgot where we are now on the rewinding/first_bytes/auto-caching stuff, but maybe we should just cover that in subsequent prs :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Anything else you want to do / summarize before I hit that big green button?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I forgot where we are now on the rewinding/first_bytes/auto-caching stuff, but maybe we should just cover that in subsequent prs :)

Yes, that's why I don't want to add more stuff here and start adding smaller chunks in dedicated PRs.

My last stand is that the best approach would be to refactor Request to have sole ownership of the raw file. It reads the file into memory on demand (maybe we can use python's mmap module here) and upon get_file() it serves a new read-only file object to the plugin which is destroyed once the plugin is done with it. In there, plugins get to do what they want and we can optimize serving cached content vs content read on-demand based on file size and and access frequency, etc.

Comment thread imageio/core/imopen.py Resolved
Comment thread imageio/core/imopen.py Resolved
@FirefoxMetzger

Copy link
Copy Markdown
Contributor Author

Anything else you want to do / summarize before I hit that big green button?

From the top of my head, here are additional things that we need to sort out after this one is merged:

  • Add documentation beyond functional docs to showcase the new API (both user-side (!) and plugin-writing side).
  • Add a feature that reorganizes the order in which files are checked based on the file's extension.
  • Refactor Request to virtualize the file object. The idea is to enable better caching and prevent plugins from influencing each other.
  • Solve the problem of having handles to open files outside a context manager. Currently instance = iio.imopen(...) -> instance stores request, which has an active/open handle to an unclosed file.
  • Setup: 'formats' for all user-facing extras #639 hinted at adding additional backends, so we should check if we can add them using the new API

Other stuff that should happen sooner or later:

  • test the new API some more
  • release imageio 2.10 (maybe set up a CD pipeline via GH actions in the process)
  • refactor scikit-image.io to be a thin wrapper around the new API

This is all stuff for new issues / new PRs, so from my side I think I am happy to get this finalized.

@almarklein Should I create issues for each point mentioned above?

@almarklein
almarklein merged commit 896aa50 into imageio:master Jun 15, 2021
@almarklein

Copy link
Copy Markdown
Member

Should I create issues for each point mentioned above?

Yes, that would be good!

@almarklein

Copy link
Copy Markdown
Member

Thanks for this massive work!

@FirefoxMetzger
FirefoxMetzger deleted the v3.0.0 branch June 15, 2021 09:57
@FirefoxMetzger FirefoxMetzger mentioned this pull request Jun 22, 2021
12 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Design for a new public API, take 2

3 participants