Skip to content

Animals - Trophic Expansion#875

Merged
TaranRallings merged 26 commits intodevelopfrom
754-animals---trophic-expansion
Jun 12, 2025
Merged

Animals - Trophic Expansion#875
TaranRallings merged 26 commits intodevelopfrom
754-animals---trophic-expansion

Conversation

@TaranRallings
Copy link
Copy Markdown
Collaborator

Description

This is the trophic expansion for the Animal Model. It's main job is to expand the types of diets that animals can have and provide the machinery for linking those diets to the right resources.

Core Changes

  • added new DietTypes in AnimalTraits
  • made get_eaten methods for carcass and waste pools
  • made carcass, detritus, and excrement accessible in the foraging loop
  • added delta_mass_ and calculate_consumed_mass_ foraging methods for carcasses, detritus, and excrement
  • expanded prey_group_selection scaling function

Supplemental

  • refactored LitterPool to align it with other resources - one object per grid cell
  • added create_new_cohort method to AnimalModel as I was repeating the same functionality in three places

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

…, a system for parsing input strings, and a broad_diet trait for linking to trophiclly specified constants.
…bring it inline with other animal facing resource pools.
…iring to connect waste, carcasses, and litter into the foraging loop.
@TaranRallings TaranRallings linked an issue May 30, 2025 that may be closed by this pull request
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 30, 2025

Codecov Report

❌ Patch coverage is 76.84564% with 69 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.97%. Comparing base (3604188) to head (63f7873).
⚠️ Report is 1973 commits behind head on develop.

Files with missing lines Patch % Lines
virtual_ecosystem/models/animal/animal_cohorts.py 53.60% 45 Missing ⚠️
virtual_ecosystem/models/animal/decay.py 68.25% 20 Missing ⚠️
virtual_ecosystem/models/animal/animal_model.py 96.36% 2 Missing ⚠️
virtual_ecosystem/models/animal/protocols.py 88.88% 1 Missing ⚠️
...rtual_ecosystem/models/animal/scaling_functions.py 95.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #875      +/-   ##
===========================================
- Coverage    94.80%   93.97%   -0.83%     
===========================================
  Files           75       75              
  Lines         5599     5813     +214     
===========================================
+ Hits          5308     5463     +155     
- Misses         291      350      +59     

☔ 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.

Copy link
Copy Markdown
Collaborator

@jacobcook1995 jacobcook1995 left a comment

Choose a reason for hiding this comment

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

The changes to the animal -> soil/litter links work for me. Just had one comment about litter wet mass

@dalonsoa
Copy link
Copy Markdown
Collaborator

dalonsoa commented Jun 4, 2025

This is a pretty long PR, and I'm at a conference the whole of this week, so please bear with me and I will review it as soon as possible next week.

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.

The code itself looks really good to me, and I only have a couple of questions and a suggestion to reduce the redundancy of the code.


class DietType(Enum):
"""Enumeration for diet types."""
class DietType(Flag):
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.

Neat :)

Comment on lines +143 to +147
self.decomposed_cnp.update(
carbon=missed_cnp["carbon"],
nitrogen=missed_cnp["nitrogen"],
phosphorus=missed_cnp["phosphorus"],
)
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.

Just to confirm this is correct, it replaces the existing decomposed_snp, not added to it, right?

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.

The actual CNP values are stored in a CNP object which has that update method. The update method does add the value to the totals rather than replacing them.

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.

The intention is that decomposed_cnp is a pool of trophically inaccessible mass to which a portion of food that is attempted to eat falls. The pool is updated rather than replaced because this is a flow into the pool and jacob's soil model handles the flow out.

Comment on lines +287 to +290
self.decomposed_cnp.update(
carbon=missed_wet * frac_C,
nitrogen=missed_wet * frac_N,
phosphorus=missed_wet * frac_P,
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.

And same here.

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.

This works the same way as above.

"""
self.decomposed_cnp = CNP(carbon=0.0, nitrogen=0.0, phosphorus=0.0)

def get_eaten(
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.

This function looks almost identical - haven't check exactly - to the other get_eaten above, so I would suggest to remove duplication. One way of doing it would be using mixins. It would look something like:

class GetEatenMixin:
    def get_eaten(...):
        # The implementation of the method, which looks identical.

@dataclass
class CarcassPool(GetEatenMixin):
    ...

@dataclass
class ExcrementPool(GetEatenMixin):
    ...

I'm not entirely sure how mixins play with dataclasses, and you might need to get creative with mypy and use a Protocol for the mixin as described in mypy docs, but it should increase the re-usability of the code.

Copy link
Copy Markdown
Collaborator

@davidorme davidorme Jun 10, 2025

Choose a reason for hiding this comment

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

If I understand your suggestion here, I think mixins and dataclasses work fine.

I haven't called them "mixins" but I believe that the PandasExporter and CohortMethods classes in pyrealm here are mixins:

https://github.com/ImperialCollegeLondon/pyrealm/blob/develop/pyrealm/demography/core.py

And I use them (multiply!) to share functionality among classes e.g. here

https://github.com/ImperialCollegeLondon/pyrealm/blob/a8a133de9bc1bc69415b2d3ebae950a5120bad37/pyrealm/demography/flora.py#L475

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.

Yep, they look the same thing.

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.

Although it seems like I don't need to declare them as an ABC? Will have to look!

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 idea. I hadn't seen those mixins before. I have replaced the redundant method with a mixin and the required protocol.

@TaranRallings TaranRallings merged commit 25e34d7 into develop Jun 12, 2025
16 checks passed
@TaranRallings TaranRallings deleted the 754-animals---trophic-expansion branch June 12, 2025 14:35
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.

Animals - Trophic Expansion

6 participants