-
Notifications
You must be signed in to change notification settings - Fork 261
Push loadsave logic into individual classes, remove logic duplication #317
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
Comments
Not writing image classes, myself, I can only give an aesthetic opinion. I think having each image type directly perform the work of establishing whether a file is that type is much neater than a global function that figures out the type. To go further, perhaps
Since I think those options would address con one. Regarding the second, is it really overly structured to give a class the ability to inspect a file to determine if it is that type? (Admittedly I'm thinking of this in terms of Finally, a looping master tuple seems a little crude when you've got a string that should be able to index a dictionary. Why not a global type map from extensions to images that use that extension? Either classes add themselves to it as they are loaded such as with a decorator:
Or Anyway, expect @matthew-brett will soon be along to explain why these exact ideas were scrapped long ago. :-) |
+1 @effigies , I like all those suggestions. I was hoping there were some superclasses to do exactly what you mentioned, but am still unfamiliar with the class structure. Nice idea on the global type map, that's much more elegant. |
To keep the conversation rolling. The current code could certainly do with some improvement. The current situation is just what happened in the initial fast get-it-working phase, and often does not have a deep justification. I think we are primarily looking at:
The use-case I was paying most attention to in the initial sketch was that of Analyze vs Nifti for
Here I guess each potential class would need to sniff the metadata for the image. It would also need to determine where the metadata was - for example Nifti and other Analyze types may have I wonder if there is a good way to sniff the file only once - maybe doing a 1K or less read from the relevant file (e.g. |
I was thinking about this. Would disk/OS caching not be sufficient to make the cost of multiple reads negligible? I'm just curious how much you gain by shaving off the odd double read of a kilobyte or so. If time( |
Once the file is open, I am sure 1K reads would be a tiny cost, but opening the file for a read might be slow on a network drive. |
At the moment, we have to deal with the following image data / metadata file pairs:
I guess we might also have to think about images in directories like DICOM, and Bruker format : http://imaging.mrc-cbu.cam.ac.uk/imaging/FormatBruker |
Another option is to pass in the sniff if we have it, otherwise None, maybe something like:
|
I was thinking similarly. |
For the image/metadata pairs, are all of these cases in which they are never unified? What's driving my concern is whether in some cases we'll want to sniff Regarding network file systems: I know that CIFS, NFS and AFS all employ caching. Are there other filesystems that need to be accounted for? I'm not trying to be contrary, but without a specific reason to be concerned about network latency of multiple openings, it strikes me as dipping into concerns that are more appropriately the domain of the filesystem. The use case in which that seems likely to become the concern of nibabel is if you can reasonably expect enough simultaneous validity checks (including the multiple process case) to overwhelm the caches. If we always know what file we want to sniff, then the above bit is mostly a non-issue as calling the header file can be done in the comfort of the individual image class. But it would force us into the situation where, if we begin supporting a file where it's not known by the extension whether we need to look at a separate metadata file, then all |
Older versions of SPM liked to have one file per volume, so this could make for thousands of files to open. You are right that this may not be a problem, but it may be hard to find out - we need to test on some slow file-systems. At the moment we might like to sniff for |
While I am thinking about it. We could go for something like this:
Then:
|
I'll play with this a bit; I think the ideas here are an improvement over what currently exists. |
that is a very helpful tip, it really worked for me. I even asked my teacher in my individual classes at http://preply.com/en and he didn't know. Good thing, you had a solution :) |
MRG: Migrate load, save, class_map, ext_map to class properties and methods This is to address #317 and #323 (and aims to supercede PR #319), both issues around image-specific code being distributed across files, rather than localized in the class definition. Changes here include: * Removing image-specific code from `nib.load` and `nib.save` functions, moving the logic into `ImageKlass.is_image` and `HeaderKlass.is_header` class functions. * Deprecating `class_map` and `ext_map` objects, and moving necessary info to properties defined on the image classes themselves.
Closed by #329. |
MRG: Migrate load, save, class_map, ext_map to class properties and methods This is to address nipy#317 and nipy#323 (and aims to supercede PR nipy#319), both issues around image-specific code being distributed across files, rather than localized in the class definition. Changes here include: * Removing image-specific code from `nib.load` and `nib.save` functions, moving the logic into `ImageKlass.is_image` and `HeaderKlass.is_header` class functions. * Deprecating `class_map` and `ext_map` objects, and moving necessary info to properties defined on the image classes themselves.
In modifying
loadsave.py
forCIFTI
file format, I found three design choices that made it more challenging for me to understand and modify code:load
andsave
functions.load
andsave
, each).I propose the following redesign (which I think is relatively simple):
valid_ext
(valid filename extensions) and class functionis_image(filename)
(which returnsTrue
if the given filename is of the Image class.loadsave.py
should have a global tuple of all image classes.load
andsave
should simply loop through this list, should not have any image-specific code.Pros:
Cons:
Thoughts?
The text was updated successfully, but these errors were encountered: