예제 #1
0
def test_dataset_histogram():
    var = sc.Variable(dims=['x'], shape=[2], dtype=sc.dtype.event_list_float64)
    var['x', 0].values = np.arange(3)
    var['x', 0].values.append(42)
    var['x', 0].values.extend(np.ones(3))
    var['x', 1].values = np.ones(6)
    ds = sc.Dataset()
    s = sc.DataArray(data=sc.Variable(dims=['x'],
                                      values=np.ones(2),
                                      variances=np.ones(2)),
                     coords={'y': var})
    s1 = sc.DataArray(data=sc.Variable(dims=['x'],
                                       values=np.ones(2),
                                       variances=np.ones(2)),
                      coords={'y': var * 5.0})
    realign_coords = {
        'y': sc.Variable(values=np.arange(5, dtype=np.float64), dims=['y'])
    }
    ds['s'] = sc.realign(s, realign_coords)
    ds['s1'] = sc.realign(s1, realign_coords)
    h = sc.histogram(ds)
    assert np.array_equal(
        h['s'].values, np.array([[1.0, 4.0, 1.0, 0.0], [0.0, 6.0, 0.0, 0.0]]))
    assert np.array_equal(
        h['s1'].values, np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]))
예제 #2
0
def test_realign():
    base = make_dataarray()
    y = sc.Variable(dims=['y'], values=[0.0, 4.0])
    realigned = sc.realign(base, coords={'y': y})
    assert realigned.data is None
    assert sc.is_equal(realigned.unaligned, base)
    expected = sc.DataArray(data=sc.Variable(dims=['y'], values=[4.0]),
                            coords={'y': y})
    assert sc.is_equal(sc.histogram(realigned), expected)
예제 #3
0
def project_xy(data, nx=100, ny=20):
    z = sc.geometry.z(sc.neutron.position(data))
    x = sc.geometry.x(sc.neutron.position(data)) / z
    y = sc.geometry.y(sc.neutron.position(data)) / z
    data.coords['x/z'] = x
    data.coords['y/z'] = y
    x = sc.Variable(dims=['x/z'],
                    values=np.linspace(sc.min(x).value,
                                       sc.max(x).value,
                                       num=nx))
    y = sc.Variable(dims=['y/z'],
                    values=np.linspace(sc.min(y).value,
                                       sc.max(y).value,
                                       num=ny))
    return sc.realign(data, coords={'y/z': y, 'x/z': x})
예제 #4
0
def test_realign():
    co = sc.Variable(['x'], shape=[1], dtype=sc.dtype.event_list_float64)
    co.values[0].append(1.0)
    co.values[0].append(2.0)
    co.values[0].append(2.0)
    data = sc.Variable(['y'],
                       dtype=sc.dtype.float64,
                       values=np.array([1]),
                       variances=np.array([1]))
    da = sc.DataArray(data=data, coords={'x': co})
    assert not da.unaligned
    da_r = sc.realign(
        da, {'x': sc.Variable(['x'], values=np.array([0.0, 1.0, 3.0]))})
    assert da_r.shape == [1, 2]
    assert da_r.unaligned == da
    assert not da_r.data
    assert np.allclose(sc.histogram(da_r).values, np.array([0, 3]), atol=1e-9)
    da.realign({'x': sc.Variable(['x'], values=np.array([0.0, 1.0, 3.0]))})
    assert da.shape == [1, 2]
예제 #5
0
파일: test_dict.py 프로젝트: TC5027/scipp
def test_data_array_unaligned_to_dict():
    N = 50
    values = 10 * np.random.rand(N)
    da = sc.DataArray(data=sc.Variable(dims=['position'],
                                       unit=sc.units.counts,
                                       values=values,
                                       variances=values),
                      coords={
                          'position':
                          sc.Variable(
                              dims=['position'],
                              values=['site-{}'.format(i) for i in range(N)]),
                          'x':
                          sc.Variable(dims=['position'],
                                      unit=sc.units.m,
                                      values=np.random.rand(N)),
                          'y':
                          sc.Variable(dims=['position'],
                                      unit=sc.units.m,
                                      values=np.random.rand(N))
                      })
    xbins = sc.Variable(dims=['x'], unit=sc.units.m, values=[0.1, 0.5, 0.9])
    ybins = sc.Variable(dims=['y'],
                        unit=sc.units.m,
                        values=[0.1, 0.3, 0.5, 0.7, 0.9])
    realigned = sc.realign(da, {'y': ybins, 'x': xbins})

    da_dict = sc.to_dict(realigned)
    assert "unaligned" in da_dict
    assert "data" not in da_dict
    assert sc.from_dict(da_dict["coords"]["x"]) == xbins
    assert sc.from_dict(da_dict["coords"]["y"]) == ybins
    assert sc.from_dict(
        da_dict["unaligned"]["coords"]["x"]) == realigned.unaligned.coords["x"]
    assert sc.from_dict(
        da_dict["unaligned"]["coords"]["y"]) == realigned.unaligned.coords["y"]
예제 #6
0
def test_plot_realigned_1d():
    d = make_events_dataset(ndim=1)
    tbins = sc.Variable(dims=['tof'], unit=sc.units.us, values=np.arange(100.))
    r = sc.realign(d, {'tof': tbins})
    plot(r['x', 25])