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 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 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 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 test_import_data(self): """ Test import_data method. """ 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] spectrum = RawSpectrum(name) spectrum.import_data(filepath) self.assertEqual(-32.00, spectrum.energies_eV[0]) self.assertEqual(2282, spectrum.raw_counts[0]) self.assertEqual(21.84, spectrum.energies_eV[-1]) self.assertEqual(0, spectrum.raw_counts[-1]) self.assertEqual(1024, len(spectrum.energies_eV)) self.assertEqual(1024, len(spectrum.raw_counts)) self.assertEqual(0.918375, spectrum.gain_corrections[0]) self.assertEqual(0.000000, spectrum.gain_corrections[-1]) self.assertEqual(1024, len(spectrum.gain_corrections)) self.assertEqual(2313, spectrum.dark_currents[0]) self.assertEqual(0, spectrum.dark_currents[-1]) self.assertEqual(1024, len(spectrum.dark_currents))
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 test_read_hdf5(self): """ Test read_hdf5 method. """ filepath = os.path.join(self.test_data_path, "test_map_read_hdf5.hdf5") if is_bad_file(filepath): pytest.skip("File not found: {}".format(filepath)) with h5py.File(filepath, "r") as hdf5_file: self.map.read_hdf5(hdf5_file)
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_read_hdf5_bad_project(self): """ Test read_hdf5 method with a different project name. """ name_ref = "TestMap_init" map = Map(name_ref) filepath = os.path.join(self.test_data_path, "test_map_read_hdf5.hdf5") if is_bad_file(filepath): pytest.skip("File not found: {}".format(filepath)) with h5py.File(filepath, "r") as hdf5_file: self.assertRaises(ValueError, map.read_hdf5, hdf5_file)
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 test_read_hdf5(self): """ Test read_hdf5 method. """ filepath = os.path.join(self.test_data_path, "test_project_read_hdf5.hdf5") if is_bad_file(filepath): pytest.skip("File not found: {}".format(filepath)) with h5py.File(filepath, "r") as hdf5_file: self.project.read_hdf5(hdf5_file) author_ref = "Hendrix Demers" self.assertEqual(author_ref, self.project.author) self.assertEqual(3, len(self.project.spectra)) self.assertEqual(3, len(self.project.si_points)) self.assertEqual(2, len(self.project.si_line_scans)) self.assertEqual(1, len(self.project.si_maps)) self.assertEqual(1, len(self.project.energy_filtered_micrographs))
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.")
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_bad_file_git_lfs(self): self.assertEqual(True, is_bad_file(self.git_lfs_file))
def test_is_bad_file(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_bad_file(file_path))
def test_import_data_read_hdf5(self): """ Test read_hdf5 method. """ filepath = self.elv_file_path name = os.path.splitext(os.path.basename(filepath))[0] spectrum_read = RawSpectrum(name) filepath = os.path.join( self.test_data_path, "test_raw_spectrum_import_data_read_hdf5.hdf5") if is_bad_file(filepath): pytest.skip("File not found: {}".format(filepath)) with h5py.File(filepath, "r") as hdf5_file: spectrum_read.read_hdf5(hdf5_file) self.assertEqual(self.name_import_ref, spectrum_read.name) self.assertTrue(HDF5_ATTRIBUTE_ACCELERATING_VOLTAGE_V in spectrum_read.extra_parameters) self.assertAlmostEqual( self. import_sem_parameters[HDF5_ATTRIBUTE_ACCELERATING_VOLTAGE_V], spectrum_read. extra_parameters[HDF5_ATTRIBUTE_ACCELERATING_VOLTAGE_V]) self.assertEqual(-32.00, spectrum_read.energies_eV[0]) self.assertEqual(21.84, spectrum_read.energies_eV[-1]) self.assertEqual(1024, len(spectrum_read.energies_eV)) self.assertEqual(2282, spectrum_read.raw_counts[0]) self.assertEqual(0, spectrum_read.raw_counts[-1]) self.assertEqual(1024, len(spectrum_read.raw_counts)) self.assertEqual(0.918375, spectrum_read.gain_corrections[0]) self.assertEqual(0.000000, spectrum_read.gain_corrections[-1]) self.assertEqual(1024, len(spectrum_read.gain_corrections)) self.assertEqual(2313, spectrum_read.dark_currents[0]) self.assertEqual(0, spectrum_read.dark_currents[-1]) self.assertEqual(1024, len(spectrum_read.dark_currents)) self.assertEqual(-33.755274261603375, spectrum_read.counts[0]) self.assertTrue(np.isnan(spectrum_read.counts)[-1]) self.assertEqual(1024, len(spectrum_read.counts)) parameters = spectrum_read.eels_parameters self.assertEqual("SU-EELS", parameters[HDF5_ATTRIBUTE_MODEL]) self.assertEqual(0.0, parameters[HDF5_ATTRIBUTE_SAMPLE_HEIGHT_mm]) self.assertEqual(r"D:\2017\System_baseline_march2017\30kV_7eV.elv", parameters[HDF5_ATTRIBUTE_FILEPATH]) self.assertEqual("", parameters[HDF5_ATTRIBUTE_COMMENT]) self.assertEqual("01/Mar/2017", parameters[HDF5_ATTRIBUTE_DATE]) self.assertEqual("10:59", parameters[HDF5_ATTRIBUTE_TIME]) self.assertEqual(30000, parameters[HDF5_ATTRIBUTE_ACCELERATING_VOLTAGE_V]) self.assertEqual(7.0, parameters[HDF5_ATTRIBUTE_ENERGY_WIDTH_eV]) self.assertEqual(0.0, parameters[HDF5_ATTRIBUTE_ENERGY_LOSS_eV]) self.assertEqual(500, parameters[HDF5_ATTRIBUTE_SPEED_us]) self.assertEqual("01/Mar/2017", parameters[HDF5_DATE]) self.assertEqual("10:59", parameters[HDF5_TIME]) self.assertEqual("", parameters[HDF5_COMMENT]) self.assertEqual(500.0, parameters[HDF5_ACQUISITION_SPEED]) self.assertEqual(0.0, parameters[HDF5_ENERGY_LOSS]) self.assertEqual(98.7, parameters[HDF5_RAW]) self.assertEqual(7.0, parameters[HDF5_ENERGY_WIDTH_eV]) self.assertEqual(586, parameters[HDF5_DUAL_DET_POSITION]) self.assertEqual(133, parameters[HDF5_DUAL_DET_POST]) self.assertEqual(608, parameters[HDF5_DUAL_DET_CENTER]) self.assertEqual(13575, parameters[HDF5_Q1]) self.assertEqual(3850, parameters[HDF5_Q1S]) self.assertEqual(0, parameters[HDF5_Q2]) self.assertEqual(0, parameters[HDF5_Q2S]) self.assertEqual(2700, parameters[HDF5_Q3]) self.assertEqual(2900, parameters[HDF5_H1]) self.assertEqual(6150, parameters[HDF5_H1S]) self.assertEqual(-600, parameters[HDF5_H2]) self.assertEqual(350, parameters[HDF5_H2S]) self.assertEqual(0, parameters[HDF5_H4]) self.assertEqual(0, parameters[HDF5_ELV_X]) self.assertEqual(0, parameters[HDF5_ELV_Y]) self.assertEqual(259, parameters[HDF5_SPECTRUM_ALIGNMENT_X]) self.assertEqual(0, parameters[HDF5_SPECTRUM_ALIGNMENT_Y]) self.assertEqual(-1500, parameters[HDF5_DET_SPEC_ALIGNMENT_X]) self.assertEqual(470, parameters[HDF5_DET_SPEC_ALIGNMENT_Y]) self.assertEqual(-1500, parameters[HDF5_DET_MAP_ALIGNMENT_X]) self.assertEqual(1500, parameters[HDF5_DET_MAP_ALIGNMENT_Y]) self.assertEqual(37443, parameters[HDF5_MAGNIFICATION])