Skip to content

Fixing behaviour of T Model with zero DBH values #525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

davidorme
Copy link
Collaborator

@davidorme davidorme commented Jun 24, 2025

Description

Tip

OK - this is ready for review now.

Currently, if the DBH ($D$) of a stem is zero, then in the StemAllometry class:

  • the crown fraction ($f_c$) is NaN, so...
  • the sapwood mass (which uses $f_c$) is NaN

And in the StemAllocation class:

  • Consequently, sapwood respiration is NaN and as a result NPP and all the delta variables (diameter, mass, foliage) are NaN.

That leads to all sorts of unexpected behaviour. We could make more sensible predictions when $D=0$ - which is what the first iteration of this PR did - but it basically doesn't make sense to make predictions when $D=0$. That's actually a bit more work because we currently allow zero so this PR:

  • Adds a new _enforce_positive_sizes helper function to pyrealm.demography.tmodel
  • Calls that function from within nearly all of the validation sections of the T model functions. We don't call it on the reproductive mass functions because that is an experimental extension that we want to be able to set to zero.
  • Add tests that zero and negative values are caught correctly.
  • Updates tests to pass

The PR also:

  • Makes it a ValueError to create StemAllometry or Cohorts with negative DBH - this guarantees positive sizes and allows the per function validation to be skipped.
  • Makes Cohorts.dbh_values a property raises ValueError when set with negative DBH values.

Fixes #524

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

@davidorme davidorme linked an issue Jun 24, 2025 that may be closed by this pull request
@codecov-commenter
Copy link

codecov-commenter commented Jun 24, 2025

Codecov Report

Attention: Patch coverage is 98.48485% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.11%. Comparing base (e90a2d5) to head (4abea43).
Report is 9 commits behind head on develop.

Files with missing lines Patch % Lines
pyrealm/demography/tmodel.py 98.03% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #525      +/-   ##
===========================================
+ Coverage    96.02%   96.11%   +0.08%     
===========================================
  Files           41       41              
  Lines         3171     3242      +71     
===========================================
+ Hits          3045     3116      +71     
  Misses         126      126              

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

@davidorme davidorme moved this to In Progress in ICCS development board Jun 24, 2025
@davidorme davidorme moved this from In Progress to Review in ICCS development board Jun 24, 2025
@davidorme davidorme marked this pull request as draft June 24, 2025 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Review
Development

Successfully merging this pull request may close these issues.

Behaviour of T Model allometry and allocation with zero DBH values
2 participants