Repository navigation
fix(feature): position- and device-independent mesh curvature features - #68
alxndrkalinin wants to merge 4 commits into
Conversation
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
Reviewer's GuideAPIAdd a reusable device context and make device transfer target the GPU holding the reference array.
Run the public mesh feature entry points on their input device and define the updated curvedness-weighted shape-index behavior.
ImplementationKeep ArrayMesh construction, scaling, and lazy geometry and adjacency calculations on the mesh's GPU.
Crop masks to occupied bounds, restore cropped blurred-mesh coordinates, and recompute level-set vertices in float64 for position-independent measurements.
TestsCover position-independent mask measurements, float64 level-set geometry, curvedness-weighted shape index, and preserved cropped-mesh coordinates on CPU and GPU.
Verify reference-GPU transfers and the device context manager, including no-op host behavior.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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
Stacked on #66.
Changes
Measure a mask over its object's bounding box (1ef9d29).
extract_surface_features, andmask2mesh(..., 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 valuesextract_featuresgives 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
ArrayMeshproperty ran on the current device. A newcubic.cuda.on_device_ofmakes the array's GPU current; the four entry points andArrayMesh's cached properties run under it.to_same_devicenow 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)
Test plan
tests/: 1017 passedtests/: 1209 passed; CuPy 14.0.1 mesh + cuda tests: 71 passed🤖 Generated with Claude Code
https://claude.ai/code/session_011hEQ1TZJUbmhYKcC14RYHT