def test_assert_warns_type(): with all_warnings(): warnings.simplefilter("error") with assert_warns(category=UserWarning): warnsA() warnsC() with assert_warns(category=DeprecationWarning): warnsB()
def test_assert_warns_full_message(): with all_warnings(): warnings.simplefilter("error") with assert_warns(message="Warning A!"): warnsA() with assert_warns(message="Warning B!"): warnsB() with assert_warns(message="Warning C!"): warnsC() with assert_warns(message=["Warning A!", "Warning B!", "Warning C!"]): warnsA() warnsB() warnsC()
def test_convert_to_units_unsupported_units(self, same_units): with assert_warns(message="not supported for conversion.", category=UserWarning): self.am.convert_units('navigation', units='toto', same_units=same_units) assert_deep_almost_equal(self.am._get_axes_dicts(), self.axes_list)
def test_min_not_in_window(self): # If I use a much lower window, this is the value that has to be # returned as threshold. s = self.spectrum with assert_warns("No inflexion point could be found in some " "positions that have been marked with nans."): data = s.estimate_elastic_scattering_threshold(window=1.5, tol=0.001).data nt.assert_true(np.all(np.isnan(data)))
def test(self): s = self.signal sAl = s.get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] assert sAl.axes_manager.signal_dimension == 0 np.testing.assert_allclose(24.99516, sAl.data[0, 0, 0], atol=1e-3) sAl = s.inav[0].get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] np.testing.assert_allclose(24.99516, sAl.data[0, 0], atol=1e-3) sAl = s.inav[0, 0].get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] np.testing.assert_allclose(24.99516, sAl.data[0], atol=1e-3) sAl = s.inav[0, 0, 0].get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] np.testing.assert_allclose(24.99516, sAl.data, atol=1e-3) s.axes_manager[-1].offset = 1.0 with assert_warns(message="C_Ka is not in the data energy range."): sC = s.get_lines_intensity(["C_Ka"], plot_result=False) assert len(sC) == 0 assert sAl.metadata.Sample.elements == ["Al"] assert sAl.metadata.Sample.xray_lines == ["Al_Ka"]
def test_converted_compact_scale_units(self): self.uc.units = 'micron' with assert_warns(message="not supported for conversion.", category=UserWarning): self.uc._convert_compact_units() assert self.uc.units == 'micron' nt.assert_almost_equal(self.uc.scale, 1.0E-3)
def test_assert_warns_partial_message(): with all_warnings(): warnings.simplefilter("error") with assert_warns(message="Warning"): warnsA() warnsB() warnsC()
def test_ignore_conversion(self): assert self.uc._ignore_conversion(t.Undefined) with assert_warns( message="not supported for conversion.", category=UserWarning): assert self.uc._ignore_conversion('unit_not_supported') assert not self.uc._ignore_conversion('m')
def test_assert_warns_regex_message(): with all_warnings(): warnings.simplefilter("error") with assert_warns(message="Warning .?!"): warnsA() warnsB() warnsC()
def test_units_not_supported_by_pint_warning_raised2(self): # raising a warning, not converting scale self.axis.units = 'µm' with assert_warns(message="not supported for conversion.", category=UserWarning): self.axis.convert_to_units('toto') nt.assert_almost_equal(self.axis.scale, 12E-12) assert self.axis.units == 'µm'
def test_converted_compact_scale_units(self): self.uc.units = 'micron' with assert_warns( message="not supported for conversion.", category=UserWarning): self.uc._convert_compact_units() assert self.uc.units == 'micron' nt.assert_almost_equal(self.uc.scale, 1.0E-3)
def test_convert_to_units_unsupported_units(self, same_units): with assert_warns( message="not supported for conversion.", category=UserWarning): self.am.convert_units('navigation', units='toto', same_units=same_units) assert_deep_almost_equal(self.am._get_axes_dicts(), self.axes_list)
def test_units_not_supported_by_pint_warning_raised2(self): # raising a warning, not converting scale self.axis.units = 'µm' with assert_warns( message="not supported for conversion.", category=UserWarning): self.axis.convert_to_units('toto') nt.assert_almost_equal(self.axis.scale, 12E-12) assert self.axis.units == 'µm'
def test_min_not_in_window(self): # If I use a much lower window, this is the value that has to be # returned as threshold. s = self.signal with assert_warns("No inflexion point could be found in some " "positions that have been marked with nans."): data = s.estimate_elastic_scattering_threshold(window=1.5, tol=0.001).data nt.assert_true(np.all(np.isnan(data)))
def test_assert_warns_type_fails(): with all_warnings(): warnings.simplefilter("error") try: with assert_warns(category=UserWarning): warnsB() except ValueError: pass else: raise ValueError("Expected warning to give error!")
def test_assert_warns_message_fails(): with all_warnings(): warnings.simplefilter("error") try: with assert_warns(message="Warning [AB]!"): warnsC() except ValueError: pass else: raise AssertionError("ValueError expected!") with all_warnings(): warnings.simplefilter("error") try: with assert_warns(message="Warning A! Too much"): warnsA() except ValueError: pass else: raise ValueError("ValueError expected!")
def test_calibrate_energy_resolution(self): s = self.s m = s.create_model() m.fit() m.fit_background() reso = (s.metadata.Acquisition_instrument.TEM.Detector.EDS.energy_resolution_MnKa,) s.set_microscope_parameters(energy_resolution_MnKa=150) with assert_warns(message=r"Energy resolution \(FWHM at Mn Ka\) " "changed from"): m.calibrate_energy_axis(calibrate="resolution") nt.assert_true( np.allclose(s.metadata.Acquisition_instrument.TEM.Detector.EDS.energy_resolution_MnKa, reso, atol=1) )
def test_calibrate_energy_resolution(self): s = self.s m = s.create_model() m.fit() m.fit_background() reso = s.metadata.Acquisition_instrument.TEM.Detector.EDS.\ energy_resolution_MnKa, s.set_microscope_parameters(energy_resolution_MnKa=150) with assert_warns(message=r"Energy resolution \(FWHM at Mn Ka\) " "changed from"): m.calibrate_energy_axis(calibrate='resolution') nt.assert_true(np.allclose( s.metadata.Acquisition_instrument.TEM.Detector.EDS. energy_resolution_MnKa, reso, atol=1))
def test_calibrate_xray_weight(self): s = self.s s1 = utils_eds.xray_lines_model( elements=["Co"], weight_percents=[50], energy_axis={"units": "keV", "size": 400, "scale": 0.01, "name": "E", "offset": 4.9}, ) s = s + s1 / 50 m = s.create_model() m.fit() with assert_warns(message="The X-ray line expected to be in the model " "was not found"): m.calibrate_xray_lines(calibrate="sub_weight", xray_lines=["Fe_Ka"], bound=100) nt.assert_true(np.allclose(0.0347, m["Fe_Kb"].A.value, atol=1e-3))
def test(self): s = self.signal sAl = s.get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] np.testing.assert_allclose(24.99516, sAl.data[0, 0, 0], atol=1e-3) sAl = s.inav[0].get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] np.testing.assert_allclose(24.99516, sAl.data[0, 0], atol=1e-3) sAl = s.inav[0, 0].get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] np.testing.assert_allclose(24.99516, sAl.data[0], atol=1e-3) sAl = s.inav[0, 0, 0].get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] np.testing.assert_allclose(24.99516, sAl.data, atol=1e-3) s.axes_manager[-1].offset = 1.0 with assert_warns(message="C_Ka is not in the data energy range."): sC = s.get_lines_intensity(["C_Ka"], plot_result=False) nt.assert_equal(len(sC), 0) nt.assert_equal(sAl.metadata.Sample.elements, ["Al"]) nt.assert_equal(sAl.metadata.Sample.xray_lines, ["Al_Ka"])
def test(self): s = self.signal # get_lines_intensity() should raise TypeError when # xray_lines is a string or a dictionary for bad_iter in ["Al_Kb", {"A": "Al_Kb", "B": "Ca_Ka"}]: assert_raises(TypeError, s.get_lines_intensity, xray_lines=bad_iter, plot_result=False) # get_lines_intensity() should succeed and return a list # when xray_lines is an iterable (other than a str or dict) good_iter = { "tuple": ("Al_Kb", "Ca_Ka"), "list": ["Al_Kb", "Ca_Ka"], "set": set(["Al_Kb", "Ca_Ka"]) } for itr in good_iter: assert isinstance(s.get_lines_intensity( xray_lines=good_iter[itr], plot_result=False), list), \ "get_lines_intensity() trouble with {}".format( itr ) sAl = s.get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] assert sAl.axes_manager.signal_dimension == 0 np.testing.assert_allclose(24.99516, sAl.data[0, 0, 0], atol=1e-3) sAl = s.inav[0].get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] np.testing.assert_allclose(24.99516, sAl.data[0, 0], atol=1e-3) sAl = s.inav[0, 0].get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] np.testing.assert_allclose(24.99516, sAl.data[0], atol=1e-3) sAl = s.inav[0, 0, 0].get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] np.testing.assert_allclose(24.99516, sAl.data, atol=1e-3) s.axes_manager[-1].offset = 1.0 with assert_warns(message="C_Ka is not in the data energy range."): sC = s.get_lines_intensity(["C_Ka"], plot_result=False) assert len(sC) == 0 assert sAl.metadata.Sample.elements == ["Al"] assert sAl.metadata.Sample.xray_lines == ["Al_Ka"]
def test_calibrate_xray_weight(self): s = self.s s1 = utils_eds.xray_lines_model( elements=['Co'], weight_percents=[50], energy_axis={'units': 'keV', 'size': 400, 'scale': 0.01, 'name': 'E', 'offset': 4.9}) s = (s + s1 / 50) m = s.create_model() m.fit() with assert_warns(message='The X-ray line expected to be in the model ' 'was not found'): m.calibrate_xray_lines(calibrate='sub_weight', xray_lines=['Fe_Ka'], bound=100) np.testing.assert_allclose(0.0347, m['Fe_Kb'].A.value, atol=1e-3)
def test_guess_units_from_mode(self): from hyperspy.io_plugins.fei import _guess_units_from_mode, \ convert_xml_to_dict, get_xml_info_from_emi fname0_emi = os.path.join( self.dirpathold, '64x64_TEM_images_acquire.emi') fname0_ser = os.path.join( self.dirpathold, '64x64_TEM_images_acquire_1.ser') objects = get_xml_info_from_emi(fname0_emi) header0, data0 = load_ser_file(fname0_ser) objects_dict = convert_xml_to_dict(objects[0]) unit = _guess_units_from_mode(objects_dict, header0) nt.assert_equal(unit, 'meters') # objects is empty dictionary with assert_warns( message="The navigation axes units could not be determined.", category=UserWarning): unit = _guess_units_from_mode({}, header0) nt.assert_equal(unit, 'meters')
def test_guess_units_from_mode(self): from hyperspy.io_plugins.fei import _guess_units_from_mode, \ convert_xml_to_dict, get_xml_info_from_emi fname0_emi = os.path.join(self.dirpathold, '64x64_TEM_images_acquire.emi') fname0_ser = os.path.join(self.dirpathold, '64x64_TEM_images_acquire_1.ser') objects = get_xml_info_from_emi(fname0_emi) header0, data0 = load_ser_file(fname0_ser) objects_dict = convert_xml_to_dict(objects[0]) unit = _guess_units_from_mode(objects_dict, header0) assert unit == 'meters' # objects is empty dictionary with assert_warns( message="The navigation axes units could not be determined.", category=UserWarning): unit = _guess_units_from_mode({}, header0) assert unit == 'meters'
def test(self): s = self.signal sAl = s.get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] nt.assert_true(np.allclose(24.99516, sAl.data[0, 0, 0], atol=1e-3)) sAl = s.inav[0].get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] nt.assert_true(np.allclose(24.99516, sAl.data[0, 0], atol=1e-3)) sAl = s.inav[0, 0].get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] nt.assert_true(np.allclose(24.99516, sAl.data[0], atol=1e-3)) sAl = s.inav[0, 0, 0].get_lines_intensity(["Al_Ka"], plot_result=False, integration_windows=5)[0] nt.assert_true(np.allclose(24.99516, sAl.data, atol=1e-3)) s.axes_manager[-1].offset = 1.0 with assert_warns(message="C_Ka is not in the data energy range."): sC = s.get_lines_intensity(["C_Ka"], plot_result=False) nt.assert_equal(len(sC), 0) nt.assert_true(sAl.metadata.Sample.elements, ["Al"]) nt.assert_true(sAl.metadata.Sample.xray_lines, ["Al_Ka"])
def test_ignore_conversion(self): assert self.uc._ignore_conversion(t.Undefined) with assert_warns(message="not supported for conversion.", category=UserWarning): assert self.uc._ignore_conversion('unit_not_supported') assert not self.uc._ignore_conversion('m')