Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Check dimensions of arrays passed to C++ #5246

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

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 15 additions & 0 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2241,6 +2241,21 @@ def _reshape_2D(X):
return X


def ensure_3d(arr):
"""
Return a version of arr with ndim==3, with extra dimensions added
at the end of arr.shape as needed.
"""
arr = np.asanyarray(arr)
if arr.ndim == 1:
arr = arr[:, None, None]
elif arr.ndim == 2:
arr = arr[:, :, None]
elif arr.ndim > 3 or arr.ndim < 1:
raise ValueError("cannot convert arr to 3-dimensional")
return arr


def violin_stats(X, method, points=100):
'''
Returns a list of dictionaries of data which can be used to draw a series
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from numpy import ma

from matplotlib import _path
from matplotlib.cbook import simple_linear_interpolation, maxdict
from matplotlib.cbook import simple_linear_interpolation, maxdict, ensure_3d
from matplotlib import rcParams


Expand Down Expand Up @@ -988,7 +988,7 @@ def get_path_collection_extents(
if len(paths) == 0:
raise ValueError("No paths provided")
return Bbox.from_extents(*_path.get_path_collection_extents(
master_transform, paths, np.atleast_3d(transforms),
master_transform, paths, ensure_3d(transforms),
offsets, offset_transform))


Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,11 @@ def test_step_fails():
np.arange(12))
assert_raises(ValueError, cbook._step_validation,
np.arange(12), np.arange(3))


def test_ensure_3d():
assert_array_equal([[[1]], [[2]], [[3]]],
cbook.ensure_3d([1, 2, 3]))
assert_array_equal([[[1], [2]], [[3], [4]]],
cbook.ensure_3d([[1, 2], [3, 4]]))
assert_raises(ValueError, cbook.ensure_3d, [[[[1]]]])
3 changes: 2 additions & 1 deletion lib/matplotlib/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from sets import Set as set

from .path import Path
from .cbook import ensure_3d

DEBUG = False
# we need this later, but this is very expensive to set up
Expand Down Expand Up @@ -667,7 +668,7 @@ def count_overlaps(self, bboxes):
bboxes is a sequence of :class:`BboxBase` objects
"""
return count_bboxes_overlapping_bbox(
self, np.atleast_3d([np.array(x) for x in bboxes]))
self, ensure_3d([np.array(x) for x in bboxes]))

def expanded(self, sw, sh):
"""
Expand Down
36 changes: 0 additions & 36 deletions src/_backend_agg.h
Original file line number Diff line number Diff line change
Expand Up @@ -922,22 +922,6 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
typedef agg::conv_curve<snapped_t> snapped_curve_t;
typedef agg::conv_curve<clipped_t> curve_t;

if (offsets.dim(0) != 0 && offsets.dim(1) != 2) {
throw "Offsets array must be Nx2 or empty";
}

if (facecolors.dim(0) != 0 && facecolors.dim(1) != 4) {
throw "Facecolors array must be a Nx4 array or empty";
}

if (edgecolors.dim(0) != 0 && edgecolors.dim(1) != 4) {
throw "Edgecolors array must by Nx4 or empty";
}

if (transforms.dim(0) != 0 && (transforms.dim(1) != 3 || transforms.dim(2) != 3)) {
throw "Transforms array must by Nx3x3 or empty";
}

size_t Npaths = path_generator.num_paths();
size_t Noffsets = offsets.size();
size_t N = std::max(Npaths, Noffsets);
Expand Down Expand Up @@ -1266,14 +1250,6 @@ inline void RendererAgg::draw_gouraud_triangle(GCAgg &gc,
set_clipbox(gc.cliprect, theRasterizer);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans);

if (points.dim(0) != 3 || points.dim(1) != 2) {
throw "points must be a 3x2 array";
}

if (colors.dim(0) != 3 || colors.dim(1) != 4) {
throw "colors must be a 3x4 array";
}

_draw_gouraud_triangle(points, colors, trans, has_clippath);
}

Expand All @@ -1288,18 +1264,6 @@ inline void RendererAgg::draw_gouraud_triangles(GCAgg &gc,
set_clipbox(gc.cliprect, theRasterizer);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans);

if (points.dim(1) != 3 || points.dim(2) != 2) {
throw "points must be a Nx3x2 array";
}

if (colors.dim(1) != 3 || colors.dim(2) != 4) {
throw "colors must be a Nx3x4 array";
}

if (points.dim(0) != colors.dim(0)) {
throw "points and colors arrays must be the same length";
}

for (int i = 0; i < points.dim(0); ++i) {
typename PointArray::sub_t point = points[i];
typename ColorArray::sub_t color = colors[i];
Expand Down
50 changes: 43 additions & 7 deletions src/_backend_agg_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,15 @@ PyRendererAgg_draw_path_collection(PyRendererAgg *self, PyObject *args, PyObject
&convert_trans_affine,
&master_transform,
&pathobj,
&transforms.converter,
&convert_transforms,
&transforms,
&offsets.converter,
&convert_points,
&offsets,
&convert_trans_affine,
&offset_trans,
&facecolors.converter,
&convert_colors,
&facecolors,
&edgecolors.converter,
&convert_colors,
&edgecolors,
&linewidths.converter,
&linewidths,
Expand Down Expand Up @@ -411,14 +411,14 @@ static PyObject *PyRendererAgg_draw_quad_mesh(PyRendererAgg *self, PyObject *arg
&mesh_height,
&coordinates.converter,
&coordinates,
&offsets.converter,
&convert_points,
&offsets,
&convert_trans_affine,
&offset_trans,
&facecolors.converter,
&convert_colors,
&facecolors,
&antialiased,
&edgecolors.converter,
&convert_colors,
&edgecolors)) {
return NULL;
}
Expand Down Expand Up @@ -459,6 +459,21 @@ PyRendererAgg_draw_gouraud_triangle(PyRendererAgg *self, PyObject *args, PyObjec
return NULL;
}

if (points.dim(0) != 3 || points.dim(1) != 2) {
PyErr_Format(PyExc_ValueError,
"points must be a 3x2 array, got %dx%d",
points.dim(0), points.dim(1));
return NULL;
}

if (colors.dim(0) != 3 || colors.dim(1) != 4) {
PyErr_Format(PyExc_ValueError,
"colors must be a 3x4 array, got %dx%d",
colors.dim(0), colors.dim(1));
return NULL;
}


CALL_CPP("draw_gouraud_triangle", (self->x->draw_gouraud_triangle(gc, points, colors, trans)));

Py_RETURN_NONE;
Expand All @@ -485,6 +500,27 @@ PyRendererAgg_draw_gouraud_triangles(PyRendererAgg *self, PyObject *args, PyObje
return NULL;
}

if (points.dim(1) != 3 || points.dim(2) != 2) {
PyErr_Format(PyExc_ValueError,
"points must be a Nx3x2 array, got %dx%dx%d",
points.dim(0), points.dim(1), points.dim(2));
return NULL;
}

if (colors.dim(1) != 3 || colors.dim(2) != 4) {
PyErr_Format(PyExc_ValueError,
"colors must be a Nx3x4 array, got %dx%dx%d",
colors.dim(0), colors.dim(1), colors.dim(2));
return NULL;
}

if (points.dim(0) != colors.dim(0)) {
PyErr_Format(PyExc_ValueError,
"points and colors arrays must be the same length, got %d and %d",
points.dim(0), colors.dim(0));
return NULL;
}

CALL_CPP("draw_gouraud_triangles", self->x->draw_gouraud_triangles(gc, points, colors, trans));

Py_RETURN_NONE;
Expand Down
19 changes: 11 additions & 8 deletions src/_path_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static PyObject *Py_points_in_path(PyObject *self, PyObject *args, PyObject *kwd

if (!PyArg_ParseTuple(args,
"O&dO&O&:points_in_path",
&points.converter,
&convert_points,
&points,
&r,
&convert_path,
Expand Down Expand Up @@ -128,7 +128,7 @@ static PyObject *Py_points_on_path(PyObject *self, PyObject *args, PyObject *kwd

if (!PyArg_ParseTuple(args,
"O&dO&O&:points_on_path",
&points.converter,
&convert_points,
&points,
&r,
&convert_path,
Expand Down Expand Up @@ -200,7 +200,10 @@ static PyObject *Py_update_path_extents(PyObject *self, PyObject *args, PyObject
}

if (minpos.dim(0) != 2) {
PyErr_SetString(PyExc_ValueError, "minpos must be of length 2");
PyErr_Format(PyExc_ValueError,
"minpos must be of length 2, got %d",
minpos.dim(0));
return NULL;
}

extent_limits e;
Expand Down Expand Up @@ -263,9 +266,9 @@ static PyObject *Py_get_path_collection_extents(PyObject *self, PyObject *args,
&convert_trans_affine,
&master_transform,
&pathsobj,
&transforms.converter,
&convert_transforms,
&transforms,
&offsets.converter,
&convert_points,
&offsets,
&convert_trans_affine,
&offset_trans)) {
Expand Down Expand Up @@ -319,9 +322,9 @@ static PyObject *Py_point_in_path_collection(PyObject *self, PyObject *args, PyO
&convert_trans_affine,
&master_transform,
&pathsobj,
&transforms.converter,
&convert_transforms,
&transforms,
&offsets.converter,
&convert_points,
&offsets,
&convert_trans_affine,
&offset_trans,
Expand Down Expand Up @@ -464,7 +467,7 @@ static PyObject *Py_count_bboxes_overlapping_bbox(PyObject *self, PyObject *args
"O&O&:count_bboxes_overlapping_bbox",
&convert_rect,
&bbox,
&bboxes.converter,
&convert_bboxes,
&bboxes)) {
return NULL;
}
Expand Down
96 changes: 96 additions & 0 deletions src/py_converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,100 @@ int convert_face(PyObject *color, GCAgg &gc, agg::rgba *rgba)

return 1;
}

int convert_points(PyObject *obj, void *pointsp)
{
numpy::array_view<double, 2> *points = (numpy::array_view<double, 2> *)pointsp;

if (obj == NULL || obj == Py_None) {
return 1;
}

points->set(obj);

if (points->dim(0) == 0) {
return 1;
}

if (points->dim(1) != 2) {
PyErr_Format(PyExc_ValueError,
"Points must be Nx2 array, got %dx%d",
points->dim(0), points->dim(1));
return 0;
}

return 1;
}

int convert_transforms(PyObject *obj, void *transp)
{
numpy::array_view<double, 3> *trans = (numpy::array_view<double, 3> *)transp;

if (obj == NULL || obj == Py_None) {
return 1;
}

trans->set(obj);

if (trans->dim(0) == 0) {
return 1;
}

if (trans->dim(1) != 3 || trans->dim(2) != 3) {
PyErr_Format(PyExc_ValueError,
"Transforms must be Nx3x3 array, got %dx%dx%d",
trans->dim(0), trans->dim(1), trans->dim(2));
return 0;
}

return 1;
}

int convert_bboxes(PyObject *obj, void *bboxp)
{
numpy::array_view<double, 3> *bbox = (numpy::array_view<double, 3> *)bboxp;

if (obj == NULL || obj == Py_None) {
return 1;
}

bbox->set(obj);

if (bbox->dim(0) == 0) {
return 1;
}

if (bbox->dim(1) != 2 || bbox->dim(2) != 2) {
PyErr_Format(PyExc_ValueError,
"Bbox array must be Nx2x2 array, got %dx%dx%d",
bbox->dim(0), bbox->dim(1), bbox->dim(2));
return 0;
}

return 1;
}

int convert_colors(PyObject *obj, void *colorsp)
{
numpy::array_view<double, 2> *colors = (numpy::array_view<double, 2> *)colorsp;

if (obj == NULL || obj == Py_None) {
return 1;
}

colors->set(obj);

if (colors->dim(0) == 0) {
return 1;
}

if (colors->dim(1) != 4) {
PyErr_Format(PyExc_ValueError,
"Colors array must be Nx4 array, got %dx%d",
colors->dim(0), colors->dim(1));
return 0;
}

return 1;
}
}
4 changes: 4 additions & 0 deletions src/py_converters.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ int convert_snap(PyObject *obj, void *snapp);
int convert_offset_position(PyObject *obj, void *offsetp);
int convert_sketch_params(PyObject *obj, void *sketchp);
int convert_gcagg(PyObject *pygc, void *gcp);
int convert_points(PyObject *pygc, void *pointsp);
int convert_transforms(PyObject *pygc, void *transp);
int convert_bboxes(PyObject *pygc, void *bboxp);
int convert_colors(PyObject *pygc, void *colorsp);

int convert_face(PyObject *color, GCAgg &gc, agg::rgba *rgba);
}
Expand Down