Repository navigation
Closed
Labels
Description
This is a meta issue about meta data - in this top post I try to keep track of the current status. If you want to comment, see if there already is an issue related to your question/comment, open a new issue, or comment below.
Related issues
- SKIP: The way
skimageprocesses and stores metadata scikit-image/scikit-image#2605 - basically the skimage equivalent of this issue
Goals
- Provide the user with a small subset of meta data with standardized fields and values.
- Provide the user with the full original raw metadata (differs per format).
- Make it possible for the user to read both of the above without actually reading the image data (where the format allows it).
- Make it possible to provide both standardized and format-specific metadata when writing images.
Questions
- What USE CASES are there that we must consider? Let us know if you have a use-case that we're not covering yet! (see Meta data use cases #501)
- What fields / values should the standardized metadata have (see Standardized meta data fields #382)?
- How to deal with different units in metadata values (part of Standardized meta data fields #382)?
- Should imageio return
(image, meta)instead of having the meta data as a property (Consider returning a normal numpy array + meta data #503)? - Should imageio report RGB vs HSV, or always convert to gray/RGB/RGBA (which I think is more or less what happens)? (How should imageio deal with colorspaces? #504)
- What should the metadata dict look like (The shape of the new metadata object #502)?
Current status of imageio
- The read functions return an
imageio.Imageobject (subclass ofndarray) which has a.metadict attached. - We have
Reader.get_meta_data()but it does not have standard elements, so is usually not very helpful (Inspecting shape, dtype, etc. #263). - The (generic) Writer class allows giving meta data explicitly, and also checks for the
.metaattribute. - The high level public API (e.g.
imwrite()) does not have a way to provide meta data explicitly.
The plan / tasks
- Rename and better docs for the
ImageArrayclass (done in Rename the array subclass and clarify docs #375, solving Clarification regarding object returned by imread #333) - Document that code that processes images should probably not change its behavior based on the meta data itself, but only from args/kwargs that the user may set using the meta data.
- Define and implement standardized field names: Standardized meta data fields #382, The shape of the new metadata object #502
- Better support for getting and setting meta data: Make it easier to get and set meta data #383
- Implement meta data standardized fields on reading and writing for all plugins ... (this is probably where most of the work will go).
Activity
lschr commentedon Aug 6, 2018
One could also use an approach similar to https://github.com/soft-matter/slicerator:
Imagethat contains a list of attributes that should be copied after image processing, e.g._propagate_attrsto the result array.(not sure if above code actually works, but I think you get the idea). This is a very general approach; one could also limit it to checking whether a
metaattribute exists and if so, copy it.almarklein commentedon Aug 7, 2018
I can see how such an approach would make it easier to work with a custom
Imageclass. The thing is thatimageiodoes not have any processing functions itself, andskimagegot rid of using a custom class some years ago.The purpose of this issue is to find a way to make meta data more (generically) useful (e.g. by standardizing certain info), let it work well with whatever
skimagewill decide on in scikit-image/scikit-image#2605, and along the way, if possible, drop theImageclass too.jakirkham commentedon Aug 9, 2018
What sort of metadata can the builtin plugins currently provide (including stuff that might not be nicely exposed currently)?
almarklein commentedon Aug 10, 2018
@jakirkham That's a good question. I think that it should be possible to inspect
shape,dtypeandnchannelsfor most plugins, though many plugins will need work to do this before the loading of the data. There may also be plugins for which it is not possible to read metadata before reading the image data. What other kinds of generic metadata could we think of?lschr commentedon Aug 10, 2018
I think most formats support some kind of description or comment tag, which would be very helpful to have access to. Also date and time are very common.
lschr commentedon Aug 13, 2018
Additionally, colorspace (what each channel represents; RGB or CMYK …) information may be useful.
jni commentedon Aug 21, 2018
I totally missed this! Hi @almarklein! =) (And hi @jakirkham! =) (@lschr we haven't met but now it feels rude not to say hi. =P 👋 )
Regarding what metadata you could include, for microscopy an absolutely essential one is the pixel spacing. Some microscopes also do tiling and include e.g. stage positioning information, which then aids in mosaicing/registration to get the full image. Sometimes the units of the image data are also useful, e.g. "photons".
Regarding what to do with it, it goes far beyond copying and every function does something different. For example, downsampling requires one to increase the pixel spacing by a corresponding factor. Scaling the image requires adjusting the units. Colorspace conversion requires changing the colorspace. etc.
I think I just talked myself out of handling all this in scikit-image. =P
As to what imageio does with the Image class, from scikit-image's perspective I am agnostic. You can unpack into (array, dict) using
return_meta, or you can keep returningImageand we can unpack it on our end easily enough.I do like the idea of standard metadata fields and having imageio provide them (rather than doing the conversion on skimage's end). I can do a bit of work here both in discussing standard field names, and in providing an appropriate conversion for some formats (notably FEI, for which I wrote the reader).
One question is how to deal with the existing metadata tags that may or may not map to our standard ones. I think we would want to keep them, while still doing the conversion. For example, the FEI format has two layers of annotations, and pixel spacing is under
['Scan']['PixelHeight']and['Scan']['PixelWidth']. (And these are in meters, which is simultaneously sensible and silly. =P) How do we prevent namespace clashes in the metadata hierarchy? I see two options:['imageio'], and if that key exists, check whether it's there from saving a previous imageio standard set, in which case we just keep it, or whether it's someone else's 'imageio' tag, in which case...? This gets messy quickly.Anyway, they're my thoughts for now. Happy to have some friends in this adventure. =D
lschr commentedon Aug 31, 2018
Hey @jni ;-)
After all, I gather it seems best to have an extra dict for metadata. I was thinking, that instead of having something like
return_meta=Trueand changing the return signature accordingly, simply introducing ametaparameter may also be an option. This can either beNone, in which case nothing happens or a user-supplied dictionary to be updated, likeNot sure if this has any advantage; just an idea.
jni commentedon Sep 1, 2018
@lschr Being a functional programming proponent, I'm quite opposed to modifying input arguments, especially in public-facing functions. =)
almarklein commentedon Sep 3, 2018
Things I am starting to converge on (will update the post top accordingly):
Since we have to keep backward compatibility, I feel that any attempt to try and get rid of the
Imageclass will add complexity. Also, as @jni points out, skimage can easily strip&convert. Therefore, I think it's best to keep it, but rename toArrayWithMetaArray, which (I hope) sounds more like andarraysubclass than a wrapper.Imageis kept as an alias. And we should add docs (in the class's docstring and in the higher level docs) on how to convert.We should add an
imageio.metaread()function that will (where possible) return the meta data without reading the image data. We should also add ametaargument to thewrite()functions.Things that need more thoughs and discussion:
It seems evident that we should standardize the meta information. This means that every image comes with its "raw" meta data, as well as a "standardized" meta data. Some information may be duplicate and upon writing, it should be merged to raw meta data again. I think it makes sense to have any standardized fields overwrite the raw fields here. An alternative is to consider the standardized information as read-only and ignore it in the writers (ppl that actively write meta data probably know how to use the raw data).
Its not trivial how to provide these two forms of meta data. As @jni mentioned:
infoattribute on theArrayWithMetaclass). Probably easiest forimageiobut awkward in skimage.jni commentedon Sep 3, 2018
@almarklein
Like, as a monkey-patched attribute? That doesn't seem like something I would describe as "nice and clean"???
jni commentedon Sep 3, 2018
(Overall though, I'm glad we are converging on vaguely the same space, I think!)
almarklein commentedon Sep 3, 2018
@jni, haha no, as having a dict with the standardized data, which has one field called
rawthat contains the raw meta data. As in the other way around as what you proposed.22 remaining items
almarklein commentedon Nov 17, 2021
Good point. I assumed that the unit of the values in
spacingwould be homogeneous. It did not occur to me that some could be spatial units (e.g. mm) and others could be temporal, or even something else. On that note, the channels could also have a relation that can be expressed with some spacing value!almarklein commentedon Nov 17, 2021
Thinking about this more ... I wonder whether we should distinguish here. If I call a skimage function that takes spacing into account, then it will very likely assume that this spacing is spatial. Should we somehow take precautions for this situation? Or is it up to the user to provide a slice that only includes spatial dimensions?
This brings me to another point:
Looks like this brings us full circle to the custom Image class, so we can do this:
FirefoxMetzger commentedon Nov 17, 2021
@jni Ah, so you are proposing a different idea from what we have been discussing so far? This looks like
Imageaims to lazy-load pixels using Dask (ImageIO using Dask for performance improvement), whereas I am saying that Dask can use ImageIO for performance improvement. I might misunderstand your intention though.Dask needs cheap access to
shape+dtypeinformation (what we currently callImageDescriptor). Workers then load the actual pixel data eg. viaiio.imread. From what I understand, however, this loading of pixel data currently happens twice. Once in the main thread to be immediately discarded after having extractedshape+dtype, and once again on the worker thread/node/... The first pixel-data read can often be avoided, and that is what I was trying to achieve withImageDescriptor.I am even more convinced that we aren't currently not talking about the same thing 😅. My idea of
iio.imread(..., out=my_object)is to takemy_object, place the image data into it ("modify it in-place"), and to then returnmy_objectinstead ofndarray. Your suggestion looks like we would getmy_object's buffer, modify it (in-place) and return anImagethat shares the buffer withmy_object.Essentially, what I want to be able to do with
outis something like:The community around skimage is surprisingly homogeneous 😛 and people doing hardware and/or real-time critical work usually think of python as too slow to even try (old habits die hard 🤷). This limits the number of viable guesses considerably 😆
How morning is morning? I am in the gym between 6-8 am (CET) on weekdays. Depending on how long you think we need I can offer 5 am (CET), but I think @almarklein will not be too happy about that.
Otherwise, I think the evening (again CET) will be easier.
I would be careful with functions that self-configure based on image metadata; it sounds a bit like this walks a line bordering magic. Being explicit about where additional metadata is being used will make the code easier to understand (albeit more verbose). It also makes things scalable, because functions don't have to rely on getting an
Imageas input, but can accept anyArrayLikecombined with something that looks like a valid metadata struct.jni commentedon Nov 17, 2021
Buyer beware, imho. 😂
Yes, scikit-image will almost certainly continue to work with external parameters rather than an Image object. Here we would say eg
spacing=im[1, ...].spacingas the second argument...?This seems pointless? You already have my_object. Just do whatever you want to do with it once you've read into it? If you're worried that my
__init__doesn't actually fill the buffer, that's fine, you can doImage(filename, out=my_object).read()?No, it doesn't share a buffer, it uses my_object directly as the _data. As long as my_object satisfies the Array API, that's ok.
At any rate, I'm pretty sure we can satisfy both your and my API requirements in a single API. Let's discuss more live. It'll be nice to meet you! 😃
5am sounds painful even for me on the other end! 😂 I think two reasonable options are 8am Europe/6pm Australia (Or 8:30, dunno how much buffer you want after the gym @FirefoxMetzger!) or 10pm Europe/8am Australia. @almarklein?
almarklein commentedon Nov 17, 2021
8am-8:30 is getting-kids-breakfast-and-to-school time here. 10pm would work for me. Is that doable @FirefoxMetzger ?
almarklein commentedon Nov 17, 2021
I have always been under the impression that one of the goals of this whole metadata exercise was that skimage functions can consume the standardized metadata that comes with an image.
FirefoxMetzger commentedon Nov 17, 2021
@almarklein @jni 10pm CET / 8am ACT it is. Now for the more challenging part. Finding a date :D
Yep, I think on that part we are on the same page (though maybe I'd draw the circle broader than just around skimage). However, it is a different question on whether the metadata and the pixel data should be served together (struct/object) or individually. @jni seems to prefer the
Imageobject route, whereas I think that it will be cleaner to split metadata and pixel data into separate functions.almarklein commentedon Nov 19, 2021
Definitely!
Right. I think it makes sense for functions to automatically use the standardized fields, so subsequently it makes sense for these to be attached to the image object (so that they travel along with the image). But for the plugin-specific metadata this would get muddy, and a function using that info might result in surprising (magical in a bad way) behaviour. I think this is a valid argument against putting the specific metadata on the Image.
jni commentedon Nov 19, 2021
@FirefoxMetzger @almarklein can you discuss dates amongst yourselves? I generally don't start my day before 9am so all of my 8ams are currently free except Dec 16. 😃 As long as it's a weekday!
Regarding the one vs two objects, the issue is that two objects is clunky when the two things belong together and a single object is strictly more general, because I can always do
result = filters.gaussian(np.asarray(image), sigma=5 / image.spacing).As a general rule I am indeed anti custom-objects, and my preference here would actually be that NumPy/Dask provided a "metadata" dict/namespace for custom attributes, and we would just use that. But they don't, so we have to do something else. And, the other thing I've learned in my years of campaigning against objects, is that sometimes, if you're repeatedly writing
x1, y1, z1 = func(x0, y0, z0), then maybe you should bundle x, y, and z in an object.As to whether skimage will take these custom objects as input... I guess it depends on how custom they are vs whether we can make them standard enough. Maybe we should think about xarray? But what I'd prefer is an "annotated array" library that meets our needs and maybe a few extra — e.g. one of the things I've always wanted from skimage is the ability to annotate whether an array is an image, some coordinates, or a segmentation, among others.
FirefoxMetzger commentedon Nov 20, 2021
@jni @almarklein Awesome. I looked up both of your emails and sent a suggestion; seemed like the easier/faster thing to do.
Doesn't that go both ways though? It's pretty easy to tear a single object into multiple bits, but it is just as possible to stack parts into one object downstream if downstream decides that it wants to consume images in this way:
This touches upon another topic, which I think makes sense to talk about during the meeting: We have
iio.imopenand the plugin interface which takes care of lazy for performance junkies. Maybe we can tweak that to serve your use-case @jni.A plugin instance is actually fairly close to a lazy-loaded
Imageobject, and no matter our solution. It is missing numpy slicing (explicit choice in the new API #563 (comment)) and it doesn't supportnp.asarray(plugin_instance)(files may hold multiple images). You do getplugin_instance.read() -> ndarrayandplugin_instance.descriptor.spacingthough, so it has all the information you want/need in a single object.I agree. My angle here is that the metadata issue hasn't been resolved for years because it likely isn't as important to most users as is access to raw pixel data. So if the main use-case is
x1, _, _ = func(x0, y0, z0)(aka.np.asarray(iio.imread(...))) then perhaps it is better to keep metadata separate (but easily accessible) for those who need it.At the same time, if you are just interested in metadata, pixel-data shouldn't slow you down. There are definite use-cases for that, e.g. to pre-configure a pipeline to then pump large quantities of them through efficiently (dask use-case). Something like a
iio.improps("my.tiff")would fit that bill perfectly.Then there is the situation where you need both (standardized) metadata and pixel data. I think this is the one we are trying to solve now, right? The preferred solution is to get an
Imageobject that contains both pixel data and metadata so that they stay connected and can be accessed easily. Returning an image object would have an impact on the two scenarios above, so my question here is if this happens often enough (compared to the other two scenarios) to motivate solving it like this.The "skimage auto-config" feature falls into the last category, and I can see how this is desirable. At the same time, I agree with the Zen of python that "explicit is better than implicit", so if we do auto-config, I would like to explicitly pass in a
configobject so that (a) I know exactly which functions do auto-config, and (b) I get some means of introspecting and debugging what the auto-config does if I need that.From an I/O perspective that would depend on the underlying format used. If the format supports exif tags or a comment field, we could easily write a
commentinto the metadata for those annotations. If not then those annotations would be lost once the image is saved, which is a bit sad and kind of defeats the point I guess.FirefoxMetzger commentedon Dec 2, 2021
It looks like we can soon bring this to a close. To summarize the meeting we had, we agreed on the following points:
properties(self, index=None) -> ImageDescriptorandmetadata(self, *, remove_applied=True) -> Dict[str, Any]. Details are in META ISSUE: Revisit how imageio provides and handles meta data #362 (comment)ImageDescriptorcontains standardized metadata and will grow incrementally as we figure out what can be standardized. It will have the following fields to start with:shape,dtype,spacingiio.impropsandiio.imdetailsImageobject. This object is essentially a plugin, except that it allows slicing which triggers lazy-loading of pixel data.@almarklein @jni Anything I missed?
almarklein commentedon Dec 3, 2021
We also discussed an
ImageSequenceobject next to theImageobject, right?Names ...
Let's consistently call metadata "meta". So
iio.immetaI guess?I like "props" for the descriptor. Let's also make it an
ImagePropsobject ;)FirefoxMetzger commentedon Dec 3, 2021
Yes. We should see how we can add this one. It can't have a direct slicing API, due to the multi-image formats that allow different shapes (and are non-stackable as a consequence). Maybe it can be a plugin that returns
Imageinstead ofndarray? Then again, that could be handled by doingplugin.read(like=iio.Image).That was my first thought but I think
iio.immetais quite prone to typos due to themm. Not sure if we worry about that. The alternative could be to consistently call everythingdetails:iio.imdetails,plugin.detailsYes, I was thinking the same actually. Didn't want to edit the previous post though, because of consistency. Lets go with
properties,improps, andImageProperties.