Skip to content

Commit

Permalink
Making the canopy dataclasses PandasExporter compliant and adding sim…
Browse files Browse the repository at this point in the history
…ple tests
  • Loading branch information
davidorme committed Oct 23, 2024
1 parent cd6af9f commit 3e3dbd8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
18 changes: 18 additions & 0 deletions pyrealm/demography/canopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

from dataclasses import InitVar, dataclass, field
from typing import ClassVar

import numpy as np
from numpy.typing import NDArray
Expand Down Expand Up @@ -209,6 +210,15 @@ class CohortCanopyData(PandasExporter):
cell_area: A float setting the total canopy area available to the cohorts.
"""

array_attrs: ClassVar[tuple[str, ...]] = (
"stem_leaf_area",
"lai",
"f_trans",
"f_abs",
"cohort_fapar",
"stem_fapar",
)

# Init vars
projected_leaf_area: InitVar[NDArray[np.float64]]
"""An array of the stem projected leaf area for each cohort at each of the required
Expand Down Expand Up @@ -304,6 +314,14 @@ class CommunityCanopyData(PandasExporter):
:attr:`CohortCanopyData.f_trans<pyrealm.demography.canopy.CohortCanopyData.f_trans>`.
"""

array_attrs: ClassVar[tuple[str, ...]] = (
"f_trans",
"f_abs",
"transmission_profile",
"extinction_profile",
"fapar",
)

# Init vars
cohort_transmissivity: InitVar[NDArray[np.float64]]
"""An array providing the per cohort light transmissivity at each of the required
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/demography/test_canopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_CohortCanopyData__init__(
assert np.allclose(instance.lai, exp_lai)
assert np.allclose(instance.f_trans, exp_f_trans)

# Unpack and test expectations
# Unpack and test expectations for cohort and stem fapar
exp_f_trans, exp_trans_prof = community_expected
expected_fapar = -np.diff(exp_trans_prof, prepend=1)
assert np.allclose(
Expand All @@ -83,6 +83,16 @@ def test_CohortCanopyData__init__(
instance.stem_fapar, np.tile((expected_fapar / 6)[:, None], 3)
)

# Test the inherited to_pandas method
df = instance.to_pandas()

assert df.shape == (
np.prod(exp_stem_leaf_area.shape),
len(instance.array_attrs),
)

assert set(instance.array_attrs) == set(df.columns)

def test_CommunityCanopyData__init__(
self, cohort_args, cohort_expected, community_expected
):
Expand All @@ -99,6 +109,16 @@ def test_CommunityCanopyData__init__(
assert np.allclose(instance.f_trans, exp_f_trans)
assert np.allclose(instance.transmission_profile, exp_trans_prof)

# Test the inherited to_pandas method
df = instance.to_pandas()

assert df.shape == (
len(exp_f_trans),
len(instance.array_attrs),
)

assert set(instance.array_attrs) == set(df.columns)


def test_Canopy__init__():
"""Test happy path for initialisation.
Expand Down

0 comments on commit 3e3dbd8

Please sign in to comment.