Skip to content

feat: Implement flexible ArrayLike input handling for arrays, lists, and tuples #73

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Spkap
Copy link
Contributor

@Spkap Spkap commented Apr 18, 2025

This PR enhances the flexibility and usability of osipi functions by implementing support for array-like inputs. Users can now pass Python lists and tuples, in addition to np.ndarrays, for parameters representing array data.

Background:

During the review of PR #63 (which focused on fixing initial type annotations using NDArray), @plaresmedima and @ltorres6 suggested improving usability by accepting more general array-like types (like lists and tuples), aligning better with standard NumPy practice. This PR fulfills that follow-up action.

How it Works:

  1. For Array-Like Inputs (t, ca, R1, S, etc.):

    • Type hints changed from NDArray to numpy.typing.ArrayLike.
    • Functions now call np.asarray() on these inputs upon entry. This converts lists, tuples, or existing arrays into NumPy ndarrays.
    • A subsequent check ensures the resulting array has a floating-point dtype, converting if necessary (.astype(np.float64)).
    • All downstream calculations use the ensured ndarray.
  2. For Scalar Inputs (k, TR, a, Ktrans, ve, r1, etc.):

    • These parameters retain their np.floating type hints.
    • Crucially, np.asarray() is not called on scalars. They are used directly.
    • NumPy's broadcasting handles operations between these scalars and the converted arrays.

Consistency Achieved:

  • Uniform Input Handling: Provides a consistent mechanism (ArrayLike + np.asarray) for all parameters designed to accept array data.
  • Clear Scalar Handling: Maintains a distinct approach for single-value parameters.

Testing:

  • A new test suite (tests/test_array_like_inputs.py) has been added to verify:
    • Correct function output with np.ndarray, list, and tuple inputs.
    • Proper handling of float32 and float64 dtypes.
    • Appropriate TypeError or ValueError is raised for invalid inputs (e.g., strings, incorrect dimensions).
  • Existing functionality tests continue to pass, ensuring no regressions.
  • All tests were run using pytest and passed successfully before submission.

Implements the suggestion from PR OSIPI#63 to improve input flexibility using `numpy.typing.ArrayLike`. Allows lists and tuples as inputs alongside NumPy arrays.

- Replaced `NDArray` hints with `ArrayLike` for array parameters.
- Added internal `np.asarray()` for conversion.
- Maintained float dtype checks post-conversion.
- Scalar parameters (`np.floating`) remain unchanged.
- Added new tests (`test_array_like_inputs.py`) covering list/tuple inputs, float dtypes, and error handling.
@Spkap
Copy link
Contributor Author

Spkap commented Apr 18, 2025

Hi @ltorres6,

Following up on our discussion in PR #63, I've created a separate PR to implement ArrayLike support for more flexible array inputs (lists, tuples). I have also ran tests locally with pytest.

Could you please review it when you have a chance? I will address any feedback and make any necessary changes.

Thanks you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant