Skip to content

[Animal] Fix DIM=1 Skipped - #3206

Open
allisterakun wants to merge 7 commits into
devfrom
skip_dim_1
Open

[Animal] Fix DIM=1 Skipped#3206
allisterakun wants to merge 7 commits into
devfrom
skip_dim_1

Conversation

@allisterakun

@allisterakun allisterakun commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Fixes the off-by-one error where cow milking history skipped DIM = 1 on calving day, causing every lactation's first record to start at DIM = 2 instead of DIM = 1.

Context

Issue(s) closed by this pull request: closes #3021.

What

  • Added _is_calving_today() method to Animal — returns True when the cow is dry (days_in_milk == 0), pregnant, and days_in_pregnancy == gestation_length, indicating that the reproduction update later in the routine will start a new lactation.
  • Added just_calved: bool = False field to MilkProductionInputs dataclass, with documentation explaining it bridges the ordering gap between milking and reproduction updates.
  • Updated Animal.daily_milking_update() to pass just_calved=self._is_calving_today() in the MilkProductionInputs constructor.
  • Added record_first_day_in_milk(days_born, time) method to MilkProduction — computes milk production at DIM = 1 using Wood's curve, generates the daily milk variance, calculates nutrient contents (crude protein, true protein, fat, lactose), and writes a DIM = 1 record to the milking history.
  • Updated perform_daily_milking_update() in MilkProduction — when is_milking is False but just_calved is True, calls record_first_day_in_milk() and returns early instead of writing a dry-day record.
  • Updated perform_daily_milking_update() and perform_daily_milking_update_without_history() — changed calculate_daily_milk_production to use milk_production_outputs.days_in_milk (the already-incremented value) instead of milk_production_inputs.days_in_milk, fixing the one-day rightward shift between the Wood's curve index and the DIM label.
  • Updated Animal.transition_heiferIII_to_cow() to call self.milk_production.record_first_day_in_milk(self.days_born, time) after setting Wood parameters, so that heifers transitioning to cows also get a DIM = 1 record on the calving day.
  • Added parametrized test_is_calving_today covering: dry cow at end of gestation (True), dry cow not yet at gestation length (False), dry non-pregnant (False), and still-lactating at end of gestation (False).
  • Added test_daily_milking_update_passes_just_calved_flag verifying the flag is forwarded to the milking update.
  • Added test_perform_daily_milking_update_records_dim_1_when_just_calved verifying that record_first_day_in_milk is called when just_calved=True and is_milking=False.
  • Added test_record_first_day_in_milk verifying production is computed at DIM = 1, nutrient contents are refreshed, and a single DIM = 1 history record is appended.
  • Updated test_transition_heiferIII_to_cow to mock and assert record_first_day_in_milk is called.

Why

The root cause was an ordering dependency in Animal.daily_routines: the milking update ran before the reproduction update. On the calving day, the cow entered with days_in_milk = 0 (still dry). The milking update saw is_milking = False and wrote a dry-day record (DIM = 0). Then the reproduction update detected the birth and set days_in_milk = 1. The next simulation day, milking ran with input DIM = 1, incremented to 2, and wrote the first real production record stamped DIM = 2. This meant DIM = 1 never appeared in the milking history, the Wood's curve was evaluated at the wrong DIM values throughout the lactation, and only 304 records were written instead of 305.

How

Rather than reordering the daily routines (which could have other ordering dependencies), the fix uses a look-ahead flag. The new _is_calving_today() method checks whether the reproduction update will start a new lactation later in the routine. This just_calved flag is passed into MilkProductionInputs. In perform_daily_milking_update(), when the cow is not yet milking but just_calved is True, the method calls record_first_day_in_milk() to compute and record DIM = 1 production, then returns early — avoiding the dry-day record. Additionally, the Wood's curve input was corrected from the pre-increment milk_production_inputs.days_in_milk to the post-increment milk_production_outputs.days_in_milk, aligning the curve evaluation with the DIM label. For heiferIII-to-cow transitions, record_first_day_in_milk is called directly after setting Wood parameters.

Test plan

  • Added parametrized test_is_calving_today covering all four branches (dry at gestation end, dry not at gestation end, dry non-pregnant, lactating at gestation end).
  • Added test_daily_milking_update_passes_just_calved_flag verifying the flag reaches the milking inputs.
  • Added test_perform_daily_milking_update_records_dim_1_when_just_calved verifying record_first_day_in_milk is called and output DIM stays 0.
  • Added test_record_first_day_in_milk verifying production at DIM = 1, nutrient content refresh, and a single DIM = 1 history record.
  • Updated test_transition_heiferIII_to_cow to assert record_first_day_in_milk is called with correct arguments.
  • 99% coverage maintained. 3 more mypy errors on branch vs. dev (1137 vs. 1134) — to be resolved.

Input Changes

  • N/A

Output Changes

  • N/A

Filter

@allisterakun allisterakun linked an issue Aug 13, 2026 that may be closed by this pull request
@github-actions

Copy link
Copy Markdown
Contributor

Current Coverage: 99%

Mypy errors on skip_dim_1 branch: 1137
Mypy errors on dev branch: 1134
3 more errors on skip_dim_1 branch

@github-actions

Copy link
Copy Markdown
Contributor

🚨 Please update the changelog. This PR cannot be merged until changelog_WIP.md is updated.

@github-actions

Copy link
Copy Markdown
Contributor

Current Coverage: 99%

Mypy errors on skip_dim_1 branch: 1137
Mypy errors on dev branch: 1134
3 more errors on skip_dim_1 branch

@github-actions

Copy link
Copy Markdown
Contributor

🚨 Please update the changelog. This PR cannot be merged until changelog_WIP.md is updated.

@allisterakun
allisterakun marked this pull request as ready for review August 18, 2026 15:17
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.

[Animal] Fix Cow milking history skips DIM = 1

1 participant