Skip to content

🧪 Add unit tests for coherence_index calculation#11

Draft
Sir-Ripley wants to merge 1 commit intomainfrom
test-coherence-index-17589903377000490864
Draft

🧪 Add unit tests for coherence_index calculation#11
Sir-Ripley wants to merge 1 commit intomainfrom
test-coherence-index-17589903377000490864

Conversation

@Sir-Ripley
Copy link
Owner

@Sir-Ripley Sir-Ripley commented Mar 14, 2026

🎯 What: The coherence_index function in Copy_of_Untitled7.ipynb was missing tests, making it susceptible to regressions. Added a new cell with a unit test to verify the function's output.

📊 Coverage: The tests now cover:

  • Boundary input (r=0)
  • Typical inputs (r=10, rho_info=1.0)
  • NumPy array inputs

Result: Increased test coverage and confidence in the coherence_index logic. Tests are executed under the if __name__ == "__main__": block as per existing convention.


PR created automatically by Jules for task 17589903377000490864 started by @Sir-Ripley

Summary by Sourcery

Tests:

  • Introduce a test function that exercises coherence_index on boundary, typical, and NumPy array inputs, and wire it to run under the module main guard.

Co-authored-by: Sir-Ripley <31619989+Sir-Ripley@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 14, 2026

Reviewer's Guide

Adds an inline unit-test cell in the notebook to exercise coherence_index across scalar and array inputs, and wires it to run under the existing main convention; also introduces a .gitignore file (content not shown in diff).

Sequence diagram for coherence_index unit test execution

sequenceDiagram
    actor User
    participant PythonInterpreter
    participant NotebookMainCell as Notebook_main_block
    participant TestFunction as test_coherence_index
    participant CoherenceFunction as coherence_index
    participant NumPy as numpy

    User ->> PythonInterpreter: run notebook
    PythonInterpreter ->> NotebookMainCell: evaluate cells
    NotebookMainCell ->> NotebookMainCell: check __name__ == __main__
    NotebookMainCell ->> TestFunction: test_coherence_index()

    TestFunction ->> NumPy: import numpy as np

    TestFunction ->> CoherenceFunction: coherence_index(0, 1.5)
    CoherenceFunction -->> TestFunction: result_scalar_r0
    TestFunction ->> NumPy: np.isclose(result_scalar_r0, 1.0)
    NumPy -->> TestFunction: bool_r0

    TestFunction ->> CoherenceFunction: coherence_index(10, 1.0)
    CoherenceFunction -->> TestFunction: result_scalar_typical
    TestFunction ->> NumPy: np.exp(-10 / (10 * 1.0))
    NumPy -->> TestFunction: expected_scalar_typical
    TestFunction ->> NumPy: np.isclose(result_scalar_typical, expected_scalar_typical)
    NumPy -->> TestFunction: bool_typical

    TestFunction ->> NumPy: np.array([0, 10, 20])
    NumPy -->> TestFunction: r_arr
    TestFunction ->> CoherenceFunction: coherence_index(r_arr, 1.0)
    CoherenceFunction -->> TestFunction: result_array
    TestFunction ->> NumPy: np.exp(-r_arr / (10 * 1.0))
    NumPy -->> TestFunction: expected_array
    TestFunction ->> NumPy: np.testing.assert_allclose(result_array, expected_array)
    NumPy -->> TestFunction: assertion_ok

    TestFunction -->> NotebookMainCell: tests_done
    NotebookMainCell -->> PythonInterpreter: print All tests passed
    PythonInterpreter -->> User: output
Loading

Flow diagram for test_coherence_index logic

flowchart TD
    A["Start test_coherence_index"] --> B["Import numpy as np"]

    B --> C["Compute scalar case r = 0, rho_info = 1.5"]
    C --> D["Call coherence_index(0, 1.5)"]
    D --> E["Assert np.isclose(result, 1.0)"]

    E --> F["Set r = 10, rho_info = 1.0"]
    F --> G["Compute expected = np.exp(-10 / (10 * 1.0))"]
    G --> H["Call coherence_index(r, rho_info)"]
    H --> I["Assert np.isclose(result, expected)"]

    I --> J["Create r_arr = np.array([0, 10, 20])"]
    J --> K["Compute expected_arr = np.exp(-r_arr / (10 * 1.0))"]
    K --> L["Call coherence_index(r_arr, 1.0)"]
    L --> M["np.testing.assert_allclose(result_arr, expected_arr)"]

    M --> N["Print All tests for coherence_index passed!"]
    N --> O["End test_coherence_index"]
Loading

File-Level Changes

Change Details Files
Add inline unit tests for coherence_index in the notebook, covering scalar, array, and boundary inputs and executing them via a main guard.
  • Introduce a new code cell that imports NumPy and defines test_coherence_index with assertions for r=0, typical scalar inputs, and NumPy array inputs.
  • Use np.isclose and np.testing.assert_allclose to validate coherence_index outputs against analytically computed expected values.
  • Print a success message from the test function instead of integrating with a testing framework.
  • Add an if name == "main": block in the new cell to execute test_coherence_index when the notebook is run as a script.
Copy_of_Untitled7.ipynb
Add a .gitignore to the repository (contents not present in the provided diff).
  • Create a new .gitignore file to exclude repository artifacts from version control (exact patterns not visible in this snippet).
.gitignore

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on enhancing the reliability of the coherence_index function by introducing comprehensive unit tests. The primary goal is to prevent regressions and ensure the function behaves as expected across various input scenarios, thereby improving the overall stability of the codebase. Additionally, a minor update to the .gitignore file was made to streamline development environment management.

Highlights

  • Unit Test Addition: Added a new unit test cell to Copy_of_Untitled7.ipynb specifically for the coherence_index function.
  • Test Coverage: The new tests cover boundary inputs (r=0), typical inputs (r=10, rho_info=1.0), and NumPy array inputs, increasing confidence in the function's logic.
  • Dependency Management: Included venv in the .gitignore file to prevent virtual environment directories from being committed.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • .gitignore
    • Added 'venv' to ignore virtual environment directories.
  • Copy_of_Untitled7.ipynb
    • Introduced a new code cell containing test_coherence_index to validate the coherence_index function.
    • Tests include checks for zero input, typical values, and NumPy array processing.
    • The tests are executed within an if __name__ == "__main__": block.
Activity
  • The pull request was automatically created by Jules for a task initiated by @Sir-Ripley.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds valuable unit tests for the coherence_index function, significantly improving the robustness and maintainability of the code. The tests cover several key scenarios, including boundary conditions and array inputs. My feedback focuses on enhancing the test suite further by using pre-calculated values for assertions, which makes tests less brittle, and by adding coverage for edge cases like division by zero to make the tests more comprehensive.

Comment on lines +239 to +248
" # Test with typical values\n",
" r = 10\n",
" rho_info = 1.0\n",
" expected = np.exp(-10 / (10 * 1.0))\n",
" assert np.isclose(coherence_index(r, rho_info), expected), \"Failed on typical values\"\n",
" \n",
" # Test with array input\n",
" r_arr = np.array([0, 10, 20])\n",
" expected_arr = np.exp(-r_arr / (10 * 1.0))\n",
" np.testing.assert_allclose(coherence_index(r_arr, 1.0), expected_arr, err_msg=\"Failed on array input\")\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While these tests are a great start, they can be made more robust and maintainable. Currently, the expected values are calculated by re-implementing the logic from the coherence_index function. This makes the tests brittle; if the implementation of coherence_index changes (e.g., the constant 10 is updated), these tests might still pass if the same logic error is made in both places, or they will require updating the same logic in two places.

A better practice is to test against pre-calculated, known-good values. This verifies that the function produces the correct output for a given input, making the test's intent clearer and more resilient to implementation changes.

    # Test with typical values
    r = 10
    rho_info = 1.0
    expected = 0.36787944  # np.exp(-1)
    assert np.isclose(coherence_index(r, rho_info), expected), "Failed on typical values"
    
    # Test with array input
    r_arr = np.array([0, 10, 20])
    expected_arr = np.array([1.0, 0.36787944, 0.13533528])  # [np.exp(0), np.exp(-1), np.exp(-2)]
    np.testing.assert_allclose(coherence_index(r_arr, 1.0), expected_arr, err_msg="Failed on array input")

Comment on lines +250 to +252
" # Test error condition (e.g. division by zero if rho_info is 0)\n",
" # Although the function doesn't explicitly handle it, it will return inf or raise warning. \n",
" # We focus on the functional math values.\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The comment here correctly identifies the division-by-zero edge case when rho_info is 0. While you've noted you're focusing on functional values, a comprehensive test suite should cover such predictable edge cases to prevent future regressions and clarify the function's behavior under these conditions.

I suggest replacing these comments with actual test assertions for rho_info = 0. For r > 0, the function should return 0.0 (and raise a RuntimeWarning). For r = 0, it should return NaN.

    # Test error condition (e.g. division by zero if rho_info is 0)
    # This should produce a RuntimeWarning and result in 0.0 for r > 0.
    assert np.isclose(coherence_index(10, 0), 0.0), "Failed on rho_info=0"

    # Test with r=0 and rho_info=0, which should result in NaN.
    assert np.isnan(coherence_index(0, 0)), "Failed on r=0, rho_info=0"

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