Example #1
0
    def test_nesting_param_10(self):
        """Unit test of function nesting_param, which determine how model parameters are converted for nesting."""

        # Define parameters
        model_params = MODEL_PARAMS_NS_MMQ_3SITE
        nested_model_params = MODEL_PARAMS_NS_MMQ_2SITE

        # Get the dictionary of parameter conversion.
        par_dic = nesting_param(model_params=model_params,
                                nested_model_params=nested_model_params)

        # Test the number of elements in the dictionary.
        self.assertEqual(len(par_dic), len(model_params))

        print("model parameters", model_params)
        print("nested_model_params", nested_model_params)

        # Test content of dictionary.
        self.assertEqual(par_dic['r2'], 'r2')
        self.assertEqual(par_dic['pA'], 'pA')
        self.assertEqual(par_dic['dw_AB'], 'dw')
        self.assertEqual(par_dic['dwH_AB'], 'dwH')
        self.assertEqual(par_dic['kex_AB'], 'kex')
        self.assertEqual(par_dic['pB'], '1 - pA')
        self.assertEqual(par_dic['dw_BC'], 'dw')
        self.assertEqual(par_dic['dwH_BC'], 'dwH')
        self.assertEqual(par_dic['kex_BC'], 'kex')
        self.assertEqual(par_dic['kex_AC'], 'kex')
Example #2
0
    def test_nesting_param_10(self):
        """Unit test of function nesting_param, which determine how model parameters are converted for nesting."""

        # Define parameters
        model_params = MODEL_PARAMS_NS_MMQ_3SITE
        nested_model_params = MODEL_PARAMS_NS_MMQ_2SITE

        # Get the dictionary of parameter conversion.
        par_dic = nesting_param(model_params=model_params, nested_model_params=nested_model_params)

        # Test the number of elements in the dictionary.
        self.assertEqual(len(par_dic), len(model_params))

        print("model parameters", model_params)
        print("nested_model_params", nested_model_params)

        # Test content of dictionary.
        self.assertEqual(par_dic['r2'], 'r2')
        self.assertEqual(par_dic['pA'], 'pA')
        self.assertEqual(par_dic['dw_AB'], 'dw')
        self.assertEqual(par_dic['dwH_AB'], 'dwH')
        self.assertEqual(par_dic['kex_AB'], 'kex')
        self.assertEqual(par_dic['pB'], '1 - pA')
        self.assertEqual(par_dic['dw_BC'], 'dw')
        self.assertEqual(par_dic['dwH_BC'], 'dwH')
        self.assertEqual(par_dic['kex_BC'], 'kex')
        self.assertEqual(par_dic['kex_AC'], 'kex')
Example #3
0
    def test_nesting_param_6(self):
        """Unit test of function nesting_param, which determine how model parameters are converted for nesting."""

        # Define parameters
        model_params = deepcopy(MODEL_PARAMS_NS_R1RHO_3SITE)
        nested_model_params = deepcopy(MODEL_PARAMS_NS_R1RHO_3SITE_LINEAR)

        # Get the dictionary of parameter conversion.
        par_dic = nesting_param(model_params=model_params,
                                nested_model_params=nested_model_params)

        # Test the number of elements in the dictionary.
        self.assertEqual(len(par_dic), len(model_params))

        print("model parameters", model_params)
        print("nested_model_params", nested_model_params)

        # Test content of dictionary.
        self.assertEqual(par_dic['r2'], 'r2')
        self.assertEqual(par_dic['pA'], 'pA')
        self.assertEqual(par_dic['dw_AB'], 'dw_AB')
        self.assertEqual(par_dic['kex_AB'], 'kex_AB')
        self.assertEqual(par_dic['pB'], 'pB')
        self.assertEqual(par_dic['dw_BC'], 'dw_BC')
        self.assertEqual(par_dic['kex_BC'], 'kex_BC')
        self.assertEqual(par_dic['kex_AC'], '0.0')
Example #4
0
    def test_nesting_param_3(self):
        """Unit test of function nesting_param, which determine how model parameters are converted for nesting."""

        # Define parameters
        model_params = MODEL_PARAMS_CR72
        nested_model_params = MODEL_PARAMS_CR72_FULL

        # Get the dictionary of parameter conversion.
        par_dic = nesting_param(model_params=model_params, nested_model_params=nested_model_params)

        # Test the number of elements in the dictionary.
        self.assertEqual(len(par_dic), len(model_params))

        print("model parameters", model_params)
        print("nested_model_params", nested_model_params)

        # Test content of dictionary.
        self.assertEqual(par_dic['r2'], 'r2a')
        self.assertEqual(par_dic['pA'], 'pA')
        self.assertEqual(par_dic['dw'], 'dw')
        self.assertEqual(par_dic['kex'], 'kex')
Example #5
0
    def test_nesting_param_3(self):
        """Unit test of function nesting_param, which determine how model parameters are converted for nesting."""

        # Define parameters
        model_params = MODEL_PARAMS_CR72
        nested_model_params = MODEL_PARAMS_CR72_FULL

        # Get the dictionary of parameter conversion.
        par_dic = nesting_param(model_params=model_params,
                                nested_model_params=nested_model_params)

        # Test the number of elements in the dictionary.
        self.assertEqual(len(par_dic), len(model_params))

        print("model parameters", model_params)
        print("nested_model_params", nested_model_params)

        # Test content of dictionary.
        self.assertEqual(par_dic['r2'], 'r2a')
        self.assertEqual(par_dic['pA'], 'pA')
        self.assertEqual(par_dic['dw'], 'dw')
        self.assertEqual(par_dic['kex'], 'kex')
Example #6
0
    def test_nesting_param_1(self):
        """Unit test of function nesting_param, which determine how model parameters are converted for nesting."""

        # Define parameters
        model_params = deepcopy(MODEL_PARAMS_LM63_3SITE)
        nested_model_params = deepcopy(MODEL_PARAMS_LM63)

        # Get the dictionary of parameter conversion.
        par_dic = nesting_param(model_params=model_params,
                                nested_model_params=nested_model_params)

        # Test the number of elements in the dictionary.
        self.assertEqual(len(par_dic), len(model_params))

        print("model parameters", model_params)
        print("nested_model_params", nested_model_params)

        # Test content of dictionary.
        self.assertEqual(par_dic['r2'], 'r2')
        self.assertEqual(par_dic['phi_ex_B'], 'phi_ex')
        self.assertEqual(par_dic['phi_ex_C'], 'phi_ex')
        self.assertEqual(par_dic['kB'], 'kex')
        self.assertEqual(par_dic['kC'], 'kex')
Example #7
0
    def nesting(self, model=None):
        """Support for model nesting.

        If model nesting is detected, the optimised parameters from the simpler model will be used for the more complex model.  The method will then signal if the nesting condition is met for the model, allowing the grid search to be skipped.


        @keyword model: The model to be optimised.
        @type model:    str
        @return:        True if the model parameters is equivalent to the nested model, and all parameters are copied.  False if none or some of the parameters have been translated from the nested model.  Here the Grid search should still be performed.
        @rtype:         bool
        """

        # Printout.
        subsection(file=sys.stdout,
                   text="Nesting and model equivalence checks",
                   prespace=1)

        # The simpler model.
        model_info, comparable_model_info = nesting_model(
            self_models=self.models, model=model)
        if comparable_model_info != None:
            nested_pipe = self.name_pipe(comparable_model_info.model)
        else:
            nested_pipe = None

        # No nesting.
        if not nested_pipe:
            print("No model nesting or model equivalence detected.")
            return False

        # Copying the parameters to a numerical model from an analytic solution.
        if model_info.eq in [EQ_NUMERIC, EQ_SILICO
                             ] and comparable_model_info.eq == EQ_ANALYTIC:
            analytic = True
        else:
            analytic = False

        # Determine if model is equivalent or nested.
        if model_info.params == comparable_model_info.params:
            equivalent = True
        else:
            equivalent = False

        # Printout.
        if equivalent:
            print(
                "Model equivalence detected, copying the optimised parameters from the '%s' model rather than performing a grid search."
                % comparable_model_info.model)
        else:
            print(
                "Model nesting detected, translating the optimised parameters %s from the '%s' model to the parameters %s of model '%s'.  A grid search is issued for the remaining parameters."
                % (comparable_model_info.params, comparable_model_info.model,
                   model_info.params, model))
        if analytic:
            print("The parameters are copied from a %s model to a %s model." %
                  (comparable_model_info.eq, model_info.eq))

        # Get the dictionary of how the model parameters of the current model can be copied.
        par_dic = nesting_param(
            model_params=model_info.params,
            nested_model_params=comparable_model_info.params)

        # Loop over the parameters in current model.
        for param in model_info.params:
            # Extract how parameter is translated.
            param_conv = par_dic[param]

            # If the param_conv is None, then continue.
            if param_conv == None:
                continue

            print("Copying from parameter '%s' to '%s'." % (param_conv, param))

            # Loop over the spins to copy the parameters.
            for spin, spin_id in spin_loop(return_id=True, skip_desel=True):
                # Get the nested spin.
                nested_spin = return_spin(spin_id=spin_id, pipe=nested_pipe)

                # Set value.
                # Some special conversions.
                if param_conv == '1 - pA':
                    val = 1.0 - getattr(nested_spin, 'pA')

                elif param_conv == '0.0':
                    val = 0.0

                else:
                    val = deepcopy(getattr(nested_spin, param_conv))

                # Set the attribute.
                setattr(spin, param, val)

        # Determine if model is equivalent, and should not be Grid searched, or if nested, and some parameters are pre-set. Here Grid search should still be issued.
        return equivalent
Example #8
0
    def nesting(self, model=None):
        """Support for model nesting.

        If model nesting is detected, the optimised parameters from the simpler model will be used for the more complex model.  The method will then signal if the nesting condition is met for the model, allowing the grid search to be skipped.


        @keyword model: The model to be optimised.
        @type model:    str
        @return:        True if the model parameters is equivalent to the nested model, and all parameters are copied.  False if none or some of the parameters have been translated from the nested model.  Here the Grid search should still be performed.
        @rtype:         bool
        """

        # Printout. 
        subsection(file=sys.stdout, text="Nesting and model equivalence checks", prespace=1)

        # The simpler model.
        model_info, comparable_model_info = nesting_model(self_models=self.models, model=model)
        if comparable_model_info != None:
            nested_pipe = self.name_pipe(comparable_model_info.model)
        else:
            nested_pipe = None

        # No nesting.
        if not nested_pipe:
            print("No model nesting or model equivalence detected.")
            return False

        # Copying the parameters to a numerical model from an analytic solution.
        if model_info.eq in [EQ_NUMERIC, EQ_SILICO] and comparable_model_info.eq == EQ_ANALYTIC:
            analytic = True
        else:
            analytic = False

        # Determine if model is equivalent or nested.
        if model_info.params == comparable_model_info.params:
            equivalent = True
        else:
            equivalent = False

        # Printout.
        if equivalent:
            print("Model equivalence detected, copying the optimised parameters from the '%s' model rather than performing a grid search." % comparable_model_info.model)
        else:
            print("Model nesting detected, translating the optimised parameters %s from the '%s' model to the parameters %s of model '%s'.  A grid search is issued for the remaining parameters." % (comparable_model_info.params, comparable_model_info.model, model_info.params, model))
        if analytic:
            print("The parameters are copied from a %s model to a %s model." % (comparable_model_info.eq, model_info.eq))

        # Get the dictionary of how the model parameters of the current model can be copied.
        par_dic = nesting_param(model_params=model_info.params, nested_model_params=comparable_model_info.params)

        # Loop over the parameters in current model.
        for param in model_info.params:
            # Extract how parameter is translated.
            param_conv = par_dic[param]

            # If the param_conv is None, then continue.
            if param_conv == None:
                continue

            print("Copying from parameter '%s' to '%s'." % (param_conv, param))

            # Loop over the spins to copy the parameters.
            for spin, spin_id in spin_loop(return_id=True, skip_desel=True):
                # Get the nested spin.
                nested_spin = return_spin(spin_id=spin_id, pipe=nested_pipe)

                # Set value.
                # Some special conversions.
                if param_conv == '1 - pA':
                    val = 1.0 - getattr(nested_spin, 'pA')

                elif param_conv == '0.0':
                    val = 0.0

                else:
                    val = deepcopy(getattr(nested_spin, param_conv))

                # Set the attribute.
                setattr(spin, param, val)

        # Determine if model is equivalent, and should not be Grid searched, or if nested, and some parameters are pre-set. Here Grid search should still be issued.
        return equivalent