Skip to content

Commit

Permalink
Remove usage of nodes from more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ksunden committed Jul 26, 2024
1 parent 958eb83 commit 51aa8cf
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 34 deletions.
8 changes: 8 additions & 0 deletions data_prototype/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ def __init__(self, container, edges=None, norm=None, cmap=None, **kwargs):
{"image": Desc(("O", "P", 4), "rgba_resampled")},
{"image": Desc(("O", "P", 4), "display")},
),
FuncEdge.from_func(
"rgb_rgba",
lambda image: np.append(
image, np.ones(image.shape[:-1] + (1,)), axis=-1
),
{"image": Desc(("M", "N", 3), "rgb")},
{"image": Desc(("M", "N", 4), "rgba")},
),
self._interpolation_edge,
]

Expand Down
4 changes: 2 additions & 2 deletions data_prototype/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, container, edges=None, **kwargs):

scalar = Desc((), "display") # ... this needs thinking...

edges = [
default_edges = [
CoordinateEdge.from_coords("xycoords", {"x": "auto", "y": "auto"}, "data"),
CoordinateEdge.from_coords("color", {"color": Desc(())}, "display"),
CoordinateEdge.from_coords("linewidth", {"linewidth": Desc(())}, "display"),
Expand All @@ -45,7 +45,7 @@ def __init__(self, container, edges=None, **kwargs):
DefaultEdge.from_default_value("mew_def", "markeredgewidth", scalar, 1),
DefaultEdge.from_default_value("marker_def", "marker", scalar, "None"),
]
self._graph = self._graph + Graph(edges)
self._graph = self._graph + Graph(default_edges)
# Currently ignoring:
# - cap/join style
# - url
Expand Down
16 changes: 10 additions & 6 deletions examples/2Dfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import matplotlib.pyplot as plt
import numpy as np

from data_prototype.wrappers import ImageWrapper
from data_prototype.artist import CompatibilityAxes
from data_prototype.image import Image
from data_prototype.containers import FuncContainer

from matplotlib.colors import Normalize
Expand All @@ -19,20 +20,23 @@
fc = FuncContainer(
{},
xyfuncs={
"xextent": ((2,), lambda x, y: [x[0], x[-1]]),
"yextent": ((2,), lambda x, y: [y[0], y[-1]]),
"x": ((2,), lambda x, y: [x[0], x[-1]]),
"y": ((2,), lambda x, y: [y[0], y[-1]]),
"image": (
("N", "M"),
lambda x, y: np.sin(x).reshape(1, -1) * np.cos(y).reshape(-1, 1),
),
},
)
norm = Normalize(vmin=-1, vmax=1)
im = ImageWrapper(fc, norm=norm)
im = Image(fc, norm=norm)

fig, nax = plt.subplots()
ax = CompatibilityAxes(nax)
nax.add_artist(ax)

fig, ax = plt.subplots()
ax.add_artist(im)
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
fig.colorbar(im)
# fig.colorbar(im, ax=nax)
plt.show()
65 changes: 45 additions & 20 deletions examples/mapped.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import numpy as np

from matplotlib.colors import Normalize
from matplotlib.font_manager import FontProperties

from data_prototype.wrappers import FormattedText
from data_prototype.artist import CompatibilityArtist as CA
from data_prototype.artist import CompatibilityAxes
from data_prototype.line import Line
from data_prototype.containers import ArrayContainer
from data_prototype.description import Desc
from data_prototype.conversion_node import FunctionConversionNode
from data_prototype.conversion_edge import FuncEdge
from data_prototype.text import Text


cmap = plt.colormaps["viridis"]
Expand Down Expand Up @@ -49,19 +49,41 @@
),
]

text_converter = FunctionConversionNode.from_funcs(
{
"text": lambda j, cat: f"index={j[()]} class={cat!r}",
"y": lambda j: j,
"x": lambda x: 2 * np.pi,
},
)
text_edges = [
FuncEdge.from_func(
"text",
lambda j, cat: f"index={j[()]} class={cat!r}",
{"j": Desc((), "auto"), "cat": Desc((), "auto")},
{"text": Desc((), "display")},
),
FuncEdge.from_func(
"y",
lambda j: j,
{"j": Desc((), "auto")},
{"y": Desc((), "data")},
),
FuncEdge.from_func(
"x",
lambda: 2 * np.pi,
{},
{"x": Desc((), "data")},
),
FuncEdge.from_func(
"color",
lambda: (0, 0, 0, 1),
{},
{"color": Desc((4,), "rgba")},
),
]


th = np.linspace(0, 2 * np.pi, 128)
delta = np.pi / 9

fig, ax = plt.subplots()
fig, nax = plt.subplots()

ax = CompatibilityAxes(nax)
nax.add_artist(ax)

for j in range(10):
ac = ArrayContainer(
Expand All @@ -74,20 +96,23 @@
}
)
ax.add_artist(
CA(
Line(
ac,
line_edges,
)
Line(
ac,
line_edges,
)
)
ax.add_artist(
FormattedText(
Text(
ac,
text_converter,
text_edges,
x=2 * np.pi,
ha="right",
bbox={"facecolor": "gray", "alpha": 0.5},
# ha="right",
# bbox={"facecolor": "gray", "alpha": 0.5},
antialiased=False,
fontproperties=FontProperties(),
rotation=0,
alpha=1,
usetex=False,
)
)
ax.set_xlim(0, np.pi * 2)
Expand Down
23 changes: 17 additions & 6 deletions examples/mulivariate_cmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import matplotlib.pyplot as plt
import numpy as np

from data_prototype.wrappers import ImageWrapper
from data_prototype.image import Image
from data_prototype.artist import CompatibilityAxes
from data_prototype.description import Desc
from data_prototype.containers import FuncContainer
from data_prototype.conversion_node import FunctionConversionNode
from data_prototype.conversion_edge import FuncEdge

from matplotlib.colors import hsv_to_rgb

Expand All @@ -35,15 +37,24 @@ def image_nu(image):
fc = FuncContainer(
{},
xyfuncs={
"xextent": ((2,), lambda x, y: [x[0], x[-1]]),
"yextent": ((2,), lambda x, y: [y[0], y[-1]]),
"x": ((2,), lambda x, y: [x[0], x[-1]]),
"y": ((2,), lambda x, y: [y[0], y[-1]]),
"image": (("N", "M", 2), func),
},
)

im = ImageWrapper(fc, FunctionConversionNode.from_funcs({"image": image_nu}))
image_edges = FuncEdge.from_func(
"image",
image_nu,
{"image": Desc(("M", "N", 2), "auto")},
{"image": Desc(("M", "N", 3), "rgb")},
)

im = Image(fc, [image_edges])

fig, ax = plt.subplots()
fig, nax = plt.subplots()
ax = CompatibilityAxes(nax)
nax.add_artist(ax)
ax.add_artist(im)
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
Expand Down

0 comments on commit 51aa8cf

Please sign in to comment.