-
Notifications
You must be signed in to change notification settings - Fork 54
better buffer handling #150
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,13 @@ | |
from math import ceil | ||
from itertools import product | ||
|
||
import numpy as np | ||
import pygfx | ||
from pygfx.utils import unpack_bitfield | ||
|
||
from ._base import Graphic, Interaction, PreviouslyModifiedData | ||
from .features import ImageCmapFeature, ImageDataFeature, HeatmapDataFeature, HeatmapCmapFeature | ||
from .features._base import to_gpu_supported_dtype | ||
from ..utils import quick_min_max | ||
|
||
|
||
|
@@ -23,6 +25,7 @@ def __init__( | |
vmax: int = None, | ||
cmap: str = 'plasma', | ||
filter: str = "nearest", | ||
isolated_buffer: bool = True, | ||
*args, | ||
**kwargs | ||
): | ||
|
@@ -43,6 +46,10 @@ def __init__( | |
colormap to use to display the image data, ignored if data is RGB | ||
filter: str, optional, default "nearest" | ||
interpolation filter, one of "nearest" or "linear" | ||
isolated_buffer: bool, default True | ||
If True, initialize a buffer with the same shape as the input data and then | ||
set the data, useful if the data arrays are ready-only such as memmaps. | ||
If False, the input array is itself used as the buffer. | ||
args: | ||
additional arguments passed to Graphic | ||
kwargs: | ||
|
@@ -65,20 +72,29 @@ def __init__( | |
|
||
super().__init__(*args, **kwargs) | ||
|
||
self.data = ImageDataFeature(self, data) | ||
data = to_gpu_supported_dtype(data) | ||
|
||
# TODO: we need to organize and do this better | ||
if isolated_buffer: | ||
# initialize a buffer with the same shape as the input data | ||
# we do not directly use the input data array as the buffer | ||
# because if the input array is a read-only type, such as | ||
# numpy memmaps, we would not be able to change the image data | ||
buffer_init = np.zeros(shape=data.shape, dtype=data.dtype) | ||
else: | ||
buffer_init = data | ||
|
||
if (vmin is None) or (vmax is None): | ||
vmin, vmax = quick_min_max(data) | ||
|
||
texture_view = pygfx.Texture(self.data(), dim=2).get_view(filter=filter) | ||
texture_view = pygfx.Texture(buffer_init, dim=2).get_view(filter=filter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. before, |
||
|
||
geometry = pygfx.Geometry(grid=texture_view) | ||
|
||
# if data is RGB | ||
if self.data().ndim == 3: | ||
if data.ndim == 3: | ||
self.cmap = None | ||
material = pygfx.ImageBasicMaterial(clim=(vmin, vmax)) | ||
|
||
# if data is just 2D without color information, use colormap LUT | ||
else: | ||
self.cmap = ImageCmapFeature(self, cmap) | ||
|
@@ -89,6 +105,13 @@ def __init__( | |
material | ||
) | ||
|
||
self.data = ImageDataFeature(self, data) | ||
# TODO: we need to organize and do this better | ||
if isolated_buffer: | ||
# if the buffer was initialized with zeros | ||
# set it with the actual data | ||
self.data = data | ||
|
||
@property | ||
def vmin(self) -> float: | ||
"""Minimum contrast limit.""" | ||
|
@@ -176,6 +199,7 @@ def __init__( | |
cmap: str = 'plasma', | ||
filter: str = "nearest", | ||
chunk_size: int = 8192, | ||
isolated_buffer: bool = True, | ||
*args, | ||
**kwargs | ||
): | ||
|
@@ -198,6 +222,10 @@ def __init__( | |
interpolation filter, one of "nearest" or "linear" | ||
chunk_size: int, default 8192, max 8192 | ||
chunk size for each tile used to make up the heatmap texture | ||
isolated_buffer: bool, default True | ||
If True, initialize a buffer with the same shape as the input data and then | ||
set the data, useful if the data arrays are ready-only such as memmaps. | ||
If False, the input array is itself used as the buffer. | ||
args: | ||
additional arguments passed to Graphic | ||
kwargs: | ||
|
@@ -223,7 +251,17 @@ def __init__( | |
if chunk_size > 8192: | ||
raise ValueError("Maximum chunk size is 8192") | ||
|
||
self.data = HeatmapDataFeature(self, data) | ||
data = to_gpu_supported_dtype(data) | ||
|
||
# TODO: we need to organize and do this better | ||
if isolated_buffer: | ||
# initialize a buffer with the same shape as the input data | ||
# we do not directly use the input data array as the buffer | ||
# because if the input array is a read-only type, such as | ||
# numpy memmaps, we would not be able to change the image data | ||
buffer_init = np.zeros(shape=data.shape, dtype=data.dtype) | ||
else: | ||
buffer_init = data | ||
|
||
row_chunks = range(ceil(data.shape[0] / chunk_size)) | ||
col_chunks = range(ceil(data.shape[1] / chunk_size)) | ||
|
@@ -249,7 +287,7 @@ def __init__( | |
# x and y positions of the Tile in world space coordinates | ||
y_pos, x_pos = row_start, col_start | ||
|
||
tex_view = pygfx.Texture(data[row_start:row_stop, col_start:col_stop], dim=2).get_view(filter=filter) | ||
tex_view = pygfx.Texture(buffer_init[row_start:row_stop, col_start:col_stop], dim=2).get_view(filter=filter) | ||
geometry = pygfx.Geometry(grid=tex_view) | ||
# material = pygfx.ImageBasicMaterial(clim=(0, 1), map=self.cmap()) | ||
|
||
|
@@ -264,6 +302,13 @@ def __init__( | |
|
||
self.world_object.add(img) | ||
|
||
self.data = HeatmapDataFeature(self, buffer_init) | ||
# TODO: we need to organize and do this better | ||
if isolated_buffer: | ||
# if the buffer was initialized with zeros | ||
# set it with the actual data | ||
self.data = data | ||
|
||
@property | ||
def vmin(self) -> float: | ||
"""Minimum contrast limit.""" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so, the point of having the option for using an isolated buffer is to be able to change data in the event that the type of the data is read only? will this occur when using improv?