Skip to content

Commit 4748e59

Browse files
authored
auto-replace buffers (#974)
* remove isolated_buffer * remove isolated_buffer from mixin * basics works for positions data * replaceable buffers for all positions related features * image data buffer can change * resizeable buffers for volume * black * buffer resize condition checked only if new value is an array * gc for buffer managers * uniform colors WIP * switching color modes works! * typo * balck * update tests for color_mode * update examples * backend tests passing * default for all uniforms is True * update examples * forgot * update test * example tests passing * dereferencing test and fixes * simplify texture array tests a bit * image replace buffer tests pass yay * forgot a file * comments, check image graphic * add image reshaping example * add buffer replace imgui thing for manual testing * black * dont call wgpu_obj.destroy(), seems to work and clear VRAM with normal dereferencing * slower changes * update * update example * fixes and tweaks for test * remove unecessary stuff * update * docstrings * fix example * update example * update example * update docs
1 parent e5afb2a commit 4748e59

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1160
-466
lines changed

docs/source/api/graphics/LineGraphic.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Properties
2525
LineGraphic.axes
2626
LineGraphic.block_events
2727
LineGraphic.cmap
28+
LineGraphic.color_mode
2829
LineGraphic.colors
2930
LineGraphic.data
3031
LineGraphic.deleted

docs/source/api/graphics/ScatterGraphic.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Properties
2525
ScatterGraphic.axes
2626
ScatterGraphic.block_events
2727
ScatterGraphic.cmap
28+
ScatterGraphic.color_mode
2829
ScatterGraphic.colors
2930
ScatterGraphic.data
3031
ScatterGraphic.deleted

examples/events/cmap_event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
xs = np.linspace(0, 4 * np.pi, 100)
3535
ys = np.sin(xs)
3636

37-
figure["sine"].add_line(np.column_stack([xs, ys]))
37+
figure["sine"].add_line(np.column_stack([xs, ys]), color_mode="vertex")
3838

3939
# make a 2D gaussian cloud
4040
cloud_data = np.random.normal(0, scale=3, size=1000).reshape(500, 2)

examples/gridplot/multigraphic_gridplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def make_circle(center, radius: float, n_points: int = 75) -> np.ndarray:
106106
gaussian_cloud2 = np.random.multivariate_normal(mean, covariance, n_points)
107107

108108
# add the scatter graphics to the figure
109-
figure["scatter"].add_scatter(data=gaussian_cloud, sizes=2, cmap="jet")
109+
figure["scatter"].add_scatter(data=gaussian_cloud, sizes=2, cmap="jet", color_mode="vertex")
110110
figure["scatter"].add_scatter(data=gaussian_cloud2, colors="r", sizes=2)
111111

112112
figure.show()

examples/guis/imgui_basic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
figure = fpl.Figure(size=(700, 560))
3030

3131
# make some scatter points at every 10th point
32-
figure[0, 0].add_scatter(data[::10], colors="cyan", sizes=15, name="sine-scatter", uniform_color=True)
32+
figure[0, 0].add_scatter(data[::10], colors="cyan", sizes=15, name="sine-scatter")
3333

3434
# place a line above the scatter
35-
figure[0, 0].add_line(data, thickness=3, colors="r", name="sine-wave", uniform_color=True)
35+
figure[0, 0].add_line(data, thickness=3, colors="r", name="sine-wave")
3636

3737

3838
class ImguiExample(EdgeWindow):

examples/image/image_reshaping.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Image reshaping
3+
===============
4+
5+
An example that shows replacement of the image data with new data of a different shape. Under the hood, this creates a
6+
new buffer and a new array of Textures on the GPU that replace the older Textures. Creating a new buffer and textures
7+
has a performance cost, so you should do this only if you need to or if the performance drawback is not a concern for
8+
your use case.
9+
10+
Note that the vmin-vmax is reset when you replace the buffers.
11+
"""
12+
13+
# test_example = false
14+
# sphinx_gallery_pygfx_docs = 'animate'
15+
16+
17+
import numpy as np
18+
import fastplotlib as fpl
19+
20+
# create some data, diagonal sinusoidal bands
21+
xs = np.linspace(0, 2300, 2300, dtype=np.float16)
22+
full_data = np.vstack([np.cos(np.sqrt(xs + (np.pi / 2) * i)) * i for i in range(2_300)])
23+
24+
figure = fpl.Figure()
25+
26+
image = figure[0, 0].add_image(full_data)
27+
28+
figure.show()
29+
30+
i, j = 1, 1
31+
32+
33+
def update():
34+
global i, j
35+
# set the new image data as a subset of the full data
36+
row = np.abs(np.sin(i)) * 2300
37+
col = np.abs(np.cos(i)) * 2300
38+
image.data = full_data[: int(row), : int(col)]
39+
40+
i += 0.01
41+
j += 0.01
42+
43+
44+
figure.add_animations(update)
45+
46+
# NOTE: fpl.loop.run() should not be used for interactive sessions
47+
# See the "JupyterLab and IPython" section in the user guide
48+
if __name__ == "__main__":
49+
print(__doc__)
50+
fpl.loop.run()

examples/line/line_cmap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
data=sine_data,
2828
thickness=10,
2929
cmap="plasma",
30-
cmap_transform=sine_data[:, 1]
30+
cmap_transform=sine_data[:, 1],
3131
)
3232

3333
# qualitative colormaps, useful for cluster labels or other types of categorical labels
@@ -36,7 +36,7 @@
3636
data=cosine_data,
3737
thickness=10,
3838
cmap="tab10",
39-
cmap_transform=labels
39+
cmap_transform=labels,
4040
)
4141

4242
figure.show()

examples/line/line_cmap_more.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,35 @@
3131
# set colormap by mapping data using a transform
3232
# here we map the color using the y-values of the sine data
3333
# i.e., the color is a function of sine(x)
34-
line2 = figure[0, 0].add_line(sine, thickness=10, cmap="jet", cmap_transform=sine[:, 1], offset=(0, 4, 0))
34+
line2 = figure[0, 0].add_line(
35+
sine,
36+
thickness=10,
37+
cmap="jet",
38+
cmap_transform=sine[:, 1],
39+
offset=(0, 4, 0),
40+
)
3541

3642
# make a line and change the cmap afterward, here we are using the cosine instead fot the transform
37-
line3 = figure[0, 0].add_line(sine, thickness=10, cmap="jet", cmap_transform=cosine[:, 1], offset=(0, 6, 0))
43+
line3 = figure[0, 0].add_line(
44+
sine,
45+
thickness=10,
46+
cmap="jet",
47+
cmap_transform=cosine[:, 1],
48+
offset=(0, 6, 0)
49+
)
50+
3851
# change the cmap
3952
line3.cmap = "bwr"
4053

4154
# use quantitative colormaps with categorical cmap_transforms
4255
labels = [0] * 25 + [1] * 5 + [2] * 50 + [3] * 20
43-
line4 = figure[0, 0].add_line(sine, thickness=10, cmap="tab10", cmap_transform=labels, offset=(0, 8, 0))
56+
line4 = figure[0, 0].add_line(
57+
sine,
58+
thickness=10,
59+
cmap="tab10",
60+
cmap_transform=labels,
61+
offset=(0, 8, 0),
62+
)
4463

4564
# some text labels
4665
for i in range(5):

examples/line/line_colorslice.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
sine = figure[0, 0].add_line(
3131
data=sine_data,
3232
thickness=5,
33-
colors="magenta"
33+
colors="magenta",
34+
color_mode="vertex", # initialize with same color across vertices, but we will change the per-vertex colors later
3435
)
3536

3637
# you can also use colormaps for lines!
@@ -56,6 +57,7 @@
5657
data=zeros_data,
5758
thickness=8,
5859
colors="w",
60+
color_mode="vertex", # initialize with same color across vertices, but we will change the per-vertex colors later
5961
offset=(0, 10, 0)
6062
)
6163

examples/line_collection/line_collection_slicing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
multi_data,
2727
thickness=[2, 10, 2, 5, 5, 5, 8, 8, 8, 9, 3, 3, 3, 4, 4],
2828
separation=4,
29+
color_mode="vertex", # this will allow us to set per-vertex colors on each line
2930
metadatas=list(range(15)), # some metadata
3031
names=list("abcdefghijklmno"), # unique name for each line
3132
)

0 commit comments

Comments
 (0)