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

Skip to content

Replace C++ quad contouring code with use of ContourPy #22529

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
ianthomas23 opened this issue Feb 22, 2022 · 1 comment · Fixed by #22567
Closed

Replace C++ quad contouring code with use of ContourPy #22529

ianthomas23 opened this issue Feb 22, 2022 · 1 comment · Fixed by #22567
Milestone

Comments

@ianthomas23
Copy link
Member

I propose to remove the C++ quad contouring code that is in Matplotlib, which I originally wrote in 2014, and replace it with use of a new external dependency ContourPy.

ContourPy is a new Python/C++ project that contains the original (2005) and current (2014) Matplotlib contouring algorithms as well as a new one that includes more features and is faster is many situations. The project primarily exists to allow other Python libraries to have access to contouring without having to have Matplotlib as a dependency, but I am happy to do the work required for it to be used by Matplotlib as well.

The new algorithm is a complete rewrite, taking the good features of the previous two algorithms and replacing the bad features with better ones. There are new features which may be of interest eventually, such as the quad_as_tri option which divides each full quad into 4 triangles for contouring purposes, and support for multithreading (high risk at the moment). The major difference is that it supports a number of different return types so that, for example, you can request Matplotlib Path kind codes or not.

I recommend a conservative approach, which is to use the new algorithm but without any of the new features and using exactly the same return types that Matplotlib is used to. Later on we can look at those two areas which will give improved functionality and performance.

The changes for this can be done in a single PR (which I have waiting in the wings) as follows:

  1. Delete the three src/_contour* files and remove the contour section from setupext.py.
  2. Add the new dependency contour>=1 to setup.py, environment.yml and possibly other places in the CI config files.
  3. In contour.py change the import statement and creation of a ContourGenerator object. This changes only ~6 lines of code.
  4. Removing the tests of the internal contour API in test_contour.py.
  5. Baseline image changes.

The latter all occur because the new contouring algorithm returns exactly the same lines/polygons but not necessarily starting with the same point. The Agg backend renders polygons with 4+ points exactly the same regardless of starting point, but it renders triangles differently. This is a feature/bug of Agg which has been around for years.

Your concerns should be ones of packaging and maintenance.

Packaging

ContourPy is written in C++11 and wrapped using pybind11. I don't use any C++ language features invented in the last 10+ years. Pybind11 is actively maintained and builds on all hardware/OS/Python version combinations. I have PyPI wheels available for all the hardware/OS/Python version combinations that Matplotlib supports plus a few others, all via cibuildwheel. There are conda-forge packages too.

The only runtime dependency is numpy.

Maintenance

I am the only maintainer of ContourPy and I expect it will stay that way although obviously I would welcome contributions from others. My job (https://makepath.com/) is in the areas of visualisation and spatial algorithms and ContourPy sits firmly in both camps, and I have a remit to use, develop and support Open Source projects, so the short to medium term future of ContourPy is assured. I suppose the other comment here is that I am already the de facto maintainer of the Matplotlib C++ contouring and tricontouring algorithms that I wrote, but I can only recall fixing one bug in each over the last 8 and 12 years respectively, both just after initial release, so you should not have any particular concerns about the quality of my C++.

If you don't want to go ahead with this change and would prefer to keep the existing C++ code in Matplotlib then I am fine with this. I am also OK with the idea of using ContourPy but via the existing mpl2014 algorithm (as I call it) which involves a PR with all the changes above except for the baseline image changes.

@oscargus oscargus added the status: needs comment/discussion needs consensus on next step label Feb 22, 2022
@tacaswell
Copy link
Member

The consensus on the dev call is we want to do this!

  • simplifies the mpl code base
  • gives users that actually just want contours a path to get them directly
  • gives us a nice path upgrades and improved functionality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants