FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Draft: Experiment with using nanobind by iccir · Pull Request #32411 · matplotlib/matplotlib · GitHub

Repository navigation

Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .build  (3) .cpp  (10) .h  (6) .py  (4) .pyi  (1) .toml  (1) .txt  (1) .wrap  (2) All 8 file types selected
Only manifest files
Deleted files Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 2 additions & 2 deletions ci/mypy-stubtest-allowlist.txt
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
Expand Up @@ -29,8 +29,8 @@ matplotlib\.ticker\.LogitLocator\.nonsingular
# Stdlib/Enum considered inconsistent (no fault of ours, I don't think)
matplotlib\.backend_bases\._Mode\.__new__

# pybind11 internals
matplotlib\..*\.__pybind11_native_enum__
# nanobind internals
matplotlib\..*\.__nb_enum__

# 3.6 Pending deprecations
matplotlib\.figure\.Figure\.set_constrained_layout
Expand Down
1 change: 1 addition & 0 deletions extern/agg24-svn/include/agg_color_rgba.h
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
Expand Up @@ -25,6 +25,7 @@
#define AGG_COLOR_RGBA_INCLUDED

#include <math.h>
#include <array>
#include "agg_basics.h"
#include "agg_gamma_lut.h"

Expand Down
8 changes: 4 additions & 4 deletions lib/matplotlib/_tri.pyi
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
Expand Up @@ -25,12 +25,12 @@ class Triangulation:
x: npt.NDArray[np.float64],
y: npt.NDArray[np.float64],
triangles: npt.NDArray[np.int_],
mask: npt.NDArray[np.bool_] | tuple[()],
edges: npt.NDArray[np.int_] | tuple[()],
neighbors: npt.NDArray[np.int_] | tuple[()],
mask: npt.NDArray[np.bool_] | None,
edges: npt.NDArray[np.int_] | None,
neighbors: npt.NDArray[np.int_] | None,
correct_triangle_orientation: bool,
): ...
def calculate_plane_coefficients(self, z: npt.ArrayLike) -> npt.NDArray[np.float64]: ...
def get_edges(self) -> npt.NDArray[np.int_]: ...
def get_neighbors(self) -> npt.NDArray[np.int_]: ...
def set_mask(self, mask: npt.NDArray[np.bool_] | tuple[()]) -> None: ...
def set_mask(self, mask: npt.NDArray[np.bool_] | None) -> None: ...
33 changes: 4 additions & 29 deletions lib/matplotlib/tests/test_ft2font.py
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
Expand Up @@ -212,7 +212,7 @@ def test_ft2font_invalid_args(tmp_path):
file = fm.findfont('DejaVu Sans')

# hinting_factor argument.
with pytest.raises(TypeError, match='incompatible constructor arguments'):
with pytest.raises(TypeError, match='incompatible function arguments'):
ft2font.FT2Font(file, 1.3)
with pytest.warns(mpl.MatplotlibDeprecationWarning,
match='text.hinting_factor rcParam was deprecated .+ 3.11'):
Expand All @@ -221,14 +221,14 @@ def test_ft2font_invalid_args(tmp_path):
match='The hinting_factor parameter was deprecated'):
ft2font.FT2Font(file, 0)

with pytest.raises(TypeError, match='incompatible constructor arguments'):
with pytest.raises(TypeError, match='incompatible function arguments'):
# failing to be a list will fail before the 0
ft2font.FT2Font(file, _fallback_list=(0,))
with pytest.raises(TypeError, match='incompatible constructor arguments'):
with pytest.raises(TypeError, match='incompatible function arguments'):
ft2font.FT2Font(file, _fallback_list=[0])

# kerning_factor argument.
with pytest.raises(TypeError, match='incompatible constructor arguments'):
with pytest.raises(TypeError, match='incompatible function arguments'):
ft2font.FT2Font(file, _kerning_factor=1.3)
with pytest.warns(mpl.MatplotlibDeprecationWarning,
match='text.kerning_factor rcParam was deprecated .+ 3.11'):
Expand Down Expand Up @@ -1077,31 +1077,6 @@ def test__layout():
assert Path(item.ft_object.fname).name == 'cmr10.ttf'


def test_render_glyph_cache():
# Reusing a cached outline must not change what is rendered.
ft = fm.get_font(fm.findfont('DejaVu Sans'))
ft.set_size(12, 100)
index = ft.get_char_index(ord('e'))
identity = [[0x10000, 0], [0, 0x10000]]

def render(delta=(0, 0)):
ft._set_transform(identity, list(delta))
return ft._render_glyph(index, ft2font.LoadFlags.DEFAULT,
ft2font.RenderMode.NORMAL)

first = render()
reference = first.buffer.copy()
# A whole-pixel shift reuses the outline and only moves the glyph.
shifted = render(delta=(0x40 * 3, 0x40 * 5))
assert np.array_equal(shifted.buffer, reference)
assert (shifted.left, shifted.top) == (first.left + 3, first.top + 5)
# A fractional shift must reach the rasterizer rather than be rounded away.
assert not np.array_equal(render(delta=(0x20, 0x20)).buffer, reference)
# The size is part of the key.
ft.set_size(24, 100)
assert render().buffer.shape != reference.shape


def test_layout_cache():
# A cached layout must match a fresh one, and the size is part of the key.
ft = fm.get_font(fm.findfont('DejaVu Sans'))
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_image.py
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
Expand Up @@ -1671,12 +1671,12 @@ def test__resample_valid_output():
resample(np.zeros((9, 9, 4)), np.zeros((9, 9, 3)))
with pytest.raises(ValueError, match="mismatched types"):
resample(np.zeros((9, 9), np.uint8), np.zeros((9, 9)))
with pytest.raises(ValueError, match="must be C-contiguous"):
with pytest.raises(TypeError, match="incompatible function arguments"):
resample(np.zeros((9, 9)), np.zeros((9, 9)).T)

out = np.zeros((9, 9))
out.flags.writeable = False
with pytest.raises(ValueError, match="Output array must be writeable"):
with pytest.raises(TypeError, match="incompatible function arguments"):
resample(np.zeros((9, 9)), out)


Expand Down
41 changes: 23 additions & 18 deletions lib/matplotlib/tests/test_triangulation.py
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
Expand Up @@ -1190,63 +1190,68 @@ def test_internal_cpp_api() -> None:
# C++ Triangulation.
with pytest.raises(
TypeError,
match=r'__init__\(\): incompatible constructor arguments.'):
match=r'__init__\(\): incompatible function arguments.'):
mpl._tri.Triangulation() # type: ignore[call-arg]

with pytest.raises(
ValueError, match=r'x and y must be 1D arrays of the same length'):
mpl._tri.Triangulation(np.array([]), np.array([1]), np.array([[]]), (), (), (),
False)
mpl._tri.Triangulation(np.array([]), np.array([1]), np.array([[]]),
None, None, None, False)

x = np.array([0, 1, 1], dtype=np.float64)
y = np.array([0, 0, 1], dtype=np.float64)
with pytest.raises(
ValueError,
match=r'triangles must be a 2D array of shape \(\?,3\)'):
mpl._tri.Triangulation(x, y, np.array([[0, 1]]), (), (), (), False)
mpl._tri.Triangulation(x, y, np.array([[0, 1]]), None, None, None, False)

tris = np.array([[0, 1, 2]], dtype=np.int_)
with pytest.raises(
ValueError,
match=r'mask must be a 1D array with the same length as the '
r'triangles array'):
mpl._tri.Triangulation(x, y, tris, np.array([0, 1]), (), (), False)
mpl._tri.Triangulation(x, y, tris, np.array([0, 1]), None, None, False)

with pytest.raises(
ValueError, match=r'edges must be a 2D array with shape \(\?,2\)'):
mpl._tri.Triangulation(x, y, tris, (), np.array([[1]]), (), False)
mpl._tri.Triangulation(x, y, tris, None, np.array([[1]]), None, False)

with pytest.raises(
ValueError,
match=r'neighbors must be a 2D array with the same shape as the '
r'triangles array'):
mpl._tri.Triangulation(x, y, tris, (), (), np.array([[-1]]), False)
mpl._tri.Triangulation(x, y, tris, None, None, np.array([[-1]]), False)

triang = mpl._tri.Triangulation(x, y, tris, (), (), (), False)
triang = mpl._tri.Triangulation(x, y, tris, None, None, None, False)

with pytest.raises(TypeError, match=r': incompatible function arguments'):
triang.calculate_plane_coefficients([]) # type: ignore[arg-type]

with pytest.raises(
ValueError,
match=r'z must be a 1D array with the same length as the '
r'triangulation x and y arrays'):
triang.calculate_plane_coefficients([])
triang.calculate_plane_coefficients(np.empty(0))

for mask in ([0, 1], None):
with pytest.raises(
ValueError,
match=r'mask must be a 1D array with the same length as the '
r'triangles array'):
triang.set_mask(mask) # type: ignore[arg-type]
with pytest.raises(TypeError, match=r': incompatible function arguments'):
triang.set_mask([]) # type: ignore[arg-type]

with pytest.raises(
ValueError,
match=r'mask must be a 1D array with the same length as the '
r'triangles array'):
triang.set_mask(np.asarray([0, 1])) # type: ignore[arg-type]

triang.set_mask(np.array([True]))
assert_array_equal(triang.get_edges(), np.empty((0, 2)))

triang.set_mask(()) # Equivalent to Python Triangulation mask=None
triang.set_mask(None)
assert_array_equal(triang.get_edges(), [[1, 0], [2, 0], [2, 1]])

# C++ TriContourGenerator.
with pytest.raises(
TypeError,
match=r'__init__\(\): incompatible constructor arguments.'):
match=r'__init__\(\): incompatible function arguments.'):
mpl._tri.TriContourGenerator() # type: ignore[call-arg]

with pytest.raises(
Expand All @@ -1264,7 +1269,7 @@ def test_internal_cpp_api() -> None:
# C++ TrapezoidMapTriFinder.
with pytest.raises(
TypeError,
match=r'__init__\(\): incompatible constructor arguments.'):
match=r'__init__\(\): incompatible function arguments.'):
mpl._tri.TrapezoidMapTriFinder() # type: ignore[call-arg]

trifinder = mpl._tri.TrapezoidMapTriFinder(triang)
Expand Down
15 changes: 8 additions & 7 deletions lib/matplotlib/tri/_triangulation.py
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
Expand Up @@ -57,7 +57,8 @@ def __init__(self, x, y, triangles=None, mask=None):
if triangles is None:
# No triangulation specified, so use matplotlib._qhull to obtain
# Delaunay triangulation.
self.triangles, self._neighbors = _qhull.delaunay(x, y, sys.flags.verbose)
self.triangles, self._neighbors = _qhull.delaunay(self.x, self.y,
sys.flags.verbose)
self.is_delaunay = True
else:
# Triangulation specified. Copy, since we may correct triangle
Expand Down Expand Up @@ -98,6 +99,7 @@ def calculate_plane_coefficients(self, z):
position in triangle tri to be calculated using
``z = array[tri, 0] * x + array[tri, 1] * y + array[tri, 2]``.
"""
z = np.asarray(z, dtype=np.float64)
return self.get_cpp_triangulation().calculate_plane_coefficients(z)

@property
Expand All @@ -122,11 +124,11 @@ def get_cpp_triangulation(self):
from matplotlib import _tri
if self._cpp_triangulation is None:
self._cpp_triangulation = _tri.Triangulation(
# For unset arrays use empty tuple which has size of zero.
# For unset arrays use None
self.x, self.y, self.triangles,
self.mask if self.mask is not None else (),
self._edges if self._edges is not None else (),
self._neighbors if self._neighbors is not None else (),
self.mask,
self._edges,
self._neighbors,
not self.is_delaunay)
return self._cpp_triangulation

Expand Down Expand Up @@ -235,8 +237,7 @@ def set_mask(self, mask):

# Set mask in C++ Triangulation.
if self._cpp_triangulation is not None:
self._cpp_triangulation.set_mask(
self.mask if self.mask is not None else ())
self._cpp_triangulation.set_mask(self.mask)

# Clear derived fields so they are recalculated when needed.
self._edges = None
Expand Down
4 changes: 4 additions & 0 deletions meson.build
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
Expand Up @@ -48,6 +48,10 @@ py3 = py_mod.find_installation(pure: false)
py3_dep = py3.dependency()

pybind11_dep = dependency('pybind11', version: '>=3')
nanobind_dep = declare_dependency(
dependencies: dependency('nanobind'),
compile_args: ['-DNOMINMAX'],
)

subdir('extern')
subdir('src')
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
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
Expand Up @@ -60,6 +60,7 @@ requires = [
# you really need it and aren't using an sdist.
"meson-python>=0.13.2,!=0.17.*",
"pybind11>=3",
"nanobind",
# setuptools_scm 10 breaks versioning in editable installs. You can remove this pin
# if you're a downstream distributor just building wheels or your equivalent.
"setuptools_scm>=7,<10",
Expand All @@ -81,6 +82,7 @@ build = [
# Should be the same as `[build-system] requires` above.
"meson-python>=0.13.1,!=0.17.*",
"pybind11>=3",
"nanobind",
"setuptools_scm>=7,<10",
# Not required by us but setuptools_scm without a version, so _if_
# installed, then setuptools_scm 8 requires at least this version.
Expand Down
Loading
Loading

Back | FazBrowse Home | New Git URL