shape.py 24.5 KB
Newer Older
Andrea Bizzego's avatar
Andrea Bizzego committed
1
2
3
4
import numpy
import SimpleITK as sitk

from radiomics import base, cShape, deprecated
5
from scipy.spatial import ConvexHull
Andrea Bizzego's avatar
Andrea Bizzego committed
6
7
8


class RadiomicsShape(base.RadiomicsFeaturesBase):
Nicole Bussola's avatar
Nicole Bussola committed
9
    r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
10
11
12
13
14
15
16
17
18
19
  In this group of features we included descriptors of the three-dimensional size and shape of the ROI. These features
  are independent from the gray level intensity distribution in the ROI and are therefore only calculated on the
  non-derived image and mask.

  Let:

  - :math:`V` the volume of the ROI in mm\ :sup:`3`
  - :math:`A` the surface area of the ROI in mm\ :sup:`2`
  """

Nicole Bussola's avatar
Nicole Bussola committed
20
21
    def __init__(self, inputImage, inputMask, **kwargs):
        super(RadiomicsShape, self).__init__(inputImage, inputMask, **kwargs)
Andrea Bizzego's avatar
Andrea Bizzego committed
22

Nicole Bussola's avatar
Nicole Bussola committed
23
24
25
26
    def _initVoxelBasedCalculation(self):
        raise NotImplementedError(
            'Shape features are not available in voxel-based mode'
        )
Andrea Bizzego's avatar
Andrea Bizzego committed
27

Nicole Bussola's avatar
Nicole Bussola committed
28
    def _initSegmentBasedCalculation(self):
Andrea Bizzego's avatar
Andrea Bizzego committed
29

Nicole Bussola's avatar
Nicole Bussola committed
30
        self.pixelSpacing = numpy.array(self.inputImage.GetSpacing()[::-1])
Andrea Bizzego's avatar
Andrea Bizzego committed
31

Nicole Bussola's avatar
Nicole Bussola committed
32
33
        # Pad inputMask to prevent index-out-of-range errors
        self.logger.debug('Padding the mask with 0s')
Andrea Bizzego's avatar
Andrea Bizzego committed
34

Nicole Bussola's avatar
Nicole Bussola committed
35
        cpif = sitk.ConstantPadImageFilter()
Andrea Bizzego's avatar
Andrea Bizzego committed
36

Nicole Bussola's avatar
Nicole Bussola committed
37
38
39
40
41
42
43
44
        padding = numpy.tile(1, 3)
        try:
            cpif.SetPadLowerBound(padding)
            cpif.SetPadUpperBound(padding)
        except TypeError:
            # newer versions of SITK/python want a tuple or list
            cpif.SetPadLowerBound(padding.tolist())
            cpif.SetPadUpperBound(padding.tolist())
Andrea Bizzego's avatar
Andrea Bizzego committed
45

Nicole Bussola's avatar
Nicole Bussola committed
46
        self.inputMask = cpif.Execute(self.inputMask)
Andrea Bizzego's avatar
Andrea Bizzego committed
47

Nicole Bussola's avatar
Nicole Bussola committed
48
49
50
51
52
        # Reassign self.maskArray using the now-padded self.inputMask and force and interger datatype
        self.maskArray = (sitk.GetArrayFromImage(self.inputMask) == self.label).astype(
            'int'
        )
        self.labelledVoxelCoordinates = numpy.where(self.maskArray != 0)
Andrea Bizzego's avatar
Andrea Bizzego committed
53

Nicole Bussola's avatar
Nicole Bussola committed
54
        self.logger.debug('Pre-calculate Volume, Surface Area and Eigenvalues')
Andrea Bizzego's avatar
Andrea Bizzego committed
55

Nicole Bussola's avatar
Nicole Bussola committed
56
57
58
59
60
        # Volume, Surface Area and eigenvalues are pre-calculated
        # Compute volume
        z, y, x = self.pixelSpacing
        Np = len(self.labelledVoxelCoordinates[0])
        self.Volume = Np * (z * x * y)
Andrea Bizzego's avatar
Andrea Bizzego committed
61

Nicole Bussola's avatar
Nicole Bussola committed
62
63
        # Compute Surface Area
        self.SurfaceArea = self._calculateSurfaceArea()
Andrea Bizzego's avatar
Andrea Bizzego committed
64

Nicole Bussola's avatar
Nicole Bussola committed
65
66
67
68
69
70
71
72
73
74
75
        # Compute eigenvalues and -vectors
        coordinates = numpy.array(self.labelledVoxelCoordinates, dtype='int').transpose(
            (1, 0)
        )  # Transpose equals zip(*a)
        physicalCoordinates = coordinates * self.pixelSpacing[None, :]
        physicalCoordinates -= numpy.mean(physicalCoordinates, axis=0)  # Centered at 0
        physicalCoordinates /= numpy.sqrt(Np)
        covariance = numpy.dot(physicalCoordinates.T.copy(), physicalCoordinates)
        self.eigenValues, eigenVectors = numpy.linalg.eig(
            covariance
        )  # eigenVectors are not used
Andrea Bizzego's avatar
Andrea Bizzego committed
76

Nicole Bussola's avatar
Nicole Bussola committed
77
78
79
80
81
82
83
84
85
86
        # Correct machine precision errors causing very small negative eigen values in case of some 2D segmentations
        machine_errors = numpy.bitwise_and(
            self.eigenValues < 0, self.eigenValues > -1e-10
        )
        if numpy.sum(machine_errors) > 0:
            self.logger.warning(
                'Encountered %d eigenvalues < 0 and > -1e-10, rounding to 0',
                numpy.sum(machine_errors),
            )
            self.eigenValues[machine_errors] = 0
Andrea Bizzego's avatar
Andrea Bizzego committed
87

Nicole Bussola's avatar
Nicole Bussola committed
88
        self.eigenValues.sort()  # Sort the eigenValues from small to large
Andrea Bizzego's avatar
Andrea Bizzego committed
89

Nicole Bussola's avatar
Nicole Bussola committed
90
        self.diameters = None  # Do not precompute diameters, but instantiate the variable for lazy assignment
Andrea Bizzego's avatar
Andrea Bizzego committed
91

Nicole Bussola's avatar
Nicole Bussola committed
92
        self.logger.debug('Shape feature class initialized')
Andrea Bizzego's avatar
Andrea Bizzego committed
93

Nicole Bussola's avatar
Nicole Bussola committed
94
95
    def _calculateSurfaceArea(self):
        self.logger.debug('Calculating Surface Area in C')
Andrea Bizzego's avatar
Andrea Bizzego committed
96

Nicole Bussola's avatar
Nicole Bussola committed
97
        return cShape.calculate_surfacearea(self.maskArray, self.pixelSpacing)
Andrea Bizzego's avatar
Andrea Bizzego committed
98

Nicole Bussola's avatar
Nicole Bussola committed
99
100
    def calculateDiameters(self):
        """
Andrea Bizzego's avatar
Andrea Bizzego committed
101
102
103
104
105
106
107
    Calculate maximum diameters in 2D and 3D using C extension. Function returns a tuple with 4 elements:

    0. Maximum 2D diameter Slice (XY Plane, Axial)
    1. Maximum 2D diameter Column (ZX Plane, Coronal)
    2. Maximum 2D diameter Row (ZY Plane, Sagittal)
    3. Maximum 3D diameter
    """
Nicole Bussola's avatar
Nicole Bussola committed
108
109
110
        self.logger.debug('Calculating Maximum diameters in C')
        Ns = len(self.labelledVoxelCoordinates[0])
        return cShape.calculate_diameter(self.maskArray, self.pixelSpacing, Ns)
Andrea Bizzego's avatar
Andrea Bizzego committed
111

Nicole Bussola's avatar
Nicole Bussola committed
112
113
    def getVolumeFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
114
115
116
117
118
119
120
121
122
123
124
125
126
    **1. Volume**

    .. math::
      V = \displaystyle\sum^{N}_{i=1}{V_i}

    The volume of the ROI :math:`V` is approximated by multiplying the number of voxels in the ROI by the volume of a
    single voxel :math:`V_i`.

    .. note::
      In the IBSI feature definitions, a more precise approximation of the volume is used. That method uses tetrahedrons
      consisting of the origin and faces in the ROI. Although the method implemented here overestimates the volume,
      especially in small volumes, the difference will be negligible in large ROIs.
    """
Nicole Bussola's avatar
Nicole Bussola committed
127
        return self.Volume
Andrea Bizzego's avatar
Andrea Bizzego committed
128

Nicole Bussola's avatar
Nicole Bussola committed
129
130
    def getSurfaceAreaFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    **2. Surface Area**

    .. math::
      A = \displaystyle\sum^{N}_{i=1}{\frac{1}{2}|\text{a}_i\text{b}_i \times \text{a}_i\text{c}_i|}

    Where:

    :math:`N` is the number of triangles forming the surface mesh of the volume (ROI)

    :math:`\text{a}_i\text{b}_i` and :math:`\text{a}_i\text{c}_i` are the edges of the :math:`i^{\text{th}}` triangle
    formed by points :math:`\text{a}_i`, :math:`\text{b}_i` and :math:`\text{c}_i`

    Surface Area is an approximation of the surface of the ROI in mm2, calculated using a marching cubes algorithm.

    References:

    - Lorensen WE, Cline HE. Marching cubes: A high resolution 3D surface construction algorithm. ACM SIGGRAPH Comput
      Graph `Internet <http://portal.acm.org/citation.cfm?doid=37402.37422>`_. 1987;21:163-9.
    """
Nicole Bussola's avatar
Nicole Bussola committed
150
        return self.SurfaceArea
Andrea Bizzego's avatar
Andrea Bizzego committed
151

Nicole Bussola's avatar
Nicole Bussola committed
152
153
    def getSurfaceVolumeRatioFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
154
155
156
157
158
159
160
161
    **3. Surface Area to Volume ratio**

    .. math::
      \textit{surface to volume ratio} = \frac{A}{V}

    Here, a lower value indicates a more compact (sphere-like) shape. This feature is not dimensionless, and is
    therefore (partly) dependent on the volume of the ROI.
    """
Nicole Bussola's avatar
Nicole Bussola committed
162
        return self.SurfaceArea / self.Volume
Andrea Bizzego's avatar
Andrea Bizzego committed
163

Nicole Bussola's avatar
Nicole Bussola committed
164
165
    def getSphericityFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    **4. Sphericity**

    .. math::
      \textit{sphericity} = \frac{\sqrt[3]{36 \pi V^2}}{A}

    Sphericity is a measure of the roundness of the shape of the tumor region relative to a sphere. It is a
    dimensionless measure, independent of scale and orientation. The value range is :math:`0 < sphericity \leq 1`, where
    a value of 1 indicates a perfect sphere (a sphere has the smallest possible surface area for a given volume,
    compared to other solids).

    .. note::
      This feature is correlated to Compactness 1, Compactness 2 and Spherical Disproportion. In the default
      parameter file provided in the ``pyradiomics/examples/exampleSettings`` folder, Compactness 1 and Compactness 2
      are therefore disabled.
    """
Nicole Bussola's avatar
Nicole Bussola committed
181
        return (36 * numpy.pi * self.Volume ** 2) ** (1.0 / 3.0) / self.SurfaceArea
Andrea Bizzego's avatar
Andrea Bizzego committed
182

Nicole Bussola's avatar
Nicole Bussola committed
183
184
185
    @deprecated
    def getCompactness1FeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    **5. Compactness 1**

    .. math::
      \textit{compactness 1} = \frac{V}{\sqrt{\pi A^3}}

    Similar to Sphericity, Compactness 1 is a measure of how compact the shape of the tumor is relative to a sphere
    (most compact). It is therefore correlated to Sphericity and redundant. It is provided here for completeness.
    The value range is :math:`0 < compactness\ 1 \leq \frac{1}{6 \pi}`, where a value of :math:`\frac{1}{6 \pi}`
    indicates a perfect sphere.

    By definition, :math:`compactness\ 1 = \frac{1}{6 \pi}\sqrt{compactness\ 2} =
    \frac{1}{6 \pi}\sqrt{sphericity^3}`.

    .. note::
      This feature is correlated to Compactness 2, Sphericity and Spherical Disproportion.
      Therefore, this feature is marked, so it is not enabled by default (i.e. this feature will not be enabled if no
      individual features are specified (enabling 'all' features), but will be enabled when individual features are
      specified, including this feature). To include this feature in the extraction, specify it by name in the enabled features.
    """
Nicole Bussola's avatar
Nicole Bussola committed
205
        return self.Volume / (self.SurfaceArea ** (3.0 / 2.0) * numpy.sqrt(numpy.pi))
Andrea Bizzego's avatar
Andrea Bizzego committed
206

Nicole Bussola's avatar
Nicole Bussola committed
207
208
    def getCompactness2FeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    **6. Compactness 2**

    .. math::
      \textit{compactness 2} = 36 \pi \frac{V^2}{A^3}

    Similar to Sphericity and Compactness 1, Compactness 2 is a measure of how compact the shape of the tumor is
    relative to a sphere (most compact). It is a dimensionless measure, independent of scale and orientation. The value
    range is :math:`0 < compactness\ 2 \leq 1`, where a value of 1 indicates a perfect sphere.

    By definition, :math:`compactness\ 2 = (sphericity)^3`

    .. note::
      This feature is correlated to Compactness 1, Sphericity and Spherical Disproportion.
      Therefore, this feature is marked, so it is not enabled by default (i.e. this feature will not be enabled if no
      individual features are specified (enabling 'all' features), but will be enabled when individual features are
      specified, including this feature). To include this feature in the extraction, specify it by name in the enabled features.
    """
Nicole Bussola's avatar
Nicole Bussola committed
226
        return (36.0 * numpy.pi) * (self.Volume ** 2.0) / (self.SurfaceArea ** 3.0)
Andrea Bizzego's avatar
Andrea Bizzego committed
227

Nicole Bussola's avatar
Nicole Bussola committed
228
229
230
    @deprecated
    def getSphericalDisproportionFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    **7. Spherical Disproportion**

    .. math::
      \textit{spherical disproportion} = \frac{A}{4\pi R^2} = \frac{A}{\sqrt[3]{36 \pi V^2}}

    Where :math:`R` is the radius of a sphere with the same volume as the tumor, and equal to
    :math:`\sqrt[3]{\frac{3V}{4\pi}}`.

    Spherical Disproportion is the ratio of the surface area of the tumor region to the surface area of a sphere with
    the same volume as the tumor region, and by definition, the inverse of Sphericity. Therefore, the value range is
    :math:`spherical\ disproportion \geq 1`, with a value of 1 indicating a perfect sphere.

    .. note::
      This feature is correlated to Compactness 2, Compactness2 and Sphericity.
      Therefore, this feature is marked, so it is not enabled by default (i.e. this feature will not be enabled if no
      individual features are specified (enabling 'all' features), but will be enabled when individual features are
      specified, including this feature). To include this feature in the extraction, specify it by name in the enabled features.
    """
Nicole Bussola's avatar
Nicole Bussola committed
249
        return self.SurfaceArea / (36 * numpy.pi * self.Volume ** 2) ** (1.0 / 3.0)
Andrea Bizzego's avatar
Andrea Bizzego committed
250

Nicole Bussola's avatar
Nicole Bussola committed
251
252
    def getMaximum3DDiameterFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
253
254
255
256
257
258
259
260
261
    **8. Maximum 3D diameter**

    Maximum 3D diameter is defined as the largest pairwise Euclidean distance between surface voxels in the ROI.

    Also known as Feret Diameter.

    .. warning::
      This feature is only available when C Extensions are enabled
    """
Nicole Bussola's avatar
Nicole Bussola committed
262
263
264
        if self.diameters is None:
            self.diameters = self.calculateDiameters()
        return self.diameters[3]
Andrea Bizzego's avatar
Andrea Bizzego committed
265

Nicole Bussola's avatar
Nicole Bussola committed
266
267
    def getMaximum2DDiameterSliceFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
268
269
270
271
272
273
274
275
    **9. Maximum 2D diameter (Slice)**

    Maximum 2D diameter (Slice) is defined as the largest pairwise Euclidean distance between tumor surface voxels in
    the row-column (generally the axial) plane.

    .. warning::
      This feature is only available when C Extensions are enabled
    """
Nicole Bussola's avatar
Nicole Bussola committed
276
277
278
        if self.diameters is None:
            self.diameters = self.calculateDiameters()
        return self.diameters[0]
Andrea Bizzego's avatar
Andrea Bizzego committed
279

Nicole Bussola's avatar
Nicole Bussola committed
280
281
    def getMaximum2DDiameterColumnFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
282
283
284
285
286
287
288
289
    **10. Maximum 2D diameter (Column)**

    Maximum 2D diameter (Column) is defined as the largest pairwise Euclidean distance between tumor surface voxels in
    the row-slice (usually the coronal) plane.

    .. warning::
      This feature is only available when C Extensions are enabled
    """
Nicole Bussola's avatar
Nicole Bussola committed
290
291
292
        if self.diameters is None:
            self.diameters = self.calculateDiameters()
        return self.diameters[1]
Andrea Bizzego's avatar
Andrea Bizzego committed
293

Nicole Bussola's avatar
Nicole Bussola committed
294
295
    def getMaximum2DDiameterRowFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
296
297
298
299
300
301
302
303
    **11. Maximum 2D diameter (Row)**

    Maximum 2D diameter (Row) is defined as the largest pairwise Euclidean distance between tumor surface voxels in the
    column-slice (usually the sagittal) plane.

    .. warning::
      This feature is only available when C Extensions are enabled
    """
Nicole Bussola's avatar
Nicole Bussola committed
304
305
306
        if self.diameters is None:
            self.diameters = self.calculateDiameters()
        return self.diameters[2]
Andrea Bizzego's avatar
Andrea Bizzego committed
307

Nicole Bussola's avatar
Nicole Bussola committed
308
309
    def getMajorAxisFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
310
311
312
313
314
315
    **12. Major Axis**

    .. math::
      \textit{major axis} = 4 \sqrt{\lambda_{\text{major}}}

    """
Nicole Bussola's avatar
Nicole Bussola committed
316
317
318
319
320
321
322
323
324
        if self.eigenValues[2] < 0:
            self.logger.warning(
                'Major axis eigenvalue negative! (%g)', self.eigenValues[2]
            )
            return numpy.nan
        return numpy.sqrt(self.eigenValues[2]) * 4

    def getMinorAxisFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
325
326
327
328
329
330
    **13. Minor Axis**

    .. math::
      \textit{minor axis} = 4 \sqrt{\lambda_{\text{minor}}}

    """
Nicole Bussola's avatar
Nicole Bussola committed
331
332
333
334
335
336
337
338
339
        if self.eigenValues[1] < 0:
            self.logger.warning(
                'Minor axis eigenvalue negative! (%g)', self.eigenValues[1]
            )
            return numpy.nan
        return numpy.sqrt(self.eigenValues[1]) * 4

    def getLeastAxisFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
340
341
342
343
344
345
    **14. Least Axis**

    .. math::
      \textit{least axis} = 4 \sqrt{\lambda_{\text{least}}}

    """
Nicole Bussola's avatar
Nicole Bussola committed
346
347
348
349
350
351
352
353
354
        if self.eigenValues[0] < 0:
            self.logger.warning(
                'Least axis eigenvalue negative! (%g)', self.eigenValues[0]
            )
            return numpy.nan
        return numpy.sqrt(self.eigenValues[0]) * 4

    def getElongationFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
355
356
357
358
359
360
361
362
363
364
365
366
    **15. Elongation**

    Elongation is calculated using its implementation in SimpleITK, and is defined as:

    .. math::
      \textit{elongation} = \sqrt{\frac{\lambda_{\text{minor}}}{\lambda_{\text{major}}}}

    Here, :math:`\lambda_{\text{major}}` and :math:`\lambda_{\text{minor}}` are the lengths of the largest and second
    largest principal component axes. The values range between 1 (where the cross section through the first and second
    largest principal moments is circle-like (non-elongated)) and 0 (where the object is a single point or 1 dimensional
    line).
    """
Nicole Bussola's avatar
Nicole Bussola committed
367
368
369
370
371
372
373
374
375
376
377
        if self.eigenValues[1] < 0 or self.eigenValues[2] < 0:
            self.logger.warning(
                'Elongation eigenvalue negative! (%g, %g)',
                self.eigenValues[1],
                self.eigenValues[2],
            )
            return numpy.nan
        return numpy.sqrt(self.eigenValues[1] / self.eigenValues[2])

    def getFlatnessFeatureValue(self):
        r"""
Andrea Bizzego's avatar
Andrea Bizzego committed
378
379
380
381
382
383
384
385
386
387
    **16. Flatness**

    Flatness is calculated using its implementation in SimpleITK, and is defined as:

    .. math::
      \textit{flatness} = \sqrt{\frac{\lambda_{\text{least}}}{\lambda_{\text{major}}}}

    Here, :math:`\lambda_{\text{major}}` and :math:`\lambda_{\text{least}}` are the lengths of the largest and smallest
    principal component axes. The values range between 1 (non-flat, sphere-like) and 0 (a flat object).
    """
Nicole Bussola's avatar
Nicole Bussola committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
        if self.eigenValues[0] < 0 or self.eigenValues[2] < 0:
            self.logger.warning(
                'Elongation eigenvalue negative! (%g, %g)',
                self.eigenValues[0],
                self.eigenValues[2],
            )
            return numpy.nan
        return numpy.sqrt(self.eigenValues[0] / self.eigenValues[2])

    def getEccentricityFeatureValue(self):
        r"""
    **17. Eccentricity**

    This is a custom shape feature, implemented for comparison with Valieres et al.
    Eccentricity is defined as: 

    .. math::
      \textit{Eccentricity} = \sqrt{1-\frac{a\times b}{c^2}}

    Here, where c is the longest semi-principal axes of the ellipsoid,
    and a and b are the second and third longest semi-principal axes of the ellipsoid.
    """

        major_axis = numpy.sqrt(self.eigenValues[2]) * 4
        minor_axis = numpy.sqrt(self.eigenValues[1]) * 4
        least_axis = numpy.sqrt(self.eigenValues[0]) * 4

        return numpy.sqrt(1.0 - (minor_axis * least_axis / (major_axis ** 2)))

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
    def getSolidityFeatureValue(self):
        r"""
    **18. Solidity**

    This is a custom shape feature, implemented for comparison with Valieres et al.
    Eccentricity is defined as: 

    .. math::
      \textit{Eccentricity} = \sqrt{1-\frac{a\times b}{c^2}}

    Here, where c is the longest semi-principal axes of the ellipsoid,
    and a and b are the second and third longest semi-principal axes of the ellipsoid.
    """

        ROIVolume = self.Volume

        def tetrahedron_volume(a, b, c, d):
            return (
                numpy.abs(numpy.einsum('ij,ij->i', a - d, numpy.cross(b - d, c - d)))
                / 6
            )

        Np = len(self.labelledVoxelCoordinates[0])
        coordinates = numpy.array(self.labelledVoxelCoordinates, dtype='int').transpose(
            (1, 0)
        )  # Transpose equals zip(*a)
        physicalCoordinates = coordinates * self.pixelSpacing[None, :]
        physicalCoordinates -= numpy.mean(physicalCoordinates, axis=0)  # Centered at 0
        physicalCoordinates /= numpy.sqrt(Np)
        # import ipdb

        # ipdb.set_trace()
        ch = ConvexHull(physicalCoordinates)

        simplices = numpy.column_stack(
            (numpy.repeat(ch.vertices[0], ch.nsimplex), ch.simplices)
        )
        tets = ch.points[simplices]
        a, b, c, d = tets[:, 0], tets[:, 1], tets[:, 2], tets[:, 3]
        tetrahedron_vol = (
            numpy.abs(numpy.einsum('ij,ij->i', a - d, numpy.cross(b - d, c - d))) / 6
        )

        return ROIVolume / numpy.sum(tetrahedron_vol)

Nicole Bussola's avatar
Nicole Bussola committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
    def _interpolate(self, grid, p1, p2):
        diff = (0.5 - self.maskArray[tuple(grid[p1])]) / (
            self.maskArray[tuple(grid[p2])] - self.maskArray[tuple(grid[p1])]
        )
        return (grid[p1] + ((grid[p2] - grid[p1]) * diff)) * self.pixelSpacing

    def _getMarchingTables(self):
        vertList = numpy.array(
            (
                (0, 0, 0.5),
                (0, 0.5, 1),
                (0, 1, 0.5),
                (0, 0.5, 0),
                (1, 0, 0.5),
                (1, 0.5, 1),
                (1, 1, 0.5),
                (1, 0.5, 0),
                (0.5, 0, 0),
                (0.5, 0, 1),
                (0.5, 1, 1),
                (0.5, 1, 0),
            ),
            dtype='float64',
        )
        vertList *= self.pixelSpacing[None, :]

        triTable = [
            [],
            [[0, 8, 3]],
            [[0, 1, 9]],
            [[1, 8, 3], [9, 8, 1]],
            [[1, 2, 10]],
            [[0, 8, 3], [1, 2, 10]],
            [[9, 2, 10], [0, 2, 9]],
            [[2, 8, 3], [2, 10, 8], [10, 9, 8]],
            [[3, 11, 2]],
            [[0, 11, 2], [8, 11, 0]],
            [[1, 9, 0], [2, 3, 11]],
            [[1, 11, 2], [1, 9, 11], [9, 8, 11]],
            [[3, 10, 1], [11, 10, 3]],
            [[0, 10, 1], [0, 8, 10], [8, 11, 10]],
            [[3, 9, 0], [3, 11, 9], [11, 10, 9]],
            [[9, 8, 10], [10, 8, 11]],
            [[4, 7, 8]],
            [[4, 3, 0], [7, 3, 4]],
            [[0, 1, 9], [8, 4, 7]],
            [[4, 1, 9], [4, 7, 1], [7, 3, 1]],
            [[1, 2, 10], [8, 4, 7]],
            [[3, 4, 7], [3, 0, 4], [1, 2, 10]],
            [[9, 2, 10], [9, 0, 2], [8, 4, 7]],
            [[2, 10, 9], [2, 9, 7], [2, 7, 3], [7, 9, 4]],
            [[8, 4, 7], [3, 11, 2]],
            [[11, 4, 7], [11, 2, 4], [2, 0, 4]],
            [[9, 0, 1], [8, 4, 7], [2, 3, 11]],
            [[4, 7, 11], [9, 4, 11], [9, 11, 2], [9, 2, 1]],
            [[3, 10, 1], [3, 11, 10], [7, 8, 4]],
            [[1, 11, 10], [1, 4, 11], [1, 0, 4], [7, 11, 4]],
            [[4, 7, 8], [9, 0, 11], [9, 11, 10], [11, 0, 3]],
            [[4, 7, 11], [4, 11, 9], [9, 11, 10]],
            [[9, 5, 4]],
            [[9, 5, 4], [0, 8, 3]],
            [[0, 5, 4], [1, 5, 0]],
            [[8, 5, 4], [8, 3, 5], [3, 1, 5]],
            [[1, 2, 10], [9, 5, 4]],
            [[3, 0, 8], [1, 2, 10], [4, 9, 5]],
            [[5, 2, 10], [5, 4, 2], [4, 0, 2]],
            [[2, 10, 5], [3, 2, 5], [3, 5, 4], [3, 4, 8]],
            [[9, 5, 4], [2, 3, 11]],
            [[0, 11, 2], [0, 8, 11], [4, 9, 5]],
            [[0, 5, 4], [0, 1, 5], [2, 3, 11]],
            [[2, 1, 5], [2, 5, 8], [2, 8, 11], [4, 8, 5]],
            [[10, 3, 11], [10, 1, 3], [9, 5, 4]],
            [[4, 9, 5], [0, 8, 1], [8, 10, 1], [8, 11, 10]],
            [[5, 4, 0], [5, 0, 11], [5, 11, 10], [11, 0, 3]],
            [[5, 4, 8], [5, 8, 10], [10, 8, 11]],
            [[9, 7, 8], [5, 7, 9]],
            [[9, 3, 0], [9, 5, 3], [5, 7, 3]],
            [[0, 7, 8], [0, 1, 7], [1, 5, 7]],
            [[1, 5, 3], [3, 5, 7]],
            [[9, 7, 8], [9, 5, 7], [10, 1, 2]],
            [[10, 1, 2], [9, 5, 0], [5, 3, 0], [5, 7, 3]],
            [[8, 0, 2], [8, 2, 5], [8, 5, 7], [10, 5, 2]],
            [[2, 10, 5], [2, 5, 3], [3, 5, 7]],
            [[7, 9, 5], [7, 8, 9], [3, 11, 2]],
            [[9, 5, 7], [9, 7, 2], [9, 2, 0], [2, 7, 11]],
            [[2, 3, 11], [0, 1, 8], [1, 7, 8], [1, 5, 7]],
            [[11, 2, 1], [11, 1, 7], [7, 1, 5]],
            [[9, 5, 8], [8, 5, 7], [10, 1, 3], [10, 3, 11]],
            [[5, 7, 0], [5, 0, 9], [7, 11, 0], [1, 0, 10], [11, 10, 0]],
            [[11, 10, 0], [11, 0, 3], [10, 5, 0], [8, 0, 7], [5, 7, 0]],
            [[11, 10, 5], [7, 11, 5]],
            [[10, 6, 5]],
            [[0, 8, 3], [5, 10, 6]],
            [[9, 0, 1], [5, 10, 6]],
            [[1, 8, 3], [1, 9, 8], [5, 10, 6]],
            [[1, 6, 5], [2, 6, 1]],
            [[1, 6, 5], [1, 2, 6], [3, 0, 8]],
            [[9, 6, 5], [9, 0, 6], [0, 2, 6]],
            [[5, 9, 8], [5, 8, 2], [5, 2, 6], [3, 2, 8]],
            [[2, 3, 11], [10, 6, 5]],
            [[11, 0, 8], [11, 2, 0], [10, 6, 5]],
            [[0, 1, 9], [2, 3, 11], [5, 10, 6]],
            [[5, 10, 6], [1, 9, 2], [9, 11, 2], [9, 8, 11]],
            [[6, 3, 11], [6, 5, 3], [5, 1, 3]],
            [[0, 8, 11], [0, 11, 5], [0, 5, 1], [5, 11, 6]],
            [[3, 11, 6], [0, 3, 6], [0, 6, 5], [0, 5, 9]],
            [[6, 5, 9], [6, 9, 11], [11, 9, 8]],
            [[5, 10, 6], [4, 7, 8]],
            [[4, 3, 0], [4, 7, 3], [6, 5, 10]],
            [[1, 9, 0], [5, 10, 6], [8, 4, 7]],
            [[10, 6, 5], [1, 9, 7], [1, 7, 3], [7, 9, 4]],
            [[6, 1, 2], [6, 5, 1], [4, 7, 8]],
            [[1, 2, 5], [5, 2, 6], [3, 0, 4], [3, 4, 7]],
            [[8, 4, 7], [9, 0, 5], [0, 6, 5], [0, 2, 6]],
            [[7, 3, 9], [7, 9, 4], [3, 2, 9], [5, 9, 6], [2, 6, 9]],
            [[3, 11, 2], [7, 8, 4], [10, 6, 5]],
            [[5, 10, 6], [4, 7, 2], [4, 2, 0], [2, 7, 11]],
            [[0, 1, 9], [4, 7, 8], [2, 3, 11], [5, 10, 6]],
            [[9, 2, 1], [9, 11, 2], [9, 4, 11], [7, 11, 4], [5, 10, 6]],
            [[8, 4, 7], [3, 11, 5], [3, 5, 1], [5, 11, 6]],
            [[5, 1, 11], [5, 11, 6], [1, 0, 11], [7, 11, 4], [0, 4, 11]],
            [[0, 5, 9], [0, 6, 5], [0, 3, 6], [11, 6, 3], [8, 4, 7]],
            [[6, 5, 9], [6, 9, 11], [4, 7, 9], [7, 11, 9]],
            [[10, 4, 9], [6, 4, 10]],
            [[4, 10, 6], [4, 9, 10], [0, 8, 3]],
            [[10, 0, 1], [10, 6, 0], [6, 4, 0]],
            [[8, 3, 1], [8, 1, 6], [8, 6, 4], [6, 1, 10]],
            [[1, 4, 9], [1, 2, 4], [2, 6, 4]],
            [[3, 0, 8], [1, 2, 9], [2, 4, 9], [2, 6, 4]],
            [[0, 2, 4], [4, 2, 6]],
            [[8, 3, 2], [8, 2, 4], [4, 2, 6]],
            [[10, 4, 9], [10, 6, 4], [11, 2, 3]],
            [[0, 8, 2], [2, 8, 11], [4, 9, 10], [4, 10, 6]],
            [[3, 11, 2], [0, 1, 6], [0, 6, 4], [6, 1, 10]],
            [[6, 4, 1], [6, 1, 10], [4, 8, 1], [2, 1, 11], [8, 11, 1]],
            [[9, 6, 4], [9, 3, 6], [9, 1, 3], [11, 6, 3]],
            [[8, 11, 1], [8, 1, 0], [11, 6, 1], [9, 1, 4], [6, 4, 1]],
            [[3, 11, 6], [3, 6, 0], [0, 6, 4]],
            [[6, 4, 8], [11, 6, 8]],
            [[7, 10, 6], [7, 8, 10], [8, 9, 10]],
            [[0, 7, 3], [0, 10, 7], [0, 9, 10], [6, 7, 10]],
            [[10, 6, 7], [1, 10, 7], [1, 7, 8], [1, 8, 0]],
            [[10, 6, 7], [10, 7, 1], [1, 7, 3]],
            [[1, 2, 6], [1, 6, 8], [1, 8, 9], [8, 6, 7]],
            [[2, 6, 9], [2, 9, 1], [6, 7, 9], [0, 9, 3], [7, 3, 9]],
            [[7, 8, 0], [7, 0, 6], [6, 0, 2]],
            [[7, 3, 2], [6, 7, 2]],
            [[2, 3, 11], [10, 6, 8], [10, 8, 9], [8, 6, 7]],
            [[2, 0, 7], [2, 7, 11], [0, 9, 7], [6, 7, 10], [9, 10, 7]],
            [[1, 8, 0], [1, 7, 8], [1, 10, 7], [6, 7, 10], [2, 3, 11]],
            [[11, 2, 1], [11, 1, 7], [10, 6, 1], [6, 7, 1]],
            [[8, 9, 6], [8, 6, 7], [9, 1, 6], [11, 6, 3], [1, 3, 6]],
            [[0, 9, 1], [11, 6, 7]],
            [[7, 8, 0], [7, 0, 6], [3, 11, 0], [11, 6, 0]],
            [[7, 11, 6]],
        ]
        return vertList, triTable