From 9a9d8ba77be43ba61fe6d25040b2487996a3c6d4 Mon Sep 17 00:00:00 2001 From: Antithetical Date: Tue, 9 Sep 2025 15:09:11 +0200 Subject: [PATCH 1/7] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor:=20Standardiz?= =?UTF-8?q?e=20'tree'=20to=20'mesh'=20naming=20convention=20(#111)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(src/plaid/containers/sample.py): * Renamed methods: show_tree -> show_mesh, init_tree -> init_mesh, add_tree -> add_mesh, del_tree -> del_mesh, link_tree -> link_mesh * Added @deprecated decorators to old methods for backward compatibility * Corrected _links and _paths initialization in add_mesh to fix KeyError * refactor(tests/containers/test_sample.py): * Updated method calls (ex: sample.add_tree -> sample.add_mesh) * Renamed fixtures (ex: sample_with_tree -> sample_with_mesh, tree -> mesh) * Updated fixture usage in test functions * refactor(tests/containers/test_dataset.py): * Updated fixture usage (dataset_with_samples_with_tree -> dataset_with_samples_with_mesh) * refactor(tests/pipelines/test_sklearn_block_wrappers.py): * Updated fixture usage (dataset_with_samples_with_tree_nodes_feat_ids -> dataset_with_samples_with_mesh_nodes_feat_ids) * refactor(tests/utils/test_cgns_helper.py): * Updated fixture usage (sample_with_tree -> sample_with_mesh, tree -> mesh) * refactor(tests/conftest.py): * Renamed fixtures (tree -> mesh, sample_with_tree -> sample_with_mesh, samples_with_tree -> samples_with_mesh, dataset_with_samples_with_tree -> dataset_with_samples_with_mesh) * Updated fixture usage (heterogeneous_dataset) * refactor(tests/utils/test_stats.py): * Updated method calls (s.init_tree -> s.init_mesh) * refactor(examples/): * Updated method calls (ex: sample.add_tree -> sample.add_mesh, sample.show_tree -> sample.show_mesh) * refactor(benchmarks/): * Updated method calls (ex: new_sample.add_tree -> new_sample.add_mesh, new_sample.link_tree -> new_sample.link_mesh) * refactor(docs/notebooks): * Updated notebook examples to use mesh instead of tree * docs(CHANGELOG.md): * Added entry for tree → mesh refactor (#111) --- CHANGELOG.md | 7 + .../convert_to_rectilinear_grid.py | 4 +- .../prepare_2D_MultiScHypEl.py | 2 +- .../FNO/2D_profile/prepare_2d_profile.py | 2 +- benchmarks/FNO/Rotor37/prepare_rotor37.py | 2 +- benchmarks/FNO/Tensile2d/prepare_tensile2d.py | 2 +- benchmarks/FNO/VKI-LS59/prepare_vki.py | 2 +- .../convert_users_data_into_plaid.ipynb | 3 +- docs/source/notebooks/dataset.ipynb | 8 +- docs/source/notebooks/huggingface.ipynb | 6 +- docs/source/notebooks/sample.ipynb | 21 +- .../bridges/huggingface_bridge_example.py | 2 +- examples/containers/dataset_example.py | 8 +- examples/containers/sample_example.py | 16 +- examples/convert_users_data_example.py | 2 +- src/plaid/containers/sample.py | 212 +++++---- tests/conftest.py | 26 +- tests/containers/test_dataset.py | 118 ++--- tests/containers/test_sample.py | 424 +++++++++--------- tests/pipelines/conftest.py | 8 +- .../pipelines/test_sklearn_block_wrappers.py | 4 +- tests/utils/test_cgns_helper.py | 20 +- tests/utils/test_stats.py | 2 +- 23 files changed, 482 insertions(+), 419 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da6f4adb..d683a169 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Move to Muscat=2.5.0 (for tests and examples support) - Update repo configuration (actions: rely more on pypi dependencies, action versions) - Rename types to remove `Type` from name of types: https://github.com/PLAID-lib/plaid/pull/164 +- Refactored all `tree` methods, fixtures, and examples to use `mesh` instead: + - Methods: `add_tree` → `add_mesh`, `del_tree` → `del_mesh`, `show_tree` → `show_mesh`, + `init_tree` → `init_mesh`, `link_tree` → `link_mesh` + - Fixtures: `sample_with_tree` → `sample_with_mesh`, `tree` → `mesh`, etc. + - Updated usage in tests, docs, examples, and benchmarks +- Deprecated old `tree` methods for backward compatibility +- Fixed `_links` and `_paths` initialization in `add_mesh` to prevent `KeyError` ### Fixes diff --git a/benchmarks/FNO/2D_ElPlDynamics/convert_to_rectilinear_grid.py b/benchmarks/FNO/2D_ElPlDynamics/convert_to_rectilinear_grid.py index 63ab6f51..06c0ca80 100644 --- a/benchmarks/FNO/2D_ElPlDynamics/convert_to_rectilinear_grid.py +++ b/benchmarks/FNO/2D_ElPlDynamics/convert_to_rectilinear_grid.py @@ -72,7 +72,7 @@ # Building a new sample new_sample = Sample() tree = MeshToCGNS(ref_mesh) - new_sample.add_tree(tree, time=0) + new_sample.add_mesh(tree, time=0) ux = sample.get_field(name="U_x", zone_name="Zone", base_name="Base_2_3", time=0) @@ -128,7 +128,7 @@ uy = old_mesh.nodeFields["U_y"] path_linked_sample = os.path.join( rect_dataset_path, f"dataset/samples/sample_{sample_index:09d}/meshes/mesh_{0:09d}.cgns") - new_sample.link_tree( + new_sample.link_mesh( path_linked_sample, linked_sample=new_sample, linked_time=0, diff --git a/benchmarks/FNO/2D_MultiScHypEl/prepare_2D_MultiScHypEl.py b/benchmarks/FNO/2D_MultiScHypEl/prepare_2D_MultiScHypEl.py index 7556376a..9f71f9e6 100644 --- a/benchmarks/FNO/2D_MultiScHypEl/prepare_2D_MultiScHypEl.py +++ b/benchmarks/FNO/2D_MultiScHypEl/prepare_2D_MultiScHypEl.py @@ -86,7 +86,7 @@ def compute_signed_distance(mesh,eval_points): new_sample=Sample() tree = MeshToCGNS(rec_mesh) - new_sample.add_tree(tree,time=0) + new_sample.add_mesh(tree,time=0) if sample_index in ids_train: diff --git a/benchmarks/FNO/2D_profile/prepare_2d_profile.py b/benchmarks/FNO/2D_profile/prepare_2d_profile.py index 33e20ac6..9e54a8d3 100644 --- a/benchmarks/FNO/2D_profile/prepare_2d_profile.py +++ b/benchmarks/FNO/2D_profile/prepare_2d_profile.py @@ -79,7 +79,7 @@ def compute_signed_distance(mesh,eval_points): new_sample=Sample() tree = MeshToCGNS(rec_mesh) - new_sample.add_tree(tree,time=0) + new_sample.add_mesh(tree,time=0) if sample_index in ids_train: diff --git a/benchmarks/FNO/Rotor37/prepare_rotor37.py b/benchmarks/FNO/Rotor37/prepare_rotor37.py index 8bc6f8ae..89cae516 100644 --- a/benchmarks/FNO/Rotor37/prepare_rotor37.py +++ b/benchmarks/FNO/Rotor37/prepare_rotor37.py @@ -84,7 +84,7 @@ def compute_signed_distance(mesh,eval_points): new_sample=Sample() tree = MeshToCGNS(ref_mesh) - new_sample.add_tree(tree) + new_sample.add_mesh(tree) if sample_index in ids_train: diff --git a/benchmarks/FNO/Tensile2d/prepare_tensile2d.py b/benchmarks/FNO/Tensile2d/prepare_tensile2d.py index d90d1d46..8b470562 100644 --- a/benchmarks/FNO/Tensile2d/prepare_tensile2d.py +++ b/benchmarks/FNO/Tensile2d/prepare_tensile2d.py @@ -81,7 +81,7 @@ def compute_signed_distance(mesh,eval_points): new_sample=Sample() tree = MeshToCGNS(rec_mesh) - new_sample.add_tree(tree,time=0) + new_sample.add_mesh(tree,time=0) if sample_index in ids_train: diff --git a/benchmarks/FNO/VKI-LS59/prepare_vki.py b/benchmarks/FNO/VKI-LS59/prepare_vki.py index d6f722e4..3d917e74 100644 --- a/benchmarks/FNO/VKI-LS59/prepare_vki.py +++ b/benchmarks/FNO/VKI-LS59/prepare_vki.py @@ -49,7 +49,7 @@ tree = MeshToCGNS(rec_mesh) new_sample = Sample() - new_sample.add_tree(tree) + new_sample.add_mesh(tree) if sample_index in ids_train: scalar_names = in_scalars_names + out_scalars_names diff --git a/docs/source/notebooks/convert_users_data_into_plaid.ipynb b/docs/source/notebooks/convert_users_data_into_plaid.ipynb index c78a8541..6e34d58e 100644 --- a/docs/source/notebooks/convert_users_data_into_plaid.ipynb +++ b/docs/source/notebooks/convert_users_data_into_plaid.ipynb @@ -192,7 +192,8 @@ " # Add CGNS Meshe to samples with specific time steps\n", " sample = Sample()\n", "\n", - " sample.add_tree(cgns_tree)\n", + sample.add_mesh(cgns_tree) + "\n", " # Add random scalar values to the sample\n", " for sname in in_scalars_names:\n", diff --git a/docs/source/notebooks/dataset.ipynb b/docs/source/notebooks/dataset.ipynb index b5d04113..4d3df32e 100644 --- a/docs/source/notebooks/dataset.ipynb +++ b/docs/source/notebooks/dataset.ipynb @@ -143,7 +143,7 @@ "outputs": [], "source": [ "# Add a CGNS tree structure to the Sample\n", - "sample_01.add_tree(cgns_mesh)\n", + "sample_01.add_mesh(cgns_mesh)\n", "print(f\"{sample_01 = }\")" ] }, @@ -206,10 +206,10 @@ "sample_03 = Sample()\n", "sample_03.add_scalar(\"speed\", np.random.randn())\n", "sample_03.add_scalar(\"rotation\", sample_01.get_scalar(\"rotation\"))\n", - "sample_03.add_tree(cgns_mesh)\n", + "sample_03.add_mesh(cgns_mesh)\n", "\n", "# Show Sample CGNS content\n", - "sample_03.show_tree()" + "sample_03.show_mesh()" ] }, { @@ -220,7 +220,7 @@ "source": [ "# Add a field to the third empty Sample\n", "sample_03.add_field(\"temperature\", np.random.rand(5), \"Zone\", \"Base_2_2\")\n", - "sample_03.show_tree()" + "sample_03.show_mesh()" ] }, { diff --git a/docs/source/notebooks/huggingface.ipynb b/docs/source/notebooks/huggingface.ipynb index 72b6c1d9..30666cc7 100644 --- a/docs/source/notebooks/huggingface.ipynb +++ b/docs/source/notebooks/huggingface.ipynb @@ -46,7 +46,8 @@ "# Print Sample util\n", "def show_sample(sample: Sample):\n", " print(f\"sample = {sample}\")\n", - " sample.show_tree()\n", + sample.show_mesh() + " print(f\"{sample.get_scalar_names() = }\")\n", " print(f\"{sample.get_field_names() = }\")" ] @@ -92,7 +93,8 @@ "\n", " sample = Sample()\n", "\n", - " sample.add_tree(MeshToCGNS(mesh))\n", + sample.add_mesh(MeshToCGNS(mesh)) + " sample.add_scalar(\"scalar\", np.random.randn())\n", " sample.add_field(\"node_field\", np.random.rand(1, len(points)), location=\"Vertex\")\n", " sample.add_field(\n", diff --git a/docs/source/notebooks/sample.ipynb b/docs/source/notebooks/sample.ipynb index 96502f71..8959d449 100644 --- a/docs/source/notebooks/sample.ipynb +++ b/docs/source/notebooks/sample.ipynb @@ -58,7 +58,8 @@ "# Print Sample util\n", "def show_sample(sample: Sample):\n", " print(f\"sample = {sample}\")\n", - " sample.show_tree()\n", + sample.show_mesh() + " print(f\"{sample.get_scalar_names() = }\")\n", " print(f\"{sample.get_field_names() = }\")" ] @@ -216,10 +217,11 @@ "outputs": [], "source": [ "# Add the previously created CGNS tree to the sample\n", - "sample.add_tree(tree)\n", + sample.add_mesh(mesh) + "\n", "# Display the Sample CGNS tree\n", - "sample.show_tree()" + "sample.show_mesh()" ] }, { @@ -261,7 +263,8 @@ "outputs": [], "source": [ "path_linked_sample = Path.cwd() / \"dataset/samples/sample_000000000/meshes/mesh_000000000.cgns\"\n", - "new_sample_mult_mesh.link_tree(\n", + new_sample_mult_mesh.link_mesh( + " path_linked_sample, linked_sample=sample, linked_time=0.0, time=1.5\n", ")\n", "print(f\"{new_sample_mult_mesh.get_all_mesh_times() = }\")" @@ -488,7 +491,8 @@ "tmp_sample = Sample()\n", "\n", "# Add the previously created CGNS tree in the Sample\n", - "tmp_sample.add_tree(tree)\n", + tmp_sample.add_mesh(mesh) + "\n", "print(\"element connectivity = \\n\", f\"{tmp_sample.get_elements()}\")" ] @@ -663,7 +667,8 @@ "print(f\"{sample.get_all_mesh_times() = }\")\n", "\n", "# Add one CGNS tree at time 1.\n", - "sample.add_tree(tree, 1.0)\n", + sample.add_mesh(mesh, 1.0) + "\n", "# After adding new tree\n", "print(f\"{sample.get_all_mesh_times() = }\")" @@ -729,7 +734,7 @@ "print(f\"{sample.get_time_assignment() = }\", end=\"\\n\\n\")\n", "\n", "# Print the tree at time 1.0\n", - "sample.show_tree() # == sample.show_tree(1.0)" + "sample.show_mesh() # == sample.show_mesh(1.0)" ] }, { @@ -739,7 +744,7 @@ "outputs": [], "source": [ "# If time is specified as an argument in a function, it takes precedence over the default time.\n", - "sample.show_tree(0.0) # Print the tree at time 0.0 even if default time is 1.0" + "sample.show_mesh(0.0) # Print the mesh at time 0.0 even if default time is 1.0" ] }, { diff --git a/examples/bridges/huggingface_bridge_example.py b/examples/bridges/huggingface_bridge_example.py index 923980bd..e40a5e00 100644 --- a/examples/bridges/huggingface_bridge_example.py +++ b/examples/bridges/huggingface_bridge_example.py @@ -87,7 +87,7 @@ # Add CGNS Meshe to samples with specific time steps sample = Sample() - sample.add_tree(cgns_tree) + sample.add_mesh(cgns_tree) # Add random scalar values to the sample for sname in in_scalars_names: diff --git a/examples/containers/dataset_example.py b/examples/containers/dataset_example.py index 9f161921..9f417e6d 100644 --- a/examples/containers/dataset_example.py +++ b/examples/containers/dataset_example.py @@ -88,7 +88,7 @@ def dprint(name: str, dictio: dict, end: str = "\n"): # %% # Add a CGNS tree structure to the Sample -sample_01.add_tree(cgns_mesh) +sample_01.add_mesh(cgns_mesh) print(f"{sample_01 = }") # %% @@ -119,15 +119,15 @@ def dprint(name: str, dictio: dict, end: str = "\n"): sample_03 = Sample() sample_03.add_scalar("speed", np.random.randn()) sample_03.add_scalar("rotation", sample_01.get_scalar("rotation")) -sample_03.add_tree(cgns_mesh) +sample_03.add_mesh(cgns_mesh) # Show Sample CGNS content -sample_03.show_tree() +sample_03.show_mesh() # %% # Add a field to the third empty Sample sample_03.add_field("temperature", np.random.rand(5), "Zone", "Base_2_2") -sample_03.show_tree() +sample_03.show_mesh() # %% [markdown] # ### Get Sample data diff --git a/examples/containers/sample_example.py b/examples/containers/sample_example.py index 0129322e..1c5811df 100644 --- a/examples/containers/sample_example.py +++ b/examples/containers/sample_example.py @@ -31,7 +31,7 @@ # Print Sample util def show_sample(sample: Sample): print(f"{sample = }") - sample.show_tree() + sample.show_mesh() print(f"{sample.get_scalar_names() = }") print(f"{sample.get_field_names() = }") @@ -121,10 +121,10 @@ def show_sample(sample: Sample): # %% # Add the previously created CGNS tree to the sample -sample.add_tree(tree) +sample.add_mesh(tree) # Display the Sample CGNS tree -sample.show_tree() +sample.show_mesh() # %% [markdown] # ### Set all meshes with their corresponding time step @@ -140,7 +140,7 @@ def show_sample(sample: Sample): new_sample_mult_mesh.set_meshes(meshes_dict) print(f"{new_sample_mult_mesh.get_all_mesh_times() = }") -# new_sample_mult_mesh.show_tree(1.) +# new_sample_mult_mesh.show_mesh(1.) # %% [markdown] # ## Section 2: Accessing and Modifying Sample Data @@ -264,7 +264,7 @@ def show_sample(sample: Sample): tmp_sample = Sample() # Add the previously created CGNS tree in the Sample -tmp_sample.add_tree(tree) +tmp_sample.add_mesh(tree) print("element connectivity = \n", f"{tmp_sample.get_elements()}") @@ -357,7 +357,7 @@ def show_sample(sample: Sample): print(f"{sample.get_all_mesh_times() = }") # Add one CGNS tree at time 1. -sample.add_tree(tree, 1.0) +sample.add_mesh(tree, 1.0) # After adding new tree print(f"{sample.get_all_mesh_times() = }") @@ -399,11 +399,11 @@ def show_sample(sample: Sample): print(f"{sample.get_time_assignment() = }", end="\n\n") # Print the tree at time 1.0 -sample.show_tree() # == sample.show_tree(1.0) +sample.show_mesh() # == sample.show_tree(1.0) # %% # If time is specified as an argument in a function, it takes precedence over the default time. -sample.show_tree(0.0) # Print the tree at time 0.0 even if default time is 1.0 +sample.show_mesh(0.0) # Print the mesh at time 0.0 even if default time is 1.0 # %% [markdown] # ### Set and use default base and time in a Sample diff --git a/examples/convert_users_data_example.py b/examples/convert_users_data_example.py index 3a666830..66a68938 100644 --- a/examples/convert_users_data_example.py +++ b/examples/convert_users_data_example.py @@ -121,7 +121,7 @@ # Add CGNS Meshe to samples with specific time steps sample = Sample() - sample.add_tree(cgns_tree) + sample.add_mesh(cgns_tree) # Add random scalar values to the sample for sname in in_scalars_names: diff --git a/src/plaid/containers/sample.py b/src/plaid/containers/sample.py index 1dd17ca0..8c9211e6 100644 --- a/src/plaid/containers/sample.py +++ b/src/plaid/containers/sample.py @@ -54,6 +54,7 @@ ) from plaid.utils import cgns_helper as CGH from plaid.utils.base import safe_len +from plaid.utils.deprecation import deprecated, deprecated_argument logger = logging.getLogger(__name__) logging.basicConfig( @@ -510,34 +511,42 @@ def get_zone_assignment( ) # -------------------------------------------------------------------------# - def show_tree(self, time: float = None) -> None: - """Display the structure of the CGNS tree for a specified time. + def show_mesh(self, time: float = None) -> None: + """Display the structure of the CGNS mesh for a specified time. Args: - time (float, optional): The time step for which you want to display the CGNS tree structure. Defaults to None. If a specific time is not provided, the method will display the tree structure for the default time step. + time (float, optional): The time step for which you want to display the CGNS mesh structure. Defaults to None. If a specific time is not provided, the method will display the mesh structure for the default time step. Examples: .. code-block:: python - # To display the CGNS tree structure for the default time step: - sample.show_tree() + # To display the CGNS mesh structure for the default time step: + sample.show_mesh() - # To display the CGNS tree structure for a specific time step: - sample.show_tree(0.5) + # To display the CGNS mesh structure for a specific time step: + sample.show_mesh(0.5) """ time = self.get_time_assignment(time) if self._meshes is not None: CGH.show_cgns_tree(self._meshes[time]) - def init_tree(self, time: float = None) -> CGNSTree: - """Initialize a CGNS tree structure at a specified time step or create a new one if it doesn't exist. + @deprecated( + "The `show_tree` method is deprecated, use `show_mesh` instead.", + version="0.2.0" + ) + def show_tree(self, time: float = None) -> None: + """DEPRECATED: use `show_mesh` instead""" + return self.show_mesh(time) + + def init_mesh(self, time: float = None) -> CGNSTree: + """Initialize a CGNS mesh structure at a specified time step or create a new one if it doesn't exist. Args: - time (float, optional): The time step for which to initialize the CGNS tree structure. If a specific time is not provided, the method will display the tree structure for the default time step. + time (float, optional): The time step for which to initialize the CGNS mesh structure. If a specific time is not provided, the method will display the mesh structure for the default time step. Returns: - CGNSTree (list): The initialized or existing CGNS tree structure for the specified time step. + CGNSTree (list): The initialized or existing CGNS mesh structure for the specified time step. """ time = self.get_time_assignment(time) @@ -552,30 +561,38 @@ def init_tree(self, time: float = None) -> CGNSTree: return self._meshes[time] + @deprecated( + "The `init_tree` method is deprecated, use `init_mesh` instead.", + version="0.2.0" + ) + def init_tree(self, time: float = None) -> CGNSTree: + """DEPRECATED: use `init_mesh` instead""" + return self.init_mesh(time) + def get_mesh( self, time: float = None, apply_links: bool = False, in_memory=False ) -> CGNSTree: - """Retrieve the CGNS tree structure for a specified time step, if available. + """Retrieve the CGNS mesh structure for a specified time step, if available. Args: - time (float, optional): The time step for which to retrieve the CGNS tree structure. If a specific time is not provided, the method will display the tree structure for the default time step. - apply_links (bool, optional): Activates the following of the CGNS links to reconstruct the complete CGNS tree - in this case, a deepcopy of the tree is made to prevent from modifying the existing tree. + time (float, optional): The time step for which to retrieve the CGNS mesh structure. If a specific time is not provided, the method will display the mesh structure for the default time step. + apply_links (bool, optional): Activates the following of the CGNS links to reconstruct the complete CGNS mesh - in this case, a deepcopy of the mesh is made to prevent from modifying the existing mesh. in_memory (bool, optional): Active if apply_links == True, ONLY WORKING if linked mesh is in the current sample. This option follows the link in memory from current sample. Returns: - CGNSTree: The CGNS tree structure for the specified time step if available; otherwise, returns None. + CGNSTree: The CGNS mesh structure for the specified time step if available; otherwise, returns None. """ if self._meshes is None: return None time = self.get_time_assignment(time) - tree = self._meshes[time] + mesh = self._meshes[time] links = self.get_links(time) if not apply_links or links is None: - return tree + return mesh - tree = copy.deepcopy(tree) + mesh = copy.deepcopy(mesh) for link in links: if not in_memory: subtree, _, _ = CGM.load(str(Path(link[0]) / link[1]), subtree=link[2]) @@ -584,13 +601,13 @@ def get_mesh( linked_timestamp = list(self._meshes.keys())[linked_timestep] subtree = self.get_mesh(linked_timestamp) node_path = "/".join(link[2].split("/")[:-1]) - node_to_append = CGU.getNodeByPath(tree, node_path) + node_to_append = CGU.getNodeByPath(mesh, node_path) assert node_to_append is not None, ( - f"nodepath {node_path} not present in tree, cannot apply link" + f"nodepath {node_path} not present in mesh, cannot apply link" ) node_to_append[2].append(CGU.getNodeByPath(subtree, link[2])) - return tree + return mesh def get_links(self, time: float = None) -> list[CGNSLink]: """Retrieve the CGNS links for a specified time step, if available. @@ -630,47 +647,48 @@ def set_meshes(self, meshes: dict[float, CGNSTree]) -> None: self._paths[time] = None else: raise KeyError( - "meshes is already set, you cannot overwrite it, delete it first or extend it with `Sample.add_tree`" + "meshes is already set, you cannot overwrite it, delete it first or extend it with `Sample.add_mesh`" ) - def add_tree(self, tree: CGNSTree, time: float = None) -> CGNSTree: - """Merge a CGNS tree to the already existing tree. + def add_mesh(self, mesh: CGNSTree, time: float = None) -> CGNSTree: + """Merge a CGNS mesh to the already existing mesh. Args: - tree (CGNSTree): The CGNS tree to be merged. If a Base node already exists, it is ignored. - time (float, optional): The time step for which to add the CGNS tree structure. If a specific time is not provided, the method will display the tree structure for the default time step. + mesh (CGNSTree): The CGNS mesh to be merged. If a Base node already exists, it is ignored. + time (float, optional): The time step for which to add the CGNS mesh structure. If a specific time is not provided, the method will display the mesh structure for the default time step. Raises: - ValueError: If the provided CGNS tree is an empty list. + ValueError: If the provided CGNS mesh is an empty list. Returns: - CGNSTree: The merged CGNS tree. + CGNSTree: The merged CGNS mesh. """ - if tree == []: + if mesh == []: raise ValueError("CGNS Tree should not be an empty list") time = self.get_time_assignment(time) if self._meshes is None: - self._meshes = {time: tree} - self._links = {time: None} - self._paths = {time: None} - elif time not in self._meshes: - self._meshes[time] = tree + self._meshes = {} # Initialize as empty dict + self._links = {} # Initialize as empty dict + self._paths = {} # Initialize as empty dict + + self._meshes[time] = mesh + if time not in self._links: # Only initialize if not already present self._links[time] = None + if time not in self._paths: # Only initialize if not already present self._paths[time] = None - else: - # TODO: gérer le cas où il y a des bases de mêmes noms... + merge - # récursif des nœuds - local_bases = self.get_base_names(time=time) - base_nodes = CGU.getNodesFromTypeSet(tree, "CGNSBase_t") - for _, node in base_nodes: - if node[__NAME__] not in local_bases: # pragma: no cover - self._meshes[time][__CHILDREN__].append(node) - else: - logger.warning( - f"base <{node[__NAME__]}> already exists in self._tree --> ignored" - ) + + # The rest of the logic remains the same + local_bases = self.get_base_names(time=time) + base_nodes = CGU.getNodesFromTypeSet(mesh, "CGNSBase_t") + for _, node in base_nodes: + if node[__NAME__] not in local_bases: # pragma: no cover + self._meshes[time][__CHILDREN__].append(node) + else: + logger.warning( + f"base <{node[__NAME__]}> already exists in self._meshes --> ignored" + ) base_names = self.get_base_names(time=time) for base_name in base_names: @@ -684,45 +702,61 @@ def add_tree(self, tree: CGNSTree, time: float = None) -> CGNSTree: return self._meshes[time] - def del_tree(self, time: float) -> CGNSTree: - """Delete the CGNS tree for a specific time. + @deprecated( + "The `add_tree` method is deprecated, use `add_mesh` instead.", + version="0.2.0" + ) + def add_tree(self, tree: CGNSTree, time: float = None) -> CGNSTree: + """DEPRECATED: use `add_mesh` instead""" + return self.add_mesh(tree, time) + + def del_mesh(self, time: float) -> CGNSTree: + """Delete the CGNS mesh for a specific time. Args: - time (float): The time step for which to delete the CGNS tree structure. + time (float): The time step for which to delete the CGNS mesh structure. Raises: - KeyError: There is no CGNS tree in this Sample / There is no CGNS tree for the provided time. + KeyError: There is no CGNS mesh in this Sample / There is no CGNS mesh for the provided time. Returns: - CGNSTree: The deleted CGNS tree. + CGNSTree: The deleted CGNS mesh. """ if self._meshes is None: - raise KeyError("There is no CGNS tree in this sample.") + raise KeyError("There is no CGNS mesh in this sample.") if time not in self._meshes: - raise KeyError(f"There is no CGNS tree for time {time}.") + raise KeyError(f"There is no CGNS mesh for time {time}.") self._links.pop(time, None) self._paths.pop(time, None) return self._meshes.pop(time) - def link_tree( + @deprecated( + "The `del_tree` method is deprecated, use `del_mesh` instead.", + version="0.2.0" + ) + def del_tree(self, time: float) -> CGNSTree: + """DEPRECATED: use `del_mesh` instead""" + return self.del_mesh(time) + + def link_mesh( self, path_linked_sample: Union[str, Path], linked_sample: Self, linked_time: float, time: float, ) -> CGNSTree: - """Link the geometrical features of the CGNS tree of the current sample at a given time, to the ones of another sample. + """Link the geometrical features of the CGNS mesh of the current sample at a given time, to the ones of another sample. Args: path_linked_sample (Union[str,Path]): The absolute path of the folder containing the linked CGNS linked_sample (Sample): The linked sample linked_time (float): The time step of the linked CGNS in the linked sample - time (float): The time step the current sample to which the CGNS tree is linked. + time (float): The time step the current sample to which the CGNS mesh is linked. Returns: - CGNSTree: The deleted CGNS tree. + CGNSTree: The linked CGNS mesh. """ # see https://pycgns.github.io/MAP/sids-to-python.html#links # difficulty is to link only the geometrical objects, which can be complex @@ -734,19 +768,19 @@ def link_tree( if linked_time not in linked_sample._meshes: # pragma: no cover raise KeyError( - f"There is no CGNS tree for time {linked_time} in linked_sample." + f"There is no CGNS mesh for time {linked_time} in linked_sample." ) if time in self._meshes: # pragma: no cover - raise KeyError(f"A CGNS tree is already linked in self for time {time}.") + raise KeyError(f"A CGNS mesh is already linked in self for time {time}.") - tree = CGL.newCGNSTree() + mesh = CGL.newCGNSTree() base_names = linked_sample.get_base_names(time=linked_time) for bn in base_names: base_node = linked_sample.get_base(bn, time=linked_time) base = [bn, base_node[1], [], "CGNSBase_t"] - tree[2].append(base) + mesh[2].append(base) family = [ "Bulk", @@ -813,13 +847,27 @@ def find_feature_roots(sample: Sample, time: float, Type_t: str): for feature in ["ZoneBC_t", "Elements_t", "GridCoordinates_t"]: feature_paths += find_feature_roots(linked_sample, linked_time, feature) - self.add_tree(tree, time=time) + self.add_mesh(mesh, time=time) dname = path_linked_sample.parent bname = path_linked_sample.name self._links[time] = [[str(dname), bname, fp, fp] for fp in feature_paths] - return tree + return mesh + + @deprecated( + "The `link_tree` method is deprecated, use `link_mesh` instead.", + version="0.2.0" + ) + def link_tree( + self, + path_linked_sample: Union[str, Path], + linked_sample: Self, + linked_time: float, + time: float, + ) -> CGNSTree: + """DEPRECATED: use `link_mesh` instead""" + return self.link_mesh(path_linked_sample, linked_sample, linked_time, time) # -------------------------------------------------------------------------# def get_topological_dim(self, base_name: str = None, time: float = None) -> int: @@ -897,7 +945,7 @@ def init_base( + str(physical_dim) ) - self.init_tree(time) + self.init_mesh(time) if not (self.has_base(base_name, time)): base_node = CGL.newCGNSBase( self._meshes[time], base_name, topological_dim, physical_dim @@ -925,17 +973,17 @@ def del_base(self, base_name: str, time: float) -> CGNSTree: time (float): The time step for which to delete the CGNS base node. Raises: - KeyError: There is no CGNS tree in this sample / There is no CGNS tree for the provided time. + KeyError: There is no CGNS mesh in this sample / There is no CGNS mesh for the provided time. KeyError: If there is no base node with the given base name or time. Returns: - CGNSTree: The tree at the provided time (without the deleted node) + CGNSTree: The mesh at the provided time (without the deleted node) """ if self._meshes is None: - raise KeyError("There is no CGNS tree in this sample.") + raise KeyError("There is no CGNS mesh in this sample.") if time not in self._meshes: - raise KeyError(f"There is no CGNS tree for time {time}.") + raise KeyError(f"There is no CGNS mesh for time {time}.") base_node = self.get_base(base_name, time) mesh_tree = self._meshes[time] @@ -971,14 +1019,14 @@ def get_base_names( return [] def has_base(self, base_name: str, time: float = None) -> bool: - """Check if a CGNS tree contains a Base with a given name at a specified time. + """Check if a CGNS mesh contains a Base with a given name at a specified time. Args: - base_name (str): The name of the Base to check for in the CGNS tree. - time (float, optional): The time at which to check for the Base. If a specific time is not provided, the method will display the tree structure for the default time step. + base_name (str): The name of the Base to check for in the CGNS mesh. + time (float, optional): The time at which to check for the Base. If a specific time is not provided, the method will display the mesh structure for the default time step. Returns: - bool: `True` if the CGNS tree has a Base called `base_name`, else return `False`. + bool: `True` if the CGNS mesh has a Base called `base_name`, else return `False`. """ # get_base_names will look for the default time return base_name in self.get_base_names(time=time) @@ -999,7 +1047,7 @@ def get_base(self, base_name: str = None, time: float = None) -> CGNSNode: base_name = self.get_base_assignment(base_name, time) if (self._meshes is None) or (self._meshes[time] is None): - logger.warning(f"No base with name {base_name} and this tree") + logger.warning(f"No base with name {base_name} and this mesh") return None return CGU.getNodeByPath(self._meshes[time], f"/CGNSTree/{base_name}") @@ -1030,8 +1078,8 @@ def init_zone( """ _check_names([zone_name]) - # init_tree will look for default time - self.init_tree(time) + # init_mesh will look for default time + self.init_mesh(time) # get_base will look for default base_name and time base_node = self.get_base(base_name, time) if base_node is None: @@ -1055,17 +1103,17 @@ def del_zone(self, zone_name: str, base_name: str, time: float) -> CGNSTree: time (float, optional): The time step for which to delete the zone. Defaults to None. Raises: - KeyError: There is no CGNS tree in this sample / There is no CGNS tree for the provided time. + KeyError: There is no CGNS mesh in this sample / There is no CGNS mesh for the provided time. KeyError: If there is no base node with the given base name or time. Returns: - CGNSTree: The tree at the provided time (without the deleted node) + CGNSTree: The mesh at the provided time (without the deleted node) """ if self._meshes is None: # pragma: no cover - raise KeyError("There is no CGNS tree in this sample.") + raise KeyError("There is no CGNS mesh in this sample.") if time not in self._meshes: - raise KeyError(f"There is no CGNS tree for time {time}.") + raise KeyError(f"There is no CGNS mesh for time {time}.") zone_node = self.get_zone(zone_name, base_name, time) mesh_tree = self._meshes[time] @@ -1147,7 +1195,7 @@ def get_zone( # get_base will look for default base_name and time base_node = self.get_base(base_name, time) if base_node is None: - logger.warning(f"No base with name {base_name} and this tree") + logger.warning(f"No base with name {base_name} and this mesh") return None # _zone_attribution will look for default base_name @@ -1611,8 +1659,8 @@ def add_field( KeyError: Raised if the specified zone does not exist in the given base. """ _check_names([name]) - # init_tree will look for default time - self.init_tree(time) + # init_mesh will look for default time + self.init_mesh(time) # get_zone will look for default zone_name, base_name and time zone_node = self.get_zone(zone_name, base_name, time) diff --git a/tests/conftest.py b/tests/conftest.py index f01553a8..762036e4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -132,30 +132,30 @@ def cell_center_field(): @pytest.fixture() -def tree(nodes, triangles, vertex_field, cell_center_field, nodal_tags): +def mesh(nodes, triangles, vertex_field, cell_center_field, nodal_tags): Mesh = MCT.CreateMeshOfTriangles(nodes, triangles) Mesh.GetNodalTag("tag").AddToTag(nodal_tags) Mesh.nodeFields["test_node_field_1"] = vertex_field Mesh.nodeFields["big_node_field"] = np.random.randn(50) Mesh.elemFields["test_elem_field_1"] = cell_center_field - tree = MeshToCGNS(Mesh) - return tree + mesh = MeshToCGNS(Mesh) + return mesh @pytest.fixture() -def sample_with_tree(tree: CGNSTree) -> Sample: - """Generate a Sample objects with a tree.""" +def sample_with_mesh(mesh: CGNSTree) -> Sample: + """Generate a Sample objects with a mesh.""" sample = Sample() - sample.add_tree(tree) + sample.add_mesh(mesh) return sample @pytest.fixture() -def samples_with_tree(nb_samples: int, sample_with_tree: Sample) -> list[Sample]: - """Generate a list of Sample objects with a tree.""" +def samples_with_mesh(nb_samples: int, sample_with_mesh: Sample) -> list[Sample]: + """Generate a list of Sample objects with a mesh.""" sample_list = [] for _ in range(nb_samples): - sample_list.append(copy.deepcopy(sample_with_tree)) + sample_list.append(copy.deepcopy(sample_with_mesh)) return sample_list @@ -192,9 +192,9 @@ def dataset_with_samples(dataset, samples, infos): @pytest.fixture() -def dataset_with_samples_with_tree(samples_with_tree, infos): +def dataset_with_samples_with_mesh(samples_with_mesh, infos): dataset = Dataset() - dataset.add_samples(samples_with_tree) + dataset.add_samples(samples_with_mesh) dataset.set_infos(infos) return dataset @@ -207,8 +207,8 @@ def other_dataset_with_samples(other_samples): @pytest.fixture() -def heterogeneous_dataset(dataset_with_samples_with_tree): - dataset = dataset_with_samples_with_tree.copy() +def heterogeneous_dataset(dataset_with_samples_with_mesh): + dataset = dataset_with_samples_with_mesh.copy() dataset.add_sample(Sample()) sample_with_scalar = Sample() sample_with_scalar.add_scalar("scalar", 1.0) diff --git a/tests/containers/test_dataset.py b/tests/containers/test_dataset.py index 6f766fb2..2e1196d9 100644 --- a/tests/containers/test_dataset.py +++ b/tests/containers/test_dataset.py @@ -395,7 +395,7 @@ def test_get_scalars_to_tabular_same_scalars_name( # -------------------------------------------------------------------------# def test_get_feature_from_string_identifier( - self, dataset_with_samples, dataset_with_samples_with_tree + self, dataset_with_samples, dataset_with_samples_with_mesh ): dataset_with_samples.get_feature_from_string_identifier("scalar::test_scalar") @@ -419,19 +419,19 @@ def test_get_feature_from_string_identifier( "field::test_field_same_size/TestBaseName/TestZoneName/Vertex/0" ) - dataset_with_samples_with_tree.get_feature_from_string_identifier("nodes::") - dataset_with_samples_with_tree.get_feature_from_string_identifier( + dataset_with_samples_with_mesh.get_feature_from_string_identifier("nodes::") + dataset_with_samples_with_mesh.get_feature_from_string_identifier( "nodes::Base_2_2" ) - dataset_with_samples_with_tree.get_feature_from_string_identifier( + dataset_with_samples_with_mesh.get_feature_from_string_identifier( "nodes::Base_2_2/Zone" ) - dataset_with_samples_with_tree.get_feature_from_string_identifier( + dataset_with_samples_with_mesh.get_feature_from_string_identifier( "nodes::Base_2_2/Zone/0" ) def test_get_feature_from_identifier( - self, dataset_with_samples, dataset_with_samples_with_tree + self, dataset_with_samples, dataset_with_samples_with_mesh ): dataset_with_samples.get_feature_from_identifier( {"type": "scalar", "name": "test_scalar"} @@ -498,28 +498,28 @@ def test_get_feature_from_identifier( } ) - dataset_with_samples_with_tree.get_feature_from_identifier({"type": "nodes"}) - dataset_with_samples_with_tree.get_feature_from_identifier( + dataset_with_samples_with_mesh.get_feature_from_identifier({"type": "nodes"}) + dataset_with_samples_with_mesh.get_feature_from_identifier( {"type": "nodes", "base_name": "Base_2_2"} ) - dataset_with_samples_with_tree.get_feature_from_identifier( + dataset_with_samples_with_mesh.get_feature_from_identifier( {"type": "nodes", "base_name": "Base_2_2", "zone_name": "Zone"} ) - dataset_with_samples_with_tree.get_feature_from_identifier( + dataset_with_samples_with_mesh.get_feature_from_identifier( {"type": "nodes", "base_name": "Base_2_2", "zone_name": "Zone", "time": 0.0} ) - dataset_with_samples_with_tree.get_feature_from_identifier( + dataset_with_samples_with_mesh.get_feature_from_identifier( {"type": "nodes", "zone_name": "Zone"} ) - dataset_with_samples_with_tree.get_feature_from_identifier( + dataset_with_samples_with_mesh.get_feature_from_identifier( {"type": "nodes", "time": 0.0} ) - def test_get_features_from_identifiers(self, dataset_with_samples_with_tree): - dataset_with_samples_with_tree.get_features_from_identifiers( + def test_get_features_from_identifiers(self, dataset_with_samples_with_mesh): + dataset_with_samples_with_mesh.get_features_from_identifiers( [{"type": "nodes"}] ) - dataset_with_samples_with_tree.get_features_from_identifiers( + dataset_with_samples_with_mesh.get_features_from_identifiers( [ {"type": "nodes", "base_name": "Base_2_2"}, { @@ -533,7 +533,7 @@ def test_get_features_from_identifiers(self, dataset_with_samples_with_tree): ) def test_update_features_from_identifier( - self, dataset_with_samples, dataset_with_samples_with_tree + self, dataset_with_samples, dataset_with_samples_with_mesh ): indices = dataset_with_samples.get_sample_ids() dataset_with_samples.update_features_from_identifier( @@ -553,8 +553,8 @@ def test_update_features_from_identifier( in_place=False, ) - indices = dataset_with_samples_with_tree.get_sample_ids() - before = dataset_with_samples_with_tree[0].get_field( + indices = dataset_with_samples_with_mesh.get_sample_ids() + before = dataset_with_samples_with_mesh[0].get_field( name="test_node_field_1", zone_name="Zone", base_name="Base_2_2", @@ -562,7 +562,7 @@ def test_update_features_from_identifier( time=0.0, ) - dataset_with_samples_with_tree.update_features_from_identifier( + dataset_with_samples_with_mesh.update_features_from_identifier( feature_identifiers={ "type": "field", "name": "test_node_field_1", @@ -575,10 +575,10 @@ def test_update_features_from_identifier( in_place=False, ) - before = dataset_with_samples_with_tree[0].get_nodes( + before = dataset_with_samples_with_mesh[0].get_nodes( zone_name="Zone", base_name="Base_2_2", time=0.0 ) - dataset_with_samples_with_tree.update_features_from_identifier( + dataset_with_samples_with_mesh.update_features_from_identifier( feature_identifiers={ "type": "nodes", "base_name": "Base_2_2", @@ -589,9 +589,9 @@ def test_update_features_from_identifier( in_place=False, ) - before_1 = dataset_with_samples_with_tree[0].get_field("test_node_field_1") - before_2 = dataset_with_samples_with_tree[0].get_nodes() - dataset_with_samples_with_tree.update_features_from_identifier( + before_1 = dataset_with_samples_with_mesh[0].get_field("test_node_field_1") + before_2 = dataset_with_samples_with_mesh[0].get_nodes() + dataset_with_samples_with_mesh.update_features_from_identifier( feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, {"type": "nodes"}, @@ -606,14 +606,14 @@ def test_update_features_from_identifier( in_place=False, ) - dataset_with_samples_with_tree.update_features_from_identifier( + dataset_with_samples_with_mesh.update_features_from_identifier( feature_identifiers=[{"type": "field", "name": "test_node_field_1"}], features={ind: [np.random.rand(*before_1.shape)] for ind in indices}, in_place=True, ) def test_from_features_identifier( - self, dataset_with_samples, dataset_with_samples_with_tree + self, dataset_with_samples, dataset_with_samples_with_mesh ): dataset_with_samples.from_features_identifier( feature_identifiers={"type": "scalar", "name": "test_scalar"}, @@ -622,7 +622,7 @@ def test_from_features_identifier( feature_identifiers={"type": "time_series", "name": "test_time_series_1"}, ) - dataset_with_samples_with_tree.from_features_identifier( + dataset_with_samples_with_mesh.from_features_identifier( feature_identifiers={ "type": "field", "name": "test_node_field_1", @@ -633,7 +633,7 @@ def test_from_features_identifier( }, ) - dataset_with_samples_with_tree.from_features_identifier( + dataset_with_samples_with_mesh.from_features_identifier( feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, {"type": "nodes"}, @@ -641,14 +641,14 @@ def test_from_features_identifier( ) def test_get_tabular_from_homogeneous_identifiers( - self, nb_samples, dataset_with_samples, dataset_with_samples_with_tree + self, nb_samples, dataset_with_samples, dataset_with_samples_with_mesh ): X = dataset_with_samples.get_tabular_from_homogeneous_identifiers( feature_identifiers=[{"type": "scalar", "name": "test_scalar"}], ) assert X.shape == (nb_samples, 1, 1) - X = dataset_with_samples_with_tree.get_tabular_from_homogeneous_identifiers( + X = dataset_with_samples_with_mesh.get_tabular_from_homogeneous_identifiers( feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, {"type": "field", "name": "OriginalIds"}, @@ -683,13 +683,13 @@ def test_get_tabular_from_stacked_identifiers( assert X.shape == (nb_samples, 1) def test_get_tabular_from_homogeneous_identifiers_inconsistent_features_through_features( - self, dataset_with_samples_with_tree + self, dataset_with_samples_with_mesh ): - dataset_with_samples_with_tree_ = copy.deepcopy(dataset_with_samples_with_tree) - for sample in dataset_with_samples_with_tree_: + dataset_with_samples_with_mesh_ = copy.deepcopy(dataset_with_samples_with_mesh) + for sample in dataset_with_samples_with_mesh_: sample.add_field("test_node_field_1", [0, 1], warning_overwrite=False) with pytest.raises(AssertionError): - dataset_with_samples_with_tree_.get_tabular_from_homogeneous_identifiers( + dataset_with_samples_with_mesh_.get_tabular_from_homogeneous_identifiers( feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, {"type": "field", "name": "OriginalIds"}, @@ -697,31 +697,31 @@ def test_get_tabular_from_homogeneous_identifiers_inconsistent_features_through_ ) def test_get_tabular_from_homogeneous_identifiers_inconsistent_features_through_samples( - self, dataset_with_samples_with_tree + self, dataset_with_samples_with_mesh ): - dataset_with_samples_with_tree_ = copy.deepcopy(dataset_with_samples_with_tree) - dataset_with_samples_with_tree_[0].add_field( + dataset_with_samples_with_mesh_ = copy.deepcopy(dataset_with_samples_with_mesh) + dataset_with_samples_with_mesh_[0].add_field( "test_node_field_1", [0, 1], warning_overwrite=False ) - dataset_with_samples_with_tree_[0].add_field( + dataset_with_samples_with_mesh_[0].add_field( "OriginalIds", [0, 1], warning_overwrite=False ) with pytest.raises(AssertionError): - dataset_with_samples_with_tree_.get_tabular_from_homogeneous_identifiers( + dataset_with_samples_with_mesh_.get_tabular_from_homogeneous_identifiers( feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, {"type": "field", "name": "OriginalIds"}, ], ) - def test_from_tabular(self, dataset_with_samples_with_tree): - X = dataset_with_samples_with_tree.get_tabular_from_homogeneous_identifiers( + def test_from_tabular(self, dataset_with_samples_with_mesh): + X = dataset_with_samples_with_mesh.get_tabular_from_homogeneous_identifiers( feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, {"type": "field", "name": "OriginalIds"}, ], ) - dataset = dataset_with_samples_with_tree.from_tabular( + dataset = dataset_with_samples_with_mesh.from_tabular( tabular=X, feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, @@ -729,31 +729,31 @@ def test_from_tabular(self, dataset_with_samples_with_tree): ], restrict_to_features=True, ) - last_index = dataset_with_samples_with_tree.get_sample_ids()[-1] + last_index = dataset_with_samples_with_mesh.get_sample_ids()[-1] assert np.isclose( dataset[0].get_field("OriginalIds"), - dataset_with_samples_with_tree[0].get_field("OriginalIds"), + dataset_with_samples_with_mesh[0].get_field("OriginalIds"), ).all() assert np.isclose( dataset[0].get_field("test_node_field_1"), - dataset_with_samples_with_tree[0].get_field("test_node_field_1"), + dataset_with_samples_with_mesh[0].get_field("test_node_field_1"), ).all() assert np.isclose( dataset[last_index].get_field("OriginalIds"), - dataset_with_samples_with_tree[last_index].get_field("OriginalIds"), + dataset_with_samples_with_mesh[last_index].get_field("OriginalIds"), ).all() assert np.isclose( dataset[last_index].get_field("test_node_field_1"), - dataset_with_samples_with_tree[last_index].get_field("test_node_field_1"), + dataset_with_samples_with_mesh[last_index].get_field("test_node_field_1"), ).all() - X = dataset_with_samples_with_tree.get_tabular_from_homogeneous_identifiers( + X = dataset_with_samples_with_mesh.get_tabular_from_homogeneous_identifiers( feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, {"type": "field", "name": "OriginalIds"}, ], ) - dataset = dataset_with_samples_with_tree.from_tabular( + dataset = dataset_with_samples_with_mesh.from_tabular( tabular=X, feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, @@ -761,25 +761,25 @@ def test_from_tabular(self, dataset_with_samples_with_tree): ], restrict_to_features=False, ) - last_index = dataset_with_samples_with_tree.get_sample_ids()[-1] + last_index = dataset_with_samples_with_mesh.get_sample_ids()[-1] assert np.isclose( dataset[0].get_field("OriginalIds"), - dataset_with_samples_with_tree[0].get_field("OriginalIds"), + dataset_with_samples_with_mesh[0].get_field("OriginalIds"), ).all() assert np.isclose( dataset[0].get_field("test_node_field_1"), - dataset_with_samples_with_tree[0].get_field("test_node_field_1"), + dataset_with_samples_with_mesh[0].get_field("test_node_field_1"), ).all() assert np.isclose( dataset[last_index].get_field("OriginalIds"), - dataset_with_samples_with_tree[last_index].get_field("OriginalIds"), + dataset_with_samples_with_mesh[last_index].get_field("OriginalIds"), ).all() assert np.isclose( dataset[last_index].get_field("test_node_field_1"), - dataset_with_samples_with_tree[last_index].get_field("test_node_field_1"), + dataset_with_samples_with_mesh[last_index].get_field("test_node_field_1"), ).all() - dataset = dataset_with_samples_with_tree.from_tabular( + dataset = dataset_with_samples_with_mesh.from_tabular( tabular=X, feature_identifiers={"type": "field", "name": "OriginalIds"}, ) @@ -874,13 +874,13 @@ def test_merge_dataset_by_features( def test_summarize_features( self, dataset_with_samples, - dataset_with_samples_with_tree, + dataset_with_samples_with_mesh, empty_dataset, heterogeneous_dataset, scalar_dataset, ): dataset_with_samples.summarize_features() - dataset_with_samples_with_tree.summarize_features() + dataset_with_samples_with_mesh.summarize_features() empty_dataset.summarize_features() heterogeneous_dataset.summarize_features() scalar_dataset.summarize_features() @@ -888,13 +888,13 @@ def test_summarize_features( def test_check_feature_completeness( self, dataset_with_samples, - dataset_with_samples_with_tree, + dataset_with_samples_with_mesh, empty_dataset, heterogeneous_dataset, scalar_dataset, ): dataset_with_samples.check_feature_completeness() - dataset_with_samples_with_tree.check_feature_completeness() + dataset_with_samples_with_mesh.check_feature_completeness() empty_dataset.check_feature_completeness() heterogeneous_dataset.check_feature_completeness() scalar_dataset.check_feature_completeness() diff --git a/tests/containers/test_sample.py b/tests/containers/test_sample.py index ae0d9ac9..1b6f9f5d 100644 --- a/tests/containers/test_sample.py +++ b/tests/containers/test_sample.py @@ -75,42 +75,42 @@ def nodes3d(): @pytest.fixture() -def sample_with_linked_tree(tree, tmp_path): - sample_with_linked_tree = Sample() - sample_with_linked_tree.add_tree(tree) +def sample_with_linked_mesh(mesh, tmp_path): + sample_with_linked_mesh = Sample() + sample_with_linked_mesh.add_mesh(mesh) path_linked_sample = tmp_path / "test_dir" / "meshes/mesh_000000000.cgns" - sample_with_linked_tree.link_tree( - path_linked_sample, sample_with_linked_tree, linked_time=0.0, time=1.0 + sample_with_linked_mesh.link_mesh( + path_linked_sample, sample_with_linked_mesh, linked_time=0.0, time=1.0 ) - return sample_with_linked_tree + return sample_with_linked_mesh @pytest.fixture() -def tree3d(nodes3d, triangles, vertex_field, cell_center_field): +def mesh3d(nodes3d, triangles, vertex_field, cell_center_field): Mesh = MCT.CreateMeshOfTriangles(nodes3d, triangles) Mesh.nodeFields["test_node_field_1"] = vertex_field Mesh.nodeFields["big_node_field"] = np.random.randn(50) Mesh.elemFields["test_elem_field_1"] = cell_center_field - tree = MeshToCGNS(Mesh) - return tree + mesh = MeshToCGNS(Mesh) + return mesh @pytest.fixture() -def sample_with_tree3d(sample, tree3d): - sample.add_tree(tree3d) +def sample_with_mesh3d(sample, mesh3d): + sample.add_mesh(mesh3d) return sample @pytest.fixture() -def sample_with_tree_and_scalar_and_time_series( - sample_with_tree, +def sample_with_mesh_and_scalar_and_time_series( + sample_with_mesh, ): - sample_with_tree.add_scalar("r", np.random.randn()) - sample_with_tree.add_scalar("test_scalar_1", np.random.randn()) - sample_with_tree.add_time_series( + sample_with_mesh.add_scalar("r", np.random.randn()) + sample_with_mesh.add_scalar("test_scalar_1", np.random.randn()) + sample_with_mesh.add_time_series( "test_time_series_1", np.arange(111, dtype=float), np.random.randn(111) ) - return sample_with_tree + return sample_with_mesh # %% Test @@ -127,16 +127,16 @@ def test_check_names(): _check_names([r"test\/name"]) -def test_read_index(tree, physical_dim): - _read_index(tree, physical_dim) +def test_read_index(mesh, physical_dim): + _read_index(mesh, physical_dim) -def test_read_index_array(tree): - _read_index_array(tree) +def test_read_index_array(mesh): + _read_index_array(mesh) -def test_read_index_range(tree, physical_dim): - _read_index_range(tree, physical_dim) +def test_read_index_range(mesh, physical_dim): + _read_index_range(mesh, physical_dim) @pytest.fixture() @@ -192,8 +192,8 @@ def test__init__both_path_and_directory_path(self, current_directory): with pytest.raises(ValueError): Sample(path=sample_path, directory_path=sample_path) - def test_copy(self, sample_with_tree_and_scalar_and_time_series): - sample_with_tree_and_scalar_and_time_series.copy() + def test_copy(self, sample_with_mesh_and_scalar_and_time_series): + sample_with_mesh_and_scalar_and_time_series.copy() # -------------------------------------------------------------------------# def test_set_default_base(self, sample, topological_dim, physical_dim): @@ -275,60 +275,60 @@ def test_set_default_time(self, sample, topological_dim, physical_dim): # -------------------------------------------------------------------------# - def test_show_tree(self, sample_with_tree_and_scalar_and_time_series): - sample_with_tree_and_scalar_and_time_series.show_tree() + def test_show_mesh(self, sample_with_mesh_and_scalar_and_time_series): + sample_with_mesh_and_scalar_and_time_series.show_mesh() - def test_init_tree(self, sample): - sample.init_tree() - sample.init_tree(0.5) + def test_init_mesh(self, sample): + sample.init_mesh() + sample.init_mesh(0.5) def test_get_mesh_empty(self, sample): sample.get_mesh() - def test_get_mesh(self, sample_with_tree_and_scalar_and_time_series): - sample_with_tree_and_scalar_and_time_series.get_mesh() + def test_get_mesh(self, sample_with_mesh_and_scalar_and_time_series): + sample_with_mesh_and_scalar_and_time_series.get_mesh() - def test_get_mesh_without_links(self, sample_with_linked_tree): - sample_with_linked_tree.get_mesh(time=1.0, apply_links=False) + def test_get_mesh_without_links(self, sample_with_linked_mesh): + sample_with_linked_mesh.get_mesh(time=1.0, apply_links=False) - def test_get_mesh_with_links_in_memory(self, sample_with_linked_tree): - sample_with_linked_tree.get_mesh(time=1.0, apply_links=True, in_memory=True) + def test_get_mesh_with_links_in_memory(self, sample_with_linked_mesh): + sample_with_linked_mesh.get_mesh(time=1.0, apply_links=True, in_memory=True) - def test_get_mesh_with_links(self, sample_with_linked_tree, tmp_path): - sample_with_linked_tree.save(tmp_path / "test_dir") - sample_with_linked_tree.get_mesh(time=1.0, apply_links=True) + def test_get_mesh_with_links(self, sample_with_linked_mesh, tmp_path): + sample_with_linked_mesh.save(tmp_path / "test_dir") + sample_with_linked_mesh.get_mesh(time=1.0, apply_links=True) - def test_set_meshes_empty(self, sample, tree): - sample.set_meshes({0.0: tree}) + def test_set_meshes_empty(self, sample, mesh): + sample.set_meshes({0.0: mesh}) - def test_set_meshes(self, sample_with_tree, tree): + def test_set_meshes(self, sample_with_mesh, mesh): with pytest.raises(KeyError): - sample_with_tree.set_meshes({0.0: tree}) + sample_with_mesh.set_meshes({0.0: mesh}) - def test_add_tree_empty(self, sample_with_tree): + def test_add_mesh_empty(self, sample_with_mesh): with pytest.raises(ValueError): - sample_with_tree.add_tree([]) + sample_with_mesh.add_mesh([]) - def test_add_tree(self, sample, tree): - sample.add_tree(tree) - sample.add_tree(tree) - sample.add_tree(tree, time=0.2) + def test_add_mesh(self, sample, mesh): + sample.add_mesh(mesh) + sample.add_mesh(mesh) + sample.add_mesh(mesh, time=0.2) - def test_del_tree(self, sample, tree): - sample.add_tree(tree) - sample.add_tree(tree, time=0.2) + def test_del_mesh(self, sample, mesh): + sample.add_mesh(mesh) + sample.add_mesh(mesh, time=0.2) - assert isinstance(sample.del_tree(0.2), list) + assert isinstance(sample.del_mesh(0.2), list) assert list(sample._meshes.keys()) == [0.0] assert list(sample._links.keys()) == [0.0] assert list(sample._paths.keys()) == [0.0] - assert isinstance(sample.del_tree(0.0), list) + assert isinstance(sample.del_mesh(0.0), list) assert list(sample._meshes.keys()) == [] assert list(sample._links.keys()) == [] assert list(sample._paths.keys()) == [] - def test_link_tree(self, sample_with_linked_tree): + def test_link_mesh(self, sample_with_linked_mesh): link_checks = [ "/Base_2_2/Zone/Elements_Selections", "/Base_2_2/Zone/Points_Selections", @@ -337,19 +337,19 @@ def test_link_tree(self, sample_with_linked_tree): "/Base_2_2/Zone/GridCoordinates", "/Base_2_2/Zone/ZoneBC", ] - for link in sample_with_linked_tree._links[1]: + for link in sample_with_linked_mesh._links[1]: assert link[1] == "mesh_000000000.cgns" assert link[2] == link[3] assert link[2] in link_checks - def test_on_error_del_tree(self, sample, tree): + def test_on_error_del_mesh(self, sample, mesh): with pytest.raises(KeyError): - sample.del_tree(0.0) + sample.del_mesh(0.0) - sample.add_tree(tree) - sample.add_tree(tree, time=0.2) + sample.add_mesh(mesh) + sample.add_mesh(mesh, time=0.2) with pytest.raises(KeyError): - sample.del_tree(0.7) + sample.del_mesh(0.7) # -------------------------------------------------------------------------# def test_init_base(self, sample, base_name, topological_dim, physical_dim): @@ -366,41 +366,41 @@ def test_del_base_existing_base( sample.init_base(topological_dim, physical_dim, second_base_name) # Delete first base - updated_cgns_tree = sample.del_base(base_name, 0.0) - assert updated_cgns_tree is not None and isinstance(updated_cgns_tree, list) + updated_cgns_mesh = sample.del_base(base_name, 0.0) + assert updated_cgns_mesh is not None and isinstance(updated_cgns_mesh, list) - # Testing the resulting tree + # Testing the resulting mesh new_sample = Sample() - new_sample.add_tree(updated_cgns_tree, 0.1) + new_sample.add_mesh(updated_cgns_mesh, 0.1) assert new_sample.get_topological_dim() == topological_dim assert new_sample.get_physical_dim() == physical_dim assert new_sample.get_base_names() == [second_base_name] # Add 2 bases and delete one base at time 0.2 - sample.init_base(topological_dim, physical_dim, "tree", 0.2) + sample.init_base(topological_dim, physical_dim, "mesh", 0.2) sample.init_base(topological_dim, physical_dim, base_name, 0.2) - updated_cgns_tree = sample.del_base("tree", 0.2) - assert sample.get_base("tree", 0.2) is None + updated_cgns_mesh = sample.del_base("mesh", 0.2) + assert sample.get_base("mesh", 0.2) is None assert sample.get_base(base_name, 0.2) is not None assert sample.get_base(second_base_name) is not None - assert updated_cgns_tree is not None and isinstance(updated_cgns_tree, list) + assert updated_cgns_mesh is not None and isinstance(updated_cgns_mesh, list) # Testing the resulting from time 0.2 new_sample = Sample() - new_sample.add_tree(updated_cgns_tree) + new_sample.add_mesh(updated_cgns_mesh) assert new_sample.get_topological_dim() == topological_dim assert new_sample.get_physical_dim() == physical_dim assert new_sample.get_base_names() == [base_name] # Deleting the last base at time 0.0 - updated_cgns_tree = sample.del_base(second_base_name, 0.0) + updated_cgns_mesh = sample.del_base(second_base_name, 0.0) assert sample.get_base(second_base_name) is None - assert updated_cgns_tree is not None and isinstance(updated_cgns_tree, list) + assert updated_cgns_mesh is not None and isinstance(updated_cgns_mesh, list) # Deleting the last base at time 0.2 - updated_cgns_tree = sample.del_base(base_name, 0.2) + updated_cgns_mesh = sample.del_base(base_name, 0.2) assert sample.get_base(base_name) is None - assert updated_cgns_tree is not None and isinstance(updated_cgns_tree, list) + assert updated_cgns_mesh is not None and isinstance(updated_cgns_mesh, list) def test_del_base_nonexistent_base_nonexistent_time( self, sample, base_name, topological_dim, physical_dim @@ -411,7 +411,7 @@ def test_del_base_nonexistent_base_nonexistent_time( with pytest.raises(KeyError): sample.del_base("unknown", time=1.0) - def test_del_base_no_cgns_tree(self, sample): + def test_del_base_no_cgns_mesh(self, sample): with pytest.raises(KeyError): sample.del_base("unknwon", 0.0) @@ -446,7 +446,7 @@ def test_get_base_names(self, sample): assert sample.get_physical_dim("base_name_2") == 3 def test_get_base(self, sample, base_name): - sample.init_tree() + sample.init_mesh() assert sample.get_base() is None sample.init_base(3, 3, base_name) assert sample.get_base(base_name) is not None @@ -487,12 +487,12 @@ def test_del_zone_existing_zone(self, sample, base_name, zone_name, zone_shape): ) # Delete first zone - updated_cgns_tree = sample.del_zone(zone_name, base_name, 0.0) - assert updated_cgns_tree is not None and isinstance(updated_cgns_tree, list) + updated_cgns_mesh = sample.del_zone(zone_name, base_name, 0.0) + assert updated_cgns_mesh is not None and isinstance(updated_cgns_mesh, list) - # Testing the resulting tree + # Testing the resulting mesh new_sample = Sample() - new_sample.add_tree(updated_cgns_tree, 0.1) + new_sample.add_mesh(updated_cgns_mesh, 0.1) assert new_sample.get_zone_names() == [second_zone_name] # Add 2 zones and delete one zone at time 0.2 @@ -504,26 +504,26 @@ def test_del_zone_existing_zone(self, sample, base_name, zone_name, zone_shape): zone_shape, CGK.Unstructured_s, "test", base_name=base_name, time=0.2 ) - updated_cgns_tree = sample.del_zone("test", base_name, 0.2) - assert sample.get_zone("tree", base_name, 0.2) is None + updated_cgns_mesh = sample.del_zone("test", base_name, 0.2) + assert sample.get_zone("mesh", base_name, 0.2) is None assert sample.get_zone(zone_name, base_name, 0.2) is not None assert sample.get_zone(second_zone_name, base_name) is not None - assert updated_cgns_tree is not None and isinstance(updated_cgns_tree, list) + assert updated_cgns_mesh is not None and isinstance(updated_cgns_mesh, list) # Testing the resulting from time 0.2 new_sample = Sample() - new_sample.add_tree(updated_cgns_tree) + new_sample.add_mesh(updated_cgns_mesh) assert new_sample.get_zone_names(base_name) == [zone_name] # Deleting the last zone at time 0.0 - updated_cgns_tree = sample.del_zone(second_zone_name, base_name, 0.0) + updated_cgns_mesh = sample.del_zone(second_zone_name, base_name, 0.0) assert sample.get_zone(second_zone_name, base_name) is None - assert updated_cgns_tree is not None and isinstance(updated_cgns_tree, list) + assert updated_cgns_mesh is not None and isinstance(updated_cgns_mesh, list) # Deleting the last zone at time 0.2 - updated_cgns_tree = sample.del_zone(zone_name, base_name, 0.2) + updated_cgns_mesh = sample.del_zone(zone_name, base_name, 0.2) assert sample.get_zone(zone_name, base_name) is None - assert updated_cgns_tree is not None and isinstance(updated_cgns_tree, list) + assert updated_cgns_mesh is not None and isinstance(updated_cgns_mesh, list) def test_del_zone_nonexistent_zone_nonexistent_time( self, sample, base_name, zone_shape, topological_dim, physical_dim @@ -538,7 +538,7 @@ def test_del_zone_nonexistent_zone_nonexistent_time( with pytest.raises(KeyError): sample.del_zone("unknown", base_name, 1.0) - def test_del_zone_no_cgns_tree(self, sample): + def test_del_zone_no_cgns_mesh(self, sample): sample.init_base(2, 3, "only_base") with pytest.raises(KeyError): sample.del_zone("unknwon", "only_base", 0.0) @@ -548,7 +548,7 @@ def test_has_zone(self, sample, base_name, zone_name): sample.init_zone( np.random.randint(0, 10, size=3), zone_name=zone_name, base_name=base_name ) - sample.show_tree() + sample.show_mesh() assert sample.has_zone(zone_name, base_name) assert not sample.has_zone("not_present_zone_name", base_name) assert not sample.has_zone(zone_name, "not_present_base_name") @@ -578,7 +578,7 @@ def test_get_zone_names(self, sample, base_name): def test_get_zone_type(self, sample, zone_name, base_name): with pytest.raises(KeyError): sample.get_zone_type(zone_name, base_name) - sample.init_tree() + sample.init_mesh() with pytest.raises(KeyError): sample.get_zone_type(zone_name, base_name) sample.init_base(3, 3, base_name) @@ -714,18 +714,18 @@ def test_del_time_series(self, sample_with_time_series): def test_get_nodal_tags_empty(self, sample): assert sample.get_nodal_tags() == {} - def test_get_nodal_tags(self, sample_with_tree, nodal_tags): - assert np.all(sample_with_tree.get_nodal_tags()["tag"] == nodal_tags) + def test_get_nodal_tags(self, sample_with_mesh, nodal_tags): + assert np.all(sample_with_mesh.get_nodal_tags()["tag"] == nodal_tags) # -------------------------------------------------------------------------# def test_get_nodes_empty(self, sample): assert sample.get_nodes() is None - def test_get_nodes(self, sample_with_tree, nodes): - assert np.all(sample_with_tree.get_nodes() == nodes) + def test_get_nodes(self, sample_with_mesh, nodes): + assert np.all(sample_with_mesh.get_nodes() == nodes) - def test_get_nodes3d(self, sample_with_tree3d, nodes3d): - assert np.all(sample_with_tree3d.get_nodes() == nodes3d) + def test_get_nodes3d(self, sample_with_mesh3d, nodes3d): + assert np.all(sample_with_mesh3d.get_nodes() == nodes3d) def test_set_nodes(self, sample, nodes, zone_name, base_name): sample.init_base(3, 3, base_name) @@ -740,11 +740,11 @@ def test_set_nodes(self, sample, nodes, zone_name, base_name): def test_get_elements_empty(self, sample): assert sample.get_elements() == {} - def test_get_elements(self, sample_with_tree, triangles): - assert list(sample_with_tree.get_elements().keys()) == ["TRI_3"] + def test_get_elements(self, sample_with_mesh, triangles): + assert list(sample_with_mesh.get_elements().keys()) == ["TRI_3"] print(f"{triangles=}") - print(f"{sample_with_tree.get_elements()=}") - assert np.all(sample_with_tree.get_elements()["TRI_3"] == triangles) + print(f"{sample_with_mesh.get_elements()=}") + assert np.all(sample_with_mesh.get_elements()["TRI_3"] == triangles) # -------------------------------------------------------------------------# def test_get_field_names(self, sample): @@ -755,10 +755,10 @@ def test_get_field_empty(self, sample): assert sample.get_field("missing_field_name") is None assert sample.get_field("missing_field_name", location="CellCenter") is None - def test_get_field(self, sample_with_tree): - assert sample_with_tree.get_field("missing_field") is None - assert sample_with_tree.get_field("test_node_field_1").shape == (5,) - assert sample_with_tree.get_field( + def test_get_field(self, sample_with_mesh): + assert sample_with_mesh.get_field("missing_field") is None + assert sample_with_mesh.get_field("test_node_field_1").shape == (5,) + assert sample_with_mesh.get_field( "test_elem_field_1", location="CellCenter" ).shape == (3,) @@ -794,19 +794,19 @@ def test_add_field_cell_center( location="CellCenter", ) - def test_add_field_vertex_already_present(self, sample_with_tree, vertex_field): + def test_add_field_vertex_already_present(self, sample_with_mesh, vertex_field): # with pytest.raises(KeyError): - sample_with_tree.show_tree() - sample_with_tree.add_field( + sample_with_mesh.show_mesh() + sample_with_mesh.add_field( "test_node_field_1", vertex_field, "Zone", "Base_2_2" ) def test_add_field_cell_center_already_present( - self, sample_with_tree, cell_center_field + self, sample_with_mesh, cell_center_field ): # with pytest.raises(KeyError): - sample_with_tree.show_tree() - sample_with_tree.add_field( + sample_with_mesh.show_mesh() + sample_with_mesh.add_field( "test_elem_field_1", cell_center_field, "Zone", @@ -814,13 +814,13 @@ def test_add_field_cell_center_already_present( location="CellCenter", ) - def test_del_field_existing(self, sample_with_tree): + def test_del_field_existing(self, sample_with_mesh): with pytest.raises(KeyError): - sample_with_tree.del_field( + sample_with_mesh.del_field( "unknown", "Zone", "Base_2_2", location="CellCenter" ) with pytest.raises(KeyError): - sample_with_tree.del_field( + sample_with_mesh.del_field( "unknown", "unknown_zone", "Base_2_2", location="CellCenter" ) @@ -862,13 +862,13 @@ def test_del_field_in_zone(self, zone_name, base_name, cell_center_field): ) # Del field 'test_elem_field_2' - new_tree = sample.del_field( + new_mesh = sample.del_field( "test_elem_field_2", zone_name, base_name, location="CellCenter" ) - # Testing new tree on field 'test_elem_field_2' + # Testing new mesh on field 'test_elem_field_2' new_sample = Sample() - new_sample.add_tree(new_tree) + new_sample.add_mesh(new_mesh) assert ( new_sample.get_field( @@ -882,13 +882,13 @@ def test_del_field_in_zone(self, zone_name, base_name, cell_center_field): assert "test_elem_field_1" in fields # Del field 'test_elem_field_1' - new_tree = sample.del_field( + new_mesh = sample.del_field( "test_elem_field_1", zone_name, base_name, location="CellCenter" ) - # Testing new tree on field 'test_elem_field_1' + # Testing new mesh on field 'test_elem_field_1' new_sample = Sample() - new_sample.add_tree(new_tree) + new_sample.add_mesh(new_mesh) assert ( new_sample.get_field( @@ -899,68 +899,68 @@ def test_del_field_in_zone(self, zone_name, base_name, cell_center_field): fields = new_sample.get_field_names(zone_name, base_name, location="CellCenter") assert len(fields) == 0 - def test_del_all_fields(self, sample_with_tree): - sample_with_tree.del_all_fields() + def test_del_all_fields(self, sample_with_mesh): + sample_with_mesh.del_all_fields() # -------------------------------------------------------------------------# def test_get_feature_from_string_identifier( - self, sample_with_tree_and_scalar_and_time_series + self, sample_with_mesh_and_scalar_and_time_series ): - sample_with_tree_and_scalar_and_time_series.get_feature_from_string_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_string_identifier( "scalar::test_scalar_1" ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_string_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_string_identifier( "time_series::test_time_series_1" ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_string_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_string_identifier( "field::test_node_field_1" ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_string_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_string_identifier( "field::test_node_field_1/Base_2_2" ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_string_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_string_identifier( "field::test_node_field_1/Base_2_2/Zone" ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_string_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_string_identifier( "field::test_node_field_1/Base_2_2/Zone/Vertex" ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_string_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_string_identifier( "field::test_node_field_1/Base_2_2/Zone/Vertex/0" ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_string_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_string_identifier( "nodes::" ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_string_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_string_identifier( "nodes::Base_2_2" ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_string_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_string_identifier( "nodes::Base_2_2/Zone" ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_string_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_string_identifier( "nodes::Base_2_2/Zone/0" ) def test_get_feature_from_identifier( - self, sample_with_tree_and_scalar_and_time_series + self, sample_with_mesh_and_scalar_and_time_series ): - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( {"type": "scalar", "name": "test_scalar_1"} ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( {"type": "time_series", "name": "test_time_series_1"} ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( {"type": "field", "name": "test_node_field_1"} ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( {"type": "field", "name": "test_node_field_1", "base_name": "Base_2_2"} ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( { "type": "field", "name": "test_node_field_1", @@ -968,7 +968,7 @@ def test_get_feature_from_identifier( "zone_name": "Zone", } ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( { "type": "field", "name": "test_node_field_1", @@ -977,7 +977,7 @@ def test_get_feature_from_identifier( "location": "Vertex", } ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( { "type": "field", "name": "test_node_field_1", @@ -987,10 +987,10 @@ def test_get_feature_from_identifier( "time": 0.0, } ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( {"type": "field", "name": "test_node_field_1", "time": 0.0} ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( { "type": "field", "name": "test_node_field_1", @@ -998,7 +998,7 @@ def test_get_feature_from_identifier( "time": 0.0, } ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( { "type": "field", "name": "test_node_field_1", @@ -1008,39 +1008,39 @@ def test_get_feature_from_identifier( } ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( {"type": "nodes"} ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( {"type": "nodes", "base_name": "Base_2_2"} ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( {"type": "nodes", "base_name": "Base_2_2", "zone_name": "Zone"} ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( {"type": "nodes", "base_name": "Base_2_2", "zone_name": "Zone", "time": 0.0} ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( {"type": "nodes", "zone_name": "Zone"} ) - sample_with_tree_and_scalar_and_time_series.get_feature_from_identifier( + sample_with_mesh_and_scalar_and_time_series.get_feature_from_identifier( {"type": "nodes", "time": 0.0} ) def test_get_features_from_identifiers( - self, sample_with_tree_and_scalar_and_time_series + self, sample_with_mesh_and_scalar_and_time_series ): - sample_with_tree_and_scalar_and_time_series.get_features_from_identifiers( + sample_with_mesh_and_scalar_and_time_series.get_features_from_identifiers( [{"type": "scalar", "name": "test_scalar_1"}] ) - sample_with_tree_and_scalar_and_time_series.get_features_from_identifiers( + sample_with_mesh_and_scalar_and_time_series.get_features_from_identifiers( [ {"type": "scalar", "name": "test_scalar_1"}, {"type": "time_series", "name": "test_time_series_1"}, ] ) - sample_with_tree_and_scalar_and_time_series.get_features_from_identifiers( + sample_with_mesh_and_scalar_and_time_series.get_features_from_identifiers( [ { "type": "field", @@ -1056,11 +1056,11 @@ def test_get_features_from_identifiers( ) def test_update_features_from_identifier( - self, sample_with_tree_and_scalar_and_time_series + self, sample_with_mesh_and_scalar_and_time_series ): - before = sample_with_tree_and_scalar_and_time_series.get_scalar("test_scalar_1") + before = sample_with_mesh_and_scalar_and_time_series.get_scalar("test_scalar_1") sample_ = ( - sample_with_tree_and_scalar_and_time_series.update_features_from_identifier( + sample_with_mesh_and_scalar_and_time_series.update_features_from_identifier( feature_identifiers={"type": "scalar", "name": "test_scalar_1"}, features=3.1415, in_place=False, @@ -1069,11 +1069,11 @@ def test_update_features_from_identifier( after = sample_.get_scalar("test_scalar_1") assert after != before - before = sample_with_tree_and_scalar_and_time_series.get_time_series( + before = sample_with_mesh_and_scalar_and_time_series.get_time_series( "test_time_series_1" ) sample_ = ( - sample_with_tree_and_scalar_and_time_series.update_features_from_identifier( + sample_with_mesh_and_scalar_and_time_series.update_features_from_identifier( feature_identifiers={ "type": "time_series", "name": "test_time_series_1", @@ -1085,7 +1085,7 @@ def test_update_features_from_identifier( after = sample_.get_time_series("test_time_series_1") assert len(after[0]) != len(before[0]) - before = sample_with_tree_and_scalar_and_time_series.get_field( + before = sample_with_mesh_and_scalar_and_time_series.get_field( name="test_node_field_1", zone_name="Zone", base_name="Base_2_2", @@ -1093,7 +1093,7 @@ def test_update_features_from_identifier( time=0.0, ) sample_ = ( - sample_with_tree_and_scalar_and_time_series.update_features_from_identifier( + sample_with_mesh_and_scalar_and_time_series.update_features_from_identifier( feature_identifiers={ "type": "field", "name": "test_node_field_1", @@ -1115,11 +1115,11 @@ def test_update_features_from_identifier( ) assert np.any(~np.isclose(after, before)) - before = sample_with_tree_and_scalar_and_time_series.get_nodes( + before = sample_with_mesh_and_scalar_and_time_series.get_nodes( zone_name="Zone", base_name="Base_2_2", time=0.0 ) sample_ = ( - sample_with_tree_and_scalar_and_time_series.update_features_from_identifier( + sample_with_mesh_and_scalar_and_time_series.update_features_from_identifier( feature_identifiers={ "type": "nodes", "base_name": "Base_2_2", @@ -1133,12 +1133,12 @@ def test_update_features_from_identifier( after = sample_.get_nodes(zone_name="Zone", base_name="Base_2_2", time=0.0) assert np.any(~np.isclose(after, before)) - before_1 = sample_with_tree_and_scalar_and_time_series.get_field( + before_1 = sample_with_mesh_and_scalar_and_time_series.get_field( "test_node_field_1" ) - before_2 = sample_with_tree_and_scalar_and_time_series.get_nodes() + before_2 = sample_with_mesh_and_scalar_and_time_series.get_nodes() sample_ = ( - sample_with_tree_and_scalar_and_time_series.update_features_from_identifier( + sample_with_mesh_and_scalar_and_time_series.update_features_from_identifier( feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, {"type": "nodes"}, @@ -1156,36 +1156,36 @@ def test_update_features_from_identifier( assert np.any(~np.isclose(after_2, before_2)) sample_ = ( - sample_with_tree_and_scalar_and_time_series.update_features_from_identifier( + sample_with_mesh_and_scalar_and_time_series.update_features_from_identifier( feature_identifiers=[{"type": "field", "name": "test_node_field_1"}], features=[np.random.rand(*before_1.shape)], in_place=True, ) ) - ref_1 = sample_with_tree_and_scalar_and_time_series.get_field( + ref_1 = sample_with_mesh_and_scalar_and_time_series.get_field( "test_node_field_1" ) ref_2 = sample_.get_field("test_node_field_1") assert np.any(np.isclose(ref_1, ref_2)) def test_from_features_identifier( - self, sample_with_tree_and_scalar_and_time_series + self, sample_with_mesh_and_scalar_and_time_series ): - sample_ = sample_with_tree_and_scalar_and_time_series.from_features_identifier( + sample_ = sample_with_mesh_and_scalar_and_time_series.from_features_identifier( feature_identifiers={"type": "scalar", "name": "test_scalar_1"}, ) assert sample_.get_scalar_names() == ["test_scalar_1"] assert len(sample_.get_time_series_names()) == 0 assert len(sample_.get_field_names()) == 0 - sample_ = sample_with_tree_and_scalar_and_time_series.from_features_identifier( + sample_ = sample_with_mesh_and_scalar_and_time_series.from_features_identifier( feature_identifiers={"type": "time_series", "name": "test_time_series_1"}, ) assert len(sample_.get_scalar_names()) == 0 assert sample_.get_time_series_names() == ["test_time_series_1"] assert len(sample_.get_field_names()) == 0 - sample_ = sample_with_tree_and_scalar_and_time_series.from_features_identifier( + sample_ = sample_with_mesh_and_scalar_and_time_series.from_features_identifier( feature_identifiers={ "type": "field", "name": "test_node_field_1", @@ -1199,7 +1199,7 @@ def test_from_features_identifier( assert len(sample_.get_time_series_names()) == 0 assert sample_.get_field_names() == ["test_node_field_1"] - sample_ = sample_with_tree_and_scalar_and_time_series.from_features_identifier( + sample_ = sample_with_mesh_and_scalar_and_time_series.from_features_identifier( feature_identifiers={ "type": "nodes", "base_name": "Base_2_2", @@ -1211,7 +1211,7 @@ def test_from_features_identifier( assert len(sample_.get_time_series_names()) == 0 assert len(sample_.get_field_names()) == 0 - sample_ = sample_with_tree_and_scalar_and_time_series.from_features_identifier( + sample_ = sample_with_mesh_and_scalar_and_time_series.from_features_identifier( feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, {"type": "nodes"}, @@ -1222,10 +1222,10 @@ def test_from_features_identifier( assert sample_.get_field_names() == ["test_node_field_1"] def test_get_all_features_identifiers( - self, sample_with_tree_and_scalar_and_time_series + self, sample_with_mesh_and_scalar_and_time_series ): feat_ids = ( - sample_with_tree_and_scalar_and_time_series.get_all_features_identifiers() + sample_with_mesh_and_scalar_and_time_series.get_all_features_identifiers() ) assert len(feat_ids) == 10 assert {"type": "scalar", "name": "r"} in feat_ids @@ -1287,21 +1287,21 @@ def test_get_all_features_identifiers( } in feat_ids def test_get_all_features_identifiers_by_type( - self, sample_with_tree_and_scalar_and_time_series + self, sample_with_mesh_and_scalar_and_time_series ): - feat_ids = sample_with_tree_and_scalar_and_time_series.get_all_features_identifiers_by_type( + feat_ids = sample_with_mesh_and_scalar_and_time_series.get_all_features_identifiers_by_type( "scalar" ) assert len(feat_ids) == 2 assert {"type": "scalar", "name": "r"} in feat_ids assert {"type": "scalar", "name": "test_scalar_1"} in feat_ids - feat_ids = sample_with_tree_and_scalar_and_time_series.get_all_features_identifiers_by_type( + feat_ids = sample_with_mesh_and_scalar_and_time_series.get_all_features_identifiers_by_type( "time_series" ) assert {"type": "time_series", "name": "test_time_series_1"} in feat_ids - feat_ids = sample_with_tree_and_scalar_and_time_series.get_all_features_identifiers_by_type( + feat_ids = sample_with_mesh_and_scalar_and_time_series.get_all_features_identifiers_by_type( "nodes" ) assert { @@ -1311,7 +1311,7 @@ def test_get_all_features_identifiers_by_type( "time": 0.0, } in feat_ids - feat_ids = sample_with_tree_and_scalar_and_time_series.get_all_features_identifiers_by_type( + feat_ids = sample_with_mesh_and_scalar_and_time_series.get_all_features_identifiers_by_type( "field" ) assert len(feat_ids) == 6 @@ -1325,20 +1325,20 @@ def test_get_all_features_identifiers_by_type( } in feat_ids def test_merge_features( - self, sample_with_tree_and_scalar_and_time_series, sample_with_tree + self, sample_with_mesh_and_scalar_and_time_series, sample_with_mesh ): feat_id = ( - sample_with_tree_and_scalar_and_time_series.get_all_features_identifiers() + sample_with_mesh_and_scalar_and_time_series.get_all_features_identifiers() ) feat_id = [ fid for fid in feat_id if fid["type"] not in ["scalar", "time_series"] ] - sample_1 = sample_with_tree_and_scalar_and_time_series.from_features_identifier( + sample_1 = sample_with_mesh_and_scalar_and_time_series.from_features_identifier( feat_id ) - feat_id = sample_with_tree.get_all_features_identifiers() + feat_id = sample_with_mesh.get_all_features_identifiers() feat_id = [fid for fid in feat_id if fid["type"] not in ["field", "node"]] - sample_2 = sample_with_tree.from_features_identifier(feat_id) + sample_2 = sample_with_mesh.from_features_identifier(feat_id) sample_merge_1 = sample_1.merge_features(sample_2, in_place=False) sample_merge_2 = sample_2.merge_features(sample_1, in_place=False) assert ( @@ -1349,32 +1349,32 @@ def test_merge_features( sample_1.merge_features(sample_2, in_place=True) # -------------------------------------------------------------------------# - def test_save(self, sample_with_tree_and_scalar_and_time_series, tmp_path): + def test_save(self, sample_with_mesh_and_scalar_and_time_series, tmp_path): save_dir = tmp_path / "test_dir" - sample_with_tree_and_scalar_and_time_series.save(save_dir) + sample_with_mesh_and_scalar_and_time_series.save(save_dir) assert save_dir.is_dir() with pytest.raises(ValueError): - sample_with_tree_and_scalar_and_time_series.save(save_dir) - sample_with_tree_and_scalar_and_time_series.save(save_dir, overwrite=True) + sample_with_mesh_and_scalar_and_time_series.save(save_dir) + sample_with_mesh_and_scalar_and_time_series.save(save_dir, overwrite=True) def test_load_from_saved_file( - self, sample_with_tree_and_scalar_and_time_series, tmp_path + self, sample_with_mesh_and_scalar_and_time_series, tmp_path ): save_dir = tmp_path / "test_dir" - sample_with_tree_and_scalar_and_time_series.save(save_dir) + sample_with_mesh_and_scalar_and_time_series.save(save_dir) new_sample = Sample() new_sample.load(save_dir) assert CGU.checkSameTree( - sample_with_tree_and_scalar_and_time_series.get_mesh(), + sample_with_mesh_and_scalar_and_time_series.get_mesh(), new_sample.get_mesh(), ) - def test_load_from_dir(self, sample_with_tree_and_scalar_and_time_series, tmp_path): + def test_load_from_dir(self, sample_with_mesh_and_scalar_and_time_series, tmp_path): save_dir = tmp_path / "test_dir" - sample_with_tree_and_scalar_and_time_series.save(save_dir) + sample_with_mesh_and_scalar_and_time_series.save(save_dir) new_sample = Sample.load_from_dir(save_dir) assert CGU.checkSameTree( - sample_with_tree_and_scalar_and_time_series.get_mesh(), + sample_with_mesh_and_scalar_and_time_series.get_mesh(), new_sample.get_mesh(), ) @@ -1385,13 +1385,13 @@ def test___repr___empty(self, sample): def test___repr__with_scalar(self, sample_with_scalar): print(sample_with_scalar) - def test___repr__with_tree(self, sample_with_tree): - print(sample_with_tree) + def test___repr__with_mesh(self, sample_with_mesh): + print(sample_with_mesh) - def test___repr__with_tree_and_scalar( - self, sample_with_tree_and_scalar_and_time_series + def test___repr__with_mesh_and_scalar( + self, sample_with_mesh_and_scalar_and_time_series ): - print(sample_with_tree_and_scalar_and_time_series) + print(sample_with_mesh_and_scalar_and_time_series) # -------------------------------------------------------------------------# @@ -1401,13 +1401,13 @@ def test_summarize_empty(self, sample): def test_summarize_with_scalar(self, sample_with_scalar): print(sample_with_scalar.summarize()) - def test_summarize_with_tree(self, sample_with_tree): - print(sample_with_tree.summarize()) + def test_summarize_with_mesh(self, sample_with_mesh): + print(sample_with_mesh.summarize()) - def test_summarize_with_tree_and_scalar( - self, sample_with_tree_and_scalar_and_time_series + def test_summarize_with_mesh_and_scalar( + self, sample_with_mesh_and_scalar_and_time_series ): - print(sample_with_tree_and_scalar_and_time_series.summarize()) + print(sample_with_mesh_and_scalar_and_time_series.summarize()) def test_check_completeness_empty(self, sample): print(sample.check_completeness()) @@ -1415,10 +1415,10 @@ def test_check_completeness_empty(self, sample): def test_check_completeness_with_scalar(self, sample_with_scalar): print(sample_with_scalar.check_completeness()) - def test_check_completeness_with_tree(self, sample_with_tree): - print(sample_with_tree.check_completeness()) + def test_check_completeness_with_mesh(self, sample_with_mesh): + print(sample_with_mesh.check_completeness()) - def test_check_completeness_with_tree_and_scalar( - self, sample_with_tree_and_scalar_and_time_series + def test_check_completeness_with_mesh_and_scalar( + self, sample_with_mesh_and_scalar_and_time_series ): - print(sample_with_tree_and_scalar_and_time_series.check_completeness()) + print(sample_with_mesh_and_scalar_and_time_series.check_completeness()) \ No newline at end of file diff --git a/tests/pipelines/conftest.py b/tests/pipelines/conftest.py index f0504e4d..fa8153aa 100644 --- a/tests/pipelines/conftest.py +++ b/tests/pipelines/conftest.py @@ -55,20 +55,20 @@ def dataset_with_samples_time_series_feat_ids(dataset_with_samples): @pytest.fixture() -def dataset_with_samples_with_tree_field_feat_ids(dataset_with_samples_with_tree): +def dataset_with_samples_with_mesh_field_feat_ids(dataset_with_samples_with_mesh): return dataset_with_samples_with_tree.get_all_features_identifiers_by_type("field") @pytest.fixture() -def dataset_with_samples_with_tree_1field_feat_ids(dataset_with_samples_with_tree): +def dataset_with_samples_with_mesh_1field_feat_ids(dataset_with_samples_with_mesh): return [ dataset_with_samples_with_tree.get_all_features_identifiers_by_type("field")[0] ] @pytest.fixture() -def dataset_with_samples_with_tree_nodes_feat_ids(dataset_with_samples_with_tree): - return dataset_with_samples_with_tree.get_all_features_identifiers_by_type("nodes") +def dataset_with_samples_with_mesh_nodes_feat_ids(dataset_with_samples_with_mesh): + return dataset_with_samples_with_mesh.get_all_features_identifiers_by_type("nodes") # --------------------------------------------------------------------------------------- diff --git a/tests/pipelines/test_sklearn_block_wrappers.py b/tests/pipelines/test_sklearn_block_wrappers.py index 467573f0..3c585f83 100644 --- a/tests/pipelines/test_sklearn_block_wrappers.py +++ b/tests/pipelines/test_sklearn_block_wrappers.py @@ -64,10 +64,10 @@ def test_get_2Darray_from_homogeneous_identifiers( def test_get_2Darray_from_homogeneous_identifiers_nodes( - dataset_with_samples_with_tree, dataset_with_samples_with_tree_nodes_feat_ids + dataset_with_samples_with_mesh, dataset_with_samples_with_mesh_nodes_feat_ids ): X = get_2Darray_from_homogeneous_identifiers( - dataset_with_samples_with_tree, dataset_with_samples_with_tree_nodes_feat_ids + dataset_with_samples_with_mesh, dataset_with_samples_with_mesh_nodes_feat_ids ) assert X.shape == (4, 10) diff --git a/tests/utils/test_cgns_helper.py b/tests/utils/test_cgns_helper.py index 6b2c729c..8eb9e4d0 100644 --- a/tests/utils/test_cgns_helper.py +++ b/tests/utils/test_cgns_helper.py @@ -19,8 +19,8 @@ # %% Tests class Test_cgns_helper: - def test_get_base_names(self, sample_with_tree): - tree = sample_with_tree.get_mesh() + def test_get_base_names(self, sample_with_mesh): + tree = sample_with_mesh.get_mesh() # Test with full_path=False and unique=False base_names = get_base_names(tree, full_path=False, unique=False) assert base_names == ["Base_2_2"] @@ -35,8 +35,8 @@ def test_get_base_names(self, sample_with_tree): print(base_names_unique) assert base_names_unique == ["Base_2_2"] - def test_get_time_values(self, sample_with_tree): - tree = sample_with_tree.get_mesh() + def test_get_time_values(self, sample_with_mesh): + tree = sample_with_mesh.get_mesh() time_value = get_time_values(tree) assert time_value == 0.0 @@ -44,15 +44,15 @@ def test_get_time_values(self, sample_with_tree): with pytest.raises(IndexError): get_time_values(empty_tree) - def test_show_cgns_tree(self, tree): - show_cgns_tree(tree) + def test_show_cgns_tree(self, mesh): + show_cgns_tree(mesh) def test_show_cgns_tree_not_a_list(self): with pytest.raises(TypeError): show_cgns_tree({1: 2}) - def test_summarize_cgns_tree(self, tree): - summarize_cgns_tree(tree, verbose=False) + def test_summarize_cgns_tree(self, mesh): + summarize_cgns_tree(mesh, verbose=False) - def test_summarize_cgns_tree_verbose(self, tree): - summarize_cgns_tree(tree, verbose=True) + def test_summarize_cgns_tree_verbose(self, mesh): + summarize_cgns_tree(mesh, verbose=True) diff --git a/tests/utils/test_stats.py b/tests/utils/test_stats.py index f8d791ae..998cda7d 100644 --- a/tests/utils/test_stats.py +++ b/tests/utils/test_stats.py @@ -67,7 +67,7 @@ def sample_with_scalar(np_samples_3): def sample_with_field(np_samples_6): s = Sample() # 1. Initialize the CGNS tree - s.init_tree() + s.init_mesh() # 2. Create a base and a zone s.init_base(topological_dim=3, physical_dim=3) s.init_zone(zone_shape=np.array([np_samples_6.shape[0], 0, 0])) From e790a1bbc5f7b5d492932b602e8fa369ac26d84a Mon Sep 17 00:00:00 2001 From: Antithetical Date: Tue, 9 Sep 2025 15:54:15 +0200 Subject: [PATCH 2/7] fix: coverage, lint, and docs build issues --- .../convert_users_data_into_plaid.ipynb | 2 +- docs/source/notebooks/huggingface.ipynb | 4 ++-- docs/source/notebooks/sample.ipynb | 15 ++++-------- src/plaid/containers/sample.py | 23 +++++++++---------- tests/containers/test_sample.py | 2 +- tests/pipelines/conftest.py | 4 ++-- 6 files changed, 22 insertions(+), 28 deletions(-) diff --git a/docs/source/notebooks/convert_users_data_into_plaid.ipynb b/docs/source/notebooks/convert_users_data_into_plaid.ipynb index 6e34d58e..1c97661b 100644 --- a/docs/source/notebooks/convert_users_data_into_plaid.ipynb +++ b/docs/source/notebooks/convert_users_data_into_plaid.ipynb @@ -192,7 +192,7 @@ " # Add CGNS Meshe to samples with specific time steps\n", " sample = Sample()\n", "\n", - sample.add_mesh(cgns_tree) +" sample.add_mesh(cgns_tree)\n", "\n", " # Add random scalar values to the sample\n", diff --git a/docs/source/notebooks/huggingface.ipynb b/docs/source/notebooks/huggingface.ipynb index 30666cc7..336e7651 100644 --- a/docs/source/notebooks/huggingface.ipynb +++ b/docs/source/notebooks/huggingface.ipynb @@ -46,7 +46,7 @@ "# Print Sample util\n", "def show_sample(sample: Sample):\n", " print(f\"sample = {sample}\")\n", - sample.show_mesh() + " sample.show_mesh()", " print(f\"{sample.get_scalar_names() = }\")\n", " print(f\"{sample.get_field_names() = }\")" @@ -93,7 +93,7 @@ "\n", " sample = Sample()\n", "\n", - sample.add_mesh(MeshToCGNS(mesh)) + " sample.add_mesh(MeshToCGNS(mesh))", " sample.add_scalar(\"scalar\", np.random.randn())\n", " sample.add_field(\"node_field\", np.random.rand(1, len(points)), location=\"Vertex\")\n", diff --git a/docs/source/notebooks/sample.ipynb b/docs/source/notebooks/sample.ipynb index 8959d449..983feae4 100644 --- a/docs/source/notebooks/sample.ipynb +++ b/docs/source/notebooks/sample.ipynb @@ -58,8 +58,7 @@ "# Print Sample util\n", "def show_sample(sample: Sample):\n", " print(f\"sample = {sample}\")\n", - sample.show_mesh() - + " sample.show_mesh()\n", " print(f\"{sample.get_scalar_names() = }\")\n", " print(f\"{sample.get_field_names() = }\")" ] @@ -217,8 +216,7 @@ "outputs": [], "source": [ "# Add the previously created CGNS tree to the sample\n", - sample.add_mesh(mesh) - + "sample.add_mesh(tree)\n", "\n", "# Display the Sample CGNS tree\n", "sample.show_mesh()" @@ -263,8 +261,7 @@ "outputs": [], "source": [ "path_linked_sample = Path.cwd() / \"dataset/samples/sample_000000000/meshes/mesh_000000000.cgns\"\n", - new_sample_mult_mesh.link_mesh( - + "new_sample_mult_mesh.link_mesh(\n", " path_linked_sample, linked_sample=sample, linked_time=0.0, time=1.5\n", ")\n", "print(f\"{new_sample_mult_mesh.get_all_mesh_times() = }\")" @@ -491,8 +488,7 @@ "tmp_sample = Sample()\n", "\n", "# Add the previously created CGNS tree in the Sample\n", - tmp_sample.add_mesh(mesh) - + "tmp_sample.add_mesh(tree)\n", "\n", "print(\"element connectivity = \\n\", f\"{tmp_sample.get_elements()}\")" ] @@ -667,8 +663,7 @@ "print(f\"{sample.get_all_mesh_times() = }\")\n", "\n", "# Add one CGNS tree at time 1.\n", - sample.add_mesh(mesh, 1.0) - + "sample.add_mesh(tree, 1.0)\n", "\n", "# After adding new tree\n", "print(f\"{sample.get_all_mesh_times() = }\")" diff --git a/src/plaid/containers/sample.py b/src/plaid/containers/sample.py index 8c9211e6..f0f74813 100644 --- a/src/plaid/containers/sample.py +++ b/src/plaid/containers/sample.py @@ -54,7 +54,7 @@ ) from plaid.utils import cgns_helper as CGH from plaid.utils.base import safe_len -from plaid.utils.deprecation import deprecated, deprecated_argument +from plaid.utils.deprecation import deprecated logger = logging.getLogger(__name__) logging.basicConfig( @@ -536,8 +536,8 @@ def show_mesh(self, time: float = None) -> None: version="0.2.0" ) def show_tree(self, time: float = None) -> None: - """DEPRECATED: use `show_mesh` instead""" - return self.show_mesh(time) + """DEPRECATED: use `show_mesh` instead.""" + return self.show_mesh(time) # pragma: no cover def init_mesh(self, time: float = None) -> CGNSTree: """Initialize a CGNS mesh structure at a specified time step or create a new one if it doesn't exist. @@ -566,8 +566,8 @@ def init_mesh(self, time: float = None) -> CGNSTree: version="0.2.0" ) def init_tree(self, time: float = None) -> CGNSTree: - """DEPRECATED: use `init_mesh` instead""" - return self.init_mesh(time) + """DEPRECATED: use `init_mesh` instead.""" + return self.init_mesh(time) # pragma: no cover def get_mesh( self, time: float = None, apply_links: bool = False, in_memory=False @@ -678,8 +678,7 @@ def add_mesh(self, mesh: CGNSTree, time: float = None) -> CGNSTree: self._links[time] = None if time not in self._paths: # Only initialize if not already present self._paths[time] = None - - # The rest of the logic remains the same + # The rest of the logic remains the same local_bases = self.get_base_names(time=time) base_nodes = CGU.getNodesFromTypeSet(mesh, "CGNSBase_t") for _, node in base_nodes: @@ -707,7 +706,7 @@ def add_mesh(self, mesh: CGNSTree, time: float = None) -> CGNSTree: version="0.2.0" ) def add_tree(self, tree: CGNSTree, time: float = None) -> CGNSTree: - """DEPRECATED: use `add_mesh` instead""" + """DEPRECATED: use `add_mesh` instead.""" return self.add_mesh(tree, time) def del_mesh(self, time: float) -> CGNSTree: @@ -737,8 +736,8 @@ def del_mesh(self, time: float) -> CGNSTree: version="0.2.0" ) def del_tree(self, time: float) -> CGNSTree: - """DEPRECATED: use `del_mesh` instead""" - return self.del_mesh(time) + """DEPRECATED: use `del_mesh` instead.""" + return self.del_mesh(time) # pragma: no cover def link_mesh( self, @@ -866,8 +865,8 @@ def link_tree( linked_time: float, time: float, ) -> CGNSTree: - """DEPRECATED: use `link_mesh` instead""" - return self.link_mesh(path_linked_sample, linked_sample, linked_time, time) + """DEPRECATED: use `link_mesh` instead.""" + return self.link_mesh(path_linked_sample, linked_sample, linked_time, time) # pragma: no cover # -------------------------------------------------------------------------# def get_topological_dim(self, base_name: str = None, time: float = None) -> int: diff --git a/tests/containers/test_sample.py b/tests/containers/test_sample.py index 1b6f9f5d..b2fea19d 100644 --- a/tests/containers/test_sample.py +++ b/tests/containers/test_sample.py @@ -1421,4 +1421,4 @@ def test_check_completeness_with_mesh(self, sample_with_mesh): def test_check_completeness_with_mesh_and_scalar( self, sample_with_mesh_and_scalar_and_time_series ): - print(sample_with_mesh_and_scalar_and_time_series.check_completeness()) \ No newline at end of file + print(sample_with_mesh_and_scalar_and_time_series.check_completeness()) diff --git a/tests/pipelines/conftest.py b/tests/pipelines/conftest.py index fa8153aa..d54f6ea1 100644 --- a/tests/pipelines/conftest.py +++ b/tests/pipelines/conftest.py @@ -56,13 +56,13 @@ def dataset_with_samples_time_series_feat_ids(dataset_with_samples): @pytest.fixture() def dataset_with_samples_with_mesh_field_feat_ids(dataset_with_samples_with_mesh): - return dataset_with_samples_with_tree.get_all_features_identifiers_by_type("field") + return dataset_with_samples_with_mesh.get_all_features_identifiers_by_type("field") @pytest.fixture() def dataset_with_samples_with_mesh_1field_feat_ids(dataset_with_samples_with_mesh): return [ - dataset_with_samples_with_tree.get_all_features_identifiers_by_type("field")[0] + dataset_with_samples_with_mesh.get_all_features_identifiers_by_type("field")[0] ] From 407d11af85c258104d7fe56847777bee04e8a645 Mon Sep 17 00:00:00 2001 From: Antithetical Date: Tue, 9 Sep 2025 16:00:43 +0200 Subject: [PATCH 3/7] fix: sample.py formatting --- src/plaid/containers/sample.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/plaid/containers/sample.py b/src/plaid/containers/sample.py index f0f74813..6c5515cf 100644 --- a/src/plaid/containers/sample.py +++ b/src/plaid/containers/sample.py @@ -533,11 +533,11 @@ def show_mesh(self, time: float = None) -> None: @deprecated( "The `show_tree` method is deprecated, use `show_mesh` instead.", - version="0.2.0" + version="0.2.0", ) def show_tree(self, time: float = None) -> None: """DEPRECATED: use `show_mesh` instead.""" - return self.show_mesh(time) # pragma: no cover + return self.show_mesh(time) # pragma: no cover def init_mesh(self, time: float = None) -> CGNSTree: """Initialize a CGNS mesh structure at a specified time step or create a new one if it doesn't exist. @@ -563,11 +563,11 @@ def init_mesh(self, time: float = None) -> CGNSTree: @deprecated( "The `init_tree` method is deprecated, use `init_mesh` instead.", - version="0.2.0" + version="0.2.0", ) def init_tree(self, time: float = None) -> CGNSTree: """DEPRECATED: use `init_mesh` instead.""" - return self.init_mesh(time) # pragma: no cover + return self.init_mesh(time) # pragma: no cover def get_mesh( self, time: float = None, apply_links: bool = False, in_memory=False @@ -669,16 +669,16 @@ def add_mesh(self, mesh: CGNSTree, time: float = None) -> CGNSTree: time = self.get_time_assignment(time) if self._meshes is None: - self._meshes = {} # Initialize as empty dict + self._meshes = {} # Initialize as empty dict self._links = {} # Initialize as empty dict self._paths = {} # Initialize as empty dict self._meshes[time] = mesh - if time not in self._links: # Only initialize if not already present + if time not in self._links: # Only initialize if not already present self._links[time] = None - if time not in self._paths: # Only initialize if not already present + if time not in self._paths: # Only initialize if not already present self._paths[time] = None - # The rest of the logic remains the same + # The rest of the logic remains the same local_bases = self.get_base_names(time=time) base_nodes = CGU.getNodesFromTypeSet(mesh, "CGNSBase_t") for _, node in base_nodes: @@ -702,8 +702,7 @@ def add_mesh(self, mesh: CGNSTree, time: float = None) -> CGNSTree: return self._meshes[time] @deprecated( - "The `add_tree` method is deprecated, use `add_mesh` instead.", - version="0.2.0" + "The `add_tree` method is deprecated, use `add_mesh` instead.", version="0.2.0" ) def add_tree(self, tree: CGNSTree, time: float = None) -> CGNSTree: """DEPRECATED: use `add_mesh` instead.""" @@ -732,12 +731,11 @@ def del_mesh(self, time: float) -> CGNSTree: return self._meshes.pop(time) @deprecated( - "The `del_tree` method is deprecated, use `del_mesh` instead.", - version="0.2.0" + "The `del_tree` method is deprecated, use `del_mesh` instead.", version="0.2.0" ) def del_tree(self, time: float) -> CGNSTree: """DEPRECATED: use `del_mesh` instead.""" - return self.del_mesh(time) # pragma: no cover + return self.del_mesh(time) # pragma: no cover def link_mesh( self, @@ -856,7 +854,7 @@ def find_feature_roots(sample: Sample, time: float, Type_t: str): @deprecated( "The `link_tree` method is deprecated, use `link_mesh` instead.", - version="0.2.0" + version="0.2.0", ) def link_tree( self, @@ -866,7 +864,9 @@ def link_tree( time: float, ) -> CGNSTree: """DEPRECATED: use `link_mesh` instead.""" - return self.link_mesh(path_linked_sample, linked_sample, linked_time, time) # pragma: no cover + return self.link_mesh( + path_linked_sample, linked_sample, linked_time, time + ) # pragma: no cover # -------------------------------------------------------------------------# def get_topological_dim(self, base_name: str = None, time: float = None) -> int: From d43ca58d0c545124361c80f460068b3f5edf8b84 Mon Sep 17 00:00:00 2001 From: Antithetical Date: Thu, 11 Sep 2025 15:47:17 +0200 Subject: [PATCH 4/7] fix: update version depreciation for sample methods & remove new methods in benchmarks --- CHANGELOG.md | 2 +- .../convert_to_rectilinear_grid.py | 4 ++-- .../2D_MultiScHypEl/prepare_2D_MultiScHypEl.py | 2 +- benchmarks/FNO/2D_profile/prepare_2d_profile.py | 2 +- benchmarks/FNO/Rotor37/prepare_rotor37.py | 2 +- benchmarks/FNO/Tensile2d/prepare_tensile2d.py | 2 +- benchmarks/FNO/VKI-LS59/prepare_vki.py | 2 +- src/plaid/containers/sample.py | 17 ++++++++++++----- 8 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d683a169..68ca97a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,7 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Methods: `add_tree` → `add_mesh`, `del_tree` → `del_mesh`, `show_tree` → `show_mesh`, `init_tree` → `init_mesh`, `link_tree` → `link_mesh` - Fixtures: `sample_with_tree` → `sample_with_mesh`, `tree` → `mesh`, etc. - - Updated usage in tests, docs, examples, and benchmarks + - Updated usage in tests, docs and examples - Deprecated old `tree` methods for backward compatibility - Fixed `_links` and `_paths` initialization in `add_mesh` to prevent `KeyError` diff --git a/benchmarks/FNO/2D_ElPlDynamics/convert_to_rectilinear_grid.py b/benchmarks/FNO/2D_ElPlDynamics/convert_to_rectilinear_grid.py index 06c0ca80..63ab6f51 100644 --- a/benchmarks/FNO/2D_ElPlDynamics/convert_to_rectilinear_grid.py +++ b/benchmarks/FNO/2D_ElPlDynamics/convert_to_rectilinear_grid.py @@ -72,7 +72,7 @@ # Building a new sample new_sample = Sample() tree = MeshToCGNS(ref_mesh) - new_sample.add_mesh(tree, time=0) + new_sample.add_tree(tree, time=0) ux = sample.get_field(name="U_x", zone_name="Zone", base_name="Base_2_3", time=0) @@ -128,7 +128,7 @@ uy = old_mesh.nodeFields["U_y"] path_linked_sample = os.path.join( rect_dataset_path, f"dataset/samples/sample_{sample_index:09d}/meshes/mesh_{0:09d}.cgns") - new_sample.link_mesh( + new_sample.link_tree( path_linked_sample, linked_sample=new_sample, linked_time=0, diff --git a/benchmarks/FNO/2D_MultiScHypEl/prepare_2D_MultiScHypEl.py b/benchmarks/FNO/2D_MultiScHypEl/prepare_2D_MultiScHypEl.py index 9f71f9e6..7556376a 100644 --- a/benchmarks/FNO/2D_MultiScHypEl/prepare_2D_MultiScHypEl.py +++ b/benchmarks/FNO/2D_MultiScHypEl/prepare_2D_MultiScHypEl.py @@ -86,7 +86,7 @@ def compute_signed_distance(mesh,eval_points): new_sample=Sample() tree = MeshToCGNS(rec_mesh) - new_sample.add_mesh(tree,time=0) + new_sample.add_tree(tree,time=0) if sample_index in ids_train: diff --git a/benchmarks/FNO/2D_profile/prepare_2d_profile.py b/benchmarks/FNO/2D_profile/prepare_2d_profile.py index 9e54a8d3..33e20ac6 100644 --- a/benchmarks/FNO/2D_profile/prepare_2d_profile.py +++ b/benchmarks/FNO/2D_profile/prepare_2d_profile.py @@ -79,7 +79,7 @@ def compute_signed_distance(mesh,eval_points): new_sample=Sample() tree = MeshToCGNS(rec_mesh) - new_sample.add_mesh(tree,time=0) + new_sample.add_tree(tree,time=0) if sample_index in ids_train: diff --git a/benchmarks/FNO/Rotor37/prepare_rotor37.py b/benchmarks/FNO/Rotor37/prepare_rotor37.py index 89cae516..8bc6f8ae 100644 --- a/benchmarks/FNO/Rotor37/prepare_rotor37.py +++ b/benchmarks/FNO/Rotor37/prepare_rotor37.py @@ -84,7 +84,7 @@ def compute_signed_distance(mesh,eval_points): new_sample=Sample() tree = MeshToCGNS(ref_mesh) - new_sample.add_mesh(tree) + new_sample.add_tree(tree) if sample_index in ids_train: diff --git a/benchmarks/FNO/Tensile2d/prepare_tensile2d.py b/benchmarks/FNO/Tensile2d/prepare_tensile2d.py index 8b470562..d90d1d46 100644 --- a/benchmarks/FNO/Tensile2d/prepare_tensile2d.py +++ b/benchmarks/FNO/Tensile2d/prepare_tensile2d.py @@ -81,7 +81,7 @@ def compute_signed_distance(mesh,eval_points): new_sample=Sample() tree = MeshToCGNS(rec_mesh) - new_sample.add_mesh(tree,time=0) + new_sample.add_tree(tree,time=0) if sample_index in ids_train: diff --git a/benchmarks/FNO/VKI-LS59/prepare_vki.py b/benchmarks/FNO/VKI-LS59/prepare_vki.py index 3d917e74..d6f722e4 100644 --- a/benchmarks/FNO/VKI-LS59/prepare_vki.py +++ b/benchmarks/FNO/VKI-LS59/prepare_vki.py @@ -49,7 +49,7 @@ tree = MeshToCGNS(rec_mesh) new_sample = Sample() - new_sample.add_mesh(tree) + new_sample.add_tree(tree) if sample_index in ids_train: scalar_names = in_scalars_names + out_scalars_names diff --git a/src/plaid/containers/sample.py b/src/plaid/containers/sample.py index 6c5515cf..09982d90 100644 --- a/src/plaid/containers/sample.py +++ b/src/plaid/containers/sample.py @@ -533,7 +533,8 @@ def show_mesh(self, time: float = None) -> None: @deprecated( "The `show_tree` method is deprecated, use `show_mesh` instead.", - version="0.2.0", + version="0.1.8", + removal="0.2", ) def show_tree(self, time: float = None) -> None: """DEPRECATED: use `show_mesh` instead.""" @@ -563,7 +564,8 @@ def init_mesh(self, time: float = None) -> CGNSTree: @deprecated( "The `init_tree` method is deprecated, use `init_mesh` instead.", - version="0.2.0", + version="0.1.8", + removal="0.2", ) def init_tree(self, time: float = None) -> CGNSTree: """DEPRECATED: use `init_mesh` instead.""" @@ -702,7 +704,9 @@ def add_mesh(self, mesh: CGNSTree, time: float = None) -> CGNSTree: return self._meshes[time] @deprecated( - "The `add_tree` method is deprecated, use `add_mesh` instead.", version="0.2.0" + "The `add_tree` method is deprecated, use `add_mesh` instead.", + version="0.1.8", + removal="0.2", ) def add_tree(self, tree: CGNSTree, time: float = None) -> CGNSTree: """DEPRECATED: use `add_mesh` instead.""" @@ -731,7 +735,9 @@ def del_mesh(self, time: float) -> CGNSTree: return self._meshes.pop(time) @deprecated( - "The `del_tree` method is deprecated, use `del_mesh` instead.", version="0.2.0" + "The `del_tree` method is deprecated, use `del_mesh` instead.", + version="0.1.8", + removal="0.2", ) def del_tree(self, time: float) -> CGNSTree: """DEPRECATED: use `del_mesh` instead.""" @@ -854,7 +860,8 @@ def find_feature_roots(sample: Sample, time: float, Type_t: str): @deprecated( "The `link_tree` method is deprecated, use `link_mesh` instead.", - version="0.2.0", + version="0.1.8", + removal="0.2", ) def link_tree( self, From 12fdbb9798a1407a52bef06a86df299561ae022a Mon Sep 17 00:00:00 2001 From: Antithetical Date: Mon, 15 Sep 2025 15:07:51 +0200 Subject: [PATCH 5/7] fix: add_mesh refactor for key error fix --- CHANGELOG.md | 1 - src/plaid/containers/sample.py | 33 ++++++++++++++++----------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68ca97a1..31f39b39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixtures: `sample_with_tree` → `sample_with_mesh`, `tree` → `mesh`, etc. - Updated usage in tests, docs and examples - Deprecated old `tree` methods for backward compatibility -- Fixed `_links` and `_paths` initialization in `add_mesh` to prevent `KeyError` ### Fixes diff --git a/src/plaid/containers/sample.py b/src/plaid/containers/sample.py index 09982d90..87a85f36 100644 --- a/src/plaid/containers/sample.py +++ b/src/plaid/containers/sample.py @@ -671,25 +671,24 @@ def add_mesh(self, mesh: CGNSTree, time: float = None) -> CGNSTree: time = self.get_time_assignment(time) if self._meshes is None: - self._meshes = {} # Initialize as empty dict - self._links = {} # Initialize as empty dict - self._paths = {} # Initialize as empty dict - - self._meshes[time] = mesh - if time not in self._links: # Only initialize if not already present + self._meshes = {time: mesh} + self._links = {time: None} + self._paths = {time: None} + elif time not in self._meshes: + self._meshes[time] = mesh self._links[time] = None - if time not in self._paths: # Only initialize if not already present self._paths[time] = None - # The rest of the logic remains the same - local_bases = self.get_base_names(time=time) - base_nodes = CGU.getNodesFromTypeSet(mesh, "CGNSBase_t") - for _, node in base_nodes: - if node[__NAME__] not in local_bases: # pragma: no cover - self._meshes[time][__CHILDREN__].append(node) - else: - logger.warning( - f"base <{node[__NAME__]}> already exists in self._meshes --> ignored" - ) + else: + # TODO: handle merging bases with same names + recursive node merge + local_bases = self.get_base_names(time=time) + base_nodes = CGU.getNodesFromTypeSet(mesh, "CGNSBase_t") + for _, node in base_nodes: + if node[__NAME__] not in local_bases: # pragma: no cover + self._meshes[time][__CHILDREN__].append(node) + else: + logger.warning( + f"base <{node[__NAME__]}> already exists in self._meshes --> ignored" + ) base_names = self.get_base_names(time=time) for base_name in base_names: From 79278805eb678ccc245cfd77dd9b4b762ccfff4d Mon Sep 17 00:00:00 2001 From: Antithetical Date: Mon, 15 Sep 2025 15:32:11 +0200 Subject: [PATCH 6/7] fix: unstaged file fix --- examples/containers/sample_example.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/examples/containers/sample_example.py b/examples/containers/sample_example.py index 427f72c6..a88bf321 100644 --- a/examples/containers/sample_example.py +++ b/examples/containers/sample_example.py @@ -47,13 +47,8 @@ # %% # Print Sample util def show_sample(sample: Sample): -<<<<<<< HEAD print(f"{sample = }") sample.show_mesh() -======= - print(f"sample = {sample}") - sample.show_tree() ->>>>>>> main print(f"{sample.get_scalar_names() = }") print(f"{sample.get_field_names() = }") @@ -165,9 +160,7 @@ def show_sample(sample: Sample): new_sample_mult_mesh.set_meshes(meshes_dict) print(f"{new_sample_mult_mesh.get_all_mesh_times() = }") -<<<<<<< HEAD # new_sample_mult_mesh.show_mesh(1.) -======= # %% [markdown] # ### Link tree from another sample @@ -178,7 +171,6 @@ def show_sample(sample: Sample): path_linked_sample, linked_sample=sample, linked_time=0.0, time=1.5 ) print(f"{new_sample_mult_mesh.get_all_mesh_times() = }") ->>>>>>> main # %% [markdown] # ## Section 2: Accessing and Modifying Sample Data From 260216553ce17444ca9ea542c6f7eaa038140780 Mon Sep 17 00:00:00 2001 From: Antithetical Date: Tue, 16 Sep 2025 14:58:03 +0200 Subject: [PATCH 7/7] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor:=20Rename=20m?= =?UTF-8?q?ethods=20for=20clarity=20(#195)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Renamed Dataset/Sample methods: - from_tabular → add_features_from_tabular - from_features_identifier → extract_dataset_from_identifier - from_features_identifier → extract_sample_from_identifier --- CHANGELOG.md | 4 + examples/pipelines/pipeline_example.py | 2 +- src/plaid/containers/dataset.py | 55 +++++++++++--- src/plaid/containers/sample.py | 14 +++- src/plaid/pipelines/plaid_blocks.py | 6 +- src/plaid/pipelines/sklearn_block_wrappers.py | 6 +- tests/containers/test_dataset.py | 18 ++--- tests/containers/test_sample.py | 73 +++++++++++-------- 8 files changed, 123 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8ed54c1..2aad5c87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Move to Muscat=2.5.0 (for tests and examples support) - Update repo configuration (actions: rely more on pypi dependencies, action versions) - Rename types to remove `Type` from name of types: https://github.com/PLAID-lib/plaid/pull/164 +- Refactored method names for improved clarity: + - `Dataset.from_tabular` → `Dataset.add_features_from_tabular` + - `Dataset.from_features_identifier` → `Dataset.extract_dataset_from_identifier` + - `Sample.from_features_identifier` → `Sample.extract_sample_from_identifier` - Refactored all `tree` methods, fixtures, and examples to use `mesh` instead: - Methods: `add_tree` → `add_mesh`, `del_tree` → `del_mesh`, `show_tree` → `show_mesh`, `init_tree` → `init_mesh`, `link_tree` → `link_mesh` diff --git a/examples/pipelines/pipeline_example.py b/examples/pipelines/pipeline_example.py index abe64123..43af723e 100644 --- a/examples/pipelines/pipeline_example.py +++ b/examples/pipelines/pipeline_example.py @@ -114,7 +114,7 @@ # In this example, we aim to predict the ``mach`` field based on two input scalars ``angle_in`` and ``mach_out``, and the mesh node coordinates. To contain memory consumption, we restrict the dataset to the features required for this example: # %% -dataset_train = dataset_train.from_features_identifier(all_feature_id) +dataset_train = dataset_train.extract_dataset_from_identifier(all_feature_id) print("dataset_train:", dataset_train) print("scalar names =", dataset_train.get_scalar_names()) print("field names =", dataset_train.get_field_names()) diff --git a/src/plaid/containers/dataset.py b/src/plaid/containers/dataset.py index 38f9bb96..ecf27554 100644 --- a/src/plaid/containers/dataset.py +++ b/src/plaid/containers/dataset.py @@ -34,6 +34,7 @@ from plaid.containers.utils import check_features_size_homogeneity from plaid.types import Array, Feature, FeatureIdentifier from plaid.utils.base import DeprecatedError, ShapeError, generate_random_ASCII +from plaid.utils.deprecation import deprecated logger = logging.getLogger(__name__) logging.basicConfig( @@ -652,7 +653,7 @@ def update_features_from_identifier( ) return dataset - def from_features_identifier( + def extract_dataset_from_identifier( self, feature_identifiers: Union[FeatureIdentifier, list[FeatureIdentifier]], ) -> Self: @@ -674,10 +675,24 @@ def from_features_identifier( dataset.set_infos(copy.deepcopy(self.get_infos())) for id in self.get_sample_ids(): - extracted_sample = self[id].from_features_identifier(feature_identifiers) + extracted_sample = self[id].extract_sample_from_identifier( + feature_identifiers + ) dataset.add_sample(sample=extracted_sample, id=id) return dataset + @deprecated( + "Use extract_dataset_from_identifier() instead", + version="0.1.8", + removal="0.2", + ) + def from_features_identifier( + self, + feature_identifiers: Union[FeatureIdentifier, list[FeatureIdentifier]], + ) -> Self: + """DEPRECATED: Use extract_dataset_from_identifier() instead.""" + return self.extract_dataset_from_identifier(feature_identifiers) + def get_tabular_from_homogeneous_identifiers( self, feature_identifiers: list[FeatureIdentifier], @@ -734,22 +749,28 @@ def get_tabular_from_stacked_identifiers( return tabular - def from_tabular( + def add_features_from_tabular( self, tabular: Array, feature_identifiers: Union[FeatureIdentifier, list[FeatureIdentifier]], restrict_to_features: bool = True, ) -> Self: - """Generates a dataset from tabular data and feature_identifiers. + """Add or update features in the dataset from tabular data using feature identifiers. + + This method takes tabular data and applies it to the dataset, either by updating existing features + or adding new ones based on the provided feature identifiers. The method can either: + 1. Extract only the specified features and return a new dataset with just those features (if restrict_to_features=True) + 2. Update the specified features in the current dataset while keeping all other existing features (if restrict_to_features=False) Parameters: tabular (Array): of size (nb_sample, nb_features) or (nb_sample, nb_features, dim_feature) if dim_feature>1 - feature_identifiers (dict or list of dict): One or more feature identifiers. - extract_features (bool, optional): If True, only returns the features from feature identifiers, otherwise keep the other features as well + feature_identifiers (dict or list of dict): One or more feature identifiers specifying which features to update/add. + restrict_to_features (bool, optional): If True, only returns the features from feature identifiers, otherwise keep the other features as well. Defaults to True. Returns: - Self - A new dataset defined from tabular data and feature_identifiers. + Self: A new dataset with features updated/added from the tabular data. If restrict_to_features=True, + contains only the specified features. If restrict_to_features=False, contains all original + features plus the updated/added ones. Raises: AssertionError @@ -765,7 +786,7 @@ def from_tabular( features = {id: tabular[i] for i, id in enumerate(self.get_sample_ids())} if restrict_to_features: - dataset = self.from_features_identifier(feature_identifiers) + dataset = self.extract_dataset_from_identifier(feature_identifiers) dataset.update_features_from_identifier( feature_identifiers=feature_identifiers, features=features, @@ -780,6 +801,22 @@ def from_tabular( return dataset + @deprecated( + "Use add_features_from_tabular() instead", + version="0.1.8", + removal="0.2", + ) + def from_tabular( + self, + tabular: Array, + feature_identifiers: Union[FeatureIdentifier, list[FeatureIdentifier]], + restrict_to_features: bool = True, + ) -> Self: + """DEPRECATED: Use add_features_from_tabular() instead.""" + return self.add_features_from_tabular( + tabular, feature_identifiers, restrict_to_features + ) + # -------------------------------------------------------------------------# def add_info(self, cat_key: str, info_key: str, info: str) -> None: """Add information to the :class:`Dataset `, overwriting existing information if there's a conflict. diff --git a/src/plaid/containers/sample.py b/src/plaid/containers/sample.py index 87a85f36..663faf3d 100644 --- a/src/plaid/containers/sample.py +++ b/src/plaid/containers/sample.py @@ -2080,7 +2080,7 @@ def update_features_from_identifier( return sample - def from_features_identifier( + def extract_sample_from_identifier( self, feature_identifiers: Union[FeatureIdentifier, list[FeatureIdentifier]], ) -> Self: @@ -2132,6 +2132,18 @@ def from_features_identifier( return sample + @deprecated( + "Use extract_sample_from_identifier() instead", + version="0.1.8", + removal="0.2", + ) + def from_features_identifier( + self, + feature_identifiers: Union[FeatureIdentifier, list[FeatureIdentifier]], + ) -> Self: + """DEPRECATED: Use extract_sample_from_identifier() instead.""" + return self.extract_sample_from_identifier(feature_identifiers) + def merge_features(self, sample: Self, in_place: bool = False) -> Self: """Merge features from another sample into the current sample. diff --git a/src/plaid/pipelines/plaid_blocks.py b/src/plaid/pipelines/plaid_blocks.py index f9fe210e..10e09fd6 100644 --- a/src/plaid/pipelines/plaid_blocks.py +++ b/src/plaid/pipelines/plaid_blocks.py @@ -96,7 +96,7 @@ def fit(self, dataset: Dataset, _y=None) -> Self: if isinstance(transformer, Pipeline) else transformer.in_features_identifiers ) - sub_dataset = dataset.from_features_identifier(in_feat_id) + sub_dataset = dataset.extract_dataset_from_identifier(in_feat_id) transformer_ = clone(transformer).fit(sub_dataset) self.transformers_.append((name, transformer_, "_")) @@ -136,7 +136,7 @@ def transform(self, dataset: Dataset) -> Dataset: if isinstance(transformer_, Pipeline) else transformer_.in_features_identifiers_ ) - sub_dataset = dataset.from_features_identifier(in_feat_id) + sub_dataset = dataset.extract_dataset_from_identifier(in_feat_id) transformed = transformer_.transform(sub_dataset) transformed_datasets.append(transformed) return Dataset.merge_dataset_by_features(transformed_datasets) @@ -174,7 +174,7 @@ def inverse_transform(self, dataset: Dataset) -> Dataset: if isinstance(transformer_, Pipeline) else transformer_.in_features_identifiers_ ) - sub_dataset = dataset.from_features_identifier(in_feat_id) + sub_dataset = dataset.extract_dataset_from_identifier(in_feat_id) transformed = transformer_.inverse_transform(sub_dataset) transformed_datasets.append(transformed) return Dataset.merge_dataset_by_features(transformed_datasets) diff --git a/src/plaid/pipelines/sklearn_block_wrappers.py b/src/plaid/pipelines/sklearn_block_wrappers.py index 01588421..c045d85c 100644 --- a/src/plaid/pipelines/sklearn_block_wrappers.py +++ b/src/plaid/pipelines/sklearn_block_wrappers.py @@ -141,7 +141,7 @@ def transform(self, dataset: Dataset) -> Dataset: (len(dataset), len(self.out_features_identifiers_), -1) ) - dataset_transformed = dataset.from_tabular( + dataset_transformed = dataset.add_features_from_tabular( X_transformed, self.out_features_identifiers_, restrict_to_features=False ) @@ -168,7 +168,7 @@ def inverse_transform(self, dataset: Dataset) -> Dataset: (len(dataset), len(self.in_features_identifiers_), -1) ) - dataset_inv_transformed = dataset.from_tabular( + dataset_inv_transformed = dataset.add_features_from_tabular( X_inv_transformed, self.in_features_identifiers_, restrict_to_features=False ) @@ -236,7 +236,7 @@ def predict(self, dataset: Dataset) -> Dataset: y = self.sklearn_block_.predict(X) y = y.reshape((len(dataset), len(self.out_features_identifiers_), -1)) - dataset_predicted = dataset.from_tabular( + dataset_predicted = dataset.add_features_from_tabular( y, self.out_features_identifiers_, restrict_to_features=False ) diff --git a/tests/containers/test_dataset.py b/tests/containers/test_dataset.py index 2e1196d9..119cdf32 100644 --- a/tests/containers/test_dataset.py +++ b/tests/containers/test_dataset.py @@ -612,17 +612,17 @@ def test_update_features_from_identifier( in_place=True, ) - def test_from_features_identifier( + def test_extract_dataset_from_identifier( self, dataset_with_samples, dataset_with_samples_with_mesh ): - dataset_with_samples.from_features_identifier( + dataset_with_samples.extract_dataset_from_identifier( feature_identifiers={"type": "scalar", "name": "test_scalar"}, ) - dataset_with_samples.from_features_identifier( + dataset_with_samples.extract_dataset_from_identifier( feature_identifiers={"type": "time_series", "name": "test_time_series_1"}, ) - dataset_with_samples_with_mesh.from_features_identifier( + dataset_with_samples_with_mesh.extract_dataset_from_identifier( feature_identifiers={ "type": "field", "name": "test_node_field_1", @@ -633,7 +633,7 @@ def test_from_features_identifier( }, ) - dataset_with_samples_with_mesh.from_features_identifier( + dataset_with_samples_with_mesh.extract_dataset_from_identifier( feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, {"type": "nodes"}, @@ -714,14 +714,14 @@ def test_get_tabular_from_homogeneous_identifiers_inconsistent_features_through_ ], ) - def test_from_tabular(self, dataset_with_samples_with_mesh): + def test_add_features_from_tabular(self, dataset_with_samples_with_mesh): X = dataset_with_samples_with_mesh.get_tabular_from_homogeneous_identifiers( feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, {"type": "field", "name": "OriginalIds"}, ], ) - dataset = dataset_with_samples_with_mesh.from_tabular( + dataset = dataset_with_samples_with_mesh.add_features_from_tabular( tabular=X, feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, @@ -753,7 +753,7 @@ def test_from_tabular(self, dataset_with_samples_with_mesh): {"type": "field", "name": "OriginalIds"}, ], ) - dataset = dataset_with_samples_with_mesh.from_tabular( + dataset = dataset_with_samples_with_mesh.add_features_from_tabular( tabular=X, feature_identifiers=[ {"type": "field", "name": "test_node_field_1"}, @@ -779,7 +779,7 @@ def test_from_tabular(self, dataset_with_samples_with_mesh): dataset_with_samples_with_mesh[last_index].get_field("test_node_field_1"), ).all() - dataset = dataset_with_samples_with_mesh.from_tabular( + dataset = dataset_with_samples_with_mesh.add_features_from_tabular( tabular=X, feature_identifiers={"type": "field", "name": "OriginalIds"}, ) diff --git a/tests/containers/test_sample.py b/tests/containers/test_sample.py index b2fea19d..1da5ddf6 100644 --- a/tests/containers/test_sample.py +++ b/tests/containers/test_sample.py @@ -1168,54 +1168,67 @@ def test_update_features_from_identifier( ref_2 = sample_.get_field("test_node_field_1") assert np.any(np.isclose(ref_1, ref_2)) - def test_from_features_identifier( + def test_extract_sample_from_identifier( self, sample_with_mesh_and_scalar_and_time_series ): - sample_ = sample_with_mesh_and_scalar_and_time_series.from_features_identifier( - feature_identifiers={"type": "scalar", "name": "test_scalar_1"}, + sample_ = ( + sample_with_mesh_and_scalar_and_time_series.extract_sample_from_identifier( + feature_identifiers={"type": "scalar", "name": "test_scalar_1"}, + ) ) assert sample_.get_scalar_names() == ["test_scalar_1"] assert len(sample_.get_time_series_names()) == 0 assert len(sample_.get_field_names()) == 0 - sample_ = sample_with_mesh_and_scalar_and_time_series.from_features_identifier( - feature_identifiers={"type": "time_series", "name": "test_time_series_1"}, + sample_ = ( + sample_with_mesh_and_scalar_and_time_series.extract_sample_from_identifier( + feature_identifiers={ + "type": "time_series", + "name": "test_time_series_1", + }, + ) ) assert len(sample_.get_scalar_names()) == 0 assert sample_.get_time_series_names() == ["test_time_series_1"] assert len(sample_.get_field_names()) == 0 - sample_ = sample_with_mesh_and_scalar_and_time_series.from_features_identifier( - feature_identifiers={ - "type": "field", - "name": "test_node_field_1", - "base_name": "Base_2_2", - "zone_name": "Zone", - "location": "Vertex", - "time": 0.0, - }, + sample_ = ( + sample_with_mesh_and_scalar_and_time_series.extract_sample_from_identifier( + feature_identifiers={ + "type": "field", + "name": "test_node_field_1", + "base_name": "Base_2_2", + "zone_name": "Zone", + "location": "Vertex", + "time": 0.0, + }, + ) ) assert len(sample_.get_scalar_names()) == 0 assert len(sample_.get_time_series_names()) == 0 assert sample_.get_field_names() == ["test_node_field_1"] - sample_ = sample_with_mesh_and_scalar_and_time_series.from_features_identifier( - feature_identifiers={ - "type": "nodes", - "base_name": "Base_2_2", - "zone_name": "Zone", - "time": 0.0, - }, + sample_ = ( + sample_with_mesh_and_scalar_and_time_series.extract_sample_from_identifier( + feature_identifiers={ + "type": "nodes", + "base_name": "Base_2_2", + "zone_name": "Zone", + "time": 0.0, + }, + ) ) assert len(sample_.get_scalar_names()) == 0 assert len(sample_.get_time_series_names()) == 0 assert len(sample_.get_field_names()) == 0 - sample_ = sample_with_mesh_and_scalar_and_time_series.from_features_identifier( - feature_identifiers=[ - {"type": "field", "name": "test_node_field_1"}, - {"type": "nodes"}, - ], + sample_ = ( + sample_with_mesh_and_scalar_and_time_series.extract_sample_from_identifier( + feature_identifiers=[ + {"type": "field", "name": "test_node_field_1"}, + {"type": "nodes"}, + ], + ) ) assert len(sample_.get_scalar_names()) == 0 assert len(sample_.get_time_series_names()) == 0 @@ -1333,12 +1346,14 @@ def test_merge_features( feat_id = [ fid for fid in feat_id if fid["type"] not in ["scalar", "time_series"] ] - sample_1 = sample_with_mesh_and_scalar_and_time_series.from_features_identifier( - feat_id + sample_1 = ( + sample_with_mesh_and_scalar_and_time_series.extract_sample_from_identifier( + feat_id + ) ) feat_id = sample_with_mesh.get_all_features_identifiers() feat_id = [fid for fid in feat_id if fid["type"] not in ["field", "node"]] - sample_2 = sample_with_mesh.from_features_identifier(feat_id) + sample_2 = sample_with_mesh.extract_sample_from_identifier(feat_id) sample_merge_1 = sample_1.merge_features(sample_2, in_place=False) sample_merge_2 = sample_2.merge_features(sample_1, in_place=False) assert (