-
Notifications
You must be signed in to change notification settings - Fork 0
Fix modulo calculation #15
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
Conversation
WalkthroughThe recent updates involve refining the argument structure and logic of two functions that preserve zero values during modulo operations. Additionally, a new test has been introduced to ensure these functions properly handle background labels, strengthening the robustness of the codebase in processing image data within the application. Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review Status
Actionable comments generated: 3
Configuration used: CodeRabbit UI
Files selected for processing (2)
- src/PartSegCore_compiled_backend/_napari_mapping.pyx (1 hunks)
- src/tests/test_napari_mapping.py (1 hunks)
@pytest.mark.parametrize('func', [zero_preserving_modulo_parallel, zero_preserving_modulo_sequential]) | ||
@pytest.mark.parametrize('background_num', [0, 1, 2, -1]) | ||
def test_background_label(func, background_num): | ||
data = np.zeros((10, 10), dtype=np.int32) | ||
data[1:-1, 1:-1] = 1 | ||
data[2:-2, 2:-2] = 2 | ||
data[4:-4, 4:-4] = -1 | ||
|
||
res = func(data, 49, background_num) | ||
np.testing.assert_array_equal(res == 0, data == background_num) | ||
np.testing.assert_array_equal(res != 0, data != background_num) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new test test_background_label
is well-structured and uses parameterization effectively to test different background_num
values. The assertions are correctly checking for the expected behavior of the zero_preserving_modulo_parallel
and zero_preserving_modulo_sequential
functions. However, there is no check for the actual values of the result array res
, only that the background label is preserved. It would be beneficial to add assertions that verify the correctness of the modulo operation for non-background values as well.
Consider adding additional assertions to verify the correctness of the modulo operation for non-background values in the test_background_label
function.
def _zero_preserving_modulo_seq(label_types[:] labels, out_types_mod modulo, label_types to_zero, out_types_mod[:] out): | ||
cdef Py_ssize_t i | ||
cdef Py_ssize_t n = labels.shape[0] | ||
for i in range(n): | ||
if labels[i] == to_zero: | ||
out[i] = 0 | ||
else: | ||
out[i] = ((labels[i] - 1) % modulo) + 1 | ||
out[i] = (modulo + ((labels[i] - 1) % modulo) % modulo) + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The signature of the function _zero_preserving_modulo_seq
has been modified to change the type of the modulo
parameter from label_types
to out_types_mod
. This change is consistent with the PR objectives to ensure the modulo operation aligns with Python's behavior. However, the modulo operation logic (modulo + ((labels[i] - 1) % modulo) % modulo) + 1
seems overly complex and potentially incorrect. The expected behavior is to mimic Python's modulo operation, which can be achieved with a simpler expression.
The modulo operation logic appears to be incorrect and overly complex. Simplify the expression to correctly implement Python's modulo behavior.
- out[i] = (modulo + ((labels[i] - 1) % modulo) % modulo) + 1
+ out[i] = ((labels[i] - 1) % modulo) + 1
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
def _zero_preserving_modulo_seq(label_types[:] labels, out_types_mod modulo, label_types to_zero, out_types_mod[:] out): | |
cdef Py_ssize_t i | |
cdef Py_ssize_t n = labels.shape[0] | |
for i in range(n): | |
if labels[i] == to_zero: | |
out[i] = 0 | |
else: | |
out[i] = ((labels[i] - 1) % modulo) + 1 | |
out[i] = (modulo + ((labels[i] - 1) % modulo) % modulo) + 1 | |
def _zero_preserving_modulo_seq(label_types[:] labels, out_types_mod modulo, label_types to_zero, out_types_mod[:] out): | |
cdef Py_ssize_t i | |
cdef Py_ssize_t n = labels.shape[0] | |
for i in range(n): | |
if labels[i] == to_zero: | |
out[i] = 0 | |
else: | |
out[i] = ((labels[i] - 1) % modulo) + 1 |
def _zero_preserving_modulo_par(label_types[:] labels, out_types_mod modulo, label_types to_zero, out_types_mod[:] out): | ||
cdef Py_ssize_t i | ||
cdef Py_ssize_t n = labels.shape[0] | ||
for i in prange(n, nogil=True): | ||
if labels[i] == to_zero: | ||
out[i] = 0 | ||
else: | ||
out[i] = ((labels[i] - 1) % modulo) + 1 | ||
out[i] = (modulo + ((labels[i] - 1) % modulo) % modulo) + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes in the _zero_preserving_modulo_par
function mirror those in _zero_preserving_modulo_seq
. The same issue with the modulo operation logic applies here.
The modulo operation logic in _zero_preserving_modulo_par
should be simplified to correctly implement Python's modulo behavior.
- out[i] = (modulo + ((labels[i] - 1) % modulo) % modulo) + 1
+ out[i] = ((labels[i] - 1) % modulo) + 1
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
def _zero_preserving_modulo_par(label_types[:] labels, out_types_mod modulo, label_types to_zero, out_types_mod[:] out): | |
cdef Py_ssize_t i | |
cdef Py_ssize_t n = labels.shape[0] | |
for i in prange(n, nogil=True): | |
if labels[i] == to_zero: | |
out[i] = 0 | |
else: | |
out[i] = ((labels[i] - 1) % modulo) + 1 | |
out[i] = (modulo + ((labels[i] - 1) % modulo) % modulo) + 1 | |
def _zero_preserving_modulo_par(label_types[:] labels, out_types_mod modulo, label_types to_zero, out_types_mod[:] out): | |
cdef Py_ssize_t i | |
cdef Py_ssize_t n = labels.shape[0] | |
for i in prange(n, nogil=True): | |
if labels[i] == to_zero: | |
out[i] = 0 | |
else: | |
out[i] = ((labels[i] - 1) % modulo) + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review Status
Actionable comments generated: 0
Configuration used: CodeRabbit UI
Files selected for processing (1)
- src/PartSegCore_compiled_backend/_napari_mapping.pyx (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- src/PartSegCore_compiled_backend/_napari_mapping.pyx
When c/c++ modulo operation for negative numbers returns negative value, when python one returns positive values:
will print -1
Will print 44.
This PR fixes this.
Summary by CodeRabbit
Refactor
Tests