Skip to content

Commit

Permalink
update to_pygfx (#31)
Browse files Browse the repository at this point in the history
* update to_pygfx

* try pyside2

* more stuff

* add more drivers

* skip again

* update docs and overload

* update test

* back to pyqt6
  • Loading branch information
tlambert03 authored Dec 4, 2023
1 parent 1d5bcbb commit 0d03d78
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 39 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.12"]
python-version: ["3.8", "3.11"]
platform: [ubuntu-latest, macos-latest, windows-latest]
include:
- python-version: "3.9"
platform: ubuntu-latest
- python-version: "3.10"
platform: ubuntu-latest
- python-version: "3.11"
- python-version: "3.12"
platform: ubuntu-latest

steps:
Expand All @@ -66,6 +66,7 @@ jobs:
run: |
python -m pip install -U pip
python -m pip install -e .[test,thirdparty] ${{ github.event_name == 'schedule' && '--pre' || '' }}
python -m pip install pyqt6
- name: Run test
uses: aganders3/headless-gui@v1.2
Expand Down
3 changes: 1 addition & 2 deletions docs/colormaps.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ external visualization libraries. To that end, `cmap.Colormap` provides
Colormap("viridis").to_pygfx()
```

Returns an instance of `pygfx.TextureView` (unless `as_view` is `False`, in which case
a `pygfx.Texture` is returned).
Returns an instance of `pygfx.Texture`.

- [plotly](https://plotly.com/python/)

Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ thirdparty = [
"plotly",
"pydantic",
"pygfx",
"pyqt6",
"pytest-qt",
"rich",
"viscm",
Expand Down
28 changes: 9 additions & 19 deletions src/cmap/_colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,25 +472,15 @@ def to_vispy(self) -> vispy.color.Colormap:
"""Return a vispy colormap."""
return _external.to_vispy(self)

@overload
def to_pygfx(
self, N: int = ..., *, as_view: Literal[True] = ...
) -> pygfx.TextureView:
...

@overload
def to_pygfx(self, N: int = ..., *, as_view: Literal[False]) -> pygfx.Texture:
...

def to_pygfx(
self, N: int = 256, *, as_view: bool = True
) -> pygfx.TextureView | pygfx.Texture:
"""Return a pygfx TextureView, or Texture if as_view is False.
If you want to customize the TextureView, use `as_view == False` and then
call `get_view()` on the returned Texture, providing the desired arguments.
"""
return _external.to_pygfx(self, N=N, as_view=as_view)
def to_pygfx(self, N: int = 256, *, as_view: bool | None = None) -> pygfx.Texture:
"""Return a pygfx Texture."""
if as_view is not None:
warnings.warn(
"as_view argument is deprecated and does nothing",
DeprecationWarning,
stacklevel=2,
)
return _external.to_pygfx(self, N=N)

def to_napari(self) -> napari.utils.colormaps.Colormap:
"""Return a napari colormap.
Expand Down
13 changes: 3 additions & 10 deletions src/cmap/_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,15 @@ def to_vispy(cm: Colormap) -> VispyColormap:
return Colormap(colors=cm.color_stops.color_array, controls=cm.color_stops.stops)


def to_pygfx(
cm: Colormap, N: int = 256, *, as_view: bool = True
) -> pygfx.TextureView | pygfx.Texture:
"""Return a pygfx TextureView, or Texture if as_view is False.
If you want to customize the TextureView, use `as_view == False` and then
call `get_view()` on the returned Texture, providing the desired arguments.
"""
def to_pygfx(cm: Colormap, N: int = 256) -> pygfx.Texture:
"""Return a pygfx Texture."""
import pygfx

# TODO: check whether pygfx has it's own stop-aware interpolation,
# and if so, use that instead of .lut()
# (get_view has a filter argument... but I don't know whether it will take
# care of the stops)
tex = pygfx.Texture(cm.lut(N).astype(np.float32), dim=1)
return tex.get_view() if as_view else tex
return pygfx.Texture(cm.lut(N).astype(np.float32), dim=1)


def to_plotly(cm: Colormap) -> list[list[float | str]]:
Expand Down
13 changes: 8 additions & 5 deletions tests/test_third_party.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

CMAP = Colormap(["black", (0, 1, 0), "00FFFF33", "w"])
IMG = np.random.rand(10, 10).astype("float32")
CI = bool(os.getenv("CI"))
LINUX = sys.platform.startswith("linux")


def test_colour_support() -> None:
Expand Down Expand Up @@ -79,19 +81,20 @@ def test_plotly() -> None:
px.imshow(IMG, color_continuous_scale=CMAP.to_plotly())


@pytest.mark.skipif(bool(os.getenv("CI")), reason="segfaults")
@pytest.mark.skipif(CI and LINUX, reason="need to fix drivers")
def test_pygfx(qapp: "QApplication") -> None:
from qtpy.QtWidgets import QWidget

gfx = pytest.importorskip("pygfx")
pytest.importorskip("pygfx")
auto = pytest.importorskip("wgpu.gui.auto")
import pygfx as gfx

canvas = auto.WgpuCanvas(size=IMG.shape)
renderer = gfx.renderers.WgpuRenderer(canvas)
camera = gfx.OrthographicCamera(*IMG.shape)
camera.position.y = IMG.shape[0] / 2
camera.position.x = IMG.shape[1] / 2
camera.scale.y = -1
# camera.position.y = IMG.shape[0] / 2
# camera.position.x = IMG.shape[1] / 2
# camera.scale.y = -1

scene = gfx.Scene()
scene.add(
Expand Down

0 comments on commit 0d03d78

Please sign in to comment.