Skip to content

Commit c249812

Browse files
authored
Merge pull request #48 from DiamondLightSource/yamlsweepgenerator
Yaml piepeline generator for sweeps
2 parents 9057795 + 82017a5 commit c249812

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
- method: standard_tomo
2+
module_path: httomo.data.hdf.loaders
3+
- method: normalize
4+
module_path: httomolibgpu.prep.normalize
5+
- method: FBP
6+
module_path: httomolibgpu.recon.algorithm
7+
sweep_parameter: center
8+
sweep_start: 1100
9+
sweep_stop: 1300
10+
sweep_step: 25
11+

httomo_backends/scripts/yaml_pipelines_generator.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Created Date: 22/January/2025
2020
# version ='0.1'
2121
# ---------------------------------------------------------------------------
22-
"""Script that generates YAML pipeline for HTTomo using YAML templates from httomo-backends
22+
"""Script that generates YAML pipeline for HTTomo using YAML templates from httomo-backends
2323
(should be already installed in your environment).
2424
2525
Please run the generator as:
@@ -29,10 +29,32 @@
2929
import os
3030
import ruamel.yaml
3131
import httomo_backends
32+
import yaml
3233

3334
CS = ruamel.yaml.comments.CommentedSeq # defaults to block style
3435

3536

37+
class SweepRange:
38+
"""SweepRange class."""
39+
40+
def __init__(self, start, stop, step):
41+
self._start, self._stop, self._step = start, stop, step
42+
43+
44+
def __sweeprange_representer(
45+
dumper: yaml.SafeDumper, swp: SweepRange
46+
) -> yaml.nodes.MappingNode:
47+
"""Represent a sweeprange as a YAML mapping node."""
48+
return dumper.represent_mapping(
49+
"!SweepRange",
50+
{
51+
"start": swp._start,
52+
"stop": swp._stop,
53+
"step": swp._step,
54+
},
55+
)
56+
57+
3658
def __represent_none(self, data):
3759
return self.represent_scalar("tag:yaml.org,2002:null", "null")
3860

@@ -64,10 +86,18 @@ def yaml_pipelines_generator(
6486
# a loop over methods in the high-level pipeline file (directive)
6587
methods_no = len(pipeline_file_content)
6688
pipeline_full = CS()
89+
sweep_enabled = False
6790
for i in range(methods_no):
6891
method_content = pipeline_file_content[i]
6992
method_name = method_content["method"]
7093
module_name = method_content["module_path"]
94+
if "sweep_parameter" in method_content:
95+
sweep_parameter = method_content["sweep_parameter"]
96+
sweep_start = method_content["sweep_start"]
97+
sweep_stop = method_content["sweep_stop"]
98+
sweep_step = method_content["sweep_step"]
99+
sweep_enabled = True
100+
71101
# get the corresponding yaml template from httomo-backends
72102
backend_name = module_name[0 : module_name.find(".")]
73103
full_path_to_yamls = (
@@ -116,15 +146,15 @@ def yaml_pipelines_generator(
116146
pipeline_full[i]["parameters"].yaml_add_eol_comment(
117147
key="side",
118148
comment="'None' corresponds to fully automated determination, '0' to the left side, '1' to the right side.",
119-
)
149+
)
120150
pipeline_full[i]["side_outputs"].yaml_add_eol_comment(
121151
key="cor",
122152
comment="A side output of the method, here a CoR scalar value",
123153
)
124154
pipeline_full[i]["side_outputs"].yaml_add_eol_comment(
125155
key="overlap",
126156
comment="An overlap to use for converting 360 degrees scan to 180 degrees scan.",
127-
)
157+
)
128158
elif "corr" in module_name and "remove_outlier" in method_name:
129159
pipeline_full.yaml_set_comment_before_after_key(
130160
i,
@@ -160,7 +190,7 @@ def yaml_pipelines_generator(
160190
pipeline_full[i]["parameters"].yaml_add_eol_comment(
161191
key="rotation",
162192
comment="'left' if rotation center is close to the left of the field-of-view, 'right' otherwise.",
163-
)
193+
)
164194
elif "normalize" in module_name:
165195
pipeline_full.yaml_set_comment_before_after_key(
166196
i,
@@ -182,7 +212,7 @@ def yaml_pipelines_generator(
182212
pipeline_full[i]["parameters"].yaml_add_eol_comment(
183213
key="alpha",
184214
comment="Controls the balance between the strength of the filter and the amount of noise reduction. Higher leads to less noise and more blur.",
185-
)
215+
)
186216
elif "stripe" in module_name:
187217
pipeline_full.yaml_set_comment_before_after_key(
188218
i,
@@ -208,7 +238,7 @@ def yaml_pipelines_generator(
208238
pipeline_full[i]["parameters"].yaml_add_eol_comment(
209239
key="neglog",
210240
comment="Perform negative log here if it was previously switched off.",
211-
)
241+
)
212242
if "algorithm" in pipeline_full[i]["parameters"]:
213243
# fix for a default parameter (None) in TomoPy's algorithm
214244
pipeline_full[i]["parameters"]["algorithm"] = "gridrec"
@@ -256,7 +286,13 @@ def yaml_pipelines_generator(
256286
)
257287
pipeline_full += yaml_template_method
258288

289+
if sweep_enabled:
290+
pipeline_full[i]["parameters"][sweep_parameter] = SweepRange(
291+
start=sweep_start, stop=sweep_stop, step=sweep_step
292+
)
293+
259294
yaml.representer.add_representer(type(None), __represent_none)
295+
yaml.representer.add_representer(SweepRange, __sweeprange_representer)
260296
yaml.dump(pipeline_full, f)
261297

262298
return 0

0 commit comments

Comments
 (0)