def test_integration(): x_domain = np.linspace(0, 10, 100) f = Function(x_domain, lambda x: 6 * x) F = Integral.from_function(f) F2 = Function(x_domain, lambda x: 3 * x**2) assert_equal(F, F2)
def plot(self, name, f: Function, **kwargs): self.axs.plot(f.get_domain(), f.eval(), label=name, **self._plot_args, **kwargs) self.axs.set_xlabel("depth [$\AA$]") self.axs.set_ylabel(self._ylabel) self.axs.legend() return self
def abstract_test_integration(domain): x_domain = domain f = Function(x_domain, lambda x: 6 * x) F = Integral.from_function(f) F2 = Function(x_domain, lambda x: 3 * x**2) assert_equal(F, F2)
def test_oversample_does_not_change_function(): f = Function(Domain(-10, 10, 100), lambda x: x**2) fexact = Function(Domain(-10, 10, 1000), lambda x: x**2) fnew = f.oversample(10) assert_equal(fexact, fnew)
def test_addition_with_constant(): f1 = Function(np.linspace(0, 10, 100), lambda x: np.sin(x)) f2 = Function(np.linspace(0, 10, 100), lambda x: np.sin(x) + 5) f = f1 + 5 assert_equal(f, f2)
def test_absolute_value_stiched(): fl = Function(np.linspace(-10, 0, 50), lambda x: -x) fr = Function(np.linspace(0, 10, 100), lambda x: x) f = StitchedFunction.from_functions(fl, fr) fabs = Function(np.linspace(-10, 10, 200), lambda x: abs(x)) assert_equal(f, fabs)
def test_oversample_domain(): f = Function(Domain.linear(-10, 10, 100), lambda x: x**2) #randint = np.random.randint(1, 100) randint = 2 fnew = f.oversample(randint) np.testing.assert_array_equal(fnew.get_domain(), Domain(-10, 10, randint * 100 + 1).get())
def test_strechted_exponential(): x_domain = np.linspace(0, 10, 50000) f = Function(x_domain, lambda x: np.exp(-np.sqrt(x))) F = Integral.from_function(f, 0) F_exact = Function( x_domain, lambda x: -2 * np.exp(-np.sqrt(x)) * (1 + np.sqrt(x)) + 2) assert_equal(F, F_exact, TOL=1e-6)
def test_linearity(): x_domain = np.linspace(0, 10, 1000) f1 = Function(x_domain, lambda x: 1) f2 = Function(x_domain, lambda x: 2 * x) f = f1 + f2 F = Integral.from_function(f) f3 = Function(x_domain, lambda x: x + x**2) assert_equal(F, f3)
def to_function(cls, domain, feval, x_err=None, y_err=None): f = Function.to_function(domain, feval) if x_err is not None: x_err = Function.to_function(domain, x_err) if y_err is not None: y_err = Function.to_function(domain, y_err) return cls(f, x_err, y_err)
def function_to_sin(fctn: Function): psi = fctn.get_domain() theta = fctn.eval() sinsqrd = np.fromiter(map(lambda x: np.sin(np.deg2rad(x)) ** 2, psi), float) dy = fctn.dy if dy is not None: dy = Function.to_function(sinsqrd, dy.eval()) return Function.to_function(sinsqrd, theta, dy=dy)
def test_subtraction(): f1 = Function(np.linspace(0, 10, 100), lambda x: 5 * x + 1) f2 = Function(np.linspace(0, 20, 100), lambda x: 4 * x) f3 = Function(np.linspace(0, 10, 100), lambda x: x + 1) f = f1 - f2 assert np.all(f.get_domain() == f1.get_domain()) assert_equal(f, f3)
def test_power_with_function(): f1 = Function(np.linspace(0, 10, 100), lambda x: x**2) f2 = Function(np.linspace(0, 10, 100), lambda x: 0.5) f3 = Function(np.linspace(0, 10, 100), lambda x: x) f4 = Function(np.linspace(0, 10, 100), lambda x: 0.5**x) assert_equal(f1**f2, f3) assert_equal(f2**f3, f4)
def test_exp(): domain = np.linspace(-5, 5, 100) w_space = np.linspace(-10, 10, 1000) a = 2 f = Function.to_function(domain, lambda x: np.exp(-a * x ** 2)) F_analytical = Function.to_function(w_space, lambda x: np.sqrt(np.pi / a) * np.exp(-x ** 2 / (4 * a))) F = FourierTransform.from_function(w_space, f) assert_equal(F, F_analytical)
def test_addition(): f1 = Function(np.linspace(0, 10, 100), lambda x: np.sin(x)**2) f2 = Function(np.linspace(0, 20, 100), lambda x: np.cos(x)**2) f3 = Function(np.linspace(0, 10, 100), lambda x: 1) f = f1 + f2 assert np.all(f.get_domain() == f1.get_domain()) assert_equal(f, f3)
def _to_sin_sqr(self, fctn): psi = fctn.get_domain() theta = fctn.eval() sinsqrd = np.fromiter(map(lambda x: np.sin(np.deg2rad(x))**2, psi), float) dy = fctn.dy if not dy.is_null(): dy = Function.to_function(sinsqrd, dy.eval()) return Function.to_function(sinsqrd, theta, dy=dy)
def from_file(self): from numpy import loadtxt q, R, dq, dR = loadtxt(self._file).T f = Function.to_function(q, R) df = Function.to_function(q, dR) dx = Function.to_function(q, dq) f.set_dy(df) f.set_dx(dx) return f
def test_absolute_value(): fl = Function(np.linspace(-10, 0, 50), lambda x: -x) fr = Function(np.linspace(0, 10, 100), lambda x: x) f = PiecewiseFunction.from_function(np.linspace(-10, 10, 200), fl, lambda x: x <= 0, fr) fabs = Function(np.linspace(-10, 10, 200), lambda x: abs(x)) assert_equal(f, fabs)
def from_file(self): from numpy import loadtxt q, dq, R, dR = loadtxt(self._file, usecols=(0, 1, 2, 3)).T f = Function.to_function(q, R) dx = Function.to_function(q, dq) dy = Function.to_function(q, dR) f.set_dy(dy) f.set_dx(dx) return f
def test_compare_InverseFourier_matrix(): x_space = np.linspace(-10, 10, 1000) w_space = np.linspace(-10, 10, 1000) fun = Function(x_space, lambda x: np.exp(-x**2 / 2)) f = FourierTransform.from_function(w_space, fun) trafo = invfourier_matrix(x_space, w_space) fun2 = Function.to_function(w_space, np.dot(trafo, f(x_space))) assert_equal(fun, fun2)
def test_compare_matrix_with_fourier_function(): x_space = np.linspace(-10, 10, 1000) fun = Function(x_space, lambda x: np.exp(-x**2 / 2)) w_space = np.linspace(-10, 10, 1000) f1 = FourierTransform.from_function(w_space, fun) trafo = fourier_matrix(x_space, w_space) f2 = Function.to_function(w_space, np.dot(trafo, fun(x_space))) assert_equal(f1, f2, 1e-14)
def from_function(cls, function: Function): dy = function.dy if dy is None: return function value = numpy.random.normal(function.eval().real, dy.eval().real) if function.is_complex(): value = value + 1j * numpy.random.normal(function.eval().imag, dy.eval().imag) return Function.to_function(function.get_dom(), value)
def test_identity_matrix(): x_space = np.linspace(-10, 10, 1000) w_space = np.linspace(-10, 10, 1000) fun = Function(x_space, lambda x: np.exp(-x**2 / 2)) trafo = fourier_matrix(x_space, w_space) f = Function.to_function(w_space, np.dot(trafo, fun(x_space))) trafo = invfourier_matrix(w_space, x_space) fun2 = Function.to_function(x_space, np.dot(trafo, f(w_space))) assert_equal(fun, fun2)
def test_absolute_value(): f1 = Function(np.linspace(0, 10, 100), lambda x: np.exp(1j * x)) f2 = Function(np.linspace(0, 10, 100), lambda x: 1) f3 = Function(f1.get_domain(), lambda x: -x) f4 = Function(f1.get_domain(), lambda x: x) assert_equal(f1.abs(), f2) assert_equal(f3.abs(), f4)
def remesh(self, interpolation=1, interpolation_kind=None): """ Re-meshes the loaded data onto a common grid by interpolation. Usually, the reflectivity data is not measured at the very same q points. Also the min/max range of the q values might vary. But, to reconstruct the phase, the reflectivity needs to be measured at the same q values. This method achieves this goal. The new grid is the coarsest possible grid. The min/max values are chosen such that every reflectivity measurement contains the min/max values. :param interpolation: integer number. Defines the number of additional interpolations between two q-grid points :param interpolation_kind: interpolation of the function between the point (linear/quadratic/etc..) See scipy.interp1d for possible values :return: None """ # coarsest possible grid qmin = max([ms['Qin'][0] for ms in self._measurements]) qmax = min([ms['Qin'][-1] for ms in self._measurements]) npts = min([len(ms['Qin']) for ms in self._measurements]) new_mesh = np.linspace(qmin, qmax, npts + 1) for measurement in self._measurements: q, R, dR = measurement['Qin'], measurement['Rin'], measurement[ 'dRin'] try: from skipi.function import Function f = Function.to_function( q, R, interpolation=interpolation_kind).remesh( new_mesh).oversample(interpolation) if dR is not None: df = Function.to_function( q, dR, interpolation=interpolation_kind).remesh( new_mesh).oversample(interpolation) dR = df.eval() measurement['Qin'], measurement['Rin'], measurement[ 'dRin'] = f.get_domain(), f.eval(), dR except: # Fallback if skipi is not available q, R = remesh([q, R], qmin, qmax, npts, left=0, right=0) if dR is not None: q, dR = remesh([q, dR], qmin, qmax, npts, left=0, right=0) measurement['Qin'], measurement['Rin'], measurement[ 'dRin'] = q, R, dR
def merge_group(self, group): multi_merge = MultiMerger(MeasurementMerger()) merged = {} for psi, mss in group.items(): merge = multi_merge.merge(mss) merge = ErrorCalculation().manipulate( merge, self._c._settings_controller.get_measurement_context()) theta, dtheta, counts, dcounts = zip(*merge.get_data()) theta = np.array(theta) dx = Function.to_function(theta, dtheta) dy = Function.to_function(theta, dcounts) merged[psi] = Function.to_function(theta, counts, dx=dx, dy=dy) return merged
def plot(self, name, f: Function, f0: Function, f1: Function = None, **kwargs): x = f.get_domain() y = f.eval() kwargs['label'] = name if 'color' in kwargs: self._plot_args['color'] = kwargs['color'] self._axs.fill_between(x, y, f0(x), **self._plot_args) if f1 is not None: self._axs.fill_between(x, y, f1(x), **self._plot_args) super(FillBetweenPlotter, self).plot(name, f, **kwargs) return self
def test_linearity(): x_space = np.linspace(0, 10, 10) w_space = np.linspace(-5, 5, 1000) f1 = Function(x_space, lambda x: 3 * x) f2 = Function(x_space, lambda x: -2 * x) f3 = Function(x_space, lambda x: x) F1 = FourierTransform.from_function(w_space, f1) F2 = FourierTransform.from_function(w_space, f2) F3 = FourierTransform.from_function(w_space, f3) F3p = FourierTransform.from_function(w_space, (f1 + f2)) assert_equal(F1 + F2, F3) assert_equal(F3, F3p)
def from_functions(cls, functions: [Function], domain=None, avg_fun=None): if domain is None: domain = functions[0].get_domain() if avg_fun is None: avg_fun = cls.avg return Function.to_function(domain, lambda x: avg_fun([f(x) for f in functions]))
def to_file(self, function: Function): transform = PhasePlotter.transform() f = function.transform(transform) if function.dy is not None: f.set_dy(function.dy.transform(transform)) header = "q, (100 q)**2 Re R(q), (100 q)**2 Im R(q)" return super(ReflectionFileLoader, self).to_file(f, header=header)