Skip to content

Drastic decrease in performance for matplotlib plotting #297

Closed

Description

@LucaMarconato

I found the cause of the bug, shown in a couple of message down. Merging the fix PR and closing this issue.

I am experience a drastic decrease in performance when plotting shapes, observed on the Visium HD notebook.

The notebook from the docs (which is using either the latest release code but maybe also some earlier code), took ~10 seconds to make this plot, for ~90000 shapes:
image

With the latest main, using method='matplotlib' didn't finish in one hour. Here is the code

%%time
plt.figure(figsize=(10, 10))
ax = plt.gca()

gene_name = "AA986860"
sdata.pl.render_shapes("Visium_HD_Mouse_Small_Intestine_square_016um", color=gene_name, method="matplotlib").pl.show(
    coordinate_systems="global", ax=ax
)

I have therefore used datashader, which took ~40 seconds (still worse than the original code, but I reported this in a separate issue #296). Here I would put the focus on finding out why matplotlib based plotting got so slow.
image

A final note, I am setting up a codspeed GitHub Action so in the future benchmarks will be automatically run at each commit; I am working on this in a draft PR #295.

Activity

  1. LucaMarconato commented on Jul 13, 2024

    @LucaMarconato
    MemberAuthor

    Here is the viz from a .pstat benchmark file collected by running the test (recorded while executing 'method'='matplotlib')

    @pytest.mark.parametrize("method", ["matplotlib", "datashader"])
    @pytest.mark.benchmark
    def test_plot_can_render_large_polygons(method: str):
        sdata.pl.render_shapes(element="polygons", method=method).pl.show()

    (test available from the PR #295).
    spatialdata-plot

  2. LucaMarconato commented on Jul 13, 2024

    @LucaMarconato
    MemberAuthor

    It seems that the problem are the calls to add_collection() from spatialdata_plot.pl.render._render_shapes()

        elif method == "matplotlib":
            _cax = _get_collection_shape(
                shapes=shapes,
                s=render_params.scale,
                c=color_vector,
                render_params=render_params,
                rasterized=sc_settings._vector_friendly,
                cmap=render_params.cmap_params.cmap,
                norm=norm,
                fill_alpha=render_params.fill_alpha,
                outline_alpha=render_params.outline_alpha,
                zorder=render_params.zorder,
                # **kwargs,
            )
            cax = ax.add_collection(_cax)
    
            # Transform the paths in PatchCollection
            for path in _cax.get_paths():
                path.vertices = trans.transform(path.vertices)
                cax = ax.add_collection(_cax)

    Any comment?

  3. LucaMarconato commented on Jul 13, 2024

    @LucaMarconato
    MemberAuthor

    Removing the line cax = ax.add_collection(_cax) fixes the issue; it was introduced in this PR here: https://github.com/scverse/spatialdata-plot/pull/244/files#diff-eb3d4aa0a6246689774651947ba2b351d639ab0f0585b67de070a3c33f2e92f4.

    It seems to be just a typo after a copy-paste.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions