8000 Fix modulo calculation by Czaki · Pull Request #15 · 4DNucleome/PartSegCore-compiled-backend · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Merged
merged 2 commits into from
Jan 24, 2024
Merged

Fix modulo calculation #15

merged 2 commits into from
Jan 24, 2024

Conversation

Czaki
Copy link
Contributor
@Czaki Czaki commented Jan 24, 2024

When c/c++ modulo operation for negative numbers returns negative value, when python one returns positive values:

#include<iostream>

int main() {
    int result = -1 % 45;
    std::cout << result;
    return 0;
}

will print -1

print(-1 % 45)

Will print 44.

This PR fixes this.

Summary by CodeRabbit

  • Refactor

    • Improved internal logic for certain processing functions to enhance performance and accuracy.
  • Tests

    • Implemented new tests to ensure correct handling of background labels in data processing.

Copy link
Contributor
coderabbitai bot commented Jan 24, 2024

Walkthrough

The 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

File Path Summary of Changes
..._compiled_backend/_napari_mapping.pyx Modified argument order and logic in _zero_preserving_modulo_seq and _zero_preserving_modulo_par functions.
.../tests/test_napari_mapping.py Added test_background_label to verify correct handling of background labels by zero_preserving_modulo_parallel and zero_preserving_modulo_sequential.

Poem

🐇✨
Code hops through the field of bits,
Fixing bugs and logic splits.
With each leap, the tests affirm,
No label in the background squirms.

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?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor
@coderabbitai coderabbitai bot left a 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

Commits Files that changed from the base of the PR and between 10215d6 and 166bcb6.
Files selected for processing (2)
  • src/PartSegCore_compiled_backend/_napari_mapping.pyx (1 hunks)
  • src/tests/test_napari_mapping.py (1 hunks)

Comment on lines +40 to +51
@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)

Copy link
Contributor

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.

Comment on lines 31 to 38
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
Copy link
Contributor

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.

Suggested change
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

Comment on lines 40 to 47
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
Copy link
Contributor

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.

Suggested change
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

Copy link
Contributor
@coderabbitai coderabbitai bot left a 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

Commits Files that changed from the base of the PR and between 166bcb6 and 67b7a65.
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

@Czaki Czaki merged commit d53dceb into master Jan 24, 2024
@Czaki Czaki deleted the fix_modulo branch January 24, 2024 17:02
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
0