-
Notifications
You must be signed in to change notification settings - Fork 0
π§ͺ Add unit tests for coherence_index calculation #11
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| venv |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -222,6 +222,39 @@ | |
| "metadata": {} | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# --- UNIT TESTS ---\n", | ||
| "import numpy as np\n", | ||
| "\n", | ||
| "def test_coherence_index():\n", | ||
| " # Test with r=0, should be 1.0\n", | ||
| " assert np.isclose(coherence_index(0, 1.5), 1.0), \"Failed on r=0\"\n", | ||
| " \n", | ||
| " # 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", | ||
| " \n", | ||
| " # 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", | ||
|
Comment on lines
+250
to
+252
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment here correctly identifies the division-by-zero edge case when I suggest replacing these comments with actual test assertions for |
||
| " print(\"All tests for coherence_index passed!\")\n", | ||
| "\n", | ||
| "if __name__ == \"__main__\":\n", | ||
| " test_coherence_index()\n" | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
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.
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_indexfunction. This makes the tests brittle; if the implementation ofcoherence_indexchanges (e.g., the constant10is 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.