Skip to content

Push loadsave logic into individual classes, remove logic duplication #317

Closed
@bcipolli

Description

@bcipolli

In modifying loadsave.py for CIFTI file format, I found three design choices that made it more challenging for me to understand and modify code:

  1. The logic for whether a file is of each Image type is contained in a single function, not modularized into each image (yet the load/save functionality is).
  2. Image type / file extension associations is duplicated between load and save functions.
  3. The list of available file formats is duplicated (in imports, then again in the explicit logic of load and save, each).

I propose the following redesign (which I think is relatively simple):

  • Each Image class should declare class variable valid_ext (valid filename extensions) and class function is_image(filename) (which returns True if the given filename is of the Image class.
  • loadsave.py should have a global tuple of all image classes. load and save should simply loop through this list, should not have any image-specific code.

Pros:

  • This should improve code modularity, readability, maintainability, and avoid errors in adding new formats.
  • It gives a clear interface for determining file Image type.

Cons:

  • This design will have a small time cost in opening files when a file extension is ambiguous about the file type, and the file type isn't the most common for that file extension.
  • It gives more structure to determining image type, which could be detrimental in the future.

Thoughts?

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