euler-train 2.9.0__tar.gz → 2.11.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. {euler_train-2.9.0 → euler_train-2.11.0}/PKG-INFO +1 -1
  2. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train/__init__.py +23 -0
  3. euler_train-2.11.0/euler_train/outputs_from_batch.py +275 -0
  4. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train/run.py +87 -0
  5. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train.egg-info/PKG-INFO +1 -1
  6. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train.egg-info/SOURCES.txt +2 -0
  7. {euler_train-2.9.0 → euler_train-2.11.0}/pyproject.toml +1 -1
  8. euler_train-2.11.0/tests/test_outputs_from_batch.py +463 -0
  9. {euler_train-2.9.0 → euler_train-2.11.0}/tests/test_runlog.py +156 -0
  10. {euler_train-2.9.0 → euler_train-2.11.0}/README.md +0 -0
  11. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train/_dataset_contract.py +0 -0
  12. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train/architecture.py +0 -0
  13. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train/environment.py +0 -0
  14. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train/git_info.py +0 -0
  15. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train/outputs.py +0 -0
  16. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train/serialization.py +0 -0
  17. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train/slurm.py +0 -0
  18. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train/stream.py +0 -0
  19. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train/stream_check_cli.py +0 -0
  20. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train.egg-info/dependency_links.txt +0 -0
  21. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train.egg-info/entry_points.txt +0 -0
  22. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train.egg-info/requires.txt +0 -0
  23. {euler_train-2.9.0 → euler_train-2.11.0}/euler_train.egg-info/top_level.txt +0 -0
  24. {euler_train-2.9.0 → euler_train-2.11.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: euler-train
3
- Version: 2.9.0
3
+ Version: 2.11.0
4
4
  Summary: Lightweight file-based experiment logger for PyTorch
5
5
  Requires-Python: >=3.9
6
6
  Requires-Dist: numpy
@@ -6,6 +6,14 @@ from typing import Any
6
6
  from . import _dataset_contract # noqa: F401
7
7
  from .run import Run
8
8
  from .architecture import export_architecture
9
+ from .outputs_from_batch import (
10
+ SAMPLE_ID_UNSAFE_PATTERN,
11
+ build_save_kwargs,
12
+ extract_sample_ids,
13
+ make_named_slot,
14
+ sanitize_sample_id,
15
+ save_outputs_from_batch,
16
+ )
9
17
  from .stream import (
10
18
  EulerViewStreamConfig,
11
19
  EulerViewStreamConsumer,
@@ -20,10 +28,16 @@ __all__ = [
20
28
  "EulerViewStreamConsumer",
21
29
  "OutputStream",
22
30
  "OutputStreamConsumer",
31
+ "SAMPLE_ID_UNSAFE_PATTERN",
23
32
  "StreamContext",
33
+ "build_save_kwargs",
24
34
  "check_stream_handshake",
35
+ "extract_sample_ids",
25
36
  "init",
37
+ "make_named_slot",
26
38
  "Run",
39
+ "sanitize_sample_id",
40
+ "save_outputs_from_batch",
27
41
  "export_architecture",
28
42
  ]
29
43
  __version__ = "0.1.0"
@@ -42,6 +56,7 @@ def init(
42
56
  mode: str | None = None,
43
57
  stream: Any = None,
44
58
  metric_naming: dict[str, Any] | None = None,
59
+ pipeline: dict[str, Any] | None = None,
45
60
  ) -> Run:
46
61
  """Create a new run — or resume an existing one — and return the handle.
47
62
 
@@ -121,6 +136,13 @@ def init(
121
136
  in the stream init event so euler-view can resolve per-metric
122
137
  namespaces. Typically generated by
123
138
  ``euler_metric_naming.MetricNamespace.training_naming_config()``.
139
+ pipeline:
140
+ Optional pipeline identity mapping. When the run is launched by
141
+ an automated pipeline, pass a dict with at least ``attach_id``
142
+ (a non-empty string) so downstream processes can correlate
143
+ file-based artifacts back to the originating pipeline stage.
144
+ Stored in ``meta.json`` under ``pipeline``. Additional keys
145
+ (e.g. ``stage``, ``pipeline_id``) are preserved as-is.
124
146
  """
125
147
  return Run(
126
148
  dir=dir, config=config, meta=meta,
@@ -131,4 +153,5 @@ def init(
131
153
  evaluations=evaluations, mode=mode,
132
154
  stream=stream,
133
155
  metric_naming=metric_naming,
156
+ pipeline=pipeline,
134
157
  )
@@ -0,0 +1,275 @@
1
+ """Batch-aware convenience layer for :meth:`Run.save_outputs`.
2
+
3
+ The helpers here bridge a collated training batch (for example, the output
4
+ of a ``torch.utils.data.DataLoader`` wrapping an
5
+ ``euler_loading.MultiModalDataset``) and the slot structure that
6
+ :meth:`Run.save_outputs` expects:
7
+
8
+ - sample ids are pulled from the batch dict and sanitized into
9
+ filesystem-safe basenames,
10
+ - per-slot tensors are wrapped into ``{sample_id: item}`` mappings so
11
+ ``save_outputs`` uses named filenames instead of sequential indices,
12
+ - an optional ``id_schema`` is injected into the manifest metadata so
13
+ downstream consumers can split or match ids back to their source
14
+ dataset.
15
+
16
+ There is no import-time dependency on euler-loading: the *dataset*
17
+ argument is duck-typed through a ``describe_id_schema()`` method and the
18
+ batch is duck-typed through its ``full_id`` / ``id`` list entries.
19
+ """
20
+ from __future__ import annotations
21
+
22
+ import re
23
+ from collections.abc import Mapping
24
+ from typing import Any, Callable, Iterable
25
+
26
+ __all__ = [
27
+ "SAMPLE_ID_UNSAFE_PATTERN",
28
+ "sanitize_sample_id",
29
+ "extract_sample_ids",
30
+ "make_named_slot",
31
+ "build_save_kwargs",
32
+ "save_outputs_from_batch",
33
+ ]
34
+
35
+
36
+ SAMPLE_ID_UNSAFE_PATTERN = r"[^A-Za-z0-9._-]+"
37
+ _SAMPLE_ID_UNSAFE = re.compile(SAMPLE_ID_UNSAFE_PATTERN)
38
+
39
+
40
+ def sanitize_sample_id(name: Any) -> str:
41
+ """Return a filesystem-safe basename derived from *name*.
42
+
43
+ Runs of characters outside ``[A-Za-z0-9._-]`` collapse into a single
44
+ underscore, and leading/trailing underscores are stripped. Empty
45
+ results fall back to ``"sample"``.
46
+ """
47
+ text = str(name)
48
+ cleaned = _SAMPLE_ID_UNSAFE.sub("_", text).strip("_")
49
+ return cleaned or "sample"
50
+
51
+
52
+ def extract_sample_ids(
53
+ batch: Mapping[str, Any],
54
+ *,
55
+ sanitize: bool = True,
56
+ keys: Iterable[str] | None = None,
57
+ ) -> list[str] | None:
58
+ """Pull per-sample identifiers from a collated batch dict.
59
+
60
+ PyTorch's default ``collate_fn`` turns a per-sample string field into a
61
+ ``list[str]``. This helper returns the first such list found,
62
+ preferring ``full_id`` (which includes the hierarchy path) over the
63
+ bare ``id``.
64
+
65
+ Args:
66
+ batch: A batched sample dict.
67
+ sanitize: When *True*, each id is passed through
68
+ :func:`sanitize_sample_id` to make it filesystem-safe.
69
+ keys: Optional custom search order. Defaults to
70
+ ``("full_id", "id")``.
71
+
72
+ Returns:
73
+ A list of (optionally sanitized) ids, or *None* when no suitable
74
+ field is present.
75
+ """
76
+ lookup = tuple(keys) if keys is not None else ("full_id", "id")
77
+ for key in lookup:
78
+ candidate = batch.get(key)
79
+ if isinstance(candidate, list) and candidate:
80
+ if all(isinstance(v, (str, bytes)) for v in candidate):
81
+ values = [
82
+ v.decode() if isinstance(v, bytes) else v for v in candidate
83
+ ]
84
+ return [sanitize_sample_id(v) for v in values] if sanitize else values
85
+ return None
86
+
87
+
88
+ def _slice_tensor(value: Any, n: int | None) -> Any:
89
+ """Return ``value[:n]`` — moving torch tensors off-GPU when possible."""
90
+ if n is not None:
91
+ value = value[:n]
92
+ detach = getattr(value, "detach", None)
93
+ if callable(detach):
94
+ detached = detach()
95
+ cpu = getattr(detached, "cpu", None)
96
+ return cpu() if callable(cpu) else detached
97
+ return value
98
+
99
+
100
+ def make_named_slot(
101
+ tensor: Any,
102
+ sample_ids: list[str] | None,
103
+ n: int | None = None,
104
+ *,
105
+ transform: Callable[[Any], Any] | None = None,
106
+ ) -> Any:
107
+ """Return an indexed batch slice or a ``{sample_id: item}`` dict.
108
+
109
+ When *sample_ids* is provided, the tensor's leading dimension is split
110
+ and paired with the ids, producing a dict that
111
+ :meth:`Run.save_outputs` treats as *named* outputs. When *sample_ids*
112
+ is *None*, the tensor is returned unchanged so ``save_outputs`` falls
113
+ back to sequential-index filenames.
114
+
115
+ Args:
116
+ tensor: Tensor or ``None`` to wrap.
117
+ sample_ids: Optional list of sanitized ids (one per batch item).
118
+ n: Optional prefix length; defaults to using the whole tensor.
119
+ transform: Optional callable applied to the sliced tensor before
120
+ wrapping (e.g. a denormalize step).
121
+
122
+ Returns:
123
+ ``None`` when *tensor* is ``None``; the sliced tensor when
124
+ *sample_ids* is ``None``; otherwise a ``{id: item}`` mapping.
125
+ """
126
+ if tensor is None:
127
+ return None
128
+ sliced = _slice_tensor(tensor, n)
129
+ if transform is not None:
130
+ sliced = transform(sliced)
131
+ if sample_ids is None:
132
+ return sliced
133
+ count = min(
134
+ n if n is not None else len(sample_ids),
135
+ len(sample_ids),
136
+ len(sliced),
137
+ )
138
+ return {sample_ids[i]: sliced[i] for i in range(count)}
139
+
140
+
141
+ def _map_slot_value(value: Any, sample_ids: list[str] | None, n: int | None) -> Any:
142
+ """Shape a single slot value for :meth:`Run.save_outputs`.
143
+
144
+ Dict values (already named, or aux sub-dicts) are copied through
145
+ unchanged; ``None`` skips the slot; everything else is assumed to be a
146
+ tensor/array and is wrapped via :func:`make_named_slot`.
147
+ """
148
+ if value is None:
149
+ return None
150
+ if isinstance(value, Mapping):
151
+ return dict(value)
152
+ return make_named_slot(value, sample_ids, n)
153
+
154
+
155
+ def build_save_kwargs(
156
+ slots: Mapping[str, Mapping[str, Any] | None],
157
+ sample_ids: list[str] | None,
158
+ n: int | None = None,
159
+ ) -> dict[str, dict[str, Any]]:
160
+ """Apply :func:`make_named_slot` across a full save_outputs kwargs dict.
161
+
162
+ *slots* has the same ``{output_type: {slot_name: value}}`` shape that
163
+ :meth:`Run.save_outputs` accepts as keyword arguments. Non-dict,
164
+ non-``None`` leaf values are wrapped as named-sample dicts; ``aux``
165
+ sub-dicts are recursed into so each contained tensor is wrapped too.
166
+ """
167
+ result: dict[str, dict[str, Any]] = {}
168
+ for output_type, slot_map in slots.items():
169
+ if slot_map is None:
170
+ continue
171
+ transformed: dict[str, Any] = {}
172
+ for slot_name, value in slot_map.items():
173
+ if slot_name == "aux" and isinstance(value, Mapping):
174
+ aux_transformed: dict[str, Any] = {}
175
+ for aux_name, aux_value in value.items():
176
+ aux_transformed[aux_name] = _map_slot_value(
177
+ aux_value, sample_ids, n
178
+ )
179
+ transformed[slot_name] = aux_transformed
180
+ else:
181
+ transformed[slot_name] = _map_slot_value(value, sample_ids, n)
182
+ result[output_type] = transformed
183
+ return result
184
+
185
+
186
+ def _describe_dataset_id_schema(dataset: Any) -> dict[str, Any] | None:
187
+ """Read an id-construction schema from a duck-typed dataset object."""
188
+ describer = getattr(dataset, "describe_id_schema", None)
189
+ if not callable(describer):
190
+ return None
191
+ try:
192
+ schema = describer()
193
+ except Exception:
194
+ return None
195
+ if isinstance(schema, Mapping) and schema:
196
+ return dict(schema)
197
+ return None
198
+
199
+
200
+ def save_outputs_from_batch(
201
+ run: Any,
202
+ *,
203
+ batch: Mapping[str, Any],
204
+ epoch: int | None = None,
205
+ step: int | None = None,
206
+ metadata: Mapping[str, Any] | None = None,
207
+ dataset: Any = None,
208
+ id_schema: Mapping[str, Any] | None = None,
209
+ n: int | None = None,
210
+ sample_ids: list[str] | None = None,
211
+ include_id_schema: bool = True,
212
+ **output_types: Mapping[str, Any] | None,
213
+ ) -> Any:
214
+ """Forward a batch of predictions to :meth:`Run.save_outputs`.
215
+
216
+ This is the free-function form; :meth:`Run.save_outputs_from_batch` is
217
+ the preferred entry point on a live run.
218
+
219
+ Extracts per-sample ids from *batch*, converts each output slot from a
220
+ tensor into the ``{id: item}`` form ``save_outputs`` expects, and —
221
+ when *dataset* (or an explicit *id_schema*) is supplied — injects the
222
+ id-construction schema into *metadata* so downstream tooling can match
223
+ ids back to their ds-crawler origin.
224
+
225
+ Args:
226
+ run: Object exposing ``save_outputs(epoch, step, metadata, **kwargs)``.
227
+ Duck-typed; typically a :class:`Run`.
228
+ batch: Collated batch dict; ``full_id`` (preferred) or ``id`` is
229
+ expected to be a ``list[str]``.
230
+ epoch: Forwarded to ``run.save_outputs``.
231
+ step: Forwarded to ``run.save_outputs``.
232
+ metadata: Base metadata to forward. When *include_id_schema* is
233
+ *True*, *metadata* is not *None*, and either *id_schema*
234
+ or ``dataset.describe_id_schema()`` is available, the
235
+ schema is merged under ``metadata["id_schema"]``
236
+ (existing values take precedence). When *metadata* is
237
+ *None*, no id-schema is injected — injecting would
238
+ create a metadata dict missing the required ``dataset``
239
+ key that :meth:`Run.save_outputs` validates.
240
+ dataset: Optional dataset object exposing ``describe_id_schema()``.
241
+ Used only for metadata enrichment — no hard dependency.
242
+ id_schema: Optional explicit id-schema dict. Overrides the
243
+ dataset-derived schema when both are supplied.
244
+ n: Optional batch-prefix length; defaults to the full batch.
245
+ sample_ids: Optional explicit id list overriding batch extraction.
246
+ include_id_schema: When *False*, skip id-schema injection entirely.
247
+ **output_types: Per-output-type slot dicts forwarded to
248
+ ``run.save_outputs`` after id-wrapping.
249
+
250
+ Returns:
251
+ Whatever ``run.save_outputs`` returns (typically the output
252
+ directory).
253
+ """
254
+ if sample_ids is None:
255
+ sample_ids = extract_sample_ids(batch)
256
+
257
+ kwargs = build_save_kwargs(output_types, sample_ids, n)
258
+
259
+ merged_metadata: dict[str, Any] | None = (
260
+ dict(metadata) if metadata is not None else None
261
+ )
262
+
263
+ if include_id_schema and merged_metadata is not None:
264
+ schema = dict(id_schema) if id_schema is not None else None
265
+ if schema is None and dataset is not None:
266
+ schema = _describe_dataset_id_schema(dataset)
267
+ if schema:
268
+ merged_metadata.setdefault("id_schema", schema)
269
+
270
+ return run.save_outputs(
271
+ epoch=epoch,
272
+ step=step,
273
+ metadata=merged_metadata,
274
+ **kwargs,
275
+ )
@@ -49,6 +49,7 @@ class Run:
49
49
  mode: str | None = None,
50
50
  stream: Any = None,
51
51
  metric_naming: dict[str, Any] | None = None,
52
+ pipeline: dict[str, Any] | None = None,
52
53
  ) -> None:
53
54
  # ── detect run-directory shorthand ────────────────────────
54
55
  # When `dir` points to an existing run directory (contains
@@ -182,6 +183,13 @@ class Run:
182
183
  _validate_metric_naming(metric_naming)
183
184
  self._meta["metric_naming"] = metric_naming
184
185
 
186
+ # ── pipeline identity (optional, for automated pipelines) ──
187
+ if pipeline is None:
188
+ pipeline = _pipeline_from_env()
189
+ if pipeline is not None:
190
+ _validate_pipeline(pipeline)
191
+ self._meta["pipeline"] = pipeline
192
+
185
193
  self._mark_mode_running()
186
194
  self._flush_meta()
187
195
  self._bind_stream()
@@ -463,6 +471,66 @@ class Run:
463
471
  )
464
472
  return base
465
473
 
474
+ def save_outputs_from_batch(
475
+ self,
476
+ *,
477
+ batch: Mapping[str, Any],
478
+ epoch: int | None = None,
479
+ step: int | None = None,
480
+ metadata: Mapping[str, Any] | None = None,
481
+ dataset: Any = None,
482
+ id_schema: Mapping[str, Any] | None = None,
483
+ n: int | None = None,
484
+ sample_ids: list[str] | None = None,
485
+ include_id_schema: bool = True,
486
+ **output_types: Mapping[str, Any] | None,
487
+ ) -> Path:
488
+ """Convenience wrapper around :meth:`save_outputs` for collated batches.
489
+
490
+ Pulls per-sample ids from *batch* (preferring ``full_id`` over ``id``,
491
+ as produced by ``euler_loading.MultiModalDataset``), wraps each
492
+ per-slot tensor as a ``{sample_id: item}`` mapping so output files are
493
+ named, and — when *dataset* or an explicit *id_schema* is supplied —
494
+ merges the id-construction schema under ``metadata["id_schema"]`` so
495
+ downstream tooling can match ids back to their ds-crawler origin.
496
+
497
+ The *dataset* argument is duck-typed through ``describe_id_schema()``;
498
+ there is no import-time dependency on euler-loading.
499
+
500
+ Args:
501
+ batch: Collated batch dict with ``full_id`` or ``id`` as
502
+ ``list[str]`` (PyTorch's default collate behavior).
503
+ epoch: Forwarded to :meth:`save_outputs`.
504
+ step: Forwarded to :meth:`save_outputs`.
505
+ metadata: Base metadata. Existing keys take precedence over the
506
+ auto-injected ``id_schema``.
507
+ dataset: Optional dataset exposing ``describe_id_schema()``.
508
+ id_schema: Explicit id-schema override.
509
+ n: Optional batch-prefix length; defaults to the full batch.
510
+ sample_ids: Explicit id list overriding batch extraction.
511
+ include_id_schema: When *False*, skip id-schema injection.
512
+ **output_types: Per-output-type slot dicts (same shape as
513
+ :meth:`save_outputs`).
514
+
515
+ Returns:
516
+ The directory that was written to.
517
+ """
518
+ from .outputs_from_batch import save_outputs_from_batch as _impl
519
+
520
+ return _impl(
521
+ self,
522
+ batch=batch,
523
+ epoch=epoch,
524
+ step=step,
525
+ metadata=metadata,
526
+ dataset=dataset,
527
+ id_schema=id_schema,
528
+ n=n,
529
+ sample_ids=sample_ids,
530
+ include_id_schema=include_id_schema,
531
+ **output_types,
532
+ )
533
+
466
534
  # ── checkpoints ───────────────────────────────────────────────
467
535
 
468
536
  def init_checkpoint_dir(self, base: str | Path | None = None) -> Path:
@@ -1081,6 +1149,25 @@ def _validate_metric_naming(value: Any) -> None:
1081
1149
  )
1082
1150
 
1083
1151
 
1152
+ def _pipeline_from_env() -> dict[str, Any] | None:
1153
+ """Build a pipeline dict from ``$EULER_SESSION_ID`` when set."""
1154
+ session_id = os.environ.get("EULER_SESSION_ID", "").strip()
1155
+ if not session_id:
1156
+ return None
1157
+ return {"attach_id": session_id}
1158
+
1159
+
1160
+ def _validate_pipeline(value: Any) -> None:
1161
+ """Validate the pipeline identity payload."""
1162
+ if not isinstance(value, dict):
1163
+ raise TypeError("pipeline must be a dict")
1164
+ attach_id = value.get("attach_id")
1165
+ if not isinstance(attach_id, str) or not attach_id.strip():
1166
+ raise ValueError(
1167
+ "pipeline must contain a non-empty 'attach_id' string"
1168
+ )
1169
+
1170
+
1084
1171
  def _build_datasets_meta(
1085
1172
  datasets: Mapping[str, Any],
1086
1173
  existing: Any,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: euler-train
3
- Version: 2.9.0
3
+ Version: 2.11.0
4
4
  Summary: Lightweight file-based experiment logger for PyTorch
5
5
  Requires-Python: >=3.9
6
6
  Requires-Dist: numpy
@@ -6,6 +6,7 @@ euler_train/architecture.py
6
6
  euler_train/environment.py
7
7
  euler_train/git_info.py
8
8
  euler_train/outputs.py
9
+ euler_train/outputs_from_batch.py
9
10
  euler_train/run.py
10
11
  euler_train/serialization.py
11
12
  euler_train/slurm.py
@@ -17,4 +18,5 @@ euler_train.egg-info/dependency_links.txt
17
18
  euler_train.egg-info/entry_points.txt
18
19
  euler_train.egg-info/requires.txt
19
20
  euler_train.egg-info/top_level.txt
21
+ tests/test_outputs_from_batch.py
20
22
  tests/test_runlog.py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "euler-train"
7
- version = "2.9.0"
7
+ version = "2.11.0"
8
8
  description = "Lightweight file-based experiment logger for PyTorch"
9
9
  requires-python = ">=3.9"
10
10
  dependencies = ["numpy", "euler-dataset-contract"]
@@ -0,0 +1,463 @@
1
+ """Tests for euler_train.outputs_from_batch — batch-aware save_outputs layer."""
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ from pathlib import Path
6
+
7
+ import numpy as np
8
+ import pytest
9
+ import torch
10
+
11
+ import euler_train
12
+ from euler_train.outputs_from_batch import (
13
+ build_save_kwargs,
14
+ extract_sample_ids,
15
+ make_named_slot,
16
+ sanitize_sample_id,
17
+ save_outputs_from_batch,
18
+ )
19
+
20
+
21
+ # ═══════════════════════════════════════════════════════════════════════════
22
+ # Helpers
23
+ # ═══════════════════════════════════════════════════════════════════════════
24
+
25
+ class _StubDataset:
26
+ """Duck-types ``describe_id_schema()`` without importing euler-loading."""
27
+
28
+ def __init__(self, schema: dict | None) -> None:
29
+ self._schema = schema
30
+
31
+ def describe_id_schema(self) -> dict | None:
32
+ return self._schema
33
+
34
+
35
+ class _FailingDataset:
36
+ def describe_id_schema(self) -> dict:
37
+ raise RuntimeError("boom")
38
+
39
+
40
+ # ═══════════════════════════════════════════════════════════════════════════
41
+ # sanitize_sample_id
42
+ # ═══════════════════════════════════════════════════════════════════════════
43
+
44
+ class TestSanitizeSampleId:
45
+ def test_safe_chars_passthrough(self):
46
+ assert sanitize_sample_id("abc_123.png-v2") == "abc_123.png-v2"
47
+
48
+ def test_unsafe_chars_collapsed_to_underscore(self):
49
+ assert sanitize_sample_id("a/b\\c d") == "a_b_c_d"
50
+
51
+ def test_leading_trailing_underscores_stripped(self):
52
+ assert sanitize_sample_id("///foo///") == "foo"
53
+
54
+ def test_empty_input_falls_back_to_default(self):
55
+ assert sanitize_sample_id("") == "sample"
56
+ assert sanitize_sample_id("///") == "sample"
57
+
58
+ def test_non_string_converted_via_str(self):
59
+ assert sanitize_sample_id(42) == "42"
60
+
61
+
62
+ # ═══════════════════════════════════════════════════════════════════════════
63
+ # extract_sample_ids
64
+ # ═══════════════════════════════════════════════════════════════════════════
65
+
66
+ class TestExtractSampleIds:
67
+ def test_prefers_full_id_over_id(self):
68
+ batch = {"full_id": ["/scene/a", "/scene/b"], "id": ["a", "b"]}
69
+ assert extract_sample_ids(batch) == ["scene_a", "scene_b"]
70
+
71
+ def test_falls_back_to_id_when_full_id_missing(self):
72
+ batch = {"id": ["alpha", "beta"]}
73
+ assert extract_sample_ids(batch) == ["alpha", "beta"]
74
+
75
+ def test_returns_none_when_no_ids_present(self):
76
+ assert extract_sample_ids({"foo": "bar"}) is None
77
+
78
+ def test_returns_none_when_list_is_empty(self):
79
+ assert extract_sample_ids({"full_id": []}) is None
80
+
81
+ def test_returns_none_for_non_list_value(self):
82
+ assert extract_sample_ids({"full_id": "single-string"}) is None
83
+
84
+ def test_returns_none_for_mixed_content(self):
85
+ assert extract_sample_ids({"full_id": ["a", 123]}) is None
86
+
87
+ def test_sanitize_false_preserves_raw(self):
88
+ batch = {"full_id": ["/scene/a", "/scene/b"]}
89
+ assert extract_sample_ids(batch, sanitize=False) == ["/scene/a", "/scene/b"]
90
+
91
+ def test_custom_keys_order(self):
92
+ batch = {"full_id": ["x"], "id": ["y"]}
93
+ assert extract_sample_ids(batch, keys=("id",)) == ["y"]
94
+
95
+ def test_bytes_decoded_to_str(self):
96
+ batch = {"full_id": [b"foo/bar", b"baz"]}
97
+ assert extract_sample_ids(batch) == ["foo_bar", "baz"]
98
+
99
+
100
+ # ═══════════════════════════════════════════════════════════════════════════
101
+ # make_named_slot
102
+ # ═══════════════════════════════════════════════════════════════════════════
103
+
104
+ class TestMakeNamedSlot:
105
+ def test_none_tensor_returns_none(self):
106
+ assert make_named_slot(None, ["a", "b"]) is None
107
+
108
+ def test_none_ids_returns_tensor_passthrough(self):
109
+ t = torch.zeros(2, 3, 3)
110
+ out = make_named_slot(t, None)
111
+ assert torch.is_tensor(out)
112
+ assert out.shape == (2, 3, 3)
113
+
114
+ def test_wraps_to_named_dict(self):
115
+ t = torch.arange(6).view(2, 3)
116
+ out = make_named_slot(t, ["s1", "s2"])
117
+ assert set(out.keys()) == {"s1", "s2"}
118
+ assert torch.equal(out["s1"], torch.tensor([0, 1, 2]))
119
+ assert torch.equal(out["s2"], torch.tensor([3, 4, 5]))
120
+
121
+ def test_n_prefix_limits_count(self):
122
+ t = torch.arange(12).view(4, 3)
123
+ out = make_named_slot(t, ["s0", "s1", "s2", "s3"], n=2)
124
+ assert set(out.keys()) == {"s0", "s1"}
125
+
126
+ def test_transform_applied_before_wrap(self):
127
+ t = torch.ones(2, 2) * 3.0
128
+ out = make_named_slot(t, ["a", "b"], transform=lambda x: x * 2)
129
+ assert torch.equal(out["a"], torch.tensor([6.0, 6.0]))
130
+
131
+ def test_length_mismatch_uses_min_safely(self):
132
+ t = torch.arange(3).view(3, 1)
133
+ out = make_named_slot(t, ["a", "b"])
134
+ assert set(out.keys()) == {"a", "b"}
135
+
136
+ def test_cuda_detach_cpu_path_on_plain_tensor(self):
137
+ # detach()/cpu() exist on plain torch tensors even without CUDA;
138
+ # this exercises the detach/cpu coercion path.
139
+ t = torch.arange(4, dtype=torch.float32).view(2, 2).requires_grad_(True)
140
+ out = make_named_slot(t, None)
141
+ assert not out.requires_grad
142
+
143
+ def test_numpy_array_accepted(self):
144
+ arr = np.arange(6).reshape(2, 3)
145
+ out = make_named_slot(arr, ["a", "b"])
146
+ assert set(out.keys()) == {"a", "b"}
147
+ np.testing.assert_array_equal(out["a"], np.array([0, 1, 2]))
148
+
149
+
150
+ # ═══════════════════════════════════════════════════════════════════════════
151
+ # build_save_kwargs
152
+ # ═══════════════════════════════════════════════════════════════════════════
153
+
154
+ class TestBuildSaveKwargs:
155
+ def test_tensors_wrapped_into_named_dicts(self):
156
+ slots = {"rgb": {"pred": torch.arange(6).view(2, 3)}}
157
+ out = build_save_kwargs(slots, ["s0", "s1"])
158
+ assert set(out["rgb"]["pred"].keys()) == {"s0", "s1"}
159
+
160
+ def test_none_output_type_skipped(self):
161
+ out = build_save_kwargs({"rgb": None, "depth": {"pred": torch.ones(1, 2)}}, ["a"])
162
+ assert "rgb" not in out
163
+ assert "depth" in out
164
+
165
+ def test_none_slot_value_preserved_as_none(self):
166
+ slots = {"rgb": {"pred": torch.ones(1, 2), "gt": None}}
167
+ out = build_save_kwargs(slots, ["a"])
168
+ assert out["rgb"]["gt"] is None
169
+ assert isinstance(out["rgb"]["pred"], dict)
170
+
171
+ def test_existing_mapping_passed_through(self):
172
+ explicit = {"custom_id": torch.zeros(2, 2)}
173
+ slots = {"rgb": {"pred": explicit}}
174
+ out = build_save_kwargs(slots, ["a"])
175
+ assert out["rgb"]["pred"] == explicit
176
+ # must be a *copy*, not the same object
177
+ assert out["rgb"]["pred"] is not explicit
178
+
179
+ def test_aux_sub_dict_recurses(self):
180
+ slots = {
181
+ "depth": {
182
+ "aux": {
183
+ "transmission": torch.arange(4).view(2, 2),
184
+ "attention": None,
185
+ }
186
+ }
187
+ }
188
+ out = build_save_kwargs(slots, ["a", "b"])
189
+ aux = out["depth"]["aux"]
190
+ assert isinstance(aux["transmission"], dict)
191
+ assert set(aux["transmission"].keys()) == {"a", "b"}
192
+ assert aux["attention"] is None
193
+
194
+ def test_no_sample_ids_leaves_tensors_unwrapped(self):
195
+ slots = {"rgb": {"pred": torch.arange(6).view(2, 3)}}
196
+ out = build_save_kwargs(slots, None)
197
+ assert torch.is_tensor(out["rgb"]["pred"])
198
+
199
+
200
+ # ═══════════════════════════════════════════════════════════════════════════
201
+ # save_outputs_from_batch (free function + Run method)
202
+ # ═══════════════════════════════════════════════════════════════════════════
203
+
204
+ def _make_run(tmp_path: Path):
205
+ return euler_train.init(dir=str(tmp_path / "r"), config={})
206
+
207
+
208
+ def _read_manifest(run_dir: Path, epoch: int, step: int) -> dict:
209
+ return json.loads(
210
+ (run_dir / "outputs" / f"epoch_{epoch}_step_{step}" / "manifest.json").read_text()
211
+ )
212
+
213
+
214
+ class TestSaveOutputsFromBatch:
215
+ def test_free_function_wraps_tensors_into_named_files(self, tmp_path):
216
+ run = _make_run(tmp_path)
217
+ batch = {"full_id": ["/scene/img_001", "/scene/img_002"]}
218
+ imgs = torch.randint(0, 255, (2, 3, 8, 8), dtype=torch.uint8)
219
+
220
+ save_outputs_from_batch(
221
+ run, batch=batch, epoch=1, step=10, rgb={"pred": imgs}
222
+ )
223
+
224
+ pred_dir = run.dir / "outputs" / "epoch_1_step_10" / "rgb" / "pred"
225
+ names = sorted(f.name for f in pred_dir.iterdir())
226
+ assert names == ["scene_img_001.png", "scene_img_002.png"]
227
+ run.finish()
228
+
229
+ def test_run_method_matches_free_function(self, tmp_path):
230
+ run = _make_run(tmp_path)
231
+ batch = {"full_id": ["/a", "/b"]}
232
+ imgs = torch.randint(0, 255, (2, 3, 4, 4), dtype=torch.uint8)
233
+
234
+ run.save_outputs_from_batch(
235
+ batch=batch, epoch=0, step=0, rgb={"pred": imgs}
236
+ )
237
+
238
+ pred_dir = run.dir / "outputs" / "epoch_0_step_0" / "rgb" / "pred"
239
+ names = sorted(f.name for f in pred_dir.iterdir())
240
+ assert names == ["a.png", "b.png"]
241
+ run.finish()
242
+
243
+ def test_id_schema_merged_from_dataset(self, tmp_path):
244
+ run = _make_run(tmp_path)
245
+ batch = {"full_id": ["/s/a", "/s/b"]}
246
+ imgs = torch.randint(0, 255, (2, 3, 4, 4), dtype=torch.uint8)
247
+ ds = _StubDataset(
248
+ {"hierarchy_separator": "-", "id_join_char": "+", "full_id_separator": "/"}
249
+ )
250
+
251
+ run.save_outputs_from_batch(
252
+ batch=batch,
253
+ epoch=1,
254
+ step=1,
255
+ metadata={"dataset": "vkitti2"},
256
+ dataset=ds,
257
+ rgb={"pred": imgs},
258
+ )
259
+
260
+ manifest = _read_manifest(run.dir, 1, 1)
261
+ assert manifest["metadata"]["id_schema"]["id_join_char"] == "+"
262
+ assert manifest["metadata"]["id_schema"]["hierarchy_separator"] == "-"
263
+ assert manifest["metadata"]["dataset"] == "vkitti2"
264
+ run.finish()
265
+
266
+ def test_explicit_id_schema_overrides_dataset(self, tmp_path):
267
+ run = _make_run(tmp_path)
268
+ batch = {"full_id": ["/a"]}
269
+ imgs = torch.randint(0, 255, (1, 3, 4, 4), dtype=torch.uint8)
270
+ ds = _StubDataset({"id_join_char": "+"})
271
+
272
+ run.save_outputs_from_batch(
273
+ batch=batch,
274
+ epoch=1,
275
+ step=2,
276
+ metadata={"dataset": "vkitti2"},
277
+ dataset=ds,
278
+ id_schema={"id_join_char": "|", "hierarchy_separator": "-"},
279
+ rgb={"pred": imgs},
280
+ )
281
+
282
+ manifest = _read_manifest(run.dir, 1, 2)
283
+ assert manifest["metadata"]["id_schema"]["id_join_char"] == "|"
284
+ run.finish()
285
+
286
+ def test_existing_metadata_id_schema_wins_over_auto_injection(self, tmp_path):
287
+ run = _make_run(tmp_path)
288
+ batch = {"full_id": ["/a"]}
289
+ imgs = torch.randint(0, 255, (1, 3, 4, 4), dtype=torch.uint8)
290
+ ds = _StubDataset({"id_join_char": "+"})
291
+
292
+ preexisting = {"id_join_char": "preset"}
293
+ run.save_outputs_from_batch(
294
+ batch=batch,
295
+ epoch=1,
296
+ step=3,
297
+ metadata={"dataset": "vkitti2", "id_schema": preexisting},
298
+ dataset=ds,
299
+ rgb={"pred": imgs},
300
+ )
301
+
302
+ manifest = _read_manifest(run.dir, 1, 3)
303
+ assert manifest["metadata"]["id_schema"] == preexisting
304
+ run.finish()
305
+
306
+ def test_include_id_schema_false_skips_injection(self, tmp_path):
307
+ run = _make_run(tmp_path)
308
+ batch = {"full_id": ["/a"]}
309
+ imgs = torch.randint(0, 255, (1, 3, 4, 4), dtype=torch.uint8)
310
+ ds = _StubDataset({"id_join_char": "+"})
311
+
312
+ run.save_outputs_from_batch(
313
+ batch=batch,
314
+ epoch=1,
315
+ step=4,
316
+ metadata={"dataset": "vkitti2"},
317
+ dataset=ds,
318
+ include_id_schema=False,
319
+ rgb={"pred": imgs},
320
+ )
321
+
322
+ manifest = _read_manifest(run.dir, 1, 4)
323
+ assert "id_schema" not in manifest["metadata"]
324
+ run.finish()
325
+
326
+ def test_no_dataset_no_schema_no_injection(self, tmp_path):
327
+ run = _make_run(tmp_path)
328
+ batch = {"full_id": ["/a"]}
329
+ imgs = torch.randint(0, 255, (1, 3, 4, 4), dtype=torch.uint8)
330
+
331
+ run.save_outputs_from_batch(
332
+ batch=batch,
333
+ epoch=1,
334
+ step=5,
335
+ metadata={"dataset": "vkitti2"},
336
+ rgb={"pred": imgs},
337
+ )
338
+
339
+ manifest = _read_manifest(run.dir, 1, 5)
340
+ assert "id_schema" not in manifest["metadata"]
341
+ run.finish()
342
+
343
+ def test_failing_describe_id_schema_is_swallowed(self, tmp_path):
344
+ run = _make_run(tmp_path)
345
+ batch = {"full_id": ["/a"]}
346
+ imgs = torch.randint(0, 255, (1, 3, 4, 4), dtype=torch.uint8)
347
+
348
+ run.save_outputs_from_batch(
349
+ batch=batch,
350
+ epoch=1,
351
+ step=6,
352
+ metadata={"dataset": "vkitti2"},
353
+ dataset=_FailingDataset(),
354
+ rgb={"pred": imgs},
355
+ )
356
+
357
+ manifest = _read_manifest(run.dir, 1, 6)
358
+ assert "id_schema" not in manifest["metadata"]
359
+ run.finish()
360
+
361
+ def test_no_metadata_skips_id_schema_injection(self, tmp_path):
362
+ """When metadata is None, id_schema is not auto-injected.
363
+
364
+ Injection would otherwise build a metadata dict with only
365
+ ``id_schema`` (no ``dataset`` key), which ``save_outputs``
366
+ rejects. The caller must supply ``metadata={"dataset": ...}``
367
+ to receive schema enrichment.
368
+ """
369
+ run = _make_run(tmp_path)
370
+ batch = {"full_id": ["/a"]}
371
+ imgs = torch.randint(0, 255, (1, 3, 4, 4), dtype=torch.uint8)
372
+
373
+ run.save_outputs_from_batch(
374
+ batch=batch,
375
+ epoch=1,
376
+ step=7,
377
+ dataset=_StubDataset({"id_join_char": "+"}),
378
+ rgb={"pred": imgs},
379
+ )
380
+
381
+ pred_dir = run.dir / "outputs" / "epoch_1_step_7" / "rgb" / "pred"
382
+ assert (pred_dir / "a.png").exists()
383
+ manifest = _read_manifest(run.dir, 1, 7)
384
+ assert "metadata" not in manifest
385
+ run.finish()
386
+
387
+ def test_no_ids_in_batch_uses_sequential_indices(self, tmp_path):
388
+ run = _make_run(tmp_path)
389
+ batch = {} # no full_id, no id
390
+ imgs = torch.randint(0, 255, (2, 3, 4, 4), dtype=torch.uint8)
391
+
392
+ run.save_outputs_from_batch(
393
+ batch=batch, epoch=0, step=1, rgb={"pred": imgs}
394
+ )
395
+
396
+ pred_dir = run.dir / "outputs" / "epoch_0_step_1" / "rgb" / "pred"
397
+ names = sorted(f.name for f in pred_dir.iterdir())
398
+ assert names == ["0000.png", "0001.png"]
399
+ run.finish()
400
+
401
+ def test_explicit_sample_ids_override_batch(self, tmp_path):
402
+ run = _make_run(tmp_path)
403
+ batch = {"full_id": ["/a", "/b"]}
404
+ imgs = torch.randint(0, 255, (2, 3, 4, 4), dtype=torch.uint8)
405
+
406
+ run.save_outputs_from_batch(
407
+ batch=batch,
408
+ epoch=0,
409
+ step=2,
410
+ sample_ids=["override_0", "override_1"],
411
+ rgb={"pred": imgs},
412
+ )
413
+
414
+ pred_dir = run.dir / "outputs" / "epoch_0_step_2" / "rgb" / "pred"
415
+ names = sorted(f.name for f in pred_dir.iterdir())
416
+ assert names == ["override_0.png", "override_1.png"]
417
+ run.finish()
418
+
419
+ def test_n_prefix_limits_saved_files(self, tmp_path):
420
+ run = _make_run(tmp_path)
421
+ batch = {"full_id": ["/a", "/b", "/c", "/d"]}
422
+ imgs = torch.randint(0, 255, (4, 3, 4, 4), dtype=torch.uint8)
423
+
424
+ run.save_outputs_from_batch(
425
+ batch=batch, epoch=0, step=3, n=2, rgb={"pred": imgs}
426
+ )
427
+
428
+ pred_dir = run.dir / "outputs" / "epoch_0_step_3" / "rgb" / "pred"
429
+ names = sorted(f.name for f in pred_dir.iterdir())
430
+ assert names == ["a.png", "b.png"]
431
+ run.finish()
432
+
433
+ def test_aux_slot_wraps_to_named_dicts(self, tmp_path):
434
+ run = _make_run(tmp_path)
435
+ batch = {"full_id": ["/a", "/b"]}
436
+ t_map = torch.rand(2, 4, 4)
437
+ depth_pred = torch.rand(2, 4, 4)
438
+
439
+ run.save_outputs_from_batch(
440
+ batch=batch,
441
+ epoch=0,
442
+ step=4,
443
+ depth={"pred": depth_pred, "aux": {"transmission": t_map}},
444
+ )
445
+
446
+ trans_dir = (
447
+ run.dir / "outputs" / "epoch_0_step_4" / "depth" / "aux" / "transmission"
448
+ )
449
+ names = sorted(f.name for f in trans_dir.iterdir())
450
+ # float tensors default to .npy
451
+ assert names == ["a.npy", "b.npy"]
452
+ run.finish()
453
+
454
+ def test_returns_output_directory(self, tmp_path):
455
+ run = _make_run(tmp_path)
456
+ batch = {"full_id": ["/a"]}
457
+ imgs = torch.randint(0, 255, (1, 3, 4, 4), dtype=torch.uint8)
458
+
459
+ result = run.save_outputs_from_batch(
460
+ batch=batch, epoch=2, step=5, rgb={"pred": imgs}
461
+ )
462
+ assert result == run.dir / "outputs" / "epoch_2_step_5"
463
+ run.finish()
@@ -2592,3 +2592,159 @@ class TestMetricNaming:
2592
2592
  assert "gpu_util_pct" in stats
2593
2593
  assert "sys.train.gpu_util_pct" not in stats
2594
2594
  run.finish()
2595
+
2596
+
2597
+ class TestPipeline:
2598
+ def test_pipeline_stored_in_meta_json(self, tmp_path):
2599
+ pipeline = {"attach_id": "pipe-abc-123"}
2600
+ run = euler_train.init(
2601
+ dir=str(tmp_path / "r"),
2602
+ config={"lr": 1e-3},
2603
+ pipeline=pipeline,
2604
+ )
2605
+ meta = _read_json(run.dir / "meta.json")
2606
+ assert meta["pipeline"] == pipeline
2607
+ run.finish()
2608
+
2609
+ def test_pipeline_none_by_default(self, tmp_path):
2610
+ run = euler_train.init(
2611
+ dir=str(tmp_path / "r"),
2612
+ config={},
2613
+ )
2614
+ meta = _read_json(run.dir / "meta.json")
2615
+ assert "pipeline" not in meta
2616
+ run.finish()
2617
+
2618
+ def test_pipeline_with_extra_fields(self, tmp_path):
2619
+ pipeline = {
2620
+ "attach_id": "pipe-xyz",
2621
+ "stage": "training",
2622
+ "pipeline_id": "nightly-run-42",
2623
+ }
2624
+ run = euler_train.init(
2625
+ dir=str(tmp_path / "r"),
2626
+ config={},
2627
+ pipeline=pipeline,
2628
+ )
2629
+ meta = _read_json(run.dir / "meta.json")
2630
+ assert meta["pipeline"]["attach_id"] == "pipe-xyz"
2631
+ assert meta["pipeline"]["stage"] == "training"
2632
+ assert meta["pipeline"]["pipeline_id"] == "nightly-run-42"
2633
+ run.finish()
2634
+
2635
+ def test_pipeline_preserved_on_resume(self, tmp_path):
2636
+ pipeline = {"attach_id": "pipe-resume-test"}
2637
+ run1 = euler_train.init(
2638
+ dir=str(tmp_path / "r"),
2639
+ config={},
2640
+ pipeline=pipeline,
2641
+ )
2642
+ run1.finish()
2643
+
2644
+ run2 = euler_train.init(
2645
+ dir=str(run1.dir),
2646
+ )
2647
+ meta = _read_json(run2.dir / "meta.json")
2648
+ assert meta["pipeline"] == pipeline
2649
+ run2.finish()
2650
+
2651
+ def test_pipeline_in_stream_init_event(self, tmp_path):
2652
+ consumer = _RecordingStreamConsumer()
2653
+ pipeline = {"attach_id": "stream-pipe-id"}
2654
+ run = euler_train.init(
2655
+ dir=str(tmp_path / "r"),
2656
+ config={},
2657
+ stream=consumer,
2658
+ pipeline=pipeline,
2659
+ )
2660
+ run.finish()
2661
+
2662
+ init_event = consumer.events[0]
2663
+ assert init_event["type"] == "init"
2664
+ assert init_event["meta"]["pipeline"] == pipeline
2665
+
2666
+ def test_pipeline_not_in_stream_when_absent(self, tmp_path):
2667
+ consumer = _RecordingStreamConsumer()
2668
+ run = euler_train.init(
2669
+ dir=str(tmp_path / "r"),
2670
+ config={},
2671
+ stream=consumer,
2672
+ )
2673
+ run.finish()
2674
+
2675
+ init_event = consumer.events[0]
2676
+ assert "pipeline" not in init_event["meta"]
2677
+
2678
+ def test_pipeline_validation_not_dict(self, tmp_path):
2679
+ with pytest.raises(TypeError, match="pipeline must be a dict"):
2680
+ euler_train.init(
2681
+ dir=str(tmp_path / "r"),
2682
+ config={},
2683
+ pipeline="invalid",
2684
+ )
2685
+
2686
+ def test_pipeline_validation_missing_attach_id(self, tmp_path):
2687
+ with pytest.raises(ValueError, match="attach_id"):
2688
+ euler_train.init(
2689
+ dir=str(tmp_path / "r"),
2690
+ config={},
2691
+ pipeline={},
2692
+ )
2693
+
2694
+ def test_pipeline_validation_empty_attach_id(self, tmp_path):
2695
+ with pytest.raises(ValueError, match="attach_id"):
2696
+ euler_train.init(
2697
+ dir=str(tmp_path / "r"),
2698
+ config={},
2699
+ pipeline={"attach_id": " "},
2700
+ )
2701
+
2702
+ def test_pipeline_validation_non_string_attach_id(self, tmp_path):
2703
+ with pytest.raises(ValueError, match="attach_id"):
2704
+ euler_train.init(
2705
+ dir=str(tmp_path / "r"),
2706
+ config={},
2707
+ pipeline={"attach_id": 42},
2708
+ )
2709
+
2710
+ def test_pipeline_from_env_var(self, tmp_path, monkeypatch):
2711
+ monkeypatch.setenv("EULER_SESSION_ID", "env-session-99")
2712
+ run = euler_train.init(
2713
+ dir=str(tmp_path / "r"),
2714
+ config={},
2715
+ )
2716
+ meta = _read_json(run.dir / "meta.json")
2717
+ assert meta["pipeline"] == {"attach_id": "env-session-99"}
2718
+ run.finish()
2719
+
2720
+ def test_pipeline_env_var_ignored_when_explicit(self, tmp_path, monkeypatch):
2721
+ monkeypatch.setenv("EULER_SESSION_ID", "env-session-99")
2722
+ explicit = {"attach_id": "explicit-id", "stage": "eval"}
2723
+ run = euler_train.init(
2724
+ dir=str(tmp_path / "r"),
2725
+ config={},
2726
+ pipeline=explicit,
2727
+ )
2728
+ meta = _read_json(run.dir / "meta.json")
2729
+ assert meta["pipeline"] == explicit
2730
+ run.finish()
2731
+
2732
+ def test_pipeline_env_var_empty_ignored(self, tmp_path, monkeypatch):
2733
+ monkeypatch.setenv("EULER_SESSION_ID", " ")
2734
+ run = euler_train.init(
2735
+ dir=str(tmp_path / "r"),
2736
+ config={},
2737
+ )
2738
+ meta = _read_json(run.dir / "meta.json")
2739
+ assert "pipeline" not in meta
2740
+ run.finish()
2741
+
2742
+ def test_pipeline_env_var_unset_ignored(self, tmp_path, monkeypatch):
2743
+ monkeypatch.delenv("EULER_SESSION_ID", raising=False)
2744
+ run = euler_train.init(
2745
+ dir=str(tmp_path / "r"),
2746
+ config={},
2747
+ )
2748
+ meta = _read_json(run.dir / "meta.json")
2749
+ assert "pipeline" not in meta
2750
+ run.finish()
File without changes
File without changes