feat: improved backend, union handling, tensor creation APIs #68
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.
Overview
This pull request introduces refactoring and enhancements to the
camtools
backend module, focusing on improved type handling, backend compatibility, and streamlined operations across NumPy and PyTorch. The changes aim to enhance maintainability, performance, and usability.Key Changes
Renaming of Backend Decorators:
@tensor_to_auto_backend
to@tensor_backend_auto
to clarify its automatic backend selection functionality.@tensor_to_numpy_backend
to@tensor_backend_numpy
and@tensor_to_torch_backend
to@tensor_backend_torch
, making their specific backend operations more explicit.Enhanced Type Hints and Tensor Operations:
Union
types, allowing more versatile function signatures.create_array
,create_ones
,create_zeros
, andcreate_empty
. These functions standardize tensor creation across different backends and are explicitly tied to the specified backend literal.Advanced Tensor Type and Shape Checks:
_assert_tensor_hint
to handleUnion
types effectively, enhancing the robustness of type and shape verifications.ValueError
exceptions related to tensor shapes and dtypes, standardizing error messages across the module.Backend Handling Improvements:
@tensor_backend_auto
decorator for more efficient backend determination and tensor conversion.Literal["numpy", "torch"]
parameter to manage backend-dependent tensor operations better.Documentation and Code Clean-Up:
Testing and Compatibility
test_backend.py
to cover new scenarios introduced by Union type hints and new tensor creation functions.