Skip to content

Keep coordinates after resample #110

@YooSunYoung

Description

@YooSunYoung

We should keep coordinates like position by taking mean when resampling.

def resample(
image: sc.Variable | sc.DataArray,
sizes: dict[str, int],
method: str | Callable = 'sum',
) -> sc.Variable | sc.DataArray:
"""
Resample an image by folding it into blocks of specified sizes and applying a
reduction method.
The sizes should be provided as a dictionary where the keys are dimension names
and the values are the sizes of the blocks. The shape of the input image must be
divisible by the block sizes.
Parameters
----------
image:
The image to resample.
sizes:
A dictionary specifying the block sizes for each dimension.
For example, ``{'x': 4, 'y': 4}`` will create blocks of size 4x4.
method:
The reduction method to apply to the blocks. This can be a string referring to
any valid Scipp reduction method, such as 'sum', 'mean', 'max', etc.
Alternatively, a custom reduction function can be provided. The function
signature should accept a ``scipp.Variable`` or ``scipp.DataArray`` as first
argument and a set of dimensions to reduce over as second argument. The
function should return a ``scipp.Variable`` or ``scipp.DataArray``.
"""
blocked = blockify(image, **sizes)
if isinstance(method, str):
return getattr(sc, method)(blocked, set(blocked.dims) - set(image.dims))
return method(blocked, set(blocked.dims) - set(image.dims))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions