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

feat: add support for pyqtgraph #32

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,17 @@ to an integer array (like an image) you must apply any contrast limits and resca

## Third Party Library Support

The `cmap.Colormap` object has convenience methods that allow it to be used with a number of third-party colormap objects (like
`matplotlib`, `vispy`, `napari`, `plotly`, etc...).
The `cmap.Colormap` object has convenience methods that export it to a number of known
third-party colormap objects, including:

- [matplotlib](https://matplotlib.org/)
- [napari](https://napari.org/)
- [vispy](https://vispy.org/)
- [pygfx](https://pygfx.readthedocs.io/en/latest/) (& [fastplotlib](https://github.com/fastplotlib/fastplotlib))
- [plotly](https://plotly.com/python/)
- [bokeh](https://docs.bokeh.org/en/latest/)
- [altair](https://altair-viz.github.io/)
- [pyqtgraph](https://www.pyqtgraph.org/)

See [documentation](https://cmap-docs.readthedocs.io/en/latest/colormaps/#usage-with-external-visualization-libraries)
for details.
Expand All @@ -104,7 +113,6 @@ If you would like to see support added for a particular library, please open an

Other libraries providing colormaps:


- [matplotlib](https://matplotlib.org/stable/tutorials/colors/colormaps.html)
- [seaborn](https://seaborn.pydata.org/tutorial/color_palettes.html) (subclasses matplotlib)
- [proplot](https://proplot.readthedocs.io/en/latest/colormaps.html) (subclasses matplotlib)
Expand All @@ -128,4 +136,4 @@ Other libraries providing colormaps:
- [Color Map Advice for Scientific Visualization](https://www.kennethmoreland.com/color-advice/)
- <https://colorcet.com/>, Peter Kovesi
- [Kovesi: Good Colour Maps: How to Design Them.](https://arxiv.org/abs/1509.03700)
- https://www.fabiocrameri.ch/colourmaps/
- <https://www.fabiocrameri.ch/colourmaps/>
9 changes: 9 additions & 0 deletions docs/colormaps.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ external visualization libraries. To that end, `cmap.Colormap` provides

Returns a list of hexadecimal color strings.

- [pyqtgraph](https://pyqtgraph.org/)

```python
Colormap("viridis").to_pyqtgraph()
```

Returns an instance of
[`pyqtgraph.ColorMap`](https://pyqtgraph.readthedocs.io/en/latest/api_reference/colormap.html#pyqtgraph.ColorMap)

## Usage with pydantic

`Colormap` can be used as a field type in
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ thirdparty = [
"rich",
"viscm",
"vispy",
"pyqtgraph",
]
dev = [
"black",
Expand Down
5 changes: 5 additions & 0 deletions src/cmap/_colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import matplotlib.figure
import napari.utils.colormaps
import pygfx
import pyqtgraph
import vispy.color
from numpy.typing import ArrayLike, NDArray
from pydantic import GetCoreSchemaHandler
Expand Down Expand Up @@ -546,6 +547,10 @@ def to_viscm(
"""
return _external.viscm_plot(self, dpi, dest)

def to_pyqtgraph(self) -> pyqtgraph.ColorMap:
"""Return a `pyqtgraph.ColorMap`."""
return _external.to_pyqtgraph(self)


class ColorStop(NamedTuple):
"""A color stop in a color gradient."""
Expand Down
12 changes: 12 additions & 0 deletions src/cmap/_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from matplotlib.colors import LinearSegmentedColormap as MplLinearSegmentedColormap
from matplotlib.figure import Figure as MplFigure
from napari.utils.colormaps import Colormap as NapariColormap
from pyqtgraph import ColorMap as PyqtgraphColorMap
from vispy.color import Colormap as VispyColormap

from ._color import Color
Expand Down Expand Up @@ -97,6 +98,17 @@ def to_altair(cm: Colormap, N: int = 256) -> list[str]:
return [color.hex for color in cm.iter_colors(N)]


def to_pyqtgraph(cm: Colormap) -> PyqtgraphColorMap:
"""Return a `pyqtgraph.Colormap`."""
from pyqtgraph import ColorMap

colors = (cm.color_stops.color_array * 255).astype(np.uint8)
return ColorMap(cm.color_stops.stops, colors, name=cm.name)


# ==========================================


def viscm_plot(
cmap: Colormap | MplColormap, dpi: int = 100, dest: str | None = None
) -> MplFigure:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_third_party.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ def test_viscm(tmp_path: Path) -> None:
assert out.is_file()


def test_pyqtgraph() -> None:
pytest.importorskip("pyqtgraph")
cmap1 = Colormap(["red", "green", "blue"])
cm = cmap1.to_pyqtgraph()

cm.getGradient()
cm.linearize()
cm.reverse()


# def microvis_imshow(img_data: np.ndarray, cmap: cmap.Colormap) -> None:
# from microvis import _util, imshow

Expand Down