Example #1
0
def test_mean_all():
    var = sc.Variable(['x', 'y'], values=np.arange(4.0).reshape(2, 2))
    mask = sc.Variable(['x', 'y'],
                       values=np.array([[False, False], [True, False]]))
    da = sc.DataArray(var, masks={'m': mask})  # Add masks
    assert sc.sum(da).data.value == 0 + 1 + 3  # 2.0 masked
    sc.mean(da).data.value == 4 / 3
Example #2
0
def test_sum_mean():
    d = sc.Dataset(
        {
            'a':
            sc.Variable(dims=['x', 'y'],
                        values=np.arange(6, dtype=np.int64).reshape(2, 3)),
            'b':
            sc.Variable(dims=['y'], values=np.arange(3, dtype=np.int64))
        },
        coords={
            'x':
            sc.Variable(dims=['x'], values=np.arange(2, dtype=np.int64)),
            'y':
            sc.Variable(dims=['y'], values=np.arange(3, dtype=np.int64)),
            'l1':
            sc.Variable(dims=['x', 'y'],
                        values=np.arange(6, dtype=np.int64).reshape(2, 3)),
            'l2':
            sc.Variable(dims=['x'], values=np.arange(2, dtype=np.int64))
        })
    d_ref = sc.Dataset(
        {
            'a': sc.Variable(dims=['x'],
                             values=np.array([3, 12], dtype=np.int64)),
            'b': sc.Variable(3)
        },
        coords={
            'x': sc.Variable(dims=['x'], values=np.arange(2, dtype=np.int64)),
            'l2': sc.Variable(dims=['x'], values=np.arange(2, dtype=np.int64))
        })

    assert sc.sum(d, 'y') == d_ref
    assert (sc.mean(d, 'y')['a'].values == [1.0, 4.0]).all()
    assert sc.mean(d, 'y')['b'].value == 1.0
Example #3
0
def test_nanmean_all():
    var = sc.Variable(['x', 'y'], values=np.arange(4.0).reshape(2, 2))
    var['x', 0]['y', 1].value = np.nan
    mask = sc.Variable(['x', 'y'],
                       values=np.array([[False, False], [True, False]]))
    da = sc.DataArray(var, masks={'m': mask})  # Add masks
    assert sc.nansum(da).data.value == 0 + 3  # 2.0 masked, 1.0 is nan
    sc.mean(da).data.value == 3 / 2
Example #4
0
def test_mean():
    var = sc.Variable(['x', 'y'], values=np.arange(4.0).reshape(2, 2))
    assert sc.is_equal(sc.mean(var), sc.Variable(value=6.0 / 4))
    assert sc.is_equal(sc.mean(var, 'x'),
                       sc.Variable(dims=['y'], values=[1.0, 2.0]))
    out = sc.Variable(dims=['y'], values=np.zeros(2), dtype=sc.dtype.float64)
    sc.mean(var, 'x', out)
    assert sc.is_equal(out, sc.Variable(dims=['y'], values=[1.0, 2.0]))
Example #5
0
def test_nanmean():
    var = sc.Variable(['x', 'y'],
                      values=np.array([1.0, 1.0, 1.0, 1.0]).reshape(2, 2))
    assert sc.is_equal(sc.nanmean(var), sc.Variable(value=3.0 / 3))
    assert sc.is_equal(sc.nanmean(var, 'x'),
                       sc.Variable(dims=['y'], values=[1.0, 1.0]))
    out = sc.Variable(dims=['y'], values=np.zeros(2), dtype=sc.dtype.float64)
    sc.mean(var, 'x', out)
    assert sc.is_equal(out, sc.Variable(dims=['y'], values=[1.0, 1.0]))
Example #6
0
def _plot_components(scipp_obj, components, positions_var, scene):
    det_center = sc.mean(positions_var)
    # Some scaling to set width according to distance from detector center
    shapes = _instrument_view_shape_types()
    for name, settings in components.items():
        type = settings["type"]
        size = _as_vector(settings["size"])
        component_position = settings["center"]
        color = settings.get("color", "#808080")
        wireframe = settings.get("wireframe", False)
        if type not in shapes:
            supported_shapes = ", ".join(shapes.keys())
            raise ValueError(f"Unknown shape: {type} requested for {name}. "
                             f"Allowed values are: {supported_shapes}")
        component_position = sc.to_unit(component_position, positions_var.unit)
        size = sc.to_unit(size, positions_var.unit)
        _add_to_scene(position=component_position,
                      scene=scene,
                      shape=shapes[type],
                      display_text=name,
                      bounding_box=tuple(size.values),
                      color=color,
                      wireframe=wireframe,
                      det_center=det_center)
    # Reset camera
    camera = _get_camera(scene)
    if camera:
        furthest_distance = _furthest_component(det_center, scipp_obj,
                                                components)
        camera.far = max(camera.far, furthest_distance * 5.0)
Example #7
0
def mean_from_adj_pixels(data):
    """
    Applies a mean across 8 neighboring pixels (plus centre value)
    for data with 'x' and 'y' dimensions (at least).
    Result will calculate mean from slices across additional dimensions.

    For example if there is a tof dimension in addition to x, and y,
    for each set of neighbours the returned mean will take the mean
    tof value in the neighbour group.
    """
    fill = np.finfo(data.values.dtype).min
    has_variances = data.variances is not None
    container = sc.empty(dims=['neighbor'] + data.dims,
                         dtype=data.dtype,
                         shape=[
                             9,
                         ] + data.shape,
                         with_variances=has_variances,
                         unit=data.unit)
    container['neighbor', 0] = data
    container['neighbor', 1] = _shift(data, "x", True, fill)
    container['neighbor', 2] = _shift(data, "x", False, fill)
    container['neighbor', 3] = _shift(data, "y", True, fill)
    container['neighbor', 4] = _shift(data, "y", False, fill)
    container['neighbor', 5:7] = _shift(container['neighbor', 1:3], "y", True,
                                        fill)
    container['neighbor', 7:9] = _shift(container['neighbor', 1:3], "y", False,
                                        fill)

    edges_mask = container <= sc.scalar(value=fill, unit=data.unit)
    da = sc.DataArray(data=container, masks={'edges': edges_mask})
    return sc.mean(da, dim='neighbor').data
Example #8
0
def test_mean():
    var = sc.Variable([Dim.X, Dim.Y],
                      values=np.array([[0.1, 0.3], [0.2, 0.6]]),
                      unit=sc.units.m)
    expected = sc.Variable([Dim.X],
                           values=np.array([0.2, 0.4]),
                           unit=sc.units.m)
    assert sc.mean(var, Dim.Y) == expected
Example #9
0
def test_mean_masked():
    d = sc.Dataset(
        {'a': sc.Variable(dims=['x'], values=np.array([1, 5, 4, 5, 1]))})
    d['a'].masks['m1'] = sc.Variable(dims=['x'],
                                     values=np.array(
                                         [False, True, False, True, False]))
    d_ref = sc.Dataset({'a': sc.Variable(2.0)})
    assert sc.is_equal(sc.mean(d, 'x')['a'], d_ref['a'])
Example #10
0
File: stitch.py Project: scipp/ess
def stitch(
    data: Union[sc.DataArray, sc.Dataset],
    dim: str,
    frames: sc.Dataset,
    new_dim: str = 'tof',
    bins: Union[int,
                sc.Variable] = 256) -> Union[sc.DataArray, sc.Dataset, dict]:
    """
    Convert raw arrival time WFM data to time-of-flight by shifting each frame
    (described by the `frames` argument) by a time offset defined by the position
    of the WFM choppers.
    This process is also known as 'stitching' the frames.

    :param data: The DataArray or Dataset to be stitched.
    :param dim: The dimension along which the stitching will be performed.
    :param frames: The Dataset containing the information on the frame boundaries.
    :param new_dim: New dimension of the returned data, after stitching. Default: 'tof'.
    :param bins: Number or Variable describing the bins for the returned data. Default:
        256.
    """

    # TODO: for now, if frames depend on positions, we take the mean along the
    # position dimensions. We should implement the position-dependent stitching
    # in the future.
    frames = frames.copy()
    dims_to_reduce = list(set(frames.dims) - {'frame'})
    for _dim in dims_to_reduce:
        frames["time_min"] = sc.mean(frames["time_min"], _dim)
        frames["time_max"] = sc.mean(frames["time_max"], _dim)

    if isinstance(data, sc.Dataset):
        stitched = sc.Dataset()
        for i, (key, item) in enumerate(data.items()):
            stitched[key] = _stitch_item(item=item,
                                         dim=dim,
                                         frames=frames,
                                         new_dim=new_dim,
                                         bins=bins)
    else:
        stitched = _stitch_item(item=data,
                                dim=dim,
                                frames=frames,
                                new_dim=new_dim,
                                bins=bins)

    return stitched
Example #11
0
def test_mean_masked():
    d = sc.Dataset(
        {'a': sc.Variable(dims=['x'], values=np.array([1, 5, 4, 5, 1]))},
        masks={
            'm1':
            sc.Variable(dims=['x'],
                        values=np.array([False, True, False, True, False]))
        })
    d_ref = sc.Dataset({'a': sc.Variable(2.0)})
    assert sc.mean(d, 'x')['a'] == d_ref['a']
Example #12
0
def test_mean_in_place():
    var = sc.Variable([Dim.X, Dim.Y],
                      values=np.array([[0.1, 0.3], [0.2, 0.6]]),
                      unit=sc.units.m)
    out = sc.Variable([Dim.X], values=np.array([0.0, 0.0]), unit=sc.units.m)
    expected = sc.Variable([Dim.X],
                           values=np.array([0.2, 0.4]),
                           unit=sc.units.m)
    view = sc.mean(var, Dim.Y, out)
    assert out == expected
    assert view == out
Example #13
0
def illumination_of_sample(beam_size: sc.Variable, sample_size: sc.Variable,
                           theta: sc.Variable) -> sc.Variable:
    """
    Determine the illumination of the sample by the beam and therefore the size of its
    illuminated length.

    :param beam_size: Width of incident beam.
    :param sample_size: Width of sample in the dimension of the beam.
    :param theta: Incident angle.
    """
    beam_on_sample = beam_size / sc.sin(theta)
    if ((sc.mean(beam_on_sample)) > sample_size).value:
        beam_on_sample = sc.broadcast(sample_size,
                                      shape=theta.shape,
                                      dims=theta.dims)
    return beam_on_sample
Example #14
0
def smooth_data(variable, dim=None, NPoints=3):
    """
    Function that smooths data by assigning the value of each point to the
    mean of the values from surrounding points and itself. The number of points
    to average is NPoint, which is odd so that the point itself is in the
    center of the averaged interval. If an even NPoints is given, it is
    incremented. At the ends of the interval the full number of points is not
    used, but all available within NPoints//2 is.

    Parameters
    ----------
        variable: scipp variable
            The variable which should have its values smoothed

        dim: scipp Dim
            The dimension along which values should be smoothed

        NPoints: int
            The number of points to use in the mean (odd number)
    """

    if dim is None:
        raise ValueError("smooth_data was not given a dim to smooth.")

    if NPoints < 3:
        raise ValueError("smoot_hdata needs NPoints of 3 or higher.")

    data_length = dict(zip(variable.dims, variable.shape))[sc.Dim(dim)]
    out = variable.copy()  # preallocate output variable

    hr = NPoints//2  # half range rounded down

    for index in range(data_length):
        begin = max(0, index - hr)
        end = min(data_length, index + hr + 1)
        out[dim, index] = sc.mean(variable[dim, begin:end], dim)

    return out
Example #15
0
def test_sum_mean():
    var = sc.Variable([Dim.X], values=np.arange(5, dtype=np.int64))
    assert sc.sum(var, Dim.X) == sc.Variable(10)
    var = sc.Variable([Dim.X], values=np.arange(6, dtype=np.int64))
    assert sc.mean(var, Dim.X) == sc.Variable(2.5)
Example #16
0
def test_sum_mean():
    var = sc.Variable(dims=['x'], values=np.arange(5, dtype=np.int64))
    assert sc.is_equal(sc.sum(var, 'x'), sc.Variable(10))
    var = sc.Variable(dims=['x'], values=np.arange(6, dtype=np.int64))
    assert sc.is_equal(sc.mean(var, 'x'), sc.Variable(2.5))
Example #17
0
def test_sum_mean():
    var = sp.Variable([Dim.X], values=np.ones(5).astype(np.int64))
    assert sp.sum(var, Dim.X) == sp.Variable(5)
    var = sp.Variable([Dim.X], values=np.arange(6).astype(np.int64))
    assert sp.mean(var, Dim.X) == sp.Variable(2.5)
Example #18
0
def subtract_background_mean(data, dim, begin, end):
    coord = data.coords[dim]
    assert (coord.unit == begin.unit) and (coord.unit == end.unit)
    i = np.searchsorted(coord, begin.value)
    j = np.searchsorted(coord, end.value) + 1
    return data - sc.mean(data[dim, i:j], dim)