|
1 | | -import importlib |
2 | | -import json |
3 | 1 | import logging |
4 | 2 |
|
5 | 3 | import numpy.typing as npt |
6 | | -import tomli |
7 | | -import yaml |
8 | 4 |
|
9 | 5 | from libensemble.executors import Executor |
10 | 6 | from libensemble.libE import libE |
@@ -117,110 +113,6 @@ class Ensemble: |
117 | 113 | experiment = Ensemble() |
118 | 114 | experiment.sim_specs = sim_specs |
119 | 115 |
|
120 | | - .. dropdown:: Option 3: Loading parameters from files |
121 | | -
|
122 | | - .. code-block:: python |
123 | | - :linenos: |
124 | | -
|
125 | | - from libensemble import Ensemble |
126 | | -
|
127 | | - experiment = Ensemble() |
128 | | -
|
129 | | - my_experiment.from_yaml("my_parameters.yaml") |
130 | | - # or... |
131 | | - my_experiment.from_toml("my_parameters.toml") |
132 | | - # or... |
133 | | - my_experiment.from_json("my_parameters.json") |
134 | | -
|
135 | | - .. tab-set:: |
136 | | -
|
137 | | - .. tab-item:: my_parameters.yaml |
138 | | -
|
139 | | - .. code-block:: yaml |
140 | | - :linenos: |
141 | | -
|
142 | | - libE_specs: |
143 | | - save_every_k_gens: 20 |
144 | | -
|
145 | | - exit_criteria: |
146 | | - sim_max: 80 |
147 | | -
|
148 | | - gen_specs: |
149 | | - gen_f: generator.gen_random_sample |
150 | | - outputs: |
151 | | - x: |
152 | | - type: float |
153 | | - size: 1 |
154 | | - user: |
155 | | - gen_batch_size: 5 |
156 | | -
|
157 | | - sim_specs: |
158 | | - sim_f: simulator.sim_find_sine |
159 | | - inputs: |
160 | | - - x |
161 | | - outputs: |
162 | | - y: |
163 | | - type: float |
164 | | -
|
165 | | - .. tab-item:: my_parameters.toml |
166 | | -
|
167 | | - .. code-block:: toml |
168 | | - :linenos: |
169 | | -
|
170 | | - [libE_specs] |
171 | | - save_every_k_gens = 300 |
172 | | -
|
173 | | - [exit_criteria] |
174 | | - sim_max = 80 |
175 | | -
|
176 | | - [gen_specs] |
177 | | - gen_f = "generator.gen_random_sample" |
178 | | - [gen_specs.outputs] |
179 | | - [gen_specs.outputs.x] |
180 | | - type = "float" |
181 | | - size = 1 |
182 | | - [gen_specs.user] |
183 | | - gen_batch_size = 5 |
184 | | -
|
185 | | - [sim_specs] |
186 | | - sim_f = "simulator.sim_find_sine" |
187 | | - inputs = ["x"] |
188 | | - [sim_specs.outputs] |
189 | | - [sim_specs.outputs.y] |
190 | | - type = "float" |
191 | | -
|
192 | | - .. tab-item:: my_parameters.json |
193 | | -
|
194 | | - .. code-block:: json |
195 | | - :linenos: |
196 | | -
|
197 | | - { |
198 | | - "libE_specs": { |
199 | | - "save_every_k_gens": 300, |
200 | | - }, |
201 | | - "exit_criteria": { |
202 | | - "sim_max": 80 |
203 | | - }, |
204 | | - "gen_specs": { |
205 | | - "gen_f": "generator.gen_random_sample", |
206 | | - "outputs": { |
207 | | - "x": { |
208 | | - "type": "float", |
209 | | - "size": 1 |
210 | | - } |
211 | | - }, |
212 | | - "user": { |
213 | | - "gen_batch_size": 5 |
214 | | - } |
215 | | - }, |
216 | | - "sim_specs": { |
217 | | - "sim_f": "simulator.sim_find_sine", |
218 | | - "inputs": ["x"], |
219 | | - "outputs": { |
220 | | - "f": {"type": "float"} |
221 | | - } |
222 | | - } |
223 | | - } |
224 | 116 |
|
225 | 117 | Parameters |
226 | 118 | ---------- |
@@ -431,111 +323,6 @@ def nworkers(self, value): |
431 | 323 | if self._libE_specs: |
432 | 324 | self._libE_specs.nworkers = value |
433 | 325 |
|
434 | | - def _get_func(self, loaded): |
435 | | - """Extracts user function specified in loaded dict""" |
436 | | - func_path_split = loaded.rsplit(".", 1) |
437 | | - func_name = func_path_split[-1] |
438 | | - try: |
439 | | - return getattr(importlib.import_module(func_path_split[0]), func_name) |
440 | | - except AttributeError: |
441 | | - self._util_logger.manager_warning(ATTR_ERR_MSG.format(func_name)) |
442 | | - raise |
443 | | - except ModuleNotFoundError: |
444 | | - self._util_logger.manager_warning(NOTFOUND_ERR_MSG.format(func_name)) |
445 | | - raise |
446 | | - |
447 | | - @staticmethod |
448 | | - def _get_outputs(loaded): |
449 | | - """Extracts output parameters from loaded dict""" |
450 | | - if not loaded: |
451 | | - return [] |
452 | | - fields = [i for i in loaded] |
453 | | - field_params = [i for i in loaded.values()] |
454 | | - results = [] |
455 | | - for i in range(len(fields)): |
456 | | - field_type = field_params[i]["type"] |
457 | | - built_in_type = __builtins__.get(field_type, field_type) |
458 | | - try: |
459 | | - if field_params[i]["size"] == 1: |
460 | | - size = (1,) # formatting how size=1 is typically preferred |
461 | | - else: |
462 | | - size = field_params[i]["size"] |
463 | | - results.append((fields[i], built_in_type, size)) |
464 | | - except KeyError: |
465 | | - results.append((fields[i], built_in_type)) |
466 | | - return results |
467 | | - |
468 | | - @staticmethod |
469 | | - def _get_normal(loaded): |
470 | | - return loaded |
471 | | - |
472 | | - def _get_option(self, specs, name): |
473 | | - """Gets a specs value, underlying spec is either a dict or a class""" |
474 | | - attr = getattr(self, specs) |
475 | | - if isinstance(attr, dict): |
476 | | - return attr.get(name) |
477 | | - else: |
478 | | - return getattr(attr, name) |
479 | | - |
480 | | - def _parse_spec(self, loaded_spec): |
481 | | - """Parses and creates traditional libEnsemble dictionary from loaded dict info""" |
482 | | - |
483 | | - field_f = { |
484 | | - "sim_f": self._get_func, |
485 | | - "gen_f": self._get_func, |
486 | | - "alloc_f": self._get_func, |
487 | | - "inputs": self._get_normal, |
488 | | - "persis_in": self._get_normal, |
489 | | - "outputs": self._get_outputs, |
490 | | - "globus_compute_endpoint": self._get_normal, |
491 | | - "user": self._get_normal, |
492 | | - } |
493 | | - |
494 | | - userf_fields = [f for f in loaded_spec if f in field_f.keys()] |
495 | | - |
496 | | - if len(userf_fields): |
497 | | - for f in userf_fields: |
498 | | - loaded_spec[f] = field_f[f](loaded_spec[f]) |
499 | | - |
500 | | - return loaded_spec |
501 | | - |
502 | | - def _parameterize(self, loaded): |
503 | | - """Updates and sets attributes from specs loaded from file""" |
504 | | - for f in loaded: |
505 | | - loaded_spec = self._parse_spec(loaded[f]) |
506 | | - old_spec = getattr(self, f) |
507 | | - ClassType = CORRESPONDING_CLASSES[f] |
508 | | - if isinstance(old_spec, dict): |
509 | | - old_spec.update(loaded_spec) |
510 | | - if old_spec.get("in") and old_spec.get("inputs"): |
511 | | - old_spec.pop("inputs") # avoid clashes |
512 | | - elif old_spec.get("out") and old_spec.get("outputs"): |
513 | | - old_spec.pop("outputs") # avoid clashes |
514 | | - setattr(self, f, ClassType(**old_spec)) |
515 | | - else: # None. attribute not set yet |
516 | | - setattr(self, f, ClassType(**loaded_spec)) |
517 | | - |
518 | | - def from_yaml(self, file_path: str): |
519 | | - """Parameterizes libEnsemble from ``yaml`` file""" |
520 | | - with open(file_path, "r") as f: |
521 | | - loaded = yaml.full_load(f) |
522 | | - |
523 | | - self._parameterize(loaded) |
524 | | - |
525 | | - def from_toml(self, file_path: str): |
526 | | - """Parameterizes libEnsemble from ``toml`` file""" |
527 | | - with open(file_path, "rb") as f: |
528 | | - loaded = tomli.load(f) |
529 | | - |
530 | | - self._parameterize(loaded) |
531 | | - |
532 | | - def from_json(self, file_path: str): |
533 | | - """Parameterizes libEnsemble from ``json`` file""" |
534 | | - with open(file_path, "rb") as f: |
535 | | - loaded = json.load(f) |
536 | | - |
537 | | - self._parameterize(loaded) |
538 | | - |
539 | 326 | def add_random_streams(self, num_streams: int = 0, seed: str = ""): |
540 | 327 | """ |
541 | 328 |
|
@@ -588,3 +375,11 @@ def save_output(self, basename: str, append_attrs: bool = True): |
588 | 375 | ) |
589 | 376 | else: |
590 | 377 | save_libE_output(self.H, self.persis_info, basename, self.nworkers, append_attrs=append_attrs) |
| 378 | + |
| 379 | + def _get_option(self, specs, name): |
| 380 | + """Gets a specs value, underlying spec is either a dict or a class""" |
| 381 | + attr = getattr(self, specs) |
| 382 | + if isinstance(attr, dict): |
| 383 | + return attr.get(name) |
| 384 | + else: |
| 385 | + return getattr(attr, name) |
0 commit comments