Skip to content

Commit 2a57050

Browse files
feat: adding SISO decorator (#151)
* decorator and precommit recipe * ordering filter and others to avoid segfault * some inheritance * make it a class !! * stay generic in Protocol * SISOFilter for all * some more constraints * Fix for PVPythonViewConfigurator to handle new MultiblockDataSet input. Can still crash in some cases and will need separate PR to deal with it. --------- Co-authored-by: alexbenedicto <alexandre.benedicto@external.totalenergies.com>
1 parent fdbbf80 commit 2a57050

10 files changed

Lines changed: 344 additions & 440 deletions

.hooks/pre-commit.example

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash -e
2+
3+
function run_precommit_checks
4+
{
5+
echo "running mypy"
6+
python -m mypy --config-file ./.mypy.ini --check-untyped-defs $1
7+
echo "running ruff"
8+
python -m ruff check --unsafe-fixes --config .ruff.toml $1
9+
echo "running yapf"
10+
python -m yapf -r -i --style .style.yapf $1
11+
12+
return 0
13+
}
14+
15+
16+
source ${ENV_PYTHON}/bin/activate
17+
18+
#sphinx-build -b html docs/ docs/_build -W
19+
for file in $(git diff --name-only --cached | grep -e "modified\|added" | grep py)
20+
do
21+
run_precommit_checks $file
22+
done
23+

geos-pv/src/geos/pv/plugins/PVClipToMainFrame.py

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,16 @@
44
# ruff: noqa: E402 # disable Module level import not at top of file
55
import sys
66
from pathlib import Path
7-
from typing import Union
87

98
from paraview.util.vtkAlgorithm import ( # type: ignore[import-not-found]
10-
VTKPythonAlgorithmBase, smdomain, smhint, smproperty, smproxy,
9+
VTKPythonAlgorithmBase,
1110
) # source: https://github.com/Kitware/ParaView/blob/master/Wrapping/Python/paraview/util/vtkAlgorithm.py
1211
from paraview.detail.loghandler import ( # type: ignore[import-not-found]
1312
VTKHandler,
1413
) # source: https://github.com/Kitware/ParaView/blob/master/Wrapping/Python/paraview/detail/loghandler.py
1514

1615
from vtkmodules.vtkCommonDataModel import (
17-
vtkMultiBlockDataSet,
18-
vtkUnstructuredGrid,
19-
)
20-
21-
from vtkmodules.vtkCommonCore import (
22-
vtkInformation,
23-
vtkInformationVector,
24-
)
16+
vtkMultiBlockDataSet, )
2517

2618
# update sys.path to load all GEOS Python Package dependencies
2719
geos_pv_path: Path = Path( __file__ ).parent.parent.parent.parent.parent
@@ -30,6 +22,7 @@
3022

3123
update_paths()
3224

25+
from geos.pv.utils.details import SISOFilter, FilterCategory
3326
from geos.mesh.processing.ClipToMainFrame import ClipToMainFrame
3427

3528
__doc__ = """
@@ -43,67 +36,28 @@
4336
"""
4437

4538

46-
@smproxy.filter( name="PVClipToMainFrame", label="Clip to the main frame" )
47-
@smhint.xml( '<ShowInMenu category="4- Geos Utils"/>' )
48-
@smproperty.input( name="Input", port_index=0 )
49-
@smdomain.datatype(
50-
dataTypes=[ "vtkMultiBlockDataSet", "vtkUnstructuredGrid" ],
51-
composite_data_supported=True,
52-
)
39+
@SISOFilter( category=FilterCategory.GEOS_UTILS,
40+
decoratedLabel="Clip to the main frame",
41+
decoratedType=[ "vtkMultiBlockDataSet", "vtkDataSet" ] )
5342
class PVClipToMainFrame( VTKPythonAlgorithmBase ):
5443

5544
def __init__( self ) -> None:
5645
"""Init motherclass, filter and logger."""
57-
VTKPythonAlgorithmBase.__init__( self,
58-
nInputPorts=1,
59-
nOutputPorts=1,
60-
inputType="vtkDataObject",
61-
outputType="vtkDataObject" )
62-
6346
self._realFilter = ClipToMainFrame( speHandler=True )
6447
if not self._realFilter.logger.hasHandlers():
6548
self._realFilter.SetLoggerHandler( VTKHandler() )
6649

67-
#ensure I/O consistency
68-
def RequestDataObject( self, request: vtkInformation, inInfoVec: list[ vtkInformationVector ],
69-
outInfoVec: vtkInformationVector ) -> int:
70-
"""Inherited from VTKPythonAlgorithmBase::RequestDataObject.
50+
def Filter( self, inputMesh: vtkMultiBlockDataSet, outputMesh: vtkMultiBlockDataSet ) -> None:
51+
"""Is applying CreateConstantAttributePerRegion filter.
7152
7253
Args:
73-
request (vtkInformation): request
74-
inInfoVec (list[vtkInformationVector]): input objects
75-
outInfoVec (vtkInformationVector): output objects
76-
77-
Returns:
78-
int: 1 if calculation successfully ended, 0 otherwise.
54+
inputMesh : A mesh to transform.
55+
outputMesh : A mesh transformed.
7956
"""
80-
inData = self.GetInputData( inInfoVec, 0, 0 )
81-
outData = self.GetOutputData( outInfoVec, 0 )
82-
assert inData is not None
83-
if outData is None or ( not outData.IsA( inData.GetClassName() ) ):
84-
outData = inData.NewInstance()
85-
outInfoVec.GetInformationObject( 0 ).Set( outData.DATA_OBJECT(), outData )
86-
return super().RequestDataObject( request, inInfoVec, outInfoVec ) # type: ignore[no-any-return]
87-
88-
def RequestData( self, request: vtkInformation, inInfo: list[ vtkInformationVector ],
89-
outInfo: vtkInformationVector ) -> int:
90-
"""Inherited from VTKPythonAlgorithmBase::RequestData. Apply ClipToMainFrame filter.
91-
92-
Args:
93-
request (vtkInformation): Request
94-
inInfo (list[vtkInformationVector]): Input objects
95-
outInfo (vtkInformationVector): Output objects
96-
97-
Returns:
98-
int: 1 if calculation successfully ended, 0 otherwise.
99-
"""
100-
inputMesh: Union[ vtkMultiBlockDataSet, vtkUnstructuredGrid ] = self.GetInputData( inInfo, 0, 0 )
101-
outputMesh: Union[ vtkMultiBlockDataSet, vtkUnstructuredGrid ] = self.GetOutputData( outInfo, 0 )
102-
10357
# struct
10458
self._realFilter.SetInputData( inputMesh )
10559
self._realFilter.ComputeTransform()
10660
self._realFilter.Update()
10761
outputMesh.ShallowCopy( self._realFilter.GetOutputDataObject( 0 ) )
10862

109-
return 1
63+
return

geos-pv/src/geos/pv/plugins/PVCreateConstantAttributePerRegion.py

Lines changed: 26 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,31 @@
33
# SPDX-FileContributor: Martin Lemay, Romain Baville
44
# ruff: noqa: E402 # disable Module level import not at top of file
55
import sys
6+
import numpy as np
67
from pathlib import Path
78

8-
from typing import Union, Any
9+
from typing import Any
910
from typing_extensions import Self
1011

1112
from paraview.util.vtkAlgorithm import ( # type: ignore[import-not-found]
12-
smdomain, smhint, smproperty, smproxy,
13+
VTKPythonAlgorithmBase, smdomain, smproperty,
1314
) # source: https://github.com/Kitware/ParaView/blob/master/Wrapping/Python/paraview/util/vtkAlgorithm.py
1415
from paraview.detail.loghandler import ( # type: ignore[import-not-found]
1516
VTKHandler,
1617
) # source: https://github.com/Kitware/ParaView/blob/master/Wrapping/Python/paraview/detail/loghandler.py
1718

18-
from vtkmodules.util.vtkAlgorithm import VTKPythonAlgorithmBase
19-
from vtkmodules.vtkCommonCore import (
20-
vtkInformation,
21-
vtkInformationVector,
22-
)
19+
import vtkmodules.util.numpy_support as vnp
20+
2321
from vtkmodules.vtkCommonDataModel import (
24-
vtkMultiBlockDataSet,
25-
vtkDataSet,
26-
)
22+
vtkDataSet, )
2723

2824
# update sys.path to load all GEOS Python Package dependencies
2925
geos_pv_path: Path = Path( __file__ ).parent.parent.parent.parent.parent
3026
sys.path.insert( 0, str( geos_pv_path / "src" ) )
31-
from geos.pv.utils.config import update_paths
3227

33-
update_paths()
28+
from geos.mesh.processing.CreateConstantAttributePerRegion import ( CreateConstantAttributePerRegion )
3429

35-
from geos.mesh.processing.CreateConstantAttributePerRegion import CreateConstantAttributePerRegion, vnp, np
30+
from geos.pv.utils.details import SISOFilter, FilterCategory
3631

3732
__doc__ = """
3833
PVCreateConstantAttributePerRegion is a Paraview plugin that allows to create an attribute
@@ -42,7 +37,7 @@
4237
Input mesh is either vtkMultiBlockDataSet or vtkDataSet and the region attribute must have one component.
4338
The relation index/values is given by a dictionary. Its keys are the indexes and its items are the list of values for each component.
4439
45-
.. Warning::
40+
.. Warning::
4641
The input mesh should contain an attribute corresponding to the regions.
4742
4843
To use it:
@@ -56,22 +51,13 @@
5651
"""
5752

5853

59-
@smproxy.filter(
60-
name="PVCreateConstantAttributePerRegion",
61-
label="Create Constant Attribute Per Region",
62-
)
63-
@smhint.xml( """<ShowInMenu category="0- Geos Pre-processing"/>""" )
64-
@smproperty.input( name="Input", port_index=0 )
65-
@smdomain.datatype(
66-
dataTypes=[ "vtkMultiBlockDataSet", "vtkDataSet" ],
67-
composite_data_supported=True,
68-
)
54+
@SISOFilter( category=FilterCategory.GEOS_PROP,
55+
decoratedLabel="Create Constant Attribute Per Region",
56+
decoratedType=[ "vtkMultiBlockDataSet", "vtkDataSet" ] )
6957
class PVCreateConstantAttributePerRegion( VTKPythonAlgorithmBase ):
7058

7159
def __init__( self: Self ) -> None:
7260
"""Create an attribute with constant value per region."""
73-
super().__init__( nInputPorts=1, nOutputPorts=1, inputType="vtkDataObject", outputType="vtkDataObject" )
74-
7561
self.clearDictRegionValues: bool = True
7662

7763
# Region attribute settings.
@@ -111,7 +97,7 @@ def __init__( self: Self ) -> None:
11197
<NoDefault />
11298
</Hints>
11399
""" )
114-
def _setRegionAttributeName( self: Self, regionName: str ) -> None:
100+
def setRegionAttributeName( self: Self, regionName: str ) -> None:
115101
"""Set region attribute name.
116102
117103
Args:
@@ -124,7 +110,7 @@ def _setRegionAttributeName( self: Self, regionName: str ) -> None:
124110
<StringVectorProperty
125111
name="SetDictRegionValues"
126112
number_of_elements="2"
127-
command="_setDictRegionValues"
113+
command="setDictRegionValues"
128114
repeat_command="1"
129115
number_of_elements_per_command="2">
130116
<Documentation>
@@ -142,7 +128,7 @@ def _setRegionAttributeName( self: Self, regionName: str ) -> None:
142128
</Hints>
143129
</StringVectorProperty>
144130
""" )
145-
def _setDictRegionValues( self: Self, regionIndex: str, value: str ) -> None:
131+
def setDictRegionValues( self: Self, regionIndex: str, value: str ) -> None:
146132
"""Set the dictionary with the region indexes and its corresponding list of values for each components.
147133
148134
Args:
@@ -166,7 +152,7 @@ def _setDictRegionValues( self: Self, regionIndex: str, value: str ) -> None:
166152
<Property name="SetDictRegionValues"/>
167153
</PropertyGroup>
168154
""" )
169-
def _groupRegionAttributeSettingsWidgets( self: Self ) -> None:
155+
def groupRegionAttributeSettingsWidgets( self: Self ) -> None:
170156
"""Group the widgets to set the settings of the region attribute."""
171157
self.Modified()
172158

@@ -183,7 +169,7 @@ def _groupRegionAttributeSettingsWidgets( self: Self ) -> None:
183169
</Documentation>
184170
</StringVectorProperty>
185171
""" )
186-
def _setAttributeName( self: Self, newAttributeName: str ) -> None:
172+
def setAttributeName( self: Self, newAttributeName: str ) -> None:
187173
"""Set attribute name.
188174
189175
Args:
@@ -216,7 +202,7 @@ def _setAttributeName( self: Self, newAttributeName: str ) -> None:
216202
The requested numpy scalar type for values of the new attribute.
217203
</Documentation>
218204
""" )
219-
def _setValueType( self: Self, valueType: int ) -> None:
205+
def setValueType( self: Self, valueType: int ) -> None:
220206
"""Set the type for the value used to create the new attribute.
221207
222208
Args:
@@ -238,7 +224,7 @@ def _setValueType( self: Self, valueType: int ) -> None:
238224
The number of components for the new attribute to create.
239225
</Documentation>
240226
""" )
241-
def _setNbComponent( self: Self, nbComponents: int ) -> None:
227+
def setNbComponent( self: Self, nbComponents: int ) -> None:
242228
"""Set the number of components of the attribute to create.
243229
244230
Args:
@@ -261,7 +247,7 @@ def _setNbComponent( self: Self, nbComponents: int ) -> None:
261247
Names of components: X, Y, Z
262248
</Documentation>
263249
""" )
264-
def _setComponentNames( self: Self, componentNames: str ) -> None:
250+
def setComponentNames( self: Self, componentNames: str ) -> None:
265251
"""Set the names of the components of the attribute to create.
266252
267253
Args:
@@ -283,57 +269,17 @@ def _setComponentNames( self: Self, componentNames: str ) -> None:
283269
<Property name="NumberOfComponents"/>
284270
<Property name="ComponentNames"/>
285271
</PropertyGroup>""" )
286-
def _groupNewAttributeSettingsWidgets( self: Self ) -> None:
272+
def groupNewAttributeSettingsWidgets( self: Self ) -> None:
287273
"""Group the widgets to set the settings of the new attribute."""
288274
self.Modified()
289275

290-
def RequestDataObject(
291-
self: Self,
292-
request: vtkInformation,
293-
inInfoVec: list[ vtkInformationVector ],
294-
outInfoVec: vtkInformationVector,
295-
) -> int:
296-
"""Inherited from VTKPythonAlgorithmBase::RequestDataObject.
276+
def Filter( self, inputMesh: vtkDataSet, outputMesh: vtkDataSet ) -> None:
277+
"""Is applying CreateConstantAttributePerRegion filter.
297278
298279
Args:
299-
request (vtkInformation): request
300-
inInfoVec (list[vtkInformationVector]): input objects
301-
outInfoVec (vtkInformationVector): output objects
302-
303-
Returns:
304-
int: 1 if calculation successfully ended, 0 otherwise.
280+
inputMesh : A mesh to transform
281+
outputMesh : A mesh transformed.
305282
"""
306-
inData = self.GetInputData( inInfoVec, 0, 0 )
307-
outData = self.GetOutputData( outInfoVec, 0 )
308-
assert inData is not None
309-
if outData is None or ( not outData.IsA( inData.GetClassName() ) ):
310-
outData = inData.NewInstance()
311-
outInfoVec.GetInformationObject( 0 ).Set( outData.DATA_OBJECT(), outData )
312-
return super().RequestDataObject( request, inInfoVec, outInfoVec ) # type: ignore[no-any-return]
313-
314-
def RequestData(
315-
self: Self,
316-
request: vtkInformation, # noqa: F841
317-
inInfoVec: list[ vtkInformationVector ], # noqa: F841
318-
outInfoVec: vtkInformationVector, # noqa: F841
319-
) -> int:
320-
"""Inherited from VTKPythonAlgorithmBase::RequestData.
321-
322-
Args:
323-
request (vtkInformation): Request.
324-
inInfoVec (list[vtkInformationVector]): Input objects.
325-
outInfoVec (vtkInformationVector): Output objects.
326-
327-
Returns:
328-
int: 1 if calculation successfully ended, 0 otherwise.
329-
"""
330-
inputMesh: Union[ vtkDataSet, vtkMultiBlockDataSet ] = self.GetInputData( inInfoVec, 0, 0 )
331-
outputMesh: Union[ vtkDataSet, vtkMultiBlockDataSet ] = self.GetOutputData( outInfoVec, 0 )
332-
333-
assert inputMesh is not None, "Input mesh is null."
334-
assert outputMesh is not None, "Output pipeline is null."
335-
336-
outputMesh.ShallowCopy( inputMesh )
337283
filter: CreateConstantAttributePerRegion = CreateConstantAttributePerRegion(
338284
outputMesh,
339285
self.regionName,
@@ -352,4 +298,4 @@ def RequestData(
352298

353299
self.clearDictRegion = True
354300

355-
return 1
301+
return

0 commit comments

Comments
 (0)