example_runner
ExampleRunner is a minimal Runner implementation that executes a tiny example calculation, reads a numeric output from a file, and returns a simple result dictionary.
Overview
-
Performs a single short Python one-liner that sums two parameters and appends the numeric result to "
/output.txt". -
Supports three parameter modes (0, 1, 2); each mode expects two parameter keys and returns a single primary numeric output under "output_1", "output_2", or "output_3" respectively, together with a boolean "success" flag and optional diagnostic strings.
-
Intended as a lightweight example and template for implementing real runners.
ExampleRunner
ExampleRunner(*args, **kwargs)
Bases: Runner
Initialization parameters (via kwargs)
-
sleep_sec (number or two-element iterable, default 0.01): controls the pause after executing the example command. See Sleep sec behavior below.
-
fail_prob (float in [0, 1], default 0): probability that the run raises a synthetic RuntimeError after successful execution. This is useful for testing failure handling in distributed schedulers and pipelines. The failure is raised only after writing and reading the output file and after sleeping for the computed sleep duration. When fail_prob == 0 no synthetic failures are triggered.
Sleep sec behavior
-
Fixed sleep: pass a number (int, float, numpy scalar). The runner will sleep that many seconds after execution.
-
Random sleep: pass a two-element iterable [low, high] (list, tuple, numpy array). Each run will draw a single sample from a uniform distribution on [low, high] and sleep that many seconds.
-
Validation: the implementation expects exactly two bounds for random sleep; if a provided iterable has length not equal to two the runner should raise ValueError. Ensure bounds are numeric and optionally enforce low <= high.
-
Usage: call sleep(self.get_sleep_sec()) so the numeric duration returned by get_sleep_sec is passed to time.sleep.
single_code_run behavior and return contract
-
Writes to and reads from "
/output.txt"; repeated runs append unless the file is cleared by the caller. -
Converts the file contents to float and places that value in the primary output key. Non-numeric or multi-line contents will raise an exception.
-
Returned dictionary values should be non-iterable base types (int, float, str, bool) so they integrate cleanly with dataset creation and surrogate tooling.
Errors and edge cases - Raises ValueError for invalid random sleep bounds (not exactly two values or low > high).
-
Raises TypeError if sleep_sec is neither a number nor a two-element iterable.
-
If fail_prob is set in (0,1], a RuntimeError may be raised after a successful run to simulate stochastic failures; the probability of raising is equal to fail_prob.
Source code in src/enchanted_surrogates/runners/example_runner.py
97 98 99 | |
get_sleep_sec
get_sleep_sec()
Return a numeric sleep duration (seconds) derived from self.sleep_sec.
Behavior - If self.sleep_sec is a numeric value (int, float, numpy scalar), return that value cast to float. - If self.sleep_sec is an iterable of exactly two numeric values, interpret them as lower and upper bounds and return a single sample drawn from the uniform distribution on [low, high]. - If self.sleep_sec is an iterable with length not equal to two, raise ValueError. - If self.sleep_sec is neither a number nor a valid two-element iterable, raise TypeError.
Parameters - self.sleep_sec: either a number (fixed sleep seconds) or a two-element iterable [low, high] specifying uniform random bounds.
Returns - float: sleep duration in seconds suitable for time.sleep.
Raises - ValueError: when a two-element iterable is required but self.sleep_sec has a different length or when low > high. - TypeError: when self.sleep_sec is not a number and not an iterable of two numbers.
Examples - self.sleep_sec = 0.2 -> returns 0.2 - self.sleep_sec = (0.1, 0.5) -> returns a float uniformly sampled between 0.1 and 0.5
Source code in src/enchanted_surrogates/runners/example_runner.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
single_code_run
single_code_run(run_dir, params=None)
Execute a single example run in run_dir and
return a simple results dictionary suitable for dataset creation and active
learning workflows.
Behavior
- Writes the numeric result of a short Python one-liner to "self.get_sleep_sec() after execution.
- Reads the numeric output back from the file, converts it to float, and returns
a dict containing a primary output key, a boolean "success" flag, and
optional diagnostic strings.
Parameters - run_dir (str): Directory path where "output.txt" will be created and read. - params (dict, optional): Parameter mapping.
Returns - dict: At minimum contains: - "output" (float): primary numeric result. - "success" (bool): True if the run produced an output.
Notes and constraints - The method appends to "output.txt"; repeated runs will append unless the caller clears the file beforehand. - The method converts the full contents of "output.txt" to float; non-numeric or multi-line contents will raise an exception during conversion. - Returned values should be non-iterable base types (int, float, str, bool) so they are compatible with dataset and surrogate tooling. - If fail_prob > 0, a RuntimeError may be raised after successful execution with probability equal to fail_prob to simulate stochastic failures for testing.
Source code in src/enchanted_surrogates/runners/example_runner.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |