Skip to content

Do not set the collections transform#59

Merged
andrewdnolan merged 1 commit intoE3SM-Project:mainfrom
andrewdnolan:collection_transform
Apr 14, 2026
Merged

Do not set the collections transform#59
andrewdnolan merged 1 commit intoE3SM-Project:mainfrom
andrewdnolan:collection_transform

Conversation

@andrewdnolan
Copy link
Copy Markdown
Collaborator

@andrewdnolan andrewdnolan commented Apr 13, 2026

Not setting the collection transform, for GeoAxes instances, results in ~50% (or more) speed up in rendering. I reached out to the cartopy dev's about whether this was really needed, but have not heard. If we were making an obvious mistake here, I think they would have said something.

Native mesh contouring (#52) also produces matplotlib collections on map projected figures. We do not set the transform there and have not had any issues. So, I'm feeling comfortable moving forward with this (for now at least).

If we get a response or issues arise, we can always revert these changes.

Closes #33.

@andrewdnolan andrewdnolan self-assigned this Apr 13, 2026
@andrewdnolan andrewdnolan added the enhancement New feature or request label Apr 13, 2026
@andrewdnolan
Copy link
Copy Markdown
Collaborator Author

Testing: IcoswISC30E3r5

Without polycollection transform set:

$ hyperfine ./IcoswISC30E3r5.py 
Benchmark 1: ./IcoswISC30E3r5.py
  Time (mean ± σ):      4.947 s ±  0.120 s    [User: 4.267 s, System: 0.379 s]
  Range (min … max):    4.831 s …  5.132 s    10 runs

With polycollection transform set:

$ hyperfine ./IcoswISC30E3r5.py 
Benchmark 1: ./IcoswISC30E3r5.py
  Time (mean ± σ):     11.314 s ±  0.172 s    [User: 10.683 s, System: 0.387 s]
  Range (min … max):   11.039 s … 11.588 s    10 runsuns
./IcoswISC30E3r5.py
#!/usr/bin/env python

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import tempfile

from pathlib import Path

import mosaic


projection = ccrs.PlateCarree()
transform = ccrs.Geodetic()

ds = mosaic.datasets.open_dataset("mpaso.IcoswISC30E3r5")

descriptor = mosaic.Descriptor(ds, projection=projection, transform=transform)

fig, ax = plt.subplots(subplot_kw={"projection": projection})

mosaic.polypcolor(ax, descriptor, ds.bottomDepth)

with tempfile.TemporaryDirectory() as temp_dir:
    fig.savefig(
        Path(temp_dir) / "IcoswISC30E3r5.png", dpi=300, bbox_inches="tight"
    )

@andrewdnolan
Copy link
Copy Markdown
Collaborator Author

andrewdnolan commented Apr 13, 2026

Testing: RRSwISC6to18E3r5

Without polycollection transform set:

$ hyperfine ./RRSwISC6to18E3r5.py 
Benchmark 1: ./RRSwISC6to18E3r5.py
  Time (mean ± σ):     28.381 s ±  0.463 s    [User: 26.426 s, System: 1.353 s]
  Range (min … max):   27.811 s … 29.530 s    10 runs

With polycollection transform set:

$ hyperfine ./RRSwISC6to18E3r5.py      
Benchmark 1: ./RRSwISC6to18E3r5.py
  Time (mean ± σ):     83.419 s ±  1.204 s    [User: 81.553 s, System: 1.345 s]
  Range (min … max):   81.944 s … 85.999 s    10 runs
./RRSwISC6to18E3r5.py
#!/usr/bin/env python

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import tempfile
import xarray as xr

from pathlib import Path

import mosaic

projection = ccrs.PlateCarree()
transform = ccrs.Geodetic()

ds = xr.open_dataset(
    "/Users/anolan/Downloads/mpaso.RRSwISC6to18E3r5.20250205.nc"
)

descriptor = mosaic.Descriptor(ds, projection=projection, transform=transform)

fig, ax = plt.subplots(subplot_kw={"projection": projection})

mosaic.polypcolor(ax, descriptor, ds.bottomDepth)

# fig.savefig("RRSwISC6to18E3r5.png", dpi=300, bbox_inches="tight")
with tempfile.TemporaryDirectory() as temp_dir:
    fig.savefig(
        Path(temp_dir) / "RRSwISC6to18E3r5.png", dpi=300, bbox_inches="tight"
    )

@andrewdnolan
Copy link
Copy Markdown
Collaborator Author

Testing

Using cmp on png files written with the collections transform set and unset produce bit-for-bit matches.

@andrewdnolan andrewdnolan merged commit 41a1186 into E3SM-Project:main Apr 14, 2026
8 of 18 checks passed
@andrewdnolan andrewdnolan deleted the collection_transform branch April 14, 2026 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting the transform attribute of the collection **dramatically** slows down rendering

1 participant