Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ttsim/interface_dag_elements/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class FailIf(MainTargetABC):
paths_are_missing_in_targets_tree_mapper: str = (
"fail_if__paths_are_missing_in_targets_tree_mapper"
)
root_nodes_are_missing: str = "fail_if__root_nodes_are_missing"
tt_root_nodes_are_missing: str = "fail_if__tt_root_nodes_are_missing"
targets_are_not_in_specialized_environment_or_data: str = (
"fail_if__targets_are_not_in_specialized_environment_or_data"
)
Expand Down
9 changes: 8 additions & 1 deletion src/ttsim/interface_dag_elements/fail_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ def input_df_mapper_has_incorrect_format(


@fail_function()
def root_nodes_are_missing(
def tt_root_nodes_are_missing(
specialized_environment__tax_transfer_dag: nx.DiGraph,
specialized_environment__with_partialled_params_and_scalars: SpecEnvWithPartialledParamsAndScalars,
processed_data: QNameData,
Expand All @@ -663,6 +663,13 @@ def root_nodes_are_missing(
ValueError
If root nodes are missing.
"""

if not processed_data:
raise ValueError(
"For computing results, you need to pass data. "
"You can do this by passing a suitable `input_data=InputData.[x]` argument "
"to `main`."
)
# Obtain root nodes
root_nodes = nx.subgraph_view(
specialized_environment__tax_transfer_dag,
Expand Down
49 changes: 45 additions & 4 deletions tests/ttsim/interface_dag_elements/test_failures.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ def test_fail_if_input_data_has_different_lengths(backend):
)


def test_fail_if_root_nodes_are_missing_via_main(minimal_input_data, backend):
def test_fail_if_tt_root_nodes_are_missing_via_main(minimal_input_data, backend):
def b(a):
return a

Expand All @@ -920,7 +920,7 @@ def c(b):
match="The following data columns are missing",
):
main(
main_targets=["results__tree", "fail_if__root_nodes_are_missing"],
main_targets=["results__tree", "fail_if__tt_root_nodes_are_missing"],
input_data={"tree": minimal_input_data},
policy_environment=policy_environment,
date=datetime.date(2024, 1, 1),
Expand All @@ -930,7 +930,7 @@ def c(b):
)


def test_fail_if_root_nodes_are_missing_asks_for_individual_level_columns(
def test_fail_if_tt_root_nodes_are_missing_asks_for_individual_level_columns(
minimal_input_data, backend
):
@policy_function()
Expand All @@ -952,7 +952,7 @@ def a() -> int:
match="Note that the missing nodes contain columns that are grouped by ",
):
main(
main_targets=["results__tree", "fail_if__root_nodes_are_missing"],
main_targets=["results__tree", "fail_if__tt_root_nodes_are_missing"],
input_data={"tree": minimal_input_data},
policy_environment=policy_environment,
date=datetime.date(2024, 1, 1),
Expand Down Expand Up @@ -1365,3 +1365,44 @@ def test_invalid_input_data_as_object_via_main(backend: Literal["jax", "numpy"])
orig_policy_objects={"root": METTSIM_ROOT},
date_str="2025-01-01",
)


@pytest.mark.parametrize(
"main_target",
[
MainTarget.specialized_environment.tax_transfer_function,
MainTarget.raw_results.columns,
],
)
def test_raise_tt_root_nodes_are_missing_without_input_data(
main_target: MainTarget,
backend: Literal["jax", "numpy"],
):
with pytest.raises(
ValueError, match="For computing results, you need to pass data. "
):
main(
date_str="2025-01-01",
main_target=main_target,
backend=backend,
orig_policy_objects={"root": METTSIM_ROOT},
)


def test_raise_some_error_without_input_data(
backend: Literal["jax", "numpy"],
):
with pytest.raises(
ValueError,
match=(
"For computing results, you need to pass data. "
"|"
"The following arguments to `main` are missing"
),
):
main(
date_str="2025-01-01",
main_target=MainTarget.results.df_with_mapper,
backend=backend,
orig_policy_objects={"root": METTSIM_ROOT},
)
18 changes: 16 additions & 2 deletions tests/ttsim/test_end_to_end.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path
from typing import Literal

import dags.tree as dt
import pandas as pd
import pytest

Expand Down Expand Up @@ -68,7 +69,7 @@
}


EXPECTED_RESULTS = pd.DataFrame(
EXPECTED_TT_RESULTS = pd.DataFrame(
{
"payroll_tax_amount_y": [2920.0, 0.0, 0.0],
"payroll_tax_child_tax_credit_amount_m": [8.333333, 0.0, 0.0],
Expand Down Expand Up @@ -99,8 +100,21 @@ def test_end_to_end(input_data_arg, backend: Literal["numpy", "jax"]):
backend=backend,
)
pd.testing.assert_frame_equal(
EXPECTED_RESULTS,
EXPECTED_TT_RESULTS,
result,
check_dtype=False,
check_index_type=False,
)


def test_can_create_input_template(backend: Literal["numpy", "jax"]):
result_template = main(
main_target=MainTarget.templates.input_data_dtypes,
date_str="2025-01-01",
orig_policy_objects={"root": Path(__file__).parent / "mettsim"},
backend=backend,
tt_targets=TTTargets(tree=TARGETS_TREE),
)
flat_result_template = dt.flatten_to_tree_paths(result_template)
flat_expected = dt.flatten_to_tree_paths(INPUT_DF_MAPPER)
assert flat_result_template.keys() == flat_expected.keys()
Loading