def test_estimate_parameters_binned(): s = Spectrum(np.empty((100,))) axis = s.axes_manager.signal_axes[0] axis.scale = 2. axis.offset = -30 g1 = GaussianHF(50015.156, 23, 10) s.data = g1.function(axis.axis) s.metadata.Signal.binned = True g2 = GaussianHF() g2.estimate_parameters(s, axis.low_value, axis.high_value, True) nt.assert_almost_equal( g1.height.value / axis.scale, g2.height.value) nt.assert_almost_equal(g2.centre.value, g1.centre.value, delta=1e-3) nt.assert_almost_equal(g2.fwhm.value, g1.fwhm.value, delta=0.1)
def test_integral_as_signal(): s = Spectrum(np.zeros((2, 3, 100))) g1 = GaussianHF(fwhm=3.33, centre=20.) h_ref = np.linspace(0.1, 3.0, s.axes_manager.navigation_size) for d, h in zip(s._iterate_signal(), h_ref): g1.height.value = h d[:] = g1.function(s.axes_manager.signal_axes[0].axis) m = s.create_model() g2 = GaussianHF() m.append(g2) g2.estimate_parameters(s, 0, 100, True) m.multifit() s_out = g2.integral_as_signal() ref = (h_ref * 3.33 * sqrt2pi / sigma2fwhm).reshape(s_out.data.shape) np.testing.assert_almost_equal( s_out.data, ref)
class Test2D(): def setUp(self): self.s = Spectrum(np.random.random((2, 3))) def test_to_image(self): im = self.s.to_image() assert_true(isinstance(im, Image)) assert_equal(im.data.shape, self.s.data.T.shape) assert_true(im.data.flags["C_CONTIGUOUS"])
class Test2D: def setUp(self): self.s = Spectrum(np.random.random((2, 3))) def test_to_image(self): im = self.s.to_image() nose.tools.assert_true(isinstance(im, Image)) nose.tools.assert_equal(im.data.shape, self.s.data.T.shape) nose.tools.assert_true(im.data.flags["C_CONTIGUOUS"])
class Test4D(): def setUp(self): self.s = Spectrum(np.random.random((2, 3, 4, 5))) def test_to_image(self): im = self.s.to_image() assert_true(isinstance(im, Image)) assert_equal(im.data.shape, (5, 2, 3, 4)) assert_true(im.data.flags["C_CONTIGUOUS"])
def setUp(self): np.random.seed(0) # Same random every time, Line2DROi test requires it self.s_s = Spectrum(np.random.rand(50, 60, 4)) self.s_s.axes_manager[0].scale = 5 self.s_s.axes_manager[0].units = 'nm' self.s_s.axes_manager[1].scale = 5 self.s_s.axes_manager[1].units = 'nm' # 4D dataset self.s_i = Image(np.random.rand(100, 100, 4, 4))
class Test4D(): def setUp(self): self.s = Spectrum(np.random.random((2, 3, 4, 5))) def test_to_image(self): im = self.s.to_image() nose.tools.assert_true(isinstance(im, Image)) nose.tools.assert_equal(im.data.shape, (5, 2, 3, 4)) nose.tools.assert_true(im.data.flags["C_CONTIGUOUS"])
def setUp(self): self.s = Spectrum(np.random.random((2, 3)))
def setUp(self): ics = np.random.laplace(size=(3, 1000)) np.random.seed(1) mixing_matrix = np.random.random((100, 3)) self.s = Spectrum(np.dot(mixing_matrix, ics)) self.s.decomposition()
class TestBSS1D: def setUp(self): ics = np.random.laplace(size=(3, 1000)) np.random.seed(1) mixing_matrix = np.random.random((100, 3)) self.s = Spectrum(np.dot(mixing_matrix, ics)) self.s.decomposition() def test_on_loadings(self): if not sklearn_installed: raise SkipTest self.s.blind_source_separation( 3, diff_order=0, fun="exp", on_loadings=False) s2 = self.s.as_spectrum(0) s2.decomposition() s2.blind_source_separation( 3, diff_order=0, fun="exp", on_loadings=True) nose.tools.assert_true(are_bss_components_equivalent( self.s.get_bss_factors(), s2.get_bss_loadings())) def test_mask_diff_order_0(self): if not sklearn_installed: raise SkipTest # This test, unlike most other tests, either passes or raises an error. # It is designed to test if the mask is correctly dilated inside the # `blind_source_separation_method`. If the mask is not correctely # dilated the nan in the loadings should raise an error. mask = self.s._get_signal_signal(dtype="bool") mask[5] = True self.s.learning_results.factors[5, :] = np.nan self.s.blind_source_separation(3, diff_order=0, mask=mask) def test_mask_diff_order_1(self): if not sklearn_installed: raise SkipTest # This test, unlike most other tests, either passes or raises an error. # It is designed to test if the mask is correctly dilated inside the # `blind_source_separation_method`. If the mask is not correctely # dilated the nan in the loadings should raise an error. mask = self.s._get_signal_signal(dtype="bool") mask[5] = True self.s.learning_results.factors[5, :] = np.nan self.s.blind_source_separation(3, diff_order=1, mask=mask) def test_mask_diff_order_0_on_loadings(self): if not sklearn_installed: raise SkipTest # This test, unlike most other tests, either passes or raises an error. # It is designed to test if the mask is correctly dilated inside the # `blind_source_separation_method`. If the mask is not correctely # dilated the nan in the loadings should raise an error. mask = self.s._get_navigation_signal(dtype="bool") mask[5] = True self.s.learning_results.loadings[5, :] = np.nan self.s.blind_source_separation(3, diff_order=0, mask=mask, on_loadings=True) def test_mask_diff_order_1_on_loadings(self): if not sklearn_installed: raise SkipTest # This test, unlike most other tests, either passes or raises an error. # It is designed to test if the mask is correctly dilated inside the # `blind_source_separation_method`. If the mask is not correctely # dilated the nan in the loadings should raise an error. mask = self.s._get_navigation_signal(dtype="bool") mask[5] = True self.s.learning_results.loadings[5, :] = np.nan self.s.blind_source_separation(3, diff_order=1, mask=mask, on_loadings=True)