def setup_method(self, method): self.s = BaseSignal(np.random.rand(1, 2, 3, 4, 5, 6)) for ax, name in zip(self.s.axes_manager._axes, "abcdef"): ax.name = name # just to make sure in case default changes self.s.axes_manager.set_signal_dimension(6) self.s.estimate_poissonian_noise_variance()
def __init__(self, *args, **kwargs): if len(args) > 0 and isinstance(args[0], BaseSignal): # Pretend it is a hs signal, copy axes and metadata sdict = args[0]._to_dictionary() self.__class__.__init__(self, **sdict) else: BaseSignal.__init__(self, *args, **kwargs)
class Test2d: def setup_method(self, method): self.s = BaseSignal(np.random.random((2, 3))) # (|3, 2) def test_as_signal2D_T(self): assert ( self.s.data.T.shape == self.s.as_signal2D((1, 0)).data.shape) def test_as_signal2D(self): assert ( self.s.data.shape == self.s.as_signal2D((0, 1)).data.shape) def test_as_signal1D_T(self): assert ( self.s.data.T.shape == self.s.as_signal1D(1).data.shape) def test_as_signal1D(self): assert ( self.s.data.shape == self.s.as_signal1D(0).data.shape) def test_s2EELS2im2s(self): s = self.s.as_signal1D(0) s.set_signal_type("EELS") im = s.as_signal2D((1, 0)) assert im.metadata.Signal.signal_type == "EELS" s = im.as_signal1D(0) assert s.metadata.Signal.signal_type == "EELS" if s._lazy: _class = signals.LazyEELSSpectrum else: _class = signals.EELSSpectrum assert isinstance(s, _class)
def test_save_3D_signal(self, tmp_path): s = BaseSignal(np.zeros((2, 3, 4))) fname = tmp_path / 'test.nxs' s.save(fname) lin = load(fname, nxdata_only=True) assert lin.axes_manager.signal_dimension == 3 np.testing.assert_array_equal(lin.data, np.zeros((2, 3, 4)))
class Test2d: def setUp(self): self.s = BaseSignal(np.random.random((2, 3))) # (|3, 2) def test_as_signal2D_T(self): nt.assert_equal(self.s.data.T.shape, self.s.as_signal2D((1, 0)).data.shape) def test_as_signal2D(self): nt.assert_equal(self.s.data.shape, self.s.as_signal2D((0, 1)).data.shape) def test_as_signal1D_T(self): nt.assert_equal(self.s.data.T.shape, self.s.as_signal1D(1).data.shape) def test_as_signal1D(self): nt.assert_equal(self.s.data.shape, self.s.as_signal1D(0).data.shape) def test_s2EELS2im2s(self): s = self.s.as_signal1D(0) s.set_signal_type("EELS") im = s.as_signal2D((1, 0)) nt.assert_equal(im.metadata.Signal.signal_type, "EELS") s = im.as_signal1D(0) nt.assert_equal(s.metadata.Signal.signal_type, "EELS") nt.assert_is_instance(s, signals.EELSSpectrum)
class Test2d: def setup_method(self, method): self.s = BaseSignal(np.random.random((2, 3))) # (|3, 2) def test_as_signal2D_T(self): assert (self.s.data.T.shape == self.s.as_signal2D((1, 0)).data.shape) def test_as_signal2D(self): assert (self.s.data.shape == self.s.as_signal2D((0, 1)).data.shape) def test_as_signal1D_T(self): assert (self.s.data.T.shape == self.s.as_signal1D(1).data.shape) def test_as_signal1D(self): assert (self.s.data.shape == self.s.as_signal1D(0).data.shape) def test_s2EELS2im2s(self): s = self.s.as_signal1D(0) s.set_signal_type("EELS") im = s.as_signal2D((1, 0)) assert im.metadata.Signal.signal_type == "EELS" s = im.as_signal1D(0) assert s.metadata.Signal.signal_type == "EELS" if s._lazy: _class = _lazy_signals.LazyEELSSpectrum else: _class = signals.EELSSpectrum assert isinstance(s, _class)
def _denoised_data_to_signal(self): """Converts denoised data back to a HyperSpy signal.""" signal = BaseSignal(self.Y_) if self._signal_type == "spectrum": return signal.as_signal1D(2) if self._signal_type == "image": return signal.as_signal2D((1, 2))
def test_writing_unsupported_data_type(): data = np.arange(5 * 10).reshape((5, 10)) s = BaseSignal(data.astype('int64')) with tempfile.TemporaryDirectory() as tmpdir: with pytest.raises(IOError) as cm: fname = os.path.join(tmpdir, 'test_writing_unsupported_data_type.unf') s.save(fname) cm.match("The SEMPER file format does not support int64 data type",)
def test_writing_unsupported_data_type(): data = np.arange(5 * 10).reshape((5, 10)) s = BaseSignal(data.astype('int64')) with tempfile.TemporaryDirectory() as tmpdir: with pytest.raises(IOError) as cm: fname = os.path.join(tmpdir, 'test_writing_unsupported_data_type.unf') s.save(fname) cm.match( "The SEMPER file format does not support int64 data type", )
class Test3d: def setUp(self): self.s = BaseSignal(np.random.random((2, 3, 4))) # (|4, 3, 2) def test_as_signal2D_contigous(self): nt.assert_true(self.s.as_signal2D((0, 1)).data.flags["C_CONTIGUOUS"]) def test_as_signal2D_1(self): nt.assert_equal(self.s.as_signal2D((0, 1)).data.shape, (2, 3, 4)) # (2| 4, 3) def test_as_signal2D_2(self): nt.assert_equal(self.s.as_signal2D((1, 0)).data.shape, (2, 4, 3)) # (2| 3, 4) def test_as_signal2D_3(self): nt.assert_equal(self.s.as_signal2D((1, 2)).data.shape, (4, 2, 3)) # (4| 3, 2) def test_as_signal1D_contigous(self): nt.assert_true(self.s.as_signal1D(0).data.flags["C_CONTIGUOUS"]) def test_as_signal1D_0(self): nt.assert_equal(self.s.as_signal1D(0).data.shape, (2, 3, 4)) # (3, 2| 4) def test_as_signal1D_1(self): nt.assert_equal(self.s.as_signal1D(1).data.shape, (2, 4, 3)) # (4, 2| 3) def test_as_signal1D_2(self): nt.assert_equal(self.s.as_signal1D(2).data.shape, (3, 4, 2)) # (4, 3| 2) def test_remove_axis(self): im = self.s.as_signal2D((-2, -1)) im._remove_axis(-1) nt.assert_is_instance(im, signals.Signal1D)
def __init__(self, *args, **kwargs): """ If a hyperspy signal is given, it is transformed into a ModifiedSignal. In that case, additional *args and **kwargs are discarded. """ if len(args) > 0 and isinstance(args[0], BaseSignal): # Pretend it is a hs signal, copy axes and metadata sdict = args[0]._to_dictionary() self.__class__.__init__(self, **sdict) else: BaseSignal.__init__(self, *args, **kwargs)
def test_deepcopy(): s = BaseSignal([0]) s.metadata.test = [0] s.original_metadata.test = [0] s_deepcopy = s.deepcopy() s.metadata.test.append(1) s.original_metadata.test.append(1) assert s.metadata.test == [0, 1] assert s.original_metadata.test == [0, 1] assert s_deepcopy.metadata.test == [0] assert s_deepcopy.original_metadata.test == [0]
def test_writing_loading_metadata(): data = np.arange(5 * 10).reshape((5, 10)).astype(np.int8) s = BaseSignal(data) s.metadata.set_item('General.date', "2016-08-06") s.metadata.set_item('General.time', "11:55:00") with tempfile.TemporaryDirectory() as tmpdir: fname = os.path.join(tmpdir, 'test_write_with_metadata.unf') s.save(fname) s2 = load(fname) np.testing.assert_allclose(s.data, s2.data) nt.assert_equal(s.metadata.General.date, s2.metadata.General.date) nt.assert_equal(s.metadata.General.time, s2.metadata.General.time)
def test_writing_loading_metadata(): data = np.arange(5 * 10).reshape((5, 10)).astype(np.int8) s = BaseSignal(data) s.metadata.set_item('General.date', "2016-08-06") s.metadata.set_item('General.time', "11:55:00") with tempfile.TemporaryDirectory() as tmpdir: fname = os.path.join(tmpdir, 'test_write_with_metadata.unf') s.save(fname) s2 = load(fname) np.testing.assert_allclose(s.data, s2.data) assert s.metadata.General.date == s2.metadata.General.date assert s.metadata.General.time == s2.metadata.General.time
def test_save_and_read(self): signal_ref = BaseSignal(data_save) signal_ref.metadata.General.title = test_title signal_ref.axes_manager[0].name = 'x' signal_ref.axes_manager[1].name = 'y' signal_ref.axes_manager[2].name = 'z' signal_ref.axes_manager[0].scale = 2 signal_ref.axes_manager[1].scale = 3 signal_ref.axes_manager[2].scale = 4 signal_ref.axes_manager[0].offset = 10 signal_ref.axes_manager[1].offset = 20 signal_ref.axes_manager[2].offset = 30 signal_ref.axes_manager[0].units = 'nm' signal_ref.axes_manager[1].units = 'µm' signal_ref.axes_manager[2].units = 'mm' signal_ref.save(os.path.join(my_path, 'emd_files', 'example_temp.emd'), overwrite=True, signal_metadata=sig_metadata, user=user, microscope=microscope, sample=sample, comments=comments) signal = load(os.path.join(my_path, 'emd_files', 'example_temp.emd')) np.testing.assert_equal(signal.data, signal_ref.data) np.testing.assert_equal(signal.axes_manager[0].name, 'x') np.testing.assert_equal(signal.axes_manager[1].name, 'y') np.testing.assert_equal(signal.axes_manager[2].name, 'z') np.testing.assert_equal(signal.axes_manager[0].scale, 2) np.testing.assert_almost_equal(signal.axes_manager[1].scale, 3.0) np.testing.assert_almost_equal(signal.axes_manager[2].scale, 4.0) np.testing.assert_equal(signal.axes_manager[0].offset, 10) np.testing.assert_almost_equal(signal.axes_manager[1].offset, 20.0) np.testing.assert_almost_equal(signal.axes_manager[2].offset, 30.0) np.testing.assert_equal(signal.axes_manager[0].units, 'nm') np.testing.assert_equal(signal.axes_manager[1].units, 'µm') np.testing.assert_equal(signal.axes_manager[2].units, 'mm') np.testing.assert_equal(signal.metadata.General.title, test_title) np.testing.assert_equal(signal.metadata.General.user.as_dictionary(), user) np.testing.assert_equal( signal.metadata.General.microscope.as_dictionary(), microscope) np.testing.assert_equal(signal.metadata.General.sample.as_dictionary(), sample) np.testing.assert_equal( signal.metadata.General.comments.as_dictionary(), comments) for key, ref_value in sig_metadata.items(): np.testing.assert_equal( signal.metadata.Signal.as_dictionary().get(key), ref_value) assert isinstance(signal, BaseSignal)
def test_1d_azimuthal_integral_slow_shifted_center(self, ones): from hyperspy.signals import BaseSignal aff = [[1, 1, 0], [0, 1, 0], [0, 0, 1]] aff_bs = BaseSignal(data=aff) center = [1, 1] center_bs = BaseSignal(data=center) ones.get_azimuthal_integral1d( npt_rad=10, method="BBox", wavelength=1e-9, correctSolidAngle=False, affine=aff_bs, center=center_bs, )
def to_signal(self, mask=None): """ Creates a hyperspy.Signal1D object with the same spectral image. The resulting hyperspy.Signal1D contains the full spectral image. Returns ---------- s : hyperspy.Signal1D hyperspy object """ (nz, ny, nx, nf) = np.shape(self.spec_im) dx = (self.x_array[1] - self.x_array[0]) dy = (self.y_array[1] - self.y_array[0]) (units, scale) = self.get_unit_scaling() dx *= scale dy *= scale spec_name = 'index' if self.spec_units in ['nm', 'um']: spec_name = 'Wavelength' elif self.spec_units == 'eV': spec_name = 'E' dict_y = {'name': 'y', 'units': units, 'scale': dy, 'size': ny} dict_x = {'name': 'x', 'units': units, 'scale': dx, 'size': nx} dict_f = { 'name': spec_name, 'units': self.spec_units, 'scale': self.spec_x_array[1] - self.spec_x_array[0], 'size': nf, 'offset': self.spec_x_array[0] } dict_z = {'name': 'z', 'units': units, 'size': nz} if nz == 1: s = Signal1D(np.squeeze(self.spec_im), axes=[dict_y, dict_x, dict_f], mask=mask) s.change_dtype('float64') return s else: dz = (self.z_array[1] - self.z_array[0]) * scale dict_z['scale'] = dz s = BaseSignal(self.spec_im, axes=[dict_z, dict_y, dict_x, dict_f]) s.change_dtype('float64') return s.as_signal1D(0)
class Test1d: def setUp(self): self.s = BaseSignal(np.arange(2)) @nt.raises(DataDimensionError) def test_as_signal2D(self): nt.assert_true((self.s.data == self.s.as_signal2D((0, 1)).data).all()) def test_as_signal1D(self): nt.assert_true((self.s.data == self.s.as_signal1D(0).data).all()) def test_set_EELS(self): s = self.s.as_signal1D(0) s.set_signal_type("EELS") nt.assert_equal(s.metadata.Signal.signal_type, "EELS") nt.assert_is_instance(s, signals.EELSSpectrum)
def setUp(self): self.ellipse = np.random.rand(25, 200, 200) self.center = [100, 110] self.lengths = np.add(np.random.rand(25, 2) * 30, 50) self.lengths = [sorted(l, reverse=True) for l in self.lengths] self.angle = np.random.rand(25) * np.pi rand_angles = np.random.rand(25, 200) * 2 * np.pi rand_points = [[[(np.cos(ang) * l[0]), np.sin(ang) * l[1]] for ang in rand_angle] for l, rand_angle in zip(self.lengths, rand_angles)] rand_points = np.array([[[ int(point[0] * np.cos(a) - point[1] * np.sin(a) + self.center[0]), int(point[1] * np.cos(a) + point[0] * np.sin(a) + self.center[1]) ] for point in rand_point] for (rand_point, a) in zip(rand_points, self.angle)]) for i, rand_point in enumerate(rand_points): self.ellipse[i, rand_point[:, 0], rand_point[:, 1]] = 100 self.ellipse = np.random.poisson(self.ellipse) self.ellipse = np.reshape(self.ellipse, (5, 5, 200, 200)) self.ellipse = np.reshape( np.transpose([[[self.ellipse] * 2] * 2], axes=(0, 3, 1, 4, 2, 5, 6)), (10, 10, 200, 200)) print(np.shape(self.ellipse)) self.ellipse = np.random.poisson(self.ellipse) self.bs = BaseSignal(data=self.ellipse, lazy=False) self.s = Signal2D(self.bs) self.ds = DiffractionSignal(self.s)
def get_magnitudes(self, *args, **kwargs): """Calculate the magnitude of diffraction vectors. Parameters ---------- *args: Arguments to be passed to map(). **kwargs: Keyword arguments to map(). Returns ------- magnitudes : BaseSignal A signal with navigation dimensions as the original diffraction vectors containging an array of gvector magnitudes at each navigation position. """ # If ragged the signal axes will not be defined if len(self.axes_manager.signal_axes) == 0: magnitudes = self.map(calculate_norms_ragged, inplace=False, *args, **kwargs) # Otherwise easier to calculate. else: magnitudes = BaseSignal(calculate_norms(self)) magnitudes.axes_manager.set_signal_dimension(0) return magnitudes
def _create_signal( shape, dim, dtype, ): data = np.arange(np.product(shape)).reshape(shape).astype(dtype) if dim == 1: if len(shape) > 2: s = EELSSpectrum(data) s.set_microscope_parameters(beam_energy=100., convergence_angle=1., collection_angle=10.) else: s = EDSTEMSpectrum(data) s.set_microscope_parameters(beam_energy=100., live_time=1., tilt_stage=2., azimuth_angle=3., elevation_angle=4., energy_resolution_MnKa=5.) else: s = BaseSignal(data) s.axes_manager.set_signal_dimension(dim) for i, axis in enumerate(s.axes_manager._axes): i += 1 axis.offset = i * 0.5 axis.scale = i * 100 axis.name = "%i" % i if axis.navigate: axis.units = "m" else: axis.units = "eV" return s
def test_null_signal(): rng = np.random.RandomState(123) s = BaseSignal(rng.random_sample()) with pytest.raises(AttributeError): s.T.fft() with pytest.raises(AttributeError): s.T.ifft()
def test_save_and_read(self, lazy): signal_ref = BaseSignal(data_save) signal_ref.metadata.General.title = test_title signal_ref.axes_manager[0].name = 'x' signal_ref.axes_manager[1].name = 'y' signal_ref.axes_manager[2].name = 'z' signal_ref.axes_manager[0].scale = 2 signal_ref.axes_manager[1].scale = 3 signal_ref.axes_manager[2].scale = 4 signal_ref.axes_manager[0].offset = 10 signal_ref.axes_manager[1].offset = 20 signal_ref.axes_manager[2].offset = 30 signal_ref.axes_manager[0].units = 'nm' signal_ref.axes_manager[1].units = 'µm' signal_ref.axes_manager[2].units = 'mm' signal_ref.original_metadata.add_dictionary({'user': user}) signal_ref.original_metadata.add_dictionary({'microscope': microscope}) signal_ref.original_metadata.add_dictionary({'sample': sample}) signal_ref.original_metadata.add_dictionary({'comments': comments}) signal_ref.save(os.path.join(my_path, 'emd_files', 'example_temp.emd'), overwrite=True) signal = load(os.path.join(my_path, 'emd_files', 'example_temp.emd'), lazy=lazy) if lazy: signal.compute(close_file=True) om = signal.original_metadata np.testing.assert_equal(signal.data, signal_ref.data) np.testing.assert_equal(signal.axes_manager[0].name, 'x') np.testing.assert_equal(signal.axes_manager[1].name, 'y') np.testing.assert_equal(signal.axes_manager[2].name, 'z') np.testing.assert_equal(signal.axes_manager[0].scale, 2) np.testing.assert_almost_equal(signal.axes_manager[1].scale, 3.0) np.testing.assert_almost_equal(signal.axes_manager[2].scale, 4.0) np.testing.assert_equal(signal.axes_manager[0].offset, 10) np.testing.assert_almost_equal(signal.axes_manager[1].offset, 20.0) np.testing.assert_almost_equal(signal.axes_manager[2].offset, 30.0) np.testing.assert_equal(signal.axes_manager[0].units, 'nm') np.testing.assert_equal(signal.axes_manager[1].units, 'µm') np.testing.assert_equal(signal.axes_manager[2].units, 'mm') np.testing.assert_equal(signal.metadata.General.title, test_title) np.testing.assert_equal(om.user.as_dictionary(), user) np.testing.assert_equal(om.microscope.as_dictionary(), microscope) np.testing.assert_equal(om.sample.as_dictionary(), sample) np.testing.assert_equal(om.comments.as_dictionary(), comments) assert isinstance(signal, BaseSignal)
def test_save_and_read(self): signal_ref = BaseSignal(data_save) signal_ref.metadata.General.title = test_title signal_ref.axes_manager[0].name = 'x' signal_ref.axes_manager[1].name = 'y' signal_ref.axes_manager[2].name = 'z' signal_ref.axes_manager[0].scale = 2 signal_ref.axes_manager[1].scale = 3 signal_ref.axes_manager[2].scale = 4 signal_ref.axes_manager[0].offset = 10 signal_ref.axes_manager[1].offset = 20 signal_ref.axes_manager[2].offset = 30 signal_ref.axes_manager[0].units = 'nm' signal_ref.axes_manager[1].units = 'µm' signal_ref.axes_manager[2].units = 'mm' signal_ref.save(os.path.join(my_path, 'emd_files', 'example_temp.emd'), overwrite=True, signal_metadata=sig_metadata, user=user, microscope=microscope, sample=sample, comments=comments) signal = load(os.path.join(my_path, 'emd_files', 'example_temp.emd')) np.testing.assert_equal(signal.data, signal_ref.data) np.testing.assert_equal(signal.axes_manager[0].name, 'x') np.testing.assert_equal(signal.axes_manager[1].name, 'y') np.testing.assert_equal(signal.axes_manager[2].name, 'z') np.testing.assert_equal(signal.axes_manager[0].scale, 2) np.testing.assert_almost_equal(signal.axes_manager[1].scale, 3.0) np.testing.assert_almost_equal(signal.axes_manager[2].scale, 4.0) np.testing.assert_equal(signal.axes_manager[0].offset, 10) np.testing.assert_almost_equal(signal.axes_manager[1].offset, 20.0) np.testing.assert_almost_equal(signal.axes_manager[2].offset, 30.0) np.testing.assert_equal(signal.axes_manager[0].units, 'nm') np.testing.assert_equal(signal.axes_manager[1].units, 'µm') np.testing.assert_equal(signal.axes_manager[2].units, 'mm') np.testing.assert_equal(signal.metadata.General.title, test_title) np.testing.assert_equal( signal.metadata.General.user.as_dictionary(), user) np.testing.assert_equal( signal.metadata.General.microscope.as_dictionary(), microscope) np.testing.assert_equal( signal.metadata.General.sample.as_dictionary(), sample) np.testing.assert_equal( signal.metadata.General.comments.as_dictionary(), comments) for key, ref_value in sig_metadata.items(): np.testing.assert_equal( signal.metadata.Signal.as_dictionary().get(key), ref_value) assert isinstance(signal, BaseSignal)
def test_stack_stack_metadata_value(self): s = BaseSignal(1) s.metadata.General.title = 'title 1' s.original_metadata.set_item('a', 1) s2 = BaseSignal(2) s2.metadata.General.title = 'title 2' s2.original_metadata.set_item('a', 2) stack_out = utils.stack([s, s2], stack_metadata=True) elem0 = stack_out.original_metadata.stack_elements.element0 elem1 = stack_out.original_metadata.stack_elements.element1 for el, _s in zip([elem0, elem1], [s, s2]): assert el.original_metadata.as_dictionary() == \ _s.original_metadata.as_dictionary() assert el.metadata.as_dictionary() == _s.metadata.as_dictionary()
def test_1d_basesignal(self): s = BaseSignal(arange(10)) assert len(s.axes_manager.signal_extent) == 2 signal_axis = s.axes_manager.signal_axes[0] signal_extent = (signal_axis.low_value, signal_axis.high_value) assert signal_extent == s.axes_manager.signal_extent assert len(s.axes_manager.navigation_extent) == 0 assert () == s.axes_manager.navigation_extent
def test_change_scale(self): data = arange(132) s = BaseSignal(data) axes = s.axes_manager[0] scale_value_list = [0.07, 76, 1] for scale_value in scale_value_list: axes.scale = scale_value assert axes.low_value == data[0] * scale_value assert axes.high_value == data[-1] * scale_value
def test_change_offset(self): data = arange(81) s = BaseSignal(data) axes = s.axes_manager[0] offset_value_list = [12, -216, 1, 0] for offset_value in offset_value_list: axes.offset = offset_value assert axes.low_value == (data[0] + offset_value) assert axes.high_value == (data[-1] + offset_value)
def test_change_offset_scale(self): data = arange(11) s = BaseSignal(data) axes = s.axes_manager[0] scale, offset = 0.123, -314 axes.offset = offset axes.scale = scale assert axes.low_value == (data[0] * scale + offset) assert axes.high_value == (data[-1] * scale + offset)
def test_2d_azimuthal_integral_slow_mask(self, ones): from hyperspy.signals import BaseSignal aff = [[1, 1, 0], [0, 1, 0], [0, 0, 1]] aff_bs = BaseSignal(data=aff) center = [1, 1] center_bs = BaseSignal(data=center) mask = np.zeros((10, 10)) mask_bs = BaseSignal(data=mask) ones.get_azimuthal_integral2d( npt_rad=10, npt_azim=10, method="BBox", wavelength=1e-9, correctSolidAngle=False, affine=aff_bs, center=center_bs, mask=mask_bs, )
class Test1d: def setup_method(self, method): self.s = BaseSignal(np.arange(2)) def test_as_signal2D(self): with pytest.raises(DataDimensionError): assert (self.s.data == self.s.as_signal2D((0, 1)).data).all() def test_as_signal1D(self): assert (self.s.data == self.s.as_signal1D(0).data).all() def test_set_EELS(self): s = self.s.as_signal1D(0) s.set_signal_type("EELS") assert s.metadata.Signal.signal_type == "EELS" if s._lazy: _class = _lazy_signals.LazyEELSSpectrum else: _class = signals.EELSSpectrum assert isinstance(s, _class)
def test_transpose_nav0_sig0(): s = BaseSignal([0.]) assert s.axes_manager.signal_dimension == 0 assert s.axes_manager.navigation_dimension == 0 assert s.axes_manager.signal_axes[0].size == 1 assert s.axes_manager.navigation_axes == () s2 = s.T assert s2.axes_manager.signal_dimension == 0 assert s2.axes_manager.navigation_dimension == 1 assert s2.axes_manager.signal_axes == () assert s2.axes_manager.navigation_axes[0].size == 1
def setUp(self): d = np.random.rand(10, 10, 512, 512) self.center = [276, 256] self.lengths = sorted(np.random.rand(2) * 50 + 100, reverse=True) self.angle = np.random.rand() * np.pi rand_points = random_ellipse(num_points=1000, center=self.center, foci=self.lengths, angle=self.angle) d[:, :, rand_points[:, 0], rand_points[:, 1]] = 10 self.bs = BaseSignal(data=d, lazy=False) self.s = Signal2D(self.bs) self.ds = DiffractionSignal(self.s)
def test_2d_azimuthal_integral_slow(self, ones): from hyperspy.signals import BaseSignal aff = [[1, 1, 0], [0, 1, 0], [0, 0, 1]] aff_bs = BaseSignal(data=aff) ones.get_azimuthal_integral2d( npt_rad=10, npt_azim=10, method="BBox", wavelength=1e-9, correctSolidAngle=False, affine=aff_bs, )
def test_factors_error(): s = artificial_data.get_core_loss_eels_line_scan_signal() s.decomposition() factors = s.get_decomposition_factors().data with pytest.raises(TypeError, match="`factors` must be a BaseSignal instance"): s.blind_source_separation(2, factors=factors) factors = BaseSignal(s.get_decomposition_factors().data) with pytest.raises(ValueError, match="`factors` must have navigation dimension"): s.blind_source_separation(2, factors=factors)
class Test1d: def setup_method(self, method): self.s = BaseSignal(np.arange(2)) def test_as_signal2D(self): with pytest.raises(DataDimensionError): assert (self.s.data == self.s.as_signal2D( (0, 1)).data).all() def test_as_signal1D(self): assert (self.s.data == self.s.as_signal1D(0).data).all() def test_set_EELS(self): s = self.s.as_signal1D(0) s.set_signal_type("EELS") assert s.metadata.Signal.signal_type == "EELS" if s._lazy: _class = signals.LazyEELSSpectrum else: _class = signals.EELSSpectrum assert isinstance(s, _class)
class Test3d: def setup_method(self, method): self.s = BaseSignal(np.random.random((2, 3, 4))) # (|4, 3, 2) def test_as_signal2D_contigous(self): if self.s._lazy: pytest.skip("Dask array flags not supported") assert self.s.as_signal2D((0, 1)).data.flags['C_CONTIGUOUS'] def test_as_signal2D_1(self): assert ( self.s.as_signal2D((0, 1)).data.shape == (2, 3, 4)) # (2| 4, 3) def test_as_signal2D_2(self): assert ( self.s.as_signal2D((1, 0)).data.shape == (2, 4, 3)) # (2| 3, 4) def test_as_signal2D_3(self): assert ( self.s.as_signal2D((1, 2)).data.shape == (4, 2, 3)) # (4| 3, 2) def test_as_signal1D_contigous(self): if self.s._lazy: pytest.skip("Dask array flags not supported") assert self.s.as_signal1D(0).data.flags['C_CONTIGUOUS'] def test_as_signal1D_0(self): assert ( self.s.as_signal1D(0).data.shape == (2, 3, 4)) # (3, 2| 4) def test_as_signal1D_1(self): assert ( self.s.as_signal1D(1).data.shape == (2, 4, 3)) # (4, 2| 3) def test_as_signal1D_2(self): assert ( self.s.as_signal1D(2).data.shape == (3, 4, 2)) # (4, 3| 2) def test_remove_axis(self): im = self.s.as_signal2D((-2, -1)) im._remove_axis(-1) assert isinstance(im, signals.Signal1D)
def test_iterate_markers(): from skimage.feature import peak_local_max import scipy.misc ims = BaseSignal(scipy.misc.face()).as_signal2D([1, 2]) index = np.array([peak_local_max(im.data, min_distance=100, num_peaks=4) for im in ims]) # Add multiple markers for i in range(4): xs = index[:, i, 1] ys = index[:, i, 0] m = markers.point(x=xs, y=ys, color='red') ims.add_marker(m, plot_marker=True, permanent=True) m = markers.text(x=10 + xs, y=10 + ys, text=str(i), color='k') ims.add_marker(m, plot_marker=True, permanent=True) xs = index[:, :, 1] ys = index[:, :, 0] m = markers.rectangle(np.min(xs, 1), np.min(ys, 1), np.max(xs, 1), np.max(ys, 1), color='green') ims.add_marker(m, plot_marker=True, permanent=True) for im in ims: m_original = ims.metadata.Markers m_iterated = im.metadata.Markers for key in m_original.keys(): mo = m_original[key] mi = m_iterated[key] assert mo.__class__.__name__ == mi.__class__.__name__ assert mo.name == mi.name assert mo.get_data_position('x1') == mi.get_data_position('x1') assert mo.get_data_position('y1') == mi.get_data_position('y1') assert mo.get_data_position('text') == mi.get_data_position('text') assert mo.marker_properties['color'] == \ mi.marker_properties['color']
def setup_method(self, method): self.s = BaseSignal(np.random.random((2, 3, 4))) # (|4, 3, 2)
def setUp(self): self.s = BaseSignal(np.random.random((2, 3, 4))) # (|4, 3, 2)
def setup_method(self, method): self.s = BaseSignal(np.arange(2))
def reconstruct_phase(self, reference=None, sb_size=None, sb_smoothness=None, sb_unit=None, sb='lower', sb_position=None, output_shape=None, plotting=False, show_progressbar=False, store_parameters=True, parallel=None): """Reconstruct electron holograms. Operates on multidimensional hyperspy signals. There are several usage schemes: 1. Reconstruct 1d or Nd hologram without reference 2. Reconstruct 1d or Nd hologram using single reference hologram 3. Reconstruct Nd hologram using Nd reference hologram (applies each reference to each hologram in Nd stack) The reconstruction parameters (sb_position, sb_size, sb_smoothness) have to be 1d or to have same dimensionality as the hologram. Parameters ---------- reference : ndarray, :class:`~hyperspy.signals.Signal2D, None Vacuum reference hologram. sb_size : float, ndarray, :class:`~hyperspy.signals.BaseSignal, None Sideband radius of the aperture in corresponding unit (see 'sb_unit'). If None, the radius of the aperture is set to 1/3 of the distance between sideband and center band. sb_smoothness : float, ndarray, :class:`~hyperspy.signals.BaseSignal, None Smoothness of the aperture in the same unit as sb_size. sb_unit : str, None Unit of the two sideband parameters 'sb_size' and 'sb_smoothness'. Default: None - Sideband size given in pixels 'nm': Size and smoothness of the aperture are given in 1/nm. 'mrad': Size and smoothness of the aperture are given in mrad. sb : str, None Select which sideband is selected. 'upper' or 'lower'. sb_position : tuple, :class:`~hyperspy.signals.Signal1D, None The sideband position (y, x), referred to the non-shifted FFT. If None, sideband is determined automatically from FFT. output_shape: tuple, None Choose a new output shape. Default is the shape of the input hologram. The output shape should not be larger than the input shape. plotting : boolean Shows details of the reconstruction (i.e. SB selection). show_progressbar : boolean Shows progressbar while iterating over different slices of the signal (passes the parameter to map method). parallel : bool Run the reconstruction in parallel store_parameters : boolean Store reconstruction parameters in metadata Returns ------- wave : :class:`~hyperspy.signals.WaveImage Reconstructed electron wave. By default object wave is devided by reference wave Examples -------- >>> import hyperspy.api as hs >>> s = hs.datasets.example_signals.object_hologram() >>> sb_position = s.estimate_sideband_position() >>> sb_size = s.estimate_sideband_size(sb_position) >>> sb_size.data >>> wave = s.reconstruct_phase(sb_position=sb_position, sb_size=sb_size) """ # TODO: Use defaults for choosing sideband, smoothness, relative filter # size and output shape if not provided # TODO: Plot FFT with marked SB and SB filter if plotting is enabled # Parsing reference: if not isinstance(reference, HologramImage): if isinstance(reference, Signal2D): if (not reference.axes_manager.navigation_shape == self.axes_manager.navigation_shape and reference.axes_manager.navigation_size): raise ValueError('The navigation dimensions of object and' 'reference holograms do not match') _logger.warning('The reference image signal type is not ' 'HologramImage. It will be converted to ' 'HologramImage automatically.') reference.set_signal_type('hologram') elif reference is not None: reference = HologramImage(reference) if isinstance(reference.data, daArray): reference = reference.as_lazy() # Testing match of navigation axes of reference and self # (exception: reference nav_dim=1): if (reference and not reference.axes_manager.navigation_shape == self.axes_manager.navigation_shape and reference.axes_manager.navigation_size): raise ValueError('The navigation dimensions of object and ' 'reference holograms do not match') if reference and not reference.axes_manager.signal_shape == self.axes_manager.signal_shape: raise ValueError('The signal dimensions of object and reference' ' holograms do not match') # Parsing sideband position: if sb_position is None: _logger.warning('Sideband position is not specified. The sideband ' 'will be found automatically which may cause ' 'wrong results.') if reference is None: sb_position = self.estimate_sideband_position( sb=sb, parallel=parallel) else: sb_position = reference.estimate_sideband_position( sb=sb, parallel=parallel) else: if isinstance(sb_position, BaseSignal) and \ not sb_position._signal_dimension == 1: raise ValueError('sb_position dimension has to be 1') if not isinstance(sb_position, Signal1D): sb_position = Signal1D(sb_position) if isinstance(sb_position.data, daArray): sb_position = sb_position.as_lazy() if not sb_position.axes_manager.signal_size == 2: raise ValueError('sb_position should to have signal size of 2') if sb_position.axes_manager.navigation_size != self.axes_manager.navigation_size: if sb_position.axes_manager.navigation_size: raise ValueError('Sideband position dimensions do not match' ' neither reference nor hologram dimensions.') # sb_position navdim=0, therefore map function should not iterate: else: sb_position_temp = sb_position.data else: sb_position_temp = sb_position.deepcopy() ## Parsing sideband size # Default value is 1/2 distance between sideband and central band if sb_size is None: if reference is None: sb_size = self.estimate_sideband_size( sb_position, parallel=parallel) else: sb_size = reference.estimate_sideband_size( sb_position, parallel=parallel) else: if not isinstance(sb_size, BaseSignal): if isinstance(sb_size, (np.ndarray, daArray)) and sb_size.size > 1: # transpose if np.array of multiple instances sb_size = BaseSignal(sb_size).T else: sb_size = BaseSignal(sb_size) if isinstance(sb_size.data, daArray): sb_size = sb_size.as_lazy() if sb_size.axes_manager.navigation_size != self.axes_manager.navigation_size: if sb_size.axes_manager.navigation_size: raise ValueError('Sideband size dimensions do not match ' 'neither reference nor hologram dimensions.') # sb_position navdim=0, therefore map function should not iterate: else: sb_size_temp = np.float64(sb_size.data) else: sb_size_temp = sb_size.deepcopy() # Standard edge smoothness of sideband aperture 5% of sb_size if sb_smoothness is None: sb_smoothness = sb_size * 0.05 else: if not isinstance(sb_smoothness, BaseSignal): if isinstance( sb_smoothness, (np.ndarray, daArray)) and sb_smoothness.size > 1: sb_smoothness = BaseSignal(sb_smoothness).T else: sb_smoothness = BaseSignal(sb_smoothness) if isinstance(sb_smoothness.data, daArray): sb_smoothness = sb_smoothness.as_lazy() if sb_smoothness.axes_manager.navigation_size != self.axes_manager.navigation_size: if sb_smoothness.axes_manager.navigation_size: raise ValueError('Sideband smoothness dimensions do not match' ' neither reference nor hologram ' 'dimensions.') # sb_position navdim=0, therefore map function should not iterate it: else: sb_smoothness_temp = np.float64(sb_smoothness.data) else: sb_smoothness_temp = sb_smoothness.deepcopy() # Convert sideband size from 1/nm or mrad to pixels if sb_unit == 'nm': f_sampling = np.divide( 1, [a * b for a, b in \ zip(self.axes_manager.signal_shape, (self.axes_manager.signal_axes[0].scale, self.axes_manager.signal_axes[1].scale))] ) sb_size_temp = sb_size_temp / np.mean(f_sampling) sb_smoothness_temp = sb_smoothness_temp / np.mean(f_sampling) elif sb_unit == 'mrad': f_sampling = np.divide( 1, [a * b for a, b in \ zip(self.axes_manager.signal_shape, (self.axes_manager.signal_axes[0].scale, self.axes_manager.signal_axes[1].scale))] ) try: ht = self.metadata.Acquisition_instrument.TEM.beam_energy except: raise AttributeError("Please define the beam energy." "You can do this e.g. by using the " "set_microscope_parameters method") momentum = 2 * constants.m_e * constants.elementary_charge * ht * \ 1000 * (1 + constants.elementary_charge * ht * \ 1000 / (2 * constants.m_e * constants.c ** 2)) wavelength = constants.h / np.sqrt(momentum) * 1e9 # in nm sb_size_temp = sb_size_temp / (1000 * wavelength * np.mean(f_sampling)) sb_smoothness_temp = sb_smoothness_temp / (1000 * wavelength * np.mean(f_sampling)) # Find output shape: if output_shape is None: ## Future improvement will give a possibility to choose # if sb_size.axes_manager.navigation_size > 0: # output_shape = (np.int(sb_size.inav[0].data*2), np.int(sb_size.inav[0].data*2)) # else: # output_shape = (np.int(sb_size.data*2), np.int(sb_size.data*2)) output_shape = self.axes_manager.signal_shape output_shape = output_shape[::-1] # Logging the reconstruction parameters if appropriate: _logger.info('Sideband position in pixels: {}'.format(sb_position)) _logger.info('Sideband aperture radius in pixels: {}'.format(sb_size)) _logger.info('Sideband aperture smoothness in pixels: {}'.format( sb_smoothness)) # Reconstructing object electron wave: # Checking if reference is a single image, which requires sideband # parameters as a nparray to avoid iteration trough those: wave_object = self.map( reconstruct, holo_sampling=(self.axes_manager.signal_axes[0].scale, self.axes_manager.signal_axes[1].scale), sb_size=sb_size_temp, sb_position=sb_position_temp, sb_smoothness=sb_smoothness_temp, output_shape=output_shape, plotting=plotting, show_progressbar=show_progressbar, inplace=False, parallel=parallel, ragged=False) # Reconstructing reference wave and applying it (division): if reference is None: wave_reference = 1 # case when reference is 1d elif reference.axes_manager.navigation_size != self.axes_manager.navigation_size: # Prepare parameters for reconstruction of the reference wave: if reference.axes_manager.navigation_size == 0 and \ sb_position.axes_manager.navigation_size > 0: # 1d reference, but parameters are multidimensional sb_position_ref = _first_nav_pixel_data(sb_position_temp) else: sb_position_ref = sb_position_temp if reference.axes_manager.navigation_size == 0 and \ sb_size.axes_manager.navigation_size > 0: # 1d reference, but parameters are multidimensional sb_size_ref = _first_nav_pixel_data(sb_size_temp) else: sb_size_ref = sb_size_temp if reference.axes_manager.navigation_size == 0 and \ sb_smoothness.axes_manager.navigation_size > 0: # 1d reference, but parameters are multidimensional sb_smoothness_ref = np.float64( _first_nav_pixel_data(sb_smoothness_temp)) else: sb_smoothness_ref = sb_smoothness_temp # wave_reference = reference.map( reconstruct, holo_sampling=(self.axes_manager.signal_axes[0].scale, self.axes_manager.signal_axes[1].scale), sb_size=sb_size_ref, sb_position=sb_position_ref, sb_smoothness=sb_smoothness_ref, output_shape=output_shape, plotting=plotting, show_progressbar=show_progressbar, inplace=False, parallel=parallel, ragged=False) else: wave_reference = reference.map( reconstruct, holo_sampling=(self.axes_manager.signal_axes[0].scale, self.axes_manager.signal_axes[1].scale), sb_size=sb_size_temp, sb_position=sb_position_temp, sb_smoothness=sb_smoothness_temp, output_shape=output_shape, plotting=plotting, show_progressbar=show_progressbar, inplace=False, parallel=parallel, ragged=False) wave_image = wave_object / wave_reference # New signal is a complex wave_image.set_signal_type('complex_signal2d') wave_image.axes_manager.signal_axes[0].scale = \ self.axes_manager.signal_axes[0].scale * \ self.axes_manager.signal_shape[0] / output_shape[1] wave_image.axes_manager.signal_axes[1].scale = \ self.axes_manager.signal_axes[1].scale * \ self.axes_manager.signal_shape[1] / output_shape[0] # Reconstruction parameters are stored in holo_reconstruction_parameters: if store_parameters: rec_param_dict = OrderedDict( [('sb_position', sb_position_temp), ('sb_size', sb_size_temp), ('sb_units', sb_unit), ('sb_smoothness', sb_smoothness_temp)]) wave_image.metadata.Signal.add_node('Holography') wave_image.metadata.Signal.Holography.add_node( 'Reconstruction_parameters') wave_image.metadata.Signal.Holography.Reconstruction_parameters.add_dictionary( rec_param_dict) _logger.info('Reconstruction parameters stored in metadata') return wave_image
def setUp(self): self.s = BaseSignal(np.arange(2))