Beispiel #1
0
def decodeJSONFitterSteps(fitter: Fitter, dct):
    """
    Function for passing as decoder to Fitter.decodeSettingsJSON().
    Constructs scaffold objects from their JSON object encoding.
    Used as object_hook argument to json.loads as a lambda function
    to pass fitter:
    lambda dct: decodeJSONFitterSteps(fitter, dct)
    :param fitter: Owning Fitter object for new FitterSteps.
    """
    for FitterStepType in [FitterStepAlign, FitterStepConfig, FitterStepFit]:
        if FitterStepType.getJsonTypeId() in dct:
            if (FitterStepType is FitterStepConfig) and (len(
                    fitter.getFitterSteps()) == 1):
                fitterStep = fitter.getInitialFitterStepConfig()
            else:
                fitterStep = FitterStepType()
                fitter.addFitterStep(fitterStep)
            fitterStep.decodeSettingsJSONDict(dct)
            return fitterStep
    return dct
Beispiel #2
0
    def test_groupSettings(self):
        """
        Test per-group settings, and inheritance from previous 
        """
        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)
        fitter.setDiagnosticLevel(1)
        config1 = fitter.getInitialFitterStepConfig()
        groupNames = config1.getGroupSettingsNames()
        self.assertEqual(0, len(groupNames))
        self.assertEqual((1.0, False, False),
                         config1.getGroupDataProportion("sides"))
        config1.setGroupDataProportion("sides", -0.1)
        groupNames = config1.getGroupSettingsNames()
        self.assertEqual(1, len(groupNames))
        self.assertEqual((0.0, True, False),
                         config1.getGroupDataProportion("sides"))
        config1.setGroupDataProportion("sides", 0.25)
        config1.setGroupDataProportion("sides", "A")
        config1.setGroupDataProportion("top", 0.4)
        groupNames = config1.getGroupSettingsNames()
        self.assertEqual(2, len(groupNames))
        self.assertTrue("sides" in groupNames)
        self.assertTrue("top" in groupNames)
        self.assertEqual((0.25, True, False),
                         config1.getGroupDataProportion("sides"))
        self.assertEqual((0.4, True, False),
                         config1.getGroupDataProportion("top"))
        self.assertEqual((1.0, False, False),
                         config1.getGroupDataProportion("bottom"))
        config1.setGroupDataProportion("bottom", 0.1)
        self.assertEqual((0.1, True, False),
                         config1.getGroupDataProportion("bottom"))
        groupNames = config1.getGroupSettingsNames()
        self.assertEqual(3, len(groupNames))
        # setting a non-inheriting value to None clears it:
        config1.setGroupDataProportion("bottom", None)
        self.assertEqual((1.0, False, False),
                         config1.getGroupDataProportion("bottom"))
        groupNames = config1.getGroupSettingsNames()
        self.assertEqual(2, len(groupNames))
        config1.setGroupDataProportion("bottom", 0.12)
        self.assertEqual((0.12, True, False),
                         config1.getGroupDataProportion("bottom"))
        groupNames = config1.getGroupSettingsNames()
        self.assertEqual(3, len(groupNames))
        config1.clearGroupDataProportion("bottom")
        self.assertEqual((1.0, False, False),
                         config1.getGroupDataProportion("bottom"))
        groupNames = config1.getGroupSettingsNames()
        self.assertEqual(2, len(groupNames))
        self.assertTrue("sides" in groupNames)
        self.assertTrue("top" in groupNames)
        fitter.load()
        activeNodeset = fitter.getActiveDataNodesetGroup()
        self.assertEqual(141, activeNodeset.getSize())
        self.assertEqual(
            72,
            getNodesetConditionalSize(
                activeNodeset,
                fitter.getFieldmodule().findFieldByName("bottom")))
        self.assertEqual(
            36,
            getNodesetConditionalSize(
                activeNodeset,
                fitter.getFieldmodule().findFieldByName("sides")))
        self.assertEqual(
            29,
            getNodesetConditionalSize(
                activeNodeset,
                fitter.getFieldmodule().findFieldByName("top")))
        self.assertEqual(
            4,
            getNodesetConditionalSize(
                activeNodeset,
                fitter.getFieldmodule().findFieldByName("marker")))
        # test override and inherit
        config2 = FitterStepConfig()
        fitter.addFitterStep(config2)
        config2.setGroupDataProportion("top", None)
        groupNames = config2.getGroupSettingsNames()
        self.assertEqual(1, len(groupNames))
        self.assertTrue("top" in groupNames)
        self.assertEqual((0.25, False, True),
                         config2.getGroupDataProportion("sides"))
        # test that the reset proportion has setLocally None
        self.assertEqual((1.0, None, True),
                         config2.getGroupDataProportion("top"))
        config2.run()
        activeNodeset = fitter.getActiveDataNodesetGroup()
        self.assertEqual(184, activeNodeset.getSize())
        self.assertEqual(
            72,
            getNodesetConditionalSize(
                activeNodeset,
                fitter.getFieldmodule().findFieldByName("bottom")))
        self.assertEqual(
            36,
            getNodesetConditionalSize(
                activeNodeset,
                fitter.getFieldmodule().findFieldByName("sides")))
        self.assertEqual(
            72,
            getNodesetConditionalSize(
                activeNodeset,
                fitter.getFieldmodule().findFieldByName("top")))
        self.assertEqual(
            4,
            getNodesetConditionalSize(
                activeNodeset,
                fitter.getFieldmodule().findFieldByName("marker")))
        # test inherit through 2 previous configs and cancel/None in config2
        config3 = FitterStepConfig()
        fitter.addFitterStep(config3)
        groupNames = config3.getGroupSettingsNames()
        self.assertEqual(0, len(groupNames))
        self.assertEqual((0.25, False, True),
                         config3.getGroupDataProportion("sides"))
        self.assertEqual((1.0, False, False),
                         config3.getGroupDataProportion("top"))
        config3.run()
        activeNodeset = fitter.getActiveDataNodesetGroup()
        self.assertEqual(184, activeNodeset.getSize())
        self.assertEqual(
            72,
            getNodesetConditionalSize(
                activeNodeset,
                fitter.getFieldmodule().findFieldByName("bottom")))
        self.assertEqual(
            36,
            getNodesetConditionalSize(
                activeNodeset,
                fitter.getFieldmodule().findFieldByName("sides")))
        self.assertEqual(
            72,
            getNodesetConditionalSize(
                activeNodeset,
                fitter.getFieldmodule().findFieldByName("top")))
        self.assertEqual(
            4,
            getNodesetConditionalSize(
                activeNodeset,
                fitter.getFieldmodule().findFieldByName("marker")))
        del config1
        del config2
        del config3

        # 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))
        config1, config2, config3 = fitterSteps
        self.assertTrue(isinstance(config1, FitterStepConfig))
        self.assertTrue(isinstance(config2, FitterStepConfig))
        self.assertTrue(isinstance(config3, FitterStepConfig))
        groupNames = config1.getGroupSettingsNames()
        self.assertEqual(2, len(groupNames))
        self.assertTrue("sides" in groupNames)
        self.assertTrue("top" in groupNames)
        self.assertEqual((0.25, True, False),
                         config1.getGroupDataProportion("sides"))
        self.assertEqual((0.4, True, False),
                         config1.getGroupDataProportion("top"))
        self.assertEqual((1.0, False, False),
                         config1.getGroupDataProportion("bottom"))
        groupNames = config2.getGroupSettingsNames()
        self.assertEqual(1, len(groupNames))
        self.assertTrue("top" in groupNames)
        self.assertEqual((0.25, False, True),
                         config2.getGroupDataProportion("sides"))
        self.assertEqual((1.0, None, True),
                         config2.getGroupDataProportion("top"))