Skip to content
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

Fix type annotations for array inputs in signal functions #63

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

Conversation

Spkap
Copy link

@Spkap Spkap commented Mar 17, 2025

This PR fixes the type annotations in signal_linear() and signal_SPGR() functions to properly reflect that they handle both scalar and array inputs.

The fix uses NDArray[np.float64] to annotate the parameters and return values, which correctly handles both NumPy scalars (0D arrays) and multi-dimensional arrays.

Fixes #61

Update type annotations in signal_linear and signal_SPGR to correctly reflect that they handle both numpy scalar and array inputs using NDArray.
@Spkap
Copy link
Author

Spkap commented Mar 17, 2025

@ltorres6, this PR addresses issue #61. Please review it and let me know if you have any feedback. Thank you!

@ltorres6
Copy link
Collaborator

Sorry for the confusion. There are some paramters that will likely always remain scalars, and should not be arrays. TR, k, and a should be scalars.

Additionally, would you mind ensuring we accept both float32 and float64?

@Spkap
Copy link
Author

Spkap commented Mar 17, 2025

I've used np.floating to support both float32 and float64 instead of union types. Is this approach optimal, or would you prefer a different implementation?

@plaresmedima
Copy link
Collaborator

plaresmedima commented Mar 18, 2025 via email

@Spkap
Copy link
Author

Spkap commented Mar 19, 2025

@plaresmedima Thank you for your suggestions. I completely agree that we need a consistent package-wide typing approach rather than function-by-function decisions.

You raise an excellent point about lists currently the annotations would reject them when NumPy users would expect them to work. We should follow NumPy's pattern of accepting array-like objects (lists, tuples, etc.) and converting them internally.

For a consistent approach, I see two main options:

  1. Use NumPy's built-in ArrayLike type and convert inputs with np.asarray():

    from numpy.typing import ArrayLike
    
    def signal_linear(R1: ArrayLike, k: float) -> np.ndarray:
        R1 = np.asarray(R1)
        return k * R1
  2. Create input validation functions with informative error messages that enforce consistent behavior across the package.

What are your thoughts on these approaches? Alternatively, please feel free to suggest any ideas you might have.

@Spkap
Copy link
Author

Spkap commented Mar 19, 2025

@ltorres6 I've updated the PR accordingly, now we support both float32 and float64 data types. Please review it and let me know if you have any feedback. Thank you!

@plaresmedima
Copy link
Collaborator

plaresmedima commented Mar 19, 2025 via email

@Spkap
Copy link
Author

Spkap commented Mar 21, 2025

@ltorres6, just following up to see if the latest changes align with your expectations. The PR now supports both float32 and float64 while ensuring that TR, k, and a remain scalars.

When you have a moment, could you please review the changes and let me know if anything else needs adjustment? Thanks!

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.

Signal functions have incorrect type annotations for array inputs
3 participants