Skip to content

Fix: Add init=0 to prevent empty collection crash during threshold tuning. - #432

Open
vatsalyar wants to merge 4 commits into
rohitg00:mainfrom
vatsalyar:main
Open

Fix: Add init=0 to prevent empty collection crash during threshold tuning.#432
vatsalyar wants to merge 4 commits into
rohitg00:mainfrom
vatsalyar:main

Conversation

@vatsalyar

@vatsalyar vatsalyar commented Aug 24, 2026

Copy link
Copy Markdown

What this PR does

Fixes #434

Fixes an ArgumentError in main.jl by adding ; init=0 to the sum() generators in the build_metrics function to handle edge cases during threshold tuning

Kind of change

  • New lesson
  • Fix to an existing lesson
  • Translation
  • New output (prompt, skill, agent, MCP server)
  • Docs / website / tooling

Checklist

  • Code runs without errors with the listed dependencies
  • No comments in code files (docs explain, code is self-explanatory)
  • Built from scratch first, then shown with a framework (for new lessons)
  • Lesson folder matches LESSON_TEMPLATE.md structure
  • ROADMAP.md row for the lesson is a markdown link ([Name](phases/...)), not bare text
  • One lesson per commit (atomic per-lesson rule)
  • Tested locally / code output matches what docs/en.md claims

Phase / lesson

Phase 2 · 03-logistic-regression

Notes for reviewer

When running demo_threshold_tuning, extreme thresholds (like 0.7) can result in the model predicting exactly zero positive cases. When y_pred contains only 0s, the generator comprehensions inside build_metrics. (e.g., sum(1 for i in 1:length(y_true) if y_true[i] == 1 && y_pred[i] == 1)) becomes empty. This causes Julia to throw an ArgumentError: reducing over an empty collection is not allowed. Adding the ; init=0 keyword argument resolves this by safely returning 0 and allowing the script to complete successfully.)

…ection

Fix: Add init=0 to prevent empty collection crash during threshold tuning
@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: e4bf7daa-e232-45d8-9845-a179082b4d58

📥 Commits

Reviewing files that changed from the base of the PR and between f97dc6b and 64c7d1b.

📒 Files selected for processing (1)
  • phases/02-ml-fundamentals/03-logistic-regression/code/main.jl
🚧 Files skipped from review as they are similar to previous changes (1)
  • phases/02-ml-fundamentals/03-logistic-regression/code/main.jl

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

build_metrics now uses indexed predicate counts for all four confusion-matrix metrics. Empty matches return zero without raising an ArgumentError.

Changes

Logistic regression metrics

Layer / File(s) Summary
Count confusion-matrix matches
phases/02-ml-fundamentals/03-logistic-regression/code/main.jl
The true-positive, true-negative, false-positive, and false-negative calculations now use count over eachindex(y_true). Empty matches produce zero.

Estimated code review effort: 1 (Trivial) | ~3 minutes

Merge Risk: ⚪ Minimal · up to 64c7d

This localized fix prevents threshold-tuning failures when no positive cases are predicted. No actionable merge-blocking risk remains beyond normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the fix for empty collections during threshold tuning. It accurately describes the intended behavior, although the implementation uses count over indexed predicates instea…
Description check ✅ Passed The description directly explains the build_metrics failure, the empty-generator edge case, and the intended fix for threshold tuning.
Linked Issues check ✅ Passed The changes satisfy issue #434 by updating build_metrics so confusion-matrix counts return zero when no predictions match. Threshold tuning can therefore complete without the empty-collection Argument…
Out of Scope Changes check ✅ Passed The changes are limited to build_metrics in the logistic-regression lesson and directly address the linked issue. No unrelated changes are identified.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
Full details: Title check

Explanation

The title clearly identifies the fix for empty collections during threshold tuning. It accurately describes the intended behavior, although the implementation uses count over indexed predicates instead of adding init=0 directly.

Full details: Linked Issues check

Explanation

The changes satisfy issue #434 by updating build_metrics so confusion-matrix counts return zero when no predictions match. Threshold tuning can therefore complete without the empty-collection ArgumentError.

Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0 files. (1 skipped: 1 unsupported.)

✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Refactored the build_metrics function to use count() for better performance and readability.
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.

[bug] ArgumentError in build_metrics during threshold tuning (03-logistic-regression)

1 participant