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 numpy warnings #510

Merged
merged 11 commits into from
Nov 19, 2024
Merged

Fix numpy warnings #510

merged 11 commits into from
Nov 19, 2024

Conversation

waltsims
Copy link
Owner

No description provided.

@waltsims
Copy link
Owner Author

Matlab_mask is the current issue. I have to replicate legacy behavior.

Copy link

codecov bot commented Nov 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 72.77%. Comparing base (405aea4) to head (bca3a27).
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #510      +/-   ##
==========================================
+ Coverage   72.75%   72.77%   +0.02%     
==========================================
  Files          46       46              
  Lines        6783     6789       +6     
  Branches     1304     1305       +1     
==========================================
+ Hits         4935     4941       +6     
  Misses       1290     1290              
  Partials      558      558              
Flag Coverage Δ
3.10 72.77% <100.00%> (+0.02%) ⬆️
3.11 72.77% <100.00%> (+0.02%) ⬆️
3.12 72.77% <100.00%> (+0.02%) ⬆️
3.13 72.77% <100.00%> (+0.05%) ⬆️
macos-latest 72.75% <100.00%> (+0.02%) ⬆️
ubuntu-latest 72.75% <100.00%> (+0.02%) ⬆️
windows-latest 72.75% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@waltsims
Copy link
Owner Author

There is a lot of weirdness in here with the uint8 typing. We should clean this up in the future. For thow this seems to allow the CI to pass.

@@ -75,6 +75,7 @@ def matlab_find(arr: Union[List[int], np.ndarray], val: int = 0, mode: str = "ne
return np.expand_dims(arr, -1) # compatibility, n => [n, 1]


@typechecker
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not required for this PR but I just added it while I was in the file

@@ -89,6 +90,9 @@ def matlab_mask(arr: np.ndarray, mask: np.ndarray, diff: Optional[int] = None) -

"""

if mask.dtype == "uint8":
mask = mask.astype(np.int16)

if diff is None:
return np.expand_dims(arr.ravel(order="F")[mask.ravel(order="F")], axis=-1) # compatibility, n => [n, 1]
else:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there was an issue with unit8 roll-over to positive indexes. This is solved with negative indexing in python and therefore using int16 is acceptable.

@@ -6,7 +6,7 @@

import numpy as np
from numpy import arcsin, pi, cos, size, array
from numpy.linalg import linalg
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numpy depricated this import structure for the new one I modified

@@ -644,7 +644,7 @@ def delay_mask(self, mode=None):
].min() # -1s compatibility
else:
mask[unflatten_matlab_mask(mask, active_elements_index - 1)] += self.stored_beamforming_delays_offset # -1s compatibility
return mask.astype(np.uint8)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again here for one of the operations, the unit8 type was not being correctly promoted for values larger than 255 so I bumped the type.

Copy link
Collaborator

@faridyagubbayli faridyagubbayli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Left a comment

@@ -89,6 +90,9 @@ def matlab_mask(arr: np.ndarray, mask: np.ndarray, diff: Optional[int] = None) -

"""

if mask.dtype == "uint8":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we convert uint8 to uint16 and int8 to int16?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we promote from uint8 to uint16 we will have the same issue with negative indexing and value wrapping. I have tried it to see.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exaclty, current "value -1 is out of bounds of uint8 type"

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> x
array([0], dtype=uint8)
>>> x - 1
array([255], dtype=uint8)
>>> np.version
<module 'numpy.version' from '/Users/walters/miniconda3/lib/python3.12/site-packages/numpy/version.py'>
>>> np.__version__
'1.26.4'

Here is the legacy behavior

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the new behavior:

>>> import numpy as np
>>> x = np.array([0], dtype='uint8')
>>> x - 1
array([255], dtype=uint8)
>>> np.__version__
'2.1.3'

looks like my assumption was wrong

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be an issue with type promotion with the + operator.

>>> import numpy as np
>>> x = np.array([0], dtype='uint8')
>>> x - 1
array([255], dtype=uint8)
>>> x.ravel(order='F') + -1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python integer -1 out of bounds for uint8
>>> x.ravel(order='F').astype("int8") + -1
array([-1], dtype=int8)
>>> x + -1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python integer -1 out of bounds for uint8
>>> np.__version__
'2.1.3'

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all related to this:
numpy/numpy#25639 (comment)

@waltsims waltsims merged commit 85be199 into master Nov 19, 2024
44 checks passed
@waltsims waltsims deleted the numpy-patch branch November 19, 2024 23:29
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.

2 participants