Skip to content

fix(feature): position- and device-independent mesh curvature features - #68

Draft
alxndrkalinin wants to merge 4 commits into
fix/mesh-measurement-accuracyfrom
fix/mesh-device-and-position
Draft

alxndrkalinin wants to merge 4 commits into
fix/mesh-measurement-accuracyfrom
fix/mesh-device-and-position

Conversation

@alxndrkalinin

@alxndrkalinin alxndrkalinin commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #66.

Changes

  • Measure a mask over its object's bounding box (1ef9d29). extract_surface_features, and mask2mesh(..., sigma=...), blurred and meshed the mask in its own frame. Marching cubes returns float32 vertices, which round coarser far from the frame's origin, so a nucleus 2,030 voxels from the origin of a full-frame mask moved Avg Shape Index by 6.6e-4 relative (up to 3.7e-2 at anisotropic spacing). Both now crop to the object's box first; the far and near copies give identical values, and a mask gives the values extract_features gives the same object as a label. Masks that are not cropped tight move slightly (4e-6 relative on a test ellipsoid).

  • Keep a mesh on the GPU of its mask (d77f892). Only marching cubes ran on the input's GPU; padding, face reversal, the vertex merge and every ArrayMesh property ran on the current device. A new cubic.cuda.on_device_of makes the array's GPU current; the four entry points and ArrayMesh's cached properties run under it. to_same_device now places the source on the reference's GPU instead of the current one.

  • Weight the shape index by curvedness (82df452). The shape index is the angle of (k1, k2), so at a nearly flat vertex rounding sets it, and the area average counted such vertices in full: bending a cube's faces by 1e-9 moved it from 0.509 to 0.310, and NumPy and CuPy differed by up to 6.0e-7 on real nuclei. Each vertex now counts by area times curvedness, so its term goes to 0 continuously as it flattens. Constant-index surfaces read as before (sphere 1, cylinder 0.5, saddle 0); elsewhere values move (median nucleus 0.35-0.46 -> 0.49-0.54; Spearman 0.84-0.96 against the old values). This is a redefinition of an unreleased feature.

  • Place blurred level-set vertices in float64 (52b8475). Marching cubes rounds vertices to float32 at a step that grows with the index, and the curvature estimates amplified it: Total Absolute Gaussian Curvature was up to 3.6e-2 (median 2e-5 to 1.8e-4) and Avg Curvedness up to 2.8e-3 off the float64-vertex value on real nuclei. Each float32 vertex strictly inside a grid edge is re-interpolated in float64 from the field (45 lines; vertices on grid points keep float32). The rule reads only the float32 vertex and the field, so CPU and GPU give the same float64 mesh.

Effect on real nuclei (2,392 nuclei, three datasets)

  • CPU vs GPU, 99 nuclei: all 990 level-set meshes bitwise equal; Avg Shape Index within 1.9e-11, curvedness 2.2e-11, TAGC 1.2e-14.
  • Classification vs fix(feature): deterministic bounding shapes, accurate dihedral angles, fixed-order GPU sums #66, paired per fold on the benchmark's primary splits: the 18 size/shape features are unchanged. With the four smoothed features added: fibroblast +0.005 (logreg) / -0.002 (gboost); astrocyte (3 folds) +0.035 / -0.011. Shape index alone: fibroblast 0.479 -> 0.542, astrocyte 0.480 -> 0.527, PC3 0.602 -> 0.501.

Test plan

🤖 Generated with Claude Code

https://claude.ai/code/session_011hEQ1TZJUbmhYKcC14RYHT

alxndrkalinin and others added 2 commits November 20, 2026 10:35
extract_surface_features meshed and blurred the mask in its own frame.
Marching cubes returns float32 vertices, which round coarser far from
the frame's origin, so a nucleus 2,030 voxels from the origin of a
full-frame mask moved Avg Shape Index by 6.6e-4 relative, Total Absolute
Gaussian Curvature by 5.7e-4 and Avg Curvedness by 3.0e-4, and by up to
3.7e-2 at spacing (0.29, 0.1, 0.13). extract_features crops each label
to its bounding box and did not move.

extract_surface_features now crops the mask to the bounding box of its
nonzero voxels before any mesh or blur, which pad it as they need, so
the far copy gets the near copy's values exactly and a mask gets the
values extract_features gives the same object as a label.
mask2mesh(..., sigma=...) had the same shift in its blurred mesh (1.2e-4
in vertex position); it now blurs and meshes the crop too and moves the
vertices back to the caller's frame in float64. The module docstring
again states that the features do not depend on the object's position.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011hEQ1TZJUbmhYKcC14RYHT
Given a CuPy mask on GPU 1 while GPU 0 was current, mask2mesh returned
its mesh on GPU 0 with the right values. CuPy allocates new arrays and
runs kernels on the current device, and only marching cubes itself was
run on the input's device: the pad before it, the reversed faces, the
shifted vertices, ArrayMesh's vertex merge and lazily computed arrays,
and the blurs and crops of the feature functions ran with GPU 0 current.

cubic.cuda.on_device_of makes the GPU that holds an array current.
mask2mesh, extract_surface_features, extract_features and
extract_mesh_features run under the device of their first argument, and
ArrayMesh computes its merge and every cached property under the device
of its vertices. to_same_device ignored the reference's GPU and placed
the source on the current one; it now places it on the reference's
GPU, copying a CuPy source from another GPU, and returns a source
already there as it is. ArrayMesh.scaled keeps its device context,
through on_device_of: its new vertices are computed before any
to_same_device call. The two-GPU tests skip on one GPU.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011hEQ1TZJUbmhYKcC14RYHT
@sourcery-ai

sourcery-ai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

API

Add a reusable device context and make device transfer target the GPU holding the reference array.

  • cubic/cuda.py L11, L334-370

Run the public mesh feature entry points on their input device and define the updated curvedness-weighted shape-index behavior.

  • cubic/feature/mesh.py L107-125, L165-169, L1939-1950 (+5 small edits)

Implementation

Keep ArrayMesh construction, scaling, and lazy geometry and adjacency calculations on the mesh's GPU.

  • cubic/feature/_array_mesh.py L37-51, L227-243, L361-369 (+16 small edits)

Crop masks to occupied bounds, restore cropped blurred-mesh coordinates, and recompute level-set vertices in float64 for position-independent measurements.

  • cubic/feature/mesh.py L60-66, L660-708, L877-921 (+3 small edits)

Tests

Cover position-independent mask measurements, float64 level-set geometry, curvedness-weighted shape index, and preserved cropped-mesh coordinates on CPU and GPU.

  • tests/feature/conftest.py L3-4, L123-179
  • tests/feature/test_mesh.py L532-543, L731-754, L894-929 (+9 small edits)
  • tests/feature/test_mesh_gpu.py L105-135, L380-431, L1056-1066
  • tests/feature/test_mesh_units.py L263-265, L276-279

Verify reference-GPU transfers and the device context manager, including no-op host behavior.

  • tests/test_cuda.py L17, L74-115

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

alxndrkalinin and others added 2 commits November 20, 2026 14:10
Avg Shape Index averaged (2 / pi) atan2(k1 + k2, k2 - k1) by area over
the vertices with k1 or k2 nonzero. The shape index is the angle of
(k1, k2), so rounding sets it at a nearly flat vertex, and the area
average counted that vertex in full. Bending the faces of a subdivided
cube by up to 1e-9 moved the average from 0.509 to 0.310. On the 999
fibroblast nuclei, NumPy and CuPy differed by up to 6.0e-7 relative (121
nuclei above 1e-9). The cause was the flat-topped vertices, where the
two devices' arccos angle defects differ by an ulp.

Each vertex now counts by its area times its curvedness,
sqrt((k1^2 + k2^2) / 2), which is the length of (k1, k2). A vertex's term
goes to 0 continuously as the vertex flattens, and planar vertices drop
out without a separate test. The bent cube now moves by 1.5e-8, and NumPy
and CuPy agree to 4.4e-11 on all 2,392 nuclei of the three datasets.

A float64-vertex prototype of the level-set mesh, without the
bounding-box crop, shows the same on nuclei. A 1-voxel frame shift moved
the area average by up to 7.2e-8 (63 of 999 fibroblast nuclei above
1e-9), and multiplying the blur by 1 + 1e-15 u moved it by up to 4e-4.
Weighted by curvedness, those changes are at most 3.4e-11 and 6.6e-12.

Surfaces of constant shape index read as before: sphere 1, cylinder 0.5,
saddle 0. Elsewhere the values move. A blurred ball goes from 0.916 to
0.904, and the median nucleus from 0.35-0.46 to 0.49-0.54, with a
Spearman correlation of 0.84 to 0.96 against the old values.

The flat-angle and flat-defect floors stay. Without them a raw mesh's
shape index varies by 1.2e-10 relative over scale and orientation, and
curvedness and mean curvature still use them.

The ellipsoid test now compares with the curvedness-weighted analytic
average, which the mesh matches within 4.6e-5; the area average is
9.5e-4 away. A new test bends the faces of a cube by 1e-9.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011hEQ1TZJUbmhYKcC14RYHT
Both marching cubes compute each edge vertex in float64 and round it to
float32 at a step that grows with the index, so a level set's vertices
depended on where its object lay in the array, and the curvature
estimates amplified the rounding: Total Absolute Gaussian Curvature was
up to 3.6e-2 and Avg Curvedness up to 4e-3 off its float64-vertex value
on real nuclei.

_level_set now meshes at unit spacing and recomputes, in float64, every
vertex that float32 leaves strictly inside a grid edge: a float32 vertex
with exactly one non-integer coordinate lies on the edge from its floor
along that axis, and the interpolation is redone there when that edge
straddles the level and the result rounds back to the float32 vertex.
Vertices that float32 put on a grid point, and cube-centre vertices,
keep their float32 position. The rule reads only the float32 vertex and
the field, so it runs unchanged on the GPU's output, which is skimage's
up to order, and both devices give the same float64 mesh; vertices
coincide in float64 exactly when they do in float32, so the merge of
degenerate faces is unchanged. The vertices are then scaled by the step
in float64.

The flat-residue test no longer asserts that no angle defect lies in
(1e-14, 1e-13]: with float64 vertices real defects reach down to
2.5e-14, and what the defect floor zeroes is still at most four ulps
of 2 pi.

New tests: a blurred ball's level set placed at index 1 and at 1500
agrees to 1.1e-13 after the shift (6.0e-5 in float32), and CPU and GPU
give the same float64 level set bit for bit on a field with a third of
its near-level values set to 0.5 or a float32 neighbour of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011hEQ1TZJUbmhYKcC14RYHT
@alxndrkalinin alxndrkalinin changed the title fix(feature): position- and device-independent mesh features fix(feature): position- and device-independent mesh curvature features Nov 20, 2026

This branch has not been deployed

No deployments
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.

1 participant