Skip to content

Cannot transpose DimensionError when using lookup along vertical dimension #3789

@nvaytet

Description

@nvaytet

Example:

import scipp as sc

da = sc.data.data_xy()

lu = sc.lookup(da, 'y')
lu[sc.linspace('y', 0.1, 0.9, 33, unit='mm')]

works fine, but along the x dimension

lu = sc.lookup(da, 'x')
lu[sc.linspace('x', 0.1, 0.9, 33, unit='mm')]

it fails with

---------------------------------------------------------------------------
DimensionError                            Traceback (most recent call last)
Cell In[12], line 1
----> 1 lu = sc.lookup(da, 'x')
      2 lu[sc.linspace('x', 0.1, 0.9, 33, unit='mm')]

File [~/miniforge3/lib/python3.12/site-packages/scipp/core/bins.py:118](http://localhost:8890/home/nvaytet/miniforge3/lib/python3.12/site-packages/scipp/core/bins.py#line=117), in lookup(func, dim, mode, fill_value)
    116 dims = (*[d for d in func.dims if d != dim], dim)
    117 func.data = func.data.transpose(dims).copy()
--> 118 func.coords[dim] = func.coords[dim].transpose(dims).copy()
    119 for key, mask in func.masks.items():
    120     func.masks[key] = mask.transpose(
    121         # Masks potentially have fewer dims than the data.
    122         [d for d in dims if d in mask.dims] or None
    123     ).copy()

File [~/miniforge3/lib/python3.12/site-packages/scipp/core/shape.py:389](http://localhost:8890/home/nvaytet/miniforge3/lib/python3.12/site-packages/scipp/core/shape.py#line=388), in transpose(x, dims)
    366 def transpose(
    367     x: VariableLikeType, dims: Sequence[str] | None = None
    368 ) -> VariableLikeType:
    369     """Transpose dimensions of the input.
    370 
    371     Parameters
   (...)
    387         If ``dims`` are incompatible with the input data.
    388     """
--> 389     return _call_cpp_func(_cpp.transpose, x, dims if dims is not None else [])

File [~/miniforge3/lib/python3.12/site-packages/scipp/core/_cpp_wrapper_util.py:27](http://localhost:8890/home/nvaytet/miniforge3/lib/python3.12/site-packages/scipp/core/_cpp_wrapper_util.py#line=26), in call_func(func, *args, **kwargs)
     25     return data_group_nary(func, *args, **kwargs)
     26 if out is None:
---> 27     return func(*args, **kwargs)
     28 else:
     29     return func(*args, **kwargs, out=out)

DimensionError: Cannot transpose: Requested new dimension order contains different number of labels.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions