Skip to content

989 foraging methods refactor to reduce resuse#990

Merged
TaranRallings merged 6 commits intodevelopfrom
989-foraging-methods-refactor-to-reduce-resuse
Aug 12, 2025
Merged

989 foraging methods refactor to reduce resuse#990
TaranRallings merged 6 commits intodevelopfrom
989-foraging-methods-refactor-to-reduce-resuse

Conversation

@TaranRallings
Copy link
Copy Markdown
Collaborator

Description

This is a small refactor to prepare the foraging system for all of the new resource pools I am introducing. It collapses the many identical delta_mass and calculate_consumed_mass methods into something more DRY.

Predation is unchanged.

Non-predation consumption now keys off two core methods:

  • default_consumed_resource_mass
  • forage_resource_list

Small, resource-specific, methods that serve to call forage_resource_list are implemented to aid in bug hunting and facilitate later expansions.

Fixes # (issue)

Type of change

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist

  • Make sure you've run the pre-commit checks: $ pre-commit run -a
  • All tests pass: $ poetry run pytest

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works
  • Relevant documentation reviewed and updated

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Aug 7, 2025

Codecov Report

❌ Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 94.82%. Comparing base (3f830d4) to head (02702b3).
⚠️ Report is 6 commits behind head on develop.

Files with missing lines Patch % Lines
virtual_ecosystem/models/animal/animal_cohorts.py 95.23% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #990      +/-   ##
===========================================
+ Coverage    94.31%   94.82%   +0.51%     
===========================================
  Files           79       79              
  Lines         6576     6511      -65     
===========================================
- Hits          6202     6174      -28     
+ Misses         374      337      -37     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TaranRallings TaranRallings marked this pull request as ready for review August 8, 2025 07:26
Copy link
Copy Markdown
Collaborator

@dalonsoa dalonsoa left a comment

Choose a reason for hiding this comment

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

Just a couple of small comments, but otherwise it looks good, so approving.

Comment on lines +952 to +953
consumed = target.mass_current * (1.0 - exp(-F * adjusted_dt))
return max(consumed, 0.0)
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.

If F is positive (I assume adjusted_dt is positive), then consumed can not be negative, so the max(consumed, 0.0) bit is not really necessary:

Suggested change
consumed = target.mass_current * (1.0 - exp(-F * adjusted_dt))
return max(consumed, 0.0)
return target.mass_current * (1.0 - exp(-F * adjusted_dt))

herbivory_waste_pools[plant.cell_id].add_waste(plant_litter_cnp)
if herbivory_waste_pools and litter_cnp:
if resource.cell_id not in herbivory_waste_pools:
raise KeyError(f"Missing waste pool for cell {resource.cell_id}")
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.

Why or when could this happen?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I am probably overdoing it here but in general this isn't me anticipating a specific problem as much as it is laying a groundwork to help the inevitable long road of debugging.

return total_consumed_mass

def calculate_consumed_mass_herbivory(
def default_consumed_resource_mass(
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.

I'm not sure if there's going to be ever a consumed resource mass other than this one, but if there is not and the only point here is to re-use this same code by other methods of the class, the usual approach is to just make it "private" and remove the default. Otherwise, it will be understood that there is an option for not being default:

Suggested change
def default_consumed_resource_mass(
def _consumed_resource_mass(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ah, good to know, thanks!

@TaranRallings TaranRallings merged commit 266dbe6 into develop Aug 12, 2025
13 checks passed
@TaranRallings TaranRallings deleted the 989-foraging-methods-refactor-to-reduce-resuse branch August 12, 2025 14:20
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.

Foraging methods refactor to reduce resuse

3 participants