def test_crop_float_unit_convertion_signal2D(self): # Should convert the unit to nm d = np.arange(512 * 512).reshape(512, 512) s = signals.Signal2D(d) s.axes_manager[0].name = 'x' s.axes_manager[0].scale = 0.01 s.axes_manager[0].units = 'µm' s.axes_manager[1].name = 'y' s.axes_manager[1].scale = 0.01 s.axes_manager[1].units = 'µm' s.crop(0, 0.0, 0.5, convert_units=True) # also convert the other axis s.crop(1, 0.0, 500.0, convert_units=True) nt.assert_almost_equal(s.axes_manager[0].scale, 10.0) nt.assert_almost_equal(s.axes_manager[1].scale, 10.0) assert s.axes_manager[0].units == 'nm' assert s.axes_manager[1].units == 'nm' nt.assert_allclose(s.data, d[:50, :50]) # Should keep the unit to µm d = np.arange(512 * 512).reshape(512, 512) s = signals.Signal2D(d) s.axes_manager[0].name = 'x' s.axes_manager[0].scale = 0.01 s.axes_manager[0].units = 'µm' s.axes_manager[1].name = 'y' s.axes_manager[1].scale = 0.01 s.axes_manager[1].units = 'µm' s.crop(0, 0.0, 5.0, convert_units=True) s.crop(1, 0.0, 5.0, convert_units=True) nt.assert_almost_equal(s.axes_manager[0].scale, 0.01) nt.assert_almost_equal(s.axes_manager[1].scale, 0.01) assert s.axes_manager[0].units == "um" assert s.axes_manager[1].units == "um" nt.assert_allclose(s.data, d[:500, :500])
def setup_method(self, method): self.s1 = signals.Signal2D(np.ones((2, 3, 4, 5))) self.s2 = signals.Signal2D(np.ones((2, 3, 4, 5))) self.s1.axes_manager[1].scale = 1.0 self.s1.axes_manager[1].offset = 0.0 self.s1.axes_manager[1].units = "" self.s2.axes_manager[1].scale = 2.0 self.s2.axes_manager[1].offset = 0.00001 self.s2.axes_manager[1].units = "mm"
def get_atomic_resolution_tem_signal2d(): """Get an artificial atomic resolution TEM Signal2D. Returns ------- :py:class:`~hyperspy._signals.signal2d.Signal2D` Example ------- >>> s = hs.datasets.artificial_data.get_atomic_resolution_tem_signal2d() >>> s.plot() """ sX, sY = 2, 2 x_array, y_array = np.mgrid[0:200, 0:200] image = np.zeros_like(x_array, dtype=np.float32) gaussian2d = components2d.Gaussian2D(sigma_x=sX, sigma_y=sY) for x in range(10, 195, 20): for y in range(10, 195, 20): gaussian2d.centre_x.value = x gaussian2d.centre_y.value = y image += gaussian2d.function(x_array, y_array) s = signals.Signal2D(image) return s
def setup_method(self, method): rng = np.random.RandomState(100) sources = signals.Signal1D(rng.standard_t(0.5, size=(3, 100))) maps = signals.Signal2D(rng.standard_t(0.5, size=(3, 4, 5))) self.s = (sources.inav[0] * maps.inav[0].T + sources.inav[1] * maps.inav[1].T + sources.inav[2] * maps.inav[2].T)
def setup_method(self, method): np.random.seed(100) sources = signals.Signal1D(np.random.standard_t(.5, size=(3, 100))) np.random.seed(100) maps = signals.Signal2D(np.random.standard_t(.5, size=(3, 4, 5))) self.s = (sources.inav[0] * maps.inav[0].T + sources.inav[1] * maps.inav[1].T + sources.inav[2] * maps.inav[2].T)
def test_lazy_transpose_rechunks(): ar = da.ones((50, 50, 256, 256), chunks=(5, 5, 256, 256)) s = signals.Signal2D(ar).as_lazy() s1 = s.T # By default it does not rechunk cks = s.data.chunks assert s1.data.chunks == (cks[2], cks[3], cks[0], cks[1]) s2 = s.transpose(optimize=True) assert s2.data.chunks != s1.data.chunks
def setup_method(self): self.signal = signals.Signal2D(np.random.rand(11, 5, 7)) self.signal.decomposition() self.signal.blind_source_separation(number_of_components=3) self.navigation_mask = np.zeros((11,), dtype=bool) self.navigation_mask[4:6] = True self.signal_mask = np.zeros((5, 7), dtype=bool) self.signal_mask[1:4, 2:6] = True
def test_scalebar_remove(): im = signals.Signal2D(-np.arange(10000).reshape([100, 100])) for ax in im.axes_manager.signal_axes: ax.scale = 1.2 ax.units = 'nm' im.plot() assert im._plot.signal_plot.ax.scalebar is not None im._plot.signal_plot.ax.scalebar.remove()
def test_lazy_transpose_rechunks(): ar = da.ones((50, 50, 256, 256), chunks=(5, 5, 256, 256)) s = signals.Signal2D(ar).as_lazy() s1 = s.T chunks = s1.data.chunks assert len(chunks[0]) != 1 assert len(chunks[1]) != 1 assert len(chunks[2]) == 1 assert len(chunks[3]) == 1
def test_lazy_changetype_rechunk_False(): ar = da.ones((50, 50, 256, 256), chunks=(5, 5, 256, 256), dtype='uint8') s = signals.Signal2D(ar).as_lazy() s._make_lazy(rechunk=True) assert s.data.dtype is np.dtype('uint8') chunks_old = s.data.chunks s.change_dtype('float', rechunk=False) assert s.data.dtype is np.dtype('float') assert chunks_old == s.data.chunks
def test_plot_contrast_editor(gamma, saturated_pixels): np.random.seed(1) data = np.random.random(size=(10, 10, 100, 100)) * 1000 data += np.arange(10 * 10 * 100 * 100).reshape((10, 10, 100, 100)) s = signals.Signal2D(data) s.plot(gamma=gamma, saturated_pixels=saturated_pixels) ceditor = ImageContrastEditor(s._plot.signal_plot) assert ceditor.gamma == gamma assert ceditor.saturated_pixels == saturated_pixels return plt.gcf()
def test_plot_contrast_editor(gamma, percentile): np.random.seed(1) data = np.random.random(size=(10, 10, 100, 100)) * 1000 data += np.arange(10 * 10 * 100 * 100).reshape((10, 10, 100, 100)) s = signals.Signal2D(data) s.plot(gamma=gamma, vmin=percentile[0], vmax=percentile[1]) ceditor = ImageContrastEditor(s._plot.signal_plot) assert ceditor.gamma == gamma assert ceditor.vmin_percentile == float(percentile[0].split("th")[0]) assert ceditor.vmax_percentile == float(percentile[1].split("th")[0]) return plt.gcf()
def setup_method(self, method): np.random.seed(1) # Use prime numbers to avoid fluke equivalences # create 3 random clusters n_samples = [250, 100, 50] std = [1.0, 2.0, 0.5] X = [] centers = np.array([[-15.0, -15.0, -15.0], [1.0, 1.0, 1.0], [15.0, 15.0, 15.0]]) for i, (n, std) in enumerate(zip(n_samples, std)): X.append(centers[i] + np.random.normal(scale=std, size=(n, 3))) data = np.concatenate(X) # nav1, sig1 s = signals.Signal1D(data.reshape(400, 3)) # nav2, sig1 s2 = signals.Signal1D(data.reshape(40, 10, 3)) # Run decomposition and cluster analysis s.decomposition() s.cluster_analysis("decomposition", n_clusters=3, algorithm='kmeans', preprocessing="minmax", random_state=0) s.estimate_number_of_clusters("decomposition", metric="elbow") s2.decomposition() s2.cluster_analysis("decomposition", n_clusters=3, algorithm='kmeans', preprocessing="minmax", random_state=0) data = np.zeros((2000, 5)) data[:250 * 5:5, :] = 10 data[2 + 250 * 5:350 * 5:5, :] = 2 data[350 * 5:400 * 5, 4] = 20 # nav2, sig2 s3 = signals.Signal2D(data.reshape(20, 20, 5, 5)) s3.decomposition() s3.cluster_analysis("decomposition", n_clusters=3, algorithm='kmeans', preprocessing="minmax", random_state=0) self.s = s self.s2 = s2 self.s3 = s3
def test_plot_contrast_editor_norm(norm): np.random.seed(1) data = np.random.random(size=(100, 100)) * 1000 data += np.arange(100 * 100).reshape((100, 100)) s = signals.Signal2D(data) s.plot(norm=norm) ceditor = ImageContrastEditor(s._plot.signal_plot) if norm == "log": # test log with negative numbers s2 = s - 5E3 s2.plot(norm=norm) _ = ImageContrastEditor(s._plot.signal_plot) assert ceditor.norm == norm.capitalize()
def test_lazy_changetype_rechunk(): ar = da.ones((50, 50, 256, 256), chunks=(5, 5, 256, 256), dtype='uint8') s = signals.Signal2D(ar).as_lazy() s._make_lazy(rechunk=True) assert s.data.dtype is np.dtype('uint8') chunks_old = s.data.chunks s.change_dtype('float') assert s.data.dtype is np.dtype('float') chunks_new = s.data.chunks assert (len(chunks_old[0]) * len(chunks_old[1]) < len(chunks_new[0]) * len(chunks_new[1])) s.change_dtype('uint8') assert s.data.dtype is np.dtype('uint8') chunks_newest = s.data.chunks assert chunks_newest == chunks_new
def test_plot_constrast_editor_apply(): np.random.seed(1) data = np.random.random(size=(100, 100)) * 1000 data += np.arange(100 * 100).reshape((100, 100)) s = signals.Signal2D(data) s.plot() ceditor = ImageContrastEditor(s._plot.signal_plot) ceditor.span_selector.extents = (3E3, 5E3) ceditor._update_image_contrast() image_vmin_vmax = ceditor.image._vmin, ceditor.image._vmax ceditor.apply() assert not ceditor.span_selector.visible assert not ceditor._is_selector_visible np.testing.assert_allclose( (ceditor.image._vmin, ceditor.image._vmax), image_vmin_vmax, )
def test_plot_constrast_editor_reset(): np.random.seed(1) data = np.random.random(size=(100, 100)) * 1000 data += np.arange(100 * 100).reshape((100, 100)) s = signals.Signal2D(data) s.plot() ceditor = ImageContrastEditor(s._plot.signal_plot) ceditor.span_selector.extents = (3E3, 5E3) ceditor._update_image_contrast() vmin, vmax = 2.1143748173448866, 10988.568946171192 np.testing.assert_allclose(ceditor._vmin, vmin) np.testing.assert_allclose(ceditor._vmax, vmax) np.testing.assert_allclose(ceditor._get_current_range(), (3E3, 5E3)) ceditor.reset() assert not ceditor.span_selector.visible assert not ceditor._is_selector_visible np.testing.assert_allclose(ceditor._get_current_range(), (vmin, vmax)) np.testing.assert_allclose(ceditor.image._vmin, vmin) np.testing.assert_allclose(ceditor.image._vmax, vmax)
def test_plot_constrast_editor_auto_indices_changed(): np.random.seed(1) data = np.random.random(size=(10, 10, 100, 100)) * 1000 data += np.arange(10 * 10 * 100 * 100).reshape((10, 10, 100, 100)) s = signals.Signal2D(data) s.plot() ceditor = ImageContrastEditor(s._plot.signal_plot) ceditor.span_selector.extents = (3E3, 5E3) ceditor.update_span_selector_traits() s.axes_manager.indices = (0, 1) # auto is None by default, the span selector need to be removed: assert not ceditor.span_selector.visible assert not ceditor._is_selector_visible ref_value = (100045.29840954322, 110988.10581608873) np.testing.assert_allclose(ceditor._get_current_range(), ref_value) # Change auto to False ceditor.auto = False s.axes_manager.indices = (0, 2) # vmin, vmax shouldn't have changed np.testing.assert_allclose(ceditor._get_current_range(), ref_value)
def test_plot_constrast_editor_setting_changed(): # Test that changing setting works np.random.seed(1) data = np.random.random(size=(100, 100)) * 1000 data += np.arange(100 * 100).reshape((100, 100)) s = signals.Signal2D(data) s.plot() ceditor = ImageContrastEditor(s._plot.signal_plot) ceditor.span_selector.extents = (3E3, 5E3) ceditor.update_span_selector_traits() np.testing.assert_allclose(ceditor.ss_left_value, 3E3) np.testing.assert_allclose(ceditor.ss_right_value, 5E3) assert ceditor.auto # Do a cycle to trigger traits changed ceditor.auto = False assert not ceditor.auto ceditor.auto = True # reset and clear span selector assert ceditor.auto assert not ceditor.span_selector.visible assert not ceditor._is_selector_visible assert not ceditor.line.line.get_visible() ceditor.span_selector.extents = (3E3, 5E3) ceditor.span_selector.set_visible(True) ceditor.update_line() assert ceditor._is_selector_visible assert ceditor.line.line.get_visible() assert ceditor.bins == 24 assert ceditor.line.axis.shape == (ceditor.bins, ) ceditor.bins = 50 assert ceditor.bins == 50 assert ceditor.line.axis.shape == (ceditor.bins, ) # test other parameters ceditor.linthresh = 0.1 assert ceditor.image.linthresh == 0.1 ceditor.linscale = 0.5 assert ceditor.image.linscale == 0.5
def compareSaveLoad(self, testShape, dtype='int8', compressor=None, clevel=1, lazy=False, do_async=False, **kwargs): # This is the main function which reads and writes from disk. mrcName = os.path.join(tmpDir, "testMage_{}_lazy_{}.mrcz".format(dtype, lazy)) dtype = np.dtype(dtype) if dtype == 'float32' or dtype == 'float64': testData = np.random.normal(size=testShape).astype(dtype) elif dtype == 'complex64' or dtype == 'complex128': testData = np.random.normal(size=testShape).astype( dtype) + 1.0j * np.random.normal(size=testShape).astype(dtype) else: # integers testData = np.random.randint(10, size=testShape).astype(dtype) testSignal = signals.Signal2D(testData) if lazy: testSignal = testSignal.as_lazy() # Unfortunately one cannot iterate over axes_manager in a Pythonic way # for axis in testSignal.axes_manager: testSignal.axes_manager[0].name = 'z' testSignal.axes_manager[0].scale = np.random.uniform(low=0.0, high=1.0) testSignal.axes_manager[0].units = 'nm' testSignal.axes_manager[1].name = 'x' testSignal.axes_manager[1].scale = np.random.uniform(low=0.0, high=1.0) testSignal.axes_manager[1].units = 'nm' testSignal.axes_manager[2].name = 'y' testSignal.axes_manager[2].scale = np.random.uniform(low=0.0, high=1.0) testSignal.axes_manager[2].units = 'nm' # Meta-data that goes into MRC fixed header testSignal.metadata.set_item('Acquisition_instrument.TEM.beam_energy', 300.0) # Meta-data that goes into JSON extended header testSignal.metadata.set_item( 'Acquisition_instrument.TEM.magnification', 25000) testSignal.metadata.set_item( 'Signal.Noise_properties.Variance_linear_model.gain_factor', 1.0) save(mrcName, testSignal, compressor=compressor, clevel=clevel, do_async=do_async, **kwargs) if do_async: # Poll file on disk since we don't return the # concurrent.futures.Future t_stop = perf_counter() + MAX_ASYNC_TIME sleep(0.005) while (perf_counter() < t_stop): try: fh = open(mrcName, 'a') fh.close() break except IOError: sleep(0.001) print("Time to save file: {} s".format(perf_counter() - (t_stop - MAX_ASYNC_TIME))) sleep(0.1) reSignal = load(mrcName) try: os.remove(mrcName) except IOError: print("Warning: file {} left on disk".format(mrcName)) npt.assert_array_almost_equal(testSignal.data.shape, reSignal.data.shape) npt.assert_array_almost_equal(testSignal.data, reSignal.data) npt.assert_almost_equal( testSignal.metadata.Acquisition_instrument.TEM.beam_energy, reSignal.metadata.Acquisition_instrument.TEM.beam_energy) npt.assert_almost_equal( testSignal.metadata.Acquisition_instrument.TEM.magnification, reSignal.metadata.Acquisition_instrument.TEM.magnification) for aName in ['x', 'y', 'z']: npt.assert_equal(testSignal.axes_manager[aName].size, reSignal.axes_manager[aName].size) npt.assert_almost_equal(testSignal.axes_manager[aName].scale, reSignal.axes_manager[aName].scale) if dtype == 'complex64': assert isinstance(reSignal, signals.ComplexSignal2D) else: assert isinstance(reSignal, signals.Signal2D) assert_deep_almost_equal(testSignal.axes_manager.as_dictionary(), reSignal.axes_manager.as_dictionary()) assert_deep_almost_equal(testSignal.metadata.as_dictionary(), reSignal.metadata.as_dictionary()) return reSignal
def setup_method(self, method): self.signal = signals.Signal2D(np.arange(24).reshape(6, 4)) self.data = self.signal.data.copy()
from hyperspy import signals from hyperspy.misc.machine_learning import import_sklearn sklearn = pytest.importorskip("sklearn", reason="sklearn not installed") if import_sklearn.sklearn_installed: # Create the data once, since the parametrizations # will repeat the decomposition and BSS unnecessarily rng1 = np.random.RandomState(123) signal1 = signals.Signal1D(rng1.uniform(size=(7, 5, 7))) signal1.decomposition() signal1.blind_source_separation(number_of_components=3) rng2 = np.random.RandomState(123) signal2 = signals.Signal2D(rng2.uniform(size=(7, 5, 7))) signal2.decomposition() signal2.blind_source_separation(number_of_components=3) else: # No need to create the data, since BSS will fail # if sklearn is missing, and the pytest.importorskip # will skip the rest of the file anyway pass class TestCluster1D: def setup_method(self): self.signal = signal1.deepcopy() self.navigation_mask = np.zeros((7, 5), dtype=bool) self.navigation_mask[4:6, 1:4] = True self.signal_mask = np.zeros((7,), dtype=bool)
def setUp(self): self.signal = signals.Signal2D(np.arange(24).reshape(6, 4)) self.data = self.signal.data.copy()