Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotation Functional Index example #8004

Open
TomNicholas opened this issue Jul 19, 2023 · 2 comments
Open

Rotation Functional Index example #8004

TomNicholas opened this issue Jul 19, 2023 · 2 comments

Comments

@TomNicholas
Copy link
Member

Is your feature request related to a problem?

I'm trying to think of an example that would demonstrate the "functional index" pattern discussed in #3620.

I think a 2D rotation is the simplest example of an analytically-expressible, non-trivial, domain-agnostic case where you might want to back a set of multiple coordinates with a single functional index. It's also nice because there is additional information that must be passed and stored (the angle of the rotation), but that part is very simple, and domain-agnostic. I'm proposing we make this example work and put it in the custom index docs.

I had a go at making that example (notebook here) @benbovy, but I'm confused about a couple of things:

  1. How do I implement .sel in such a way that it supports indexing with slices (i.e. to crop my image)
  2. How can I make this lazy?
  3. Should the implementation be a "MetaIndex" (i.e. wrapping some pandas indexes)?

Describe the solution you'd like

No response

Describe alternatives you've considered

No response

Additional context

This example is inspired by @jni's use case in napari, where (IIUC) they want to do a lazy functional affine transformation from pixel to physical coordinates, where the simplest example of such a transform might be a linear shear (caused by the imaging focal plane being at an angle to the physical sample).

@benbovy
Copy link
Member

benbovy commented Jul 19, 2023

@TomNicholas I haven't looked much at your example yet, but to quickly respond to your questions:

  1. I guess what you want for RotationalTransformIndex.sel() is to accept two x, y input slices representing 4 corner points in rotated coordinates, then transform those points back into original (non-rotated) coordinates, create two slices from the back-transformed coordinates (also check if start/stop values need to be swapped for x and or y depending on the index rotation angle) and finally return an IndexSelResult object with the created slices for x and y.

  2. You could for example build a RotationalTransformIndex directly from the original (1-dimensional) x and y pixel coordinates.

  3. Yes it would be useful if RotationalTransformIndex encapsulates two PandasIndex for the x and y pixel coordinates. So you could still support alignment based on those original pixel coordinates (e.g., add two images partially overlapping), checking that the indexes rotation angles are all equal, while enabling selection using rotated coordinates. You could even support selection using both original and rotated coordinates, e.g.,

arr = np.array(img_greyscale)

da = xr.DataArray(
    data=arr,
    dims=['x', 'y'],
    # non-rotated pixel coordinates
    coords={'x': range(arr.shape[0]), 'y': range(arr.shape[1])},
)

da = da.set_xindex(['x', 'y'], RotationalTransformIndex, angle=-160)

# selection using original (non-rotated coordinates)
da.sel(x=slice(300, 500), y=(600, 800), method="original")

# selection using rotated coordinates
da.sel(x=slice(-400, 0), y=(-600, -400), method="rotated")

# alignment would "just" work if RotationalTransformIndex
# encapsulate two PandasIndex for x and y (meta-index)
xr.align(
    da.sel(x=slice(200, 600), method="original"),
    da.sel(x=slice(300, 700), method="rotated"),
)

It is not very nice using sel's method option for switching between original and rotated coordinates but currently it is not possible to define arbitrary option for sel (#7099).

@TomNicholas
Copy link
Member Author

@benbovy thanks for responding - I'll have a go at implementing your suggestions.

Also I had not seen your example of a 1D functional index from the list in #7041.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants