[EEE] Clean up FGF emissions calculation functions - #3237
Conversation
ba45527 to
d7dc373
Compare
d7dc373 to
f251c56
Compare
|
Current Coverage: 99% Mypy errors on clean-fgf branch: 1133 |
1 similar comment
|
Current Coverage: 99% Mypy errors on clean-fgf branch: 1133 |
|
Current Coverage: 99% Mypy errors on clean-fgf branch: 1133 |
|
Current Coverage: 99% Mypy errors on clean-fgf branch: 1133 |
|
I think it would be worth coordinating this with the bug-fix #3202 that got merged into |
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 |
|
Current Coverage: 99% Mypy errors on clean-fgf branch: 1133 |
|
@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. |
|
Current Coverage: 99% Mypy errors on clean-fgf branch: 1164 |
ew3361zh
left a comment
There was a problem hiding this comment.
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).
| 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( |
There was a problem hiding this comment.
| 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( |
| "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() |
There was a problem hiding this comment.
| 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 |
| 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(): |
There was a problem hiding this comment.
| 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(): |
| Collects a field's daily emission and resource values for each tracked | ||
| emission and resource variable. |
There was a problem hiding this comment.
| 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. |
| 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 |
There was a problem hiding this comment.
| 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 |
| "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() |
There was a problem hiding this comment.
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.
|
Current Coverage: 99% Mypy errors on clean-fgf branch: 1164 |
|
Current Coverage: 99% Mypy errors on clean-fgf branch: 1164 |
allisterakun
left a comment
There was a problem hiding this comment.
Clean behavior-preserving refactor! LGTM!
Refactors the repetitive farmgrown feed emissions code in
EmissionsEstimator.Context
Issue(s) closed by this pull request: closes #2651
What
_calculate_daily_farmgrown_feed_emissions_and_resources()into a single loop over a new_get_daily_emission_and_resource_values_for_field()helper._report_daily_farmgrown_feed_fed_emissions_and_resources()into a loop over the newFARMGROWN_FEED_FED_OUTPUT_NAME_PREFIXESconstant.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
tests/test_EEEsuite passes unchanged, including numeric fixture comparisons andadd_variable_bulkcall-count assertions.Input Changes
Output Changes