-
Notifications
You must be signed in to change notification settings - Fork 10
244 architecture resolve resource dependencies #245
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
Merged
roryclaydon1994
merged 6 commits into
develop
from
244-architecture-resolve-resource-dependencies
Aug 24, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9dda8e5
Add resource dependency helper with tests
8d31c73
Add resource dependency helpers with tests
3cc3545
docs: clarify working version of resource module in doc string
194147e
Add resource resolution orchestrator with tests
3e869c8
Introduce Resources class as resource API boundary
ae5a3af
Add debug logging for resource resolution
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| """Resource dependency handling for the quality control pipeline. | ||
|
|
||
| Defines helpers for identifying resources required by enabled quality metrics | ||
| and resolving those requirements against resources supplied by the caller. | ||
|
|
||
| :Authors: | ||
| Jennifer Pollack <jennifer.pollack@cea.fr> | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
| from collections.abc import Mapping | ||
| from typing import Any | ||
| from wf_psf.quality_control.config import QualityControlConfig | ||
|
|
||
| import logging | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Resources: | ||
| """Manage resources required by quality control metrics. | ||
|
|
||
| Assesses resource requirements and availability for a validated quality | ||
| control configuration. | ||
| """ | ||
|
|
||
| def __init__(self, config: QualityControlConfig): | ||
| self.config = config | ||
|
|
||
| def get_required(self) -> set[str]: | ||
| """Return resources required by enabled quality metrics. | ||
|
|
||
| Returns | ||
| ------- | ||
| Unique resource identifiers required by enabled quality metrics. | ||
|
|
||
| Notes | ||
| ----- | ||
| This configuration is assumed to have been validated for internal consistency between resource requirements and available resources. | ||
|
|
||
| """ | ||
| return { | ||
| resource | ||
| for metric in self.config.metrics.values() | ||
| if metric.enabled | ||
| for resource in metric.required_resources | ||
| } | ||
|
|
||
| def resolve( | ||
| self, | ||
| provided: Mapping[str, Any] | None = None, | ||
| ) -> dict[str, Any]: | ||
| """Resolve resources required by enabled quality metrics. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| provided : Mapping[str, Any] or None | ||
| Ready-to-use resources supplied by the pipeline caller. | ||
|
|
||
| Returns | ||
| ------- | ||
| dict[str, Any] | ||
| Resources required by enabled quality metrics and supplied by the | ||
| caller. | ||
|
|
||
| Raises | ||
| ------ | ||
| NotImplementedError | ||
| If required resources are not supplied by the caller. Preparation of | ||
| missing resources is not yet implemented. | ||
| """ | ||
| required = self.get_required() | ||
| provided = {} if provided is None else provided | ||
|
|
||
| resolved = { | ||
| resource: provided[resource] | ||
| for resource in required | ||
| if resource in provided | ||
| } | ||
| missing = required - provided.keys() | ||
| unused = provided.keys() - required | ||
|
|
||
| logger.debug( | ||
| "Resource resolution: resolved=%s, missing=%s, unused=%s", | ||
| sorted(resolved), | ||
| sorted(missing), | ||
| sorted(unused), | ||
| ) | ||
|
|
||
| if missing: | ||
| raise NotImplementedError( | ||
| f"Required resources are not available: {sorted(missing)}" | ||
| ) | ||
|
|
||
| return resolved |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import pytest | ||
| from wf_psf.quality_control.config import ( | ||
| QualityControlConfig, | ||
| QualityMetricConfig, | ||
| RejectionPolicyConfig, | ||
| ResourcesConfig, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def qc_config_factory(): | ||
| def factory( | ||
| *, | ||
| required_resources=None, | ||
| rejection_metric=None, | ||
| resources=None, | ||
| metrics=None, | ||
| rejection=None, | ||
| ): | ||
| metric_default = { | ||
| "goodness_of_fit": QualityMetricConfig( | ||
| enabled=True, | ||
| required_resources=required_resources or [], | ||
| ) | ||
| } | ||
|
|
||
| resources_default = ResourcesConfig( | ||
| available={ | ||
| "psf_models": { | ||
| "standard": { | ||
| "inference_config": "inference_standard.yaml", | ||
| } | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| rejection_default = { | ||
| rejection_metric or "goodness_of_fit": RejectionPolicyConfig( | ||
| enabled=True, | ||
| threshold=0.25, | ||
| ) | ||
| } | ||
|
|
||
| return QualityControlConfig( | ||
| metrics=metric_default if metrics is None else metrics, | ||
| resources=resources_default if resources is None else resources, | ||
| rejection=rejection_default if rejection is None else rejection, | ||
| ) | ||
|
|
||
| return factory | ||
105 changes: 105 additions & 0 deletions
105
src/wf_psf/tests/test_quality_control/resources_test.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| """UNIT TESTS FOR PACKAGE MODULE: Quality Control Resources | ||
|
|
||
| This module contains unit tests for the quality control resources module. | ||
|
|
||
| :Author: Jennifer Pollack <jennifer.pollack@cea.fr> | ||
|
|
||
| """ | ||
|
|
||
| import pytest | ||
| from wf_psf.quality_control.config import QualityMetricConfig | ||
| from wf_psf.quality_control.resources import Resources | ||
|
|
||
|
|
||
| def test_get_required(qc_config_factory): | ||
| config = qc_config_factory( | ||
| metrics={ | ||
| "mask_obscuration": QualityMetricConfig( | ||
| enabled=True, | ||
| required_resources=[], | ||
| ), | ||
| "goodness_of_fit": QualityMetricConfig( | ||
| enabled=True, | ||
| required_resources=["psf_models.standard"], | ||
| ), | ||
| "shapes": QualityMetricConfig( | ||
| enabled=False, | ||
| required_resources=["psf_models.oversampled"], | ||
| ), | ||
| } | ||
| ) | ||
| resources = Resources(config) | ||
| assert resources.get_required() == {"psf_models.standard"} | ||
|
|
||
|
|
||
| def test_get_required_combines_unique_resources(qc_config_factory): | ||
| config = qc_config_factory( | ||
| metrics={ | ||
| "metric_a": QualityMetricConfig( | ||
| enabled=True, | ||
| required_resources=["psf_models.standard"], | ||
| ), | ||
| "metric_b": QualityMetricConfig( | ||
| enabled=True, | ||
| required_resources=[ | ||
| "psf_models.standard", | ||
| "psf_models.oversampled", | ||
| ], | ||
| ), | ||
| } | ||
| ) | ||
|
|
||
| resources = Resources(config) | ||
| assert resources.get_required() == { | ||
| "psf_models.standard", | ||
| "psf_models.oversampled", | ||
| } | ||
|
|
||
|
|
||
| # Resource resolution orchestration tests | ||
| @pytest.mark.parametrize( | ||
| ("required_resources", "provided", "expected_resolved"), | ||
| [ | ||
| ( | ||
| {"psf_models.standard"}, | ||
| {"psf_models.standard": [1, 1, 1, 1]}, | ||
| {"psf_models.standard": [1, 1, 1, 1]}, | ||
| ), | ||
| ( | ||
| {"psf_models.standard"}, | ||
| { | ||
| "psf_models.standard": [1, 1, 1, 1], | ||
| "psf_models.oversampled": [2, 2, 2, 2], | ||
| }, | ||
| {"psf_models.standard": [1, 1, 1, 1]}, | ||
| ), | ||
| ( | ||
| [], | ||
| {}, | ||
| {}, | ||
| ), | ||
| ], | ||
| ) | ||
| def test_resolve_resources( | ||
| qc_config_factory, | ||
| required_resources, | ||
| provided, | ||
| expected_resolved, | ||
| ): | ||
| config = qc_config_factory(required_resources=required_resources) | ||
| resources = Resources(config) | ||
|
|
||
| assert resources.resolve(provided) == expected_resolved | ||
|
|
||
|
|
||
| def test_resolve_resources_missing(qc_config_factory): | ||
| config = qc_config_factory( | ||
| required_resources=["psf_models.standard"], | ||
| ) | ||
| resources = Resources(config) | ||
|
|
||
| with pytest.raises( | ||
| NotImplementedError, | ||
| match="Required resources are not available", | ||
| ): | ||
| resources.resolve() |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.