-
Notifications
You must be signed in to change notification settings - Fork 6
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
refac: Algorithm specific configurations #344
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Will def make maintenance easier in future. Added a few small comments and suggestions. (I didn't look at tests yet)
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #344 +/- ##
==========================================
- Coverage 87.80% 87.66% -0.15%
==========================================
Files 137 144 +7
Lines 4971 5049 +78
==========================================
+ Hits 4365 4426 +61
- Misses 606 623 +17 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still haven't looked at tests but I'm sure they're good 😄
Description
Important
TLDR: Split Pydantic configurations to simplify validation.
Following #320, this PR adds algorithm-specific main configurations, as well as algorithm and data sub-configurations when necessary (e.g. N2V) in order to simplify the code base. Additionally, it fix mypy handling of Pydantic models using the Pydantic mypy plugin. Finally, the
CustomModel
capacity was removed, fixing errors in the mypy report.Pydantic mypy plugin
Natively, mypy does not have a good handle of Pydantic models. Adding the Pydantic mypy plugin yields new errors that were hidden from us (see report here).
Custom models
The custom models were added without having a real use-case, and the examples were mostly very simple toy examples. It is likely that any real use case will require more than plugging in a model in the CAREamist API, and will probably start with using one's own Lightning module in the Lightning API.
In addition, the presence of unknown Pydantic models creates issues with mypy (see previous point), and adds complexity in the code for a feature we are not sure will be used. This PR removes the
CustomModel
class and all mechanism to register models.Splitting configurations
Another pain point in the code base (#320) is that having one single
Configuration
for vastly different algorithms (and their constraints) generates complex validation code: validation across sub-configurations, manyif else
statements, and long files with tons offield_validators
.An elegant solution is to define a general configuration and sub-class it with algorithm-specific Pydantic models. Then, a "factory" model can leverage Pydantic model selection to choose the correct algorithm-specific configuration based on the parameters. Here is an illustration:
In this scenario, all the validation related to the
N2VManipulate
is inN2VDataConfig
andN2VConfiguration
can freely assume that the correct transforms are present. Similarly,DataConfig
does not need to care aboutN2VManipulate
, and other subclasses can exclude it from the allowed types in its transforms. In the same spirit, the algorithm config is alwaysUNet
for N2V, andVAE
for HDN. In this PR, we followed that idea with algorithm specific configurations (both for the main configuration and the algorithm sub configuration).The main issue raised by this approach is that
Configuration
can no longer be used to instantiate the correct configuration, but ratherconfiguration_factory
should be used:Here, the model choice (
N2VConfiguration
,CAREConfiguration
, etc...) is delegated to Pydantic via theconfiguration_factory
.Similar factories have been created for the algorithm and data sub-configurations in order to allow correct instantiation in the Lightning API.
Changes Made
N2VConfiguration
: main configuration for N2VN2NConfiguration
: idem for N2NCAREConfiguration
: idem for CAREN2VDataConfig
: data subconfiguration for N2V (enforcedN2VManipulate
)N2VAlgorithm
: algorithm subconfiguration for N2VN2NAlgorithm
: idem for N2NCAREAlgorithm
: idem for CAREConfigurationFactory
: a Pydantic model instantiating the correct configurationAlgorithmFactory
: idem for algorithm subconfigurationsDataFactory
: idem for data subconfigurationsconfiguration_io.py
: read/save methods for configurations, refactored fromconfiguration_factory.py
Configuration
andConfiguration
itself.references
module, all calls tohas_n2v_manipulate
, anything to do withCustomModel
.Warning
Testing could be improve further and there are probably some aspects that I am missing. But I'd rather get reviews already!
Related Issues
Breaking changes
UNetAlgorithm
,VAEAlgorithm
).CustomModel
is now completely removed.has_n2v_manipulate
no longer work, preferisinstance(cfg, N2VConfiguration)
.Warning
Any current work on adding new algorithm and configuration will be largely impacted by this PR. @CatEek
Please ensure your PR meets the following requirements: