コード例 #1
0
def test_init_cases():
    """Tests initialization cases
    """
    try:
        Data()
    except:
        pytest.fail('Data initialization failed')
コード例 #2
0
ファイル: test_data.py プロジェクト: granrothge/neutronpy
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()
コード例 #3
0
def build_3d_data():
    """Builds 3D data object
    """
    p = np.array([0, 0, 1, 0, 0, 0.1])
    x, y = np.linspace(-1, 1, 81), np.linspace(-1, 1, 81)
    X, Y = np.meshgrid(x, y)
    z = functions.gaussian2d(p, (X, Y))
    mon = 1e5
    tim = 15

    output = Data(Q=np.vstack((item.ravel() for item in np.meshgrid(x, y, 0., 0., 300.))).T,
                  detector=z.ravel(), monitor=np.full(X.ravel().shape, mon, dtype=float),
                  time=np.full(X.ravel().shape, tim, dtype=float))

    return output
コード例 #4
0
def test_background_subtraction():
    """Test background subtraction
    """
    data = build_data(clean=True)
    background_data1 = build_data(clean=False)
    background_data2 = Data(detector=np.random.rand(101), monitor=np.full(101, 1, dtype=float),
                            time=np.full(101, 1, dtype=float), h=np.linspace(-1, 1, 101))
    try:
        data.subtract_background(background_data1, ret=False)
        data.subtract_background(background_data1, x='h', ret=False)
    except:
        pytest.fail('background subtraction failed')

    with pytest.raises(ValueError):
        data.subtract_background(background_data2, ret=False)
コード例 #5
0
def build_data(clean=True):
    """Builds data object
    """
    p = np.array([20., 0., 3., -0.15, 0.08, 0.2, 3., 0.15, 0.08, 0.2])
    x = np.linspace(-1, 1, 81)

    if clean:
        y = functions.voigt(p, x)
        mon = 1e5
        tim = 15
    else:
        y = functions.voigt(p, x) + np.random.normal(loc=0., scale=5, size=len(x))
        mon = 1e3
        tim = 5

    output = Data(Q=np.vstack((item.ravel() for item in np.meshgrid(x, 0., 0., 0., 300.))).T,
                  detector=y, monitor=np.full(x.shape, mon, dtype=float), time=np.full(x.shape, tim, dtype=float))

    return output
コード例 #6
0
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()
コード例 #7
0
ファイル: test_data.py プロジェクト: granrothge/neutronpy
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)
コード例 #8
0
def test_polarization_correction():
    data = Data()
    scattering.polarization.polarization_correction(data, data, data, data)
コード例 #9
0
def test_build_data():
    data = Data(blah='test')
    data = Data(error=1)
コード例 #10
0
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)