def setup_method(self, method): self.s1 = signals.Signal1D(np.ones((2, 3))) self.s2 = signals.Signal1D(np.ones((2, 3))) self.s2.data *= 2
def setup_method(self, method): self.s = signals.Signal1D([[1.1, 1.2, 1.4, 1.3], [1.5, 1.5, 1.4, 1.2]])
def test_decomposition_reproject_warning(reproject): s = signals.Signal1D(generate_low_rank_matrix()) with pytest.warns(UserWarning, match="Reprojecting the signal is not yet supported"): s.decomposition(algorithm="NMF", reproject=reproject)
def setup_method(self, method): self.signal = signals.Signal1D(np.arange(10)) self.data = self.signal.data.copy()
def setup_method(self, method): self.s = signals.Signal1D(generate_low_rank_matrix())
def setup_method(self, method): s = signals.Signal1D(np.ones((5, 4, 3, 6))) for axis, name in zip(s.axes_manager._get_axes_in_natural_order(), ['x', 'y', 'z', 'E']): axis.name = name self.s = s
def test_write_unsupported_data_type(): data = np.arange(5 * 10 * 15).reshape((5, 10, 15)).astype(np.float16) s = signals.Signal1D(data) with pytest.raises(IOError): s.save('test_write_unsupported_data_type.rpl')
def test_rebin_dtype(self): s = signals.Signal1D(np.arange(1000).reshape(10, 10, 10)) s.change_dtype(np.uint8) s2 = s.rebin(scale=(3, 3, 1), crop=False) assert s.sum() == s2.sum()
def setUp(self): self.signal = signals.Signal1D(np.arange(24).reshape((2, 3, 4))) self.data = self.signal.data.copy()
def setup_method(self, method): self.s = signals.Signal1D(np.random.random((20, 100)))
def setup_method(self, method): self.s_int = signals.Signal1D( (np.random.random((20, 100)) * 20).astype('int')) self.s_float = signals.Signal1D(np.random.random((20, 100)))
def test_is_binned(): s = signals.Signal1D(np.zeros((5, 5))) assert is_binned(s) == s.axes_manager[-1].is_binned with pytest.warns(VisibleDeprecationWarning, match="Use of the `binned`"): s.metadata.set_item('Signal.binned', True) assert is_binned(s) == s.metadata.Signal.binned
def test_is_hyperspy_signal(): s = signals.Signal1D(np.zeros((5, 5, 5))) p = object() assert is_hyperspy_signal(s) is True assert is_hyperspy_signal(p) is False
def test_sum_wrong_shape(self): s1 = self.s1 s2 = signals.Signal1D(np.ones((3, 3))) with pytest.raises(ValueError): s1 + s2
def setup_method(self, method): s = signals.Signal1D(np.random.random((10, 20, 1))) s.axes_manager[0].scale = 0.5 s.axes_manager[1].scale = 2 self.s = s
def setUp(self): self.signal = signals.Signal1D(np.arange(10)) self.data = self.signal.data.copy()
def setup_method(self, method): self.signal = signals.Signal1D(np.arange(5 * 10).reshape(5, 10)) self.signal.axes_manager[0].name = "x" self.signal.axes_manager[1].name = "E" self.signal.axes_manager[0].scale = 0.5 self.data = self.signal.data.copy()
def setup_method(self, method): rng = np.random.RandomState(123) self.s = signals.Signal1D(rng.random((20, 100)))
def test_write_unsupported_data_shape(): data = np.arange(5 * 10 * 15 * 20).reshape((5, 10, 15, 20)) s = signals.Signal1D(data) with pytest.raises(TypeError): s.save('test_write_unsupported_data_shape.rpl')
def setup_method(self, method): rng = np.random.RandomState(123) self.s_int = signals.Signal1D((rng.random( (20, 100)) * 20).astype("int")) self.s_float = signals.Signal1D(rng.random((20, 100)))
def setup_method(self, method): self.signal = signals.Signal1D(np.arange(24).reshape((2, 3, 4))) self.data = self.signal.data.copy()
def test_decomposition_reproject(reproject): rng = np.random.RandomState(123) s = signals.Signal1D(rng.random((20, 100))) s.decomposition(reproject=reproject)
def get_luminescence_signal(navigation_dimension=0, uniform=False, add_baseline=False, add_noise=True, random_state=None): """Get an artificial luminescence signal in wavelength scale (nm, uniform) or energy scale (eV, non-uniform), simulating luminescence data recorded with a diffracting spectrometer. Some random noise is also added to the spectrum, to simulate experimental noise. Parameters ---------- navigation_dimension: positive int. The navigation dimension(s) of the signal. 0 = single spectrum, 1 = linescan, 2 = spectral map etc... uniform: bool. return uniform (wavelength) or non-uniform (energy) spectrum add_baseline : bool If true, adds a constant baseline to the spectrum. Conversion to energy representation will turn the constant baseline into inverse powerlaw. %s Example ------- >>> import hyperspy.datasets.artificial_data as ad >>> s = ad.get_luminescence_signal() >>> s.plot() With constant baseline >>> s = ad.get_luminescence_signal(uniform=True, add_baseline=True) >>> s.plot() To make the noise the same for multiple spectra, which can be useful for testing fitting routines >>> s1 = ad.get_luminescence_signal(random_state=10) >>> s2 = ad.get_luminescence_signal(random_state=10) >>> (s1.data == s2.data).all() True 2D map >>> s = ad.get_luminescence_signal(navigation_dimension=2) >>> s.plot() See also -------- get_low_loss_eels_signal, get_core_loss_eels_signal, get_low_loss_eels_line_scan_signal, get_core_loss_eels_line_scan_signal, get_core_loss_eels_model, get_atomic_resolution_tem_signal2d, """ #Initialisation of random number generator random_state = check_random_state(random_state) #Creating a uniform data axis, roughly similar to Horiba iHR320 with a 150 mm-1 grating nm_axis = UniformDataAxis( index_in_array=None, name="Wavelength", units="nm", navigate=False, size=1024, scale=0.54, offset=222.495, is_binned=False, ) #Artificial luminescence peak gaussian_peak = components1d.Gaussian(A=5000, centre=375, sigma=25) if navigation_dimension >= 0: #Generate empty data (ones) data = np.ones([10 for i in range(navigation_dimension)] + [nm_axis.size]) #Generate spatial axes spaxes = [ UniformDataAxis( index_in_array=None, name="X{:d}".format(i), units="um", navigate=False, size=10, scale=2.1, offset=0, is_binned=False, ) for i in range(navigation_dimension) ] #Generate empty signal sig = signals.Signal1D(data, axes=spaxes + [nm_axis]) sig.metadata.General.title = '{:d}d-map Artificial Luminescence Signal'\ .format(navigation_dimension) else: raise ValueError("Value {:d} invalid as navigation dimension.".format(\ navigation_dimension)) #Populating data array, possibly with noise and baseline sig.data *= gaussian_peak.function(nm_axis.axis) if add_noise: sig.data += (random_state.uniform(size=sig.data.shape) - 0.5) * 1.4 if add_baseline: data += 350. #if not uniform, transformation into non-uniform axis if not uniform: hc = 1239.84198 #nm/eV #converting to non-uniform axis sig.axes_manager.signal_axes[0].convert_to_functional_data_axis(\ expression="a/x", name='Energy', units='eV', a=hc, ) #Reverting the orientation of signal axis to have increasing Energy sig = sig.isig[::-1] #Jacobian transformation Eax = sig.axes_manager.signal_axes[0].axis sig *= hc / Eax**2 return sig
def test_decomposition_reproject_warning(reproject): rng = np.random.RandomState(123) s = signals.Signal1D(rng.random((20, 100))) with pytest.warns(UserWarning, match="Reprojecting the signal is not yet supported"): s.decomposition(algorithm="nmf", reproject=reproject)
def setup_method(self, method): mat = generate_low_rank_matrix() self.s_int = signals.Signal1D((mat * 20).astype("int")) self.s_float = signals.Signal1D(mat)
def test_decomposition_mlpca_var_func(): rng = np.random.RandomState(123) s = signals.Signal1D(rng.random((20, 100))) s.decomposition(output_dimension=2, algorithm="mlpca", var_func=lambda x: x)
def test_decomposition_reproject(reproject): s = signals.Signal1D(generate_low_rank_matrix()) s.decomposition(reproject=reproject)
def test_negative_values_error(): x = generate_low_rank_matrix() x[0, 0] = -1.0 s = signals.Signal1D(x) with pytest.raises(ValueError, match="Negative values found in data!"): s.decomposition(normalize_poissonian_noise=True)
def test_decomposition_mlpca_var_func(): s = signals.Signal1D(generate_low_rank_matrix()) s.decomposition(output_dimension=2, algorithm="MLPCA", var_func=lambda x: x)
def setup_method(self, method): self.s1 = signals.Signal1D(np.array((1, -1, 4, -3)))