Skip to content

fix(metrics): compile and run the metrics edge-case tests - #538

Open
EnesKartal wants to merge 5 commits into
smartcorelib:mainfrom
EnesKartal:fix/metrics-edge-case-tests
Open

EnesKartal wants to merge 5 commits into
smartcorelib:mainfrom
EnesKartal:fix/metrics-edge-case-tests

Conversation

@EnesKartal

Copy link
Copy Markdown

Fixes #535

Checklist

  • My branch is up-to-date with main branch.
  • Everything works and tested on latest stable Rust.
  • Coverage and Linting have been applied

Current behaviour

src/metrics/tests_edge_cases.rs is not declared in src/metrics/mod.rs, so its 26 tests are never built. If you declare it, it does not compile:

  • it imports MeanSquaredError, but the type is MeanSquareError;
  • the HCV tests import homogeneity_score, completeness_score and v_measure_score from metrics::cluster_hcv, but these functions are in metrics;
  • these three functions require T: Number + FloatNumber + RealNumber + Ord. FloatNumber is only on f32/f64, which are not Ord, so no type can call them.

New expected behaviour

  • mod.rs declares #[cfg(test)] mod tests_edge_cases;, and the imports are fixed.
  • homogeneity_score, completeness_score and v_measure_score now use T: Number + Ord. This is the bound of ClusterMetrics::hcv_score(), which is the only thing they call. The bounds are less strict than before, so no caller breaks, and the functions now work with integer labels (for example Vec<u32>), as the HCV edge-case tests use them.
  • cargo fmt reformatted the test file (it was never checked before, because it was not part of the crate). I also changed four x >= 0.0 && x <= 1.0 asserts to (0.0..=1.0).contains(&x) so that cargo clippy --all-targets has no warnings in the file.

Test plan

# before (main): the edge-case tests are not part of the build
$ cargo test --lib metrics
test result: ok. 54 passed; 0 failed; ...

# before, with only the `mod tests_edge_cases;` line added
error[E0432]: unresolved import `crate::metrics::mean_squared_error::MeanSquaredError`
error[E0432]: unresolved imports `crate::metrics::cluster_hcv::homogeneity_score`, ...

# after
$ cargo test --lib metrics_edge_cases
test result: ok. 26 passed; 0 failed; ...
$ cargo test --lib metrics
test result: ok. 80 passed; 0 failed; ...

cargo fmt --check and cargo clippy --all-features --all-targets -- -Drust-2018-idioms show nothing for the changed files.

Note: the other tests_edge_cases.rs files (cluster, decomposition, ensemble, linear, naive_bayes, neighbors, svm, tree) are also not declared in their mod.rs. I did not change them in this PR, to keep it focused on #535.

Change logs

Changed

  • homogeneity_score, completeness_score and v_measure_score accept T: Number + Ord labels.
  • The metrics edge-case tests are compiled and run.

src/metrics/tests_edge_cases.rs was not declared in src/metrics/mod.rs,
so its tests were never built. Declare the module and fix the imports:
the regression type is MeanSquareError, and the HCV free functions live
in crate::metrics, not in cluster_hcv.

homogeneity_score, completeness_score and v_measure_score required
T: Number + FloatNumber + RealNumber + Ord. No type meets these bounds,
so nothing could call them. They only call ClusterMetrics::hcv_score,
which needs T: Number + Ord, so use the same bounds.

Closes smartcorelib#535
@EnesKartal
EnesKartal requested a review from Mec-iS as a code owner November 14, 2026 12:10
@Mec-iS

Mec-iS commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this PR. It fixes #535 and stays focused. Declaring the module, fixing the MeanSquareError import and the metrics:: import paths are all correct. Relaxing homogeneity_score, completeness_score and v_measure_score to T: Number + Ord is also right. The old FloatNumber bound could never be satisfied together with Ord, and ClusterMetrics::hcv_score() only needs Number + Ord. The relaxation is source-compatible for callers. CI was still running when I reviewed (only MSRV, audit and deny had finished), so I can't confirm the test jobs yet.

Requested changes / questions

  1. CHANGELOG. The PR description has a "Change logs" section, but the diff changes only src/metrics/mod.rs and src/metrics/tests_edge_cases.rs. Please add the entry to CHANGELOG.md as other PRs do. The bound relaxation is a public API change and should be listed.
  2. Dependency on fix(metrics): return 1.0 for zero-entropy labels in HCVScore #534. cluster_hcv_single_cluster_no_panic asserts completeness == 1.0 for a single cluster. Before fix(metrics): return 1.0 for zero-entropy labels in HCVScore #534 (zero-entropy handling in HCVScore), that value was NaN, and assert_close would fail. The base commit here differs from the one fix(metrics): return 1.0 for zero-entropy labels in HCVScore #534 used, so I assume fix(metrics): return 1.0 for zero-entropy labels in HCVScore #534 has already landed in the base. Please confirm that this is the case, or note the ordering in the PR.
  3. Wrong comment and weak assertion in cluster_hcv_all_same_true_labels. The comment says that a single-class ground truth gives completeness 1. With a single true class, homogeneity is 1 (zero class entropy) and completeness is about 0, because (MI \approx 0) while the cluster entropy is positive (this is what scikit-learn returns). The test only checks that completeness is in [0, 1], so it can't detect the old inf and NaN regressions. Please fix the comment, and assert homogeneity == 1.0, completeness near 0 (< 1e-10) and v_measure near 0.
  4. Weak range assertions. In cluster_hcv_single_cluster_no_panic, the comment says homogeneity is 0, but the test only checks 0..=1. The expected values are known: homogeneity 0, completeness 1, v-measure 0. Assert them with a tolerance. (0.0..=1.0).contains(&NaN) is false, so the range checks do catch NaN, but they would miss a wrong finite value.
  5. WASM. WASM is a first-class target in this project, and the other metrics tests carry the wasm_bindgen_test attribute (cfg_attr(all(target_arch = "wasm32", not(target_os = "wasi")), wasm_bindgen_test::wasm_bindgen_test)). These 26 tests only have #[test], so the wasm32 CI job won't run them. Please add the attribute, or explain why they are excluded.

Minor / nits

  • The new #[cfg(test)] on the module declaration makes the #[cfg(test)] inside tests_edge_cases.rs redundant. Remove one.
  • RealNumber was removed from the three bounds. Please make sure that the import in mod.rs is still used elsewhere, so that the change doesn't leave an unused-import warning. FloatNumber is still used by r2.
  • Reformatting the whole test file is fine, since it was never formatted before. Please mention it in the description so reviewers know to skip those hunks. The description already does.

Follow-up
The other tests_edge_cases.rs files (cluster, decomposition, ensemble, linear, naive_bayes, neighbors, svm, tree) are also not declared. I agree that they belong in separate PRs. Please open an issue to track them, one module per PR, so that compile errors in each can be fixed separately.

I ignored the lint failure, as you asked. After items 1–5 are addressed and the test jobs are green, this looks good to merge from my side.

@codecov

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.45%. Comparing base (9eaae9e) to head (bc4e987).
⚠️ Report is 232 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #538       +/-   ##
===========================================
+ Coverage   43.97%   87.45%   +43.48%     
===========================================
  Files          85      100       +15     
  Lines        7281     9999     +2718     
===========================================
+ Hits         3202     8745     +5543     
+ Misses       4079     1254     -2825     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Mec-iS Mec-iS mentioned this pull request Nov 15, 2026
11 tasks
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.

tests_edge_cases.rs in metrics is never compiled

2 participants