Add systematic distribution representation conversion#1929
Add systematic distribution representation conversion#1929FlorianPfaff merged 7 commits intomainfrom
Conversation
❌MegaLinter analysis: Error
Detailed Issues❌ PYTHON / mypy - 1 errorSee detailed reports in MegaLinter artifacts Your project could benefit from a custom flavor, which would allow you to run only the linters you need, and thus improve runtime performances. (Skip this info by defining
|
There was a problem hiding this comment.
Pull request overview
This PR introduces an initial, target-centric “representation conversion” gateway for PyRecEst distributions, providing a single public API to convert/approximate between distribution representations while delegating conversion logic to existing from_distribution(...) hooks.
Changes:
- Added
pyrecest.distributions.conversionwithconvert_distribution,can_convert,register_conversion,registered_conversions, andConversionResultmetadata. - Added
convert_to(...)/approximate_as(...)convenience methods onAbstractDistributionType. - Added unit tests and user documentation (MkDocs page + navigation + API overview mention).
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/distributions/test_conversion.py | Adds tests for conversion routing, identity conversion metadata, and argument validation. |
| src/pyrecest/distributions/conversion.py | Implements the conversion registry and gateway dispatch + metadata + kwargs validation. |
| src/pyrecest/distributions/abstract_distribution_type.py | Adds convenience wrappers delegating to the conversion gateway. |
| mkdocs.yml | Adds MkDocs nav entry for the new representation conversion guide. |
| docs/representation-conversion.md | Documents the conversion gateway, metadata, and registration API. |
| docs/api-overview.md | Adds a brief API overview mention and link to the new conversion docs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for entry in _matching_registered_conversions(distribution, target_type): | ||
| converted = _call_conversion_callable(entry.converter, distribution, kwargs) | ||
| result = ConversionResult( | ||
| distribution=converted, | ||
| source_type=source_type, | ||
| target_type=target_type, | ||
| method=entry.method, | ||
| exact=entry.exact, | ||
| parameters=dict(kwargs), | ||
| ) | ||
| return result if return_info else result.distribution | ||
|
|
||
| from_distribution = getattr(target_type, "from_distribution", None) | ||
| if callable(from_distribution): | ||
| converted = _call_conversion_callable( | ||
| from_distribution, | ||
| distribution, | ||
| kwargs, | ||
| conversion_name=f"{target_type.__name__}.from_distribution", | ||
| ) | ||
| result = ConversionResult( | ||
| distribution=converted, | ||
| source_type=source_type, | ||
| target_type=target_type, | ||
| method=f"{target_type.__name__}.from_distribution", | ||
| exact=False, | ||
| parameters=dict(kwargs), | ||
| ) | ||
| return result if return_info else result.distribution |
| _validate_conversion_arguments(func, kwargs, name) | ||
| try: | ||
| return func(distribution, **kwargs) | ||
| except TypeError as exc: |
| """Convert or approximate this distribution as ``target_type``. | ||
|
|
||
| This is a convenience wrapper around | ||
| :func:`pyrecest.distributions.convert_distribution`. |
| - `CircularFourierDistribution.from_distribution(...)` builds Fourier | ||
| coefficients from samples or grid values. | ||
| - `GaussianDistribution.from_distribution(...)` performs Gaussian moment | ||
| matching when the source exposes `mean()` and `covariance()`. |

This PR adds a first systematic representation-conversion layer for PyRecEst.
Main changes:
pyrecest.distributions.conversionconvert_distribution,can_convert,register_conversion, andConversionResultfrom_distribution(...)hooksconvert_to(...)/approximate_as(...)convenience methods toAbstractDistributionTypeDesign notes:
pyrecest.distributions.conversion; a later cleanup PR can re-export the gateway frompyrecest.distributionsif desired.