[Econ] Fix biophysical-to-economics bedding mapping and per-pen cost processing #3117
Open
matthew7838 wants to merge 12 commits into
Open
[Econ] Fix biophysical-to-economics bedding mapping and per-pen cost processing #3117matthew7838 wants to merge 12 commits into
matthew7838 wants to merge 12 commits into
Conversation
matthew7838
marked this pull request as ready for review
July 10, 2026 08:39
Collaborator
Collaborator
Author
bradenlimb
approved these changes
Jul 21, 2026
bradenlimb
left a comment
Collaborator
There was a problem hiding this comment.
All looks great to me - thanks for fixing this!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Context
Issue(s) closed by this pull request: closes #3088
What
Reworks how the economics module computes the bedding cost line item. The "Bedding requirements" mapping entry is now routed to a dedicated per-pen processor (EconomicPreprocessor._preprocess_bedding) instead of the generic quantity×price engine, which could not represent bedding correctly. Also extracts two shared helpers (_package_line_item, _warn_if_pricing_missing) so the generic path and the new path build their output through one definition.
Why
On the example freestall farm the old code produced ~$42.50/yr of bedding cost when the correct figure is ~$18,700/yr (verified end-to-end; see Test plan). Three underlying bugs, all in the shared engine:
Price never matched. A pen's bedding_name is a user config name (e.g. calf_straw), but it was compared directly against the price-file keys (e.g. straw), so no price file ever matched and every pen fell back to a $1/head placeholder.
Pens were lumped. All pens' daily animal counts were averaged into one number and multiplied by one averaged price, never pairing a pen's count with that pen's own bedding price.
Annual units ignored. Prices are dollar_per_head per year while counts are per day, and a multi-year run needs each year priced separately.
How
Test plan
Input Changes
input/data/EEE/economics_map.json — updated the "Bedding requirements" block to mirror mapping.py
Output Changes
Filter
{ "direction": "landscape", "multiple": [ { "name": "Bedding total cost ($)", "filters": ["econ_bedding_total_cost$"] }, { "name": "Billed lactating head-years", "filters": ["econ_bedding_billed_head_years$"] }, { "name": "Average bedding price ($/head/yr)", "filters": ["econ_bedding_avg_price_per_head_year$"] }, { "name": "Average lactating head per day", "filters": ["number_of_animals_in_pen_.*LAC_COW$"], "vertical_aggregation": "average" } ] }