def _shift(var, dim, forward, out_of_bounds):
    fill = var[dim, 0:1].copy()
    fill.values = np.full_like(fill.values, out_of_bounds)
    if forward:
        return sc.concatenate(fill, var[dim, :-1], dim)
    else:
        return sc.concatenate(var[dim, 1:], fill, dim)
Exemple #2
0
def to_bin_edges(d, dim):
    centers = d.coords[dim].copy()
    del d.coords[dim]
    first = 1.5 * centers[dim, 0] - 0.5 * centers[dim, 1]
    last = 1.5 * centers[dim, -1] - 0.5 * centers[dim, -2]
    bulk = 0.5 * (centers[dim, 1:] + centers[dim, :-1])
    edges = sc.concatenate(first, bulk, dim)
    edges = sc.concatenate(edges, last, dim)
    d.coords[dim] = edges
Exemple #3
0
def test_dataset_concatenate():
    a = sc.Dataset(
        {'data': sc.Variable(dims=['x'], values=np.array([11, 12]))},
        coords={'x': sc.Variable(dims=['x'], values=np.array([1, 2]))})
    b = sc.Dataset(
        {'data': sc.Variable(dims=['x'], values=np.array([13, 14]))},
        coords={'x': sc.Variable(dims=['x'], values=np.array([3, 4]))})

    c = sc.concatenate(a, b, 'x')

    assert np.array_equal(c.coords['x'].values, np.array([1, 2, 3, 4]))
    assert np.array_equal(c['data'].values, np.array([11, 12, 13, 14]))
Exemple #4
0
def reduce_by_wavelength(data, q_bins, groupby, wavelength_bands):
    slices = contrib.make_slices(
        contrib.midpoints(data.coords['wavelength'], 'wavelength'),
        'wavelength', wavelength_bands)
    data = sc.neutron.convert(data, 'wavelength', 'Q',
                              out=data)  # TODO no gravity yet
    bands = None
    for s in slices:
        band = sc.histogram(data['Q', s], q_bins)
        band = sc.groupby(band, group=groupby).sum('spectrum')
        bands = sc.concatenate(bands, band,
                               'wavelength') if bands is not None else band
    bands.coords['wavelength'] = wavelength_bands
    return bands
Exemple #5
0
def test_concatenate():
    var = sc.Variable([Dim.X], values=np.array([0.1, 0.2]), unit=sc.units.m)
    expected = sc.Variable([sc.Dim.X],
                           values=np.array([0.1, 0.2, 0.1, 0.2]),
                           unit=sc.units.m)
    assert sc.concatenate(var, var, Dim.X) == expected