Example #1
0
    def import_data(self, filepath, extra_parameters=None):
        """
        Import the raw EELS spectrum data (elv file).

        :param str filepath: File path of the .elv file.
        :param dict extra_parameters: Extra parameters to add as attribute in this data group.
        :return: None.
        """
        if extra_parameters:
            self.extra_parameters.update(extra_parameters)

        with open(filepath, 'r', encoding="ANSI", errors='ignore') as elv_file:
            elv_data = ElvFile()
            elv_data.read(elv_file)

            self.energies_eV = np.array(elv_data.energies_eV)
            self.raw_counts = np.array(elv_data.raw_counts)
            self.gain_corrections = np.array(elv_data.gain_corrections)
            self.dark_currents = np.array(elv_data.dark_currents)

            self.eels_parameters.update(elv_data.parameters())

        filepath_txt = os.path.splitext(filepath)[0] + ".txt"
        with open(filepath_txt, 'r', encoding="UTF-16",
                  errors='ignore') as elv_text_file:
            elv_text_parameters = ElvTextParameters()
            elv_text_parameters.read(elv_text_file)

            self.eels_parameters.update(elv_text_parameters.items())
Example #2
0
    def generate(self):
        with open(self.elv_file_path, 'r') as elv_text_file:
            elv_file = ElvFile()
            elv_file.read(elv_text_file)

            plt.figure()

            plt.plot(elv_file.energies_eV[:-1], elv_file.counts[:-1])

            energy_eV = elv_file.energies_eV[elv_file.loss_energy_channel]
            plt.axvline(energy_eV, ls='-', color='b', alpha=0.5, label="EL")

            colors = {"PreL": "g", "PreH": "b", "Post": "r"}

            for segment_name in elv_file.segments_channel:
                min_index, max_index = elv_file.segments_channel[segment_name]
                min_energy_eV = elv_file.energies_eV[min_index]
                max_energy_eV = elv_file.energies_eV[max_index]
                plt.axvspan(min_energy_eV,
                            max_energy_eV,
                            color=colors[segment_name],
                            alpha=0.2,
                            label=segment_name)

            plt.xlabel("Energy loss (eV)")
            plt.ylabel("Count")
            plt.legend()
            plt.tight_layout()

            path = os.path.dirname(self.elv_file_path)
            figure_file_path = os.path.join(path, "windows.png")
            plt.savefig(figure_file_path)
            plt.close()
Example #3
0
    def test_read_file(self):
        """
        First test to check if the testcase is working with the testing framework.
        """

        with open(self.elv_file_path, 'r') as elv_text_file:
            elv_file = ElvFile()
            elv_file.read(elv_text_file)

            self.assertEqual("01/Mar/2017", elv_file.date)
            self.assertEqual("10:59", elv_file.time)
            self.assertEqual("", elv_file.comment)
            self.assertEqual(500, elv_file.dose)
            self.assertEqual(0.0, elv_file.le)
            self.assertEqual(98.7, elv_file.raw)
            self.assertEqual(7.0, elv_file.energy_width)
            self.assertEqual(586, elv_file.dual_det_position)
            self.assertEqual(133, elv_file.dual_det_post)
            self.assertEqual(608, elv_file.dual_det_center)
            self.assertEqual(13575, elv_file.q1)
            self.assertEqual(3850, elv_file.q1s)
            self.assertEqual(0, elv_file.q2)
            self.assertEqual(0, elv_file.q2s)
            self.assertEqual(2700, elv_file.q3)
            self.assertEqual(2900, elv_file.h1)
            self.assertEqual(6150, elv_file.h1s)
            self.assertEqual(-600, elv_file.h2)
            self.assertEqual(350, elv_file.h2s)
            self.assertEqual(0, elv_file.h4)
            self.assertEqual(0, elv_file.elv_x)
            self.assertEqual(0, elv_file.elv_y)
            self.assertEqual(259, elv_file.spectrum_alignment_x)
            self.assertEqual(0, elv_file.spectrum_alignment_y)
            self.assertEqual(-1500, elv_file.det_spec_alignment_x)
            self.assertEqual(470, elv_file.det_spec_alignment_y)
            self.assertEqual(-1500, elv_file.det_map_alignment_x)
            self.assertEqual(1500, elv_file.det_map_alignment_y)

            self.assertEqual(37443, elv_file.mag)

            self.assertEqual(-32.00, elv_file.energies_eV[0])
            self.assertEqual(2282, elv_file.raw_counts[0])
            self.assertEqual(21.84, elv_file.energies_eV[-1])
            self.assertEqual(0, elv_file.raw_counts[-1])
            self.assertEqual(1024, len(elv_file.energies_eV))
            self.assertEqual(1024, len(elv_file.raw_counts))

            self.assertEqual(0.918375, elv_file.gain_corrections[0])
            self.assertEqual(0.000000, elv_file.gain_corrections[-1])
            self.assertEqual(1024, len(elv_file.gain_corrections))

            self.assertEqual(2313, elv_file.dark_currents[0])
            self.assertEqual(0, elv_file.dark_currents[-1])
            self.assertEqual(1024, len(elv_file.dark_currents))
Example #4
0
    def test_find_position(self):
        """
        Test the find_position method is working.

        """
        with open(self.elv_file_path, 'r', encoding="ANSI",
                  errors='ignore') as elv_text_file:
            elv_file = ElvFile()
            elv_file.read(elv_text_file)

            zero_lost_peak = ZeroLossPeak(elv_file.energies_eV,
                                          elv_file.raw_counts)
            zero_lost_peak.find_position()
            self.assertAlmostEqual(0.05, zero_lost_peak.position_eV)
Example #5
0
    def open_spectrum(self, file_names):
        if six.PY3:
            if isinstance(file_names, str):
                file_names = [file_names]
        elif six.PY2:
            if isinstance(file_names, basestring):
                file_name = [file_names]

        for file_name in file_names:
            if os.path.splitext(file_name)[1] == ".elv":
                with open(file_name, 'r') as elv_text_file:
                    elv_file = ElvFile()
                    elv_file.read(elv_text_file)

                    self.set_current_elv_file(elv_file)
Example #6
0
    def test_compute_fwhm(self):
        """
        Test the compute_fwhm method is working.
        """

        with open(self.elv_file_path, 'r', encoding="ANSI",
                  errors='ignore') as elv_text_file:
            elv_file = ElvFile()
            elv_file.read(elv_text_file)

            zero_lost_peak = ZeroLossPeak(elv_file.energies_eV,
                                          elv_file.raw_counts)
            zero_lost_peak.compute_fwhm()
            self.assertAlmostEqual(0.57999999999999996, zero_lost_peak.fwhm_eV)
            self.assertAlmostEqual(1.3700, zero_lost_peak.fwtm_eV)
            self.assertAlmostEqual(1.74, zero_lost_peak.fwfm_eV)
Example #7
0
    def test_compute_statistics(self):
        """
        Test the compute_statistics method is working.
        """

        with open(self.elv_file_path, 'r', encoding="ANSI",
                  errors='ignore') as elv_text_file:
            elv_file = ElvFile()
            elv_file.read(elv_text_file)

            zero_lost_peak = ZeroLossPeak(elv_file.energies_eV,
                                          elv_file.raw_counts)
            zero_lost_peak.compute_statistics()
            self.assertAlmostEqual(1042790, zero_lost_peak.number_counts)
            self.assertAlmostEqual(-4.16, zero_lost_peak.minimum_eV)
            self.assertAlmostEqual(4.16, zero_lost_peak.maximum_eV)
            self.assertAlmostEqual(0.04945319767163061, zero_lost_peak.mean_eV)
            self.assertAlmostEqual(2.2138369332753909,
                                   zero_lost_peak.variance_eV2)
            self.assertAlmostEqual(1.4878968153993042, zero_lost_peak.std_eV)
            self.assertAlmostEqual(-0.07548493790601353,
                                   zero_lost_peak.skewness)
            self.assertAlmostEqual(1.5175832076107003, zero_lost_peak.kurtosis)
Example #8
0
    def generate_msa(self):
        if self.msa_file_path is None:
            file_path, _extension = os.path.splitext(self.elv_file_path)
            file_path += ".msa"

        with open(self.elv_file_path, 'r', encoding="ANSI",
                  errors='ignore') as elv_text_file:
            elv_file = ElvFile()
            elv_file.read(elv_text_file)

            elv_file.export_msa(file_path)
Example #9
0
    def add_spectrum(self, file_path, name=None):
        if name is None:
            basename, _extension = os.path.splitext(
                os.path.basename(file_path))
            name = basename

        spectrum_group = self.hdf5_file.create_group(name)

        elv_text_file_path, _extension = os.path.splitext(file_path)
        elv_text_file_path += '.txt'
        with open(elv_text_file_path, 'r', encoding="UTF-16",
                  errors='ignore') as elv_text_file:
            elv_text_parameters = ElvTextParameters()
            elv_text_parameters.read(elv_text_file)

            spectrum_group.attrs[HDF5_MODEL] = elv_text_parameters.model
            spectrum_group.attrs[
                HDF5_SAMPLE_HEIGHT] = elv_text_parameters.sample_height_mm
            spectrum_group.attrs[
                HDF5_FILE_PATH] = elv_text_parameters.file_name
            spectrum_group.attrs[HDF5_COMMENT] = elv_text_parameters.comment
            spectrum_group.attrs[HDF5_DATE] = elv_text_parameters.date
            spectrum_group.attrs[HDF5_TIME] = elv_text_parameters.time
            spectrum_group.attrs[
                HDF5_ACCELERATING_VOLTAGE_V] = elv_text_parameters.accelerating_voltage_V
            spectrum_group.attrs[
                HDF5_ENERGY_WIDTH_eV] = elv_text_parameters.energy_width_eV
            spectrum_group.attrs[
                HDF5_ENERGY_LOSS] = elv_text_parameters.energy_loss_eV
            spectrum_group.attrs[
                HDF5_ACQUISITION_SPEED] = elv_text_parameters.speed_us

        with open(file_path, 'r', encoding="ANSI",
                  errors='ignore') as elv_text_file:
            elv_file = ElvFile()
            elv_file.read(elv_text_file)

            self.compare_attribute(spectrum_group, HDF5_DATE, elv_file.date)
            self.compare_attribute(spectrum_group, HDF5_TIME, elv_file.time)
            self.compare_attribute(spectrum_group, HDF5_COMMENT,
                                   elv_file.comment)
            self.compare_attribute(spectrum_group, HDF5_ACQUISITION_SPEED,
                                   elv_file.dose)
            self.compare_attribute(spectrum_group, HDF5_ENERGY_LOSS,
                                   elv_file.le)

            spectrum_group.attrs[HDF5_RAW] = elv_file.raw

            self.compare_attribute(spectrum_group, HDF5_ENERGY_WIDTH_eV,
                                   elv_file.energy_width)

            spectrum_group.attrs[
                HDF5_DUAL_DET_POSITION] = elv_file.dual_det_position
            spectrum_group.attrs[HDF5_DUAL_DET_POST] = elv_file.dual_det_post
            spectrum_group.attrs[
                HDF5_DUAL_DET_CENTER] = elv_file.dual_det_center
            spectrum_group.attrs[HDF5_Q1] = elv_file.q1
            spectrum_group.attrs[HDF5_Q1S] = elv_file.q1s
            spectrum_group.attrs[HDF5_Q2] = elv_file.q2
            spectrum_group.attrs[HDF5_Q2S] = elv_file.q2s
            spectrum_group.attrs[HDF5_Q3] = elv_file.q3
            spectrum_group.attrs[HDF5_H1] = elv_file.h1
            spectrum_group.attrs[HDF5_H1S] = elv_file.h1s
            spectrum_group.attrs[HDF5_H2] = elv_file.h2
            spectrum_group.attrs[HDF5_H2S] = elv_file.h2s
            spectrum_group.attrs[HDF5_H4] = elv_file.h4
            spectrum_group.attrs[HDF5_ELV_X] = elv_file.elv_x
            spectrum_group.attrs[HDF5_ELV_Y] = elv_file.elv_y
            spectrum_group.attrs[
                HDF5_SPECTRUM_ALIGNMENT_X] = elv_file.spectrum_alignment_x
            spectrum_group.attrs[
                HDF5_SPECTRUM_ALIGNMENT_Y] = elv_file.spectrum_alignment_y
            spectrum_group.attrs[
                HDF5_DET_SPEC_ALIGNMENT_X] = elv_file.det_spec_alignment_x
            spectrum_group.attrs[
                HDF5_DET_SPEC_ALIGNMENT_Y] = elv_file.det_spec_alignment_y
            spectrum_group.attrs[
                HDF5_DET_MAP_ALIGNMENT_X] = elv_file.det_map_alignment_x
            spectrum_group.attrs[
                HDF5_DET_MAP_ALIGNMENT_Y] = elv_file.det_map_alignment_y

            spectrum_group.attrs[HDF5_MAGNIFICATION] = elv_file.mag

            data = np.zeros((1023, 5))
            data[:, 0] = elv_file.energies_eV[:-1]
            data[:, 1] = elv_file.counts[:-1]
            data[:, 2] = elv_file.raw_counts[:-1]
            data[:, 3] = elv_file.gain_corrections[:-1]
            data[:, 4] = elv_file.dark_currents[:-1]

            spectrum_data_set = spectrum_group.create_dataset(HDF5_SPECTRUM,
                                                              data=data)

            data = np.arange(1, 1023 + 1)
            spectrum_channel_data_set = spectrum_group.create_dataset(
                HDF5_SPECTRUM_CHANNELS, data=data)
            spectrum_data_set.dims.create_scale(spectrum_channel_data_set,
                                                HDF5_SPECTRUM_CHANNEL)
            spectrum_data_set.dims[0].attach_scale(spectrum_channel_data_set)

            data_types = [
                HDF5_SPECTRUM_ENERGIES_eV, HDF5_SPECTRUM_COUNTS,
                HDF5_SPECTRUM_RAW_COUNTS, HDF5_SPECTRUM_GAIN_CORRECTIONS,
                HDF5_SPECTRUM_DARK_CURRENTS
            ]
            max_size = max([len(data_type) for data_type in data_types])
            data = np.array(data_types, dtype="S{}".format(max_size + 1))
            spectrum_types_data_set = spectrum_group.create_dataset(
                HDF5_SPECTRUM_DATA_TYPES, data=data)
            spectrum_data_set.dims.create_scale(spectrum_types_data_set,
                                                HDF5_SPECTRUM_DATA_TYPE)
            spectrum_data_set.dims[1].attach_scale(spectrum_types_data_set)
Example #10
0
        self.fit_results_sigma_eV = values['sigma']
        self.fit_results_gamma_eV = values['gamma']
        self.fit_results_area = values['amplitude']
        self.fit_results_height = values['height']


if __name__ == '__main__':
    import matplotlib.pyplot as plt
    from pysemeels import get_current_module_path
    from pysemeels.hitachi.eels_su.elv_file import ElvFile
    import numpy as np

    elv_file_path = get_current_module_path(
        __file__, "../../test_data/hitachi/eels_su/30kV_7eV.elv")
    with open(elv_file_path, 'r') as elv_text_file:
        elv_file = ElvFile()
        elv_file.read(elv_text_file)

        plt.figure()

        plt.plot(
            elv_file.energies_eV,
            np.array(elv_file.raw_counts) - np.array(elv_file.dark_currents),
            '.')

        zlp = ZeroLossPeak(
            elv_file.energies_eV,
            np.array(elv_file.raw_counts) - np.array(elv_file.dark_currents))
        zlp.compute_statistics()
        zlp.compute_fwhm()
        energy_min_eV = zlp.energies_eV[zlp.roi_indices[0]]