def isregistered(function):
    status, msg = True, ""
    try:
        FunctionFactory.createFunction(function)
    except RuntimeError as exc:
        status, msg = False, 'Could not create {} function: {}'.format(function, str(exc))
    return status, msg
Example #2
0
def isregistered(function):
    status, msg = True, ""
    try:
        FunctionFactory.createFunction(function)
    except RuntimeError as exc:
        status, msg = False, 'Could not create {} function: {}'.format(
            function, str(exc))
    return status, msg
 def setUp(self):
     context = setup_context()
     self.model = ModelFittingModel(context, context.model_fitting_context)
     self.result_table_names = ["Result1", "Result2"]
     self.dataset_names = ["workspace_name_A0", "workspace_name_A1", "A0_A1"]
     self.fit_function1 = FunctionFactory.createFunction("FlatBackground")
     self.fit_function2 = FunctionFactory.createFunction("LinearBackground")
     self.single_fit_functions = [self.fit_function1.clone(), self.fit_function1.clone(), self.fit_function2.clone()]
    def setup_functions(self, flat_background: IFunction = None, exp_decay: IFunction = None) -> None:
        """Sets up the functions to use by default when doing Auto corrections."""
        if flat_background is not None:
            self.flat_background = flat_background.clone()
        else:
            self.flat_background = FunctionFactory.createFunction("FlatBackground")

        if exp_decay is not None:
            self.exp_decay = exp_decay.clone()
        else:
            self.exp_decay = FunctionFactory.createFunction("ExpDecayMuon")
            self.exp_decay.setParameter("A", DEFAULT_A_VALUE)
            self.exp_decay.setParameter("Lambda", DEFAULT_LAMBDA_VALUE)
            self.exp_decay.fixParameter("Lambda")
Example #5
0
def is_registered(function_name):
    """
    Check whether the function with the specified name has been registered.

    :param function_name: The name of the function to check for registration.
    :return:              A tuple of the status (True if function is registered,
                          false otherwise) and the error message (empty if the
                          function is registered).
    """
    try:
        FunctionFactory.createFunction(function_name)
    except RuntimeError as exc:
        return False, 'Could not create {} function: {}'.format(function_name,
                                                                str(exc))
    return True, ""
Example #6
0
    def setUp(self):
        self.dataset_names = ["Results1; A0 vs A1", "Results1; A1 vs A0"]
        self.current_dataset_index = 0
        self.start_x = 0.0
        self.end_x = 15.0
        self.fit_status = "success"
        self.chi_squared = 1.5
        self.function_name = "FlatBackground"
        self.minimizer = "Levenberg-Marquardt"
        self.evaluation_type = "eval type"
        self.fit_to_raw = True
        self.plot_guess = True
        self.fit_function = FunctionFactory.createFunction("FlatBackground")
        self.single_fit_functions = [
            self.fit_function.clone(),
            self.fit_function.clone()
        ]

        self.param_combination_name = "Results1; A0 vs A1"
        self.param_group_name = "Results1; Parameter Combinations"
        self.result_table_names = ["Results1", "Results2"]
        self.x_parameters = ["A0", "A1"]
        self.y_parameters = ["A0", "A1"]
        self.x_parameter_types = [
            TableColumnType.Y.value, TableColumnType.Y.value
        ]
        self.y_parameter_types = [
            TableColumnType.Y.value, TableColumnType.Y.value
        ]

        self._setup_mock_view()
        self._setup_mock_model()
        self._setup_presenter()
Example #7
0
    def setUp(self):
        self.dataset_names = ["Name1", "Name2"]
        self.current_dataset_index = 0
        self.start_x = 0.0
        self.end_x = 15.0
        self.fit_status = "success"
        self.chi_squared = 1.5
        self.function_name = "FlatBackground"
        self.minimizer = "Levenberg-Marquardt"
        self.evaluation_type = "eval type"
        self.fit_to_raw = True
        self.plot_guess = True
        self.fit_function = FunctionFactory.createFunction("FlatBackground")
        self.single_fit_functions = [
            self.fit_function.clone(),
            self.fit_function.clone()
        ]

        self._setup_mock_view()
        self._setup_mock_model()
        self._setup_presenter()

        self.mock_view_minimizer.assert_called_once_with()
        self.mock_view_evaluation_type.assert_called_once_with()
        self.mock_view_fit_to_raw.assert_called_once_with()
        self.mock_model_minimizer.assert_called_once_with(self.minimizer)
        self.mock_model_evaluation_type.assert_called_once_with(
            self.evaluation_type)
        self.mock_model_fit_to_raw.assert_called_once_with(self.fit_to_raw)

        self.assertEqual(
            self.view.set_slot_for_fit_generator_clicked.call_count, 1)
        self.assertEqual(self.view.set_slot_for_fit_button_clicked.call_count,
                         1)
        self.assertEqual(self.view.set_slot_for_undo_fit_clicked.call_count, 1)
        self.assertEqual(self.view.set_slot_for_plot_guess_changed.call_count,
                         1)
        self.assertEqual(self.view.set_slot_for_fit_name_changed.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_covariance_matrix_clicked.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_function_structure_changed.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_function_parameter_changed.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_function_attribute_changed.call_count, 1)
        self.assertEqual(self.view.set_slot_for_start_x_updated.call_count, 1)
        self.assertEqual(self.view.set_slot_for_end_x_updated.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_exclude_range_state_changed.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_exclude_start_x_updated.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_exclude_end_x_updated.call_count, 1)
        self.assertEqual(self.view.set_slot_for_minimizer_changed.call_count,
                         1)
        self.assertEqual(
            self.view.set_slot_for_evaluation_type_changed.call_count, 1)
        self.assertEqual(self.view.set_slot_for_use_raw_changed.call_count, 1)
Example #8
0
 def setUp(self):
     context = setup_context()
     self.model = GeneralFittingModel(context, context.fitting_context)
     self.dataset_names = ["Name1", "Name2"]
     self.fit_function = FunctionFactory.createFunction("FlatBackground")
     self.single_fit_functions = [self.fit_function.clone(), self.fit_function.clone()]
     self.simultaneous_fit_function = FunctionFactory.createInitializedMultiDomainFunction(str(self.fit_function),
                                                                                           len(self.dataset_names))
Example #9
0
    def test_that_update_attribute_value_will_update_the_value_of_a_parameter_in_single_fit_mode(self):
        self.model.dataset_names = self.dataset_names
        self.model.single_fit_functions = [FunctionFactory.createFunction("Chebyshev"), None]

        self.model.update_attribute_value("StartX", 0.0)
        self.model.update_attribute_value("EndX", 15.0)

        self.assertEqual(str(self.model.current_single_fit_function), "name=Chebyshev,EndX=15,StartX=0,n=0,A0=0")
    def setUp(self):
        self.fitting_context = BasicFittingContext()

        self.dataset_names = ["Name1", "Name2"]
        self.fit_function = FunctionFactory.createFunction("FlatBackground")
        self.single_fit_functions = [
            self.fit_function.clone(),
            self.fit_function.clone()
        ]
Example #11
0
    def test_that_get_fit_function_parameters_will_return_a_list_of_parameter_names_when_in_single_fit_mode(self):
        self.model.dataset_names = self.dataset_names
        self.model.single_fit_functions = self.single_fit_functions

        self.assertEqual(self.model.get_fit_function_parameters(), ["A0"])

        self.fit_function = FunctionFactory.createFunction("ExpDecay")
        self.model.single_fit_functions = [self.fit_function.clone(), self.fit_function.clone()]

        self.assertEqual(self.model.get_fit_function_parameters(), ["Height", "Lifetime"])
Example #12
0
    def create_mantid_ifunction(self, function_name):
        """
        Create and initiializes a Mantid IFunction.

        Args:
          function_name (str): The name of the function to use.

        Returns:
          ifunction: An instance of a Mantid IFunction
        """
        return FunctionFactory.createFunction(function_name)
Example #13
0
 def setUp(self):
     context = setup_context()
     self.model = BasicFittingModel(context, context.fitting_context)
     self.dataset_names = [
         "EMU20884; Group; fwd; Asymmetry",
         "EMU20884; Group; top; Asymmetry"
     ]
     self.fit_function = FunctionFactory.createFunction("FlatBackground")
     self.single_fit_functions = [
         self.fit_function.clone(),
         self.fit_function.clone()
     ]
Example #14
0
    def create_mantid_ifunction(self, function_name):
        """
        Create and initiializes a Mantid IFunction.

        Args:
          function_name (str): The name of the function to use.

        Returns:
          ifunction: An instance of a Mantid IFunction
        """
        from mantid.api import FunctionFactory
        return FunctionFactory.createFunction(function_name)
Example #15
0
    def test_that_automatically_update_function_name_will_set_the_correct_function_name_for_a_composite_function(self):
        composite = CompositeFunction()
        composite.add(self.fit_function.clone())
        f2 = FunctionFactory.createFunction("ExpDecay")
        composite.add(f2)

        self.model.dataset_names = self.dataset_names
        self.model.single_fit_functions = [composite.clone(), composite.clone()]

        self.assertEqual(self.model.function_name, "")
        self.model.automatically_update_function_name()

        self.assertEqual(self.model.function_name, " FlatBackground,ExpDecay")
    def _get_fit_function_for_background_fit(self, correction_data: BackgroundCorrectionData) -> IFunction:
        """Returns the fit function to use for a background fit."""
        correction_data = self._set_background_parameter_if_not_using_raw(correction_data)

        if self._corrections_context.selected_function == FLAT_BACKGROUND:
            return correction_data.flat_background
        elif self._corrections_context.selected_function == FLAT_BACKGROUND_AND_EXP_DECAY:
            composite = FunctionFactory.createFunction("CompositeFunction")
            composite.add(correction_data.flat_background)
            composite.add(correction_data.exp_decay)
            return composite

        raise RuntimeError("The selected background function is not recognised.")
Example #17
0
    def __init__(self, name, **kwargs):
        """
        Called when creating an instance

        :param name:   name of fitting function to create or
                       an Ifunction object to wrap.
        :param kwargs: standard argument for initializing fit
                       function
        """
        if not isinstance(name, str):
            self.fun = name
        else:
            self.fun = FunctionFactory.createFunction(name)
        self.init_paramgeters_and_attributes(**kwargs)
Example #18
0
    def __init__(self, name, **kwargs):
        """
        Called when creating an instance

        :param name:   name of fitting function to create or
                       an Ifunction object to wrap.
        :param kwargs: standard argument for initializing fit
                       function
        """
        if not isinstance(name, str):
            self.fun = name
        else:
            self.fun = FunctionFactory.createFunction(name)
        self.init_paramgeters_and_attributes(**kwargs)
Example #19
0
    def test_that_update_attribute_value_will_update_the_value_of_a_parameter_in_simultaneous_fit_mode(self):
        self.model.dataset_names = self.dataset_names

        self.fit_function = FunctionFactory.createFunction("Chebyshev")
        self.model.simultaneous_fit_function = FunctionFactory.createInitializedMultiDomainFunction(
            str(self.fit_function), len(self.dataset_names))

        self.model.simultaneous_fitting_mode = True
        self.model.current_dataset_index = 1

        self.model.update_attribute_value("StartX", 0.0)
        self.model.update_attribute_value("EndX", 15.0)

        self.assertEqual(str(self.model.simultaneous_fit_function), "composite=MultiDomainFunction,NumDeriv=true;"
                                                                    "name=Chebyshev,EndX=1,StartX=-1,n=0,A0=0,$domains=i;"
                                                                    "name=Chebyshev,EndX=15,StartX=0,n=0,A0=0,$domains=i")
Example #20
0
    def setUp(self):
        context = setup_context()
        self.model = TFAsymmetryFittingModel(context, context.fitting_context)
        self.dataset_names = [
            "EMU20884; Group; fwd; Asymmetry",
            "EMU20884; Group; top; Asymmetry"
        ]
        self.tf_non_compliant_dataset_names = [
            "EMU20884; Group; fwd; Asymmetry",
            "EMU20884; Pair Asym; long; Asymmetry"
        ]
        self.fit_function = FunctionFactory.createFunction("FlatBackground")
        self.single_fit_functions = [
            self.fit_function.clone(),
            self.fit_function.clone()
        ]
        self.simultaneous_fit_function = FunctionFactory.createInitializedMultiDomainFunction(
            str(self.fit_function), len(self.dataset_names))

        self.tf_single_function = create_tf_asymmetry_function(
            self.fit_function.clone())
        self.tf_single_fit_functions = [
            self.tf_single_function.clone(),
            self.tf_single_function.clone()
        ]
        self.tf_simultaneous_fit_function = FunctionFactory.createInitializedMultiDomainFunction(
            str(self.tf_single_function), len(self.dataset_names))

        CreateSampleWorkspace(Function="One Peak",
                              XMin=0.0,
                              XMax=15.0,
                              BinWidth=0.1,
                              OutputWorkspace=self.dataset_names[0])
        CreateSampleWorkspace(Function="One Peak",
                              XMin=0.0,
                              XMax=15.0,
                              BinWidth=0.1,
                              OutputWorkspace=self.dataset_names[1])
Example #21
0
def create_tf_asymmetry_function(normal_function):
    flat_back_normalisation = FunctionFactory.createFunction("FlatBackground")

    comp1 = FunctionFactory.createFunction("CompositeFunction")
    flat_back = FunctionFactory.createFunction("FlatBackground")
    comp1.add(flat_back)
    comp1.add(normal_function)

    prod_func = FunctionFactory.createFunction("ProductFunction")
    prod_func.add(flat_back_normalisation)
    prod_func.add(comp1)

    exp_decay_muon = FunctionFactory.createFunction("ExpDecayMuon")

    comp2 = FunctionFactory.createFunction("CompositeFunction")
    comp2.add(prod_func)
    comp2.add(exp_decay_muon)
    return comp2
Example #22
0
    def __init__(self, name, **kwargs):
        """ 
    Called when creating an instance
    :param name: name of fitting function to create or 
    an Ifunction object to wrap.
    :param **kwargs: standard argument for __init__ function
    """
        if not isinstance(name, str):
            self.fun = name
        else:
            self.fun = FunctionFactory.createFunction(name)
            # Deal with attributes first
            for key in kwargs:
                if key == "attributes":
                    atts = kwargs[key]
                    for keya in atts:
                        self.fun.setAttributeValue(keya, atts[keya])
                elif self.fun.hasAttribute(key):
                    self.fun.setAttributeValue(key, kwargs[key])

            # Then deal with parameters
            for key in kwargs:
                if key != "attributes" and not self.fun.hasAttribute(key):
                    self.fun.setParameter(key, kwargs[key])
Example #23
0
    def __init__(self, name, params_to_optimize=None, **kwargs):
        """
        Called when creating an instance

        :param name:   name of fitting function to create or an Ifunction object to wrap.
        :param params_to_optimize:   names of the parameters you want to optimize. The omitted parameters will remain
                       fixed at their initial value.
        :param kwargs: standard argument for initializing fit function
        """
        if not isinstance(name, str):
            self.fun = name
        else:
            self.fun = FunctionFactory.createFunction(name)
        self.init_paramgeters_and_attributes(**kwargs)

        def set_parameters_by_index(params):
            for i in range(len(params)):
                self.fun.setParameter(i, params[i])

        def set_parameters_by_name(params):
            for i, param_name in enumerate(params_to_optimize):
                self.fun.setParameter(param_name, params[i])

        self._set_parameters = set_parameters_by_index if params_to_optimize is None else set_parameters_by_name
Example #24
0
    def test_that_set_fit_function_will_set_the_function_in_the_browser(self):
        fit_function = FunctionFactory.createFunction("FlatBackground")

        self.view.set_fit_function(fit_function)
        self.assertEqual(str(self.view.current_fit_function()),
                         str(fit_function))
Example #25
0
 def _setup_fitted_function(self):
     function = FunctionFactory.createFunction("FlatBackground")
     function.setParameter("A0", self.fitted_a0)
     function.setError("A0", self.fitted_a0_error)
     return function
 def test_type(self):
     p = FunctionFactory.createFunction("ProductFunction")
     self.assertTrue(isinstance(p, ProductFunction))
     self.assertTrue(isinstance(p, CompositeFunction))
 def test_length(self):
     p = FunctionFactory.createFunction("ProductFunction")
     self.assertEquals(len(p), 0)
Example #28
0
    def setUp(self):
        self.dataset_names = ["Name1", "Name2"]
        self.current_dataset_index = 0
        self.start_x = 0.0
        self.end_x = 15.0
        self.fit_status = "success"
        self.chi_squared = 1.5
        self.function_name = "FlatBackground"
        self.minimizer = "Levenberg-Marquardt"
        self.evaluation_type = "eval type"
        self.fit_to_raw = True
        self.plot_guess = True
        self.simultaneous_fitting_mode = True
        self.simultaneous_fit_by = "Group/Pair"
        self.simultaneous_fit_by_specifier = "fwd"
        self.global_parameters = ["A0"]
        self.fit_function = FunctionFactory.createFunction("FlatBackground")
        self.single_fit_functions = [
            self.fit_function.clone(),
            self.fit_function.clone()
        ]
        self.tf_asymmetry_mode = True
        self.normalisation = 3.0
        self.normalisation_error = 0.3

        self._setup_mock_view()
        self._setup_mock_model()
        self._setup_presenter()

        self.mock_view_minimizer.assert_called_once_with()
        self.mock_view_evaluation_type.assert_called_once_with()
        self.mock_view_fit_to_raw.assert_called_once_with()
        self.mock_view_simultaneous_fit_by.assert_called_once_with()
        self.mock_view_simultaneous_fit_by_specifier.assert_called_once_with()
        self.mock_view_global_parameters.assert_called_once_with()
        self.mock_view_tf_asymmetry_mode.assert_called_once_with()
        self.mock_model_minimizer.assert_called_once_with(self.minimizer)
        self.mock_model_evaluation_type.assert_called_once_with(
            self.evaluation_type)
        self.mock_model_fit_to_raw.assert_called_once_with(self.fit_to_raw)
        self.mock_model_simultaneous_fit_by.assert_called_once_with(
            self.simultaneous_fit_by)
        self.mock_model_simultaneous_fit_by_specifier.assert_called_once_with(
            self.simultaneous_fit_by_specifier)
        self.mock_model_global_parameters.assert_called_once_with(
            self.global_parameters)
        self.mock_model_tf_asymmetry_mode.assert_called_once_with(
            self.tf_asymmetry_mode)

        self.assertEqual(
            self.view.set_slot_for_fit_generator_clicked.call_count, 1)
        self.assertEqual(self.view.set_slot_for_fit_button_clicked.call_count,
                         1)
        self.assertEqual(self.view.set_slot_for_undo_fit_clicked.call_count, 1)
        self.assertEqual(self.view.set_slot_for_plot_guess_changed.call_count,
                         1)
        self.assertEqual(self.view.set_slot_for_fit_name_changed.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_function_structure_changed.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_function_parameter_changed.call_count, 1)
        self.assertEqual(self.view.set_slot_for_start_x_updated.call_count, 1)
        self.assertEqual(self.view.set_slot_for_end_x_updated.call_count, 1)
        self.assertEqual(self.view.set_slot_for_minimizer_changed.call_count,
                         1)
        self.assertEqual(
            self.view.set_slot_for_evaluation_type_changed.call_count, 1)
        self.assertEqual(self.view.set_slot_for_use_raw_changed.call_count, 1)
        self.assertEqual(self.view.set_slot_for_dataset_changed.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_fitting_mode_changed.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_simultaneous_fit_by_changed.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_simultaneous_fit_by_specifier_changed.
            call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_fitting_type_changed.call_count, 1)
        self.assertEqual(
            self.view.set_slot_for_normalisation_changed.call_count, 1)
 def test_addition(self):
     p = FunctionFactory.createFunction("ProductFunction")
     g = FunctionFactory.createFunction("Gaussian")
     p.add(g)
     p.add(g)
     self.assertEquals(len(p), 2)
 def test_addition(self):
     p = FunctionFactory.createFunction("ProductFunction")
     g = FunctionFactory.createFunction("Gaussian")
     p.add(g)
     p.add(g)
     self.assertEquals(len(p), 2)
 def test_get_Gaussian(self):
     name = "Gaussian"
     func = FunctionFactory.createFunction(name)
     self.assertTrue(func.name() == name)
     self.assertTrue(len(func.__repr__()) > len(name))
     self.assertTrue("Peak" in func.categories())
 def test_type(self):
     p = FunctionFactory.createFunction("ProductFunction")
     self.assertTrue( isinstance(p,ProductFunction) )
     self.assertTrue( isinstance(p,CompositeFunction) )
 def test_length(self):
     p = FunctionFactory.createFunction("ProductFunction")
     self.assertEquals(len(p), 0)
Example #34
0
 def test_category_with_no_override_returns_default_category(self):
     FunctionFactory.subscribe(NoCatgeoryFunction)
     func = FunctionFactory.createFunction("NoCatgeoryFunction")
     self.assertEquals("General", func.category())
     FunctionFactory.unsubscribe("NoCatgeoryFunction")
Example #35
0
 def test_category_override_returns_overridden_result(self):
     FunctionFactory.subscribe(Times2)
     func = FunctionFactory.createFunction("Times2")
     self.assertEquals("SimpleFunction", func.category())
     FunctionFactory.unsubscribe("Times2")
Example #36
0
 def test_instance_can_be_created_from_factory(self):
     FunctionFactory.subscribe(Times2)
     func_name = Times2.__name__
     func = FunctionFactory.createFunction(func_name)
     self.assertTrue(isinstance(func, IFunction1D))
     FunctionFactory.unsubscribe(func_name)
Example #37
0
 def test_get_Gaussian(self):
     name = "Gaussian"
     func = FunctionFactory.createFunction(name)
     self.assertTrue(func.name() == name)
     self.assertTrue(len(func.__repr__()) > len(name))
     self.assertTrue("Peak" in func.categories())
Example #38
0
 def test_instance_can_be_created_from_factory(self):
     FunctionFactory.subscribe(Times2)
     func_name = Times2.__name__
     func = FunctionFactory.createFunction(func_name)
     self.assertTrue(isinstance(func, IFunction1D))
     FunctionFactory.unsubscribe(func_name)
Example #39
0
 def test_category_override_returns_overridden_result(self):
     FunctionFactory.subscribe(Times2)
     func = FunctionFactory.createFunction("Times2")
     self.assertEquals("SimpleFunction", func.category())
     FunctionFactory.unsubscribe("Times2")
Example #40
0
 def test_category_with_no_override_returns_default_category(self):
     FunctionFactory.subscribe(NoCatgeoryFunction)
     func = FunctionFactory.createFunction("NoCatgeoryFunction")
     self.assertEquals("General", func.category())
     FunctionFactory.unsubscribe("NoCatgeoryFunction")