Skip to content

Commit

Permalink
New function that creates tree from list of qualified names.
Browse files Browse the repository at this point in the history
  • Loading branch information
MImmesberger committed Feb 1, 2025
1 parent d441709 commit 5f29a33
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
33 changes: 5 additions & 28 deletions src/_gettsim/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import functools
import inspect
import warnings
from functools import reduce
from typing import Any, Literal, get_args

import dags
Expand Down Expand Up @@ -38,7 +37,7 @@
)
from _gettsim.shared import (
KeyErrorMessage,
create_dict_from_list,
create_tree_from_list_of_qualified_names,
format_errors_and_warnings,
format_list_linewise,
get_by_path,
Expand Down Expand Up @@ -102,8 +101,8 @@ def compute_taxes_and_transfers( # noqa: PLR0913
data=data,
)
functions_not_overridden, functions_overridden = partition_tree_by_reference_tree(
tree_to_split=all_functions,
other_tree=data,
target_tree=all_functions,
reference_tree=data,
)
data = _convert_data_to_correct_types(data, functions_overridden)

Expand Down Expand Up @@ -195,7 +194,7 @@ def build_targets_tree(targets: NestedTargetDict | list[str] | str) -> NestedTar

if isinstance(targets, list):
# Build targets tree from list of strings
targets_tree = _build_targets_tree_from_list(targets)
targets_tree = create_tree_from_list_of_qualified_names(targets)
elif isinstance(targets, dict) and all_leafs_none:
# Input is already the correct targets tree
targets_tree = targets
Expand All @@ -210,28 +209,6 @@ def build_targets_tree(targets: NestedTargetDict | list[str] | str) -> NestedTar
return targets_tree


def _build_targets_tree_from_list(targets: list[str]) -> NestedTargetDict:
"""Build a tree from a list of targets.
Parameters
----------
targets : list[str]
List of strings with names of functions whose output is actually needed by the
user.
Example: ["a__b__c", "a__b__d", "a__e"]
Returns
-------
tree : dict
Dictionary representing the tree.
Example: {"a": {"b": {"c": None, "d": None}, "e": None}}
"""
paths = [create_dict_from_list(get_path_from_qualified_name(el)) for el in targets]
targets_tree = reduce(lambda x, y: merge_nested_dicts(x, y), paths, {})
return targets_tree


def _build_targets_tree_from_dict(targets: dict[str, dict | str]) -> NestedTargetDict:
"""Build a tree from a dictionary of targets.
Expand All @@ -257,7 +234,7 @@ def _build_targets_tree_from_dict(targets: dict[str, dict | str]) -> NestedTarge
elif isinstance(v, str):
targets[k] = {v: None}
elif isinstance(v, list):
targets[k] = _build_targets_tree_from_list(v)
targets[k] = create_tree_from_list_of_qualified_names(v)

return targets

Expand Down
22 changes: 22 additions & 0 deletions src/_gettsim/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ def format_list_linewise(list_):
).format(formatted_list=formatted_list)


def create_tree_from_list_of_qualified_names(qualified_names: list[str]) -> dict:
"""Create a tree from a list of qualified names.
Parameters
----------
qualified_names : list[str]
List of qualified names.
Example: ["a__b__c", "a__b__d", "a__e"]
Returns
-------
dict
Tree with qualified names as keys.
Example: {"a": {"b": {"c": None, "d": None}, "e": None}}
"""
paths = [
create_dict_from_list(get_path_from_qualified_name(el))
for el in qualified_names
]
return functools.reduce(lambda x, y: merge_nested_dicts(x, y), paths, {})


def tree_update(
tree: dict[str, Any], path: list[str], value: Any = None
) -> dict[str, Any]:
Expand Down
19 changes: 10 additions & 9 deletions src/_gettsim_tests/test_namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pandas as pd

from _gettsim.aggregation import AggregateByGroupSpec, AggregateByPIDSpec
from _gettsim.interface import compute_taxes_and_transfers
from _gettsim.policy_environment import PolicyEnvironment
from _gettsim_tests.test_data.namespaces.module1 import FUNCTIONS_MODULE1
Expand All @@ -27,20 +28,20 @@

AGGREGATION_BY_GROUP_SPEC = {
"module1": {
"group_mean_bg": {
"source_col": "f",
"aggr": "sum",
},
"group_mean_bg": AggregateByGroupSpec(
source_col="f",
aggr="sum",
),
},
}

AGGREGATION_BY_PID_SPEC = {
"module2": {
"p_id_aggregation_target": {
"p_id_to_aggregate_by": "groupings__some_foreign_keys",
"source_col": "g_hh",
"aggr": "sum",
},
"p_id_aggregation_target": AggregateByPIDSpec(
p_id_to_aggregate_by="groupings__some_foreign_keys",
source_col="g_hh",
aggr="sum",
),
},
}

Expand Down

0 comments on commit 5f29a33

Please sign in to comment.