reframx 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- reframx/__init__.py +180 -0
- reframx/api/__init__.py +193 -0
- reframx/core/__init__.py +1 -0
- reframx/core/components/__init__.py +5 -0
- reframx/core/components/handle.py +87 -0
- reframx/core/components/metadata.py +33 -0
- reframx/core/components/protocols.py +47 -0
- reframx/core/components/specification.py +50 -0
- reframx/core/contracts/__init__.py +5 -0
- reframx/core/contracts/capabilities.py +58 -0
- reframx/core/contracts/compatibility.py +574 -0
- reframx/core/contracts/io.py +42 -0
- reframx/core/contracts/requirements.py +115 -0
- reframx/core/exceptions.py +25 -0
- reframx/core/registry/__init__.py +27 -0
- reframx/core/registry/plugins.py +478 -0
- reframx/core/registry/registry.py +186 -0
- reframx/core/types.py +14 -0
- reframx/core/validation/__init__.py +15 -0
- reframx/core/validation/context.py +22 -0
- reframx/core/validation/report.py +80 -0
- reframx/core/validation/runner.py +41 -0
- reframx/datasets/__init__.py +10 -0
- reframx/datasets/adapters/__init__.py +5 -0
- reframx/datasets/adapters/field_mapping.py +179 -0
- reframx/datasets/interface.py +79 -0
- reframx/datasets/specification.py +31 -0
- reframx/evaluators/__init__.py +11 -0
- reframx/evaluators/interface.py +88 -0
- reframx/execution/__init__.py +14 -0
- reframx/execution/backend.py +73 -0
- reframx/execution/context.py +92 -0
- reframx/execution/engine.py +257 -0
- reframx/execution/hooks/__init__.py +6 -0
- reframx/execution/hooks/base.py +46 -0
- reframx/experiments/__init__.py +8 -0
- reframx/experiments/experiment.py +232 -0
- reframx/experiments/run.py +63 -0
- reframx/instrumentation/__init__.py +35 -0
- reframx/instrumentation/events.py +60 -0
- reframx/instrumentation/manager.py +365 -0
- reframx/instrumentation/module_hooks.py +185 -0
- reframx/instrumentation/probes.py +142 -0
- reframx/integrations/__init__.py +1 -0
- reframx/integrations/mmengine/__init__.py +15 -0
- reframx/integrations/mmengine/backend.py +236 -0
- reframx/integrations/mmengine/bridges.py +186 -0
- reframx/integrations/mmengine/strategy.py +101 -0
- reframx/integrations/mmengine/utils.py +42 -0
- reframx/integrations/pytorch/__init__.py +45 -0
- reframx/integrations/pytorch/dataset.py +78 -0
- reframx/integrations/pytorch/evaluators.py +115 -0
- reframx/integrations/pytorch/methods.py +160 -0
- reframx/integrations/pytorch/model.py +95 -0
- reframx/integrations/pytorch/objectives.py +149 -0
- reframx/integrations/pytorch/strategy.py +190 -0
- reframx/integrations/pytorch/utils.py +80 -0
- reframx/methods/__init__.py +13 -0
- reframx/methods/interface.py +138 -0
- reframx/methods/specification.py +24 -0
- reframx/models/__init__.py +8 -0
- reframx/models/interface.py +114 -0
- reframx/models/specification.py +28 -0
- reframx/objectives/__init__.py +6 -0
- reframx/objectives/interface.py +76 -0
- reframx/packaging/__init__.py +38 -0
- reframx/packaging/archive.py +195 -0
- reframx/packaging/exporter.py +206 -0
- reframx/packaging/importer.py +186 -0
- reframx/packaging/manifest.py +166 -0
- reframx/py.typed +0 -0
- reframx/strategies/__init__.py +11 -0
- reframx/strategies/interface.py +186 -0
- reframx/tasks/__init__.py +23 -0
- reframx/tasks/builtin/__init__.py +11 -0
- reframx/tasks/builtin/common.py +137 -0
- reframx/tasks/definitions/__init__.py +9 -0
- reframx/tasks/definitions/evaluation.py +20 -0
- reframx/tasks/definitions/objectives.py +25 -0
- reframx/tasks/definitions/problem.py +10 -0
- reframx/tasks/interface.py +57 -0
- reframx/tasks/specification.py +65 -0
- reframx-0.1.1.dist-info/METADATA +134 -0
- reframx-0.1.1.dist-info/RECORD +87 -0
- reframx-0.1.1.dist-info/WHEEL +5 -0
- reframx-0.1.1.dist-info/licenses/LICENSE +21 -0
- reframx-0.1.1.dist-info/top_level.txt +1 -0
reframx/__init__.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"""Research Framework public package namespace."""
|
|
2
|
+
|
|
3
|
+
from reframx.api import (
|
|
4
|
+
DEFAULT_ENTRY_POINT_GROUP,
|
|
5
|
+
RFX_EXTENSION,
|
|
6
|
+
ArchiveError,
|
|
7
|
+
ArchiveManifest,
|
|
8
|
+
BaseDataset,
|
|
9
|
+
BaseEvaluator,
|
|
10
|
+
BaseHook,
|
|
11
|
+
BaseMethod,
|
|
12
|
+
BaseModel,
|
|
13
|
+
BaseObjective,
|
|
14
|
+
BaseStrategy,
|
|
15
|
+
BaseTask,
|
|
16
|
+
Capability,
|
|
17
|
+
CausalLMTask,
|
|
18
|
+
ClassificationTask,
|
|
19
|
+
ComponentHandle,
|
|
20
|
+
ComponentIntegrationError,
|
|
21
|
+
ComponentMetadata,
|
|
22
|
+
ComponentRegistry,
|
|
23
|
+
ComponentSpec,
|
|
24
|
+
ComponentValidator,
|
|
25
|
+
ConfigProvider,
|
|
26
|
+
DatasetFieldAdapter,
|
|
27
|
+
DatasetProtocol,
|
|
28
|
+
DatasetSpec,
|
|
29
|
+
DiscoveredPlugin,
|
|
30
|
+
EvaluationDefinition,
|
|
31
|
+
EvaluatorProtocol,
|
|
32
|
+
EvaluatorSpec,
|
|
33
|
+
ExecutionBackend,
|
|
34
|
+
ExecutionContext,
|
|
35
|
+
ExecutionEngine,
|
|
36
|
+
ExecutionState,
|
|
37
|
+
Experiment,
|
|
38
|
+
ExportProfile,
|
|
39
|
+
ImportedExperiment,
|
|
40
|
+
InstrumentationEvent,
|
|
41
|
+
InstrumentationHook,
|
|
42
|
+
InstrumentationManager,
|
|
43
|
+
InstrumentationRecord,
|
|
44
|
+
InvalidSpecificationError,
|
|
45
|
+
IOContract,
|
|
46
|
+
LoadedPlugin,
|
|
47
|
+
MethodResult,
|
|
48
|
+
MethodSpec,
|
|
49
|
+
ModelAdapter,
|
|
50
|
+
ModelSpec,
|
|
51
|
+
NativeBackend,
|
|
52
|
+
ObjectiveGoal,
|
|
53
|
+
ObjectiveSpec,
|
|
54
|
+
PluginLoadError,
|
|
55
|
+
PluginManager,
|
|
56
|
+
PluginManifest,
|
|
57
|
+
PluginProtocol,
|
|
58
|
+
Probe,
|
|
59
|
+
ProblemDefinition,
|
|
60
|
+
RegistrationEntry,
|
|
61
|
+
RegressionTask,
|
|
62
|
+
Requirement,
|
|
63
|
+
RequirementGroup,
|
|
64
|
+
ResearchFrameworkError,
|
|
65
|
+
Run,
|
|
66
|
+
RunStatus,
|
|
67
|
+
SpecProvider,
|
|
68
|
+
StandardBackwardStrategy,
|
|
69
|
+
StateProvider,
|
|
70
|
+
StrategySpec,
|
|
71
|
+
SupervisedMethod,
|
|
72
|
+
TaskSpec,
|
|
73
|
+
ValidationContext,
|
|
74
|
+
ValidationFailedError,
|
|
75
|
+
ValidationIssue,
|
|
76
|
+
ValidationReport,
|
|
77
|
+
ValidationSeverity,
|
|
78
|
+
ValidatorContractError,
|
|
79
|
+
WatchHandle,
|
|
80
|
+
check_nan_inf,
|
|
81
|
+
default_plugin_manager,
|
|
82
|
+
default_registry,
|
|
83
|
+
export_experiment,
|
|
84
|
+
load_experiment,
|
|
85
|
+
probe,
|
|
86
|
+
run_component_validator,
|
|
87
|
+
tensor_stats,
|
|
88
|
+
validate_requirements,
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
__version__ = "0.1.1"
|
|
92
|
+
|
|
93
|
+
__all__ = [
|
|
94
|
+
"__version__",
|
|
95
|
+
"ArchiveError",
|
|
96
|
+
"ArchiveManifest",
|
|
97
|
+
"BaseDataset",
|
|
98
|
+
"BaseEvaluator",
|
|
99
|
+
"BaseHook",
|
|
100
|
+
"BaseMethod",
|
|
101
|
+
"BaseModel",
|
|
102
|
+
"BaseObjective",
|
|
103
|
+
"BaseStrategy",
|
|
104
|
+
"BaseTask",
|
|
105
|
+
"Capability",
|
|
106
|
+
"CausalLMTask",
|
|
107
|
+
"ClassificationTask",
|
|
108
|
+
"ComponentHandle",
|
|
109
|
+
"ComponentIntegrationError",
|
|
110
|
+
"ComponentMetadata",
|
|
111
|
+
"ComponentRegistry",
|
|
112
|
+
"ComponentSpec",
|
|
113
|
+
"ComponentValidator",
|
|
114
|
+
"ConfigProvider",
|
|
115
|
+
"DEFAULT_ENTRY_POINT_GROUP",
|
|
116
|
+
"DatasetFieldAdapter",
|
|
117
|
+
"DatasetProtocol",
|
|
118
|
+
"DatasetSpec",
|
|
119
|
+
"DiscoveredPlugin",
|
|
120
|
+
"EvaluationDefinition",
|
|
121
|
+
"EvaluatorProtocol",
|
|
122
|
+
"EvaluatorSpec",
|
|
123
|
+
"ExecutionBackend",
|
|
124
|
+
"ExecutionContext",
|
|
125
|
+
"ExecutionEngine",
|
|
126
|
+
"ExecutionState",
|
|
127
|
+
"Experiment",
|
|
128
|
+
"ExportProfile",
|
|
129
|
+
"IOContract",
|
|
130
|
+
"ImportedExperiment",
|
|
131
|
+
"InstrumentationEvent",
|
|
132
|
+
"InstrumentationHook",
|
|
133
|
+
"InstrumentationManager",
|
|
134
|
+
"InstrumentationRecord",
|
|
135
|
+
"InvalidSpecificationError",
|
|
136
|
+
"LoadedPlugin",
|
|
137
|
+
"MethodResult",
|
|
138
|
+
"MethodSpec",
|
|
139
|
+
"ModelAdapter",
|
|
140
|
+
"ModelSpec",
|
|
141
|
+
"NativeBackend",
|
|
142
|
+
"ObjectiveGoal",
|
|
143
|
+
"ObjectiveSpec",
|
|
144
|
+
"PluginLoadError",
|
|
145
|
+
"PluginManager",
|
|
146
|
+
"PluginManifest",
|
|
147
|
+
"PluginProtocol",
|
|
148
|
+
"Probe",
|
|
149
|
+
"ProblemDefinition",
|
|
150
|
+
"RFX_EXTENSION",
|
|
151
|
+
"RegistrationEntry",
|
|
152
|
+
"RegressionTask",
|
|
153
|
+
"Requirement",
|
|
154
|
+
"RequirementGroup",
|
|
155
|
+
"ResearchFrameworkError",
|
|
156
|
+
"Run",
|
|
157
|
+
"RunStatus",
|
|
158
|
+
"SpecProvider",
|
|
159
|
+
"StandardBackwardStrategy",
|
|
160
|
+
"StateProvider",
|
|
161
|
+
"StrategySpec",
|
|
162
|
+
"SupervisedMethod",
|
|
163
|
+
"TaskSpec",
|
|
164
|
+
"ValidationContext",
|
|
165
|
+
"ValidationFailedError",
|
|
166
|
+
"ValidationIssue",
|
|
167
|
+
"ValidationReport",
|
|
168
|
+
"ValidationSeverity",
|
|
169
|
+
"ValidatorContractError",
|
|
170
|
+
"WatchHandle",
|
|
171
|
+
"check_nan_inf",
|
|
172
|
+
"default_plugin_manager",
|
|
173
|
+
"default_registry",
|
|
174
|
+
"export_experiment",
|
|
175
|
+
"load_experiment",
|
|
176
|
+
"probe",
|
|
177
|
+
"run_component_validator",
|
|
178
|
+
"tensor_stats",
|
|
179
|
+
"validate_requirements",
|
|
180
|
+
]
|
reframx/api/__init__.py
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"""Stable, user-facing symbols for the Research Framework public API."""
|
|
2
|
+
|
|
3
|
+
from reframx.core.components.handle import ComponentHandle
|
|
4
|
+
from reframx.core.components.metadata import ComponentMetadata
|
|
5
|
+
from reframx.core.components.protocols import (
|
|
6
|
+
ComponentValidator,
|
|
7
|
+
ConfigProvider,
|
|
8
|
+
SpecProvider,
|
|
9
|
+
StateProvider,
|
|
10
|
+
)
|
|
11
|
+
from reframx.core.components.specification import ComponentSpec
|
|
12
|
+
from reframx.core.contracts.capabilities import Capability
|
|
13
|
+
from reframx.core.contracts.compatibility import validate_requirements
|
|
14
|
+
from reframx.core.contracts.io import IOContract
|
|
15
|
+
from reframx.core.contracts.requirements import Requirement, RequirementGroup
|
|
16
|
+
from reframx.core.exceptions import (
|
|
17
|
+
ComponentIntegrationError,
|
|
18
|
+
InvalidSpecificationError,
|
|
19
|
+
PluginLoadError,
|
|
20
|
+
ResearchFrameworkError,
|
|
21
|
+
ValidationFailedError,
|
|
22
|
+
ValidatorContractError,
|
|
23
|
+
)
|
|
24
|
+
from reframx.core.registry import (
|
|
25
|
+
DEFAULT_ENTRY_POINT_GROUP,
|
|
26
|
+
ComponentRegistry,
|
|
27
|
+
DiscoveredPlugin,
|
|
28
|
+
LoadedPlugin,
|
|
29
|
+
PluginManager,
|
|
30
|
+
PluginManifest,
|
|
31
|
+
PluginProtocol,
|
|
32
|
+
RegistrationEntry,
|
|
33
|
+
default_plugin_manager,
|
|
34
|
+
default_registry,
|
|
35
|
+
)
|
|
36
|
+
from reframx.core.validation.context import ValidationContext
|
|
37
|
+
from reframx.core.validation.report import (
|
|
38
|
+
ValidationIssue,
|
|
39
|
+
ValidationReport,
|
|
40
|
+
ValidationSeverity,
|
|
41
|
+
)
|
|
42
|
+
from reframx.core.validation.runner import run_component_validator
|
|
43
|
+
from reframx.datasets import (
|
|
44
|
+
BaseDataset,
|
|
45
|
+
DatasetFieldAdapter,
|
|
46
|
+
DatasetProtocol,
|
|
47
|
+
DatasetSpec,
|
|
48
|
+
)
|
|
49
|
+
from reframx.evaluators import (
|
|
50
|
+
BaseEvaluator,
|
|
51
|
+
EvaluatorProtocol,
|
|
52
|
+
EvaluatorSpec,
|
|
53
|
+
)
|
|
54
|
+
from reframx.execution import (
|
|
55
|
+
BaseHook,
|
|
56
|
+
ExecutionBackend,
|
|
57
|
+
ExecutionContext,
|
|
58
|
+
ExecutionEngine,
|
|
59
|
+
ExecutionState,
|
|
60
|
+
NativeBackend,
|
|
61
|
+
)
|
|
62
|
+
from reframx.experiments import Experiment, Run, RunStatus
|
|
63
|
+
from reframx.instrumentation import (
|
|
64
|
+
InstrumentationEvent,
|
|
65
|
+
InstrumentationHook,
|
|
66
|
+
InstrumentationManager,
|
|
67
|
+
InstrumentationRecord,
|
|
68
|
+
Probe,
|
|
69
|
+
WatchHandle,
|
|
70
|
+
check_nan_inf,
|
|
71
|
+
probe,
|
|
72
|
+
tensor_stats,
|
|
73
|
+
)
|
|
74
|
+
from reframx.methods import (
|
|
75
|
+
BaseMethod,
|
|
76
|
+
MethodResult,
|
|
77
|
+
MethodSpec,
|
|
78
|
+
SupervisedMethod,
|
|
79
|
+
)
|
|
80
|
+
from reframx.models import BaseModel, ModelAdapter, ModelSpec
|
|
81
|
+
from reframx.objectives import BaseObjective, ObjectiveSpec
|
|
82
|
+
from reframx.packaging import (
|
|
83
|
+
RFX_EXTENSION,
|
|
84
|
+
ArchiveError,
|
|
85
|
+
ArchiveManifest,
|
|
86
|
+
ExportProfile,
|
|
87
|
+
ImportedExperiment,
|
|
88
|
+
export_experiment,
|
|
89
|
+
load_experiment,
|
|
90
|
+
)
|
|
91
|
+
from reframx.strategies import (
|
|
92
|
+
BaseStrategy,
|
|
93
|
+
StandardBackwardStrategy,
|
|
94
|
+
StrategySpec,
|
|
95
|
+
)
|
|
96
|
+
from reframx.tasks import (
|
|
97
|
+
BaseTask,
|
|
98
|
+
CausalLMTask,
|
|
99
|
+
ClassificationTask,
|
|
100
|
+
EvaluationDefinition,
|
|
101
|
+
ObjectiveGoal,
|
|
102
|
+
ProblemDefinition,
|
|
103
|
+
RegressionTask,
|
|
104
|
+
TaskSpec,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
__all__ = [
|
|
108
|
+
"ArchiveError",
|
|
109
|
+
"ArchiveManifest",
|
|
110
|
+
"BaseDataset",
|
|
111
|
+
"BaseEvaluator",
|
|
112
|
+
"BaseHook",
|
|
113
|
+
"BaseMethod",
|
|
114
|
+
"BaseModel",
|
|
115
|
+
"BaseObjective",
|
|
116
|
+
"BaseStrategy",
|
|
117
|
+
"BaseTask",
|
|
118
|
+
"Capability",
|
|
119
|
+
"CausalLMTask",
|
|
120
|
+
"ClassificationTask",
|
|
121
|
+
"ComponentHandle",
|
|
122
|
+
"ComponentIntegrationError",
|
|
123
|
+
"ComponentMetadata",
|
|
124
|
+
"ComponentRegistry",
|
|
125
|
+
"ComponentSpec",
|
|
126
|
+
"ComponentValidator",
|
|
127
|
+
"ConfigProvider",
|
|
128
|
+
"DEFAULT_ENTRY_POINT_GROUP",
|
|
129
|
+
"DatasetFieldAdapter",
|
|
130
|
+
"DatasetProtocol",
|
|
131
|
+
"DatasetSpec",
|
|
132
|
+
"DiscoveredPlugin",
|
|
133
|
+
"EvaluationDefinition",
|
|
134
|
+
"EvaluatorProtocol",
|
|
135
|
+
"EvaluatorSpec",
|
|
136
|
+
"ExecutionBackend",
|
|
137
|
+
"ExecutionContext",
|
|
138
|
+
"ExecutionEngine",
|
|
139
|
+
"ExecutionState",
|
|
140
|
+
"Experiment",
|
|
141
|
+
"ExportProfile",
|
|
142
|
+
"IOContract",
|
|
143
|
+
"ImportedExperiment",
|
|
144
|
+
"InstrumentationEvent",
|
|
145
|
+
"InstrumentationHook",
|
|
146
|
+
"InstrumentationManager",
|
|
147
|
+
"InstrumentationRecord",
|
|
148
|
+
"InvalidSpecificationError",
|
|
149
|
+
"LoadedPlugin",
|
|
150
|
+
"MethodResult",
|
|
151
|
+
"MethodSpec",
|
|
152
|
+
"ModelAdapter",
|
|
153
|
+
"ModelSpec",
|
|
154
|
+
"NativeBackend",
|
|
155
|
+
"ObjectiveGoal",
|
|
156
|
+
"ObjectiveSpec",
|
|
157
|
+
"PluginLoadError",
|
|
158
|
+
"PluginManager",
|
|
159
|
+
"PluginManifest",
|
|
160
|
+
"PluginProtocol",
|
|
161
|
+
"Probe",
|
|
162
|
+
"ProblemDefinition",
|
|
163
|
+
"RFX_EXTENSION",
|
|
164
|
+
"RegistrationEntry",
|
|
165
|
+
"RegressionTask",
|
|
166
|
+
"Requirement",
|
|
167
|
+
"RequirementGroup",
|
|
168
|
+
"ResearchFrameworkError",
|
|
169
|
+
"Run",
|
|
170
|
+
"RunStatus",
|
|
171
|
+
"SpecProvider",
|
|
172
|
+
"StandardBackwardStrategy",
|
|
173
|
+
"StateProvider",
|
|
174
|
+
"StrategySpec",
|
|
175
|
+
"SupervisedMethod",
|
|
176
|
+
"TaskSpec",
|
|
177
|
+
"ValidationContext",
|
|
178
|
+
"ValidationFailedError",
|
|
179
|
+
"ValidationIssue",
|
|
180
|
+
"ValidationReport",
|
|
181
|
+
"ValidationSeverity",
|
|
182
|
+
"ValidatorContractError",
|
|
183
|
+
"WatchHandle",
|
|
184
|
+
"check_nan_inf",
|
|
185
|
+
"default_plugin_manager",
|
|
186
|
+
"default_registry",
|
|
187
|
+
"export_experiment",
|
|
188
|
+
"load_experiment",
|
|
189
|
+
"probe",
|
|
190
|
+
"run_component_validator",
|
|
191
|
+
"tensor_stats",
|
|
192
|
+
"validate_requirements",
|
|
193
|
+
]
|
reframx/core/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Core contracts, components, validation, and plugin registry."""
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Non-invasive wrapper used by the framework to manage component objects."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from reframx.core.components.metadata import ComponentMetadata
|
|
9
|
+
from reframx.core.components.specification import ComponentSpec
|
|
10
|
+
from reframx.core.exceptions import InvalidSpecificationError
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(slots = True)
|
|
14
|
+
class ComponentHandle:
|
|
15
|
+
"""Framework-side reference to a user-owned component instance.
|
|
16
|
+
|
|
17
|
+
The original object is kept intact. The handle stores integration metadata
|
|
18
|
+
alongside it instead of injecting framework attributes into user objects.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
instance: Any
|
|
22
|
+
role: str
|
|
23
|
+
spec: ComponentSpec
|
|
24
|
+
origin: str = "user"
|
|
25
|
+
|
|
26
|
+
@classmethod
|
|
27
|
+
def wrap(
|
|
28
|
+
cls,
|
|
29
|
+
instance: Any,
|
|
30
|
+
*,
|
|
31
|
+
role: str | None = None,
|
|
32
|
+
spec: ComponentSpec | None = None,
|
|
33
|
+
origin: str = "user",
|
|
34
|
+
) -> "ComponentHandle":
|
|
35
|
+
"""Wrap an object, using `get_spec()` when available.
|
|
36
|
+
|
|
37
|
+
Plain Python objects are accepted. If they do not implement `get_spec`,
|
|
38
|
+
the framework creates a minimal specification. Callers can also supply
|
|
39
|
+
an explicit specification without modifying the original object.
|
|
40
|
+
"""
|
|
41
|
+
if instance is None:
|
|
42
|
+
raise ValueError("Cannot wrap None as a component.")
|
|
43
|
+
|
|
44
|
+
resolved_role = role or getattr(instance, "component_type", None) or "component"
|
|
45
|
+
|
|
46
|
+
if spec is None:
|
|
47
|
+
get_spec = getattr(instance, "get_spec", None)
|
|
48
|
+
if callable(get_spec):
|
|
49
|
+
spec = get_spec()
|
|
50
|
+
if not isinstance(spec, ComponentSpec):
|
|
51
|
+
raise InvalidSpecificationError(
|
|
52
|
+
f"{type(instance).__name__}.get_spec() must return ComponentSpec; "
|
|
53
|
+
f"got {type(spec).__name__}."
|
|
54
|
+
)
|
|
55
|
+
else:
|
|
56
|
+
cls_obj = type(instance)
|
|
57
|
+
metadata = ComponentMetadata(
|
|
58
|
+
name=getattr(instance, "name", cls_obj.__name__),
|
|
59
|
+
component_type=resolved_role,
|
|
60
|
+
description=(cls_obj.__doc__.strip() if cls_obj.__doc__ else None),
|
|
61
|
+
source=getattr(cls_obj, "__module__", None),
|
|
62
|
+
)
|
|
63
|
+
spec = ComponentSpec(metadata=metadata)
|
|
64
|
+
|
|
65
|
+
if not isinstance(spec,ComponentSpec):
|
|
66
|
+
raise InvalidSpecificationError(
|
|
67
|
+
f"spec must be ComponentSpec; got {type(spec).__name__}."
|
|
68
|
+
)
|
|
69
|
+
# If the caller did not choose a role, prefer the role declared by the spec.
|
|
70
|
+
if role is None:
|
|
71
|
+
resolved_role = spec.metadata.component_type
|
|
72
|
+
|
|
73
|
+
return cls(instance=instance, role=resolved_role, spec=spec, origin=origin)
|
|
74
|
+
@property
|
|
75
|
+
def metadata(self) -> ComponentMetadata:
|
|
76
|
+
"""Convenient access to the component metadata."""
|
|
77
|
+
return self.spec.metadata
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def component_type(self) -> str:
|
|
81
|
+
"""Return the declared type from the specification."""
|
|
82
|
+
return self.spec.metadata.component_type
|
|
83
|
+
|
|
84
|
+
def get_custom_validator(self):
|
|
85
|
+
"""Return the optional user-defined validator, if one is present."""
|
|
86
|
+
validator = getattr(self.instance, "validate_component", None)
|
|
87
|
+
return validator if callable(validator) else None
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Identity and descriptive metadata for framework components."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen = True, slots = True)
|
|
9
|
+
class ComponentMetadata:
|
|
10
|
+
"""Human-readable identity for a component.
|
|
11
|
+
|
|
12
|
+
`component_type` is intentionally a string rather than a closed enum so that
|
|
13
|
+
users and plugins can introduce new component categories without modifying
|
|
14
|
+
the framework's core package.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
name: str
|
|
18
|
+
component_type: str = "component"
|
|
19
|
+
version: str | None = None
|
|
20
|
+
api_version: str | None = None
|
|
21
|
+
description: str | None = None
|
|
22
|
+
author: str | None = None
|
|
23
|
+
tags: tuple[str, ...] = ()
|
|
24
|
+
source: str | None = None
|
|
25
|
+
|
|
26
|
+
def __post_init__(self) -> None:
|
|
27
|
+
if not self.name or not self.name.strip():
|
|
28
|
+
raise ValueError("ComponentMetadata.name must be a non-empty string.")
|
|
29
|
+
if not self.component_type or not self.component_type.strip():
|
|
30
|
+
raise ValueError("ComponentMetadata.component_type must be non-empty.")
|
|
31
|
+
object.__setattr__(self, "name", self.name.strip())
|
|
32
|
+
object.__setattr__(self, "component_type", self.component_type.strip())
|
|
33
|
+
object.__setattr__(self, "tags", tuple(self.tags))
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Structural Protocols for common and optional component behaviours."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Mapping, Protocol, runtime_checkable
|
|
6
|
+
|
|
7
|
+
from reframx.core.components.specification import ComponentSpec
|
|
8
|
+
from reframx.core.validation.context import ValidationContext
|
|
9
|
+
from reframx.core.validation.report import ValidationReport
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@runtime_checkable
|
|
13
|
+
class SpecProvider(Protocol):
|
|
14
|
+
"""Optional protocol for objects that expose their own specification."""
|
|
15
|
+
|
|
16
|
+
def get_spec(self) -> ComponentSpec:
|
|
17
|
+
"""Return the component's public specification."""
|
|
18
|
+
...
|
|
19
|
+
|
|
20
|
+
@runtime_checkable
|
|
21
|
+
class ConfigProvider(Protocol):
|
|
22
|
+
"""Optional protocol for components that expose a configuration schema."""
|
|
23
|
+
|
|
24
|
+
@classmethod
|
|
25
|
+
def get_config_schema(cls) -> Any:
|
|
26
|
+
"""Return a schema understood by the chosen configuration integration."""
|
|
27
|
+
...
|
|
28
|
+
|
|
29
|
+
@runtime_checkable
|
|
30
|
+
class ComponentValidator(Protocol):
|
|
31
|
+
"""Optional protocol for component-specific validation."""
|
|
32
|
+
|
|
33
|
+
def validate_component(self, context: ValidationContext) -> ValidationReport:
|
|
34
|
+
"""Validate this component in its current integration context."""
|
|
35
|
+
...
|
|
36
|
+
|
|
37
|
+
@runtime_checkable
|
|
38
|
+
class StateProvider(Protocol):
|
|
39
|
+
"""Optional protocol for components with restorable custom runtime state."""
|
|
40
|
+
|
|
41
|
+
def get_component_state(self) -> Mapping[str, Any]:
|
|
42
|
+
"""Return the component-owned state needed for restoration."""
|
|
43
|
+
...
|
|
44
|
+
|
|
45
|
+
def load_component_state(self, state: Mapping[str, Any]) -> None:
|
|
46
|
+
"""Restore state previously returned by `get_component_state`."""
|
|
47
|
+
...
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Serializable description of a component's public contract."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from typing import Any, Mapping
|
|
7
|
+
|
|
8
|
+
from reframx.core.components.metadata import ComponentMetadata
|
|
9
|
+
from reframx.core.contracts.capabilities import (
|
|
10
|
+
Capability,
|
|
11
|
+
coerce_capability,
|
|
12
|
+
)
|
|
13
|
+
from reframx.core.contracts.requirements import (
|
|
14
|
+
Requirement,
|
|
15
|
+
RequirementGroup,
|
|
16
|
+
coerce_requirement,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True, slots=True)
|
|
21
|
+
class ComponentSpec:
|
|
22
|
+
"""Common specification shared by all component types.
|
|
23
|
+
|
|
24
|
+
Specialised specifications (TaskSpec, ModelSpec, ...) should compose or
|
|
25
|
+
extend this structure when their interfaces are designed. `attributes`
|
|
26
|
+
holds additional descriptive data; it is not a place for runtime state.
|
|
27
|
+
"""
|
|
28
|
+
metadata: ComponentMetadata
|
|
29
|
+
requirements: tuple[Requirement | RequirementGroup, ...] = ()
|
|
30
|
+
capabilities: tuple[Capability, ...] = ()
|
|
31
|
+
attributes: Mapping[str, Any] = field(default_factory=dict)
|
|
32
|
+
|
|
33
|
+
def __post_init__(self) -> None:
|
|
34
|
+
if not isinstance(self.metadata, ComponentMetadata):
|
|
35
|
+
raise TypeError("ComponentSpec.metadata must be ComponentMetadata.")
|
|
36
|
+
raw_reqs = (
|
|
37
|
+
(self.requirements,)
|
|
38
|
+
if isinstance(self.requirements, (str, Mapping, Requirement, RequirementGroup))
|
|
39
|
+
else self.requirements
|
|
40
|
+
)
|
|
41
|
+
raw_caps = (
|
|
42
|
+
(self.capabilities,)
|
|
43
|
+
if isinstance(self.capabilities, (str, Mapping, Capability))
|
|
44
|
+
else self.capabilities
|
|
45
|
+
)
|
|
46
|
+
normalized_reqs = tuple(coerce_requirement(r) for r in raw_reqs)
|
|
47
|
+
normalized_caps = tuple(coerce_capability(c) for c in raw_caps)
|
|
48
|
+
object.__setattr__(self, "requirements", normalized_reqs)
|
|
49
|
+
object.__setattr__(self, "capabilities", normalized_caps)
|
|
50
|
+
object.__setattr__(self, "attributes", dict(self.attributes))
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Capabilities a component declares that it provides."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Iterable, Mapping
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from typing import Any, TypeAlias
|
|
8
|
+
|
|
9
|
+
from .io import IOContract, coerce_io_contract
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True, slots=True)
|
|
13
|
+
class Capability:
|
|
14
|
+
"""A named ability exposed by a component.
|
|
15
|
+
|
|
16
|
+
The framework does not infer that a capability is true merely from the
|
|
17
|
+
component's architecture. It records the declaration and may run optional
|
|
18
|
+
user-provided checks to test it.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
name: str
|
|
22
|
+
description: str | None = None
|
|
23
|
+
contract: IOContract = field(default_factory=IOContract)
|
|
24
|
+
properties: Mapping[str, Any] = field(default_factory=dict)
|
|
25
|
+
|
|
26
|
+
def __post_init__(self) -> None:
|
|
27
|
+
if not self.name or not self.name.strip():
|
|
28
|
+
raise ValueError("Capability.name must be a non-empty string.")
|
|
29
|
+
object.__setattr__(self, "name", self.name.strip())
|
|
30
|
+
object.__setattr__(self, "contract", coerce_io_contract(self.contract))
|
|
31
|
+
object.__setattr__(self, "properties", dict(self.properties))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
CapabilityLike: TypeAlias = Capability | str | Mapping[str, Any]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def coerce_capability(item: CapabilityLike) -> Capability:
|
|
38
|
+
"""Coerce a ``str``, ``Mapping``, or ``Capability`` into a ``Capability``."""
|
|
39
|
+
if isinstance(item, Capability):
|
|
40
|
+
return item
|
|
41
|
+
if isinstance(item, str):
|
|
42
|
+
return Capability(item)
|
|
43
|
+
if isinstance(item, Mapping):
|
|
44
|
+
return Capability(**item)
|
|
45
|
+
raise TypeError(
|
|
46
|
+
"ComponentSpec.capabilities must contain Capability objects, strings, "
|
|
47
|
+
f"or mappings; got {type(item).__name__}."
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
CapabilitiesInput: TypeAlias = CapabilityLike | Iterable[CapabilityLike]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def coerce_capabilities(items: CapabilitiesInput) -> tuple[Capability, ...]:
|
|
55
|
+
"""Normalize a single capability or iterable of capabilities into a tuple."""
|
|
56
|
+
if isinstance(items, (Capability, str, Mapping)):
|
|
57
|
+
return (coerce_capability(items),)
|
|
58
|
+
return tuple(coerce_capability(item) for item in items)
|