def create_ll_signal(signal_shape=1000): offset = 0 zlp_param = {'A': 10000.0, 'centre': 0.0 + offset, 'sigma': 15.0} zlp = Gaussian(**zlp_param) plasmon_param = {'A': 2000.0, 'centre': 200.0 + offset, 'sigma': 75.0} plasmon = Gaussian(**plasmon_param) axis = np.arange(signal_shape) data = zlp.function(axis) + plasmon.function(axis) ll = EELSSpectrum(data) ll.axes_manager[-1].offset = -offset ll.axes_manager[-1].scale = 0.1 return ll
def get_holz_heterostructure_test_signal(lazy=False): """Get HyperSpy 2D signal with 2D navigation dimensions for HOLZ testing. The centre, radius and intensity of the ring varies as a function of probe position. The disk centre position varies as a function of probe position. Parameters ---------- lazy : bool, default False Returns ------- holz_signal : HyperSpy 2D signal Example ------- >>> s = pxm.dummy_data.get_holz_heterostructure_test_signal() >>> s.plot() Load as lazy >>> s = pxm.dummy_data.get_holz_heterostructure_test_signal(lazy=True) """ probe_size_x, probe_size_y = 40, 40 px, py = np.mgrid[0:probe_size_x:1, 0:probe_size_y:1] x, y = np.mgrid[36:38:40j, 41:43:40j] disk_r = 10 disk_I = np.ones_like(x) * 100 + np.random.random() * 20 g_r = Gaussian(A=20, centre=25, sigma=5) ring_r = np.ones_like(x) * 30 + g_r.function(py) g_I = Gaussian(A=30, centre=25, sigma=3) ring_I = np.ones_like(x) * 20 + g_I.function(py) s = mdtd.generate_4d_data( probe_size_x=probe_size_x, probe_size_y=probe_size_y, image_size_x=80, image_size_y=80, disk_x=x, disk_y=y, disk_r=disk_r, disk_I=disk_I, ring_x=x, ring_y=y, ring_r=ring_r, ring_I=ring_I, add_noise=True, lazy=lazy, ) return s
def setup_method(self, method): s = EDSTEMSpectrum(np.ones([2, 2, 1024])) energy_axis = s.axes_manager.signal_axes[0] energy_axis.scale = 1e-2 energy_axis.units = 'keV' energy_axis.name = "Energy" s.set_microscope_parameters(beam_energy=200, live_time=2.5, tilt_stage=0.0, azimuth_angle=0, elevation_angle=35, energy_resolution_MnKa=130, beam_current=0.05) elements = ['Al', 'Zn'] xray_lines = ['Al_Ka', 'Zn_Ka'] intensities = [300, 500] for i, xray_line in enumerate(xray_lines): gauss = Gaussian() line_energy, FWHM = s._get_line_energy(xray_line, FWHM_MnKa='auto') gauss.centre.value = line_energy gauss.A.value = intensities[i] gauss.sigma.value = FWHM s.data[:] += gauss.function(energy_axis.axis) s.set_elements(elements) s.add_lines(xray_lines) s.axes_manager[0].scale = 0.5 s.axes_manager[1].scale = 0.5 s.axes_manager[0].units = 'nm' s.axes_manager[1].units = 'nm' self.signal = s
def setup_method(self, method): s = EDSTEMSpectrum(np.ones([2, 2, 1024])) energy_axis = s.axes_manager.signal_axes[0] energy_axis.scale = 1e-2 energy_axis.units = 'keV' energy_axis.name = "Energy" s.set_microscope_parameters(beam_energy=200, live_time=2.5, tilt_stage=0.0, azimuth_angle=0, elevation_angle=35, energy_resolution_MnKa=130, beam_current=0.05) elements = ['Al', 'Zn'] xray_lines = ['Al_Ka', 'Zn_Ka'] intensities = [300, 500] for i, xray_line in enumerate(xray_lines): gauss = Gaussian() line_energy, FWHM = s._get_line_energy(xray_line, FWHM_MnKa='auto') gauss.centre.value = line_energy gauss.A.value = intensities[i] gauss.sigma.value = FWHM s.data[:] += gauss.function(energy_axis.axis) s.set_elements(elements) s.add_lines(xray_lines) s.axes_manager[0].scale = 0.5 s.axes_manager[1].scale = 0.5 self.signal = s
def setup_method(self, method): gaussian = Gaussian() gaussian.A.value = 20 gaussian.sigma.value = 10 gaussian.centre.value = 50 self.signal = Signal1D(gaussian.function(np.arange(0, 100, 0.01))) self.signal.axes_manager[0].scale = 0.01
def test_function(): g = Gaussian() g.centre.value = 1 g.sigma.value = 2 / sigma2fwhm g.A.value = 3 * sqrt2pi * g.sigma.value assert_allclose(g.function(2), 1.5) assert_allclose(g.function(1), 3)
def test_estimate_parameters_binned(only_current, binned, lazy, uniform): s = Signal1D(np.empty((100, ))) s.axes_manager.signal_axes[0].is_binned = binned axis = s.axes_manager.signal_axes[0] axis.scale = 1 axis.offset = -20 g1 = Gaussian(50015.156, 10 / sigma2fwhm, 10) s.data = g1.function(axis.axis) if not uniform: axis.convert_to_non_uniform_axis() if lazy: s = s.as_lazy() g2 = Gaussian() if binned and uniform: factor = axis.scale elif binned: factor = np.gradient(axis.axis) else: factor = 1 assert g2.estimate_parameters(s, axis.low_value, axis.high_value, only_current=only_current) assert g2._axes_manager[-1].is_binned == binned np.testing.assert_allclose(g1.A.value, g2.A.value * factor) assert abs(g2.centre.value - g1.centre.value) <= 1e-3 assert abs(g2.sigma.value - g1.sigma.value) <= 0.1
def setUp(self): s = EDSTEMSpectrum(np.ones([2, 2, 1024])) energy_axis = s.axes_manager.signal_axes[0] energy_axis.scale = 1e-2 energy_axis.units = "keV" energy_axis.name = "Energy" s.set_microscope_parameters( beam_energy=200, live_time=3.1, tilt_stage=0.0, azimuth_angle=None, elevation_angle=35, energy_resolution_MnKa=130, ) s.metadata.Acquisition_instrument.TEM.Detector.EDS.real_time = 2.5 s.metadata.Acquisition_instrument.TEM.beam_current = 0.05 elements = ["Al", "Zn"] xray_lines = ["Al_Ka", "Zn_Ka"] intensities = [300, 500] for i, xray_line in enumerate(xray_lines): gauss = Gaussian() line_energy, FWHM = s._get_line_energy(xray_line, FWHM_MnKa="auto") gauss.centre.value = line_energy gauss.A.value = intensities[i] gauss.sigma.value = FWHM s.data[:] += gauss.function(energy_axis.axis) s.set_elements(elements) s.add_lines(xray_lines) s.axes_manager[0].scale = 0.5 s.axes_manager[1].scale = 0.5 self.signal = s
def test_fit_component(self): m = self.model axis = self.axis g1 = Gaussian() m.append(g1) m.fit_component(g1, signal_range=(4000, 6000)) np.testing.assert_allclose(self.g.function(axis), g1.function(axis), rtol=self.rtol, atol=10e-3)
def setup_method(self, method): g = Gaussian() g.A.value = 10000.0 g.centre.value = 5000.0 g.sigma.value = 500.0 axis = np.arange(10000) s = Signal1D(g.function(axis)) m = s.create_model() self.model = m self.g = g self.axis = axis
def setup_method(self, method): g1 = Gaussian(centre=10) g2 = Gaussian(centre=20) g3 = Gaussian(centre=30) g3.A.twin = g2.A g3.A.twin_function_expr = "-0.5*x" g2.A.twin = g1.A g2.A.twin_function_expr = "-0.5*x" g1.A.value = 20 x = np.linspace(0, 50, 1000) y = g1.function(x) + g2.function(x) + g3.function(x) s = Signal1D(y) s.axes_manager[-1].scale = x[1] - x[0] gs = [g1, g2, g3] m = s.create_model() m.extend(gs) self.s, self.m, self.gs = s, m, gs
def setup_method(self, method): g = Gaussian() g.A.value = 10000.0 g.centre.value = 5000.0 g.sigma.value = 500.0 axis = np.arange(10000) s = Signal1D(g.function(axis)) m = s.create_model() self.model = m self.g = g self.axis = axis self.rtol = 0.00
def setup_method(self, method): gs1 = Gaussian() gs1.A.value = 10000.0 gs1.centre.value = 5000.0 gs1.sigma.value = 500.0 gs2 = Gaussian() gs2.A.value = 60000.0 gs2.centre.value = 2000.0 gs2.sigma.value = 300.0 gs3 = Gaussian() gs3.A.value = 20000.0 gs3.centre.value = 6000.0 gs3.sigma.value = 100.0 axis = np.arange(10000) total_signal = (gs1.function(axis) + gs2.function(axis) + gs3.function(axis)) s = Signal1D(total_signal) m = s.create_model() g1 = Gaussian() g2 = Gaussian() g3 = Gaussian() m.append(g1) m.append(g2) m.append(g3) self.model = m self.gs1 = gs1 self.gs2 = gs2 self.gs3 = gs3 self.g1 = g1 self.g2 = g2 self.g3 = g3 self.axis = axis self.rtol = 0.01
def test_function_nd(binned): s = Signal1D(np.empty((100,))) axis = s.axes_manager.signal_axes[0] axis.scale = 1 axis.offset = -20 g1 = Gaussian(50015.156, 10/sigma2fwhm, 10) s.data = g1.function(axis.axis) s.metadata.Signal.binned = binned s2 = stack([s] * 2) g2 = Gaussian() factor = axis.scale if binned else 1 g2.estimate_parameters(s2, axis.low_value, axis.high_value, False) assert g2.binned == binned assert_allclose(g2.function_nd(axis.axis) * factor, s2.data)
def setup_method(self, method): # Create an empty spectrum s = EDSSEMSpectrum(np.zeros((2, 2, 3, 100))) energy_axis = s.axes_manager.signal_axes[0] energy_axis.scale = 0.04 energy_axis.units = 'keV' energy_axis.name = "Energy" g = Gaussian() g.sigma.value = 0.05 g.centre.value = 1.487 s.data[:] = g.function(energy_axis.axis) s.metadata.Acquisition_instrument.SEM.Detector.EDS.live_time = 3.1 s.metadata.Acquisition_instrument.SEM.beam_energy = 15.0 self.signal = s
def setUp(self): # Create an empty spectrum s = EDSSEMSpectrum(np.zeros((2, 2, 3, 100))) energy_axis = s.axes_manager.signal_axes[0] energy_axis.scale = 0.04 energy_axis.units = "keV" energy_axis.name = "Energy" g = Gaussian() g.sigma.value = 0.05 g.centre.value = 1.487 s.data[:] = g.function(energy_axis.axis) s.metadata.Acquisition_instrument.SEM.Detector.EDS.live_time = 3.1 s.metadata.Acquisition_instrument.SEM.beam_energy = 15.0 self.signal = s
def create_sum_of_gaussians(convolved=False): param1 = {'A': A_value_gaussian[0], 'centre': centre_value_gaussian[0] / scale, 'sigma': sigma_value_gaussian[0] / scale} gs1 = Gaussian(**param1) param2 = {'A': A_value_gaussian[1], 'centre': centre_value_gaussian[1] / scale, 'sigma': sigma_value_gaussian[1] / scale} gs2 = Gaussian(**param2) param3 = {'A': A_value_gaussian[2], 'centre': centre_value_gaussian[2] / scale, 'sigma': sigma_value_gaussian[2] / scale} gs3 = Gaussian(**param3) axis = np.arange(1000) data = gs1.function(axis) + gs2.function(axis) + gs3.function(axis) if convolved: to_convolved = create_ll_signal(data.shape[0]).data data = np.convolve(data, to_convolved) / sum(to_convolved) s = Signal1D(data[:1000]) s.axes_manager[-1].scale = scale return s
def test_fit_component(self, signal_range): m = self.model axis = self.axis g = self.g g1 = Gaussian() m.append(g1) cf = ComponentFit(m, g1, signal_range=signal_range) if signal_range == 'interactive': cf.ss_left_value, cf.ss_right_value = (4000, 6000) cf._fit_fired() np.testing.assert_allclose(g.function(axis), g1.function(axis), rtol=0.0, atol=10e-3)
def test_estimate_parameters_negative_scale(): s = Signal1D(np.empty((100,))) axis = s.axes_manager.signal_axes[0] axis.scale = -1 axis.offset = 100 g1 = Gaussian(50015.156, 15/sigma2fwhm, 50) s.data = g1.function(axis.axis) g2 = Gaussian() with pytest.raises(ValueError): g2.estimate_parameters(s, 40, 60) assert g2.estimate_parameters(s, 90, 10) np.testing.assert_allclose(g1.A.value, g2.A.value) assert abs(g2.centre.value - g1.centre.value) <= 1e-3 assert abs(g2.sigma.value - g1.sigma.value) <= 0.1
def test_estimate_parameters_binned(only_current, binned): s = Signal1D(np.empty((100,))) s.metadata.Signal.binned = binned axis = s.axes_manager.signal_axes[0] axis.scale = 1 axis.offset = -20 g1 = Gaussian(50015.156, 10/sigma2fwhm, 10) s.data = g1.function(axis.axis) g2 = Gaussian() factor = axis.scale if binned else 1 assert g2.estimate_parameters(s, axis.low_value, axis.high_value, only_current=only_current) assert g2.binned == binned assert_allclose(g1.A.value, g2.A.value * factor) assert abs(g2.centre.value - g1.centre.value) <= 1e-3 assert abs(g2.sigma.value - g1.sigma.value) <= 0.1
def setup_method(self, method): np.random.seed(1) axes = np.array([[100 * np.random.random() + np.arange(0., 600, 1) for i in range(3)] for j in range(4)]) g = Gaussian() g.A.value = 30000. g.centre.value = 300. g.sigma.value = 150. data = g.function(axes) s = Signal1D(data) s.axes_manager[-1].offset = -150. s.axes_manager[-1].scale = 0.5 s.add_gaussian_noise(2.0) m = s.create_model() g = Gaussian() g.A.ext_force_positive = True g.A.ext_bounded = True m.append(g) g.active_is_multidimensional = True for index in m.axes_manager: m.fit() self.model = m