Skip to content

Commit 85b3cd4

Browse files
committed
Refactor copyAttribute
1 parent efe5f87 commit 85b3cd4

File tree

2 files changed

+86
-58
lines changed

2 files changed

+86
-58
lines changed

geos-mesh/src/geos/mesh/utils/arrayModifiers.py

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ def copyAttribute(
534534
attributeNameTo: str,
535535
onPoints: bool = False,
536536
logger: Union[ Logger, None ] = None,
537-
) -> bool:
537+
) -> None:
538538
"""Copy an attribute from a multiBlockDataSet to a similar one on the same piece.
539539
540540
Args:
@@ -547,66 +547,46 @@ def copyAttribute(
547547
logger (Union[Logger, None], optional): A logger to manage the output messages.
548548
Defaults to None, an internal logger is used.
549549
550-
Returns:
551-
bool: True if copy successfully ended, False otherwise.
550+
Raises:
551+
TypeError: Error with the type of the mesh from or to.
552+
ValueError: Error with the data of the meshes from and to.
553+
AttributeError: Error with the attribute attributeNameFrom or attributeNameTo.
552554
"""
553555
# Check if an external logger is given.
554556
if logger is None:
555557
logger = getLogger( "copyAttribute", True )
556558

557559
# Check if the multiBlockDataSetFrom is inherited from vtkMultiBlockDataSet.
558560
if not isinstance( multiBlockDataSetFrom, vtkMultiBlockDataSet ):
559-
logger.error( # type: ignore[unreachable]
560-
"multiBlockDataSetFrom has to be inherited from vtkMultiBlockDataSet." )
561-
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
562-
return False
561+
raise TypeError( "Input mesh from has to be inherited from vtkMultiBlockDataSet." )
563562

564563
# Check if the multiBlockDataSetTo is inherited from vtkMultiBlockDataSet.
565564
if not isinstance( multiBlockDataSetTo, vtkMultiBlockDataSet ):
566-
logger.error( # type: ignore[unreachable]
567-
"multiBlockDataSetTo has to be inherited from vtkMultiBlockDataSet." )
568-
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
569-
return False
565+
raise TypeError( "Input mesh to has to be inherited from vtkMultiBlockDataSet." )
570566

571567
# Check if the attribute exist in the multiBlockDataSetFrom.
572568
if not isAttributeInObjectMultiBlockDataSet( multiBlockDataSetFrom, attributeNameFrom, onPoints ):
573-
logger.error( f"The attribute { attributeNameFrom } is not in the multiBlockDataSetFrom." )
574-
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
575-
return False
569+
raise AttributeError( f"The attribute { attributeNameFrom } is not present in the mesh from." )
576570

577571
# Check if the attribute already exist in the multiBlockDataSetTo.
578572
if isAttributeInObjectMultiBlockDataSet( multiBlockDataSetTo, attributeNameTo, onPoints ):
579-
logger.error( f"The attribute { attributeNameTo } is already in the multiBlockDataSetTo." )
580-
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
581-
return False
573+
raise AttributeError( f"The attribute { attributeNameTo } is already present in the mesh to." )
582574

583575
# Check if the two multiBlockDataSets are similar.
584576
elementaryBlockIndexesTo: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSetTo )
585577
elementaryBlockIndexesFrom: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSetFrom )
586578
if elementaryBlockIndexesTo != elementaryBlockIndexesFrom:
587-
logger.error( "multiBlockDataSetFrom and multiBlockDataSetTo do not have the same block indexes." )
588-
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
589-
return False
579+
raise ValueError( "The two meshes do not have the same block indexes." )
590580

591581
# Parse blocks of the two mesh to copy the attribute.
592582
for idBlock in elementaryBlockIndexesTo:
593583
dataSetFrom: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSetFrom.GetDataSet( idBlock ) )
594-
if dataSetFrom is None:
595-
logger.error( f"Block { idBlock } of multiBlockDataSetFrom is null." ) # type: ignore[unreachable]
596-
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
597-
return False
598-
599584
dataSetTo: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSetTo.GetDataSet( idBlock ) )
600-
if dataSetTo is None:
601-
logger.error( f"Block { idBlock } of multiBlockDataSetTo is null." ) # type: ignore[unreachable]
602-
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
603-
return False
604585

605-
if isAttributeInObjectDataSet( dataSetFrom, attributeNameFrom, onPoints ) and \
606-
not copyAttributeDataSet( dataSetFrom, dataSetTo, attributeNameFrom, attributeNameTo, onPoints, logger ):
607-
return False
586+
if isAttributeInObjectDataSet( dataSetFrom, attributeNameFrom, onPoints ):
587+
copyAttributeDataSet( dataSetFrom, dataSetTo, attributeNameFrom, attributeNameTo, onPoints, logger )
608588

609-
return True
589+
return
610590

611591

612592
def copyAttributeDataSet(
@@ -616,7 +596,7 @@ def copyAttributeDataSet(
616596
attributeNameTo: str,
617597
onPoints: bool = False,
618598
logger: Union[ Logger, Any ] = None,
619-
) -> bool:
599+
) -> None:
620600
"""Copy an attribute from a dataSet to a similar one on the same piece.
621601
622602
Args:
@@ -629,43 +609,29 @@ def copyAttributeDataSet(
629609
logger (Union[Logger, None], optional): A logger to manage the output messages.
630610
Defaults to None, an internal logger is used.
631611
632-
Returns:
633-
bool: True if copy successfully ended, False otherwise.
612+
Raises:
613+
TypeError: Error with the type of the mesh from.
614+
AttributeError: Error with the attribute attributeNameFrom.
634615
"""
635616
# Check if an external logger is given.
636617
if logger is None:
637618
logger = getLogger( "copyAttributeDataSet", True )
638619

639620
# Check if the dataSetFrom is inherited from vtkDataSet.
640621
if not isinstance( dataSetFrom, vtkDataSet ):
641-
logger.error( "dataSetFrom has to be inherited from vtkDataSet." ) # type: ignore[unreachable]
642-
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
643-
return False
644-
645-
# Check if the dataSetTo is inherited from vtkDataSet.
646-
if not isinstance( dataSetTo, vtkDataSet ):
647-
logger.error( "dataSetTo has to be inherited from vtkDataSet." ) # type: ignore[unreachable]
648-
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
649-
return False
622+
raise TypeError( "Input mesh from has to be inherited from vtkDataSet." )
650623

651624
# Check if the attribute exist in the dataSetFrom.
652625
if not isAttributeInObjectDataSet( dataSetFrom, attributeNameFrom, onPoints ):
653-
logger.error( f"The attribute { attributeNameFrom } is not in the dataSetFrom." )
654-
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
655-
return False
656-
657-
# Check if the attribute already exist in the dataSetTo.
658-
if isAttributeInObjectDataSet( dataSetTo, attributeNameTo, onPoints ):
659-
logger.error( f"The attribute { attributeNameTo } is already in the dataSetTo." )
660-
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
661-
return False
626+
raise AttributeError( f"The attribute { attributeNameFrom } is not in the input mesh from." )
662627

663628
npArray: npt.NDArray[ Any ] = getArrayInObject( dataSetFrom, attributeNameFrom, onPoints )
664629
componentNames: tuple[ str, ...] = getComponentNamesDataSet( dataSetFrom, attributeNameFrom, onPoints )
665630
vtkArrayType: int = getVtkArrayTypeInObject( dataSetFrom, attributeNameFrom, onPoints )
666631

667632
createAttribute( dataSetTo, npArray, attributeNameTo, componentNames, onPoints, vtkArrayType, logger )
668-
return True
633+
634+
return
669635

670636

671637
def transferAttributeToDataSetWithElementMap(

geos-mesh/tests/test_arrayModifiers.py

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,7 @@ def test_copyAttribute(
534534
multiBlockDataSetTo: vtkMultiBlockDataSet = dataSetTest( "emptymultiblock" )
535535

536536
# Copy the attribute from the multiBlockDataSetFrom to the multiBlockDataSetTo.
537-
assert arrayModifiers.copyAttribute( multiBlockDataSetFrom, multiBlockDataSetTo, attributeNameFrom, attributeNameTo,
538-
onPoints )
537+
arrayModifiers.copyAttribute( multiBlockDataSetFrom, multiBlockDataSetTo, attributeNameFrom, attributeNameTo, onPoints )
539538

540539
# Parse the two multiBlockDataSet and test if the attribute has been copied.
541540
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSetFrom )
@@ -556,6 +555,49 @@ def test_copyAttribute(
556555
assert attributeExistCopied == attributeExistTest
557556

558557

558+
@pytest.mark.parametrize( "meshNameFrom, meshNameTo", [
559+
( "dataset", "emptydataset" ),
560+
( "dataset", "emptymultiblock" ),
561+
( "multiblock", "emptydataset" ),
562+
] )
563+
def test_copyAttributeTypeError(
564+
dataSetTest: Any,
565+
meshNameFrom: str,
566+
meshNameTo: str,
567+
) -> None:
568+
"""Test the raises TypeError for the function copyAttribute."""
569+
meshFrom: Union[ vtkDataSet, vtkMultiBlockDataSet ] = dataSetTest( meshNameFrom )
570+
meshTo: Union[ vtkDataSet, vtkMultiBlockDataSet ] = dataSetTest( meshNameTo )
571+
with pytest.raises( TypeError ):
572+
arrayModifiers.copyAttribute( meshFrom, meshTo, "PORO", "PORO" )
573+
574+
575+
def test_copyAttributeValueError(
576+
dataSetTest: vtkMultiBlockDataSet,
577+
) -> None:
578+
"""Test the raises ValueError for the function copyAttribute with two meshes with different block architecture."""
579+
meshFrom: vtkMultiBlockDataSet = dataSetTest( "meshGeosExtractBlockTmp" )
580+
meshTo: vtkMultiBlockDataSet = dataSetTest( "emptymultiblock" )
581+
with pytest.raises( ValueError ):
582+
arrayModifiers.copyAttribute( meshFrom, meshTo, "PORO", "PORO" )
583+
584+
585+
@pytest.mark.parametrize( "attributeNameFrom, attributeNameTo", [
586+
( "PORO", "PORO" ), # An attribute PORO is already present in the mesh to
587+
( "newAttribute", "newAttribute" ), # newAttribute is not in the mesh from
588+
] )
589+
def test_copyAttributeAttributeError(
590+
dataSetTest: vtkMultiBlockDataSet,
591+
attributeNameFrom: str,
592+
attributeNameTo: str,
593+
) -> None:
594+
"""Test the raises AttributeError for the function copyAttribute."""
595+
meshFrom: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
596+
meshTo: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
597+
with pytest.raises( AttributeError ):
598+
arrayModifiers.copyAttribute( meshFrom, meshTo, attributeNameFrom, attributeNameTo )
599+
600+
559601
@pytest.mark.parametrize( "attributeNameFrom, attributeNameTo, onPoints", [
560602
( "CellAttribute", "CellAttributeTo", False ),
561603
( "PointAttribute", "PointAttributeTo", True ),
@@ -571,7 +613,7 @@ def test_copyAttributeDataSet(
571613
dataSetTo: vtkDataSet = dataSetTest( "emptydataset" )
572614

573615
# Copy the attribute from the dataSetFrom to the dataSetTo.
574-
assert arrayModifiers.copyAttributeDataSet( dataSetFrom, dataSetTo, attributeNameFrom, attributeNameTo, onPoints )
616+
arrayModifiers.copyAttributeDataSet( dataSetFrom, dataSetTo, attributeNameFrom, attributeNameTo, onPoints )
575617

576618
# Get the tested attribute and its copy.
577619
dataFrom: Union[ vtkPointData, vtkCellData ]
@@ -607,6 +649,26 @@ def test_copyAttributeDataSet(
607649
assert vtkDataTypeCopied == vtkDataTypeTest
608650

609651

652+
def test_copyAttributeDataSetTypeError(
653+
dataSetTest: Any,
654+
) -> None:
655+
"""Test the raises TypeError for the function copyAttributeDataSet with a mesh from with a wrong type."""
656+
meshFrom: vtkMultiBlockDataSet = dataSetTest( "multiblock" )
657+
meshTo: vtkDataSet = dataSetTest( "emptydataset" )
658+
with pytest.raises( TypeError ):
659+
arrayModifiers.copyAttributeDataSet( meshFrom, meshTo, "PORO", "PORO" )
660+
661+
662+
def test_copyAttributeDataSetAttributeError(
663+
dataSetTest: vtkDataSet,
664+
) -> None:
665+
"""Test the raises AttributeError for the function copyAttributeDataSet with an attributeNameFrom not in the mesh From."""
666+
meshFrom: vtkDataSet = dataSetTest( "dataset" )
667+
meshTo: vtkDataSet = dataSetTest( "emptydataset" )
668+
with pytest.raises( AttributeError ):
669+
arrayModifiers.copyAttributeDataSet( meshFrom, meshTo, "newAttribute", "newAttribute" )
670+
671+
610672
@pytest.mark.parametrize( "meshFromName, meshToName, attributeName, onPoints, defaultValueTest", [
611673
( "fracture", "emptyFracture", "collocated_nodes", True, [ -1, -1 ] ),
612674
( "multiblock", "emptyFracture", "FAULT", False, -1 ),

0 commit comments

Comments
 (0)