def test_alignFixedRandomData(self): """ Test alignment of model and data to known transformations. """ zinc_model_file = os.path.join(here, "resources", "cube_to_sphere.exf") zinc_data_file = os.path.join(here, "resources", "cube_to_sphere_data_random.exf") fitter = Fitter(zinc_model_file, zinc_data_file) fitter.setDiagnosticLevel(1) fitter.load() dataScale = fitter.getDataScale() self.assertAlmostEqual(dataScale, 0.9958462809921166, delta=1.0E-7) fitter._dataScale = 1.0 # to match previous test results self.assertEqual(fitter.getModelCoordinatesField().getName(), "coordinates") self.assertEqual(fitter.getDataCoordinatesField().getName(), "data_coordinates") self.assertEqual(fitter.getMarkerGroup().getName(), "marker") bottomCentre1 = fitter.evaluateNodeGroupMeanCoordinates("bottom", "coordinates", isData=False) sidesCentre1 = fitter.evaluateNodeGroupMeanCoordinates("sides", "coordinates", isData=False) topCentre1 = fitter.evaluateNodeGroupMeanCoordinates("top", "coordinates", isData=False) assertAlmostEqualList(self, bottomCentre1, [0.5, 0.5, 0.0], delta=1.0E-7) assertAlmostEqualList(self, sidesCentre1, [0.5, 0.5, 0.5], delta=1.0E-7) assertAlmostEqualList(self, topCentre1, [0.5, 0.5, 1.0], delta=1.0E-7) align = FitterStepAlign() fitter.addFitterStep(align) align.setScale(1.1) align.setTranslation([0.1, -0.2, 0.3]) align.setRotation([math.pi / 4.0, math.pi / 8.0, math.pi / 2.0]) self.assertFalse(align.isAlignMarkers()) align.run() rotation = align.getRotation() scale = align.getScale() translation = align.getTranslation() rotationMatrix = getRotationMatrix(rotation) transformationMatrix = [v * scale for v in rotationMatrix] bottomCentre2Expected, sidesCentre2Expected, topCentre2Expected = transformCoordinatesList( [bottomCentre1, sidesCentre1, topCentre1], transformationMatrix, translation) bottomCentre2 = fitter.evaluateNodeGroupMeanCoordinates("bottom", "coordinates", isData=False) sidesCentre2 = fitter.evaluateNodeGroupMeanCoordinates("sides", "coordinates", isData=False) topCentre2 = fitter.evaluateNodeGroupMeanCoordinates("top", "coordinates", isData=False) assertAlmostEqualList(self, bottomCentre2, bottomCentre2Expected, delta=1.0E-7) assertAlmostEqualList(self, sidesCentre2, sidesCentre2Expected, delta=1.0E-7) assertAlmostEqualList(self, topCentre2, topCentre2Expected, delta=1.0E-7)
def test_alignMarkersFitRegularData(self): """ Test automatic alignment of model and data using fiducial markers. """ zinc_model_file = os.path.join(here, "resources", "cube_to_sphere.exf") zinc_data_file = os.path.join(here, "resources", "cube_to_sphere_data_regular.exf") fitter = Fitter(zinc_model_file, zinc_data_file) self.assertEqual(1, len(fitter.getFitterSteps()) ) # there is always an initial FitterStepConfig fitter.setDiagnosticLevel(1) fitter.load() dataScale = fitter.getDataScale() self.assertAlmostEqual(dataScale, 0.991338312625885, delta=1.0E-7) fitter._dataScale = 1.0 # to match previous test results coordinates = fitter.getModelCoordinatesField() self.assertEqual(coordinates.getName(), "coordinates") self.assertEqual(fitter.getDataCoordinatesField().getName(), "data_coordinates") self.assertEqual(fitter.getMarkerGroup().getName(), "marker") #fitter.getRegion().writeFile(os.path.join(here, "resources", "km_fitgeometry1.exf")) fieldmodule = fitter.getFieldmodule() surfaceAreaField = createFieldMeshIntegral(coordinates, fitter.getMesh(2), number_of_points=4) volumeField = createFieldMeshIntegral(coordinates, fitter.getMesh(3), number_of_points=3) fieldcache = fieldmodule.createFieldcache() result, surfaceArea = surfaceAreaField.evaluateReal(fieldcache, 1) self.assertEqual(result, RESULT_OK) self.assertAlmostEqual(surfaceArea, 6.0, delta=1.0E-6) result, volume = volumeField.evaluateReal(fieldcache, 1) self.assertEqual(result, RESULT_OK) self.assertAlmostEqual(volume, 1.0, delta=1.0E-7) align = FitterStepAlign() fitter.addFitterStep(align) self.assertEqual(2, len(fitter.getFitterSteps())) self.assertTrue(align.setAlignMarkers(True)) self.assertTrue(align.isAlignMarkers()) align.run() #fitter.getRegion().writeFile(os.path.join(here, "resources", "km_fitgeometry2.exf")) rotation = align.getRotation() scale = align.getScale() translation = align.getTranslation() assertAlmostEqualList(self, rotation, [-0.25 * math.pi, 0.0, 0.0], delta=1.0E-4) self.assertAlmostEqual(scale, 0.8047378476539072, places=5) assertAlmostEqualList( self, translation, [-0.5690355950594247, 1.1068454682130484e-05, -0.4023689233125251], delta=1.0E-6) result, surfaceArea = surfaceAreaField.evaluateReal(fieldcache, 1) self.assertEqual(result, RESULT_OK) self.assertAlmostEqual(surfaceArea, 3.885618020657802, delta=1.0E-6) result, volume = volumeField.evaluateReal(fieldcache, 1) self.assertEqual(result, RESULT_OK) self.assertAlmostEqual(volume, 0.5211506471189844, delta=1.0E-6) fit1 = FitterStepFit() fitter.addFitterStep(fit1) self.assertEqual(3, len(fitter.getFitterSteps())) fit1.setMarkerWeight(1.0) fit1.setCurvaturePenaltyWeight(0.1) fit1.setNumberOfIterations(3) fit1.setUpdateReferenceState(True) fit1.run() #fitter.getRegion().writeFile(os.path.join(here, "resources", "km_fitgeometry3.exf")) result, surfaceArea = surfaceAreaField.evaluateReal(fieldcache, 1) self.assertEqual(result, RESULT_OK) self.assertAlmostEqual(surfaceArea, 3.1892231780263853, delta=1.0E-4) result, volume = volumeField.evaluateReal(fieldcache, 1) self.assertEqual(result, RESULT_OK) self.assertAlmostEqual(volume, 0.5276229458448985, delta=1.0E-4) # test json serialisation s = fitter.encodeSettingsJSON() fitter2 = Fitter(zinc_model_file, zinc_data_file) fitter2.decodeSettingsJSON(s, decodeJSONFitterSteps) fitterSteps = fitter2.getFitterSteps() self.assertEqual(3, len(fitterSteps)) self.assertTrue(isinstance(fitterSteps[0], FitterStepConfig)) self.assertTrue(isinstance(fitterSteps[1], FitterStepAlign)) self.assertTrue(isinstance(fitterSteps[2], FitterStepFit)) #fitter2.load() #for fitterStep in fitterSteps: # fitterStep.run() s2 = fitter.encodeSettingsJSON() self.assertEqual(s, s2)