plaid.utils.cgns_helper¶
plaid.utils.cgns_helper
¶
Utility functions for working with CGNS trees and nodes.
plaid.utils.cgns_helper.get_base_names
¶
Get a list of base names from a CGNSTree.
Parameters:
-
tree(CGNSTree) –The CGNSTree containing the CGNSBase_t nodes.
-
full_path(bool, default:False) –If True, return full base paths including '/' separators. Defaults to False.
-
unique(bool, default:False) –If True, return unique base names. Defaults to False.
Returns:
-
list[str]–list[str]: A list of base names.
Source code in plaid/utils/cgns_helper.py
plaid.utils.cgns_helper.get_time_values
¶
Get consistent time values from CGNSBase_t nodes in a CGNSTree.
Parameters:
-
tree(CGNSTree) –The CGNSTree containing CGNSBase_t nodes.
Returns:
-
float–np.ndarray: An array of consistent time values.
Raises:
-
AssertionError–If the time values across bases are not consistent.
Source code in plaid/utils/cgns_helper.py
plaid.utils.cgns_helper.show_cgns_tree
¶
Pretty print for CGNS Tree.
Parameters:
-
pyTree(CGNSTree) –CGNS tree to print
-
pre(str, default:'') –indentation of print. Defaults to ''.
Source code in plaid/utils/cgns_helper.py
plaid.utils.cgns_helper.fix_cgns_tree_types
¶
Recursively fix the data types of a CGNS tree node and its children.
This function ensures that data arrays match the expected CGNS types
- "IndexArray_t": converted to integer arrays and stacked
- "Zone_t": stacked as numpy arrays
- "Elements_t", "CGNSBase_t", "BaseIterativeData_t": converted to integer arrays
Parameters:
-
tree(CGNSTree) –A CGNS tree of the form [name: str, data: Any, children: List[CGNSTree], cgns_type: str].
Returns:
-
CGNSTree(CGNSTree) –A new CGNS tree node with corrected data types and recursively
-
CGNSTree–fixed children.
Example
node = ["Zone1", [[1, 2], [3, 4]], [], "Zone_t"] fixed_node = fix_cgns_tree_types(node) fixed_node[1].shape (2, 2)
Source code in plaid/utils/cgns_helper.py
plaid.utils.cgns_helper.compare_cgns_trees
¶
Recursively compare two CGNS trees for exact equality, ignoring the order of children.
This function checks
- Node names
- Node data (numpy arrays or scalars) with exact dtype and values
- Number and names of children nodes
- CGNS type (stored as the extra field)
It prints informative messages whenever a mismatch is found, including the path in the tree where the mismatch occurs.
Parameters:
-
tree1(CGNSTree) –The first CGNS tree node to compare.
-
tree2(CGNSTree) –The second CGNS tree node to compare.
-
path(str, default:'CGNSTree') –The current path in the tree for error messages. Defaults to "CGNSTree".
Returns:
-
bool(bool) –True if the trees are identical (including node names, data, types, and children), False otherwise.
Example
identical = compare_cgns_trees(tree1, tree2) if identical: print("The trees are identical") else: print("The trees differ")
Source code in plaid/utils/cgns_helper.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
plaid.utils.cgns_helper.compare_leaves
¶
Compare two leaf values in a CGNS tree or flattened structure, handling arrays and scalars.
This function supports
- NumPy arrays, including byte arrays (converted to str)
- Floating-point arrays or scalars (compared with tolerance)
- Integer arrays or scalars (exact comparison)
- Strings and None
Parameters:
-
d1(Any) –First value to compare (scalar or np.ndarray).
-
d2(Any) –Second value to compare (scalar or np.ndarray).
Returns:
-
bool(bool) –True if the values are considered equal, False otherwise.
Note
- Floating-point comparisons use
np.allcloseornp.isclosewithrtol=1e-7andatol=0. - Byte arrays (
dtype.kind == "S") are converted to string before comparison.
Examples:
>>> compare_leaves(np.array([1.0, 2.0]), np.array([1.0, 2.0]))
True
>>> compare_leaves(3.0, 3.00000001)
True
>>> compare_leaves(np.array([1, 2]), np.array([2, 1]))
False
Source code in plaid/utils/cgns_helper.py
plaid.utils.cgns_helper.compare_cgns_trees_no_types
¶
Recursively compare two CGNS trees ignoring the order of children and relaxing strict type checks.
This function is useful for heterogeneous or nested CGNS samples,
such as those encountered in Hugging Face Arrow datasets. It compares:
- Node names
- Node data using compare_leaves (supports arrays, scalars, strings)
- CGNS type (extra field)
- Children nodes by name, ignoring their order
Parameters:
-
tree1(CGNSTree) –The first CGNS tree node to compare.
-
tree2(CGNSTree) –The second CGNS tree node to compare.
-
path(str, default:'CGNSTree') –Path for error reporting. Defaults to "CGNSTree".
Returns:
-
bool(bool) –True if the trees are considered equivalent, False otherwise.
Example
identical = compare_cgns_trees_no_types(tree1, tree2) if identical: print("The trees match ignoring types") else: print("The trees differ")
Source code in plaid/utils/cgns_helper.py
plaid.utils.cgns_helper.summarize_cgns_tree
¶
Provide a summary of a CGNS tree's contents.
Parameters:
-
pyTree(CGNSTree) –The CGNS tree to summarize.
-
verbose(bool, default:True) –If True, include detailed field information. Defaults to True.
Example
summarize_cgns_tree(pyTree) Number of Bases: 2 Number of Zones: 5 Number of Nodes: 20 Number of Elements: 10 Number of Fields: 8
Fields: 'Base1/Zone1/Solution1/Field1' 'Base1/Zone1/Solution1/Field2' 'Base2/Zone2/Solution2/Field1' ...
Source code in plaid/utils/cgns_helper.py
plaid.utils.cgns_helper.flatten_cgns_tree
¶
Flatten a CGNS tree into dictionaries of primitives.
Parameters:
-
pyTree(CGNSTree) –The CGNS tree to flatten.
Returns:
-
tuple[dict[str, object], dict[str, str]]–tuple[dict[str, object], dict[str, str]]: - flat: dict of paths to primitive values - cgns_types: dict of paths to CGNS type strings
Source code in plaid/utils/cgns_helper.py
plaid.utils.cgns_helper.nodes_to_tree
¶
Reconstruct a CGNS tree from a dictionary of nodes keyed by their paths.
Each node is assumed to follow the CGNSTree format: [name: str, data: Any, children: List[CGNSTree], cgns_type: str]
The dictionary keys are the full paths to each node, e.g. "Base1/Zone1/Field1".
Parameters:
-
nodes(Dict[str, CGNSTree]) –A dictionary mapping node paths to CGNSTree nodes.
Returns:
-
Optional[CGNSTree]–Optional[CGNSTree]: The root CGNSTree node with all children linked,
-
Optional[CGNSTree]–or None if the input dictionary is empty.
Note
- Nodes with a path of length 1 are treated as root-level nodes.
- The root node is named "CGNSTree" with type "CGNSTree_t".
- Parent-child relationships are reconstructed using path prefixes.
Source code in plaid/utils/cgns_helper.py
plaid.utils.cgns_helper.unflatten_cgns_tree
¶
Reconstruct a CGNS tree from flattened dictionaries of data and types.
This function takes a "flat" representation of a CGNS tree, where each node is stored in a dictionary keyed by its full path (e.g., "Base1/Zone1/Field1"), and another dictionary mapping each path to its CGNS type. It rebuilds the original tree structure by creating nodes and linking them according to their paths.
Parameters:
-
flat(dict[str, object]) –Dictionary mapping node paths to their data values. The data can be a scalar, list, numpy array, or None.
-
cgns_types(dict[str, str]) –Dictionary mapping node paths to CGNS type strings (e.g., "Zone_t", "FlowSolution_t").
Returns:
-
CGNSTree(CGNSTree) –The reconstructed CGNS tree with nodes properly nested according
-
CGNSTree–to their paths. Each node is a list in the format:
-
CGNSTree–[name: str, data: Any, children: List[CGNSTree], cgns_type: str]
Example
flat = { "Base1": None, "Base1/Zone1": [10, 20], "Base1/Zone1/Field1": [1.0, 2.0] } cgns_types = { "Base1": "CGNSBase_t", "Base1/Zone1": "Zone_t", "Base1/Zone1/Field1": "FlowSolution_t" } tree = unflatten_cgns_tree(flat, cgns_types)
Source code in plaid/utils/cgns_helper.py
plaid.utils.cgns_helper.update_features_for_CGNS_compatibility
¶
update_features_for_CGNS_compatibility(
features,
context_constant_features,
context_variable_features,
)
Expand a list of feature paths to include all CGNS hierarchy nodes and metadata required for compatibility.
This function takes a list of requested features and expands it to include all necessary CGNS tree nodes, metadata, and temporal information (_times suffixes) needed to maintain a valid and complete CGNS structure. It handles different feature types (global, fields, coordinates, elements, boundary conditions) and ensures all parent nodes and related metadata are included.
Parameters:
-
features(list[str]) –List of feature paths to expand. Paths follow the CGNS hierarchy format (e.g., "Base/Zone/Solution/Field", "Global/parameter").
-
context_constant_features(list[str]) –Set of constant feature paths available in the context (e.g., grid coordinates, element connectivity).
-
context_variable_features(list[str]) –Set of variable feature paths available in the context (e.g., flow solution fields, time-varying parameters).
Returns:
-
list[str]–list[str]: Expanded list of feature paths, including original requested features, parent nodes in the CGNS hierarchy, associated metadata nodes, temporal information, and related structural elements.
Raises:
-
KeyError–If any requested feature is not found in the combined context features.
Example
context_const = {'Base/Zone', 'Base/Zone/GridCoordinates', ...} context_var = {'Base/Zone/GridCoordinates/CoordinateX', ...} features = ['Base/Zone/GridCoordinates/CoordinateX'] expanded = update_features_for_CGNS_compatibility( ... features, context_const, context_var ... ) 'Base/Zone/GridCoordinates' in expanded True 'Base/Zone/GridCoordinates/CoordinateX_times' in expanded True
Note
- The function creates a copy of the input features list to avoid modifying the original.
- Features with type 'field' automatically include their parent FlowSolution node and GridLocation metadata.
- Global features trigger inclusion of 'Global' and 'Global_times' nodes.
- Spatial features (coordinates, elements, boundary conditions) include full zone hierarchy (Base, Zone, ZoneType, FamilyName) and related structural information.
Source code in plaid/utils/cgns_helper.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 | |