Exemplo n.º 1
0
    def run(self, data, output_dir, **run_args):

        results = {}

        if 'q0' not in run_args or run_args['q0'] is None:
            # Determine q based on circular_average_q2I_fit

            head, tail = os.path.split(output_dir)
            result_file = os.path.join(head, 'results',
                                       '{}.xml'.format(data.name))
            prev_results = tools.get_result_xml(result_file,
                                                'circular_average_q2I')

            run_args['q0'] = prev_results['fit_peaks_x_center1']

        # Extract circular average
        #line = data.linecut_angle(q0=run_args['q0'], dq=run_args['dq'])
        line = data.linecut_angle(**run_args)

        if 'show_region' in run_args and run_args['show_region']:
            data.plot(show=True)

        # Basic output
        outfile = self.get_outfile(data.name, output_dir, ext='.dat')
        line.save_data(outfile)

        # Fine-tune data
        line.x = line.x[1:]
        line.y = line.y[1:]
        line.smooth(2.0)

        # Fit
        class DataLines_current(DataLines):
            def _plot_extra(self, **plot_args):

                v_spacing = 0.1

                xi, xf, yi, yf = self.ax.axis()

                yp = yf
                s = '$\eta = {:.2f}$'.format(self.results['fit_eta_eta'])
                self.ax.text(xi,
                             yp,
                             s,
                             size=30,
                             color='b',
                             verticalalignment='top',
                             horizontalalignment='left')

                yp -= (yf - yi) * v_spacing
                s = '$S_{{\eta}} = {:.3f}$'.format(
                    self.results['fit_library_eta_S'])
                self.ax.text(xi,
                             yp,
                             s,
                             size=30,
                             color='b',
                             verticalalignment='top',
                             horizontalalignment='left')

                yp = yf
                s = '$m = {:.2f}$'.format(self.results['fit_MaierSaupe_m'])
                self.ax.text(xf,
                             yp,
                             s,
                             size=30,
                             color='purple',
                             verticalalignment='top',
                             horizontalalignment='right')

                yp -= (yf - yi) * v_spacing
                s = '$S_{{m}} = {:.3f}$'.format(
                    self.results['fit_library_MaierSaupe_S'])
                self.ax.text(xf,
                             yp,
                             s,
                             size=30,
                             color='purple',
                             verticalalignment='top',
                             horizontalalignment='right')

                self.ax.set_xticks([-180, -90, 0, +90, +180])

        lines = DataLines_current(
            []
        )  # Not sure why the lines=[] argument needs to be specified here...
        lines.add_line(line)
        lines.copy_labels(line)
        lines.results = {}

        color_list = [
            'b',
            'purple',
            'r',
            'green',
            'orange',
        ]
        for i, fit_name in enumerate(['fit_eta', 'fit_MaierSaupe']):

            lm_result, fit_line, fit_line_e = getattr(self,
                                                      fit_name)(line,
                                                                **run_args)
            fit_line_e.plot_args['color'] = color_list[i % len(color_list)]
            lines.add_line(fit_line_e)

            #prefactor_total = 0
            for param_name, param in lm_result.params.items():
                results['{}_{}'.format(fit_name, param_name)] = {
                    'value': param.value,
                    'error': param.stderr,
                }
                lines.results['{}_{}'.format(fit_name,
                                             param_name)] = param.value
                #if 'prefactor' in param_name:
                #prefactor_total += np.abs(param.value)

            #results['{}_prefactor_total'.format(fit_name)] = prefactor_total
            results['{}_chi_squared'.format(
                fit_name)] = lm_result.chisqr / lm_result.nfree

        # Special fit
        fit_result, fit_line = self.fit_library(line,
                                                library='eta',
                                                **run_args)
        fit_line.plot_args['color'] = 'blue'
        fit_line.plot_args['linewidth'] = 1.0
        lines.results['fit_library_eta_S'] = fit_result['S']
        lines.add_line(fit_line)
        results.update(self.prepend_keys(fit_result, 'fit_library_eta_'))

        fit_result, fit_line = self.fit_library(line,
                                                library='MaierSaupe',
                                                **run_args)
        fit_line.plot_args['color'] = 'purple'
        fit_line.plot_args['linewidth'] = 1.0
        lines.results['fit_library_MaierSaupe_S'] = fit_result['S']
        results.update(self.prepend_keys(fit_result,
                                         'fit_library_MaierSaupe_'))
        lines.add_line(fit_line)

        # Output
        if run_args['verbosity'] >= 1:
            outfile = self.get_outfile(data.name, output_dir)
            lines.lines.reverse()
            lines.plot(save=outfile, **run_args)

        if run_args['verbosity'] >= 4:
            outfile = self.get_outfile(data.name, output_dir, ext='_polar.png')
            line.plot_polar(save=outfile, **run_args)

        return results
Exemplo n.º 2
0
    def run(self, data, output_dir, **run_args):

        results = {}

        if 'q0' not in run_args or run_args['q0'] is None:
            # Determine q based on circular_average_q2I_fit

            head, tail = os.path.split(output_dir)
            result_file = os.path.join(head, 'results',
                                       '{}.xml'.format(data.name))
            prev_results = tools.get_result_xml(result_file,
                                                'circular_average_q2I')

            run_args['q0'] = prev_results['fit_peaks_x_center1']

        # Extract circular average
        #line = data.linecut_angle(q0=run_args['q0'], dq=run_args['dq'])
        line = data.linecut_angle(**run_args)

        # Remove the data near chi=0 since this is artificially high (specular ridge)
        #line.kill_x(0, 3)

        if 'show_region' in run_args and run_args['show_region']:
            data.plot(show=True)

        # Basic output
        outfile = self.get_outfile(data.name, output_dir, ext='.dat')
        #line.save_data(outfile)

        # Fine-tune data
        line.x = line.x[1:]
        line.y = line.y[1:]
        line.smooth(2.0)

        do_max = True
        do_FWHM = False
        do_fits = True

        # Fit
        class DataLines_current(DataLines):
            def _plot_extra(self, **plot_args):

                v_spacing = 0.1

                xi, xf, yi, yf = self.ax.axis()

                #yi = min( self.lines[-1].y )*0.5
                #yf = max( self.lines[-1].y )*1.5
                #self.ax.axis( [xi, xf, yi, yf] )

                self.ax.axvline(self.angle, color='r', linewidth=1.0)

                s = '$\chi = {:.1f} ^{{\circ}} $'.format(self.angle)
                self.ax.text(self.angle,
                             yf,
                             s,
                             size=30,
                             color='r',
                             verticalalignment='top',
                             horizontalalignment='left')
                s = '$f = {:.3f}$'.format(self.orientation_factor)
                self.ax.text(self.angle,
                             yi,
                             s,
                             size=30,
                             color='r',
                             verticalalignment='bottom',
                             horizontalalignment='left')

                if do_max:
                    line = self.lines[-1]
                    xpeak, ypeak = line.target_y(np.max(line.y))
                    self.ax.plot([xpeak], [ypeak],
                                 'o-',
                                 color='b',
                                 markersize=12,
                                 linewidth=1.0)
                    self.ax.axvline(xpeak, color='b', linewidth=1.0)

                    s = '$\chi = {:.1f} ^{{\circ}} $'.format(xpeak)
                    self.ax.text(xpeak,
                                 ypeak,
                                 s,
                                 size=30,
                                 color='b',
                                 verticalalignment='bottom',
                                 horizontalalignment='left')

                if do_fits:
                    yp = yf
                    s = '$\eta = {:.2f}$'.format(self.results['fit_eta_eta'])
                    self.ax.text(xi,
                                 yp,
                                 s,
                                 size=30,
                                 color='b',
                                 verticalalignment='top',
                                 horizontalalignment='left')

                    yp = yf
                    s = '$m = {:.2f}$'.format(self.results['fit_MaierSaupe_m'])
                    self.ax.text(xf,
                                 yp,
                                 s,
                                 size=30,
                                 color='purple',
                                 verticalalignment='top',
                                 horizontalalignment='right')

                if do_FWHM:
                    # FWHM
                    line = self.lines[0]
                    xt, yt = line.target_y(max(line.y))
                    hm = yt * 0.5

                    # Right (+) side
                    line_temp = line.sub_range(xt, +60)
                    xtr, ytr = line_temp.target_y(hm)

                    # Left (-) side
                    line_temp = line.sub_range(-60, xt)
                    xtl, ytl = line_temp.target_y(hm)

                    self.ax.plot([xtl, xtr], [ytl, ytr],
                                 'o-',
                                 color='b',
                                 markersize=8,
                                 linewidth=1.0)
                    s = r'$\mathrm{{FWHM}} = {:.1f}^{{\circ}}$'.format(
                        abs(xtr - xtl))
                    self.ax.text(xtr,
                                 ytr,
                                 s,
                                 color='b',
                                 size=20,
                                 verticalalignment='center',
                                 horizontalalignment='left')

                #self.ax.set_xticks([-180, -90, 0, +90, +180])

        lines = DataLines_current(
            []
        )  # Not sure why the lines=[] argument needs to be specified here...
        lines.add_line(line)
        lines.copy_labels(line)
        lines.results = {}

        if do_fits:
            color_list = [
                'b',
                'purple',
                'r',
                'green',
                'orange',
            ]
            for i, fit_name in enumerate(
                ['fit_eta', 'fit_MaierSaupe', 'fit_eta_span']):

                lm_result, fit_line, fit_line_e = getattr(self,
                                                          fit_name)(line,
                                                                    **run_args)
                fit_line.plot_args['color'] = color_list[i % len(color_list)]
                fit_line_e.plot_args['color'] = color_list[i % len(color_list)]

                lines.add_line(fit_line_e)
                if fit_name == 'fit_eta_span':
                    lines.add_line(fit_line)

                #prefactor_total = 0
                for param_name, param in lm_result.params.items():
                    results['{}_{}'.format(fit_name, param_name)] = {
                        'value': param.value,
                        'error': param.stderr,
                    }
                    lines.results['{}_{}'.format(fit_name,
                                                 param_name)] = param.value
                    #if 'prefactor' in param_name:
                    #prefactor_total += np.abs(param.value)

                #results['{}_prefactor_total'.format(fit_name)] = prefactor_total
                results['{}_chi_squared'.format(
                    fit_name)] = lm_result.chisqr / lm_result.nfree

        angle = results['fit_eta_span_x_center']['value']
        if angle < 0:
            angle += 180
        # angle is now 0 to +190
        # Convert to the 'in between the peaks' convention
        angle -= 90
        # angle is now -90 to +90
        lines.angle = angle

        qz_unit = np.cos(np.radians(angle))
        qx_unit = np.sin(np.radians(angle))
        orientation_factor = 2 * np.square(qz_unit) - 1
        lines.orientation_factor = orientation_factor

        results['orientation_angle'] = angle
        results['orientation_factor'] = orientation_factor

        # Output
        if run_args['verbosity'] >= 1:
            outfile = self.get_outfile(data.name, output_dir)
            lines.lines.reverse()
            lines.plot(save=outfile, **run_args)

        if run_args['verbosity'] >= 4:
            outfile = self.get_outfile(data.name, output_dir, ext='_polar.png')
            line.plot_polar(save=outfile, **run_args)

        return results