Skip to content

Mesh: honor force_copy throughout PyVista conversion - #1785

Merged
peterdsharpe merged 6 commits into
NVIDIA:mainfrom
peterdsharpe:codex/mesh-pyvista-force-copy-data
Jul 9, 2026
Merged

Mesh: honor force_copy throughout PyVista conversion#1785
peterdsharpe merged 6 commits into
NVIDIA:mainfrom
peterdsharpe:codex/mesh-pyvista-force-copy-data

Conversation

@peterdsharpe

@peterdsharpe peterdsharpe commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

PhysicsNeMo Pull Request

Description

from_pyvista(..., force_copy=True) previously copied geometry but still allowed attached point, cell, and global arrays to share storage with the source PyVista object. Mutating the returned Mesh could therefore mutate an input whose caller requested independent ownership. The reverse conversion had no matching opt-in ownership guarantee.

This PR:

  • Applies force_copy to imported point, cell, and global data, including cell-centroid mode.
  • Adds keyword-only to_pyvista(..., force_copy=True) for independent exported geometry and attached data.
  • Copies only buffers that can actually alias; conversion paths that already materialize independent connectivity avoid another full-size copy.
  • Adds mutation-based regression coverage for points, connectivity, and attached data in both directions.

Checklist

Dependencies

Review Process

All PRs are reviewed by the PhysicsNeMo team before merging.

Depending on which files are changed, GitHub may automatically assign a maintainer for review.

We are also testing AI-based code review tools (e.g., Greptile), which may add automated comments with a confidence score.
This score reflects the AI’s assessment of merge readiness and is not a qualitative judgment of your work, nor is
it an indication that the PR will be accepted / rejected.

AI-generated feedback should be reviewed critically for usefulness.
You are not required to respond to every AI comment, but they are intended to help both authors and reviewers.
Please react to Greptile comments with 👍 or 👎 to provide feedback on their accuracy.

@copy-pr-bot

copy-pr-bot Bot commented Jul 2, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@peterdsharpe

Copy link
Copy Markdown
Collaborator Author

/ok to test a1dd036

@peterdsharpe

Copy link
Copy Markdown
Collaborator Author

/ok to test e039fd5

@peterdsharpe
peterdsharpe marked this pull request as ready for review July 9, 2026 19:05
@peterdsharpe
peterdsharpe requested a review from Copilot July 9, 2026 19:05
@peterdsharpe
peterdsharpe requested a review from laserkelvin July 9, 2026 19:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR strengthens copy-ownership guarantees in the PyVista ↔ physicsnemo.mesh.Mesh conversion helpers by ensuring force_copy=True covers attached data arrays (point/cell/global) and by adding a matching force_copy option on export via to_pyvista.

Changes:

  • Extend from_pyvista(..., force_copy=True) to deep-copy point/cell/field data containers (including centroid mode).
  • Add keyword-only to_pyvista(..., force_copy=True) to prevent exported PyVista objects from aliasing the source Mesh’s CPU storage.
  • Add mutation-based regression tests and update the changelog entry.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
physicsnemo/mesh/io/io_pyvista.py Applies force_copy to VTK attribute conversion and adds to_pyvista(..., force_copy=...) copying semantics.
test/mesh/io/io_pyvista/test_data_preservation.py Adds regression tests asserting force_copy prevents cross-object mutation for geometry and attached data.
CHANGELOG.md Documents the strengthened force_copy behavior and new to_pyvista option.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread physicsnemo/mesh/io/io_pyvista.py Outdated
Comment thread test/mesh/io/io_pyvista/test_data_preservation.py
@greptile-apps

greptile-apps Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes incomplete ownership isolation in the PyVista ↔ Mesh conversion layer and adds a symmetric guarantee in the export direction. When force_copy=True, point, cell, and global data arrays (and geometry) are now fully decoupled from the source object in both from_pyvista and the newly keyword-only to_pyvista.

  • _vtk_data_to_tensor_dict gains a force_copy parameter that copies each numpy array before wrapping it with torch.as_tensor, preventing returned tensors from aliasing the originating PyVista buffers.
  • to_pyvista acquires a keyword-only force_copy=False argument that copies geometry (when not already independent via padding or _to_vtk_cell_array) and all attached data arrays before handing them to PyVista.
  • A new TestForceCopy class adds mutation-based regression tests for both conversion directions and for the cell_centroids path.

Important Files Changed

Filename Overview
physicsnemo/mesh/io/io_pyvista.py Extends force_copy to cover attached data in both from_pyvista and to_pyvista; logic is correct but the noqa comment for the unannotated data parameter is misplaced onto the force_copy line, which may escape linting suppression.
test/mesh/io/io_pyvista/test_data_preservation.py Adds mutation-based regression tests for force_copy in both directions; the regular_faces mutation in the to_pyvista test may not actually exercise aliasing since PyVista's regular_faces property can return a freshly derived array on each access.
CHANGELOG.md Adds a changelog entry describing the force_copy fix for both from_pyvista and to_pyvista.

Reviews (1): Last reviewed commit: "Merge branch 'main' into codex/mesh-pyvi..." | Re-trigger Greptile

Comment thread physicsnemo/mesh/io/io_pyvista.py Outdated
Comment thread test/mesh/io/io_pyvista/test_data_preservation.py

@laserkelvin laserkelvin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll conditionally approve - the thing I'm mostly thinking about is this is really only a problem when doing compute on host right? Would we consider this undesirable anyway, and users should be moving the data to device?

Comment thread physicsnemo/mesh/io/io_pyvista.py Outdated
Comment thread physicsnemo/mesh/io/io_pyvista.py
Comment thread physicsnemo/mesh/io/io_pyvista.py
@peterdsharpe

Copy link
Copy Markdown
Collaborator Author

/ok to test 7e00bbe

@peterdsharpe
peterdsharpe enabled auto-merge July 9, 2026 21:38
@peterdsharpe
peterdsharpe disabled auto-merge July 9, 2026 21:41
@peterdsharpe

Copy link
Copy Markdown
Collaborator Author

/ok to test e88afd7

@peterdsharpe
peterdsharpe enabled auto-merge July 9, 2026 21:50
@peterdsharpe
peterdsharpe added this pull request to the merge queue Jul 9, 2026
Merged via the queue into NVIDIA:main with commit 6b4babe Jul 9, 2026
15 checks passed
@peterdsharpe
peterdsharpe deleted the codex/mesh-pyvista-force-copy-data branch July 9, 2026 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants