Skip to content

feat: add predict-carbon module with auto selection and genus fallback#19

Merged
Cidree merged 3 commits into
mainfrom
avv-new-predict-carbon
Jun 20, 2026
Merged

feat: add predict-carbon module with auto selection and genus fallback#19
Cidree merged 3 commits into
mainfrom
avv-new-predict-carbon

Conversation

@aitorvv

@aitorvv aitorvv commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

Description

This PR implements the carbon prediction module (PR 4 in the development roadmap). It adds functions to estimate carbon content from predicted biomass using species- and component-specific percentages from the internal carbon_models database.

Key Changes

  • New functions:
    • silv_predict_carbon(): Base function that calculates carbon (biomass * carbon_percentage / 100) for a specified model.
    • silv_predict_carbon_auto(): Vectorized function with automatic model selection and fallback logic.
  • Fallback logic:
    • Automatically falls back to genus-level (e.g. "Pinus spp.") when the exact species matches are not found, informing the user of the shift.
    • If no match or fallback is found, returns NA and warns the user, suggesting they try manually using general groups like "Otras coníferas", "Otras frondosas", or "Otras laurisilvas".
  • Integrations:
    • Added new file R/predict-carbon.R to the package.
    • Registered the functions in DESCRIPTION (Collate field) and _pkgdown.yml.
    • Exported functions and rebuilt documentation using devtools::document().
  • Tests:
    • Added a new unit test suite in tests/testthat/test-predict-carbon.R covering base, auto-priority, fallback logic, and warnings.

Verification

devtools::load_all()

# 1. Coincidencia exacta
res_exact <- silv_predict_carbon(
  biomass = 100, 
  species = "Pinus pinaster", 
  component = "stem", 
  model = "montero-2005"
)
print(res_exact) # Esperado: 51.1 kg

# 2. Uso automático con fallback a "Eucalyptus spp."
res_auto <- silv_predict_carbon_auto(
  biomass = 100,
  species = "Eucalyptus inventatus",
  component = "tree",
  priority = "montero-2005"
)
print(res_auto)

# 3. Especie totalmente desconocida (Sugiere grupos genéricos)
res_fail <- silv_predict_carbon_auto(
  biomass = 100,
  species = "Roble magico",
  component = "stem"
)
print(res_fail)

@Cidree Cidree merged commit b73ca1c into main Jun 20, 2026
1 check passed
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.

2 participants