Konstantin Sofeikov
12/19/2024, 5:41 PM@save_to.pickle decorator in this context so the result of each node after parameterise is saved to a different file?
@parameterize(
team_nodes_one_hot=dict(target_df=source("team_nodes"), node_type=value("team")),
player_nodes_one_hot=dict(target_df=source("player_nodes"), node_type=value("player"))
)
@config.when(stage="training")
@save_to.pickle(path=value('encoders.pkl'))
def one_hot_categorical_encoding__training(
target_df: pl.DataFrame, data_config: DataConfig, node_type: str
) -> dict[str, dict[str, OneHotEncoder]]:
Thanks!Stefan Krawczyk
12/19/2024, 6:26 PMStefan Krawczyk
12/19/2024, 6:28 PM# my_reusable_dag.py
import pandas as pd
from typing import Dict, Any
from hamilton.function_modifiers import parameterize, source, value, save_to, config, subdag
@save_to.pickle(path=source('encoder_output_path'))
@config.when(stage="training")
def one_hot_categorical_encoding__training(
target_df: pd.DataFrame, data_config: Dict, node_type: str
) -> dict[str, dict[str, Any]]:
pass
You could then do manual subdag incantations:
import pandas as pd
from typing import Dict, Any
from hamilton.function_modifiers import parameterize, source, value, save_to, config, subdag
import my_reusable_dag
@subdag(
my_reusable_dag,
config={"stage":"training"},
inputs=dict(target_df=source("team_nodes"), node_type=value("team"), encoder_output_path=source("encoder_output_path"))
)
def team_nodes_one_hot(one_hot_categorical_encoding: dict[str, dict[str, Any]]) -> dict[str, dict[str, Any]]:
return one_hot_categorical_encoding
or use the parameterized_subdag decorator:
import pandas as pd
from typing import Dict, Any
from hamilton.function_modifiers import parameterize, source, value, save_to, config, subdag, parameterized_subdag
import my_reusable_dag
@parameterized_subdag(
my_reusable_dag,
team_nodes_one_hot=dict(config={"stage":"training"},
inputs=dict(target_df=source("team_nodes"), node_type=value("team"), encoder_output_path=source("encoder_output_path"))),
player_nodes_one_hot=dict(config={"stage":"training"},
inputs=dict(target_df=source("player_nodes"), node_type=value("player"), encoder_output_path=source("encoder_output_path")))
)
def encoder(one_hot_categorical_encoding: dict[str, dict[str, Any]]) -> dict[str, dict[str, Any]]:
return one_hot_categorical_encoding
We do suggest verbose ways are better than terse ones. It’s less intimidating to read.Stefan Krawczyk
12/19/2024, 6:31 PMKonstantin Sofeikov
12/19/2024, 6:32 PMElijah Ben Izzy
12/19/2024, 7:26 PM