|
33 | 33 |
|
34 | 34 | .. code-block:: python |
35 | 35 |
|
| 36 | + import logging |
36 | 37 | from geos.processing.generic_processing_tools.AttributeMapping import AttributeMapping |
37 | 38 |
|
38 | 39 | # Filter inputs. |
|
57 | 58 | attributeMappingFilter.setLoggerHandler( yourHandler ) |
58 | 59 |
|
59 | 60 | # 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 ) |
61 | 68 | """ |
62 | 69 |
|
63 | 70 | loggerTitle: str = "Attribute Mapping" |
@@ -130,70 +137,61 @@ def getElementMap( self: Self ) -> dict[ int, npt.NDArray[ np.int64 ] ]: |
130 | 137 | """ |
131 | 138 | return self.ElementMap |
132 | 139 |
|
133 | | - def applyFilter( self: Self ) -> bool: |
| 140 | + def applyFilter( self: Self ) -> None: |
134 | 141 | """Transfer global attributes from a source mesh to a final mesh. |
135 | 142 |
|
136 | 143 | Mapping the piece of the attributes to transfer. |
137 | 144 |
|
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. |
140 | 148 | """ |
141 | 149 | self.logger.info( f"Apply filter { self.logger.name }." ) |
142 | 150 |
|
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." ) |
146 | 153 |
|
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." ) |
152 | 165 |
|
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: |
156 | 173 | 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." ) |
159 | 175 |
|
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 |
165 | 181 |
|
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 }." ) |
170 | 184 |
|
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 }." ) |
176 | 190 |
|
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() |
179 | 193 |
|
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 |
197 | 195 |
|
198 | 196 | def _logOutputMessage( self: Self ) -> None: |
199 | 197 | """Create and log result messages of the filter.""" |
|
0 commit comments