Source code for cardiac_geometries.metadata
"""The metadata of a geometry file, stored inside the file (format 1).
A geometry file is an io4dolfinx file holding the mesh, its tags, the fibres and the fields.
Since format 1 it also holds one attribute group, :data:`GROUP`, with one attribute,
:data:`KEY`: a JSON document whose top-level keys are ``format``, ``unit``, ``markers``,
``microstructure``, ``additional_data`` and ``info``. The document is stored as its UTF-8 bytes
in a ``uint8`` array, since that is the form both the h5py and the adios2 backends store (adios2
refuses a string attribute).
A file without the group is format 0: its metadata, if any, sits in JSON files beside it.
"""
import json
from pathlib import Path
from typing import Any
from mpi4py import MPI
import io4dolfinx
import numpy as np
from . import utils
#: The version of the file standard that this version of cardiac-geometriesx writes, and the
#: highest it reads.
FORMAT: int = 1
#: The attribute group holding the metadata.
GROUP = "cardiac_geometries"
#: The attribute holding the metadata in :data:`GROUP`.
KEY = "metadata"
#: The units a geometry's coordinates may be in.
UNITS = ("m", "cm", "mm")
[docs]
def encode(metadata: dict[str, Any]) -> np.ndarray:
"""The JSON document `metadata` as a ``uint8`` array of its UTF-8 bytes."""
return np.frombuffer(json.dumps(metadata, default=utils.json_serial).encode(), np.uint8).copy()
[docs]
def decode(array: np.ndarray) -> dict[str, Any]:
"""The JSON document stored by :func:`encode` in `array`."""
return json.loads(np.asarray(array, dtype=np.uint8).tobytes().decode("utf-8"))