def test_repr(self):
     """assert object works"""
     _stack = {
         'CoAg': {
             'elements': ['Co', 'Ag'],
             'stoichiometric_ratio': [1, 2],
             'thickness': {
                 'value': 0.025,
                 'units': 'mm'
             },
         },
         'Ag': {
             'elements': ['Ag'],
             'stoichiometric_ratio': [1],
             'thickness': {
                 'value': 0.03,
                 'units': 'mm'
             },
         },
     }
     energy_min = 10
     energy_max = 150
     energy_step = 1
     o_reso = Resonance(stack=_stack,
                        energy_max=energy_max,
                        energy_min=energy_min,
                        energy_step=energy_step,
                        database=self.database)
     self.assertIsInstance(o_reso.__repr__(), str)
 def setUp(self):
     _stack = {
         'CoAg': {
             'elements': ['Co', 'Ag'],
             'stoichiometric_ratio': [1, 2],
             'thickness': {
                 'value': 0.025,
                 'units': 'mm'
             },
             'density': {
                 'value': np.NaN,
                 'units': 'g/cm3'
             },
         },
         'U': {
             'elements': ['U'],
             'stoichiometric_ratio': [1],
             'thickness': {
                 'value': 0.03,
                 'units': 'mm'
             },
             'density': {
                 'value': np.NaN,
                 'units': 'g/cm3'
             },
         },
     }
     self.o_reso = Resonance(stack=_stack, database=self.database)
Exemple #3
0
    def __init__(self, energy_min=1e-5, energy_max=1000, energy_step=0.01, database='ENDF_VIII'):
        """
        initialize the a Simulation() using the Resonance() in ImagingReso

        :param energy_min:
        :type energy_min:
        :param energy_max:
        :type energy_max:
        :param energy_step:
        :type energy_step:
        :param database:
        :type database:
        """
        self.energy_min = energy_min
        self.energy_max = energy_max
        self.energy_step = energy_step
        self.database = database

        self.o_reso = Resonance(energy_min=energy_min,
                                energy_max=energy_max,
                                energy_step=energy_step,
                                database=database)
        self.neutron_pulse = None

        self.layer_list = []
        self.layer = fit_util.Layer()

        self.x_tof_us = None
        self.y_att = None
Exemple #4
0
 def test_abundance(self):
     """assert"""
     o_reso = Resonance(database=self.database)
     o_reso.add_layer(formula='C', thickness=1)
     self.assertAlmostEqual(
         o_reso.stack['C']['C']['isotopes']['isotopic_ratio'][0],
         0.9893,
         delta=0.0001)
     self.assertAlmostEqual(
         o_reso.stack['C']['C']['isotopes']['isotopic_ratio'][1],
         0.0107,
         delta=0.0001)
    def setUp(self):
        _energy_min = 1
        _energy_max = 50
        _energy_step = 0.1
        _layer_1 = 'Co'
        _thickness_1 = 0.025  # mm

        o_reso = Resonance(energy_min=_energy_min,
                           energy_max=_energy_max,
                           energy_step=_energy_step,
                           database=self.database)
        o_reso.add_layer(formula=_layer_1, thickness=_thickness_1)
        self.o_reso = o_reso
def init_reso_from_tb(range_tb_df, e_step, database):
    v_1 = range_tb_df[energy_name][0]
    v_2 = range_tb_df[energy_name][1]
    if v_1 < v_2:
        o_reso = Resonance(energy_min=v_1,
                           energy_max=v_2,
                           energy_step=e_step,
                           database=database)
    else:
        o_reso = Resonance(energy_min=v_2,
                           energy_max=v_1,
                           energy_step=e_step,
                           database=database)
    return o_reso
Exemple #7
0
def _fill_iso_to_items(name, stack=None, database='ENDF_VII'):
    if '*' not in name:
        raise ValueError(
            "'*' is needed to retrieve all isotopes of '{}' ".format(name))
    else:
        ele_name = name.replace('*', '')
        if stack is None:
            o_reso = Resonance(database=database)
            o_reso.add_layer(formula=ele_name, thickness=1)
            stack = o_reso.stack
        iso_list = stack[ele_name][ele_name]['isotopes']['list']
        _path_to_iso = []
        for _each_iso in iso_list:
            _path_to_iso.append(_shape_items(_each_iso))
    return _path_to_iso
    def test_duplicated_layer_name(self):
        o_reso = Resonance(database=self.database)

        # layer 1
        layer1 = 'CoAg'
        thickness1 = 0.025
        o_reso.add_layer(formula=layer1, thickness=thickness1)

        # Duplicated layer name
        layer2 = 'CoAg'
        thickness2 = 0.01
        self.assertRaises(ValueError,
                          o_reso.add_layer,
                          formula=layer2,
                          thickness=thickness2)
 def test_initialization_of_object(self):
     """assert object is correctly initialized with energy min, max and step"""
     _stack = {
         'CoAg': {
             'elements': ['Co', 'Ag'],
             'stoichiometric_ratio': [1, 2],
             'thickness': {
                 'value': 0.025,
                 'units': 'mm'
             },
         },
         'Ag': {
             'elements': ['Ag'],
             'stoichiometric_ratio': [1],
             'thickness': {
                 'value': 0.03,
                 'units': 'mm'
             },
         },
     }
     energy_min = 10
     energy_max = 150
     energy_step = 1
     o_reso = Resonance(stack=_stack,
                        energy_max=energy_max,
                        energy_min=energy_min,
                        energy_step=energy_step,
                        database=self.database)
     self.assertEqual(o_reso.energy_max, energy_max)
     self.assertEqual(o_reso.energy_min, energy_min)
     self.assertEqual(o_reso.energy_step, energy_step)
    def test_element_metadata_via_stack_initialization(self):
        """assert __element_metadata is correctly populated using stack initialization"""

        _stack = {
            'CoAg': {
                'elements': ['Co', 'Ag'],
                'stoichiometric_ratio': [1, 2],
                'thickness': {
                    'value': 0.025,
                    'units': 'mm'
                },
            },
            'Ag': {
                'elements': ['Ag'],
                'stoichiometric_ratio': [1],
                'thickness': {
                    'value': 0.03,
                    'units': 'mm'
                },
            },
        }
        o_reso = Resonance(stack=_stack, database=self.database)

        # molar mass
        stack = o_reso.stack
        co_mass_expected = 58.9332
        ag_mass_expected = 107.8682
        self.assertEqual(co_mass_expected,
                         stack['CoAg']['Co']['molar_mass']['value'])
        self.assertEqual(ag_mass_expected,
                         stack['Ag']['Ag']['molar_mass']['value'])
Exemple #11
0
def form_iso_table(sample_df: pd.DataFrame, database: str):
    o_reso = Resonance(energy_min=1,
                       energy_max=2,
                       energy_step=1,
                       database=database)
    o_reso = unpack_sample_tb_df_and_add_layer(o_reso=o_reso,
                                               sample_tb_df=sample_df)
    layer_list = list(o_reso.stack.keys())
    lay_list = []
    ele_list = []
    iso_list = []
    iso_ratio_list = []
    for each_layer in layer_list:
        current_ele_list = o_reso.stack[each_layer]['elements']
        for each_ele in current_ele_list:
            current_iso_list = o_reso.stack[each_layer][each_ele]['isotopes'][
                'list']
            current_iso_ratio_list = o_reso.stack[each_layer][each_ele][
                'isotopes']['isotopic_ratio']
            for i, each_iso in enumerate(current_iso_list):
                lay_list.append(each_layer)
                ele_list.append(each_ele)
                iso_list.append(each_iso)
                iso_ratio_list.append(round(current_iso_ratio_list[i], 4))

    _df = pd.DataFrame({
        layer_name: lay_list,
        ele_name: ele_list,
        iso_name: iso_list,
        iso_ratio_name: iso_ratio_list,
    })
    return _df
 def test_get_atoms_per_cm3_of_layer(self):
     """assert get_atoms_per_cm3_of_layer works"""
     _stack = {
         'CoAg': {
             'elements': ['Co', 'Ag'],
             'stoichiometric_ratio': [1, 1],
             'thickness': {
                 'value': 0.025,
                 'units': 'mm'
             },
             'density': {
                 'value': 9.8,
                 'units': 'g/cm3'
             },
         },
         'Ag': {
             'elements': ['Ag'],
             'stoichiometric_ratio': [1],
             'thickness': {
                 'value': 0.03,
                 'units': 'mm'
             },
         },
     }
     o_reso = Resonance(stack=_stack, database=self.database)
     _stack_returned = o_reso.stack
     _molar_mass, _atoms_per_cm3 = get_atoms_per_cm3_of_layer(
         compound_dict=_stack['CoAg'])
     self.assertAlmostEqual(_atoms_per_cm3, 3.5381585765227397e+22, delta=1)
Exemple #13
0
 def test_raises(self):
     _energy_min = 0.002
     _energy_max = 1
     _energy_step = 0.01
     o_reso = Resonance(energy_min=_energy_min,
                        energy_max=_energy_max,
                        energy_step=_energy_step,
                        database=self.database)
     self.assertRaises(Exception)
    def test_element_metadata_via_add_layer_initialization(self):
        """assert __element_metadata is correctly populated using add layer initialization"""
        o_reso = Resonance(database=self.database)

        # layer 1
        layer1 = 'CoAg'
        thickness1 = 0.025
        o_reso.add_layer(formula=layer1, thickness=thickness1)

        # layer 2
        layer2 = 'Ag'
        thickness2 = 0.1
        o_reso.add_layer(formula=layer2, thickness=thickness2)

        stack = o_reso.stack

        # molar mass
        co_mass_expected = 58.9332
        ag_mass_expected = 107.8682
        self.assertEqual(co_mass_expected,
                         stack['CoAg']['Co']['molar_mass']['value'])
        self.assertEqual(ag_mass_expected,
                         stack['CoAg']['Ag']['molar_mass']['value'])
        self.assertEqual(ag_mass_expected,
                         stack['Ag']['Ag']['molar_mass']['value'])

        # density
        co_density_expected = 8.9
        ag_density_expected = 10.5
        self.assertEqual(co_density_expected,
                         stack['CoAg']['Co']['density']['value'])
        self.assertEqual(ag_density_expected,
                         stack['Ag']['Ag']['density']['value'])
Exemple #15
0
 def test_H_sigma(self):
     _energy_min = 0.004
     _energy_max = 1
     _energy_step = 0.01
     o_reso = Resonance(energy_min=_energy_min,
                        energy_max=_energy_max,
                        energy_step=_energy_step,
                        database=self.database)
     _layer_1 = 'H2'
     _thickness_1 = 0.025  # mm
     _layer_2 = 'H'
     _thickness_2 = 0.01
     _layer_3 = 'CH4'
     _thickness_3 = 0.01
     _layer_4 = 'ZrH'
     _thickness_4 = 0.01
     o_reso.add_layer(formula=_layer_1, thickness=_thickness_1)
     o_reso.add_layer(formula=_layer_2, thickness=_thickness_2)
     o_reso.add_layer(formula=_layer_3, thickness=_thickness_3)
     o_reso.add_layer(formula=_layer_4, thickness=_thickness_4)
     layer1_sigma = o_reso.stack_sigma[_layer_1]['H']['1-H']['sigma_b_raw'][
         0]
     layer2_sigma = o_reso.stack_sigma[_layer_2]['H']['1-H']['sigma_b_raw'][
         0]
     layer3_sigma = o_reso.stack_sigma[_layer_3]['H']['1-H']['sigma_b_raw'][
         0]
     layer4_sigma = o_reso.stack_sigma[_layer_4]['H']['1-H']['sigma_b_raw'][
         0]
     self.assertNotEqual(layer1_sigma, layer2_sigma)
     self.assertNotEqual(layer1_sigma, layer3_sigma)
     self.assertNotEqual(layer1_sigma, layer4_sigma)
     self.assertNotEqual(layer2_sigma, layer3_sigma)
     self.assertNotEqual(layer2_sigma, layer4_sigma)
     self.assertNotEqual(layer3_sigma, layer4_sigma)
    def validate_table_input_clicked(self):
        # block element table signal
        self.ui.element_table.blockSignals(True)

        # disable top part (no more changes allowed)
        self.ui.layer_groupBox.setEnabled(False)

        # collect table input
        nbr_row = self.ui.layer_table.rowCount()
        _table_dictionary = {}
        for _row_index in range(nbr_row):
            _dict = {}
            _layer_name = self.get_table_item(_row_index, 0)
            if _layer_name == '':
                break
            _dict['elements'] = self.format_string_to_array(
                string=self.get_table_item(_row_index, 1), data_type='str')
            _dict['stoichiometric_ratio'] = self.format_string_to_array(
                string=self.get_table_item(_row_index, 2), data_type='float')
            _dict['thickness'] = {
                'value': float(self.get_table_item(_row_index, 3)),
                'units': 'mm'
            }
            if self.get_table_item(_row_index, 4):
                _dict['density'] = {
                    'value': float(self.get_table_item(_row_index, 4)),
                    'units': 'g/cm3'
                }
            _table_dictionary[_layer_name] = _dict
        self.stack = _table_dictionary

        E_min = float(str(self.ui.Emin_lineEdit.text()))
        E_max = float(str(self.ui.Emax_lineEdit.text()))
        delta_E = float(str(self.ui.deltaE_lineEdit.text()))

        o_reso = Resonance(stack=self.stack,
                           energy_min=E_min,
                           energy_max=E_max,
                           energy_step=delta_E)
        self.o_reso = o_reso

        self.fill_check_groupBox()
        self.ui.check_groupBox.setVisible(True)
        self.ui.element_table.blockSignals(False)
        self.ui.ok_button.setEnabled(True)
    def test_get_sigma_isotopes(self):
        """assert get_sigma works"""
        _stack = {
            'CoAg': {
                'elements': ['Co', 'Ag'],
                'stoichiometric_ratio': [1, 2],
                'thickness': {
                    'value': 0.025,
                    'units': 'mm'
                },
            },
            'Ag': {
                'elements': ['Ag'],
                'stoichiometric_ratio': [1],
                'thickness': {
                    'value': 0.03,
                    'units': 'mm'
                },
            },
        }
        energy_min = 10
        energy_max = 150
        energy_step = 1
        o_reso = Resonance(stack=_stack,
                           energy_max=energy_max,
                           energy_min=energy_min,
                           energy_step=energy_step,
                           database=self.database)
        stack_sigma = o_reso.stack_sigma

        # for isotopes
        self.assertEqual(len(stack_sigma), 2)
        self.assertEqual(stack_sigma['Ag']['Ag']['107-Ag']['energy_eV'][0], 10)
        self.assertEqual(stack_sigma['Ag']['Ag']['107-Ag']['energy_eV'][-1],
                         150)

        # for elements
        self.assertEqual(stack_sigma['Ag']['Ag']['energy_eV'][0], 10)
 def test_correct_initialization_of_stack(self):
     """assert correct defined stack is correctly saved"""
     _stack = {
         'CoAg': {
             'elements': ['Co', 'Ag'],
             'stoichiometric_ratio': [1, 2],
             'thickness': {
                 'value': 0.025,
                 'units': 'mm'
             },
         },
         'Ag': {
             'elements': ['Ag'],
             'stoichiometric_ratio': [1],
             'thickness': {
                 'value': 0.03,
                 'units': 'mm'
             },
         },
     }
     o_reso = Resonance(stack=_stack, database=self.database)
     _stack_returned = o_reso.stack
     self.assertEqual(_stack, _stack_returned)
    def test_layer_density_locked_if_defined_during_initialization_init(self):
        """assert the layer density is locked if defined at the beginning using init"""
        _stack = {
            'CoAg': {
                'elements': ['Co', 'Ag'],
                'stoichiometric_ratio': [1, 2],
                'thickness': {
                    'value': 0.025,
                    'units': 'mm'
                },
                'density': {
                    'value': 8.9,
                    'units': 'g/cm3'
                },
            },
            'Ag': {
                'elements': ['Ag'],
                'stoichiometric_ratio': [1],
                'thickness': {
                    'value': 0.03,
                    'units': 'mm'
                },
                'density': {
                    'value': np.NaN,
                    'units': 'g/cm3'
                },
            },
        }
        o_reso = Resonance(stack=_stack, database=self.database)

        density_lock_before = 8.9
        density_lock_after = o_reso.stack['CoAg']['density']['value']
        self.assertEqual(density_lock_after, density_lock_before)

        density_unlock_expected = 10.5
        density_unlock_after = o_reso.stack['Ag']['density']['value']
        self.assertEqual(density_unlock_after, density_unlock_expected)
    def test_layer_density_locked_if_defined_during_initialization_add_layer(
            self):
        """assert the layer density is locked if defined at the beginning using add_layer"""
        o_reso = Resonance(database=self.database)

        # layer 1
        layer1 = 'CoAg'
        thickness1 = 0.025
        density = 8.9
        o_reso.add_layer(formula=layer1, thickness=thickness1, density=density)

        # layer 2
        layer2 = 'Ag'
        thickness2 = 0.1
        o_reso.add_layer(formula=layer2, thickness=thickness2)

        density_lock_before = 8.9
        density_lock_after = o_reso.stack['CoAg']['density']['value']
        self.assertEqual(density_lock_after, density_lock_before)

        density_unlock_expected = 10.5
        density_unlock_after = o_reso.stack['Ag']['density']['value']
        self.assertEqual(density_unlock_after, density_unlock_expected)
def init_reso(e_min, e_max, e_step):
    o_reso = Resonance(energy_min=e_min, energy_max=e_max, energy_step=e_step)
    return o_reso
class TestGetterSetter(unittest.TestCase):
    database = '_data_for_unittest'

    def setUp(self):
        _stack = {
            'CoAg': {
                'elements': ['Co', 'Ag'],
                'stoichiometric_ratio': [1, 2],
                'thickness': {
                    'value': 0.025,
                    'units': 'mm'
                },
                'density': {
                    'value': np.NaN,
                    'units': 'g/cm3'
                },
            },
            'U': {
                'elements': ['U'],
                'stoichiometric_ratio': [1],
                'thickness': {
                    'value': 0.03,
                    'units': 'mm'
                },
                'density': {
                    'value': np.NaN,
                    'units': 'g/cm3'
                },
            },
        }
        self.o_reso = Resonance(stack=_stack, database=self.database)

        # stoichiometric ratio

    def test_retrieve_stoichiometric_of_uo3_sample(self):
        """assert retrieve stoichiometric work for complex sample such as UO3"""
        o_reso = self.o_reso
        o_reso.add_layer(formula='UO3', thickness=0.25, density=0.5)
        o_reso.add_layer(formula='AgCo', thickness=0.5, density=0.8)
        _stoichiometric_ratio = o_reso.get_isotopic_ratio(compound='UO3',
                                                          element='U')
        self.assertEqual(_stoichiometric_ratio['238-U'], 0.992745)

    def test_retrieve_stoichiometric_ratio_raises_error_if_unknown_compound(
            self):
        """assert ValueError raised if wrong compound when getting stoichiometric ratio"""
        self.assertRaises(ValueError,
                          self.o_reso.get_isotopic_ratio,
                          compound='unknown')

    def test_if_element_misssing_use_compound_and_raises_error_if_wrong(self):
        """assert ValueError raised if element does not exist"""
        self.assertRaises(ValueError,
                          self.o_reso.get_isotopic_ratio,
                          compound='CoAg')

    def test_stoichiometric_ratio_returned(self):
        """assert the stoichiometric ratio are correctly returned"""
        _stoichiometric_ratio = self.o_reso.get_isotopic_ratio(compound='U')
        _expected_dict = {
            '233-U': 0.0,
            '234-U': 5.5e-5,
            '235-U': 0.0072,
            '238-U': 0.992745
        }
        self.assertEqual(_expected_dict['233-U'],
                         _stoichiometric_ratio['233-U'])
        self.assertEqual(_expected_dict['235-U'],
                         _stoichiometric_ratio['235-U'])
        self.assertEqual(_expected_dict['238-U'],
                         _stoichiometric_ratio['238-U'])

    def test_list_all_stoichiometric_ratio(self):
        """assert the entire stoichiometric list is returned"""
        _stoichiometric_ratio = self.o_reso.get_isotopic_ratio()
        _expected_dict = {
            'U': {
                'U': {
                    '233-U': 0.0,
                    '234-U': 5.5e-5,
                    '235-U': 0.0072,
                    '238-U': 0.992745
                },
            },
            'CoAg': {
                'Ag': {
                    '107-Ag': 0.51839,
                    '109-Ag': 0.4816
                },
                'Co': {
                    '58-Co': 0.0,
                    '59-Co': 1.0
                },
            },
        }
        self.assertEqual(_expected_dict['U']['U']['233-U'],
                         _stoichiometric_ratio['U']['U']['233-U'])
        self.assertEqual(_expected_dict['U']['U']['238-U'],
                         _stoichiometric_ratio['U']['U']['238-U'])
        self.assertEqual(_expected_dict['CoAg']['Ag']['107-Ag'],
                         _stoichiometric_ratio['CoAg']['Ag']['107-Ag'])

    def test_set_stoichiometric_ratio_raises_value_error_if_wrong_compound(
            self):
        """assert ValueError raised if wrong compound in stoichiometric ratio setter"""
        self.assertRaises(ValueError, self.o_reso.set_isotopic_ratio)
        self.assertRaises(ValueError,
                          self.o_reso.set_isotopic_ratio,
                          compound='unknown')

    def test_set_stoichiometric_ratio_raises_value_error_if_wrong_element(
            self):
        """assert ValueError raised if wrong element in stoichiometric ratio setter"""
        self.assertRaises(ValueError,
                          self.o_reso.set_isotopic_ratio,
                          compound='CoAg')
        self.assertRaises(ValueError,
                          self.o_reso.set_isotopic_ratio,
                          compound='CoAg',
                          element='unknown')

    def test_set_stoichiometric_ratio_has_correct_new_list_size(self):
        """assert ValueRror raised if new list of ratio does not match old list size"""
        self.assertRaises(ValueError,
                          self.o_reso.set_isotopic_ratio,
                          compound='U',
                          element='U',
                          list_ratio=[0, 1, 2])

    def test_set_stoichiometric_ratio_correctly_calculates_new_molar_mass(
            self):
        """assert molar mass is correctly calculated for new set of stoichiometric coefficient"""
        self.o_reso.set_isotopic_ratio(compound='CoAg',
                                       element='Co',
                                       list_ratio=[0.5, 0.5])
        new_molar_mass = self.o_reso.stack['CoAg']['Co']['molar_mass']['value']
        list_isotopes_mass = self.o_reso.stack['CoAg']['Co']['isotopes'][
            'mass']['value']
        list_isotopes_ratio = self.o_reso.stack['CoAg']['Co']['isotopes'][
            'isotopic_ratio']
        mass_ratio = zip(list_isotopes_mass, list_isotopes_ratio)
        expected_molar_mass = np.array([_m * _r
                                        for _m, _r in mass_ratio]).sum()
        self.assertAlmostEqual(new_molar_mass,
                               expected_molar_mass,
                               delta=0.0001)

    def test_set_stoichiometric_ratio_correctly_calculates_new_density(self):
        """assert density is correctly calculated for new set of stoichiometric coefficient"""
        self.o_reso.set_isotopic_ratio(compound='CoAg',
                                       element='Co',
                                       list_ratio=[0.5, 0.5])
        new_density = self.o_reso.stack['CoAg']['Co']['density']['value']
        list_density = self.o_reso.stack['CoAg']['Co']['isotopes']['density'][
            'value']
        list_isotopes_ratio = self.o_reso.stack['CoAg']['Co']['isotopes'][
            'isotopic_ratio']
        density_ratio = zip(list_density, list_isotopes_ratio)
        expected_density = np.array([_d * _r
                                     for _d, _r in density_ratio]).sum()
        self.assertAlmostEqual(new_density, expected_density, delta=0.0001)

        # density

    def test_retrieve_density_raises_error_if_unknown_compound(self):
        """assert ValueError raised if wrong compound when getting density"""
        self.assertRaises(ValueError,
                          self.o_reso.get_density,
                          compound='unknown')

    def test_if_element_misssing_use_compound_and_raises_error_if_wrong_when_getting_density(
            self):
        """assert ValueError raised if element does not exist when getting density"""
        self.assertRaises(ValueError, self.o_reso.get_density, compound='CoAg')

    def test_density_returned(self):
        """assert the density are correctly returned"""
        _density = self.o_reso.get_density(compound='U')
        _expected_density = 18.95
        self.assertEqual(_density, _expected_density)

    def test_density_returned_all(self):
        """assert the density of all element works"""
        _density_list = self.o_reso.get_density()
        _expected_density = {
            'CoAg': {
                'Co': 8.9,
                'Ag': 10.5,
            },
            'U': {
                'U': 18.95,
            },
        }
        self.assertEqual(_expected_density, _density_list)
    def test_adding_layer(self):
        """assert adding_layer works"""
        o_reso = Resonance(database=self.database)

        # layer 1
        layer1 = 'CoAg'
        thickness1 = 0.025
        o_reso.add_layer(formula=layer1, thickness=thickness1)

        # layer 2
        layer2 = 'Ag'
        thickness2 = 0.1
        density2 = 0.5
        o_reso.add_layer(formula=layer2,
                         thickness=thickness2,
                         density=density2)

        returned_stack = o_reso.stack
        expected_stack = {
            'CoAg': {
                'elements': ['Co', 'Ag'],
                'stoichiometric_ratio': [1, 1],
                'thickness': {
                    'value': 0.025,
                    'units': 'mm'
                },
                'density': {
                    'value': 9.7,
                    'units': 'g/cm3'
                },
                'Co': {
                    'isotopes': {
                        'file_names': ['Co-58.csv', 'Co-59.csv'],
                        'list': ['58-Co', '59-Co'],
                        'mass': {
                            'value': [57.9357576, 58.9332002],
                            'units': 'g/mol',
                        },
                        'isotopic_ratio': [0.0, 1.0],
                    },
                    'density': {
                        'value': 8.9,
                        'units': 'g/cm3'
                    },
                    'molar_mass': {
                        'value': 58.9332,
                        'units': 'g/mol'
                    },
                },
                'Ag': {
                    'isotopes': {
                        'file_names': ['Ag-107.csv', 'Ag-109.csv'],
                        'list': ['107-Ag', '109-Ag'],
                        'mass': {
                            'value': [106.905093, 108.904756],
                            'units': 'g/mol',
                        },
                        'isotopic_ratio': [0.51839, 0.48161000],
                    },
                    'density': {
                        'value': 10.5,
                        'units': 'g/cm3'
                    },
                    'molar_mass': {
                        'value': 107.8682,
                        'units': 'g/cm3'
                    },
                },
            },
            'Ag': {
                'elements': ['Ag'],
                'stoichiometric_ratio': [1],
                'thickness': {
                    'value': 0.1,
                    'units': 'mm'
                },
                'density': {
                    'value': 0.5,
                    'units': 'g/cm3'
                },
                'Ag': {
                    'isotopes': {
                        'file_names': ['Ag-107.csv', 'Ag-109.csv'],
                        'list': ['107-Ag', '109-Ag'],
                        'mass': {
                            'value': [106.905093, 108.904756],
                            'units': 'g/mol',
                        },
                        'isotopic_ratio': [0.51839, 0.48161000],
                    },
                    'density': {
                        'value': 10.5,
                        'units': 'g/cm3'
                    },
                    'molar_mass': {
                        'value': 107.8682,
                        'units': 'g/mol'
                    },
                },
            },
        }

        # CoAg
        # elements
        self.assertEqual(returned_stack['CoAg']['elements'],
                         expected_stack['CoAg']['elements'])
        # atomic_atomic_ratio
        self.assertEqual(returned_stack['CoAg']['stoichiometric_ratio'],
                         expected_stack['CoAg']['stoichiometric_ratio'])
        # thickness
        self.assertEqual(returned_stack['CoAg']['thickness']['value'],
                         expected_stack['CoAg']['thickness']['value'])
        self.assertEqual(returned_stack['CoAg']['thickness']['units'],
                         expected_stack['CoAg']['thickness']['units'])
        # isotopes Co
        # file names
        self.assertEqual(
            returned_stack['CoAg']['Co']['isotopes']['file_names'],
            expected_stack['CoAg']['Co']['isotopes']['file_names'])
        # list
        self.assertEqual(returned_stack['CoAg']['Co']['isotopes']['list'],
                         expected_stack['CoAg']['Co']['isotopes']['list'])
        # mass
        self.assertEqual(
            returned_stack['CoAg']['Co']['isotopes']['mass']['value'],
            expected_stack['CoAg']['Co']['isotopes']['mass']['value'])
        self.assertEqual(
            returned_stack['CoAg']['Co']['isotopes']['mass']['units'],
            expected_stack['CoAg']['Co']['isotopes']['mass']['units'])
        # atomic_ratio Co
        self.assertAlmostEqual(
            returned_stack['CoAg']['Co']['isotopes']['isotopic_ratio'][0],
            expected_stack['CoAg']['Co']['isotopes']['isotopic_ratio'][0],
            delta=0.0001)
        self.assertAlmostEqual(
            returned_stack['CoAg']['Co']['isotopes']['isotopic_ratio'][1],
            expected_stack['CoAg']['Co']['isotopes']['isotopic_ratio'][1],
            delta=0.0001)
        # isotopic_ratio Ag
        self.assertAlmostEqual(
            returned_stack['CoAg']['Ag']['isotopes']['isotopic_ratio'][0],
            expected_stack['CoAg']['Ag']['isotopes']['isotopic_ratio'][0],
            delta=0.0001)
        self.assertAlmostEqual(
            returned_stack['CoAg']['Ag']['isotopes']['isotopic_ratio'][1],
            expected_stack['CoAg']['Ag']['isotopes']['isotopic_ratio'][1],
            delta=0.0001)

        # layer density
        self.assertEqual(returned_stack['Ag']['density']['value'],
                         expected_stack['Ag']['density']['value'])
        self.assertAlmostEqual(returned_stack['CoAg']['density']['value'],
                               expected_stack['CoAg']['density']['value'],
                               delta=0.1)

        # element density
        self.assertEqual(returned_stack['CoAg']['Ag']['density']['value'],
                         expected_stack['CoAg']['Ag']['density']['value'])
        self.assertEqual(returned_stack['CoAg']['Co']['density']['value'],
                         expected_stack['CoAg']['Co']['density']['value'])
        # molar mass
        self.assertEqual(returned_stack['CoAg']['Ag']['molar_mass']['value'],
                         expected_stack['CoAg']['Ag']['molar_mass']['value'])
        self.assertEqual(returned_stack['CoAg']['Co']['molar_mass']['value'],
                         expected_stack['CoAg']['Co']['molar_mass']['value'])
Exemple #24
0
def calculate_transmission(sample_tb_df, iso_tb_df, iso_changed, beamline,
                           band_min, band_max, band_type, database):
    _main_path = os.path.abspath(os.path.dirname(__file__))
    _path_to_beam_shape = {
        'imaging': 'static/instrument_file/beam_flux_cg1d.txt',
        'imaging_crop': 'static/instrument_file/beam_flux_cg1d_crop.txt',
        'snap': 'static/instrument_file/beam_flux_snap.txt',
        # 'venus': 'static/instrument_file/beam_flux_venus.txt',
    }
    df_flux_raw = _load_beam_shape(_path_to_beam_shape[beamline])
    if beamline in ['imaging', 'imaging_crop']:
        e_min = df_flux_raw['energy_eV'].min()
        e_max = df_flux_raw['energy_eV'].max()
    else:
        if band_type == 'lambda':
            e_min = round(ir_util.angstroms_to_ev(band_max), 6)
            e_max = round(ir_util.angstroms_to_ev(band_min), 6)
        else:  # band_type == 'energy'
            e_min = band_min
            e_max = band_max

    e_step = (e_max - e_min) / (100 - 1)
    __o_reso = Resonance(energy_min=e_min,
                         energy_max=e_max,
                         energy_step=e_step,
                         database=database)
    _o_reso = unpack_sample_tb_df_and_add_layer(o_reso=__o_reso,
                                                sample_tb_df=sample_tb_df)
    o_reso = unpack_iso_tb_df_and_update(o_reso=_o_reso,
                                         iso_tb_df=iso_tb_df,
                                         iso_changed=iso_changed)
    o_stack = o_reso.stack

    # interpolate with the beam shape energy
    energy = o_reso.total_signal['energy_eV'].round(
        6)  # !!!need to fix ImagingReso energy_eV columns
    interp_type = 'cubic'
    interp_flux_function = interp1d(x=df_flux_raw['energy_eV'],
                                    y=df_flux_raw['flux'],
                                    kind=interp_type)
    flux_interp = interp_flux_function(energy)
    df_flux_interp = pd.DataFrame()
    df_flux_interp['energy_eV'] = energy
    df_flux_interp['flux'] = flux_interp
    df_flux = df_flux_interp[:]
    trans_tag = 'transmission'
    mu_tag = 'mu_per_cm'
    o_signal = o_reso.stack_signal

    _total_trans = _calculate_transmission(
        flux_df=df_flux, trans_array=o_reso.total_signal[trans_tag])

    for each_layer in o_stack.keys():
        _current_layer_thickness = o_stack[each_layer]['thickness']['value']
        if len(o_stack.keys()) == 1:
            _current_layer_trans = _total_trans
        else:
            _current_layer_trans = _calculate_transmission(
                flux_df=df_flux, trans_array=o_signal[each_layer][trans_tag])
        o_stack[each_layer][trans_tag] = _current_layer_trans
        o_stack[each_layer][mu_tag] = _transmission_to_mu_per_cm(
            transmission=_current_layer_trans,
            thickness=_current_layer_thickness)
        for each_ele in o_stack[each_layer]['elements']:
            _current_ele_trans = _calculate_transmission(
                flux_df=df_flux,
                trans_array=o_signal[each_layer][each_ele][trans_tag])
            o_stack[each_layer][each_ele][trans_tag] = _current_ele_trans
            o_stack[each_layer][each_ele][mu_tag] = _transmission_to_mu_per_cm(
                transmission=_current_ele_trans,
                thickness=_current_layer_thickness)
    return _total_trans, o_stack
Exemple #25
0
class Simulation(object):
    # Input sample name or names as str, case sensitive

    def __init__(self, energy_min=1e-5, energy_max=1000, energy_step=0.01, database='ENDF_VIII'):
        """
        initialize the a Simulation() using the Resonance() in ImagingReso

        :param energy_min:
        :type energy_min:
        :param energy_max:
        :type energy_max:
        :param energy_step:
        :type energy_step:
        :param database:
        :type database:
        """
        self.energy_min = energy_min
        self.energy_max = energy_max
        self.energy_step = energy_step
        self.database = database

        self.o_reso = Resonance(energy_min=energy_min,
                                energy_max=energy_max,
                                energy_step=energy_step,
                                database=database)
        self.neutron_pulse = None

        self.layer_list = []
        self.layer = fit_util.Layer()

        self.x_tof_us = None
        self.y_att = None

    def add_layer(self, layer: str, thickness_mm: float, density_gcm3=np.NaN):
        """

        :param layer:
        :type layer:
        :param thickness_mm:
        :type thickness_mm:
        :param density_gcm3:
        :type density_gcm3:
        :return:
        :rtype:
        """
        self.o_reso.add_layer(formula=layer,
                              thickness=thickness_mm,
                              density=density_gcm3)
        self.layer_list.append(layer)
        self.layer.add_layer(layer=layer, thickness_mm=thickness_mm, density_gcm3=density_gcm3)

    def add_Layer(self, layer: Layer):
        """
        Add layer using Layer class

        :param layer:
        """
        for _each_layer in list(layer.info.keys()):
            self.add_layer(layer=_each_layer,
                           thickness_mm=layer.info[_each_layer]['thickness']['value'],
                           density_gcm3=layer.info[_each_layer]['density']['value'])

    def set_isotopic_ratio(self, layer, element, new_isotopic_ratio_list):
        """
        Set isotopic ratios for picked element and update x y values to pass

        :param layer:
        :param element:
        :param new_isotopic_ratio_list:
        :return: x in eV
                 y in attenuation
        """
        if type(new_isotopic_ratio_list) is not list:
            raise ValueError("{} is not a list".format(new_isotopic_ratio_list))
        # Check if layer exist
        if layer not in self.layer_list:
            raise ValueError('Layer {} does not exist.'.format(layer))
        # Check if element exist
        _formula = re.findall(r'([A-Z][a-z]*)(\d*)', layer)
        _elements = []
        for _element in _formula:
            _single_element = list(_element)[0]
            _elements.append(_single_element)
        if element not in _elements:
            raise ValueError('Element {} specified does not exist in {} layer.'.format(element, layer))
        self.o_reso.set_isotopic_ratio(compound=layer, element=element, list_ratio=new_isotopic_ratio_list)
        # self.x_simu = np.array(self.o_reso.total_signal['energy_eV']).round(5)
        # self.y_simu = np.array(self.o_reso.total_signal['attenuation'])

    def get_x(self, x_type, offset_us=None, source_to_detector_m=None, t_unit='us',
              t_start_us=None, time_resolution_us=None, num_offset=0):
        """
        Get x by specified type

        :param x_type:
        :type x_type:
        :param offset_us:
        :type offset_us:
        :param source_to_detector_m:
        :type source_to_detector_m:
        :return: x in specified type
        :rtype: np.array
        """
        fit_util.check_if_in_list(x_type, fit_util.x_type_list)
        _x = np.array(self.o_reso.total_signal['energy_eV']).round(5)
        x = fit_util.convert_energy_to(x=_x,
                                       x_type=x_type,
                                       offset_us=offset_us,
                                       source_to_detector_m=source_to_detector_m,
                                       t_unit=t_unit,
                                       t_start_us=t_start_us,
                                       time_resolution_us=time_resolution_us,
                                       num_offset=num_offset)
        return x

    def get_y(self, y_type):
        """
        Get x by specified type

        :param y_type:
        :type y_type:
        :return: y in specified type
        :rtype: np.array
        """
        fit_util.check_if_in_list(y_type, fit_util.y_type_list)
        y = self.o_reso.total_signal[y_type]
        return y

    def _convolve_beam_shapes(self, source_to_detector_m, conv_proton, proton_params={}, model_index=1):
        _file_path = os.path.abspath(os.path.dirname(__file__))
        _rel_path_to_neutron1 = 'data/_data_for_tutorial/neutron_pulse/source_section_1.dat'
        _rel_path_to_neutron2 = 'data/_data_for_tutorial/neutron_pulse/source_section_2.dat'
        path1 = os.path.join(_file_path, _rel_path_to_neutron1)
        path2 = os.path.join(_file_path, _rel_path_to_neutron2)

        self.neutron_pulse = NeutronPulse(path1, model_index=model_index)
        self.neutron_pulse.load_shape_each(path2)
        self.neutron_pulse.fit_shape(e_min=1, e_max=500,
                                     drop=False, norm=True,
                                     check_each=False,
                                     save_fig=False,
                                     overwrite_csv=False)
        self.neutron_pulse.fit_params(check_each=False, loglog_fit=True, overwrite_csv=False)

        self.neutron_pulse.make_shape(e_ev=self.get_x(x_type='energy'), t_interp=None, for_sum=True, norm=False,
                                      source_to_detector_m=source_to_detector_m,
                                      conv_proton=conv_proton, proton_params=proton_params,
                                      overwrite_csv=False)

        tof_beam_shape_df = self.neutron_pulse.shape_tof_df_interp.set_index('tof_us')
        tof_trans_df = tof_beam_shape_df * self.o_reso.total_signal['transmission']

        tof_beam_shape_df['sum'] = tof_beam_shape_df.sum(axis=1)
        tof_trans_df['sum'] = tof_trans_df.sum(axis=1)
        # print(tof_beam_shape_df)
        # print(tof_trans_df)

        self.x_tof_us = np.array(tof_beam_shape_df.index)
        self.y_att = 1 - np.array(tof_trans_df['sum'] / tof_beam_shape_df['sum'])

    def peak_map(self, x_type, y_type, thres=0.15, min_dist=20, impr_reso=True,
                 offset_us=None, source_to_detector_m=None, t_unit='us',
                 t_start_us=None, time_resolution_us=None, num_offset=0,
                 ):
        """
        Get peak map for each element and/or isotope

        :param thres:
        :type thres:
        :param min_dist:
        :type min_dist:
        :param impr_reso:
        :type impr_reso:
        :return:
        :rtype:
        """
        if len(self.layer_list) == 0:
            raise ValueError("No layer has been added.")

        _stack_signal = self.o_reso.stack_signal
        _layer_list = self.layer_list
        # _x_energy = _stack_signal[_layer_list[0]][_layer_list[0]]['energy_eV']
        _x = self.get_x(x_type=x_type,
                        offset_us=offset_us,
                        source_to_detector_m=source_to_detector_m,
                        t_unit=t_unit,
                        t_start_us=t_start_us,
                        time_resolution_us=time_resolution_us,
                        num_offset=num_offset
                        )
        # _x = sorted(_x)
        peak_map = {}
        for _ele in _layer_list:
            # Isotope
            for _iso in self.o_reso.stack[_ele][_ele]['isotopes']['list']:
                peak_map[_iso] = {}
                _iso_y = _stack_signal[_ele][_ele][_iso]['attenuation']
                _peak_df = fit_util.find_peak(x=_x, y=_iso_y, x_name='x',
                                              thres=thres, min_dist=min_dist,
                                              imprv_reso=impr_reso)
                if y_type == 'transmission':
                    _peak_df['y'] = 1 - _peak_df['y']
                peak_map[_iso]['ideal'] = _peak_df
            # Element
            peak_map[_ele] = {}
            _ele_y = _stack_signal[_ele][_ele]['attenuation']
            _peak_df = fit_util.find_peak(x=_x, y=_ele_y, x_name='x',
                                          thres=thres, min_dist=min_dist,
                                          imprv_reso=impr_reso)
            if y_type == 'transmission':
                _peak_df['y'] = 1 - _peak_df['y']
            peak_map[_ele]['ideal'] = _peak_df
        peak_map_dict = {'peak_map': peak_map,
                         'x_type': x_type,
                         'y_type': y_type}
        return peak_map_dict

    def plot(self, y_type='attenuation', x_type='energy',
             logx=False, logy=False,
             mixed=True, all_layers=False, all_elements=False,
             all_isotopes=False, items_to_plot=None, time_unit='us', offset_us=0.,
             source_to_detector_m=16.,
             t_start_us=1,
             time_resolution_us=0.16,
             ax_mpl=None,
             fmt='-', ms=2, lw=1.5, alpha=1):
        if len(self.layer_list) == 0:
            raise ValueError("No layer has been added.")
        if items_to_plot is not None:
            # shape format of items
            items = fit_util.Items(o_reso=self.o_reso, database=self.database)
            items_to_plot = items.shaped(items_list=items_to_plot)

        ax = self.o_reso.plot(y_axis=y_type, x_axis=x_type, mixed=mixed,
                              all_layers=all_layers, all_elements=all_elements,
                              all_isotopes=all_isotopes, items_to_plot=items_to_plot,
                              source_to_detector_m=source_to_detector_m,
                              offset_us=offset_us,
                              time_resolution_us=time_resolution_us,
                              time_unit=time_unit,
                              t_start_us=t_start_us,
                              ax_mpl=ax_mpl,
                              logx=logx,
                              logy=logy,
                              # plotly=plotly
                              fmt=fmt,
                              ms=ms,
                              lw=lw,
                              alpha=alpha)
        return ax

    def export(self, output_type='clip', filename=None, x_type='energy', y_type='attenuation',
               all_layers=False, all_elements=False, all_isotopes=False, items_to_export=None,
               offset_us=0., source_to_detector_m=16.,
               t_start_us=1, time_resolution_us=0.16, time_unit='us'):
        if items_to_export is not None:
            # Shape items
            items = fit_util.Items(o_reso=self.o_reso, database=self.database)
            items_to_export = items.shaped(items_list=items_to_export)

        _df = self.o_reso.export(output_type=output_type,
                                 filename=filename,
                                 x_axis=x_type,
                                 y_axis=y_type,
                                 all_layers=all_layers,
                                 all_elements=all_elements,
                                 all_isotopes=all_isotopes,
                                 items_to_export=items_to_export,
                                 offset_us=offset_us,
                                 source_to_detector_m=source_to_detector_m,
                                 t_start_us=t_start_us,
                                 time_resolution_us=time_resolution_us,
                                 time_unit=time_unit)
        return _df