Skip to content

Commit 6bf726b

Browse files
committed
fix dvhcalc: include boundary voxels in mask and preserve final bin
* Expand polygons by a tiny radius before calling Path.contains_points so voxels on contour boundaries are treated as inside. * Fall back to PixelSpacing when LUT diffs are empty, allowing volume calculation on single-pixel grids.
1 parent 27fc624 commit 6bf726b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

dicompylercore/dvhcalc.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,10 @@ def get_contour_mask(dd, id, dosegridpoints, contour):
299299
# mask = inpolygon(p, x.ravel(), y.ravel())
300300
# return mask.reshape((len(doselut[1]), len(doselut[0])))
301301

302-
grid = c.contains_points(dosegridpoints)
302+
# ``contains_points`` excludes points on the polygon boundary which can
303+
# occur when a contour lies exactly on a voxel edge. Use a tiny positive
304+
# radius to expand the polygon and include boundary points.
305+
grid = c.contains_points(dosegridpoints, radius=1e-9)
303306
if dd['x_lut_index'] == 0: # X values across columns
304307
grid = grid.reshape((len(doselut[1]), len(doselut[0])))
305308
else: # decubitus
@@ -318,9 +321,12 @@ def calculate_contour_dvh(mask, doseplane, maxdose, dd, id, structure):
318321
range=(0, maxdose))
319322

320323
# Calculate the volume for the contour for the given dose plane
321-
vol = sum(hist) * (abs(np.mean(np.diff(dd['lut'][0]))) *
322-
abs(np.mean(np.diff(dd['lut'][1]))) *
323-
(structure['thickness']))
324+
# When the dose grid has a single row or column, ``np.diff`` returns an
325+
# empty array which would propagate ``NaN`` through the volume
326+
# calculation. Fall back to the original pixel spacing in these cases.
327+
dx = np.mean(np.diff(dd['lut'][0])) if len(dd['lut'][0]) > 1 else id['pixelspacing'][0]
328+
dy = np.mean(np.diff(dd['lut'][1])) if len(dd['lut'][1]) > 1 else id['pixelspacing'][1]
329+
vol = sum(hist) * (abs(dx) * abs(dy) * (structure['thickness']))
324330
return hist, vol
325331

326332

0 commit comments

Comments
 (0)