Fix: Add algorithm name validation in OsipiBase.osipi_initiate_algorithm()#160
Merged
Conversation
…thm() - Validates algorithm name against src/standardized/ before dynamic import - Raises clear ValueError listing available algorithms for typos - Adds try/except safety net for ImportError and AttributeError - Adds test_invalid_algorithm_name regression test
IvanARashid
requested changes
May 5, 2026
IvanARashid
left a comment
Contributor
There was a problem hiding this comment.
Looks good, just one more adjustment so that we stay consistent.
Contributor
Author
I have implemented the change sir. Thank you. |
Collaborator
|
Looks good to me. If Ivan agrees we can merge |
IvanARashid
approved these changes
May 5, 2026
IvanARashid
left a comment
Contributor
There was a problem hiding this comment.
Looks good now, thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes the issue where
OsipiBasecrashes with a confusingModuleNotFoundErrororAttributeErrorwhen initialized with a misspelled or invalid algorithm name. The dynamic class loader inosipi_initiate_algorithm()previously performed zero validation before callingimportlib.import_module(), causing raw Python import errors to surface to the user.Changes Made
src/wrappers/OsipiBase.pyAlgorithm name validation: Before attempting the dynamic import, the method now scans the
src/standardized/directory usingpathlib.glob("*.py")to build a set of valid algorithm names at runtime. This approach is always in sync with the actual codebase — no hardcoded list needed.Clear
ValueError: If the requested algorithm is not found, aValueErroris raised listing all available algorithms alphabetically, making typos easy to spot.Secondary safety net: The
importlib.import_module()andgetattr()calls are now wrapped in explicittry/exceptblocks with descriptive error messages:ImportError— if the file exists but fails to import (e.g., syntax error in the module).AttributeError— if the module loads but doesn't contain the expected class name.Updated docstring: Added
Raisessection documenting theValueError.tests/IVIMmodels/unit_tests/test_ivim_fit.pyAdded
test_invalid_algorithm_name()regression test with 3 sub-checks:"IAR_LU_biepx") raisesValueErrorwith"not found"in the message."totally_fake_algorithm") raisesValueErrorthat includes"Available algorithms are:"and lists known algorithms likeIAR_LU_biexpandPV_MUMC_biexp."IAR_LU_biexp") still initializes successfully without errors (no false positives).Testing & Validation
ValueErrorfixes #159