def test_function(): g = SkewNormal() g.A.value = 2.5 g.x0.value = 0 g.scale.value = 1 g.shape.value = 0 assert_allclose(g.function(0), 1, rtol=3e-3) assert_allclose(g.function(6), 1.52e-8, rtol=1e-3) g.A.value = 5 g.x0.value = 4 g.scale.value = 3 g.shape.value = 2 assert_allclose(g.function(0), 6.28e-3, rtol=1e-3) assert_allclose(g.function(g.mean), 2.855, rtol=1e-3)
def test_estimate_parameters_binned(only_current, binned, lazy, uniform): s = Signal1D(np.empty((300,))) s.axes_manager.signal_axes[0].is_binned = binned axis = s.axes_manager.signal_axes[0] axis.scale = 0.2 axis.offset = -10 g1 = SkewNormal(A=2, x0=2, scale=10, shape=5) s.data = g1.function(axis.axis) if not uniform: axis.convert_to_non_uniform_axis() if lazy: s = s.as_lazy() g2 = SkewNormal() if binned and uniform: factor = axis.scale elif binned: factor = np.gradient(axis.axis) else: factor = 1 assert g2.estimate_parameters(s, axis.low_value, axis.high_value, only_current=only_current) assert g2._axes_manager[-1].is_binned == binned np.testing.assert_allclose(g1.A.value, g2.A.value * factor) assert abs(g2.x0.value - g1.x0.value) <= 0.002 assert abs(g2.shape.value - g1.shape.value) <= 0.01 assert abs(g2.scale.value - g1.scale.value) <= 0.01
def test_function_nd(binned, lazy): s = Signal1D(np.empty((300,))) axis = s.axes_manager.signal_axes[0] axis.scale = 0.2 axis.offset = -10 g1 = SkewNormal(A=2, x0=2, scale=10, shape=5) s.data = g1.function(axis.axis) s.metadata.Signal.binned = binned s2 = stack([s] * 2) if lazy: s2 = s2.as_lazy() g2 = SkewNormal() factor = axis.scale if binned else 1 assert g2.estimate_parameters(s2, axis.low_value, axis.high_value, False) assert g2.binned == binned assert_allclose(g2.function_nd(axis.axis) * factor, s2.data, 0.06)
def test_estimate_parameters_binned(only_current, binned, lazy): s = Signal1D(np.empty((300,))) s.metadata.Signal.binned = binned axis = s.axes_manager.signal_axes[0] axis.scale = 0.2 axis.offset = -10 g1 = SkewNormal(A=2, x0=2, scale=10, shape=5) s.data = g1.function(axis.axis) if lazy: s = s.as_lazy() g2 = SkewNormal() factor = axis.scale if binned else 1 assert g2.estimate_parameters(s, axis.low_value, axis.high_value, only_current=only_current) assert g2.binned == binned assert_allclose(g1.A.value, g2.A.value * factor) assert abs(g2.x0.value - g1.x0.value) <= 0.002 assert abs(g2.shape.value - g1.shape.value) <= 0.01 assert abs(g2.scale.value - g1.scale.value) <= 0.01
def test_fit(A=1, x0=0, shape=1, scale=1, noise=0.01): """ Creates a simulated noisy skew normal distribution based on the input parameters and fits a skew normal component to this data. """ # create skew normal signal and add noise g = SkewNormal(A=A, x0=x0, scale=scale, shape=shape) x = np.arange(x0 - scale * 3, x0 + scale * 3, step=0.01 * scale) s = Signal1D(g.function(x)) s.axes_manager.signal_axes[0].axis = x s.add_gaussian_noise(std=noise * A) # fit skew normal component to signal g2 = SkewNormal() m = s.create_model() m.append(g2) g2.x0.bmin = x0 - scale * 3 # prevent parameters to run away g2.x0.bmax = x0 + scale * 3 g2.x0.bounded = True m.fit(bounded=True) m.print_current_values() # print out parameter values m.plot() # plot fit return m