Exemple #1
0
    def __init__(self, s_max=9.0, n_step=4096, fft_type=FFTTypes.REAL_ONLY):
        congruence.checkStrictlyPositiveNumber(s_max, "S_max")
        congruence.checkStrictlyPositiveNumber(n_step, "N_step")

        n_step = int(n_step)

        if not ((n_step & (n_step - 1)) == 0):
            raise ValueError("N_step should be a power of 2")
        if not (fft_type == FFTTypes.REAL_ONLY or fft_type == FFTTypes.FULL):
            raise ValueError("FFT type not recognized")

        self.s_max = s_max
        self.n_step = n_step
        self.fft_type = fft_type
Exemple #2
0
    def send_fit_initialization(self):
        try:
            if not self.fit_global_parameters is None:
                congruence.checkStrictlyPositiveNumber(self.s_max, "S Max")

                self.fit_global_parameters.fit_initialization.fft_parameters = FFTInitParameters(
                    s_max=self.s_max,
                    n_step=int(self.cb_n_step.currentText()),
                    fft_type=self.fft_type)

                self.send("Fit Global Parameters", self.fit_global_parameters)

        except Exception as e:
            QMessageBox.critical(self, "Error", str(e), QMessageBox.Ok)

            if self.IS_DEVELOP: raise e
Exemple #3
0
    def send_debye_waller(self):
        try:
            if not self.fit_global_parameters is None:
                send_data = True

                if self.use_debye_waller_factor == 1 and not self.debye_waller_factor_function == 1:
                    congruence.checkStrictlyPositiveNumber(
                        self.debye_waller_factor, "B")

                    if self.fit_global_parameters.fit_initialization.crystal_structures is None:
                        raise ValueError(
                            "Add Crystal Structure(s) before this widget")

                    if not self.fit_global_parameters.fit_initialization.crystal_structures[
                            0].use_structure:
                        send_data = ConfirmDialog.confirmed(
                            parent=self,
                            message=
                            "Debye-Waller factor is better refined when the structural model is activated.\nProceed anyway?"
                        )

                if send_data:
                    debye_waller_factor = None if self.use_debye_waller_factor == 0 else self.populate_parameter(
                        "debye_waller_factor",
                        ThermalPolarizationParameters.get_parameters_prefix())
                    if not debye_waller_factor is None:
                        debye_waller_factor.rescale(
                            0.01)  # CONVERSIONE from A-2 to nm-2

                    if self.fit_global_parameters.fit_initialization.thermal_polarization_parameters is None:
                        self.fit_global_parameters.fit_initialization.thermal_polarization_parameters = [
                            ThermalPolarizationParameters(
                                debye_waller_factor=debye_waller_factor)
                        ]
                    else:
                        self.fit_global_parameters.fit_initialization.thermal_polarization_parameters[
                            0].debye_waller_factor = debye_waller_factor

                    self.send("Fit Global Parameters",
                              self.fit_global_parameters)

        except Exception as e:
            QMessageBox.critical(self, "Error", str(e), QMessageBox.Ok)

            if self.IS_DEVELOP: raise e
Exemple #4
0
    def send_peak_displacement(self):
        try:
            if not self.fit_global_parameters is None:
                congruence.checkStrictlyPositiveNumber(self.goniometer_radius, "Goniometer Radius")

                displacement = self.populate_parameter("displacement", SpecimenDisplacement.get_parameters_prefix())
                displacement.rescale(1e-6) # to m

                self.fit_global_parameters.set_shift_parameters([SpecimenDisplacement(goniometer_radius=self.goniometer_radius, displacement=displacement)])

                self.send("Fit Global Parameters", self.fit_global_parameters)

        except Exception as e:
            QMessageBox.critical(self, "Error",
                                 str(e),
                                 QMessageBox.Ok)

            if self.IS_DEVELOP: raise e
    def send_strain(self):
        try:
            if not self.fit_global_parameters is None:
                if not self.rho_function==1: congruence.checkStrictlyPositiveNumber(self.rho, "\u03c1")
                if not self.Re_function==1: congruence.checkStrictlyPositiveNumber(self.Re, "Re")
                if not self.mix_function==1: congruence.checkPositiveNumber(self.mix, "mix")
                if not self.b_function==1: congruence.checkStrictlyPositiveNumber(self.b, "b")

                self.fit_global_parameters.strain_parameters = [KrivoglazWilkensModel(rho=self.populate_parameter("rho", KrivoglazWilkensModel.get_parameters_prefix()),
                                                                                      Re=self.populate_parameter("Re", KrivoglazWilkensModel.get_parameters_prefix()),
                                                                                      Ae=self.populate_parameter("Ae", KrivoglazWilkensModel.get_parameters_prefix()),
                                                                                      Be=self.populate_parameter("Be", KrivoglazWilkensModel.get_parameters_prefix()),
                                                                                      As=self.populate_parameter("As", KrivoglazWilkensModel.get_parameters_prefix()),
                                                                                      Bs=self.populate_parameter("Bs", KrivoglazWilkensModel.get_parameters_prefix()),
                                                                                      mix=self.populate_parameter("mix", KrivoglazWilkensModel.get_parameters_prefix()),
                                                                                      b=self.populate_parameter("b", KrivoglazWilkensModel.get_parameters_prefix()))]

                self.send("Fit Global Parameters", self.fit_global_parameters)

        except Exception as e:
            QMessageBox.critical(self, "Error",
                                 str(e),
                                 QMessageBox.Ok)

            if self.IS_DEVELOP: raise e
    def send_size(self):
        try:
            if not self.fit_global_parameters is None:
                if not self.mu_function == 1:
                    congruence.checkStrictlyPositiveNumber(
                        self.mu, "\u03bc or D")
                if self.cb_distribution.currentText(
                ) == Distribution.LOGNORMAL and not self.sigma_function == 1:
                    congruence.checkStrictlyPositiveNumber(
                        self.sigma, "\u03c3")
                if self.cb_distribution.currentText(
                ) == Distribution.DELTA and not self.fit_global_parameters.fit_initialization.crystal_structures[
                        0].use_structure:
                    raise Exception(
                        "Delta Distribution cannot be used when the structural model is not activated"
                    )

                self.fit_global_parameters.size_parameters = [
                    SizeParameters(
                        shape=self.cb_shape.currentText(),
                        distribution=self.cb_distribution.currentText(),
                        mu=self.populate_parameter(
                            "mu", SizeParameters.get_parameters_prefix()),
                        sigma=None if self.cb_distribution.currentText()
                        == Distribution.DELTA else self.populate_parameter(
                            "sigma", SizeParameters.get_parameters_prefix()),
                        add_saxs=self.add_saxs
                        if self.cb_distribution.currentText()
                        == Distribution.DELTA else False)
                ]

                self.send("Fit Global Parameters", self.fit_global_parameters)

        except Exception as e:
            QMessageBox.critical(self, "Error", str(e), QMessageBox.Ok)

            if self.IS_DEVELOP: raise e
    def send_contrast_factor_a_b(self):
        try:
            if not self.fit_global_parameters is None:
                if self.fit_global_parameters.fit_initialization is None:
                    raise ValueError(
                        "Calculation is not possibile, Crystal Structure is missing"
                    )

                if self.fit_global_parameters.fit_initialization.crystal_structures is None:
                    raise ValueError(
                        "Calculation is not possibile, Crystal Structure is missing"
                    )

                congruence.checkStrictlyPositiveNumber(self.c11, "c11")
                congruence.checkStrictlyPositiveNumber(self.c12, "c12")
                congruence.checkStrictlyPositiveNumber(self.c44, "c44")

                symmetry = self.fit_global_parameters.fit_initialization.crystal_structures[
                    0].symmetry

                Ae, Be, As, Bs = calculate_A_B_coefficients(
                    self.c11, self.c12, self.c44, symmetry)

                text = "Ae = " + str(Ae) + "\n"
                text += "Be = " + str(Be) + "\n"
                text += "As = " + str(As) + "\n"
                text += "Bs = " + str(Bs) + "\n"

                self.text_area.setText(text)

                self.fit_global_parameters.strain_parameters = [
                    KrivoglazWilkensModel(
                        Ae=FitParameter(parameter_name=KrivoglazWilkensModel.
                                        get_parameters_prefix() + "Ae",
                                        value=Ae,
                                        fixed=True),
                        Be=FitParameter(parameter_name=KrivoglazWilkensModel.
                                        get_parameters_prefix() + "Be",
                                        value=Be,
                                        fixed=True),
                        As=FitParameter(parameter_name=KrivoglazWilkensModel.
                                        get_parameters_prefix() + "As",
                                        value=As,
                                        fixed=True),
                        Bs=FitParameter(parameter_name=KrivoglazWilkensModel.
                                        get_parameters_prefix() + "Bs",
                                        value=Bs,
                                        fixed=True))
                ]

                self.send("Fit Global Parameters", self.fit_global_parameters)

        except Exception as e:
            QMessageBox.critical(self, "Error", str(e), QMessageBox.Ok)

            if self.IS_DEVELOP: raise e
    def do_fit(self):
        try:
            if not self.fit_global_parameters is None:
                self.set_plot_options_enabled(False)
                self.stop_fit = False

                congruence.checkStrictlyPositiveNumber(self.n_iterations, "Nr. Iterations")

                if self.fit_global_parameters.fit_initialization is None:
                    raise ValueError("Mandatory widgets (Load Data/Fit Initialization/Crystal Structure) are totally missing.")

                if self.fit_global_parameters.fit_initialization.fft_parameters is None:
                    raise ValueError("FFT parameters is missing: add the proper widget before the Fitter")

                if self.fit_global_parameters.fit_initialization.diffraction_patterns is None:
                    raise ValueError("Diffraction Pattern is missing: add the proper widget before the Fitter")

                if self.fit_global_parameters.fit_initialization.crystal_structures is None:
                    raise ValueError("Crystal Structure is missing: add the proper widget before the Fitter")

                self.fit_global_parameters.set_n_max_iterations(self.n_iterations)
                self.fit_global_parameters.set_convergence_reached(False)
                self.fit_global_parameters.free_output_parameters.parse_formulas(self.free_output_parameters_text)

                initial_fit_global_parameters = self.fit_global_parameters.duplicate()

                if self.is_incremental == 1 and self.current_iteration > 0:
                    if len(initial_fit_global_parameters.get_parameters()) != len(self.fitter.fit_global_parameters.get_parameters()):
                        raise Exception("Incremental Fit is not possibile!\n\nParameters in the last fitting procedure are incompatible with the received ones")

                sys.stdout = EmittingStream(textWritten=self.write_stdout)

                if self.is_incremental == 0 or (self.is_incremental == 1 and self.current_iteration == 0):
                    self.fitter = FitterFactory.create_fitter(fitter_name=self.cb_fitter.currentText())

                    self.fitter.init_fitter(initial_fit_global_parameters)
                    self.current_wss = []
                    self.current_gof = []
                    self.current_iteration = 0 if self.is_incremental == 0 else self.current_iteration

                self.fitted_fit_global_parameters = initial_fit_global_parameters
                self.current_running_iteration = 0

                try:
                    self.fit_thread = FitThread(self)
                    self.fit_thread.begin.connect(self.fit_begin)
                    self.fit_thread.update.connect(self.fit_update)
                    self.fit_thread.finished.connect(self.fit_completed)
                    self.fit_thread.error.connect(self.fit_error)
                    self.fit_thread.start()
                except Exception as e:
                    raise FitNotStartedException(str(e))

        except Exception as e:
            QMessageBox.critical(self, "Error",
                                 str(e),
                                 QMessageBox.Ok)

            self.set_plot_options_enabled(True)

            if self.IS_DEVELOP: raise e

        self.setStatusMessage("")
        self.progressBarFinished()