-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
44 lines (31 loc) · 1.47 KB
/
__init__.py
File metadata and controls
44 lines (31 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Auto-discover OpenMC model builders for benchmarks."""
from __future__ import annotations
import importlib
import pkgutil
from typing import Callable, Dict, Optional, Tuple
import openmc
ModelBuilder = Callable[[], openmc.Model]
CustomMetrics = Optional[Dict[str, Callable]]
ModelSpec = Tuple[str, ModelBuilder, Optional[Tuple[int, ...]], Optional[Tuple[Optional[int], ...]], CustomMetrics]
# Mapping of module name -> ModelSpec
MODEL_REGISTRY: Dict[str, ModelSpec] = {}
def _default_benchmark_name(module_name: str) -> str:
parts = module_name.split("_")
return "".join(part.capitalize() for part in parts)
def _discover() -> None:
package = __name__
for module_info in pkgutil.iter_modules(__path__):
if module_info.name.startswith("_"):
continue
module = importlib.import_module(f"{package}.{module_info.name}")
builder = getattr(module, "build_model", None)
if builder is None:
continue
benchmark_name = getattr(module, "BENCHMARK_NAME", _default_benchmark_name(module_info.name))
configs = getattr(module, "CONFIGS", None)
custom_metrics = getattr(module, "CUSTOM_METRICS", None)
MODEL_REGISTRY[module_info.name] = (benchmark_name, builder, configs, custom_metrics)
_discover()
for _module, (_benchmark_name, builder, _configs, _custom) in MODEL_REGISTRY.items():
globals()[_module] = builder
__all__ = ['MODEL_REGISTRY', 'ModelBuilder', 'ModelSpec'] + sorted(MODEL_REGISTRY)