Skip to content

Refactor: Replace hardcoded if elif chains for project colormaps with a centralized configuration #142

Description

@k0rosh

Description

Currently, the get_inference_from_file and saveMap functions contain extensive, hardcoded if-elif chains to determine color mappings based on the FLAGS.project variable (e.g., 00_Adjacency, 01_METbrain, etc.).

While functional, this approach violates the Open-Closed Principle (OCP). Every time a new project or class is added, the core logic of these functions must be manually modified, which increases the risk of introducing bugs and clutters the processing logic.

Benefits

Maintainability: Adding a new project requires zero changes to the core function logic; developers only need to update the mapping dictionary.

Readability: Drastically reduces the line count and cyclomatic complexity of get_inference_from_file and saveMap.

Modularity: Paves the way for loading configurations dynamically via external JSON files in future releases.

I would be happy to work on this refactoring and submit a Pull Request if the maintainers agree with this architectural improvement.

Proposed Solution

Extract the project-specific colormap configurations into a centralized Python dictionary (or an external config.json file).

Example Implementation Idea:

# Centralized configuration mapping
PROJECT_COLORMAPS = {
    '00_Adjacency': {
        1: [c('white'), c('red')],
        2: [c('white'), c('orange')],
        # ...
    },
    '01_METbrain': {
        1: [c('white'), c('black')],
        # ...
    }
}

# Simplified logic inside functions
def get_inference_from_file(lineProb_st):
    # ...
    if FLAGS.project in PROJECT_COLORMAPS:
        cmap_colors = PROJECT_COLORMAPS[FLAGS.project].get(oClass, default_colors)
        cmap = make_colormap(cmap_colors)
    # ...

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions