def setUp(self): pl = components1d.PowerLaw() pl.A.value = 1e10 pl.r.value = 3 self.signal = signals.Signal1D( pl.function(np.arange(100, 200))) self.signal.axes_manager[0].offset = 100 self.signal.metadata.Signal.binned = False
def setup_method(self, method): pl = components1d.PowerLaw() pl.A.value = 1e10 pl.r.value = 3 self.signal = hs.signals.Signal1D(pl.function(np.arange(100, 200))) self.signal.axes_manager[0].offset = 100 self.signal.metadata.Signal.binned = False self.signal_noisy = self.signal.deepcopy() self.signal_noisy.add_gaussian_noise(1) self.atol = 0.04 * abs(self.signal.data).max() self.atol_zero_fill = 0.04 * abs(self.signal.isig[10:].data).max()
def set_background_estimator(self): if self.background_type == 'Power Law': self.background_estimator = components1d.PowerLaw() self.bg_line_range = 'from_left_range' elif self.background_type == 'Gaussian': self.background_estimator = components1d.Gaussian() self.bg_line_range = 'full' elif self.background_type == 'Offset': self.background_estimator = components1d.Offset() self.bg_line_range = 'full' elif self.background_type == 'Polynomial': self.background_estimator = components1d.Polynomial( self.polynomial_order) self.bg_line_range = 'full'
def test_plot_BackgroundRemoval(): pl = components1d.PowerLaw() pl.A.value = 1e10 pl.r.value = 3 s = signals.Signal1D(pl.function(np.arange(100, 200))) s.axes_manager[0].offset = 100 br = BackgroundRemoval(s, background_type='Power Law', polynomial_order=2, fast=True, plot_remainder=True) br.span_selector.set_initial((105, 115)) br.span_selector.onmove_callback() return br.signal._plot.signal_plot.figure
def test_plot_BackgroundRemoval(): pl = components1d.PowerLaw() pl.A.value = 1e10 pl.r.value = 3 s = signals.Signal1D(pl.function(np.arange(100, 200))) s.axes_manager[0].offset = 100 s.add_poissonian_noise(random_state=1) br = BackgroundRemoval(s, background_type='Power Law', polynomial_order=2, fast=False, plot_remainder=True) br.span_selector.extents = (105, 150) # will draw the line br.span_selector_changed() # will update the right axis br.span_selector_changed() return br.signal._plot.signal_plot.figure
def test_plot_BackgroundRemoval_change_background(): pl = components1d.PowerLaw() pl.A.value = 1e10 pl.r.value = 3 s = signals.Signal1D(pl.function(np.arange(100, 200))) s.axes_manager[0].offset = 100 s.add_gaussian_noise(100) br = BackgroundRemoval(s, background_type='Power Law', polynomial_order=2, fast=False, plot_remainder=True) br.span_selector.extents = (105, 150) # will draw the line br.span_selector_changed() # will update the right axis br.span_selector_changed() assert isinstance(br.background_estimator, components1d.PowerLaw) br.background_type = 'Polynomial' assert isinstance(br.background_estimator, type(components1d.Polynomial(legacy=False)))
def remove_background(self, signal_range='interactive', background_type='PowerLaw', polynomial_order=2, fast=True, show_progressbar=None): """Remove the background, either in place using a gui or returned as a new spectrum using the command line. Parameters ---------- signal_range : tuple, optional If this argument is not specified, the signal range has to be selected using a GUI. And the original spectrum will be replaced. If tuple is given, the a spectrum will be returned. background_type : string The type of component which should be used to fit the background. Possible components: PowerLaw, Gaussian, Offset, Polynomial If Polynomial is used, the polynomial order can be specified polynomial_order : int, default 2 Specify the polynomial order if a Polynomial background is used. fast : bool If True, perform an approximative estimation of the parameters. If False, the signal is fitted using non-linear least squares afterwards.This is slower compared to the estimation but possibly more accurate. show_progressbar : None or bool If True, display a progress bar. If None the default is set in `preferences`. Examples -------- Using gui, replaces spectrum s >>>> s.remove_background() Using command line, returns a spectrum >>>> s = s.remove_background(signal_range=(400,450), background_type='PowerLaw') Using a full model to fit the background >>>> s = s.remove_background(signal_range=(400,450), fast=False) Raises ------ SignalDimensionError if the signal dimension is not 1. """ self._check_signal_dimension_equals_one() if signal_range == 'interactive': br = BackgroundRemoval(self) br.edit_traits() else: if background_type == 'PowerLaw': background_estimator = components1d.PowerLaw() elif background_type == 'Gaussian': background_estimator = components1d.Gaussian() elif background_type == 'Offset': background_estimator = components1d.Offset() elif background_type == 'Polynomial': background_estimator = components1d.Polynomial( polynomial_order) else: raise ValueError("Background type: " + background_type + " not recognized") spectra = self._remove_background_cli( signal_range=signal_range, background_estimator=background_estimator, fast=fast, show_progressbar=show_progressbar) return spectra
def get_core_loss_eels_signal(add_powerlaw=False, add_noise=True, random_state=None): """Get an artificial core loss electron energy loss spectrum. Similar to a Mn-L32 edge from a perovskite oxide. Some random noise is also added to the spectrum, to simulate experimental noise. Parameters ---------- %s %s %s Example ------- >>> import hs.datasets.artifical_data as ad >>> s = ad.get_core_loss_eels_signal() >>> s.plot() With the powerlaw background >>> s = ad.get_core_loss_eels_signal(add_powerlaw=True) >>> s.plot() To make the noise the same for multiple spectra, which can be useful for testing fitting routines >>> s1 = ad.get_core_loss_eels_signal(random_state=10) >>> s2 = ad.get_core_loss_eels_signal(random_state=10) >>> (s1.data == s2.data).all() True See also -------- get_core_loss_eels_line_scan_signal, get_low_loss_eels_line_scan_signal, get_core_loss_eels_model """ from hyperspy.signals import EELSSpectrum from hyperspy import components1d random_state = check_random_state(random_state) x = np.arange(400, 800, 1) arctan = components1d.EELSArctan(A=1, k=0.2, x0=688) mn_l3_g = components1d.Gaussian(A=100, centre=695, sigma=4) mn_l2_g = components1d.Gaussian(A=20, centre=720, sigma=4) data = arctan.function(x) data += mn_l3_g.function(x) data += mn_l2_g.function(x) if add_noise: data += random_state.uniform(size=len(x)) * 0.7 if add_powerlaw: powerlaw = components1d.PowerLaw(A=10e8, r=3, origin=0) data += powerlaw.function(x) s = EELSSpectrum(data) s.axes_manager[0].offset = x[0] s.metadata.General.title = 'Artifical core loss EEL spectrum' s.axes_manager[0].name = 'Electron energy loss' s.axes_manager[0].units = 'eV' s.set_microscope_parameters(beam_energy=200, convergence_angle=26, collection_angle=20) return s
def get_core_loss_eels_line_scan_signal(add_powerlaw=False, add_noise=True, random_state=None): """Get an artificial core loss electron energy loss line scan spectrum. Similar to a Mn-L32 and Fe-L32 edge from a perovskite oxide. Parameters ---------- %s %s %s Example ------- >>> s = hs.datasets.artificial_data.get_core_loss_eels_line_scan_signal() >>> s.plot() See also -------- get_low_loss_eels_line_scan_signal, get_core_loss_eels_model """ from hyperspy.signals import EELSSpectrum from hyperspy import components1d random_state = check_random_state(random_state) x = np.arange(400, 800, 1) arctan_mn = components1d.EELSArctan(A=1, k=0.2, x0=688) arctan_fe = components1d.EELSArctan(A=1, k=0.2, x0=612) mn_l3_g = components1d.Gaussian(A=100, centre=695, sigma=4) mn_l2_g = components1d.Gaussian(A=20, centre=720, sigma=4) fe_l3_g = components1d.Gaussian(A=100, centre=605, sigma=4) fe_l2_g = components1d.Gaussian(A=10, centre=630, sigma=3) mn_intensity = [1, 1, 1, 1, 1, 1, 0.8, 0.5, 0.2, 0, 0, 0] fe_intensity = [0, 0, 0, 0, 0, 0, 0.2, 0.5, 0.8, 1, 1, 1] data = np.zeros((len(mn_intensity), len(x))) for i in range(len(mn_intensity)): data[i] += arctan_mn.function(x) * mn_intensity[i] data[i] += mn_l3_g.function(x) * mn_intensity[i] data[i] += mn_l2_g.function(x) * mn_intensity[i] data[i] += arctan_fe.function(x) * fe_intensity[i] data[i] += fe_l3_g.function(x) * fe_intensity[i] data[i] += fe_l2_g.function(x) * fe_intensity[i] if add_noise: data[i] += random_state.uniform(size=len(x)) * 0.7 if add_powerlaw: powerlaw = components1d.PowerLaw(A=10e8, r=3, origin=0) data += powerlaw.function_nd(np.stack([x] * len(mn_intensity))) if add_powerlaw: powerlaw = components1d.PowerLaw(A=10e8, r=3, origin=0) data += powerlaw.function(x) s = EELSSpectrum(data) s.axes_manager.signal_axes[0].offset = x[0] s.metadata.General.title = 'Artifical core loss EEL spectrum' s.axes_manager.signal_axes[0].name = 'Electron energy loss' s.axes_manager.signal_axes[0].units = 'eV' s.axes_manager.navigation_axes[0].name = 'Probe position' s.axes_manager.navigation_axes[0].units = 'nm' s.set_microscope_parameters(beam_energy=200, convergence_angle=26, collection_angle=20) return s
def get_eels_spectrum_map(add_noise=True): """Get an artificial atomic resolution EELS map of LaMnO3 Containing the Mn-L23 and La-M54 edges. Parameters ---------- add_noise : bool If True, will add Gaussian noise to the spectra. Default True. Returns ------- eels_map : HyperSpy EELSSpectrum Example ------- >>> import atomap.api as am >>> s_eels_map = am.dummy_data.get_eels_spectrum_map() >>> s_eels_map.plot() Not adding noise >>> s_eels_map = am.dummy_data.get_eels_spectrum_map(add_noise=False) See also -------- get_eels_spectrum_survey_image : signal with same spatial dimensions """ x_size, y_size = 100, 100 e0, e1 = 590, 900 la_spatial = _make_eels_map_spatial_image_la(x_size=x_size, y_size=y_size) mn_spatial = _make_eels_map_spatial_image_mn(x_size=x_size, y_size=y_size) # Generate EELS spectra mn_data = _make_mn_eels_spectrum(energy_range=(e0, e1)) la_data = _make_la_eels_spectrum(energy_range=(e0, e1)) # Generate 3D-data # La data_3d_la = np.zeros(shape=(x_size, y_size, (e1 - e0))) data_3d_la[:, :] = la_data temp_3d_la = np.zeros(shape=(x_size, y_size, (e1 - e0))) temp_3d_la = temp_3d_la.swapaxes(0, 2) temp_3d_la[:] += la_spatial.data temp_3d_la = temp_3d_la.swapaxes(0, 2) data_3d_la *= temp_3d_la # Mn data_3d_mn = np.zeros(shape=(x_size, y_size, (e1 - e0))) data_3d_mn[:, :] = mn_data temp_3d_mn = np.zeros(shape=(x_size, y_size, (e1 - e0))) temp_3d_mn = temp_3d_mn.swapaxes(0, 2) temp_3d_mn[:] += mn_spatial.data temp_3d_mn = temp_3d_mn.swapaxes(0, 2) data_3d_mn *= temp_3d_mn data_3d = data_3d_mn + data_3d_la # Adding background and add noise background = components1d.PowerLaw(A=1e10, r=3, origin=0) background_data = background.function(np.arange(e0, e1, 1)) temp_background_data = np.zeros(shape=(x_size, y_size, (e1 - e0))) temp_background_data[:, :] += background_data data_3d += background_data if add_noise: data_noise = np.random.random((x_size, y_size, (e1 - e0))) * 0.7 data_3d += data_noise s_3d = EELSSpectrum(data_3d) return s_3d
def get_core_loss_eels_signal(add_powerlaw=False): """Get an artificial core loss electron energy loss spectrum. Similar to a Mn-L32 edge from a perovskite oxide. Some random noise is also added to the spectrum, to simulate experimental noise. Parameters ---------- add_powerlaw : bool If True, adds a powerlaw background to the spectrum. Default False. Returns ------- artificial_core_loss_signal : HyperSpy EELSSpectrum Example ------- >>> import hs.datasets.artifical_data as ad >>> s = ad.get_core_loss_eels_signal() >>> s.plot() With the powerlaw background >>> s = ad.get_core_loss_eels_signal(add_powerlaw=True) >>> s.plot() To make the noise the same for multiple spectra, which can be useful for testing fitting routines >>> np.random.seed(seed=10) >>> s1 = ad.get_core_loss_eels_signal() >>> np.random.seed(seed=10) >>> s2 = ad.get_core_loss_eels_signal() >>> (s1.data == s2.data).all() True See also -------- get_low_loss_eels_model : get a low loss signal get_core_loss_eels_model : get a model instead of a signal get_low_loss_eels_line_scan_signal : get EELS low loss line scan get_core_loss_eels_line_scan_signal : get EELS core loss line scan """ x = np.arange(400, 800, 1) arctan = components1d.Arctan(A=1, k=0.2, x0=688) arctan.minimum_at_zero = True mn_l3_g = components1d.Gaussian(A=100, centre=695, sigma=4) mn_l2_g = components1d.Gaussian(A=20, centre=720, sigma=4) data = arctan.function(x) data += mn_l3_g.function(x) data += mn_l2_g.function(x) data += np.random.random(size=len(x)) * 0.7 if add_powerlaw: powerlaw = components1d.PowerLaw(A=10e8, r=3, origin=0) data += powerlaw.function(x) s = EELSSpectrum(data) s.axes_manager[0].offset = x[0] s.metadata.General.title = 'Artifical core loss EEL spectrum' s.axes_manager[0].name = 'Electron energy loss' s.axes_manager[0].units = 'eV' s.set_microscope_parameters(beam_energy=200, convergence_angle=26, collection_angle=20) return s