Skip to content

Commit

Permalink
Merge branch 'develop' into remove-redundant-basemodelsetup
Browse files Browse the repository at this point in the history
  • Loading branch information
sallymatson authored Jan 14, 2025
2 parents ec0eb13 + 6443db7 commit 2ca3f42
Showing 17 changed files with 121 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ repos:
- id: check-merge-conflict
- id: debug-statements
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.6
rev: v0.9.1
hooks:
- id: ruff # Run the linter.
args: [--fix, --exit-non-zero-on-fix]
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ def generate_all_variable_markdown(
# Add the variables formatted as list table rows
for v in variables.KNOWN_VARIABLES.values():
table_rows.append(
"* - " + " - ".join([f"{getattr(v,fld)}\n" for fld in fields_to_display])
"* - " + " - ".join([f"{getattr(v, fld)}\n" for fld in fields_to_display])
)

# Wrap the variable rows in the rest of the list table syntax
31 changes: 29 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -70,6 +70,7 @@ mdformat_frontmatter = ">=0.4.1,<2.1.0"
mdformat_tables = ">=0.4.1,<1.1.0"
mypy = "^1.5.1"
pre-commit = ">=3,<5"
ruff = "^0.9.1"

[tool.poetry.group.docs.dependencies]
autodocsumm = "^0.2.8"
4 changes: 2 additions & 2 deletions tests/core/test_data.py
Original file line number Diff line number Diff line change
@@ -376,7 +376,7 @@ def fixture_load_data_grids(request):
["__any__"],
"cellid_coords_too_few.nc",
pytest.raises(ValueError),
"The data cell ids do not provide a one-to-one map onto grid " "cell ids.",
"The data cell ids do not provide a one-to-one map onto grid cell ids.",
(
(INFO, "Loading variable 'temp' from file:"),
(INFO, "Adding data array for 'temp'"),
@@ -393,7 +393,7 @@ def fixture_load_data_grids(request):
["__any__"],
"cellid_coords_bad_cellid.nc",
pytest.raises(ValueError),
"The data cell ids do not provide a one-to-one map onto grid " "cell ids.",
"The data cell ids do not provide a one-to-one map onto grid cell ids.",
(
(INFO, "Loading variable 'temp' from file:"),
(INFO, "Adding data array for 'temp'"),
3 changes: 1 addition & 2 deletions tests/core/test_registry.py
Original file line number Diff line number Diff line change
@@ -19,8 +19,7 @@
(INFO, "Schema registered for virtual_ecosystem.core:"),
(
INFO,
"Constants class registered for "
"virtual_ecosystem.core: CoreConsts",
"Constants class registered for virtual_ecosystem.core: CoreConsts",
),
),
id="core_import_good",
42 changes: 20 additions & 22 deletions tests/models/animals/test_animal_cohorts.py
Original file line number Diff line number Diff line change
@@ -797,9 +797,9 @@ def test_calculate_alpha(
result = cohort_instance.calculate_alpha()

# Assert that the result matches the expected outcome for the given scenario
assert (
result == expected_alpha
), f"Failed scenario: alpha_0_herb={alpha_0_herb}, mass_current={mass_current}"
assert result == expected_alpha, (
f"Failed scenario: alpha_0_herb={alpha_0_herb}, mass_current={mass_current}"
)

@pytest.mark.parametrize(
"alpha, mass_current, phi_herb_t, expected_biomass",
@@ -983,9 +983,9 @@ def test_calculate_theta_opt_i(self, mocker, herbivore_cohort_instance):
result = herbivore_cohort_instance.calculate_theta_opt_i()

# Assert the result matches the mocked return value
assert (
result == 0.5
), "The result does not match the expected return value from sf.theta_opt_i"
assert result == 0.5, (
"The result does not match the expected return value from sf.theta_opt_i"
)

# Assert sf.theta_opt_i was called with the correct parameters
theta_opt_i_mock.assert_called_once_with(
@@ -1125,10 +1125,7 @@ def test_F_i_j_individual(
return_value=0.8,
)
mock_theta_i_j = mocker.patch(
(
"virtual_ecosystem.models.animal.animal_cohorts."
"AnimalCohort.theta_i_j"
),
("virtual_ecosystem.models.animal.animal_cohorts.AnimalCohort.theta_i_j"),
return_value=0.7,
)
mock_potential_prey = mocker.patch(
@@ -1162,9 +1159,9 @@ def test_F_i_j_individual(
N_i = predator_cohort_instance.individuals
N_target = target_animal.individuals
expected_rate = N_i * (10 / (1 + 2)) * (1 / N_target)
assert rate == pytest.approx(
expected_rate
), "F_i_j_individual did not return the expected predation rate."
assert rate == pytest.approx(expected_rate), (
"F_i_j_individual did not return the expected predation rate."
)

def test_theta_i_j(self, predator_cohort_instance, animal_list_instance):
"""Test theta_i_j."""
@@ -1285,9 +1282,9 @@ def test_delta_mass_herbivory(
expected_delta_mass = 10.0 * len(plant_list_instance)

# Assert the calculated delta_mass matches the expected value
assert delta_mass == pytest.approx(
expected_delta_mass
), "Calculated change in mass due to herbivory did not match expected value."
assert delta_mass == pytest.approx(expected_delta_mass), (
"Calculated change in mass due to herbivory did not match expected value."
)

def test_forage_cohort(
self,
@@ -1391,9 +1388,9 @@ def test_migrate_juvenile_probability(
probability_of_dispersal = cohort.migrate_juvenile_probability()

# Assertion to check if the method returns the correct probability
assert (
probability_of_dispersal == expected_probability
), "The probability calculated did not match the expected probability."
assert probability_of_dispersal == expected_probability, (
"The probability calculated did not match the expected probability."
)

@pytest.mark.parametrize(
"is_mature, u_bg, lambda_se, t_to_maturity, t_since_maturity, lambda_max, J_st,"
@@ -1519,6 +1516,7 @@ def test_inflict_non_predation_mortality(
)

# Verify
assert (
cohort.individuals == 100 - expected_dead
), "The calculated number of dead individuals doesn't match the expected value."
assert cohort.individuals == 100 - expected_dead, (
"The calculated number of dead individuals doesn't match the expected "
"value."
)
72 changes: 36 additions & 36 deletions tests/models/animals/test_animal_model.py
Original file line number Diff line number Diff line change
@@ -185,9 +185,9 @@ def test_update_method_sequence(self, mocker, prepared_animal_model_instance):
called_methods.append(method_name)

# Ensure the methods were called in the expected order
assert (
called_methods == method_names
), f"Methods called in wrong order: {called_methods}"
assert called_methods == method_names, (
f"Methods called in wrong order: {called_methods}"
)

def test_update_method_time_index_argument(
self,
@@ -209,32 +209,32 @@ def test_setup_initializes_total_animal_respiration(
from xarray import DataArray

# Check if 'total_animal_respiration' is in the data object
assert (
"total_animal_respiration" in prepared_animal_model_instance.data
), "'total_animal_respiration' should be initialized in the data object."
assert "total_animal_respiration" in prepared_animal_model_instance.data, (
"'total_animal_respiration' should be initialized in the data object."
)

# Retrieve the total_animal_respiration DataArray from the model's data object
total_animal_respiration = prepared_animal_model_instance.data[
"total_animal_respiration"
]

# Check that total_animal_respiration is an instance of xarray.DataArray
assert isinstance(
total_animal_respiration, DataArray
), "'total_animal_respiration' should be an instance of xarray.DataArray."
assert isinstance(total_animal_respiration, DataArray), (
"'total_animal_respiration' should be an instance of xarray.DataArray."
)

# Check the initial values of total_animal_respiration are all zeros
assert np.all(
total_animal_respiration.values == 0
), "Initial values of 'total_animal_respiration' should be all zeros."
assert np.all(total_animal_respiration.values == 0), (
"Initial values of 'total_animal_respiration' should be all zeros."
)

# Optionally, you can also check the dimensions and coordinates
# This is useful if your setup method is supposed to initialize the data
# variable with specific dimensions or coordinates based on your model's
# structure
assert (
"cell_id" in total_animal_respiration.dims
), "'cell_id' should be a dimension of 'total_animal_respiration'."
assert "cell_id" in total_animal_respiration.dims, (
"'cell_id' should be a dimension of 'total_animal_respiration'."
)

def test_population_density_initialization(
self,
@@ -254,9 +254,9 @@ def test_population_density_initialization(

# Check dimensions
expected_dims = ["community_id", "functional_group_id"]
assert all(
dim in population_densities.dims for dim in expected_dims
), f"Expected dimensions {expected_dims} not found in 'population_densities'."
assert all(dim in population_densities.dims for dim in expected_dims), (
f"Expected dimensions {expected_dims} not found in 'population_densities'."
)

# Check coordinates
# you should adjust according to actual community IDs and functional group names
@@ -276,9 +276,9 @@ def test_population_density_initialization(

# Assuming densities have been updated, check if densities are greater than or
# equal to zero
assert np.all(
population_densities.values >= 0
), "Population densities should be greater than or equal to zero."
assert np.all(population_densities.values >= 0), (
"Population densities should be greater than or equal to zero."
)

def test_update_population_densities(self, prepared_animal_model_instance):
"""Test that the update_population_densities method correctly updates."""
@@ -925,15 +925,15 @@ def test_birth(
if is_semelparous:
# For semelparous organisms, the parent dies and the offspring cohort
# replaces it
assert (
len(animal_model_instance.cohorts) == initial_num_cohorts
), f"Expected {initial_num_cohorts} cohorts but"
assert len(animal_model_instance.cohorts) == initial_num_cohorts, (
f"Expected {initial_num_cohorts} cohorts but"
)
" found {len(animal_model_instance.cohorts)}"
else:
# For iteroparous organisms, the parent survives and the offspring is added
assert (
len(animal_model_instance.cohorts) == initial_num_cohorts + 1
), f"Expected {initial_num_cohorts + 1} cohorts but"
assert len(animal_model_instance.cohorts) == initial_num_cohorts + 1, (
f"Expected {initial_num_cohorts + 1} cohorts but"
)
" found {len(animal_model_instance.cohorts)}"

# Get the offspring cohort (assuming it was added correctly)
@@ -1236,9 +1236,9 @@ def test_metamorphose(
)

# Ensure the butterfly functional group is found
assert (
butterfly_functional_group is not None
), "Butterfly functional group not found"
assert butterfly_functional_group is not None, (
"Butterfly functional group not found"
)

# Run the metamorphose method on the caterpillar cohort
animal_model_instance.metamorphose(caterpillar_cohort_instance)
@@ -1260,14 +1260,14 @@ def test_metamorphose(
assert adult_cohort is not None, "Butterfly cohort was not created"

# Assert that the number of individuals in the butterfly cohort is correct
assert (
adult_cohort.individuals == caterpillar_cohort_instance.individuals
), "Butterfly cohort's individuals count does not match the expected value"
assert adult_cohort.individuals == caterpillar_cohort_instance.individuals, (
"Butterfly cohort's individuals count does not match the expected value"
)

# Assert that the caterpillar cohort is marked as dead and removed
assert (
not caterpillar_cohort_instance.is_alive
), "Caterpillar cohort should be marked as dead"
assert not caterpillar_cohort_instance.is_alive, (
"Caterpillar cohort should be marked as dead"
)
assert (
caterpillar_cohort_instance not in animal_model_instance.cohorts.values()
), "Caterpillar cohort should be removed from the model"
6 changes: 3 additions & 3 deletions tests/models/animals/test_decay.py
Original file line number Diff line number Diff line change
@@ -180,9 +180,9 @@ def test_get_eaten(self, litter_pool_instance, herbivore_cohort_instance):
* herbivore_cohort_instance.functional_group.mechanical_efficiency
* herbivore_cohort_instance.functional_group.conversion_efficiency
)
assert actual_mass_gain == pytest.approx(
expected_mass_gain
), "Actual mass gain should match expected value after efficiency adjustments."
assert actual_mass_gain == pytest.approx(expected_mass_gain), (
"Actual mass gain should match expected value after efficiency adjustments."
)

# Check that carbon:nitrogen and carbon:phosphorus ratios remain unchanged
assert initial_c_n_ratio == pytest.approx(
13 changes: 7 additions & 6 deletions tests/models/animals/test_plant_resources.py
Original file line number Diff line number Diff line change
@@ -28,14 +28,15 @@ def test_get_eaten(self, plant_instance, herbivore_cohort_instance):
* herbivore_cohort_instance.functional_group.mechanical_efficiency
* herbivore_cohort_instance.functional_group.conversion_efficiency
)
assert actual_mass_gain == pytest.approx(
expected_mass_gain
), "Actual mass gain should match expected value after efficiency adjustments."
assert actual_mass_gain == pytest.approx(expected_mass_gain), (
"Actual mass gain should match expected value after efficiency adjustments."
)

# Check if the excess mass has been calculated correctly
expected_excess_mass = consumed_mass * (
1 - herbivore_cohort_instance.functional_group.mechanical_efficiency
)
assert excess_mass == pytest.approx(
expected_excess_mass
), "Excess mass should match the expected value based on mechanical efficiency."
assert excess_mass == pytest.approx(expected_excess_mass), (
"Excess mass should match the expected value based on mechanical "
"efficiency."
)
Loading

0 comments on commit 2ca3f42

Please # to comment.