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)