Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions fastplotlib/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,10 @@ def parse_cmap_values(


def cuda_to_numpy(arr: CudaArrayProtocol) -> np.ndarray:
try:
import cupy
except ImportError:
raise ImportError(
"`cupy` is required to work with GPU arrays\npip install cupy"
)

return cupy.asnumpy(arr)

Comment thread
kushalkolar marked this conversation as resolved.
data = np.from_dlpack(arr, device='cpu')
return data

Comment thread
kushalkolar marked this conversation as resolved.


def subsample_array(
Expand Down
20 changes: 14 additions & 6 deletions fastplotlib/widgets/nd_widget/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ def spatial_dims(self, sdims: Sequence[str]):

self._spatial_dims = tuple(sdims)

## This is the ordered sequence of data indices that will be displayed
self._spatial_dims_indices = tuple(
self.spatial_dims.index(d) for d in self.dims if d in self.spatial_dims
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## This is the ordered sequence of data indices that will be displayed
self._spatial_dims_indices = tuple(
self.spatial_dims.index(d) for d in self.dims if d in self.spatial_dims
)

@property
def spatial_dims_indices(self) -> tuple[int, ...]:
"""
The ordered sequence of data indices that will be displayed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The ordered sequence of data indices that will be displayed
ordered spatial dim indices that correspond to the named spatial dims

"""
return self._spatial_dims_indices

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return self._spatial_dims_indices
return tuple(
self.spatial_dims.index(d) for d in self.dims if d in self.spatial_dims
)



Comment thread
kushalkolar marked this conversation as resolved.
@property
def tooltip(self) -> bool:
"""
Expand Down Expand Up @@ -529,12 +542,7 @@ async def get_window_output(self, indices: dict[str, Any]) -> ArrayProtocol:
f"windowed_slice.ndim != len(self.spatial_dims): {windowed_slice.ndim} != {len(self.spatial_dims)}"
)

# transpose to spatial dims
spatial_dims_int = tuple(
self.spatial_dims.index(d) for d in self.dims if d in self.spatial_dims
)

return windowed_slice.transpose(*spatial_dims_int)
return windowed_slice

async def _get_raw_data_slice(self, indices: dict[str, Any]) -> ArrayProtocol:
"""
Expand Down
15 changes: 14 additions & 1 deletion fastplotlib/widgets/nd_widget/_nd_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ def spatial_dims(self, sdims: tuple[str, str] | tuple[str, str, str]):

self._spatial_dims = tuple(sdims)

## This is the ordered sequence of data indices that will be displayed
self._spatial_dims_indices = tuple(
self.spatial_dims.index(d) for d in self.dims if d in self.spatial_dims
)

@property
def spatial_dims_indices(self) -> tuple[int, ...]:
"""
The ordered sequence of data indices that will be displayed
"""
return self._spatial_dims_indices

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## This is the ordered sequence of data indices that will be displayed
self._spatial_dims_indices = tuple(
self.spatial_dims.index(d) for d in self.dims if d in self.spatial_dims
)
@property
def spatial_dims_indices(self) -> tuple[int, ...]:
"""
The ordered sequence of data indices that will be displayed
"""
return self._spatial_dims_indices

@property
def rgb_dim(self) -> str | None:
"""
Expand Down Expand Up @@ -258,7 +270,8 @@ async def get(self, indices: dict[str, Any]) -> ArrayProtocol:
if isinstance(window_output, CudaArrayProtocol):
window_output = await run_in_thread_pool(self._executor, cuda_to_numpy, window_output)

return window_output

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

return window_output.transpose(*self.spatial_dims_indices)

def _recompute_histogram(self):
"""
Expand Down
15 changes: 15 additions & 0 deletions fastplotlib/widgets/nd_widget/_nd_positions/_nd_positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __init__(
self.cmap_transform_each = cmap_transform_each
self.sizes = sizes


Comment thread
kushalkolar marked this conversation as resolved.
def _check_shape_feature(
self, prop: str, check_shape: tuple[int, int]
) -> tuple[int, int]:
Expand Down Expand Up @@ -298,6 +299,18 @@ def spatial_dims(self, sdims: tuple[str, str, str]):

self._spatial_dims = tuple(sdims)

## This is the ordered sequence of data indices that will be displayed
self._spatial_dims_indices = tuple(
self.spatial_dims.index(d) for d in self.dims if d in self.spatial_dims
)

@property
def spatial_dims_indices(self) -> tuple[int, ...]:
"""
The ordered sequence of data indices that will be displayed
"""
return self._spatial_dims_indices

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## This is the ordered sequence of data indices that will be displayed
self._spatial_dims_indices = tuple(
self.spatial_dims.index(d) for d in self.dims if d in self.spatial_dims
)
@property
def spatial_dims_indices(self) -> tuple[int, ...]:
"""
The ordered sequence of data indices that will be displayed
"""
return self._spatial_dims_indices

@property
def slider_dims(self) -> set[Hashable]:
# append `p` dim to slider dims
Expand Down Expand Up @@ -555,6 +568,8 @@ async def get(self, indices: dict[str, Any]) -> dict[str, ArrayProtocol]:
if isinstance(data, CudaArrayProtocol):
data = await run_in_thread_pool(self._executor, cuda_to_numpy, data)

data = data.transpose(*self._spatial_dims_int)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data = data.transpose(*self._spatial_dims_int)
data = data.transpose(*self.spatial_dims_indices)

you made it a public property but didn't use it?


return {
"data": data,
**other,
Expand Down
17 changes: 15 additions & 2 deletions fastplotlib/widgets/nd_widget/_nd_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ def spatial_dims(self, sdims: tuple[str, str, str]):
f"Spatial dimensions must haves shape (num_vecs, 2, [2 or 3]) you passed {sdims}"
)

## This is the ordered sequence of data indices that will be displayed
self._spatial_dims_indices = tuple(
self.spatial_dims.index(d) for d in self.dims if d in self.spatial_dims
)

@property
def spatial_dims_indices(self) -> tuple[int, ...]:
"""
The ordered sequence of data indices that will be displayed
"""
return self._spatial_dims_indices

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## This is the ordered sequence of data indices that will be displayed
self._spatial_dims_indices = tuple(
self.spatial_dims.index(d) for d in self.dims if d in self.spatial_dims
)
@property
def spatial_dims_indices(self) -> tuple[int, ...]:
"""
The ordered sequence of data indices that will be displayed
"""
return self._spatial_dims_indices

async def get(self, indices: dict[str, Any]) -> ArrayProtocol:
"""
Get the data at the given index, process data through the window functions.
Expand All @@ -157,7 +169,7 @@ async def get(self, indices: dict[str, Any]) -> ArrayProtocol:
Example: get((100, 5))

"""
# this will be squeezed output, with dims in the order of the user set spatial dims
# this will be squeezed output, with dims in the order of self.dims
window_output = await self.get_window_output(indices)

# apply spatial_func; CUDA arrays run inline, numpy goes through the thread pool
Expand All @@ -175,7 +187,8 @@ async def get(self, indices: dict[str, Any]) -> ArrayProtocol:
if isinstance(window_output, CudaArrayProtocol):
window_output = await run_in_thread_pool(self._executor, cuda_to_numpy, window_output)

return window_output

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

return window_output.transpose(*self._spatial_dims_indices)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return window_output.transpose(*self._spatial_dims_indices)
return window_output.transpose(*self.spatial_dims_indices)

it's a public property use it



class NDVectors(NDGraphic):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ keywords = [
]
requires-python = ">= 3.10"
dependencies = [
"numpy>=1.23.0",
"numpy>=2.1.0",
"pygfx==0.16.0",
"wgpu", # Let pygfx constrain the wgpu version
"cmap>=0.1.3",
Expand Down