| 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) |
| |
| module = importlib.import_module(module_name) |
| |
| 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 |
| ): |
| downsample_factor = 1 |
| new_resolution: list[float] = [res / downsample_factor for res in resolution] |
| |
| 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 |
|
|