예제 #1
0
    def test_heart1(self):
        """
        Test creation of heart scaffold.
        """
        scaffold = MeshType_3d_heart1
        parameterSetNames = scaffold.getParameterSetNames()
        self.assertEqual(parameterSetNames, [
            "Default", "Human 1", "Mouse 1", "Pig 1", "Rat 1", "Unit Human 1",
            "Unit Mouse 1", "Unit Pig 1", "Unit Rat 1"
        ])
        options = scaffold.getDefaultOptions("Human 1")
        self.assertEqual(123, len(options))
        self.assertEqual(0.9, options.get("LV outer height"))
        self.assertEqual(80.0, options.get("Unit scale"))
        self.assertEqual(7,
                         options.get("Number of elements around LV free wall"))
        self.assertEqual(7,
                         options.get("Number of elements around RV free wall"))
        # simplify atria
        self.assertEqual(8, options.get("Number of elements over atria"))
        options["Number of elements over atria"] = 6
        self.assertEqual(
            2, options.get("Number of elements radial pulmonary vein annuli"))
        options["Number of elements radial pulmonary vein annuli"] = 1
        self.assertFalse(scaffold.checkOptions(options))
        context = Context("Test")
        region = context.getDefaultRegion()
        self.assertTrue(region.isValid())
        annotationGroups = scaffold.generateMesh(region, options)
        self.assertEqual(32, len(annotationGroups))
        fieldmodule = region.getFieldmodule()
        mesh3d = fieldmodule.findMeshByDimension(3)
        self.assertEqual(332, mesh3d.getSize())
        mesh2d = fieldmodule.findMeshByDimension(2)
        self.assertEqual(1287, mesh2d.getSize())
        mesh1d = fieldmodule.findMeshByDimension(1)
        self.assertEqual(1567, mesh1d.getSize())
        nodes = fieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        self.assertEqual(614, nodes.getSize())
        datapoints = fieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        # check coordinates range, epicardium surface area and volume
        coordinates = fieldmodule.findFieldByName(
            "coordinates").castFiniteElement()
        self.assertTrue(coordinates.isValid())
        minimums, maximums = evaluateFieldNodesetRange(coordinates, nodes)
        assertAlmostEqualList(self, minimums,
                              [-50.7876375290527, -58.42494589146976, -91.6],
                              1.0E-6)
        assertAlmostEqualList(
            self, maximums,
            [43.810947610743156, 39.03925080604259, 42.018608492002784],
            1.0E-6)
        with ChangeManager(fieldmodule):
            one = fieldmodule.createFieldConstant(1.0)
            epicardiumGroup = fieldmodule.findFieldByName(
                'epicardium').castGroup()
            self.assertTrue(epicardiumGroup.isValid())
            epicardiumMeshGroup = epicardiumGroup.getFieldElementGroup(
                mesh2d).getMeshGroup()
            self.assertTrue(epicardiumMeshGroup.isValid())
            surfaceAreaField = fieldmodule.createFieldMeshIntegral(
                one, coordinates, epicardiumMeshGroup)
            surfaceAreaField.setNumbersOfPoints(4)
            volumeField = fieldmodule.createFieldMeshIntegral(
                one, coordinates, mesh3d)
            volumeField.setNumbersOfPoints(3)
        fieldcache = fieldmodule.createFieldcache()
        result, surfaceArea = surfaceAreaField.evaluateReal(fieldcache, 1)
        self.assertEqual(result, RESULT_OK)
        self.assertAlmostEqual(surfaceArea, 36541.36513577538, delta=1.0E-2)
        result, volume = volumeField.evaluateReal(fieldcache, 1)
        self.assertEqual(result, RESULT_OK)
        self.assertAlmostEqual(volume, 218014.81436425756, delta=1.0E-1)

        # check some annotationGroups:
        expectedSizes3d = {
            "left ventricle myocardium": 110,
            "right ventricle myocardium": 95,
            "interventricular septum": 37,
            "left atrium myocardium": 88,
            "right atrium myocardium": 80,
            "interatrial septum": 23
        }
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_heart_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name], size, name)
        expectedSizes2d = {
            "endocardium of left ventricle": 88,
            "endocardium of right ventricle": 73,
            "endocardium of left atrium": 82,
            "endocardium of right atrium": 74,
            "epicardium": 229
        }
        for name in expectedSizes2d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_heart_term(name))
            size = group.getMeshGroup(mesh2d).getSize()
            self.assertEqual(expectedSizes2d[name], size, name)

        # test finding a marker in scaffold
        markerGroup = fieldmodule.findFieldByName("marker").castGroup()
        markerNodes = markerGroup.getFieldNodeGroup(nodes).getNodesetGroup()
        self.assertEqual(7, markerNodes.getSize())
        markerName = fieldmodule.findFieldByName("marker_name")
        self.assertTrue(markerName.isValid())
        markerLocation = fieldmodule.findFieldByName("marker_location")
        self.assertTrue(markerLocation.isValid())
        # test apex marker point
        cache = fieldmodule.createFieldcache()
        node = findNodeWithName(markerNodes, markerName, "apex of heart")
        self.assertTrue(node.isValid())
        cache.setNode(node)
        element, xi = markerLocation.evaluateMeshLocation(cache, 3)
        self.assertEqual(1, element.getIdentifier())
        assertAlmostEqualList(self, xi, [0.0, 0.0, 1.0], 1.0E-10)
        apexGroup = getAnnotationGroupForTerm(annotationGroups,
                                              get_heart_term("apex of heart"))
        self.assertTrue(apexGroup.getNodesetGroup(nodes).containsNode(node))

        # refine 2x2x2 and check result
        # first remove any face (but not point) annotation groups as they are re-added by defineFaceAnnotations
        removeAnnotationGroups = []
        for annotationGroup in annotationGroups:
            if (not annotationGroup.hasMeshGroup(mesh3d)) and (
                    annotationGroup.hasMeshGroup(mesh2d)
                    or annotationGroup.hasMeshGroup(mesh1d)):
                removeAnnotationGroups.append(annotationGroup)
        for annotationGroup in removeAnnotationGroups:
            annotationGroups.remove(annotationGroup)
        self.assertEqual(23, len(annotationGroups))

        refineRegion = region.createRegion()
        refineFieldmodule = refineRegion.getFieldmodule()
        options['Refine number of elements surface'] = 2
        options['Refine number of elements through LV wall'] = 2
        options['Refine number of elements through wall'] = 2
        meshrefinement = MeshRefinement(region, refineRegion, annotationGroups)
        scaffold.refineMesh(meshrefinement, options)
        annotationGroups = meshrefinement.getAnnotationGroups()

        refineFieldmodule.defineAllFaces()
        oldAnnotationGroups = copy.copy(annotationGroups)
        for annotationGroup in annotationGroups:
            annotationGroup.addSubelements()
        scaffold.defineFaceAnnotations(refineRegion, options, annotationGroups)
        for annotation in annotationGroups:
            if annotation not in oldAnnotationGroups:
                annotationGroup.addSubelements()
        self.assertEqual(32, len(annotationGroups))

        mesh3d = refineFieldmodule.findMeshByDimension(3)
        self.assertEqual(2580, mesh3d.getSize())
        mesh2d = refineFieldmodule.findMeshByDimension(2)
        self.assertEqual(8872, mesh2d.getSize())
        mesh1d = refineFieldmodule.findMeshByDimension(1)
        self.assertEqual(9983, mesh1d.getSize())
        nodes = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        self.assertEqual(3693, nodes.getSize())
        datapoints = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        # check some refined annotationGroups:
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_heart_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name] * 8, size, name)
        for name in expectedSizes2d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_heart_term(name))
            size = group.getMeshGroup(mesh2d).getSize()
            if name == "epicardium":
                # fibrous ring is only refined by 1 through its thickness
                fibrousRingReduction = (
                    options['Refine number of elements surface'] - 1
                ) * options['Refine number of elements surface'] * (
                    options['Number of elements around left atrium free wall']
                    +
                    options['Number of elements around right atrium free wall']
                )
                self.assertEqual(
                    expectedSizes2d[name] * 4 - fibrousRingReduction, size,
                    name)
            else:
                self.assertEqual(expectedSizes2d[name] * 4, size, name)

        # test finding a marker in refined scaffold
        markerGroup = refineFieldmodule.findFieldByName("marker").castGroup()
        refinedNodes = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        markerNodes = markerGroup.getFieldNodeGroup(
            refinedNodes).getNodesetGroup()
        self.assertEqual(7, markerNodes.getSize())
        markerName = refineFieldmodule.findFieldByName("marker_name")
        self.assertTrue(markerName.isValid())
        markerLocation = refineFieldmodule.findFieldByName("marker_location")
        self.assertTrue(markerLocation.isValid())
        # test apex marker point
        cache = refineFieldmodule.createFieldcache()
        node = findNodeWithName(markerNodes, markerName, "apex of heart")
        self.assertTrue(node.isValid())
        cache.setNode(node)
        element, xi = markerLocation.evaluateMeshLocation(cache, 3)
        self.assertEqual(5, element.getIdentifier())
        assertAlmostEqualList(self, xi, [0.0, 0.0, 1.0], 1.0E-10)
        apexGroup = getAnnotationGroupForTerm(annotationGroups,
                                              get_heart_term("apex of heart"))
        self.assertTrue(apexGroup.getNodesetGroup(nodes).containsNode(node))
예제 #2
0
    def _calculateMarkerDataLocations(self):
        """
        Called when markerGroup exists.
        Find matching marker mesh locations for marker data points.
        Only finds matching location where there is one datapoint and one node
        for each name in marker group.
        Defines datapoint group self._markerDataLocationGroup to contain those with locations.
        """
        self._markerDataLocationField = None
        self._markerDataLocationCoordinatesField = None
        self._markerDataDeltaField = None
        self._markerDataLocationGroupField = None
        self._markerDataLocationGroup = None
        if not (self._markerDataGroup and self._markerDataNameField
                and self._markerNodeGroup and self._markerLocationField
                and self._markerNameField):
            return

        markerPrefix = self._markerGroup.getName()
        # assume marker locations are in highest dimension mesh
        mesh = self.getHighestDimensionMesh()
        datapoints = self._fieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_DATAPOINTS)
        meshDimension = mesh.getDimension()
        fieldcache = self._fieldmodule.createFieldcache()
        with ChangeManager(self._fieldmodule):
            self._markerDataLocationField = findOrCreateFieldStoredMeshLocation(
                self._fieldmodule,
                mesh,
                name=markerPrefix + "_data_location_" + mesh.getName(),
                managed=False)
            self._markerDataLocationGroupField = self._fieldmodule.createFieldNodeGroup(
                datapoints)
            self._markerDataLocationGroupField.setName(
                getUniqueFieldName(self._fieldmodule,
                                   markerPrefix + "_data_location_group"))
            self._markerDataLocationGroup = self._markerDataLocationGroupField.getNodesetGroup(
            )
            self._updateMarkerDataLocationCoordinatesField()
            nodetemplate = self._markerDataGroup.createNodetemplate()
            nodetemplate.defineField(self._markerDataLocationField)
            datapointIter = self._markerDataGroup.createNodeiterator()
            datapoint = datapointIter.next()
            while datapoint.isValid():
                fieldcache.setNode(datapoint)
                name = self._markerDataNameField.evaluateString(fieldcache)
                # if this is the only datapoint with name:
                if name and findNodeWithName(self._markerDataGroup,
                                             self._markerDataNameField,
                                             name,
                                             ignore_case=True,
                                             strip_whitespace=True):
                    node = findNodeWithName(self._markerNodeGroup,
                                            self._markerNameField,
                                            name,
                                            ignore_case=True,
                                            strip_whitespace=True)
                    if node:
                        fieldcache.setNode(node)
                        element, xi = self._markerLocationField.evaluateMeshLocation(
                            fieldcache, meshDimension)
                        if element.isValid():
                            datapoint.merge(nodetemplate)
                            fieldcache.setNode(datapoint)
                            self._markerDataLocationField.assignMeshLocation(
                                fieldcache, element, xi)
                            self._markerDataLocationGroup.addNode(datapoint)
                datapoint = datapointIter.next()
        # Warn about datapoints without a location in model
        markerDataGroupSize = self._markerDataGroup.getSize()
        markerDataLocationGroupSize = self._markerDataLocationGroup.getSize()
        markerNodeGroupSize = self._markerNodeGroup.getSize()
        if self.getDiagnosticLevel() > 0:
            if markerDataLocationGroupSize < markerDataGroupSize:
                print("Warning: Only " + str(markerDataLocationGroupSize) +
                      " of " + str(markerDataGroupSize) +
                      " marker data points have model locations")
            if markerDataLocationGroupSize < markerNodeGroupSize:
                print("Warning: Only " + str(markerDataLocationGroupSize) +
                      " of " + str(markerNodeGroupSize) +
                      " marker model locations used")
예제 #3
0
    def test_stomach1(self):
        """
        Test creation of stomach scaffold.
        """
        scaffold = MeshType_3d_stomach1
        parameterSetNames = scaffold.getParameterSetNames()
        self.assertEqual(parameterSetNames,
                         ["Default", "Human 1", "Mouse 1", "Rat 1"])
        options = scaffold.getDefaultOptions("Rat 1")
        self.assertEqual(19, len(options))
        self.assertEqual(12,
                         options.get("Number of elements around esophagus"))
        self.assertEqual(14, options.get("Number of elements around duodenum"))
        self.assertEqual(
            2, options.get("Number of elements between cardia and duodenum"))
        self.assertEqual(1, options.get("Number of elements across cardia"))
        self.assertEqual(0.5, options.get("Wall thickness"))
        self.assertEqual(True, options.get("Limiting ridge"))
        ostiumOptions = options['Gastro-esophagal junction']
        ostiumSettings = ostiumOptions.getScaffoldSettings()
        self.assertEqual(1, ostiumSettings.get("Number of vessels"))
        self.assertEqual(
            12, ostiumSettings.get("Number of elements around ostium"))
        self.assertEqual(4,
                         ostiumSettings.get("Number of elements through wall"))
        self.assertEqual(5.0, ostiumSettings.get("Ostium diameter"))
        self.assertEqual(5.0, ostiumSettings.get("Ostium length"))
        self.assertEqual(0.5, ostiumSettings.get("Ostium wall thickness"))
        self.assertEqual(
            [0.65, 0.12, 0.18, 0.05],
            ostiumSettings.get("Ostium wall relative thicknesses"))
        self.assertEqual(2.0, ostiumSettings.get("Vessel inner diameter"))
        self.assertEqual(0.5, ostiumSettings.get("Vessel wall thickness"))
        self.assertEqual(
            [0.65, 0.12, 0.18, 0.05],
            ostiumSettings.get("Vessel wall relative thicknesses"))
        self.assertEqual(0.0, ostiumSettings.get("Vessel angle 1 degrees"))
        self.assertEqual(
            0.55,
            options.get("Gastro-esophagal junction position along factor"))
        self.assertEqual(0.2, options.get("Cardia derivative factor"))

        context = Context("Test")
        region = context.getDefaultRegion()
        self.assertTrue(region.isValid())
        annotationGroups = scaffold.generateBaseMesh(region, options)
        self.assertEqual(37, len(annotationGroups))

        fieldmodule = region.getFieldmodule()
        self.assertEqual(RESULT_OK, fieldmodule.defineAllFaces())
        mesh3d = fieldmodule.findMeshByDimension(3)
        self.assertEqual(582, mesh3d.getSize())
        mesh2d = fieldmodule.findMeshByDimension(2)
        self.assertEqual(1965, mesh2d.getSize())
        mesh1d = fieldmodule.findMeshByDimension(1)
        self.assertEqual(2195, mesh1d.getSize())
        nodes = fieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        self.assertEqual(830, nodes.getSize())
        datapoints = fieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        coordinates = fieldmodule.findFieldByName(
            "coordinates").castFiniteElement()
        self.assertTrue(coordinates.isValid())
        minimums, maximums = evaluateFieldNodesetRange(coordinates, nodes)
        assertAlmostEqualList(
            self, minimums,
            [-18.238549598577396, -16.033751319943754, -8.905924748773598],
            1.0E-6)
        assertAlmostEqualList(
            self, maximums,
            [18.285156743233415, 15.214807824088728, 8.905433142848109],
            1.0E-6)

        with ChangeManager(fieldmodule):
            one = fieldmodule.createFieldConstant(1.0)
            faceMeshGroup = createFaceMeshGroupExteriorOnFace(
                fieldmodule, Element.FACE_TYPE_XI3_1)
            surfaceAreaField = fieldmodule.createFieldMeshIntegral(
                one, coordinates, faceMeshGroup)
            surfaceAreaField.setNumbersOfPoints(4)
            volumeField = fieldmodule.createFieldMeshIntegral(
                one, coordinates, mesh3d)
            volumeField.setNumbersOfPoints(3)
        fieldcache = fieldmodule.createFieldcache()
        result, surfaceArea = surfaceAreaField.evaluateReal(fieldcache, 1)
        self.assertEqual(result, RESULT_OK)
        self.assertAlmostEqual(surfaceArea, 2557.832902256128, delta=1.0E-6)
        result, volume = volumeField.evaluateReal(fieldcache, 1)
        self.assertEqual(result, RESULT_OK)
        self.assertAlmostEqual(volume, 809.0349219398828, delta=1.0E-6)

        # check some annotationGroups:
        expectedSizes3d = {
            "body of stomach": 112,
            "cardia of stomach": 36,
            "duodenum": 56,
            "esophagus": 96,
            "fundus of stomach": 114,
            "pyloric antrum": 112,
            "pyloric canal": 56,
            "stomach": 582
        }
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_stomach_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name], size, name)

        # refine 4x4x4 and check result
        # first remove any surface annotation groups as they are re-added by defineFaceAnnotations
        removeAnnotationGroups = []
        for annotationGroup in annotationGroups:
            if (not annotationGroup.hasMeshGroup(mesh3d)) and \
                    (annotationGroup.hasMeshGroup(mesh2d) or annotationGroup.hasMeshGroup(mesh1d)):
                removeAnnotationGroups.append(annotationGroup)

        for annotationGroup in removeAnnotationGroups:
            annotationGroups.remove(annotationGroup)
        self.assertEqual(37, len(annotationGroups))

        refineRegion = region.createRegion()
        refineFieldmodule = refineRegion.getFieldmodule()
        options['Refine number of elements surface'] = 4
        options['Refine number of elements through wall'] = 4
        meshrefinement = MeshRefinement(region, refineRegion, annotationGroups)
        scaffold.refineMesh(meshrefinement, options)
        annotationGroups = meshrefinement.getAnnotationGroups()

        refineFieldmodule.defineAllFaces()
        oldAnnotationGroups = copy.copy(annotationGroups)
        for annotationGroup in annotationGroups:
            annotationGroup.addSubelements()
        scaffold.defineFaceAnnotations(refineRegion, options, annotationGroups)
        for annotation in annotationGroups:
            if annotation not in oldAnnotationGroups:
                annotationGroup.addSubelements()
        self.assertEqual(68, len(annotationGroups))
        #
        mesh3d = refineFieldmodule.findMeshByDimension(3)
        self.assertEqual(37248, mesh3d.getSize())
        mesh2d = refineFieldmodule.findMeshByDimension(2)
        self.assertEqual(115248, mesh2d.getSize())
        mesh1d = refineFieldmodule.findMeshByDimension(1)
        self.assertEqual(118796, mesh1d.getSize())
        nodes = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        self.assertEqual(40814, nodes.getSize())
        datapoints = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        # check some refined annotationGroups:
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_stomach_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name] * 64, size, name)

        # test finding a marker in refined scaffold
        markerGroup = refineFieldmodule.findFieldByName("marker").castGroup()
        refinedNodes = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        markerNodes = markerGroup.getFieldNodeGroup(
            refinedNodes).getNodesetGroup()
        self.assertEqual(18, markerNodes.getSize())
        markerName = refineFieldmodule.findFieldByName("marker_name")
        self.assertTrue(markerName.isValid())
        markerLocation = refineFieldmodule.findFieldByName("marker_location")
        self.assertTrue(markerLocation.isValid())
        cache = refineFieldmodule.createFieldcache()
        node = findNodeWithName(
            markerNodes, markerName,
            "esophagogastric junction along the lesser curvature on serosa")
        self.assertTrue(node.isValid())
        cache.setNode(node)
        element, xi = markerLocation.evaluateMeshLocation(cache, 3)
        self.assertEqual(5821, element.getIdentifier())
        assertAlmostEqualList(self, xi, [0.0, 1.0, 1.0], 1.0E-10)
예제 #4
0
    def test_lung1(self):
        """
        Test creation of heart scaffold.
        """
        scaffold = MeshType_3d_lung1
        parameterSetNames = scaffold.getParameterSetNames()
        self.assertEqual(parameterSetNames, [ "Default", "Human 1", "Mouse 1", "Pig 1", "Rat 1" ])
        options = scaffold.getDefaultOptions(["Human 1"])
        self.assertEqual(3, len(options))
        self.assertFalse(scaffold.checkOptions(options))

        context = Context("Test")
        region = context.getDefaultRegion()
        self.assertTrue(region.isValid())
        annotationGroups = scaffold.generateMesh(region, options)
        self.assertEqual(20, len(annotationGroups))
        fieldmodule = region.getFieldmodule()
        mesh3d = fieldmodule.findMeshByDimension(3)
        self.assertEqual(88, mesh3d.getSize())
        mesh2d = fieldmodule.findMeshByDimension(2)
        self.assertEqual(292, mesh2d.getSize())
        mesh1d = fieldmodule.findMeshByDimension(1)
        self.assertEqual(334, mesh1d.getSize())
        nodes = fieldmodule.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_NODES)
        self.assertEqual(141, nodes.getSize())
        datapoints = fieldmodule.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        # check coordinates range, outside surface area and volume
        coordinates = fieldmodule.findFieldByName("coordinates").castFiniteElement()
        self.assertTrue(coordinates.isValid())
        minimums, maximums = evaluateFieldNodesetRange(coordinates, nodes)
        assertAlmostEqualList(self, minimums, [ 14.153, 62.444, -353.40564 ], 1.0E-6)
        assertAlmostEqualList(self, maximums, [ 297.502, 245.3003, -23.486 ], 1.0E-6)
        with ChangeManager(fieldmodule):
            one = fieldmodule.createFieldConstant(1.0)
            upperRightFissureGroup = fieldmodule.findFieldByName('upper lobe of right lung').castGroup()
            self.assertTrue(upperRightFissureGroup.isValid())
            upperRightFissureMeshGroup = upperRightFissureGroup.getFieldElementGroup(mesh2d).getMeshGroup()
            self.assertTrue(upperRightFissureMeshGroup.isValid())
            surfaceAreaField = fieldmodule.createFieldMeshIntegral(one, coordinates, upperRightFissureMeshGroup)
            surfaceAreaField.setNumbersOfPoints(4)
            volumeField = fieldmodule.createFieldMeshIntegral(one, coordinates, mesh3d)
            volumeField.setNumbersOfPoints(3)
        fieldcache = fieldmodule.createFieldcache()
        result, surfaceArea = surfaceAreaField.evaluateReal(fieldcache, 1)
        self.assertEqual(result, RESULT_OK)
        self.assertAlmostEqual(surfaceArea, 138894.27893716586, delta=1.0E-2)
        result, volume = volumeField.evaluateReal(fieldcache, 1)
        self.assertEqual(result, RESULT_OK)
        self.assertAlmostEqual(volume, 7225489.982550352, delta=1.0E-2)

        # check some annotationGroups:
        expectedSizes3d = {
            "lung": 88,
            "left lung": 44,
            "right lung": 44,
            "upper lobe of left lung": 24,
            "lower lobe of left lung": 20,
            "upper lobe of right lung": 16,
            "middle lobe of right lung": 8,
            "lower lobe of right lung": 20,
            # "right lung accessory lobe": 0
            }
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups, get_lung_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name], size, name)
        expectedSizes2d = {
            "horizontal fissure of right lung": 4,
            "oblique fissure of left lung": 8,
            "oblique fissure of right lung": 8
            }
        for name in expectedSizes2d:
            group = getAnnotationGroupForTerm(annotationGroups, get_lung_term(name))
            size = group.getMeshGroup(mesh2d).getSize()
            self.assertEqual(expectedSizes2d[name], size, name)

        # test finding a marker in scaffold
        markerGroup = fieldmodule.findFieldByName("marker").castGroup()
        markerNodes = markerGroup.getFieldNodeGroup(nodes).getNodesetGroup()
        self.assertEqual(9, markerNodes.getSize())
        markerName = fieldmodule.findFieldByName("marker_name")
        self.assertTrue(markerName.isValid())
        markerLocation = fieldmodule.findFieldByName("marker_location")
        self.assertTrue(markerLocation.isValid())
        # test apex marker point
        cache = fieldmodule.createFieldcache()
        node = findNodeWithName(markerNodes, markerName, "apex of left lung")
        self.assertTrue(node.isValid())
        cache.setNode(node)
        element, xi = markerLocation.evaluateMeshLocation(cache, 3)
        self.assertEqual(37, element.getIdentifier())
        assertAlmostEqualList(self, xi, [ 0.0, 0.0, 1.0 ], 1.0E-10)

        # refine 4 and check result
        # first remove any face (but not point) annotation groups as they are re-added by defineFaceAnnotations
        removeAnnotationGroups = []
        for annotationGroup in annotationGroups:
            if (not annotationGroup.hasMeshGroup(mesh3d)) and (annotationGroup.hasMeshGroup(mesh2d) or annotationGroup.hasMeshGroup(mesh1d)):
                removeAnnotationGroups.append(annotationGroup)
        for annotationGroup in removeAnnotationGroups:
            annotationGroups.remove(annotationGroup)
        self.assertEqual(17, len(annotationGroups))

        refineRegion = region.createRegion()
        refineFieldmodule = refineRegion.getFieldmodule()
        options['Refine'] = True
        options['Refine number of elements'] = 4
        refineNumberOfElements = options['Refine number of elements']
        meshrefinement = MeshRefinement(region, refineRegion, annotationGroups)
        scaffold.refineMesh(meshrefinement, options)
        annotationGroups = meshrefinement.getAnnotationGroups()

        refineFieldmodule.defineAllFaces()
        oldAnnotationGroups = copy.copy(annotationGroups)
        for annotationGroup in annotationGroups:
            annotationGroup.addSubelements()
        scaffold.defineFaceAnnotations(refineRegion, options, annotationGroups)
        for annotation in annotationGroups:
            if annotation not in oldAnnotationGroups:
                annotationGroup.addSubelements()
        self.assertEqual(20, len(annotationGroups))

        mesh3d = refineFieldmodule.findMeshByDimension(3)
        self.assertEqual(5632, mesh3d.getSize())
        mesh2d = refineFieldmodule.findMeshByDimension(2)
        self.assertEqual(17344, mesh2d.getSize())
        mesh1d = refineFieldmodule.findMeshByDimension(1)
        self.assertEqual(17848, mesh1d.getSize())
        nodes = refineFieldmodule.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_NODES)
        self.assertEqual(6147, nodes.getSize())
        datapoints = refineFieldmodule.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        # check some refined annotationGroups:
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups, get_lung_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name]*(refineNumberOfElements**3), size, name)
        for name in expectedSizes2d:
            group = getAnnotationGroupForTerm(annotationGroups, get_lung_term(name))
            size = group.getMeshGroup(mesh2d).getSize()
            self.assertEqual(expectedSizes2d[name]*(refineNumberOfElements**2), size, name)

        # test finding a marker in refined scaffold
        markerGroup = refineFieldmodule.findFieldByName("marker").castGroup()
        refinedNodes = refineFieldmodule.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_NODES)
        markerNodes = markerGroup.getFieldNodeGroup(refinedNodes).getNodesetGroup()
        self.assertEqual(9, markerNodes.getSize())
        markerName = refineFieldmodule.findFieldByName("marker_name")
        self.assertTrue(markerName.isValid())
        markerLocation = refineFieldmodule.findFieldByName("marker_location")
        self.assertTrue(markerLocation.isValid())
        # test apex marker point
        cache = refineFieldmodule.createFieldcache()
        node = findNodeWithName(markerNodes, markerName, "apex of left lung")
        self.assertTrue(node.isValid())
        cache.setNode(node)
        element, xi = markerLocation.evaluateMeshLocation(cache, 3)
        self.assertEqual(2353, element.getIdentifier())
        assertAlmostEqualList(self, xi, [ 0.0, 0.0, 1.0 ], 1.0E-10)
예제 #5
0
    def test_esophagus1(self):
        """
        Test creation of esophagus scaffold.
        """
        scaffold = MeshType_3d_esophagus1
        parameterSetNames = scaffold.getParameterSetNames()
        self.assertEqual(parameterSetNames, ["Default", "Human 1"])
        options = scaffold.getDefaultOptions("Human 1")
        self.assertEqual(15, len(options))
        self.assertEqual(8, options.get("Number of elements around"))
        self.assertEqual(12, options.get("Number of elements along"))
        self.assertEqual(4, options.get("Number of elements through wall"))
        self.assertEqual(5.0, options.get("Wall thickness"))
        self.assertEqual(0.35, options.get("Mucosa relative thickness"))
        self.assertEqual(0.15, options.get("Submucosa relative thickness"))
        self.assertEqual(
            0.25, options.get("Circular muscle layer relative thickness"))
        self.assertEqual(
            0.25, options.get("Longitudinal muscle layer relative thickness"))

        context = Context("Test")
        region = context.getDefaultRegion()
        self.assertTrue(region.isValid())
        annotationGroups = scaffold.generateBaseMesh(region, options)
        self.assertEqual(12, len(annotationGroups))

        fieldmodule = region.getFieldmodule()
        self.assertEqual(RESULT_OK, fieldmodule.defineAllFaces())
        mesh3d = fieldmodule.findMeshByDimension(3)
        self.assertEqual(384, mesh3d.getSize())
        mesh2d = fieldmodule.findMeshByDimension(2)
        self.assertEqual(1280, mesh2d.getSize())
        mesh1d = fieldmodule.findMeshByDimension(1)
        self.assertEqual(1416, mesh1d.getSize())
        nodes = fieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        self.assertEqual(524, nodes.getSize())
        datapoints = fieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        coordinates = fieldmodule.findFieldByName(
            "coordinates").castFiniteElement()
        self.assertTrue(coordinates.isValid())
        minimums, maximums = evaluateFieldNodesetRange(coordinates, nodes)
        assertAlmostEqualList(
            self, minimums,
            [-17.57777917463216, -9.777573632674187, -14.999995678650333],
            1.0E-6)
        assertAlmostEqualList(
            self, maximums,
            [40.16077850310996, 291.0400575669104, 14.999999571130854], 1.0E-6)

        with ChangeManager(fieldmodule):
            one = fieldmodule.createFieldConstant(1.0)
            faceMeshGroup = createFaceMeshGroupExteriorOnFace(
                fieldmodule, Element.FACE_TYPE_XI3_1)
            surfaceAreaField = fieldmodule.createFieldMeshIntegral(
                one, coordinates, faceMeshGroup)
            surfaceAreaField.setNumbersOfPoints(4)
            volumeField = fieldmodule.createFieldMeshIntegral(
                one, coordinates, mesh3d)
            volumeField.setNumbersOfPoints(3)
        fieldcache = fieldmodule.createFieldcache()
        result, surfaceArea = surfaceAreaField.evaluateReal(fieldcache, 1)
        self.assertEqual(result, RESULT_OK)
        self.assertAlmostEqual(surfaceArea, 27931.063402890817, delta=1.0E-6)
        result, volume = volumeField.evaluateReal(fieldcache, 1)
        self.assertEqual(result, RESULT_OK)
        self.assertAlmostEqual(volume, 116538.12918074048, delta=1.0E-6)

        # check some annotationGroups:
        expectedSizes3d = {
            "abdominal part of esophagus": 64,
            "cervical part of esophagus": 64,
            "esophagus": 384,
            "esophagus mucosa": 96,
            "esophagus smooth muscle circular layer": 96,
            "esophagus smooth muscle longitudinal layer": 96,
            "submucosa of esophagus": 96,
            "thoracic part of esophagus": 256
        }
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_esophagus_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name], size, name)

        # refine 4x4x4 and check result
        # first remove any surface annotation groups as they are re-added by defineFaceAnnotations
        removeAnnotationGroups = []
        for annotationGroup in annotationGroups:
            if (not annotationGroup.hasMeshGroup(mesh3d)) and \
                    (annotationGroup.hasMeshGroup(mesh2d) or annotationGroup.hasMeshGroup(mesh1d)):
                removeAnnotationGroups.append(annotationGroup)

        for annotationGroup in removeAnnotationGroups:
            annotationGroups.remove(annotationGroup)
        self.assertEqual(12, len(annotationGroups))

        refineRegion = region.createRegion()
        refineFieldmodule = refineRegion.getFieldmodule()
        options['Refine number of elements around'] = 4
        options['Refine number of elements along'] = 4
        options['Refine number of elements through wall'] = 4
        meshrefinement = MeshRefinement(region, refineRegion, annotationGroups)
        scaffold.refineMesh(meshrefinement, options)
        annotationGroups = meshrefinement.getAnnotationGroups()

        refineFieldmodule.defineAllFaces()
        oldAnnotationGroups = copy.copy(annotationGroups)
        for annotationGroup in annotationGroups:
            annotationGroup.addSubelements()
        scaffold.defineFaceAnnotations(refineRegion, options, annotationGroups)
        for annotation in annotationGroups:
            if annotation not in oldAnnotationGroups:
                annotationGroup.addSubelements()
        self.assertEqual(13, len(annotationGroups))
        #
        mesh3d = refineFieldmodule.findMeshByDimension(3)
        self.assertEqual(24576, mesh3d.getSize())
        mesh2d = refineFieldmodule.findMeshByDimension(2)
        self.assertEqual(75776, mesh2d.getSize())
        mesh1d = refineFieldmodule.findMeshByDimension(1)
        self.assertEqual(77856, mesh1d.getSize())
        nodes = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        self.assertEqual(26660, nodes.getSize())
        datapoints = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        # check some refined annotationGroups:
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_esophagus_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name] * 64, size, name)

        # test finding a marker in refined scaffold
        markerGroup = refineFieldmodule.findFieldByName("marker").castGroup()
        refinedNodes = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        markerNodes = markerGroup.getFieldNodeGroup(
            refinedNodes).getNodesetGroup()
        self.assertEqual(4, markerNodes.getSize())
        markerName = refineFieldmodule.findFieldByName("marker_name")
        self.assertTrue(markerName.isValid())
        markerLocation = refineFieldmodule.findFieldByName("marker_location")
        self.assertTrue(markerLocation.isValid())
        cache = refineFieldmodule.createFieldcache()
        node = findNodeWithName(
            markerNodes, markerName,
            "distal point of lower esophageal sphincter serosa on the greater curvature of stomach"
        )
        self.assertTrue(node.isValid())
        cache.setNode(node)
        element, xi = markerLocation.evaluateMeshLocation(cache, 3)
        self.assertEqual(24125, element.getIdentifier())
        assertAlmostEqualList(self, xi, [0.0, 1.0, 1.0], 1.0E-10)
예제 #6
0
    def test_bladderurethra1(self):
        """
        Test creation of bladder scaffold.
        """
        scaffold = MeshType_3d_bladderurethra1
        parameterSetNames = MeshType_3d_bladderurethra1.getParameterSetNames()
        self.assertEqual(parameterSetNames,
                         ["Default", "Cat 1", "Rat 1", "Human 1"])
        options = scaffold.getDefaultOptions("Cat 1")
        self.assertEqual(26, len(options))
        self.assertEqual(12, options.get("Number of elements along bladder"))
        self.assertEqual(12, options.get("Number of elements around"))
        self.assertEqual(1, options.get("Number of elements through wall"))
        self.assertEqual(0.5, options.get("Wall thickness"))
        self.assertEqual(45, options.get("Neck angle degrees"))
        ostiumOptions = options['Ureter']
        ostiumSettings = ostiumOptions.getScaffoldSettings()
        self.assertEqual(1, ostiumSettings.get("Number of vessels"))
        self.assertEqual(
            8, ostiumSettings.get("Number of elements around ostium"))
        self.assertEqual(1,
                         ostiumSettings.get("Number of elements through wall"))
        self.assertEqual(2.2, ostiumSettings.get("Ostium diameter"))
        self.assertEqual(0.5, ostiumSettings.get("Ostium length"))
        self.assertEqual(0.5, ostiumSettings.get("Ostium wall thickness"))
        self.assertEqual(0.8, ostiumSettings.get("Vessel inner diameter"))
        self.assertEqual(0.25, ostiumSettings.get("Vessel wall thickness"))
        self.assertEqual(0.0, ostiumSettings.get("Vessel angle 1 degrees"))

        context = Context("Test")
        region = context.getDefaultRegion()
        self.assertTrue(region.isValid())
        annotationGroups = scaffold.generateBaseMesh(region, options)
        self.assertEqual(13, len(annotationGroups))

        fieldmodule = region.getFieldmodule()
        self.assertEqual(RESULT_OK, fieldmodule.defineAllFaces())
        mesh3d = fieldmodule.findMeshByDimension(3)
        self.assertEqual(240, mesh3d.getSize())
        mesh2d = fieldmodule.findMeshByDimension(2)
        self.assertEqual(960, mesh2d.getSize())
        mesh1d = fieldmodule.findMeshByDimension(1)
        self.assertEqual(1201, mesh1d.getSize())
        nodes = fieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        self.assertEqual(487, nodes.getSize())
        datapoints = fieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        coordinates = fieldmodule.findFieldByName(
            "coordinates").castFiniteElement()
        self.assertTrue(coordinates.isValid())
        minimums, maximums = evaluateFieldNodesetRange(coordinates, nodes)
        assertAlmostEqualList(self, minimums,
                              [-15.48570141314588, -12.992184072505665, -0.5],
                              1.0E-6)
        assertAlmostEqualList(
            self, maximums,
            [15.485696373577879, 13.837600783918127, 127.68599990411343],
            1.0E-6)

        with ChangeManager(fieldmodule):
            one = fieldmodule.createFieldConstant(1.0)
            faceMeshGroup = createFaceMeshGroupExteriorOnFace(
                fieldmodule, Element.FACE_TYPE_XI3_1)
            surfaceAreaField = fieldmodule.createFieldMeshIntegral(
                one, coordinates, faceMeshGroup)
            surfaceAreaField.setNumbersOfPoints(4)
            volumeField = fieldmodule.createFieldMeshIntegral(
                one, coordinates, mesh3d)
            volumeField.setNumbersOfPoints(3)
        fieldcache = fieldmodule.createFieldcache()
        result, surfaceArea = surfaceAreaField.evaluateReal(fieldcache, 1)
        self.assertEqual(result, RESULT_OK)
        self.assertAlmostEqual(surfaceArea, 5335.535739164492, delta=1.0E-6)
        result, volume = volumeField.evaluateReal(fieldcache, 1)
        self.assertEqual(result, RESULT_OK)
        self.assertAlmostEqual(volume, 2555.81694845211, delta=1.0E-6)

        # check some annotationGroups:
        expectedSizes3d = {
            "dome of the bladder": 108,
            "neck of urinary bladder": 36,
            "urethra": 96,
            "urinary bladder": 144
        }
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_bladder_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name], size, name)

        # test finding a marker in scaffold
        markerGroup = fieldmodule.findFieldByName("marker").castGroup()
        markerNodes = markerGroup.getFieldNodeGroup(nodes).getNodesetGroup()
        self.assertEqual(5, markerNodes.getSize())
        markerName = fieldmodule.findFieldByName("marker_name")
        self.assertTrue(markerName.isValid())
        markerLocation = fieldmodule.findFieldByName("marker_location")
        self.assertTrue(markerLocation.isValid())
        # test apex marker point
        cache = fieldmodule.createFieldcache()
        node = findNodeWithName(markerNodes, markerName,
                                "apex of urinary bladder")
        self.assertTrue(node.isValid())
        cache.setNode(node)
        element, xi = markerLocation.evaluateMeshLocation(cache, 3)
        self.assertEqual(1, element.getIdentifier())
        assertAlmostEqualList(self, xi, [0.0, 0.0, 0.0], 1.0E-10)
        apexGroup = getAnnotationGroupForTerm(
            annotationGroups, get_bladder_term("apex of urinary bladder"))
        self.assertTrue(apexGroup.getNodesetGroup(nodes).containsNode(node))

        # refine 4x4x4 and check result
        # first remove any surface annotation groups as they are re-added by defineFaceAnnotations
        removeAnnotationGroups = []
        for annotationGroup in annotationGroups:
            if (not annotationGroup.hasMeshGroup(mesh3d)) and (
                    annotationGroup.hasMeshGroup(mesh2d)
                    or annotationGroup.hasMeshGroup(mesh1d)):
                removeAnnotationGroups.append(annotationGroup)

        for annotationGroup in removeAnnotationGroups:
            annotationGroups.remove(annotationGroup)
        self.assertEqual(13, len(annotationGroups))

        refineRegion = region.createRegion()
        refineFieldmodule = refineRegion.getFieldmodule()
        options['Refine number of elements along'] = 4
        options['Refine number of elements around'] = 4
        options['Refine number of elements through wall'] = 4
        meshrefinement = MeshRefinement(region, refineRegion, annotationGroups)
        scaffold.refineMesh(meshrefinement, options)
        annotationGroups = meshrefinement.getAnnotationGroups()
        del meshrefinement

        refineFieldmodule.defineAllFaces()
        oldAnnotationGroups = copy.copy(annotationGroups)
        for annotationGroup in annotationGroups:
            annotationGroup.addSubelements()
        scaffold.defineFaceAnnotations(refineRegion, options, annotationGroups)
        for annotation in annotationGroups:
            if annotation not in oldAnnotationGroups:
                annotationGroup.addSubelements()
        self.assertEqual(37, len(annotationGroups))

        mesh3d = refineFieldmodule.findMeshByDimension(3)
        self.assertEqual(15360, mesh3d.getSize())
        mesh2d = refineFieldmodule.findMeshByDimension(2)
        self.assertEqual(49920, mesh2d.getSize())
        mesh1d = refineFieldmodule.findMeshByDimension(1)
        self.assertEqual(53764, mesh1d.getSize())
        nodes = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        self.assertEqual(19210, nodes.getSize())
        datapoints = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        # check some refined annotationGroups:
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_bladder_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name] * 64, size, name)

        # test finding a marker in refined scaffold
        markerGroup = refineFieldmodule.findFieldByName("marker").castGroup()
        refinedNodes = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        markerNodes = markerGroup.getFieldNodeGroup(
            refinedNodes).getNodesetGroup()
        self.assertEqual(5, markerNodes.getSize())
        markerName = refineFieldmodule.findFieldByName("marker_name")
        self.assertTrue(markerName.isValid())
        markerLocation = refineFieldmodule.findFieldByName("marker_location")
        self.assertTrue(markerLocation.isValid())
        # test apex marker point
        cache = refineFieldmodule.createFieldcache()
        node = findNodeWithName(markerNodes, markerName,
                                "apex of urinary bladder")
        self.assertTrue(node.isValid())
        cache.setNode(node)
        element, xi = markerLocation.evaluateMeshLocation(cache, 3)
        self.assertEqual(1, element.getIdentifier())
        assertAlmostEqualList(self, xi, [0.0, 0.0, 0.0], 1.0E-10)
예제 #7
0
    def test_heart1(self):
        """
        Test creation of heart scaffold.
        """
        scaffold = MeshType_3d_heart1
        parameterSetNames = scaffold.getParameterSetNames()
        self.assertEqual(parameterSetNames, [
            "Default", "Human 1", "Mouse 1", "Pig 1", "Rat 1", "Unit Human 1",
            "Unit Mouse 1", "Unit Pig 1", "Unit Rat 1"
        ])
        options = scaffold.getDefaultOptions("Human 1")
        self.assertEqual(117, len(options))
        self.assertEqual(0.9, options.get("LV outer height"))
        self.assertEqual(80.0, options.get("Unit scale"))
        self.assertEqual(7,
                         options.get("Number of elements around LV free wall"))
        self.assertEqual(7,
                         options.get("Number of elements around RV free wall"))
        context = Context("Test")
        region = context.getDefaultRegion()
        self.assertTrue(region.isValid())
        annotationGroups = scaffold.generateMesh(region, options)
        self.assertEqual(27, len(annotationGroups))
        fieldmodule = region.getFieldmodule()
        mesh3d = fieldmodule.findMeshByDimension(3)
        self.assertEqual(289, mesh3d.getSize())
        mesh2d = fieldmodule.findMeshByDimension(2)
        self.assertEqual(1117, mesh2d.getSize())
        mesh1d = fieldmodule.findMeshByDimension(1)
        self.assertEqual(1356, mesh1d.getSize())
        nodes = fieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        self.assertEqual(528, nodes.getSize())
        datapoints = fieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        coordinates = fieldmodule.findFieldByName(
            "coordinates").castFiniteElement()
        self.assertTrue(coordinates.isValid())
        minimums, maximums = evaluateFieldNodesetRange(coordinates, nodes)
        assertAlmostEqualList(self, minimums,
                              [-50.7876375290527, -57.76590573823474, -91.6],
                              1.0E-6)
        assertAlmostEqualList(
            self, maximums,
            [43.81084359764995, 39.03925080604259, 40.71693637558552], 1.0E-6)

        # check some annotationGroups:
        expectedSizes3d = {
            "left ventricle myocardium": 94,
            "right ventricle myocardium": 79,
            "interventricular septum": 30,
            "left atrium myocardium": 88,
            "right atrium myocardium": 62,
            "interatrial septum": 17
        }
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_heart_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name], size, name)
        expectedSizes2d = {
            "endocardium of left ventricle": 74,
            "endocardium of right ventricle": 59,
            "endocardium of left atrium": 82,
            "endocardium of right atrium": 56,
            "epicardium": 197
        }
        for name in expectedSizes2d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_heart_term(name))
            size = group.getMeshGroup(mesh2d).getSize()
            self.assertEqual(expectedSizes2d[name], size, name)

        # refine 2x2x2 and check result
        # first remove any surface annotation groups as they are re-added by defineFaceAnnotations
        removeAnnotationGroups = []
        for annotationGroup in annotationGroups:
            if annotationGroup.getMeshGroup(mesh3d).getSize() == 0:
                removeAnnotationGroups.append(annotationGroup)
        for annotationGroup in removeAnnotationGroups:
            annotationGroups.remove(annotationGroup)
        self.assertEqual(18, len(annotationGroups))

        refineRegion = region.createRegion()
        refineFieldmodule = refineRegion.getFieldmodule()
        options['Refine number of elements surface'] = 2
        options['Refine number of elements through LV wall'] = 2
        options['Refine number of elements through wall'] = 2
        meshrefinement = MeshRefinement(region, refineRegion, annotationGroups)
        scaffold.refineMesh(meshrefinement, options)
        annotationGroups = meshrefinement.getAnnotationGroups()

        refineFieldmodule.defineAllFaces()
        oldAnnotationGroups = copy.copy(annotationGroups)
        for annotationGroup in annotationGroups:
            annotationGroup.addSubelements()
        scaffold.defineFaceAnnotations(refineRegion, options, annotationGroups)
        for annotation in annotationGroups:
            if annotation not in oldAnnotationGroups:
                annotationGroup.addSubelements()
        self.assertEqual(27, len(annotationGroups))

        mesh3d = refineFieldmodule.findMeshByDimension(3)
        self.assertEqual(2236, mesh3d.getSize())
        mesh2d = refineFieldmodule.findMeshByDimension(2)
        self.assertEqual(7676, mesh2d.getSize())
        mesh1d = refineFieldmodule.findMeshByDimension(1)
        self.assertEqual(8623, mesh1d.getSize())
        nodes = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        self.assertEqual(3183, nodes.getSize())
        datapoints = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        # check some refined annotationGroups:
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_heart_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name] * 8, size, name)
        for name in expectedSizes2d:
            group = getAnnotationGroupForTerm(annotationGroups,
                                              get_heart_term(name))
            size = group.getMeshGroup(mesh2d).getSize()
            if name == "epicardium":
                # fibrous ring is only refined by 1 through its thickness
                fibrousRingReduction = (
                    options['Refine number of elements surface'] - 1
                ) * options['Refine number of elements surface'] * (
                    options['Number of elements around left atrium free wall']
                    +
                    options['Number of elements around right atrium free wall']
                )
                self.assertEqual(
                    expectedSizes2d[name] * 4 - fibrousRingReduction, size,
                    name)
            else:
                self.assertEqual(expectedSizes2d[name] * 4, size, name)

        # test finding a marker in refined scaffold
        markerGroup = refineFieldmodule.findFieldByName("marker").castGroup()
        refinedNodes = refineFieldmodule.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_NODES)
        markerNodes = markerGroup.getFieldNodeGroup(
            refinedNodes).getNodesetGroup()
        self.assertEqual(5, markerNodes.getSize())
        markerName = refineFieldmodule.findFieldByName("marker_name")
        self.assertTrue(markerName.isValid())
        markerLocation = refineFieldmodule.findFieldByName("marker_location")
        self.assertTrue(markerLocation.isValid())
        cache = refineFieldmodule.createFieldcache()
        node = findNodeWithName(markerNodes, markerName, "APEX")
        self.assertTrue(node.isValid())
        cache.setNode(node)
        element, xi = markerLocation.evaluateMeshLocation(cache, 3)
        self.assertEqual(5, element.getIdentifier())
        assertAlmostEqualList(self, xi, [0.0, 0.0, 1.0], 1.0E-10)
예제 #8
0
    def test_stomach1(self):
        """
        Test creation of stomach scaffold.
        """
        scaffold = MeshType_3d_stomach1
        parameterSetNames = scaffold.getParameterSetNames()
        self.assertEqual(parameterSetNames, [ "Default", "Human 1", "Rat 1" ])
        options = scaffold.getDefaultOptions("Rat 1")
        self.assertEqual(15, len(options))
        self.assertEqual(12, options.get("Number of elements around esophagus"))
        self.assertEqual(14, options.get("Number of elements around duodenum"))
        self.assertEqual(2, options.get("Number of elements between cardia and duodenum"))
        self.assertEqual(1, options.get("Number of elements across cardia"))
        self.assertEqual(0.5, options.get("Wall thickness"))
        self.assertEqual(True, options.get("Limiting ridge"))
        ostiumOptions = options['Gastro-esophagal junction']
        ostiumSettings = ostiumOptions.getScaffoldSettings()
        self.assertEqual(1, ostiumSettings.get("Number of vessels"))
        self.assertEqual(8, ostiumSettings.get("Number of elements around ostium"))
        self.assertEqual(1, ostiumSettings.get("Number of elements through wall"))
        self.assertEqual(4.0, ostiumSettings.get("Ostium diameter"))
        self.assertEqual(3.5, ostiumSettings.get("Ostium length"))
        self.assertEqual(0.5, ostiumSettings.get("Ostium wall thickness"))
        self.assertEqual(1.25, ostiumSettings.get("Vessel inner diameter"))
        self.assertEqual(0.5, ostiumSettings.get("Vessel wall thickness"))
        self.assertEqual(0.0, ostiumSettings.get("Vessel angle 1 degrees"))
        self.assertEqual(0.55, options.get("Gastro-esophagal junction position along factor"))
        self.assertEqual(0.2, options.get("Cardia derivative factor"))

        context = Context("Test")
        region = context.getDefaultRegion()
        self.assertTrue(region.isValid())
        annotationGroups = scaffold.generateBaseMesh(region, options)
        self.assertEqual(13, len(annotationGroups))

        fieldmodule = region.getFieldmodule()
        self.assertEqual(RESULT_OK, fieldmodule.defineAllFaces())
        mesh3d = fieldmodule.findMeshByDimension(3)
        self.assertEqual(158, mesh3d.getSize())
        mesh2d = fieldmodule.findMeshByDimension(2)
        self.assertEqual(643, mesh2d.getSize())
        mesh1d = fieldmodule.findMeshByDimension(1)
        self.assertEqual(823, mesh1d.getSize())
        nodes = fieldmodule.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_NODES)
        self.assertEqual(342, nodes.getSize())
        datapoints = fieldmodule.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        coordinates = fieldmodule.findFieldByName("coordinates").castFiniteElement()
        self.assertTrue(coordinates.isValid())
        minimums, maximums = evaluateFieldNodesetRange(coordinates, nodes)
        assertAlmostEqualList(self, minimums, [-18.535034704449842, -14.989550140413265, -8.726949180639382], 1.0E-6)
        assertAlmostEqualList(self, maximums, [18.39909561216101, 15.187335864125645, 8.727618869278126], 1.0E-6)

        with ChangeManager(fieldmodule):
            one = fieldmodule.createFieldConstant(1.0)
            faceMeshGroup = createFaceMeshGroupExteriorOnFace(fieldmodule, Element.FACE_TYPE_XI3_1)
            surfaceAreaField = fieldmodule.createFieldMeshIntegral(one, coordinates, faceMeshGroup)
            surfaceAreaField.setNumbersOfPoints(4)
            volumeField = fieldmodule.createFieldMeshIntegral(one, coordinates, mesh3d)
            volumeField.setNumbersOfPoints(3)
        fieldcache = fieldmodule.createFieldcache()
        result, surfaceArea = surfaceAreaField.evaluateReal(fieldcache, 1)
        self.assertEqual(result, RESULT_OK)
        self.assertAlmostEqual(surfaceArea, 2452.212186914634, delta=1.0E-6)
        result, volume = volumeField.evaluateReal(fieldcache, 1)
        self.assertEqual(result, RESULT_OK)
        self.assertAlmostEqual(volume, 1175.367658574881, delta=1.0E-6)

        # check some annotationGroups:
        expectedSizes3d = {
            "body of stomach" : 28,
            "cardia of stomach" : 12,
            "duodenum" : 14,
            "esophagus" : 24,
            "fundus of stomach" : 38,
            "pyloric antrum" : 28,
            "pylorus": 14,
            "stomach": 158
            }
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups, get_stomach_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name], size, name)

        # refine 4x4x4 and check result
        # first remove any surface annotation groups as they are re-added by defineFaceAnnotations
        removeAnnotationGroups = []
        for annotationGroup in annotationGroups:
            if (not annotationGroup.hasMeshGroup(mesh3d)) and (annotationGroup.hasMeshGroup(mesh2d) or annotationGroup.hasMeshGroup(mesh1d)):
                removeAnnotationGroups.append(annotationGroup)

        for annotationGroup in removeAnnotationGroups:
            annotationGroups.remove(annotationGroup)
        self.assertEqual(13, len(annotationGroups))

        refineRegion = region.createRegion()
        refineFieldmodule = refineRegion.getFieldmodule()
        options['Refine number of elements surface'] = 4
        options['Refine number of elements through wall'] = 4
        meshrefinement = MeshRefinement(region, refineRegion, annotationGroups)
        scaffold.refineMesh(meshrefinement, options)
        annotationGroups = meshrefinement.getAnnotationGroups()

        refineFieldmodule.defineAllFaces()
        oldAnnotationGroups = copy.copy(annotationGroups)
        for annotationGroup in annotationGroups:
            annotationGroup.addSubelements()
        scaffold.defineFaceAnnotations(refineRegion, options, annotationGroups)
        for annotation in annotationGroups:
            if annotation not in oldAnnotationGroups:
                annotationGroup.addSubelements()
        self.assertEqual(16, len(annotationGroups))
#
        mesh3d = refineFieldmodule.findMeshByDimension(3)
        self.assertEqual(10112, mesh3d.getSize())
        mesh2d = refineFieldmodule.findMeshByDimension(2)
        self.assertEqual(33232, mesh2d.getSize())
        mesh1d = refineFieldmodule.findMeshByDimension(1)
        self.assertEqual(36028, mesh1d.getSize())
        nodes = refineFieldmodule.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_NODES)
        self.assertEqual(12936, nodes.getSize())
        datapoints = refineFieldmodule.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_DATAPOINTS)
        self.assertEqual(0, datapoints.getSize())

        # check some refined annotationGroups:
        for name in expectedSizes3d:
            group = getAnnotationGroupForTerm(annotationGroups, get_stomach_term(name))
            size = group.getMeshGroup(mesh3d).getSize()
            self.assertEqual(expectedSizes3d[name]*64, size, name)

        # test finding a marker in refined scaffold
        markerGroup = refineFieldmodule.findFieldByName("marker").castGroup()
        refinedNodes = refineFieldmodule.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_NODES)
        markerNodes = markerGroup.getFieldNodeGroup(refinedNodes).getNodesetGroup()
        self.assertEqual(4, markerNodes.getSize())
        markerName = refineFieldmodule.findFieldByName("marker_name")
        self.assertTrue(markerName.isValid())
        markerLocation = refineFieldmodule.findFieldByName("marker_location")
        self.assertTrue(markerLocation.isValid())
        cache = refineFieldmodule.createFieldcache()
        node = findNodeWithName(markerNodes, markerName, "gastro-esophagal junction on lesser curvature")
        self.assertTrue(node.isValid())
        cache.setNode(node)
        element, xi = markerLocation.evaluateMeshLocation(cache, 3)
        self.assertEqual(1213, element.getIdentifier())
        assertAlmostEqualList(self, xi, [ 0.0, 1.0, 1.0 ], 1.0E-10)