File size: 2,297 Bytes
484b847 |
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import importlib
from typing import Callable
import typeguard
from hydra.utils import instantiate
from jaxtyping import Float, jaxtyped
from omegaconf import DictConfig
from pdeinvbench.utils.types import (
HIGH_RESOLUTION_PDE_SPATIAL_SIZE,
PDE,
PDE_SPATIAL_SIZE,
)
@jaxtyped(typechecker=typeguard.typechecked)
def get_function_from_string(string: str) -> Callable:
"""
Converts a function specified as a string to an actual function object. Used in hydra configs.
"""
module_name, function_name = string.rsplit(".", 1)
# Import the module dynamically
module = importlib.import_module(module_name)
# Get the function object
function = getattr(module, function_name)
return function
def resolve_pde_resolution(cfg: DictConfig) -> None:
"""
Simple utility method which checks if we are using the high resolution data.
If we are, it updates the types::PDE_SPATIAL_SIZE dict. Currently only
works with the inverse setting. Assumes keys cfg:high_resolution[bool] and
cfg.data.downsample_factor[int] exist.
"""
assert "high_resolution" in cfg, "No key 'high_resolution' found in hydra config"
assert (
"data" in cfg and "downsample_factor" in cfg.data
), "No key 'data' or 'data.downsample_factor' found in hydra config"
high_resolution: bool = cfg.high_resolution
downsample_factor: int = cfg.data.downsample_factor
pde: PDE = instantiate(cfg.data.pde)
if high_resolution:
assert (
pde in HIGH_RESOLUTION_PDE_SPATIAL_SIZE
), f"Could not find {pde} in high resolution PDE size mapping."
resolution: list[int] = (
HIGH_RESOLUTION_PDE_SPATIAL_SIZE[pde]
if high_resolution
else PDE_SPATIAL_SIZE[pde]
)
if (
downsample_factor == 0
): # Ensures that dynamic setting works without downsampling
downsample_factor = 1
new_resolution: list[float] = [res / downsample_factor for res in resolution]
# only allow downsampling to an integer factor
for res in new_resolution:
assert (
int(res) == res
), f"Downsample factor leads to non-integer resolution {res}"
new_resolution: list[int] = [int(res) for res in new_resolution]
PDE_SPATIAL_SIZE[pde] = new_resolution
|