Skip to content

Feature/model autodiscovery#173

Merged
davidorme merged 19 commits intodevelopfrom
feature/model_autodiscovery
Feb 17, 2023
Merged

Feature/model autodiscovery#173
davidorme merged 19 commits intodevelopfrom
feature/model_autodiscovery

Conversation

@davidorme
Copy link
Copy Markdown
Collaborator

@davidorme davidorme commented Feb 10, 2023

Description

This PR adds model autodiscovery to the virtual_rainforest/__init__.py file.

Any submodule within virtual_rainforests/models will automatically get its schema and BaseModel subclass registered, as long as the model __init__.py contains the schema registration and imports its BaseModel. The PR also updates the existing soil and abiotic modules to implement that requirement.

The other advantage here is that the import path for the main models is shorter. That seems like a sensible thing for the core object in each model:

from virtual_rainforest.models.soil import SoilModel

rather than:

from virtual_rainforest.models.soil.model import SoilModel

Fixes #172

ETA: Fixes #167

Type of change

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist

  • Make sure you've run the pre-commit checks: $ pre-commit run -a
  • All tests pass: $ poetry run pytest

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@davidorme
Copy link
Copy Markdown
Collaborator Author

@dalonsoa @alexdewar

@jacobcook1995 and @vgro and I discussed this recently so I haven't added them as reviewers, but it would be great to get a check for any issues. @TaranRallings is ill.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Feb 10, 2023

Codecov Report

Merging #173 (4cefa07) into develop (a6c6492) will decrease coverage by 0.25%.
The diff coverage is 96.87%.

@@             Coverage Diff             @@
##           develop     #173      +/-   ##
===========================================
- Coverage    95.23%   94.99%   -0.25%     
===========================================
  Files           19       19              
  Lines          966      959       -7     
===========================================
- Hits           920      911       -9     
- Misses          46       48       +2     
Impacted Files Coverage Δ
virtual_rainforest/core/config.py 97.68% <95.45%> (-0.86%) ⬇️
virtual_rainforest/__init__.py 100.00% <100.00%> (ø)
virtual_rainforest/core/__init__.py 100.00% <100.00%> (ø)
virtual_rainforest/models/abiotic/__init__.py 100.00% <100.00%> (ø)
virtual_rainforest/models/plants/__init__.py 100.00% <100.00%> (ø)
virtual_rainforest/models/soil/__init__.py 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@jacobcook1995
Copy link
Copy Markdown
Collaborator

If possible could you update the documentation in source/development/defining_new_models.md to reflect the change?

@davidorme
Copy link
Copy Markdown
Collaborator Author

That occurred to me too. I was going to lazily change it when I update the data part of the new model definition. But that is lazy.

Copy link
Copy Markdown
Collaborator

@alexdewar alexdewar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok to me. I've made a suggestion.

@@ -217,23 +255,3 @@ def schema() -> dict:

return config_schema
```
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code the same for all models, except for the name of the schema file?

If so, you could remove some of the boilerplate and just do something like this in all the __init__.py files:

from virtual_rainforest.core.config import default_schema_setup

schema = default_schema_setup()

You'll probably need to examine the stack for this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the same for all models. That's a good thought.

# from virtual_rainforest.models.soil.model import SoilModel # noqa: F401
# - define a schema member, decorated with @register_schema
for module_info in pkgutil.iter_modules(vfm.__path__):
import_module(f"virtual_rainforest.models.{module_info.name}")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...alternatively, you could check here whether the module has an attribute called schema and if not, add a default one.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that crossed my mind was having a config.add_module command which basically wraps this import and schema registration. That could then be used to add custom modules outside of the package tree. If you're at the Python command line, you can always import them, but if you wanted to use them in a config, we'd need a custom_model section that could use this too. But, that is probably one for the future.

@davidorme
Copy link
Copy Markdown
Collaborator Author

@alexdewar @jacobcook1995

I have updated this PR to implement Alex's suggestion of having a function to setup the schema. I've basically moved all of the reading and validation out of model __init__.py and into config. I have separated the register_schema decorator into get_schema function, which handles most of the heavy lifting and error cases, and a register_schema function, which just does the registering and wraps get_schema. There's a mild update to how we extend the validator.

I've updated the user docs and docstrings and then patched all the existing __init__.py files. This also fixes #167.

Copy link
Copy Markdown
Collaborator

@alexdewar alexdewar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Copy Markdown
Collaborator

@jacobcook1995 jacobcook1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, made some minor comments but nothing major

@davidorme davidorme merged commit a76839f into develop Feb 17, 2023
@davidorme davidorme deleted the feature/model_autodiscovery branch February 17, 2023 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autodiscovery system for models Use importlib to load files inside the package

4 participants