Skip to content

[Econ] Fix biophysical-to-economics bedding mapping and per-pen cost processing #3117

Open
matthew7838 wants to merge 12 commits into
economicsfrom
bedding-mapping
Open

[Econ] Fix biophysical-to-economics bedding mapping and per-pen cost processing #3117
matthew7838 wants to merge 12 commits into
economicsfrom
bedding-mapping

Conversation

@matthew7838

@matthew7838 matthew7838 commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

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

  • Mapping (mapping.py): tag the "Bedding requirements" entry with dedicated_processor: "bedding", add a bedding_type_to_file_key table and bedding_configs_path; remove the brittle hardcoded wildcard_value_map.
  • Processor (preprocessing.py): _preprocess_bedding resolves each pen bedding_name → bedding_configs.bedding_type → price file, matches pens by their id field (not list position), and for each calendar year computes (head-days ÷ days-in-year) × that year's price, summed over pens and years.
  • Robustness: pens with bedding_type "none" cost nothing; a missing price year falls back to the nearest available year; a missing FIPS county, an unloadable price file, an unknown config, or an unmapped type each emit a warning instead of silently charging $1/head.
  • De-duplication: both the generic engine and the bedding processor now return via a shared _package_line_item and warn via a shared _warn_if_pricing_missing, so the two paths can't drift apart.

Test plan

  • Run updated unit tests.
  • SME review.

Input Changes

input/data/EEE/economics_map.json — updated the "Bedding requirements" block to mirror mapping.py

Output Changes

  • The bedding line item within econ_economic_line_item_breakdown and econ_preprocessed_economic_inputs now reflects correct per-pen annual cost

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"
        }
    ]
}

@matthew7838 matthew7838 self-assigned this Jun 29, 2026
@matthew7838
matthew7838 marked this pull request as ready for review July 10, 2026 08:39
@matthew7838 matthew7838 changed the title [Econ] Initial implementations for the bedding mapping functions [Econ] [Econ] Fix biophysical-to-economics bedding mapping and per-pen cost processing Jul 10, 2026
@matthew7838 matthew7838 changed the title [Econ] [Econ] Fix biophysical-to-economics bedding mapping and per-pen cost processing [Econ] Fix biophysical-to-economics bedding mapping and per-pen cost processing Jul 10, 2026
@bradenlimb

Copy link
Copy Markdown
Collaborator

Hi Matthew,

I reviewed this, but the values coming out the backend still don't seem correct for me. The average bedding cost is $53.22/head/year and I believe there is 100 head of cattle. Therefore the aggregated cost in the freestall scneario should be closer in the high $40k range. It is outputting a values of $70874 for me which is also different than the ~$18,700/yr you mentioned above since it's running for 8 years.

I think something is going wrong in the preprocess step or at least how it is being reported. The total cost should equal the "biophysical_aggregate" value multiplied by the "price_aggregate" value. But currently the total cost on the "line_item_values_by_scneario" variable is equal to the "biophysical_aggregate" value

Hopefully this makes sense. If not, I'll be on the econ task force call tomorrow if you're available to discuss.

image

@matthew7838

Copy link
Copy Markdown
Collaborator Author

Hi Matthew,

I reviewed this, but the values coming out the backend still don't seem correct for me. The average bedding cost is $53.22/head/year and I believe there is 100 head of cattle. Therefore the aggregated cost in the freestall scneario should be closer in the high $40k range. It is outputting a values of $70874 for me which is also different than the ~$18,700/yr you mentioned above since it's running for 8 years.

I think something is going wrong in the preprocess step or at least how it is being reported. The total cost should equal the "biophysical_aggregate" value multiplied by the "price_aggregate" value. But currently the total cost on the "line_item_values_by_scneario" variable is equal to the "biophysical_aggregate" value

Hopefully this makes sense. If not, I'll be on the econ task force call tomorrow if you're available to discuss.

image

Hi Braden, I have made another update addressing these problems. As for the first part, I've made changes reflecting only the costs of lactating cows in the pen, which averages to around 89 of them. Therefore, I'm getting around 33k as the result. Let me know how things are looking on your side.

@bradenlimb bradenlimb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks great to me - thanks for fixing this!

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.

[Econ] Improve biophysical to economics bedding mapping and processing

2 participants