Skip to content

plaid.containers.utils

plaid.containers.utils

Utility functions for PLAID containers.

plaid.containers.utils.get_sample_ids

get_sample_ids(savedir)

Return list of sample ids from a dataset on disk.

Parameters:

  • savedir (Union[str, Path]) –

    The path to the directory where sample files are stored.

Returns:

  • list[int]

    list[int]: List of sample ids.

Source code in plaid/containers/utils.py
def get_sample_ids(savedir: Union[str, Path]) -> list[int]:
    """Return list of sample ids from a dataset on disk.

    Args:
        savedir (Union[str,Path]): The path to the directory where sample files are stored.

    Returns:
        list[int]: List of sample ids.
    """
    savedir = Path(savedir)
    return sorted(
        [int(d.stem.split("_")[-1]) for d in (savedir).glob("sample_*") if d.is_dir()]
    )

plaid.containers.utils.get_number_of_samples

get_number_of_samples(savedir)

Return number of samples in a dataset on disk.

Parameters:

  • savedir (Union[str, Path]) –

    The path to the directory where sample files are stored.

Returns:

  • int ( int ) –

    number of samples.

Source code in plaid/containers/utils.py
def get_number_of_samples(savedir: Union[str, Path]) -> int:
    """Return number of samples in a dataset on disk.

    Args:
        savedir (Union[str,Path]): The path to the directory where sample files are stored.

    Returns:
        int: number of samples.
    """
    return len(get_sample_ids(savedir))

plaid.containers.utils.get_feature_details_from_path

get_feature_details_from_path(path)

Retrieve semantic details from a CGNS-style path.

Source code in plaid/containers/utils.py
def get_feature_details_from_path(path: str) -> dict[str, str]:
    """Retrieve semantic details from a CGNS-style path."""
    split_path = path.split("/")
    feat: dict[str, str] = {}

    # ----------------------
    # Global
    # ----------------------
    if split_path[0] == "Global" or split_path[0] == "Global_times":
        feat["type"] = "global"

        if len(split_path) == 1:
            feat["sub_type"] = "root"

        elif split_path[1] == "Time":
            feat["sub_type"] = "time"
            if len(split_path) == 3:
                feat["name"] = split_path[2]

        else:
            feat["sub_type"] = "scalar"
            feat["name"] = split_path[-1]

        return feat

    # ----------------------
    # CGNS library version
    # ----------------------
    if path == "CGNSLibraryVersion":
        return {
            "type": "cgns",
            "sub_type": "library_version",
        }

    # ----------------------
    # Base / Zone
    # ----------------------
    feat["base"] = split_path[0]
    assert feat["base"].startswith("Base_"), "path not recognized"

    if len(split_path) == 1:
        feat["type"] = "base"
        return feat

    feat["zone"] = split_path[1]

    if len(split_path) == 2:
        feat["type"] = "zone"
        return feat

    node = split_path[2]

    # ----------------------
    # Grid coordinates
    # ----------------------
    if node == "GridCoordinates" and len(split_path) >= 3 and len(split_path) <= 4:
        feat["type"] = "coordinate"
        feat["sub_type"] = "node"
        if len(split_path) == 4:
            feat["name"] = split_path[3]
        return feat

    # ----------------------
    # Elements
    # ----------------------
    if node.startswith("Elements_") and len(split_path) == 4:
        feat["type"] = "elements"
        feat["element_type"] = node[len("Elements_") :]

        leaf = split_path[3]
        if leaf == "ElementConnectivity":
            feat["sub_type"] = "connectivity"
        elif leaf == "ElementRange":
            feat["sub_type"] = "range"

        return feat

    # ----------------------
    # Boundary conditions
    # ----------------------
    if node == "ZoneBC" and len(split_path) >= 4:
        feat["type"] = "boundary_condition"
        feat["name"] = split_path[3]

        if len(split_path) == 4:
            feat["sub_type"] = "bc"
        elif len(split_path) == 5:
            feat["sub_type"] = split_path[4]  # PointList or GridLocation

        return feat

    # ----------------------
    # Fields (generic location)
    # ----------------------
    # Base_3_3/Zone/VertexFields/RU1:
    if (
        node in path_to_location
        and len(split_path) == 4
        and split_path[3] != "GridLocation"
    ):
        feat["type"] = "field"
        feat["location"] = path_to_location[node]
        feat["name"] = split_path[3]
        feat["name"] = split_path[3]

        return feat
    # ----------------------
    # special case for Integration point fields
    # ----------------------
    # Base_3_3/Zone/cucum1_IntegrationPointFields/cucum1
    if len(split_path) == 4 and split_path[2].endswith("IntegrationPointFields"):
        feat["type"] = "field"
        feat["location"] = "IntegrationPoint"
        feat["name"] = split_path[3]
        return feat

    # ----------------------
    # Fallback
    # ----------------------
    feat["type"] = "other"
    feat["path"] = path
    return feat