Skip to content

Commit 109dc3c

Browse files
committed
feat: dml.log_metric() now doesn't fail per default if no stage is present
1 parent 9050cf5 commit 109dc3c

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

dmlcloud/core/pipeline.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,22 @@ def current_stage() -> Stage | None:
6666
return current_pipe().current_stage
6767

6868

69-
def log_metric(name: str, value: Any, reduction: str = 'mean', prefixed: bool = True):
69+
def log_metric(name: str, value: Any, reduction: str = 'mean', prefixed: bool = True, ignore_missing_stage: bool = True):
7070
"""
7171
Shorthand for current_stage().log
72+
73+
Args:
74+
name (str): Name of the metric to log.
75+
value (Any): Value of the metric to log.
76+
reduction (str, optional): Reduction method to apply to the metric value. Defaults to 'mean'.
77+
prefixed (bool, optional): Whether to prefix the metric name with the stage name. Defaults to True.
78+
ignore_missing_stage (bool, optional): Whether to ignore logging if there is no current stage. Defaults to True.
7279
"""
73-
return current_stage().log(name, value, reduction=reduction, prefixed=prefixed)
80+
stage = current_stage()
81+
if stage is not None:
82+
stage.log(name, value, reduction=reduction, prefixed=prefixed)
83+
elif not ignore_missing_stage:
84+
raise ValueError('No current stage to log metric to. Set ignore_missing_stage=True to ignore this error.')
7485

7586

7687
class _RunGuard:

0 commit comments

Comments
 (0)