def y_gap_for_fitting(params,
                      exp_x_interp,
                      exp_y_interp,
                      layer_list,
                      energy_min,
                      energy_max,
                      energy_step,
                      database,
                      each_step=False):
    parvals = params.valuesdict()
    simulation = Simulation(energy_min=energy_min,
                            energy_max=energy_max,
                            energy_step=energy_step,
                            database=database)
    for each_layer in layer_list:
        simulation.add_layer(
            layer=each_layer,
            thickness_mm=parvals['thickness_mm_' + each_layer],
            density_gcm3=parvals['density_gcm3_' + each_layer])
    simu_x = simulation.get_x(x_type='energy')
    simu_y = simulation.get_y(y_type='attenuation')
    gap = (exp_y_interp - simu_y)  # ** 2

    if each_step is True:
        for each_layer in layer_list:
            print(
                "Trying: density_gcm3_{}: {}    thickness_mm_{}: {}    chi^2: {}"
                .format(each_layer, parvals['density_gcm3_' + each_layer],
                        each_layer, parvals['thickness_mm_' + each_layer],
                        sum((exp_y_interp - simu_y)**2)))
    return gap
Esempio n. 2
0
    def __init__(
            self,
            # Initialize ResoFit.experiment
            spectra_file: str,
            data_file: str,
            folder: str,
            exp_source_to_detector_m,
            exp_offset_us,
            baseline: bool,
            baseline_deg: int,
            # Initialize ResoFit.simulation
            layer: fit_util.Layer,
            energy_min,
            energy_max,
            energy_step,
            database: str,
            x_type: str,
            y_type: str):
        """
        Initialization with passed file location and sample info

        :param spectra_file:
        :type spectra_file:
        :param data_file:
        :type data_file:
        :param layer: Layer()
        :type layer:
        :param energy_min:
        :type energy_min:
        :param energy_max:
        :type energy_max:
        :param energy_step:
        :type energy_step:
        :param folder:
        :type folder:
        :param baseline: True -> to remove baseline/background by detrend
        :type baseline: boolean
        """
        self.x_type = x_type
        self.y_type = y_type
        self.energy_min = energy_min
        self.energy_max = energy_max
        self.energy_step = energy_step
        self.simulation = Simulation(energy_min=energy_min,
                                     energy_max=energy_max,
                                     energy_step=energy_step,
                                     database=database)
        self.simulation.add_Layer(layer=layer)
        self.experiment = Experiment(
            spectra_file=spectra_file,
            data_file=data_file,
            folder=folder,
            source_to_detector_m=exp_source_to_detector_m,
            offset_us=exp_offset_us,
            baseline=baseline,
            baseline_deg=baseline_deg)
        self.experiment.t_start_us = self.experiment.t_start_us + _exp_time_offset_us
        self.init_source_to_detector_m = exp_source_to_detector_m
        self.init_offset_us = exp_offset_us
        self.calibrated_offset_us = None
        self.calibrated_source_to_detector_m = None
        self.calibrate_result = None
        self.params_to_calibrate = None
class TestPeaks(unittest.TestCase):
    energy_min = 7
    energy_max = 150
    energy_step = 0.01
    simulation = Simulation(energy_min=energy_min,
                            energy_max=energy_max,
                            energy_step=energy_step,
                            database='_data_for_unittest')
    simulation.add_layer(layer='U', thickness_mm=0.05)
    x = simulation.o_reso.stack_sigma['U']['U']['energy_eV']
    y = simulation.o_reso.stack_sigma['U']['U']['sigma_b']

    def test_findpeak1(self):
        peak_found = fit_util._find_peak(y=self.y,
                                         x=self.x,
                                         thres=0.015,
                                         min_dist=1,
                                         imprv_reso=False)
        peak_expected = {
            'x': [20.87, 36.68, 66.03, 80.75, 102.57, 116.91],
            'y': [
                9801.184720322835, 13337.61249582717, 4356.4307835150385,
                276.22478464487637, 6022.958717161858, 2003.9245670422251
            ],
        }
        self.assertDictEqual(peak_found, peak_expected)

    def test_findpeak2(self):
        peak_found = fit_util._find_peak(y=self.y,
                                         x=self.x,
                                         thres=0.015,
                                         min_dist=1,
                                         imprv_reso=True)
        peak_expected = {
            'x': [
                20.87274280816388, 36.68474982964769, 66.03358430830164,
                80.75548785869171, 102.56856740218703, 116.91012795048718
            ],
            'y': [
                9801.184720322835, 13337.61249582717, 4356.4307835150385,
                276.22478464487637, 6022.958717161858, 2003.9245670422251
            ]
        }
        self.assertDictEqual(peak_found, peak_expected)

    def test_findpeak3(self):
        peak_found = fit_util._find_peak(y=self.y,
                                         thres=0.015,
                                         min_dist=1,
                                         imprv_reso=False)
        peak_expected = {
            'x': [1387, 2968, 5903, 7375, 9557, 10991],
            'y': [
                9801.184720322835, 13337.61249582717, 4356.4307835150385,
                276.22478464487637, 6022.958717161858, 2003.9245670422251
            ]
        }
        self.assertDictEqual(peak_found, peak_expected)

    def test_findpeak4(self):
        peak_found = fit_util._find_peak(y=self.y,
                                         thres=0.015,
                                         min_dist=1,
                                         imprv_reso=True)
        peak_expected = {
            'x': [
                1387.2742808163878, 2968.47498296478, 5903.358430830144,
                7375.5487858692095, 9556.85674021868, 10991.012795048715
            ],
            'y': [
                9801.184720322835, 13337.61249582717, 4356.4307835150385,
                276.22478464487637, 6022.958717161858, 2003.9245670422251
            ]
        }
        print(peak_found)
        self.assertDictEqual(peak_found, peak_expected)

    def test_indexes(self):
        x = self.simulation.o_reso.stack_sigma['U']['U']['energy_eV']
        y = self.simulation.o_reso.stack_sigma['U']['U']['sigma_b']
        peak_df = fit_util.find_peak(y=y,
                                     x=x,
                                     x_name='x',
                                     thres=0.015,
                                     min_dist=1)
        peak_df_expected = {
            'x': [20.87, 36.68, 66.03, 80.75, 102.57, 116.91],
            'y': [
                9801.18472032, 13337.61249583, 4356.43078352, 276.22478464,
                6022.95871716, 2003.92456704
            ],
        }
        assert peak_df['x'].tolist() == pytest.approx(peak_df_expected['x'])
        assert peak_df['y'].tolist() == pytest.approx(peak_df_expected['y'])

        peak_df = fit_util.find_peak(y=y, x_name='x', thres=0.015, min_dist=1)
        peak_df_expected = {
            'x': [1387, 2968, 5903, 7375, 9557, 10991],
            'y': [
                9801.18472032, 13337.61249583, 4356.43078352, 276.22478464,
                6022.95871716, 2003.92456704
            ],
        }
        assert peak_df['x'].tolist() == pytest.approx(peak_df_expected['x'])
        assert peak_df['y'].tolist() == pytest.approx(peak_df_expected['y'])
Esempio n. 4
0
    def fit(self, raw_layer: fit_util.Layer, vary='density', each_step=False):
        if vary not in ['density', 'thickness', 'none']:
            raise ValueError(
                "'vary=' can only be one of ['density', 'thickness', 'none']")
        # Default vary is: 'density'
        self.sample_vary = vary
        thickness_vary_tag = False
        density_vary_tag = True
        if vary == 'thickness':
            thickness_vary_tag = True
            density_vary_tag = False
        if vary == 'none':
            density_vary_tag = False
        self.raw_layer = raw_layer
        '''Load params'''
        print(raw_layer)
        self.layer_list = list(raw_layer.info.keys())
        self.params_for_fit = Parameters()
        for _each_layer in self.layer_list:
            if self.raw_layer.info[_each_layer]['density']['value'] is np.NaN:
                self.raw_layer.info[_each_layer]['density'][
                    'value'] = pt.elements.isotope(_each_layer).density
            self.params_for_fit.add(
                'thickness_mm_' + _each_layer,
                value=self.raw_layer.info[_each_layer]['thickness']['value'],
                vary=thickness_vary_tag,
                min=0)
            self.params_for_fit.add(
                'density_gcm3_' + _each_layer,
                value=self.raw_layer.info[_each_layer]['density']['value'],
                vary=density_vary_tag,
                min=0)
        # Print before
        print(
            "+----------------- Fitting ({}) -----------------+\nParams before:"
            .format(vary))
        self.params_for_fit.pretty_print()
        # Fitting
        self.fit_result = minimize(y_gap_for_fitting,
                                   self.params_for_fit,
                                   method='leastsq',
                                   args=(self.exp_x_interp, self.exp_y_interp,
                                         self.layer_list, self.energy_min,
                                         self.energy_max, self.energy_step,
                                         self.database, each_step))
        # Print after
        print("\nParams after:")
        self.fit_result.__dict__['params'].pretty_print()
        # Print chi^2
        self.fitted_residual = self.fit_result.__dict__['residual']
        print("Fitting chi^2 : {}\n".format(sum(self.fitted_residual**2)))
        '''Export fitted params as Layer()'''

        # Save the fitted 'density' or 'thickness' in Layer()
        self.fitted_layer = Layer()
        for _each_layer in self.layer_list:
            self.fitted_layer.add_layer(
                layer=_each_layer,
                thickness_mm=self.fit_result.__dict__['params'].valuesdict()[
                    'thickness_mm_' + _each_layer],
                density_gcm3=self.fit_result.__dict__['params'].valuesdict()[
                    'density_gcm3_' + _each_layer])
        # self.fitted_fjac = self.fit_result.__dict__['fjac']
        # print(self.fit_result.__dict__['fjac'][0])
        '''Create fitted simulation'''

        self.fitted_simulation = Simulation(energy_min=self.energy_min,
                                            energy_max=self.energy_max,
                                            energy_step=self.energy_step,
                                            database=self.database)
        for each_layer in self.layer_list:
            self.fitted_simulation.add_layer(
                layer=each_layer,
                thickness_mm=self.fitted_layer.info[each_layer]['thickness']
                ['value'],
                density_gcm3=self.fitted_layer.info[each_layer]['density']
                ['value'])
        return self.fit_result
class TestItems(unittest.TestCase):
    layer_1 = 'U'
    thickness_1 = 0.05
    layer_2 = 'Ag'
    thickness_2 = 0.05
    layers = fit_util.Layer()
    layers.add_layer(layer=layer_1, thickness_mm=thickness_1)
    layers.add_layer(layer=layer_2, thickness_mm=thickness_2)
    database = '_data_for_unittest'
    simulation = Simulation(database=database)
    simulation.add_Layer(layers)
    items = fit_util.Items(simulation.o_reso)

    def test_raises(self):
        name = 'AG'
        pytest.raises(ValueError, fit_util._shape_items, name=name)
        name = 'aG'
        pytest.raises(ValueError, fit_util._shape_items, name=name)
        name = 'AgO'
        pytest.raises(ValueError, fit_util._shape_items, name=name)
        name = 'ag'
        pytest.raises(ValueError, fit_util._shape_items, name=name)
        name = ''
        pytest.raises(ValueError, fit_util._shape_items, name=name)
        name = []
        pytest.raises(ValueError, fit_util._shape_items, name=name)

    def test_isotope_format(self):
        name = '238-U'
        expected_path = ['U', 'U', '238-U']
        assert fit_util._shape_items(name) == expected_path
        name = '238U'
        assert fit_util._shape_items(name) == expected_path
        name = 'U-238'
        assert fit_util._shape_items(name) == expected_path
        name = 'U238'
        assert fit_util._shape_items(name) == expected_path

    def test_fill_iso_to_items(self):
        name = 'U*'
        expected_path_list = [['U', 'U', '233-U'], ['U', 'U', '234-U'],
                              ['U', 'U', '235-U'], ['U', 'U', '238-U']]
        assert fit_util._fill_iso_to_items(
            name, database=self.database) == expected_path_list
        name = 'U'
        pytest.raises(ValueError,
                      fit_util._fill_iso_to_items,
                      name=name,
                      database=self.database)

    def test_shape_items(self):
        name = 'U'
        expected_path = ['U', 'U']
        assert fit_util._shape_items(name) == expected_path
        name = 'u'
        expected_path = ['U', 'U']
        assert fit_util._shape_items(name) == expected_path
        name = 'Gd'
        expected_path = ['Gd', 'Gd']
        assert fit_util._shape_items(name) == expected_path

    def test_items_shaped(self):
        _input = ['Gd', ['U'], 'U-238', 'U*']
        expected = [['Gd', 'Gd'], ['U', 'U'], ['U', 'U', '233-U'],
                    ['U', 'U', '234-U'], ['U', 'U', '235-U'],
                    ['U', 'U', '238-U']]
        obtained = self.items.shaped(_input)
        assert obtained == expected

    def test_items_original(self):
        _input = [['Gd', 'Gd'], ['U', 'U'], ['U', 'U', '233-U'],
                  ['U', 'U', '234-U'], ['U', 'U', '235-U'],
                  ['U', 'U', '238-U']]
        expected = [['Gd', 'Gd'], ['U', 'U'], ['U', 'U', '233-U'],
                    ['U', 'U', '234-U'], ['U', 'U', '235-U'],
                    ['U', 'U', '238-U']]
        obtained = self.items.shaped(_input)
        assert obtained == expected
Esempio n. 6
0
    def plot(self,
             error=True,
             table=True,
             grid=True,
             before=False,
             interp=False,
             total=True,
             all_elements=False,
             all_isotopes=False,
             items_to_plot=None,
             peak_mark=True,
             peak_id='indexed',
             y_type='transmission',
             x_type='energy',
             t_unit='us',
             logx=False,
             logy=False,
             save_fig=False):
        """

        :param error:
        :type error:
        :param table:
        :type table:
        :param grid:
        :type grid:
        :param before:
        :type before:
        :param interp:
        :type interp:
        :param total:
        :type total:
        :param all_elements:
        :type all_elements:
        :param all_isotopes:
        :type all_isotopes:
        :param items_to_plot:
        :type items_to_plot:
        :param peak_mark:
        :type peak_mark:
        :param peak_id:
        :type peak_id:
        :param y_type:
        :type y_type:
        :param x_type:
        :type x_type:
        :param t_unit:
        :type t_unit:
        :param logx:
        :type logx:
        :param logy:
        :type logy:
        :param save_fig:
        :type save_fig:
        :return:
        :rtype:
        """
        # Form signals from fitted_layer
        if self.fitted_simulation is None:
            self.fitted_simulation = Simulation(energy_min=self.energy_min,
                                                energy_max=self.energy_max,
                                                energy_step=self.energy_step)
            for each_layer in self.layer_list:
                self.fitted_simulation.add_layer(
                    layer=each_layer,
                    thickness_mm=self.fitted_layer.info[each_layer]
                    ['thickness']['value'],
                    density_gcm3=self.fitted_layer.info[each_layer]['density']
                    ['value'])
        if peak_id not in ['indexed', 'all']:
            raise ValueError("'peak=' must be one of ['indexed', 'full'].")
        simu_x = self.fitted_simulation.get_x(x_type='energy')
        simu_y = self.fitted_simulation.get_y(y_type='attenuation')

        # Get plot labels
        simu_label = 'Fit'
        simu_before_label = 'Fit_init'
        exp_label = 'Exp'
        exp_interp_label = 'Exp_interp'
        sample_name = ' & '.join(self.layer_list)
        if self.sample_vary is None:
            raise ValueError("Vary type ['density'|'thickness'] is not set.")
        fig_title = 'Fitting result of sample (' + sample_name + ')'

        # Create pd.DataFrame
        self.df = pd.DataFrame()

        # Clear any left plt
        plt.close()

        # plot table + graph
        if table is True:
            ax1 = plt.subplot2grid(shape=(10, 10),
                                   loc=(0, 1),
                                   rowspan=8,
                                   colspan=8)
        # plot graph only
        else:
            ax1 = plt.subplot(111)

        # Plot after fitting
        if total is True:
            ax1.plot(simu_x, simu_y, 'b-', label=simu_label, linewidth=1)

        # Save to df
        _live_df_x_label = simu_label + '_eV'
        _live_df_y_label = simu_label + '_attenuation'
        self.df[_live_df_x_label] = simu_x
        self.df[_live_df_y_label] = simu_y
        """Plot options"""

        # 1.
        if before is True:
            # Plot before fitting
            # Form signals from raw_layer
            simulation = Simulation(energy_min=self.energy_min,
                                    energy_max=self.energy_max,
                                    energy_step=self.energy_step)
            for each_layer in self.layer_list:
                simulation.add_layer(
                    layer=each_layer,
                    thickness_mm=self.raw_layer.info[each_layer]['thickness']
                    ['value'],
                    density_gcm3=self.raw_layer.info[each_layer]['density']
                    ['value'])
            simu_x = simulation.get_x(x_type='energy')
            simu_y_before = simulation.get_y(y_type='attenuation')
            ax1.plot(simu_x,
                     simu_y_before,
                     'c-.',
                     label=simu_before_label,
                     linewidth=1)
            # Save to df
            _live_df_x_label = simu_before_label + '_eV'
            _live_df_y_label = simu_before_label + '_attenuation'
            self.df[_live_df_x_label] = simu_x
            self.df[_live_df_y_label] = simu_y_before
        # 2.
        if interp is True:
            # Plot exp. data (interpolated)
            x_interp, y_interp = self.experiment.xy_scaled(
                energy_max=self.energy_max,
                energy_min=self.energy_min,
                energy_step=self.energy_step,
                x_type='energy',
                y_type='attenuation',
                baseline=self.baseline,
                offset_us=self.calibrated_offset_us,
                source_to_detector_m=self.calibrated_source_to_detector_m)
            ax1.plot(x_interp,
                     y_interp,
                     'r:',
                     label=exp_interp_label,
                     linewidth=1)
            # Save to df
            _live_df_x_label = exp_interp_label + '_eV'
            _live_df_y_label = exp_interp_label + '_attenuation'
            self.df[_live_df_x_label] = x_interp
            self.df[_live_df_y_label] = y_interp
        else:
            # Plot exp. data (raw)
            exp_x = self.experiment.get_x(
                x_type='energy',
                offset_us=self.calibrated_offset_us,
                source_to_detector_m=self.calibrated_source_to_detector_m)
            exp_y = self.experiment.get_y(y_type='attenuation',
                                          baseline=self.baseline)
            ax1.plot(exp_x,
                     exp_y,
                     linestyle='-',
                     linewidth=1,
                     marker='o',
                     markersize=2,
                     color='r',
                     label=exp_label)

            # Save to df
            _df = pd.DataFrame()
            _live_df_x_label = exp_label + '_eV'
            _live_df_y_label = exp_label + '_attenuation'
            _df[_live_df_x_label] = exp_x
            _df[_live_df_y_label] = exp_y
            # Concatenate since the length of raw and simu are not the same
            self.df = pd.concat([self.df, _df], axis=1)

        # 3.
        if error is True:
            # Plot fitting differences
            error_label = 'Diff.'
            _move_below_by = 0.2
            moved_fitted_residual = self.fitted_residual - _move_below_by
            ax1.plot(simu_x,
                     moved_fitted_residual,
                     'g-',
                     label=error_label,
                     linewidth=1,
                     alpha=1)
            # Save to df
            _live_df_x_label = error_label + '_eV'
            _live_df_y_label = error_label + '_attenuation'
            self.df[_live_df_x_label] = simu_x
            self.df[_live_df_y_label] = moved_fitted_residual
        # 4.
        if all_elements is True:
            # show signal from each elements
            _stack_signal = self.fitted_simulation.o_reso.stack_signal
            _stack = self.fitted_simulation.o_reso.stack
            y_axis_tag = 'attenuation'

            for _layer in _stack.keys():
                for _element in _stack[_layer]['elements']:
                    _y_axis = _stack_signal[_layer][_element][y_axis_tag]
                    ax1.plot(simu_x,
                             _y_axis,
                             label="{}".format(_element),
                             linewidth=1,
                             alpha=0.85)
                    # Save to df
                    _live_df_x_label = _element + '_eV'
                    _live_df_y_label = _element + '_attenuation'
                    self.df[_live_df_x_label] = simu_x
                    self.df[_live_df_y_label] = _y_axis
        # 4.
        if all_isotopes is True:
            # show signal from each isotopes
            _stack_signal = self.fitted_simulation.o_reso.stack_signal
            _stack = self.fitted_simulation.o_reso.stack
            y_axis_tag = 'attenuation'
            for _layer in _stack.keys():
                for _element in _stack[_layer]['elements']:
                    for _isotope in _stack[_layer][_element]['isotopes'][
                            'list']:
                        _y_axis = _stack_signal[_layer][_element][_isotope][
                            y_axis_tag]
                        ax1.plot(simu_x,
                                 _y_axis,
                                 label="{}".format(_isotope),
                                 linewidth=1,
                                 alpha=1)
                        # Save to df
                        _live_df_x_label = _isotope + '_eV'
                        _live_df_y_label = _isotope + '_attenuation'
                        self.df[_live_df_x_label] = simu_x
                        self.df[_live_df_y_label] = _y_axis
        # 5.
        if items_to_plot is not None:
            # plot specified from 'items_to_plot'
            y_axis_tag = 'attenuation'
            items = fit_util.Items(o_reso=self.fitted_simulation.o_reso,
                                   database=self.database)
            shaped_items = items.shaped(items_list=items_to_plot)
            _signal_dict = items.values(y_axis_type=y_axis_tag)
            for _each_label in list(_signal_dict.keys()):
                ax1.plot(simu_x,
                         _signal_dict[_each_label],
                         '--',
                         label=_each_label,
                         linewidth=1,
                         alpha=1)
                # Save to df
                _live_df_x_label = _each_label + '_eV'
                _live_df_y_label = _each_label + '_attenuation'
                self.df[_live_df_x_label] = simu_x
                self.df[_live_df_y_label] = _signal_dict[_each_label]

        # plot peaks detected and indexed
        if self.experiment.o_peak and self.experiment.o_peak.peak_map_indexed is not None:
            _peak_df_scaled = self.experiment.o_peak.peak_df_scaled
            _peak_map_indexed = self.experiment.o_peak.peak_map_indexed
            _peak_map_full = self.experiment.o_peak.peak_map_full
            if peak_mark is True:
                ax1.plot(_peak_df_scaled['x'],
                         _peak_df_scaled['y'],
                         'kx',
                         label='_nolegend_')
            if error is False:
                ax1.set_ylim(ymin=-0.1)
            for _ele_name in _peak_map_indexed.keys():
                if peak_id is 'all':
                    ax1.plot(_peak_map_full[_ele_name]['ideal']['x'], [-0.05] *
                             len(_peak_map_full[_ele_name]['ideal']['x']),
                             '|',
                             ms=10,
                             label=_ele_name)
                elif peak_id is 'indexed':
                    ax1.plot(_peak_map_indexed[_ele_name]['exp']['x'],
                             [-0.05] *
                             len(_peak_map_indexed[_ele_name]['exp']['x']),
                             '|',
                             ms=8,
                             label=_ele_name)
                if 'peak_span' in _peak_map_indexed[_ele_name].keys():
                    _data_point_x = _peak_map_indexed[_ele_name]['peak_span'][
                        'energy_ev']
                    _data_point_y = _peak_map_indexed[_ele_name]['peak_span'][
                        'y']
                    ax1.scatter(_data_point_x,
                                _data_point_y,
                                label='_nolegend_')

        # Set plot limit and captions
        fit_util.set_plt(ax=ax1,
                         fig_title=fig_title,
                         grid=grid,
                         x_type=x_type,
                         y_type=y_type,
                         t_unit=t_unit,
                         logx=logx,
                         logy=logy)

        # Plot table
        if table is True:
            if self.fitted_iso_result is None:
                columns = list(
                    self.fit_result.__dict__['params'].valuesdict().keys())
            else:
                columns = self.fit_result.__dict__['var_names']

            columns_to_show_dict = {}
            for _each in columns:
                _split = _each.split('_')
                if _split[0] == 'thickness':
                    _name_to_show = r'$d_{\rm{' + _split[-1] + '}}$' + ' (mm)'
                else:
                    _name_to_show = r'$\rho_{\rm{' + _split[
                        -1] + '}}$' + ' (g/cm$^3$)'
                columns_to_show_dict[_each] = _name_to_show
            columns_to_show = list(columns_to_show_dict.values())
            rows = ['Before', 'After']
            _row_before = []
            _row_after = []
            for _each in columns:
                _row_after.append(
                    round(
                        self.fit_result.__dict__['params'].valuesdict()[_each],
                        3))
                _row_before.append(
                    round(self.params_for_fit.valuesdict()[_each], 3))

            if self.fitted_iso_result is not None:
                _iso_columns = list(self.fitted_iso_result.__dict__['params'].
                                    valuesdict().keys())
                columns = columns + _iso_columns
                _iso_columns_to_show_dict = {}
                for _each_iso in _iso_columns:
                    _num_str = re.findall('\d+', _each_iso)[0]
                    _name_str = _each_iso[0]
                    _sup_name = r"$^{" + _num_str + "}$" + _name_str
                    _iso_columns_to_show_dict[_each_iso] = _sup_name
                _iso_columns_to_show = list(_iso_columns_to_show_dict.values())
                columns_to_show = columns_to_show + _iso_columns_to_show
                for _each in _iso_columns:
                    _row_after.append(
                        round(
                            self.fitted_iso_result.__dict__['params'].
                            valuesdict()[_each], 3))
                    _row_before.append(
                        round(self.params_for_iso_fit.valuesdict()[_each], 3))
            table = ax1.table(rowLabels=rows,
                              colLabels=columns_to_show,
                              cellText=[_row_before, _row_after],
                              loc='upper right',
                              bbox=[0, -0.33, 1.0, 0.18])
            table.auto_set_font_size(False)
            table.set_fontsize(10)
            plt.tight_layout()

        if save_fig:
            _sample_name = '_'.join(self.layer_list)
            _filename = 'fitting_' + _sample_name + '.png'
            plt.savefig(_filename, dpi=600, transparent=True)
            plt.close()
        else:
            plt.show()
Esempio n. 7
0
import matplotlib.pyplot as plt

from ResoFit.experiment import Experiment
from ResoFit.simulation import Simulation
from ResoFit._pulse_shape import NeutronPulse

overwrite_csv = False
source_to_detector_m = 16.45

simulation = Simulation(energy_min=78,
                        energy_max=82,
                        energy_step=0.01,
                        database='ENDF_VII')
simulation.add_layer(layer='Gd', thickness_mm=0.075)
simulation._convolve_beam_shapes(source_to_detector_m=source_to_detector_m,
                                 model_index=1,
                                 conv_proton=True,
                                 proton_params={})
# model_index:
# 1: 'ikeda_carpenter',
# 2: 'cole_windsor',
# 3: 'pseudo_voigt',
# 4: 'ikeda_carpenter_jparc',
# 5: 'cole_windsor_jparc'

# folder = 'data/IPTS_19558/reso_data_19558'
# data_file1 = 'Gd_thin.csv'
# spectra_file = 'Image002_Spectra.txt'
# experiment1 = Experiment(data_file=data_file1,
#                          spectra_file=spectra_file,
#                          folder=folder,
class TestSimulation(unittest.TestCase):
    energy_min = 7
    energy_max = 10
    energy_step = 1
    database = '_data_for_unittest'
    simulation = Simulation(energy_min=energy_min,
                            energy_max=energy_max,
                            energy_step=energy_step,
                            database='_data_for_unittest')

    def test_add_layer(self):
        simulation = self.simulation
        simulation.add_layer(layer='U', thickness_mm=0.15)
        _simu_x_returned = simulation.get_x(x_type='energy')
        _simu_y_returned = simulation.get_y(y_type='attenuation')
        _simu_x_expected = np.array([7., 8., 9., 10.])
        _simu_y_expected = np.array(
            [0.03699373, 0.00936537, 0.00854215, 0.00726004])
        self.assertAlmostEqual(_simu_x_returned[0],
                               _simu_x_expected[0],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_x_returned[1],
                               _simu_x_expected[1],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_x_returned[2],
                               _simu_x_expected[2],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_x_returned[3],
                               _simu_x_expected[3],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_y_returned[0],
                               _simu_y_expected[0],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_y_returned[1],
                               _simu_y_expected[1],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_y_returned[2],
                               _simu_y_expected[2],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_y_returned[3],
                               _simu_y_expected[3],
                               delta=0.000001)

    def test_set_isotopic_ratio(self):
        simulation = self.simulation
        simulation.add_layer(layer='U', thickness_mm=0.15)
        simulation.set_isotopic_ratio('U', 'U', [0., 0., 0.99, 0.01])
        _isotopic_ratio_list_wrong_len = [0., 0.99, 0.01]
        self.assertRaises(
            ValueError,
            simulation.set_isotopic_ratio,
            layer='U',
            element='U',
            new_isotopic_ratio_list=_isotopic_ratio_list_wrong_len)
        _simu_x_returned = simulation.get_x(x_type='energy')
        _simu_y_returned = simulation.get_y(y_type='attenuation')
        _simu_x_expected = np.array([7., 8., 9., 10.])
        _simu_y_expected = np.array(
            [0.06464851, 0.01259978, 0.11890677, 0.02255858])
        self.assertAlmostEqual(_simu_x_returned[0],
                               _simu_x_expected[0],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_x_returned[1],
                               _simu_x_expected[1],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_x_returned[2],
                               _simu_x_expected[2],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_x_returned[3],
                               _simu_x_expected[3],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_y_returned[0],
                               _simu_y_expected[0],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_y_returned[1],
                               _simu_y_expected[1],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_y_returned[2],
                               _simu_y_expected[2],
                               delta=0.000001)
        self.assertAlmostEqual(_simu_y_returned[3],
                               _simu_y_expected[3],
                               delta=0.000001)

    def test_x_angstrom(self):
        simulation = self.simulation
        simulation.add_layer(layer='U', thickness_mm=0.15)
        _x_returned = simulation.get_x(x_type='lambda')
        _x_expected = np.array(
            [0.10809189, 0.10111071, 0.09532809, 0.09043617])
        self.assertAlmostEqual(_x_returned[0], _x_expected[0], delta=0.000001)
        self.assertAlmostEqual(_x_returned[1], _x_expected[1], delta=0.000001)
        self.assertAlmostEqual(_x_returned[2], _x_expected[2], delta=0.000001)
        self.assertAlmostEqual(_x_returned[3], _x_expected[3], delta=0.000001)

    def test_y_transmission(self):
        simulation = self.simulation
        simulation.add_layer(layer='U', thickness_mm=0.15)
        _y_returned = simulation.get_y(y_type='transmission')
        _y_expected = np.array(
            [0.96300627, 0.99063463, 0.99145785, 0.99273996])
        self.assertAlmostEqual(_y_returned[0], _y_expected[0], delta=0.000001)
        self.assertAlmostEqual(_y_returned[1], _y_expected[1], delta=0.000001)
        self.assertAlmostEqual(_y_returned[2], _y_expected[2], delta=0.000001)
        self.assertAlmostEqual(_y_returned[3], _y_expected[3], delta=0.000001)

    def test_xy_simu(self):
        simulation = self.simulation
        simulation.add_layer(layer='U', thickness_mm=0.15)
        _x_returned = simulation.get_x(x_type='lambda')
        _y_returned = simulation.get_y(y_type='transmission')
        _x_expected = np.array(
            [0.10809189, 0.10111071, 0.09532809, 0.09043617])
        self.assertAlmostEqual(_x_returned[0], _x_expected[0], delta=0.000001)
        self.assertAlmostEqual(_x_returned[1], _x_expected[1], delta=0.000001)
        self.assertAlmostEqual(_x_returned[2], _x_expected[2], delta=0.000001)
        self.assertAlmostEqual(_x_returned[3], _x_expected[3], delta=0.000001)
        _y_expected = np.array(
            [0.96300627, 0.99063463, 0.99145785, 0.99273996])
        self.assertAlmostEqual(_y_returned[0], _y_expected[0], delta=0.000001)
        self.assertAlmostEqual(_y_returned[1], _y_expected[1], delta=0.000001)
        self.assertAlmostEqual(_y_returned[2], _y_expected[2], delta=0.000001)
        self.assertAlmostEqual(_y_returned[3], _y_expected[3], delta=0.000001)
        _x_returned = simulation.get_x(x_type='energy')
        _y_returned = simulation.get_y(y_type='attenuation')
        _x_expected = np.array([7., 8., 9., 10.])
        self.assertAlmostEqual(_x_returned[0], _x_expected[0], delta=0.000001)
        self.assertAlmostEqual(_x_returned[1], _x_expected[1], delta=0.000001)
        self.assertAlmostEqual(_x_returned[2], _x_expected[2], delta=0.000001)
        self.assertAlmostEqual(_x_returned[3], _x_expected[3], delta=0.000001)
        _y_expected = np.array(
            [0.03699373, 0.00936537, 0.00854215, 0.00726004])
        self.assertAlmostEqual(_y_returned[0], _y_expected[0], delta=0.000001)
        self.assertAlmostEqual(_y_returned[1], _y_expected[1], delta=0.000001)
        self.assertAlmostEqual(_y_returned[2], _y_expected[2], delta=0.000001)
        self.assertAlmostEqual(_y_returned[3], _y_expected[3], delta=0.000001)

    def test_peak_map(self):
        pass
import numpy as np
import pprint
import matplotlib.pyplot as plt
from ResoFit.experiment import Experiment
import peakutils as pku
from ResoFit.simulation import Simulation
from scipy import signal
import scipy
folder = 'data/IPTS_19558/reso_data_19558'
data_file1 = 'spheres.csv'
# data_file2 = 'spheres_background_1.csv'
spectra_file = 'Image002_Spectra.txt'
#
# source_to_detector_m = 16.45  # 16#16.445359069030175#16.447496101100739
# offset_us = 2.752  # 0#2.7120797253959119#2.7355447625559037
# baseline = False
# energy_xmax = 150
# lambda_xmax = None
# x_axis = 'number'
#
# # # Calibrate the peak positions
experiment1 = Experiment(data_file=data_file1,
                         spectra_file=spectra_file,
                         folder=folder,
                         baseline=True)
experiment1.slice(start=300, reset_index=False)
peak_df = experiment1.find_peak()
experiment1.plot(x_axis='number', t_unit='s')
plt.plot(peak_df['x_num'], peak_df['y'], 'kx')
energy_min = 7
import numpy as np
import pprint
import matplotlib.pyplot as plt
from ResoFit.experiment import Experiment
import peakutils as pku
from ResoFit.simulation import Simulation
from scipy import signal
import scipy
folder = 'data/IPTS_20439/reso_data_20439'
sample_name = [
    'Ta on MCP (6pC)',
    'Ta on 10mm Pb on MCP (6pC)',
    # 'Ta',
    # 'Ta 9C',
    # 'Ta Cd',
    'Ta + Cd on MCP (9pC)',
    'Ta inside ILL at 80C Cd on MCP (12pC)'
]
data_file = [
    'Ta_no_lead_2C_total_6C.csv',
    'Ta_10mm_lead_2C_total_6C.csv',
    # 'Ta.csv',
    # 'Ta_9C.csv',
    # 'Ta_Cd.csv',
    'Ta_Cd_9C.csv',
    'Ta_80C_12pC.csv'
]
norm_to_file = [
    'OB_no_lead_2C_total_6C.csv',
    'OB_10mm_lead_2C_total_6C.csv',