Skip to content

Conversation

@jojoelfe
Copy link
Collaborator

This improves the reading/writing of meshes. One issue that I stumbled upon is that in the mod format the vertices actually contain vertices+normals (most of the time anyway). I modified the vertices and indices properties to remove the normals, which are now available through the normal property.

I also added support for writing of meshes. It's still a bit clunky, but functional

image

I also started to create some example notebooks under docs/examples

Read meshes
Write meshes

Minor refactors

Dependency Updates:

  • Added numpydantic to the dependencies in pyproject.toml to support new data structures.

Model Enhancements:

  • Updated the MeshHeader class to use the new MeshFlags class and added a field_validator for the flags attribute.
  • Modified the Mesh class to initialize with default values, added a model_validator to update sizes, and added new properties and setters for vertices, normals, and indices. [1] [2]

Code Refactoring:

  • Replaced the flag attribute with flags in the ModFileSpecification class.
  • Updated the Contour class to use NDArray from numpydantic for the points attribute.

Testing Enhancements:

  • Added new test test_read_write_read_roundtrip_mesh to verify the roundtrip read and write of mesh data.
  • Added new test test_mesh_flags to check the mesh flags property.

Copy link
Contributor

Copilot AI left a comment

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 improves mesh reading and writing by updating the mesh model, header, and flag handling while adding new tests and example notebooks. Key changes include:

  • New tests in tests/test_model_api.py for mesh roundtrip and flag behavior.
  • Updates in src/imodmodel/models.py to introduce MeshFlags, interleave vertices and normals, and adjust indices handling.
  • Refactors in writers, binary specification, and pyproject.toml along with an added example notebook.

Reviewed Changes

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

Show a summary per file
File Description
tests/test_model_api.py Added new tests to verify mesh read/write roundtrips and flag property functionality.
src/imodmodel/writers.py Changed mesh header serialization from .dict() to dict(header) in header writing logic.
src/imodmodel/models.py Introduced MeshFlags, updated MeshHeader defaults and validators, and refined vertices/indices properties.
src/imodmodel/binary_specification.py Updated field name from 'flag' to 'flags' to support the new MeshFlags implementation.
pyproject.toml Added "numpydantic" to support new data structures.
docs/examples/smiley.ipynb Added an example notebook demonstrating mesh export functionality.
Files not reviewed (1)
  • docs/examples/export_mesh.ipynb: File too large
Comments suppressed due to low confidence (1)

src/imodmodel/writers.py:64

  • Using dict(header) instead of header.dict() may bypass pydantic's custom processing; please confirm that the intended fields are correctly serialized.
_write_to_specification(file, ModFileSpecification.MESH_HEADER, dict(header))

jojoelfe and others added 2 commits March 29, 2025 16:34
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@jojoelfe
Copy link
Collaborator Author

  • Using dict(header) instead of header.dict() may bypass pydantic's custom processing; please confirm that the intended fields are correctly serialized.

Yes, Mr. AI, this is intentional to avoid converting the Flags submodel into a dictionary

Copy link
Collaborator

@alisterburt alisterburt left a comment

Choose a reason for hiding this comment

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

looks fantastic! Minor suggestion would be to move the obj into examples/data

Thanks for adding the notebooks too - is it easy to render notebooks in mkdocs do you know?

@alisterburt
Copy link
Collaborator

what made you remove numpydantic? I assume it avoids having to set up the custom model config when fields are numpy arrays? neat!

@jojoelfe
Copy link
Collaborator Author

is it easy to render notebooks in mkdocs do you know?

I think so, let me try.

what made you remove numpydantic? I assume it avoids having to set up the custom model config when fields are numpy arrays? neat!

Yeah, I thought it would be neat to specify array shapes that way instead of using validators. There seem to be two packages numpydantic and pydantic-numpy. numpydantic seemed to have the cleaner API, but I haven't really looked into advantages/diadvantages. Removed it for now because it is incompatible with python 3.8 and broke the tests.

@jojoelfe
Copy link
Collaborator Author

is it easy to render notebooks in mkdocs do you know?

I think so, let me try.

Yes, super simple, just added a plugin. Looks great, too, the trimesh web-thing renders properly.

@alisterburt
Copy link
Collaborator

Removed it for now because it is incompatible with python 3.8 and broke the tests.

if you want to add it back in and deprecate 3.8 that's fine by me! I follow NEP29 for python versions

amazing re: docs, look forward to seeing these online

@jojoelfe jojoelfe merged commit c1b7d53 into teamtomo:main Mar 30, 2025
5 checks passed
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.

2 participants