Skip to content

Commit

Permalink
BUG: Fix memory error in MCC
Browse files Browse the repository at this point in the history
Due to the broadcasting of 4 arrays (with each of shape Ng x Ng x Ng x Na after broadcasting), too much memory is needed.
Therefore, compute Q using a for loop instead of broadcasting and `numpy.sum`. This is a slightly slower computation method, but should take care of the memory issue.
  • Loading branch information
JoostJM committed Oct 5, 2018
1 parent d8074dc commit 167888b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions radiomics/glcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,12 @@ def getMCCFeatureValue(self):

# Calculate Q (shape (i, i, d)). To prevent division by 0, add epsilon (such a division can occur when in a ROI
# along a certain angle, voxels with gray level i do not have neighbors
Q = numpy.sum((self.P_glcm[:, None, :, :] * self.P_glcm[None, :, :, :]) / # slice: i, j, k, d
(px[:, None, :, :] * py[None, :, :, :] + eps), 2) # sum over k (3rd axis --> index 2)
Q = ((self.P_glcm[:, None, 0, :] * self.P_glcm[None, :, 0, :]) / # slice: i, j, k, d
(px[:, None, 0, :] * py[None, :, 0, :] + eps)) # sum over k (3rd axis --> index 2)

for gl in range(1, self.P_glcm.shape[1]):
Q += ((self.P_glcm[:, None, gl, :] * self.P_glcm[None, :, gl, :]) / # slice: i, j, k, d
(px[:, None, 0, :] * py[None, :, gl, :] + eps)) # sum over k (3rd axis --> index 2)

# calculation of eigenvalues if performed on last 2 dimensions, therefore, move the angles dimension (d) forward
Q_eigenValue = numpy.linalg.eigvals(Q.transpose((2, 0, 1)))
Expand Down

0 comments on commit 167888b

Please sign in to comment.