Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log calculated metric with SplitTrainer #364

Merged
merged 4 commits into from
Nov 7, 2023
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
9 changes: 5 additions & 4 deletions psycop/common/model_training_v2/loggers/base_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import wasabi

from psycop.common.global_utils.config_utils import flatten_nested_dict
from psycop.common.model_training_v2.metrics.base_metric import CalculatedMetric


class BaselineLogger(Protocol):
Expand All @@ -15,7 +16,7 @@ def warn(self, message: str) -> None:
def fail(self, message: str) -> None:
...

def log_metric(self, name: str, value: float) -> None:
def log_metric(self, metric: CalculatedMetric) -> None:
...

def log_config(self, config: dict[str, Any]) -> None:
Expand All @@ -35,9 +36,9 @@ def warn(self, message: str) -> None:
def fail(self, message: str) -> None:
self._l.fail(message)

def log_metric(self, name: str, value: float) -> None:
self._l.divider(f"Logging metric {name}")
self._l.info(f"{name}: {value}")
def log_metric(self, metric: CalculatedMetric) -> None:
self._l.divider(f"Logging metric {metric.name}")
self._l.info(f"{metric.name}: {metric.value}")

def log_config(self, config: dict[str, Any]) -> None:
self._l.divider("Logging config")
Expand Down
2 changes: 1 addition & 1 deletion psycop/common/model_training_v2/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def train_baseline_model(cfg: BaselineSchema) -> float:
result = cfg.training_method.train()
result.eval_dataset.to_disk(path=cfg.experiment_path)

return result.metric
return result.metric.value
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from dataclasses import dataclass
from typing import Protocol

from psycop.common.model_training_v2.metrics.base_metric import CalculatedMetric

from ..loggers.base_logger import BaselineLogger
from ..presplit_preprocessing.pipeline import PreprocessingPipeline
from ..presplit_preprocessing.polars_frame import PolarsFrame
Expand All @@ -12,7 +14,7 @@

@dataclass(frozen=True)
class TrainingResult:
metric: float
metric: CalculatedMetric
eval_dataset: BaseEvalDataset


Expand Down Expand Up @@ -60,18 +62,20 @@ def train(self) -> TrainingResult:
training_data_preprocessed = self.preprocessing_pipeline.apply(
data=self.training_data,
)
self.preprocessing_pipeline.apply(
validation_data = self.preprocessing_pipeline.apply(
data=self.validation_data,
)

self.problem_type.train(
x=training_data_preprocessed.drop(self.training_outcome_col_name),
y=training_data_preprocessed.select(self.training_outcome_col_name),
)

result = self.problem_type.evaluate(
x=self.validation_data.drop(self.validation_outcome_col_name),
y=self.validation_data.select(self.validation_outcome_col_name),
x=validation_data.drop(self.validation_outcome_col_name),
y=validation_data.select(self.validation_outcome_col_name),
)

self.logger.log_metric(name="metric", value=result.metric)
self.logger.log_metric(result.metric)

return result
Loading