From a4ff5e578c357fbca456abcdaa4896a6bef17e46 Mon Sep 17 00:00:00 2001 From: alexbenedicto Date: Thu, 12 Sep 2024 17:25:16 -0700 Subject: [PATCH] Last part of correction review --- .../mesh/doctor/checks/check_fractures.py | 53 +++++++------------ .../mesh/doctor/checks/generate_fractures.py | 6 +-- .../src/geos/mesh/doctor/checks/vtk_utils.py | 43 ++++++--------- geos-mesh/tests/test_generate_fractures.py | 2 +- 4 files changed, 38 insertions(+), 66 deletions(-) diff --git a/geos-mesh/src/geos/mesh/doctor/checks/check_fractures.py b/geos-mesh/src/geos/mesh/doctor/checks/check_fractures.py index 2cd8242..21695ad 100644 --- a/geos-mesh/src/geos/mesh/doctor/checks/check_fractures.py +++ b/geos-mesh/src/geos/mesh/doctor/checks/check_fractures.py @@ -1,30 +1,14 @@ -from dataclasses import dataclass import logging - -from typing import ( - Collection, - FrozenSet, - Iterable, - Sequence, - Set, - Tuple, -) - -from tqdm import tqdm import numpy - -from vtkmodules.vtkCommonDataModel import ( - vtkUnstructuredGrid, - vtkCell, -) -from vtkmodules.vtkCommonCore import ( - vtkPoints, ) -from vtkmodules.vtkIOXML import ( - vtkXMLMultiBlockDataReader, ) -from vtkmodules.util.numpy_support import ( - vtk_to_numpy, ) -from .vtk_utils import ( - vtk_iter, ) +from dataclasses import dataclass +from typing import Collection, Iterable, Sequence +from tqdm import tqdm +from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid, vtkCell +from vtkmodules.vtkCommonCore import vtkPoints +from vtkmodules.vtkIOXML import vtkXMLMultiBlockDataReader +from vtkmodules.util.numpy_support import vtk_to_numpy +from geos.mesh.doctor.checks.vtk_utils import vtk_iter +from geos.mesh.doctor.checks.generate_fractures import Coordinates3D @dataclass( frozen=True ) @@ -44,7 +28,7 @@ class Result: def __read_multiblock( vtk_input_file: str, matrix_name: str, - fracture_name: str ) -> Tuple[ vtkUnstructuredGrid, vtkUnstructuredGrid ]: + fracture_name: str ) -> tuple[ vtkUnstructuredGrid, vtkUnstructuredGrid ]: reader = vtkXMLMultiBlockDataReader() reader.SetFileName( vtk_input_file ) reader.Update() @@ -73,9 +57,9 @@ def format_collocated_nodes( fracture_mesh: vtkUnstructuredGrid ) -> Sequence[ I def __check_collocated_nodes_positions( - matrix_points: Sequence[ Tuple[ float, float, float ] ], fracture_points: Sequence[ Tuple[ float, float, float ] ], - g2l: Sequence[ int ], collocated_nodes: Iterable[ Iterable[ int ] ] -) -> Collection[ Tuple[ int, Iterable[ int ], Iterable[ Tuple[ float, float, float ] ] ] ]: + matrix_points: Sequence[ Coordinates3D ], fracture_points: Sequence[ Coordinates3D ], g2l: Sequence[ int ], + collocated_nodes: Iterable[ Iterable[ int ] ] +) -> Collection[ tuple[ int, Iterable[ int ], Iterable[ Coordinates3D ] ] ]: issues = [] for li, bucket in enumerate( collocated_nodes ): matrix_nodes = ( fracture_points[ li ], ) + tuple( map( lambda gi: matrix_points[ g2l[ gi ] ], bucket ) ) @@ -98,14 +82,14 @@ def my_iter( ccc ): def __check_neighbors( matrix: vtkUnstructuredGrid, fracture: vtkUnstructuredGrid, g2l: Sequence[ int ], collocated_nodes: Sequence[ Iterable[ int ] ] ): - fracture_nodes: Set[ int ] = set() + fracture_nodes: set[ int ] = set() for bucket in collocated_nodes: for gi in bucket: fracture_nodes.add( g2l[ gi ] ) # For each face of each cell, # if all the points of the face are "made" of collocated nodes, # then this is a fracture face. - fracture_faces: Set[ FrozenSet[ int ] ] = set() + fracture_faces: set[ frozenset[ int ] ] = set() for c in range( matrix.GetNumberOfCells() ): cell: vtkCell = matrix.GetCell( c ) for f in range( cell.GetNumberOfFaces() ): @@ -116,7 +100,7 @@ def __check_neighbors( matrix: vtkUnstructuredGrid, fracture: vtkUnstructuredGri # Finding the cells for c in tqdm( range( fracture.GetNumberOfCells() ), desc="Finding neighbor cell pairs" ): cell: vtkCell = fracture.GetCell( c ) - cns: Set[ FrozenSet[ int ] ] = set() # subset of collocated_nodes + cns: set[ frozenset[ int ] ] = set() # subset of collocated_nodes point_ids = frozenset( vtk_iter( cell.GetPointIds() ) ) for point_id in point_ids: bucket = collocated_nodes[ point_id ] @@ -129,9 +113,8 @@ def __check_neighbors( matrix: vtkUnstructuredGrid, fracture: vtkUnstructuredGri if f in fracture_faces: found += 1 if found != 2: - logging.warning( - f"Something went wrong since we should have found 2 fractures faces (we found {found}) for collocated nodes {cns}." - ) + logging.warning( f"Something went wrong since we should have found 2 fractures faces (we found {found})" + + f" for collocated nodes {cns}." ) def __check( vtk_input_file: str, options: Options ) -> Result: diff --git a/geos-mesh/src/geos/mesh/doctor/checks/generate_fractures.py b/geos-mesh/src/geos/mesh/doctor/checks/generate_fractures.py index f7818e0..a6236f6 100644 --- a/geos-mesh/src/geos/mesh/doctor/checks/generate_fractures.py +++ b/geos-mesh/src/geos/mesh/doctor/checks/generate_fractures.py @@ -1,10 +1,10 @@ +import logging +import networkx +import numpy from collections import defaultdict from dataclasses import dataclass -import logging from enum import Enum from tqdm import tqdm -import networkx -import numpy from typing import Collection, Iterable, Mapping, Optional, Sequence, TypeAlias from vtk import vtkDataArray from vtkmodules.vtkCommonCore import vtkIdList, vtkPoints diff --git a/geos-mesh/src/geos/mesh/doctor/checks/vtk_utils.py b/geos-mesh/src/geos/mesh/doctor/checks/vtk_utils.py index 2345e32..3b581b7 100644 --- a/geos-mesh/src/geos/mesh/doctor/checks/vtk_utils.py +++ b/geos-mesh/src/geos/mesh/doctor/checks/vtk_utils.py @@ -1,25 +1,11 @@ -from dataclasses import dataclass import os.path import logging -import sys -from typing import ( - Any, - Iterator, - Optional, -) - -from vtkmodules.vtkCommonCore import ( - vtkIdList, ) -from vtkmodules.vtkCommonDataModel import ( - vtkUnstructuredGrid, ) -from vtkmodules.vtkIOLegacy import ( - vtkUnstructuredGridWriter, - vtkUnstructuredGridReader, -) -from vtkmodules.vtkIOXML import ( - vtkXMLUnstructuredGridReader, - vtkXMLUnstructuredGridWriter, -) +from dataclasses import dataclass +from typing import Iterator, Optional +from vtkmodules.vtkCommonCore import vtkIdList +from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid +from vtkmodules.vtkIOLegacy import vtkUnstructuredGridWriter, vtkUnstructuredGridReader +from vtkmodules.vtkIOXML import vtkXMLUnstructuredGridReader, vtkXMLUnstructuredGridWriter @dataclass( frozen=True ) @@ -36,7 +22,7 @@ def to_vtk_id_list( data ) -> vtkIdList: return result -def vtk_iter( l ) -> Iterator[ Any ]: +def vtk_iter( l ) -> Iterator[ any ]: """ Utility function transforming a vtk "container" (e.g. vtkIdList) into an iterable to be used for building built-ins python containers. :param l: A vtk container. @@ -116,8 +102,9 @@ def read_mesh( vtk_input_file: str ) -> vtkUnstructuredGrid: :return: A unstructured grid. """ if not os.path.exists( vtk_input_file ): - logging.critical( f"Invalid file path. COuld not read \"{vtk_input_file}\". Dying..." ) - sys.exit( 1 ) + err_msg: str = f"Invalid file path. Could not read \"{vtk_input_file}\". Dying..." + logging.error( err_msg ) + raise ValueError( err_msg ) file_extension = os.path.splitext( vtk_input_file )[ -1 ] extension_to_reader = { ".vtk": __read_vtk, ".vtu": __read_vtu } # Testing first the reader that should match @@ -131,8 +118,9 @@ def read_mesh( vtk_input_file: str ) -> vtkUnstructuredGrid: if output_mesh: return output_mesh # No reader did work. Dying. - logging.critical( f"Could not find the appropriate VTK reader for file \"{vtk_input_file}\". Dying..." ) - sys.exit( 1 ) + err_msg = f"Could not find the appropriate VTK reader for file \"{vtk_input_file}\". Dying..." + logging.error( err_msg ) + raise ValueError( err_msg ) def __write_vtk( mesh: vtkUnstructuredGrid, output: str ) -> int: @@ -170,6 +158,7 @@ def write_mesh( mesh: vtkUnstructuredGrid, vtk_output: VtkOutput ) -> int: success_code = __write_vtu( mesh, vtk_output.output, vtk_output.is_data_mode_binary ) else: # No writer found did work. Dying. - logging.critical( f"Could not find the appropriate VTK writer for extension \"{file_extension}\". Dying..." ) - sys.exit( 1 ) + err_msg = f"Could not find the appropriate VTK writer for extension \"{file_extension}\". Dying..." + logging.error( err_msg ) + raise ValueError( err_msg ) return 0 if success_code else 2 # the Write member function return 1 in case of success, 0 otherwise. diff --git a/geos-mesh/tests/test_generate_fractures.py b/geos-mesh/tests/test_generate_fractures.py index a0e9cf2..30e0e7d 100644 --- a/geos-mesh/tests/test_generate_fractures.py +++ b/geos-mesh/tests/test_generate_fractures.py @@ -1,7 +1,7 @@ -from dataclasses import dataclass import logging import numpy import pytest +from dataclasses import dataclass from typing import Iterable, Iterator, Sequence, TypeAlias from vtkmodules.vtkCommonDataModel import ( vtkUnstructuredGrid, vtkQuad, VTK_HEXAHEDRON, VTK_POLYHEDRON, VTK_QUAD ) from vtkmodules.util.numpy_support import numpy_to_vtk, vtk_to_numpy