def testWebsiteExample(self): """ Test from value given in the Egerton (2011) book website. """ # Drude(ep,ew,eb,epc,e0,beta,nn,tnm) energies_eV, eps1, eps2, im_eps, im4_eps, re_eps, probabilities_total, probabilities_volume, probabilities_surface, ps, pv, mfp_volume_nm, mfp_free_nm = drude(15, 3, 0, 0.1, 200, 5, 500, 50) self.assertEqual(500, len(eps1)) self.assertEqual(500, len(eps2)) self.assertEqual(500, len(im_eps)) self.assertEqual(500, len(im4_eps)) self.assertEqual(500, len(re_eps)) self.assertEqual(500, len(probabilities_total)) self.assertEqual(500, len(probabilities_volume)) self.assertEqual(500, len(probabilities_surface)) self.assertAlmostEqual(0.0163826, ps, 7) # self.assertAlmostEqual(0.254905, pv, 6) self.assertAlmostEqual(0.25491101951346251, pv, 6) # self.assertAlmostEqual(196.151, mfp_volume_nm, 3) self.assertAlmostEqual(196.14687546828225, mfp_volume_nm, 3) self.assertAlmostEqual(183.836505, mfp_free_nm, 6) test_data_file_path = get_current_module_path(__file__, "../../test_data/egerton2011/Drude.ssd") if is_bad_file(test_data_file_path): pytest.skip("File not found: {}".format(test_data_file_path)) with open(test_data_file_path, 'r') as ref_file: lines = ref_file.readlines() for channel_id, line in enumerate(lines): items = line.split() xx = float(items[0]) yy = float(items[1]) self.assertAlmostEqual(xx, energies_eV[channel_id], 7) self.assertAlmostEqual(yy, probabilities_total[channel_id], 7, channel_id) test_data_file_path = get_current_module_path(__file__, "../../test_data/egerton2011/Drude.eps") if is_bad_file(test_data_file_path): pytest.skip("File not found: {}".format(test_data_file_path)) with open(test_data_file_path, 'r') as ref_file: lines = ref_file.readlines() for channel_id, line in enumerate(lines): items = line.split() xx = float(items[0]) yy = float(items[1]) yy2 = float(items[2]) self.assertAlmostEqual(xx, energies_eV[channel_id], 7) self.assertAlmostEqual(yy, eps1[channel_id], 7, channel_id) self.assertAlmostEqual(yy2, eps2[channel_id], 7, channel_id)
def setUp(self): """ Setup method. """ unittest.TestCase.setUp(self) self.elv_file_path = get_current_module_path(__file__, "../../test_data/hitachi/eels_su/30kV_7eV.elv") self.hdf5_file_path = get_current_module_path(__file__, "../../test_data/hitachi/eels_su/30kV_7eV.hdf5") if not os.path.isfile(self.elv_file_path): # pragma: no cover pytest.skip("File not found: {}".format(self.elv_file_path))
def setUp(self): """ Setup method. """ unittest.TestCase.setUp(self) self.path = get_current_module_path(__file__, r"../../test_data") self.figures_file_path = get_current_module_path(__file__, r"../../test_data\hitachi\eels_su\SCNA9_EFSTEM_C_04\windows.png") if not os.path.isdir(self.path): # pragma: no cover pytest.skip("File not found: {}".format(self.path))
def setUp(self): """ Setup method. """ unittest.TestCase.setUp(self) if sys.platform != "win32": pytest.skip("only run on windows") self.name_ref = "TestRawSpectrum" self.spectrum = RawSpectrum(self.name_ref) self.test_data_path = get_current_module_path(__file__, '../test_data') self.name_import_ref = "30kV_7eV" self.import_sem_parameters = { HDF5_ATTRIBUTE_ACCELERATING_VOLTAGE_V: 30.0e3 } self.elv_file_path = os.path.join(self.test_data_path, "hitachi/eels_su/30kV_7eV.elv") if is_bad_file(self.elv_file_path): pytest.skip("File not found: {}".format(self.elv_file_path)) filepath = self.elv_file_path name = os.path.splitext(os.path.basename(filepath))[0] self.spectrum_import = RawSpectrum(name) self.spectrum_import.import_data(filepath, self.import_sem_parameters)
def setUp(self): """ Setup method. """ unittest.TestCase.setUp(self) self.name_ref = "TestProject" self.project = Project(self.name_ref) self.project.spectra.append(RawSpectrum("RawSpectrum_1")) self.project.spectra.append(RawSpectrum("RawSpectrum_2")) self.project.spectra.append(RawSpectrum("RawSpectrum_3")) self.project.si_points.append(Point("Point_1")) self.project.si_points.append(Point("Point_2")) self.project.si_points.append(Point("Point_3")) self.project.si_line_scans.append(Linescan("Linescan_1")) self.project.si_line_scans.append(Linescan("Linescan_2")) self.project.si_maps.append(Map("Map_3")) self.project.energy_filtered_micrographs.append(Eftem("Eftem_1")) self.test_data_path = get_current_module_path(__file__, '../test_data')
def test_read_oos_file(self): """ Test the method to read the oos file. """ file_path = get_current_module_path(__file__, "../../test_data/mc/li_hcp.oos") if is_bad_file(file_path): pytest.skip("File not found: {}".format(file_path)) oos_file = OosFile() oos_file.read_file(file_path) self.assertEqual("OOS of li", oos_file.title) self.assertEqual(20, oos_file.number_electrons) self.assertAlmostEqual(6.941, oos_file.molecular_weight) self.assertAlmostEqual(0.5, oos_file.density_g_cm3) self.assertAlmostEqual(55.0, oos_file.switch_energy_eV) self.assertEqual(346, len(oos_file.energies_eV)) self.assertEqual(346, len(oos_file.oos)) self.assertAlmostEqual(0.01361, oos_file.energies_eV[0]) self.assertAlmostEqual(4.2988e-09, oos_file.oos[0]) self.assertAlmostEqual(4.3295e+05, oos_file.energies_eV[-1]) self.assertAlmostEqual(4.6583e-13, oos_file.oos[-1])
def test_read_file_linescan(self): """ First test to check if the testcase is working with the testing framework. """ ana_file_path = get_current_module_path( __file__, "../../../../test_data/hitachi/eels_su/vacuum_linescan_07eV_i50_52pts/spectra_3.ana" ) if is_bad_file(ana_file_path): pytest.skip("File not found: {}".format(ana_file_path)) with open(ana_file_path, 'r') as ana_text_file: ana_file = AnaFile() ana_file.read(ana_text_file) self.assertEqual("03/Mar/2017", ana_file.date) self.assertEqual("15:32", ana_file.time) self.assertEqual("", ana_file.comment) self.assertEqual("0.5ms", ana_file.dose) self.assertEqual("0.0eV", ana_file.le) self.assertEqual(75.2, ana_file.raw) self.assertEqual("7.0eV", ana_file.energy_width) self.assertEqual("586ch", ana_file.dual_det_position) self.assertEqual("133ch", ana_file.dual_det_post) self.assertEqual("608ch", ana_file.dual_det_center) self.assertEqual(13560, ana_file.q1) self.assertEqual(4150, ana_file.q1s) self.assertEqual(0, ana_file.q2) self.assertEqual(0, ana_file.q2s) self.assertEqual(2700, ana_file.q3) self.assertEqual(0, ana_file.q3s) self.assertEqual(3050, ana_file.h1) self.assertEqual(5850, ana_file.h1s) self.assertEqual(-650, ana_file.h2) self.assertEqual(1250, ana_file.h2s) self.assertEqual(0, ana_file.h3) self.assertEqual(0, ana_file.h3s) self.assertEqual(0, ana_file.elv_x) self.assertEqual(0, ana_file.elv_y) self.assertEqual(196, ana_file.spectrum_alignment_x) self.assertEqual(0, ana_file.spectrum_alignment_y) self.assertEqual(-1280, ana_file.det_map_alignment_x) self.assertEqual(1500, ana_file.det_map_alignment_y) self.assertEqual(37443, ana_file.mag) self.assertEqual(1024, len(ana_file.energies_eV)) self.assertEqual(52, len(ana_file.total_spectra)) self.assertEqual(1024, len(ana_file.total_spectra[0])) self.assertEqual(1024, len(ana_file.total_spectra[51])) self.assertEqual(-32.00, ana_file.energies_eV[0]) self.assertEqual(1.0, ana_file.total_spectra[0][0]) self.assertEqual(21.842, ana_file.energies_eV[-1]) self.assertEqual(0, ana_file.total_spectra[0][-1]) self.assertEqual(52, len(ana_file.spectra)) self.assertEqual(50, len(ana_file.spectra[0])) self.assertEqual(49, ana_file.spectrum_id)
def setUp(self): """ Setup method. """ unittest.TestCase.setUp(self) self.elv_file_path = get_current_module_path( __file__, r"../../test_data\hitachi\eels_su\SCNA9_EFSTEM_C_04\windows.elv") self.figures_file_path = get_current_module_path( __file__, r"../../test_data\hitachi\eels_su\SCNA9_EFSTEM_C_04\windows.png") if is_bad_file(self.elv_file_path): pytest.skip("File not found: {}".format(self.elv_file_path))
def setUp(self): """ Setup method. """ unittest.TestCase.setUp(self) self.path = get_current_module_path( __file__, r"../../test_data/hitachi/eels_su/vacuum_linescan_07eV_i50_52pts") self.figures_file_path = get_current_module_path( __file__, r"../../test_data/hitachi/eels_su/vacuum_linescan_07eV_i50_52pts/vacuum_linescan_07eV_i50_52pts-all_spectra_3.pdf" ) if not os.path.isdir(self.path): # pragma: no cover pytest.skip("File not found: {}".format(self.path))
def testWebsiteExample(self): """ Test from value given in the Egerton (2011) book website. """ eout, outssd, outpsd = spec_gen(16.7, 3.2, 1, 5, 0.1, 10000, 1.5, 1000, 2, 0.5, 10) self.assertEqual(1000, len(eout)) self.assertEqual(1000, len(outssd)) self.assertEqual(1000, len(outpsd)) test_data_file_path = get_current_module_path( __file__, "../../test_data/egerton2011/SpecGen.ssd") if is_bad_file(test_data_file_path): pytest.skip("File not found: {}".format(test_data_file_path)) with open(test_data_file_path, 'r') as ref_file: lines = ref_file.readlines() for channel_id, line in enumerate(lines): items = line.split() xx = float(items[0]) yy = float(items[1]) self.assertAlmostEqual(xx, eout[channel_id], 7) self.assertAlmostEqual(yy, outssd[channel_id], 7, channel_id) test_data_file_path = get_current_module_path( __file__, "../../test_data/egerton2011/SpecGen.psd") if is_bad_file(test_data_file_path): pytest.skip("File not found: {}".format(test_data_file_path)) with open(test_data_file_path, 'r') as ref_file: lines = ref_file.readlines() for channel_id, line in enumerate(lines): items = line.split() xx = float(items[0]) yy = float(items[1]) self.assertAlmostEqual(xx, eout[channel_id], 7) self.assertAlmostEqual(yy, outpsd[channel_id], 7, channel_id)
def setUp(self): """ Setup method. """ unittest.TestCase.setUp(self) self.elv_text_file_path = get_current_module_path(__file__, "../../../test_data/hitachi/eels_su/30kV_7eV.txt") if is_bad_file(self.elv_text_file_path): pytest.skip("File not found: {}".format(self.elv_text_file_path))
def export_msa(): from pysemeels import get_current_module_path elv_file_path = get_current_module_path( __file__, r"D:\Dropbox\hdemers\professional\results\experiments\2017\su9000\eels\2017\System_baseline_march2017/30kV_7eV.elv" ) with open(elv_file_path, 'r') as elv_text_file: elv_file = ElvFile() elv_file.read(elv_text_file) msa_file_path = get_current_module_path( __file__, r"D:\Dropbox\hdemers\professional\results\experiments\2017\su9000\eels\2017\System_baseline_march2017/30kV_7eV.msa" ) elv_file.export_msa(msa_file_path) for channel_id in range(len(elv_file.energies_eV) - 1): print(elv_file.energies_eV[channel_id + 1] - elv_file.energies_eV[channel_id])
def setUp(self): """ Setup method. """ unittest.TestCase.setUp(self) self.name_ref = "TestEftem" self.eftem = Eftem(self.name_ref) self.test_data_path = get_current_module_path(__file__, '../test_data')
def setUp(self): """ Setup method. """ unittest.TestCase.setUp(self) self.name_ref = "TestPoint" self.point = Point(self.name_ref) self.test_data_path = get_current_module_path(__file__, '../../test_data')
def setUp(self): """ Setup method. """ unittest.TestCase.setUp(self) self.name_ref = "TestLinescan" self.linescan = Linescan(self.name_ref) self.test_data_path = get_current_module_path(__file__, '../../test_data')
def test_is_git_lfs_file_good(self): self.assertEqual(True, is_git_lfs_file(self.git_lfs_file)) self.assertEqual(True, is_git_lfs_file(self.git_lfs_file2)) file_path = get_current_module_path(__file__, "../test_data/30kV_7eV.elv.lfs") self.assertEqual(True, os.path.isfile(file_path)) if not os.path.isfile(file_path): pytest.skip("File not found: {}".format(file_path)) self.assertEqual(True, is_git_lfs_file(file_path))
def setUp(self): """ Setup method. """ unittest.TestCase.setUp(self) self.ana_file_path = get_current_module_path( __file__, "../../../../test_data/hitachi/eels_su/30kV_march2017_7eV/spectra_1.ana" ) if is_bad_file(self.ana_file_path): pytest.skip("File not found: {}".format(self.ana_file_path))
def test_make_oos(self): """ Test the method to make the oos data. """ oos_maker = OosMaker() oos_maker.title = "OOS of li" oos_maker.number_electrons = 20 oos_maker.molecular_weight = 6.941 oos_maker.density_g_cm3 = 0.5 oos_maker.switch_energy_eV = 55.0 oos_maker.transition_energy_eV = 55.0 oos_file = oos_maker.get_oos_file() file_path = get_current_module_path(__file__, "../../test_data/mc/li_hcp.oos") if is_bad_file(file_path): pytest.skip("File not found: {}".format(file_path)) oos_file_ref = OosFile() oos_file_ref.read_file(file_path) self.assertEqual(oos_file_ref.title, oos_file.title) self.assertEqual(oos_file_ref.number_electrons, oos_file.number_electrons) self.assertEqual(oos_file_ref.molecular_weight, oos_file.molecular_weight) self.assertEqual(oos_file_ref.density_g_cm3, oos_file.density_g_cm3) self.assertEqual(oos_file_ref.switch_energy_eV, oos_file.switch_energy_eV) self.assertAlmostEqual(0.01361, oos_file.energies_eV[0]) self.assertAlmostEqual(4.2988e-09, oos_file.oos[0]) self.assertAlmostEqual(4.3295e+05, oos_file.energies_eV[-1]) self.assertAlmostEqual(4.6583e-13, oos_file.oos[-1]) for energy_ref_eV, df_dw_ref, energy_eV, df_dw in zip(oos_file_ref.energies_eV, oos_file_ref.oos, oos_file.energies_eV, oos_file.oos): self.assertAlmostEqual(energy_ref_eV, energy_eV) self.assertAlmostEqual(df_dw_ref, df_dw) self.fail("Test if the testcase is working.")
self.fit_results_position_eV = values['center'] self.fit_results_fwhm_eV = values['fwhm'] 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()
def plot_spectrum(): import matplotlib.pyplot as plt from pysemeels import get_current_module_path from pysemeels.hitachi.eels_su.ana_file import AnaFile import numpy as np ana_file = AnaFile() ana_file_path = get_current_module_path( __file__, r"D:\Dropbox\hdemers\professional\results\experiments\2017\su9000\eels\2017\System_baseline_march2017/30kV_7eV.ana" ) with open(ana_file_path, 'r') as elv_text_file: ana_file.read(elv_text_file) elv_file_path = get_current_module_path( __file__, r"D:\Dropbox\hdemers\professional\results\experiments\2017\su9000\eels\2017\System_baseline_march2017/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, elv_file.raw_counts, '.') plt.plot(ana_file.energies_eV, ana_file.counts, '-') corrected_counts = (np.array(elv_file.raw_counts) - np.array( elv_file.dark_currents)) / np.array(elv_file.gain_corrections) #plt.plot(ana_file.energies_eV, corrected_counts, '.') plt.plot(ana_file.energies_eV, elv_file.counts, '.') #plt.close() print(elv_file.raw_counts[0]) print(elv_file.gain_corrections[0]) print(elv_file.dark_currents[0]) print(ana_file.counts[0]) print(corrected_counts[0] - ana_file.counts[0]) print( int(elv_file.raw_counts[0]) - int(elv_file.dark_currents[0]) - int(ana_file.counts[0])) print(elv_file.raw_counts[0] * elv_file.gain_corrections[0] - elv_file.dark_currents[0]) print(elv_file.raw_counts[0] / elv_file.gain_corrections[0] - elv_file.dark_currents[0]) print((elv_file.raw_counts[0] - elv_file.dark_currents[0]) * elv_file.gain_corrections[0]) print((elv_file.raw_counts[0] - elv_file.dark_currents[0]) / elv_file.gain_corrections[0]) print(elv_file.raw_counts[0] - elv_file.dark_currents[0] * elv_file.gain_corrections[0]) print(ana_file.counts[0] - (elv_file.raw_counts[0] - elv_file.dark_currents[0]) / elv_file.gain_corrections[0]) plt.figure() plt.plot( ana_file.energies_eV, np.array(ana_file.counts) - (np.array(elv_file.raw_counts) - np.array(elv_file.dark_currents)) / np.array(elv_file.gain_corrections)) plt.figure() plt.plot(elv_file.energies_eV, elv_file.dark_currents, '-') plt.figure() plt.plot(elv_file.energies_eV, elv_file.gain_corrections, '-') plt.close() print(elv_file.energies_eV.index(0.0)) plt.figure() plt.plot(elv_file.energies_eV, elv_file.raw_counts, '-') for energy_eV in [-30.0, -20.0, -10.0, 0.0, 10, 20.0]: plt.axvline(energy_eV, ls='--', color='b') for min_index in [586, 443, 300]: min_energy_eV = elv_file.energies_eV[min_index] max_energy_eV = elv_file.energies_eV[min_index + 133] plt.axvspan(min_energy_eV, max_energy_eV, color='r', alpha=0.5) plt.xlim((elv_file.energies_eV[0], elv_file.energies_eV[-1])) #plt.subplots_adjust(left=0.0, bottom=0.0, right=1.0, top=1.0) plt.xlabel("Energy (eV)") plt.show()
def test_is_bad_file_no_file(self): file_path = get_current_module_path(__file__, "../../test_data/this_file_does_not_exist.txt") self.assertEqual(True, is_bad_file(file_path))
def test_is_git_lfs_file_bad(self): file_path = get_current_module_path(__file__, "test_pysemeels.py") if not os.path.isfile(file_path): pytest.skip("File not found: {}".format(file_path)) self.assertEqual(False, is_git_lfs_file(file_path))
def read_hdf5(self, hdf5_file_path): with h5py.File(hdf5_file_path, 'r') as hdf5_file: self.energies_eV = hdf5_file["energy (eV)"][:] self.total_spectra = hdf5_file["total spectra"][:] if __name__ == '__main__': import matplotlib.pyplot as plt import numpy as np from pysemeels import get_current_module_path ana_file_path = get_current_module_path( __file__, "../../../../test_data/hitachi/eels_su/30kV_march2017_7eV/spectra_1.ana" ) with open(ana_file_path, 'r') as ana_text_file: ana_file = AnaFile() ana_file.read(ana_text_file) plt.figure() plt.plot(ana_file.energies_eV, ana_file.total_spectra, '.') energy_eV = ana_file.energies_eV[133] plt.axvline(energy_eV) energy_eV = ana_file.energies_eV[586] plt.axvline(energy_eV) energy_eV = ana_file.energies_eV[608] plt.axvline(energy_eV) plt.figure()
buffer = file.read(size) if len(buffer) == 0: break counts = struct.unpack(format, buffer) self.raw_spectra.append(counts) self.raw_spectrum_id += 1 if __name__ == '__main__': import matplotlib.pyplot as plt import numpy as np from pysemeels import get_current_module_path file_path = get_current_module_path( __file__, "../../../../test_data/hitachi/eels_su/30kV_march2017_7eV/RawSpectra/rawspect-1.dat" ) with open(file_path, 'rb') as raw_spectra_file: raw_spectra = RawSpectra() raw_spectra.read(raw_spectra_file) xs = np.arange(1024) plt.figure() for spectrum in raw_spectra.raw_spectra: plt.plot(xs, spectrum, '-') plt.show()