Exemple #1
0
    def write_twk_jcpds(self):
        if not self.model.jcpds_exist():
            return
        idx_checked = [
            s.row() for s in
            self.widget.tableWidget_JCPDS.selectionModel().selectedRows()
        ]

        if idx_checked == []:
            QtWidgets.QMessageBox.warning(
                self.widget, "Warning",
                "Highlight the name of JCPDS to write twk jcpds.")
            return
        if idx_checked.__len__() != 1:
            QtWidgets.QMessageBox.warning(
                self.widget, "Warning",
                "Only one JCPDS card can be written at a time.")
            return
        # get filename to write
        path, __ = os.path.split(self.model.get_base_ptn_filename())
        suggested_filen = os.path.join(
            path, self.model.jcpds_lst[idx_checked[0]].name + '-twk.jcpds')
        filen_twk_jcpds = dialog_savefile(self.widget, suggested_filen)
        if filen_twk_jcpds == '':
            return
        # make comments
        comments = "modified from " + \
            self.model.jcpds_lst[idx_checked[0]].file + \
            ", twk for " + \
            self.model.base_ptn.fname
        self.model.jcpds_lst[idx_checked[0]].write_to_twk_jcpds(
            filen_twk_jcpds, comments=comments)
Exemple #2
0
 def save_bgsubchi(self):
     """
     Save bg subtractd pattern to a chi file
     """
     if not self.model.base_ptn_exist():
         return
     filen_chi_t = self.model.make_filename('bgsub.chi')
     filen_chi = dialog_savefile(self.widget, filen_chi_t)
     if str(filen_chi) == '':
         return
     x, y = self.model.base_ptn.get_bgsub()
     preheader_line0 = \
         '2-theta # BG ROI: {0: .5e}, {1: .5e} \n'.format(
             self.widget.doubleSpinBox_Background_ROI_min.value(),
             self.widget.doubleSpinBox_Background_ROI_max.value())
     preheader_line1 = \
         '2-theta # BG Params: {0: d}, {1: d}, {2: d} \n'.format(
             self.widget.spinBox_BGParam0.value(),
             self.widget.spinBox_BGParam1.value(),
             self.widget.spinBox_BGParam2.value())
     preheader_line2 = '\n'
     writechi(filen_chi,
              x,
              y,
              preheader=preheader_line0 + preheader_line1 + preheader_line2)
Exemple #3
0
 def integrate_to_1d(self):
     azi_list = self._read_azilist()
     if azi_list is None:
         QtWidgets.QMessageBox.warning(self.widget, 'Warning',
                                       'No azimuthal ranges in the queue.')
         return None
     # self.produce_cake()
     self.cakemake_ctrl.read_settings()
     tth = []
     intensity = []
     mid_angle = self.widget.spinBox_AziShift.value()
     for azi_i in azi_list:
         azi_conv = []
         if mid_angle <= 180:
             azi_conv.append(azi_i[2] - mid_angle)
             azi_conv.append(azi_i[4] - mid_angle)
         else:
             azi_conv.append(azi_i[2] + 360 - mid_angle)
             azi_conv.append(azi_i[4] + 360 - mid_angle)
         azi_real = []
         for azi_conv_i in azi_conv:
             if azi_conv_i < -180:
                 azi_real.append(360 + azi_conv_i)
             elif azi_conv_i > 180:
                 azi_real.append(azi_conv_i - 360)
             else:
                 azi_real.append(azi_conv_i)
         tth_i, intensity_i = self.model.diff_img.integrate_to_1d(
             azimuth_range=(azi_real[0], azi_real[1]))
         tth.append(tth_i)
         intensity.append(intensity_i)
     intensity_merged = np.zeros_like(intensity[0])
     for tth_i, intensity_i in zip(tth, intensity):
         if not np.array_equal(tth_i, tth[0]):
             QtWidgets.QMessageBox.warning(self.widget, 'Warning',
                                           'Error occured.  No output.')
             return None
         intensity_merged += intensity_i
     n_azi = azi_list.__len__()
     first_azi = azi_list[0]
     intensity_output = intensity_merged
     ext = "{0:d}_{1:d}_{2:d}.chi".format(n_azi, int(first_azi[2]),
                                          int(first_azi[4]))
     filen_chi_t = self.model.make_filename(ext)
     filen_chi = dialog_savefile(self.widget, filen_chi_t)
     if str(filen_chi) == '':
         return None
     azi_text = '# azi. angles: '
     for azi_i in azi_list:
         azi_text += "({0:.5e}, {1:.5e})".format(azi_i[2], azi_i[4])
     preheader_line0 = azi_text + ' \n'
     preheader_line1 = '2-theta\n'
     preheader_line2 = '\n'
     writechi(filen_chi,
              tth[0],
              intensity_output,
              preheader=preheader_line0 + preheader_line1 + preheader_line2)
     return filen_chi
Exemple #4
0
 def export_to_xls(self):
     """
     Export ucfitlist to an excel file
     """
     if not self.model.ucfit_exist():
         return
     new_filen_xls = self.model.make_filename('ucfit.xls')
     filen_xls = dialog_savefile(self.widget, new_filen_xls)
     if str(filen_xls) == '':
         return
     xls_ucfitlist(filen_xls, self.model.ucfit_lst)
Exemple #5
0
 def _save_cake_marker_file(self):
     azi_list = self._read_azilist()
     if azi_list is None:
         return
     ext = "cake.marker"
     filen_t = self.model.make_filename(ext)
     filen = dialog_savefile(self.widget, filen_t)
     if str(filen) == '':
         return
     with open(filen, "w") as f:
         for s in azi_list:
             f.write(s[0] + ',' + str(s[1]) + ',' + str(s[2]) + ',' +
                     str(s[3]) + ',' + str(s[4]) + '\n')
Exemple #6
0
 def save_xls(self):
     """
     Export jlist to an excel file
     """
     if not self.model.jcpds_exist():
         return
     filen_xls_t = self.model.make_filename('jlist.xls')
     filen_xls = dialog_savefile(self.widget, filen_xls_t)
     if str(filen_xls) == '':
         return
     xls_jlist(filen_xls, self.model.jcpds_lst,
               self.widget.doubleSpinBox_Pressure.value(),
               self.widget.doubleSpinBox_Temperature.value())
Exemple #7
0
 def save_xls(self):
     """
     Export jlist to an excel file
     """
     if not self.model.jcpds_exist():
         return
     temp_dir = get_temp_dir(self.model.get_base_ptn_filename())
     filen_xls_t = make_filename(self.model.get_base_ptn_filename(),
                                 'jlist.xls',
                                 temp_dir=temp_dir)
     filen_xls = dialog_savefile(self.widget, filen_xls_t)
     if str(filen_xls) == '':
         return
     xls_jlist(filen_xls, self.model.jcpds_lst,
               self.widget.doubleSpinBox_Pressure.value(),
               self.widget.doubleSpinBox_Temperature.value())
Exemple #8
0
 def write_jcpds(self):
     if self.file_name == '':
         QtWidgets.QMessageBox.warning(self.widget, "Warning",
                           "Input filename is not given.")
         return
     path, filen, ext = breakdown_filename(self.file_name)
     if ext == '.cif':
         stamp = '-cif-jt'
     else:
         stamp = '-jt'
     filen_default = os.path.join(path, filen + stamp + '.jcpds')
     filen = dialog_savefile(self.widget, filen_default)
     if filen == '':
         return
     comment = str(self.widget.lineEdit_Comment.text())
     int_min = self.widget.doubleSpinBox_MinDsp.value()
     dsp_min = self.widget.doubleSpinBox_MinInt.value()
     self.read_jcpds_values()
     textoutput = self.model.write_to_file(filen, comments = comment, \
                              int_min = int_min, dsp_min=dsp_min, \
                              calculate_1bar_table = True)
Exemple #9
0
    def write_dioptas_jcpds(self):
        QtWidgets.QMessageBox.warning(self.widget, "Warning",
                  "This function is not yet supported.")
        return

        if self.file_name == '':
            QtWidgets.QMessageBox.warning(self.widget, "Warning",
                              "Input filename is not given.")
            return
        path, filen, ext = breakdown_filename(self.file_name)
        stamp = '-dioptas-jt'
        filen_default = os.path.join(path, filen + stamp + '.jcpds')
        filen = dialog_savefile(self.widget, filen_default)
        if filen == '':
            return
        self.model.comments = str(self.widget.lineEdit_Comment.text())
        int_min = self.widget.doubleSpinBox_MinDsp.value()
        dsp_min = self.widget.doubleSpinBox_MinInt.value()
        self.read_jcpds_values()
        self.model.write_to_dioptas_jcpds(filen, int_min=int_min, \
                                          dsp_min=dsp_min)
Exemple #10
0
    def save_cake_format_file(self):
        # make filename
        ext = "cakeformat"
        filen_t = self.model.make_filename(ext)
        print('please be here')
        filen = dialog_savefile(self.widget, filen_t)
        if str(filen) == '':
            return
        # save cake related Values
        names = ['azi_shift', 'int_max', 'min_bar', 'max_bar', 'scale_bar']
        values = [
            self.widget.spinBox_AziShift.value(),
            self.widget.spinBox_MaxCakeScale.value(),
            self.widget.horizontalSlider_VMin.value(),
            self.widget.horizontalSlider_VMax.value(),
            self.widget.horizontalSlider_MaxScaleBars.value()
        ]

        with open(filen, "w") as f:
            for n, v in zip(names, values):
                f.write(n + ' : ' + str(v) + '\n')
Exemple #11
0
    def perform_ucfit(self):
        # get jcpds data in df.  use display to choose data points
        if self.model.section_lst == []:
            QtWidgets.QMessageBox.warning(
                self.widget, "Warning",
                "No peak fitting result exist for this file.")
            return
        if self.phase == None:
            QtWidgets.QMessageBox.warning(
                self.widget, "Warning", "No phase has been chosen for fitting")
            return
        data_by_phase_df = self._get_all_peakfit_results_df()
        data_to_fit_df = data_by_phase_df[self.phase].loc[data_by_phase_df[
            self.phase]['display'] == True]
        # number of data point check
        n_data_points = len(data_to_fit_df.index)
        if n_data_points < 2:
            QtWidgets.QMessageBox.warning(self.widget, "Warning",
                                          "You need at least 2 data points.")
            return
        # perform ucfit
        text_output = self.phase + '\n\n'
        text_output += 'Fitted unit cell parameters \n'
        text_output += 'Crystal system = ' + \
            self.widget.comboBox_Symmetry.currentText() + '\n'
        wavelength = self.model.get_base_ptn_wavelength()
        if self.widget.comboBox_Symmetry.currentText() == 'cubic':
            a, s_a, v, s_v, res_lin, res_nlin = fit_cubic_cell(data_to_fit_df,
                                                               wavelength,
                                                               verbose=False)
            cell_params = [a, a, a]
            text_output += "a = {0:.5f} +/- {1:.5f} \n".format(a, s_a)
            text_output += "V = {0:.5f} +/- {1:.5f} \n\n".format(v, s_v)
        elif self.widget.comboBox_Symmetry.currentText() == 'tetragonal':
            if n_data_points < 3:
                QtWidgets.QMessageBox.warning(
                    self.widget, "Warning",
                    "You need at least 3 data points for tetragonal.")
                return
            a, s_a, c, s_c, v, s_v, res_lin, res_nlin = \
                fit_tetragonal_cell(data_to_fit_df, wavelength,
                                    verbose=False)
            cell_params = [a, a, c]
            text_output += "a = {0:.5f} +/- {1:.5f} \n".format(a, s_a)
            text_output += "c = {0:.5f} +/- {1:.5f} \n".format(c, s_c)
            text_output += "V = {0:.5f} +/- {1:.5f} \n\n".format(v, s_v)
        elif self.widget.comboBox_Symmetry.currentText() == 'hexagonal':
            if n_data_points < 3:
                QtWidgets.QMessageBox.warning(
                    self.widget, "Warning",
                    "You need at least 3 data points for hexagonal.")
                return
            a, s_a, c, s_c, v, s_v, res_lin, res_nlin = \
                fit_hexagonal_cell(data_to_fit_df, wavelength,
                                  verbose=False)
            cell_params = [a, a, c]
            text_output += "a = {0:.5f} +/- {1:.5f} \n".format(a, s_a)
            text_output += "c = {0:.5f} +/- {1:.5f} \n".format(c, s_c)
            text_output += "V = {0:.5f} +/- {1:.5f} \n\n".format(v, s_v)
        elif self.widget.comboBox_Symmetry.currentText() == 'orthorhombic':
            if n_data_points < 4:
                QtWidgets.QMessageBox.warning(
                    self.widget, "Warning",
                    "You need at least 4 data points for orthorhombic.")
                return
            a, s_a, b, s_b, c, s_c, v, s_v, res_lin, res_nlin = \
                fit_orthorhombic_cell(data_to_fit_df, wavelength,
                                      verbose=False)
            cell_params = [a, b, c]
            text_output += "a = {0:.5f} +/- {1:.5f} \n".format(a, s_a)
            text_output += "b = {0:.5f} +/- {1:.5f} \n".format(b, s_b)
            text_output += "c = {0:.5f} +/- {1:.5f} \n".format(c, s_c)
            text_output += "V = {0:.5f} +/- {1:.5f} \n\n".format(v, s_v)
        # output results
        output_df = make_output_table(res_lin, res_nlin, data_to_fit_df)
        text_output += 'Output table\n'
        text_output += output_df[[
            'h', 'k', 'l', 'twoth', 'dsp', 'twoth residue'
        ]].to_string()
        text_output += '\n\nHat: influence for the fit result. \n'
        text_output += '     1 ~ large influence, 0 ~ no influence.\n'
        text_output += output_df[['h', 'k', 'l', 'twoth', 'dsp',
                                  'hat']].to_string()
        text_output += '\n\nRstudent: how much the parameter would change' + \
            ' if deleted.\n'
        text_output += output_df[['h', 'k', 'l', 'twoth', 'dsp',
                                  'Rstudent']].to_string()
        text_output += '\n\ndfFits: deletion diagnostic giving' + \
            ' the change in\n'
        text_output += '        the predicted value twotheta.\n'
        text_output += '        upon deletion of the data point as a ' + \
            'multiple of\n'
        text_output += '        the standard deviation for 1/d-spacing^2.\n'
        text_output += output_df[['h', 'k', 'l', 'twoth', 'dsp',
                                  'dfFits']].to_string()
        text_output += '\n\ndfBetas: normalized residual\n'
        text_output += output_df[['h', 'k', 'l', 'twoth', 'dsp',
                                  'dfBetas']].to_string()
        text_output += '\n\nNon-linear fit statistics \n'
        text_output += lmfit.fit_report(res_nlin)

        self.widget.plainTextEdit_UCFitOutput.setPlainText(text_output)

        # save jcpds and save output file automatically.
        # ask for filename.  at the moment, simply overwrite
        temp_dir = get_temp_dir(self.model.get_base_ptn_filename())
        ext = "ucfit.jcpds"
        #filen_t = self.model.make_filename(ext)
        filen_t = make_filename(self.template_jcpds.file,
                                ext,
                                temp_dir=temp_dir)
        filen_j = dialog_savefile(self.widget, filen_t)
        if str(filen_j) == '':
            return
        self._write_to_jcpds(filen_j, cell_params)

        # write to a textfile
        ext = "ucfit.output"
        #filen_t = self.model.make_filename(ext)
        filen_t = make_filename(self.template_jcpds.file,
                                ext,
                                temp_dir=temp_dir)
        filen_o = dialog_savefile(self.widget, filen_t)
        if str(filen_o) == '':
            return

        with open(filen_o, "w") as f:
            f.write(text_output)