Skip to content

Commit d54b1c8

Browse files
refactor: refactor vtk filter without try/except scheme (#185)
1 parent 19bbbf6 commit d54b1c8

32 files changed

Lines changed: 878 additions & 646 deletions

geos-processing/src/geos/processing/generic_processing_tools/AttributeMapping.py

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
3434
.. code-block:: python
3535
36+
import logging
3637
from geos.processing.generic_processing_tools.AttributeMapping import AttributeMapping
3738
3839
# Filter inputs.
@@ -57,7 +58,13 @@
5758
attributeMappingFilter.setLoggerHandler( yourHandler )
5859
5960
# Do calculations.
60-
attributeMappingFilter.applyFilter()
61+
try:
62+
attributeMappingFilter.applyFilter()
63+
except( ValueError, AttributeError ) as e:
64+
attributeMappingFilter.logger.error( f"The filter { attributeMappingFilter.logger.name } failed due to: { e }" )
65+
except Exception as e:
66+
mess: str = f"The filter { attributeMappingFilter.logger.name } failed due to: { e }"
67+
attributeMappingFilter.logger.critical( mess, exc_info=True )
6168
"""
6269

6370
loggerTitle: str = "Attribute Mapping"
@@ -130,70 +137,61 @@ def getElementMap( self: Self ) -> dict[ int, npt.NDArray[ np.int64 ] ]:
130137
"""
131138
return self.ElementMap
132139

133-
def applyFilter( self: Self ) -> bool:
140+
def applyFilter( self: Self ) -> None:
134141
"""Transfer global attributes from a source mesh to a final mesh.
135142
136143
Mapping the piece of the attributes to transfer.
137144
138-
Returns:
139-
boolean (bool): True if calculation successfully ended, False otherwise.
145+
Raises:
146+
ValueError: Errors with the input attributeNames or the input mesh.
147+
AttributeError: Errors with the attribute of the mesh.
140148
"""
141149
self.logger.info( f"Apply filter { self.logger.name }." )
142150

143-
try:
144-
if len( self.attributeNames ) == 0:
145-
raise ValueError( f"Please enter at least one { self.piece } attribute to transfer." )
151+
if len( self.attributeNames ) == 0:
152+
raise ValueError( f"Please enter at least one { self.piece } attribute to transfer." )
146153

147-
attributesInMeshFrom: set[ str ] = getAttributeSet( self.meshFrom, self.onPoints )
148-
wrongAttributeNames: set[ str ] = self.attributeNames.difference( attributesInMeshFrom )
149-
if len( wrongAttributeNames ) > 0:
150-
raise AttributeError(
151-
f"The { self.piece } attributes { wrongAttributeNames } are not present in the source mesh." )
154+
attributesInMeshFrom: set[ str ] = getAttributeSet( self.meshFrom, self.onPoints )
155+
wrongAttributeNames: set[ str ] = self.attributeNames.difference( attributesInMeshFrom )
156+
if len( wrongAttributeNames ) > 0:
157+
raise AttributeError(
158+
f"The { self.piece } attributes { wrongAttributeNames } are not present in the source mesh." )
159+
160+
attributesInMeshTo: set[ str ] = getAttributeSet( self.meshTo, self.onPoints )
161+
attributesAlreadyInMeshTo: set[ str ] = self.attributeNames.intersection( attributesInMeshTo )
162+
if len( attributesAlreadyInMeshTo ) > 0:
163+
raise AttributeError(
164+
f"The { self.piece } attributes { attributesAlreadyInMeshTo } are already present in the final mesh." )
152165

153-
attributesInMeshTo: set[ str ] = getAttributeSet( self.meshTo, self.onPoints )
154-
attributesAlreadyInMeshTo: set[ str ] = self.attributeNames.intersection( attributesInMeshTo )
155-
if len( attributesAlreadyInMeshTo ) > 0:
166+
if isinstance( self.meshFrom, vtkMultiBlockDataSet ):
167+
partialAttributes: list[ str ] = []
168+
for attributeName in self.attributeNames:
169+
if not isAttributeGlobal( self.meshFrom, attributeName, self.onPoints ):
170+
partialAttributes.append( attributeName )
171+
172+
if len( partialAttributes ) > 0:
156173
raise AttributeError(
157-
f"The { self.piece } attributes { attributesAlreadyInMeshTo } are already present in the final mesh."
158-
)
174+
f"All { self.piece } attributes to transfer must be global, { partialAttributes } are partials." )
159175

160-
if isinstance( self.meshFrom, vtkMultiBlockDataSet ):
161-
partialAttributes: list[ str ] = []
162-
for attributeName in self.attributeNames:
163-
if not isAttributeGlobal( self.meshFrom, attributeName, self.onPoints ):
164-
partialAttributes.append( attributeName )
176+
self.ElementMap = computeElementMapping( self.meshFrom, self.meshTo, self.onPoints )
177+
sharedElement: bool = False
178+
for key in self.ElementMap:
179+
if np.any( self.ElementMap[ key ] > -1 ):
180+
sharedElement = True
165181

166-
if len( partialAttributes ) > 0:
167-
raise AttributeError(
168-
f"All { self.piece } attributes to transfer must be global, { partialAttributes } are partials."
169-
)
182+
if not sharedElement:
183+
raise ValueError( f"The two meshes do not have any shared { self.piece }." )
170184

171-
self.ElementMap = computeElementMapping( self.meshFrom, self.meshTo, self.onPoints )
172-
sharedElement: bool = False
173-
for key in self.ElementMap:
174-
if np.any( self.ElementMap[ key ] > -1 ):
175-
sharedElement = True
185+
for attributeName in self.attributeNames:
186+
# TODO:: Modify arrayModifiers function to raise error.
187+
if not transferAttributeWithElementMap( self.meshFrom, self.meshTo, self.ElementMap, attributeName,
188+
self.onPoints, self.logger ):
189+
raise ValueError( f"Fail to transfer the attribute { attributeName }." )
176190

177-
if not sharedElement:
178-
raise ValueError( f"The two meshes do not have any shared { self.piece }." )
191+
# Log the output message.
192+
self._logOutputMessage()
179193

180-
for attributeName in self.attributeNames:
181-
# TODO:: Modify arrayModifiers function to raise error.
182-
if not transferAttributeWithElementMap( self.meshFrom, self.meshTo, self.ElementMap, attributeName,
183-
self.onPoints, self.logger ):
184-
raise
185-
186-
# Log the output message.
187-
self._logOutputMessage()
188-
except ( TypeError, ValueError, AttributeError ) as e:
189-
self.logger.error( f"The filter { self.logger.name } failed.\n{ e }" )
190-
return False
191-
except Exception as e:
192-
mess: str = f"The filter { self.logger.name } failed.\n{ e }"
193-
self.logger.critical( mess, exc_info=True )
194-
return False
195-
196-
return True
194+
return
197195

198196
def _logOutputMessage( self: Self ) -> None:
199197
"""Create and log result messages of the filter."""

0 commit comments

Comments
 (0)