Skip to content

[Animal] Fix Cow milking history skips DIM = 1 #3021

Description

@matthew7838

Cow milking history skips DIM = 1; first record of every lactation is DIM = 2

Expected Behavior

For each new lactation, a cow's milk production history should include a record at days_in_milk = 1 representing the first day of milking after calving. The recorded DIM should align with the standard dairy convention where DIM = 1 is the first milking day.

Current Behavior

The first milking record of every lactation is written at days_in_milk = 2. DIM = 1 never appears in milk_production_history. The recorded daily production values are computed using Wood's curve at days 1..304 but stamped in the history as days 2..305, i.e. a one-day rightward shift between the curve index and the sim's DIM label. Across a full lactation, 304 daily milking records get written rather than 305.

Possible Solution

The root cause is the order of operations on the calving day inside Animal.daily_routines (RUFAS/biophysical/animal/animal.py:1641):

The cow enters the day with days_in_milk = 0 (she was dry the day before).
daily_milking_update runs first. is_milking returns self.days_in_milk > 0, so she's still flagged dry. The milking routine writes a days_in_milk = 0 record to history.
daily_reproduction_update runs next, detects the birth, and _determine_days_in_milk updates self.days_in_milk = 1.
The next sim day, milking runs with input DIM = 1, increments to 2, and writes the first real production record stamped DIM = 2.
So DIM = 1 represents the calving day with no milking event recorded for it. Two possible fixes:

Reorder the day: run reproduction (which detects calving and sets DIM = 1) before milking on the same day, so the milking step sees the cow as lactating and records DIM = 1 with that day's production.
Special-case the calving day in milking: when reproduction is about to set DIM = 1, have the milking step skip the dry-day record and instead emit a DIM = 1 record (or coordinate via a "just calved" flag).
The first option is cleaner if there are no other ordering dependencies between the two updates.

Steps to Reproduce

  1. Run any simulation that has a cow going through a full dry → calving → lactation cycle.
  2. Inspect cow.milk_production.milk_production_history for that cow.
  3. Find the dry-off marker (days_in_milk == 0) before the new lactation begins.
  4. Observe that the records immediately following the dry-off block jump from DIM = 0 (calving day) straight to DIM = 2, with no DIM = 1 record.

Context (Environment)

Detailed Description

Possible Implementation

Target Deadline

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions