| """Adapt hypothesis modules' build_spatial_code() calling convention across formats. |
| |
| Mirrors symbolic/adapters.py's job (absorb a spatial-code schema difference behind one call), |
| but the difference handled here is in the HYPOTHESIS MODULE ITSELF, not the on-disk JSON: each |
| file under experiments/hypotheses/ is a full standalone fork of encoder/geometric.py. |
| |
| - Older hypotheses were forked before the compact schema existed and expose a single-arg |
| build_spatial_code(scene) that always builds the "explicit" answer-oriented schema. |
| - Newer hypotheses (forked from the current encoder/geometric.py, which already supports both |
| schemas from one function) expose build_spatial_code(scene, spatial_code_format="explicit"), |
| matching encoder/geometric.py's own real entry point. |
| |
| experiments/run.py calls build() below instead of the hypothesis module directly, so callers |
| never need to know which style a given hypothesis file uses. |
| """ |
|
|
| from __future__ import annotations |
|
|
| import inspect |
| from types import ModuleType |
|
|
| from experiments.config import SPATIAL_CODE_FORMATS, validate_spatial_code_format |
|
|
|
|
| def supports_compact(hypothesis_module: ModuleType) -> bool: |
| """Whether this hypothesis module's build_spatial_code() accepts a format argument.""" |
| params = inspect.signature(hypothesis_module.build_spatial_code).parameters |
| return len(params) >= 2 |
|
|
|
|
| def build(hypothesis_module: ModuleType, scene, spatial_code_format: str = "explicit"): |
| """Build one spatial code from a loaded hypothesis module, in the requested format. |
| |
| Raises ValueError if an "explicit"-only (older-style) hypothesis is asked to build |
| "compact" -- that hypothesis genuinely cannot produce that schema, so failing loudly here |
| is preferable to silently building the wrong format. |
| """ |
| validate_spatial_code_format(spatial_code_format) |
| if supports_compact(hypothesis_module): |
| return hypothesis_module.build_spatial_code(scene, spatial_code_format) |
| if spatial_code_format != "explicit": |
| raise ValueError( |
| f"{hypothesis_module.__name__} only supports the 'explicit' spatial-code " |
| f"format (its build_spatial_code() takes a single scene argument); requested " |
| f"{spatial_code_format!r}" |
| ) |
| return hypothesis_module.build_spatial_code(scene) |
|
|
|
|
| __all__ = ["SPATIAL_CODE_FORMATS", "supports_compact", "build"] |
|
|