Skip to content

[Econ] Purchased feed cost preprocessing - #3215

Open
matthew7838 wants to merge 1 commit into
refactor_preprocessfrom
purchased-feed-cost-preprocessing
Open

[Econ] Purchased feed cost preprocessing#3215
matthew7838 wants to merge 1 commit into
refactor_preprocessfrom
purchased-feed-cost-preprocessing

Conversation

@matthew7838

Copy link
Copy Markdown
Collaborator

Supersedes #3167, which was merged into refactor_preprocess prematurely. That merge has been reverted (refactor_preprocess force-pushed back to e11346754); GitHub does not allow reopening a merged PR, so this PR replaces it. Same branch, same base, same commit b8d312f51.

Uses the ration interval feed purchases (priced from the feed input file) for the "Purchased feed costs" economics line item, and teaches the preprocessing to handle interval-reported data.

Context

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

What

  • "Purchased feed costs" is now computed from the simulation's ration interval purchase outputs: biophysical_aggregate is kg purchased (ration_interval_.*_amount_purchased), price_aggregate is the $/kg actually paid weighted by purchase amount (total $ ÷ total kg), and line_item_values_by_scenario is the $ total from ration_interval_.*_cost — so biophysical_aggregate × price_aggregate = line_item holds. The 23 commodity reference files are no longer fetched for this item.
  • Interval-reported values are expanded to a daily series (zero-filled on non-formulation days) so their length aligns with daily-reported data.
  • Fixes the BIOPHYSICAL_FALLBACKS keys for these patterns (ration_interval_*_cost could never match the mapping's pattern string).
  • Splits _fetch_prices and the pricing logic in preprocess() into small helpers (CodeFactor complexity).

Why

The generic preprocessing treated the already-in-dollars interval costs as physical quantities and multiplied their sum by an average of 23 $/kg commodity prices — double-counting price. Ration interval outputs are also only reported on formulation days, so the series is shorter than daily data and needed explicit handling.

How

  • New cost_simulation mapping key pairs the quantity pattern with the cost pattern; such items derive per-feed prices (cost ÷ kg, recovering the feed input file's purchased_feed_cost) and sum costs directly for the line item instead of fetching commodity prices.
  • New expand_interval_to_daily mapping key fetches via filter_variables_pool with expand_data/fill_value: 0.0 (keyed by each value's simulation_day); skipped with a warning if OutputManager.time isn't initialized.

Test plan

  • Unit tests cover: weighted price derivation and the aggregate × price = line item identity, interval→daily zero-fill expansion, graceful skip without time, mapping/fallback key wiring, and the real mapping entry with data and with the empty-pool fallback.
  • Freestall example run: 8,350,557.09 kg × 0.105158 $/kg = 878,131.62 (total unchanged from before); per-feed derived prices match the feed input file exactly.
  • Full test suite passes; Black applied; mypy error count unchanged vs base.

Input Changes

  • N/A (no input file or schema changes; the new keys live in the hardcoded ECONOMIC_MAP).

Output Changes

  • No new output variables. Within the economic_preprocessed results for "Purchased feed costs": biophysical_values are daily-length kg purchased (previously $ on formulation days), price_values/price_aggregate are the prices actually paid (previously unused commodity reference averages), and the line item total is unchanged.

Filter

N/A

Adapts the purchased feed cost work to the refactored preprocessing
pipeline: PurchasedFeedCostHandler reports purchased amounts (kg) as the
biophysical quantity, derives per-feed prices from cost/amount outputs,
and uses the purchase-amount-weighted average price so that
biophysical_aggregate * price_aggregate equals the line item total.
Shared fetch/expansion helpers move into PreprocessingContext.
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.

1 participant