Skip to content

Commit 993efe6

Browse files
authored
Merge branch 'main' into pipeline_with_processsteps
2 parents fb4f7b4 + 59d325c commit 993efe6

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ stamina
1010
tox
1111
scipy
1212
uncertainties
13+
pyyaml

src/modacor/runner/pipeline.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from graphlib import TopologicalSorter
66
from pathlib import Path
7+
import yaml
78

89
from attrs import define, field
910
from attrs import validators as v
@@ -26,9 +27,25 @@ def __attrs_post_init__(self):
2627
super().__init__(graph=self.graph)
2728

2829
@classmethod
29-
def from_json(cls, path_to_json: Path):
30-
# functionality postponed
31-
return cls(name="dummy")
30+
def from_yaml(cls, path_to_yaml: Path):
31+
"""
32+
Instantiate a Pipeline from a yaml configuration file.
33+
"""
34+
35+
yaml_obj = yaml.safe_load(path_to_yaml)
36+
process_step_instances = {}
37+
id_graph = {}
38+
for module_name, module_data in yaml_obj["steps"].items():
39+
# we need to instantiate ProcessSteps here, but
40+
# need to implement a ProcessStep registry
41+
step_id = module_data.get("step_id")
42+
process_step_instances[step_id] = ProcessStep(io_sources=None)
43+
id_graph[step_id] = module_data.get("requires_steps")
44+
# translate step_id graph into ProcessStep graph
45+
graph = {}
46+
for k, v in id_graph.items():
47+
graph[process_step_instances[k]] = {process_step_instances[i] for i in v}
48+
return cls(name=yaml_obj["name"], graph=graph)
3249

3350
@classmethod
3451
def from_dict(cls, graph_dict: dict, name=""):

src/modacor/tests/test_pipeline.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from pathlib import Path
3+
import yaml
34

45
from ..runner.pipeline import Pipeline
56
from ..dataclasses.process_step import ProcessStep
@@ -23,6 +24,47 @@ class DummyProcessStep:
2324
pass
2425

2526

27+
@pytest.fixture
28+
def yaml_one_step():
29+
return """
30+
name: one_step
31+
steps:
32+
multiply by xy:
33+
module: PoissonUncertainties
34+
step_id: 3
35+
requires_steps: []
36+
configuration:
37+
multiplier: 3
38+
signal: sample::signal
39+
io_sources:
40+
- sample
41+
"""
42+
43+
44+
@pytest.fixture
45+
def yaml_linear_pipeline():
46+
return """
47+
name: simple_pipeline
48+
steps:
49+
poisson:
50+
module: PoissonUncertainties
51+
step_id: 1
52+
another poisson:
53+
module: PoissonUncertainties
54+
step_id: 2
55+
requires_steps: [1]
56+
multiply by xy:
57+
module: PoissonUncertainties
58+
step_id: 3
59+
requires_steps: [2]
60+
configuration:
61+
multiplier: 3
62+
signal: sample::signal
63+
io_sources:
64+
- sample
65+
"""
66+
67+
2668
def test_linear_pipeline(linear_pipeline):
2769
"tests the sequence is expected for a linear graph"
2870
pipeline = Pipeline(graph=linear_pipeline)
@@ -75,3 +117,16 @@ def test_diverging_branch_addition(
75117
pipeline.add_outgoing_branch(branch, branching_node)
76118
assert [*pipeline.static_order()] == [1, 2, 3, 6, 5]
77119
assert pipeline.graph == {3: {2, 1}, 2: {1}, 5: {6}, 6: {2}}
120+
121+
122+
def test_yaml_format(yaml_linear_pipeline):
123+
yaml_obj = yaml.safe_load(yaml_linear_pipeline)
124+
assert "steps" in yaml_obj
125+
assert "poisson" in yaml_obj["steps"]
126+
assert type(yaml_obj["steps"]["multiply by xy"]["configuration"]) == dict
127+
128+
129+
def test_pipeline_from_yaml(yaml_one_step):
130+
pipeline = Pipeline.from_yaml(yaml_one_step)
131+
assert pipeline.name == "one_step"
132+
assert type(pipeline) == Pipeline

0 commit comments

Comments
 (0)