Skip to content

[EEE] Clean up FGF emissions calculation functions - #3237

Merged
matthew7838 merged 23 commits into
devfrom
clean-fgf
Sep 15, 2026
Merged

matthew7838 merged 23 commits into
devfrom
clean-fgf

Conversation

@matthew7838

Copy link
Copy Markdown
Collaborator

Refactors the repetitive farmgrown feed emissions code in EmissionsEstimator.

Context

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

What

  • Extracts the six per-variable accumulation blocks in _calculate_daily_farmgrown_feed_emissions_and_resources() into a single loop over a new _get_daily_emission_and_resource_values_for_field() helper.
  • Collapses the six copy-pasted reporting blocks in _report_daily_farmgrown_feed_fed_emissions_and_resources() into a loop over the new FARMGROWN_FEED_FED_OUTPUT_NAME_PREFIXES constant.
  • Replaces the repeated six-key dict literals with comprehensions over FARMGROWN_FEED_EMISSION_AND_RESOURCE_VARIABLES.

Why

Addresses the duplicate-code review notes from PR #2633.

How

The tracked variables and their output name prefixes are defined once at module level; calculation and reporting iterate over them. The refactor is behavior-preserving.

Test plan

  • Existing tests/test_EEE suite passes unchanged, including numeric fixture comparisons and add_variable_bulk call-count assertions.
  • Adds unit tests for the new helper.

Input Changes

  • N/A

Output Changes

  • N/A

@github-actions

Copy link
Copy Markdown
Contributor

Current Coverage: 99%

Mypy errors on clean-fgf branch: 1133
Mypy errors on dev branch: 1133
No difference in error counts

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

Current Coverage: 99%

Mypy errors on clean-fgf branch: 1133
Mypy errors on dev branch: 1133
No difference in error counts

@matthew7838 matthew7838 changed the title Clean up FGF emissions calculation functions [EEE] Clean up FGF emissions calculation functions Aug 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Current Coverage: 99%

Mypy errors on clean-fgf branch: 1133
Mypy errors on dev branch: 1133
No difference in error counts

@github-actions

Copy link
Copy Markdown
Contributor

Current Coverage: 99%

Mypy errors on clean-fgf branch: 1133
Mypy errors on dev branch: 1133
No difference in error counts

@ew3361zh

Copy link
Copy Markdown
Collaborator

I think it would be worth coordinating this with the bug-fix #3202 that got merged into test but I believe still needs to get addressed on dev.

@KFosterReed

Copy link
Copy Markdown
Contributor

I think it would be worth coordinating this with the bug-fix #3202 that got merged into test but I believe still needs to get addressed on dev.

Great catch Niko - I very much agree! I opened this issue about bringing both of the relevant updates to dev and also tried to start this PR #3230 for the emissions one in particular but coordinating this work with that one will make our lives a lot easier

@github-actions

Copy link
Copy Markdown
Contributor

Current Coverage: 99%

Mypy errors on clean-fgf branch: 1133
Mypy errors on dev branch: 1133
No difference in error counts

@matthew7838

Copy link
Copy Markdown
Collaborator Author

@ew3361zh @KFosterReed I think, since this PR is low complexity, I will wait until after the test changes merge to resolve any conflicts, then re-request a review of this PR.

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Current Coverage: 99%

Mypy errors on clean-fgf branch: 1164
Mypy errors on dev branch: 1164
No difference in error counts

@ew3361zh ew3361zh 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.

Looks good to me! Most fb centers around wanting more specificity in variable naming which I think would be helpful for future devs (and current devs when we need to look at the code again in 6 months).

Comment thread RUFAS/EEE/emissions.py Outdated
farmgrown_feed_inventory_by_feed_id = self._gather_farmgrown_feed_inventory_data(all_simulation_days)

for field_name in harvest_yield_by_field:
daily_values_by_variable = self._get_daily_emission_and_resource_values_for_field(

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.

Suggested change
daily_values_by_variable = self._get_daily_emission_and_resource_values_for_field(
daily_emission_and_resource_values = self._get_daily_emission_and_resource_values_for_field(

Comment thread RUFAS/EEE/emissions.py Outdated
"fertilizer_P": 0.0,
"fertilizer_K": 0.0,
"manure_N": 0.0,
variable: 0.0 for variable in FARMGROWN_FEED_FED_OUTPUT_NAME_PREFIXES.keys()

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.

Suggested change
variable: 0.0 for variable in FARMGROWN_FEED_FED_OUTPUT_NAME_PREFIXES.keys()
emission_or_resource: 0.0 for emission_or_resource in FARMGROWN_FEED_FED_OUTPUT_NAME_PREFIXES

Comment thread RUFAS/EEE/emissions.py Outdated
start=0.0,
)
total_farmgrown_feed_emission_and_resource_by_feed_id[feed_id]["fertilizer_K"] += sum(
for variable, daily_values in daily_values_by_variable.items():

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.

Suggested change
for variable, daily_values in daily_values_by_variable.items():
for emission_or_resource, daily_emission_and_resource_values in daily_emission_and_resource_values.items():

Comment thread RUFAS/EEE/emissions.py Outdated
Comment on lines +718 to +719
Collects a field's daily emission and resource values for each tracked
emission and resource variable.

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.

Suggested change
Collects a field's daily emission and resource values for each tracked
emission and resource variable.
Collects a field's daily emission and resource values.

Comment thread RUFAS/EEE/emissions.py Outdated
dict[str, dict[int, float]]
A nested dictionary structured as ``{variable: {simulation_day: value}}``
for each variable in ``FARMGROWN_FEED_FED_OUTPUT_NAME_PREFIXES``.
Variables whose application type has no data for the field map to empty

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.

Suggested change
Variables whose application type has no data for the field map to empty
Variables whose application type (fertilizer or manure) has no data for the field map to empty

Comment thread RUFAS/EEE/emissions.py Outdated
"fertilizer_P": data_for_feed_id_for_day["fertilizer_P"] * feed_deduction,
"fertilizer_K": data_for_feed_id_for_day["fertilizer_K"] * feed_deduction,
"manure_N": data_for_feed_id_for_day["manure_N"] * feed_deduction,
variable: value * feed_deduction for variable, value in data_for_feed_id_for_day.items()

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.

Same feedback as for the other - I would try to avoid the generic variable and value for this stuff since it is fairly specific and knowing what's what is helpful here.

@github-actions

Copy link
Copy Markdown
Contributor

Current Coverage: 99%

Mypy errors on clean-fgf branch: 1164
Mypy errors on dev branch: 1164
No difference in error counts

@github-actions

Copy link
Copy Markdown
Contributor

Current Coverage: 99%

Mypy errors on clean-fgf branch: 1164
Mypy errors on dev branch: 1164
No difference in error counts

@allisterakun allisterakun 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.

Clean behavior-preserving refactor! LGTM!

@matthew7838
matthew7838 merged commit ca51145 into dev Sep 15, 2026
1 check passed
@matthew7838
matthew7838 deleted the clean-fgf branch September 15, 2026 08:19
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.

[EEE] Clean up FGF Emissions Calculation Functions

4 participants