def test_gauss_norm(): """Test 1d gaussian """ p = np.array([0., 0., 1., -30., 3., 1., 30., 3.]) x = np.linspace(-1e6, 1e6, 8e6 + 1) y = functions.gaussian(p, x) integ = simps(y, x) assert (abs(integ - 2.) < 1e-5)
def test_voigt_fit_of_gauss_rand(): """Test voigt fit of gaussian """ p = np.array([0., 0., 3., 0., 0.3]) x = np.linspace(-1, 1, 201) y = functions.gaussian(p, x) + np.random.normal(loc=0., scale=0.1, size=201) err = np.sqrt(np.abs(y)) fitobj = Fitter(residuals=residuals, data=(functions.voigt, x, y, err)) fitobj.parinfo = [{'fixed': fix} for fix in np.asarray([0, 0, 0, 0, 0, 0]).astype(bool)] fitobj.fit(params0=np.concatenate((p, np.array([0.2])))) assert ((fitobj.chi2_min < 5.))
def test_gauss_fit_of_gauss_rand(): """Test gaussian fit """ p = np.array([0., 0., 3., 0., 0.3]) x = np.linspace(-1, 1, 201) np.random.seed(0) y = functions.gaussian(p, x) + np.random.normal(loc=0., scale=0.05, size=201) err = np.sqrt(np.abs(y)) fitobj = Fitter(residuals=residuals, data=(functions.gaussian, x, y, err)) fitobj.parinfo = [{'fixed': fix} for fix in np.asarray([0, 0, 0, 0, 0]).astype(bool)] fitobj.fit(params0=p) assert ((fitobj.chi2_min < 5.))
def test_voigt_fit_of_gauss_rand(): """Test voigt fit of gaussian """ p = np.array([0., 0., 3., 0., 0.3]) x = np.linspace(-1, 1, 201) np.random.seed(0) y = functions.gaussian(p, x) + np.random.normal( loc=0., scale=0.1, size=201) err = np.sqrt(np.abs(y)) fitobj = Fitter(residuals=residuals, data=(functions.voigt, x, y, err)) fitobj.parinfo = [{ 'fixed': fix } for fix in np.asarray([0, 0, 0, 0, 0, 0]).astype(bool)] fitobj.fit(params0=np.concatenate((p, np.array([0.2])))) assert ((fitobj.chi2_min < 5.))
def test_rebin(): """Tests data rebinning """ data = Data(h=np.linspace(0, 1, 101), k=0, l=0, e=0, temp=0, detector=functions.gaussian([0, 0, 10, 0.5, 0.5], np.linspace(0, 1, 101)), monitor=np.ones(101), time=np.ones(101)) data_bin = data.bin(dict(h=[0, 1., 51], k=[-0.1, 0.1, 1], l=[-0.1, 0.1, 1], e=[-0.5, 0.5, 1])) assert (data_bin.Q.shape[0] == 51) assert (data_bin.monitor.shape[0] == 51) assert (data_bin.detector.shape[0] == 51) assert (np.average(data_bin.monitor) == np.average(data.monitor)) assert (abs(simps(data_bin.detector, data_bin.Q[:, 0]) - simps(data.detector, data.Q[:, 0])) <= 0.1) assert (np.abs(data_bin.integrate() - data.integrate()) < 1e-1) assert (np.abs(data_bin.position()[0] - data.position()[0]) < 1e-1) assert (np.abs(data_bin.width()[0] - data.width()[0]) < 1e-1) def _test(): data_bin = data.bin(dict(blah=[1, 2, 4])) with pytest.raises(KeyError): _test()
def test_analysis(): """Tests analysis methods """ x = np.linspace(-2, 2, 100) y = functions.gaussian([0, 0, 1, 0, 0.5], x) data = Data(Q=np.vstack((item.ravel() for item in np.meshgrid(x, 0., 0., 4., 300.))).T, detector=y, monitor=np.full(x.shape, 1, dtype=float), time=np.full(x.shape, 1, dtype=float)) assert (np.abs(data.integrate() - 1) < 1e-5) assert (np.abs(data.position()[0]) < 1e-5) assert (abs(data.width(fwhm=True)[0] - 0.5) < 1e-1) assert isinstance(data.position(hkle=False), dict) assert isinstance(data.width(hkle=False), dict) assert (np.abs(data.integrate(hkle=False) - 1) < 1e-5) assert (np.abs(data.position(hkle=False)['h']) < 1e-5) assert (abs(data.width(fwhm=True, hkle=False)['h'] - 0.5) < 1e-1) bounds = (data.h >= -1) & (data.h <= 1) assert (np.abs(data.integrate(bounds=bounds) - 1) < 1e-5) assert (np.abs(data.position(bounds=bounds)[0]) < 1e-5) assert (abs(data.width(bounds=bounds, fwhm=True)[0] - 0.5) < 1e-1) background = dict(type='constant', value=0.) assert (np.abs(data.integrate(background=background) - 1) < 1e-5) assert (np.abs(data.position(background=background)[0]) < 1e-5) assert (abs(data.width(background=background, fwhm=True)[0] - 0.5) < 1e-1) background = dict(type='percent', value=2) assert (np.abs(data.integrate(background=background) - 1) < 1e-5) background = dict(type='minimum') assert (np.abs(data.integrate(background=background) - 1) < 1e-5) background = dict(type='blah') assert (np.abs(data.integrate(background=background) - 1) < 1e-5)
def test_rebin(): """Tests data rebinning """ data = Data(h=np.linspace(0, 1, 101), k=0, l=0, e=0, temp=0, detector=functions.gaussian([0, 0, 10, 0.5, 0.5], np.linspace(0, 1, 101)), monitor=np.ones(101), time=np.ones(101)) data_bin = data.bin(dict(h=[0, 1., 51], k=[-0.1, 0.1, 1], l=[-0.1, 0.1, 1], e=[-0.5, 0.5, 1])) assert (data_bin.Q.shape[0] == 51) assert (data_bin.monitor.shape[0] == 51) assert (data_bin.detector.shape[0] == 51) assert (np.average(data_bin.monitor) == np.average(data.monitor)) assert (abs(simps(data_bin.detector, data_bin.Q[:, 0]) - simps(data.detector, data.Q[:, 0])) <= 0.1) assert (np.abs(data_bin.integrate() - data.integrate()) < 1e-1) assert (np.abs(data_bin.position()[0] - data.position()[0]) < 1e-1) assert (np.abs(data_bin.width()[0] - data.width()[0]) < 1e-1) def _test(): data_bin = data.bin(dict(blah=[1, 2, 4])) with pytest.raises(KeyError): _test()
def test_analysis(): """Tests analysis methods """ x = np.linspace(-2, 2, 100) y = functions.gaussian([0, 0, 1, 0, 0.5], x) data = Data(Q=np.vstack((item.ravel() for item in np.meshgrid(x, 0., 0., 4., 300.))).T, detector=y, monitor=np.full(x.shape, 1, dtype=float), time=np.full(x.shape, 1, dtype=float)) assert (np.abs(data.integrate() - 1) < 1e-5) assert (np.abs(data.position()[0]) < 1e-5) assert (abs(data.width(fwhm=True)[0] - 0.5) < 1e-1) assert isinstance(data.position(hkle=False), dict) assert isinstance(data.width(hkle=False), dict) assert (np.abs(data.integrate(hkle=False) - 1) < 1e-5) assert (np.abs(data.position(hkle=False)['h']) < 1e-5) assert (abs(data.width(fwhm=True, hkle=False)['h'] - 0.5) < 1e-1) bounds = (data.h >= -1) & (data.h <= 1) assert (np.abs(data.integrate(bounds=bounds) - 1) < 1e-5) assert (np.abs(data.position(bounds=bounds)[0]) < 1e-5) assert (abs(data.width(bounds=bounds, fwhm=True)[0] - 0.5) < 1e-1) background = dict(type='constant', value=0.) assert (np.abs(data.integrate(background=background) - 1) < 1e-5) assert (np.abs(data.position(background=background)[0]) < 1e-5) assert (abs(data.width(background=background, fwhm=True)[0] - 0.5) < 1e-1) background = dict(type='percent', value=2) assert (np.abs(data.integrate(background=background) - 1) < 1e-5) background = dict(type='minimum') assert (np.abs(data.integrate(background=background) - 1) < 1e-5) background = dict(type='blah') assert (np.abs(data.integrate(background=background) - 1) < 1e-5)