<commit_before>import numpy
import collections
from radiomics import base, imageoperations
import SimpleITK as sitk

class RadiomicsGLCM(base.RadiomicsFeaturesBase):
  """GLCM feature calculation."""
  def __init__(self, inputImage, inputMask, **kwargs):
    super(RadiomicsGLCM,self).__init__(inputImage, inputMask, **kwargs)

    if inputImage == None or inputMask == None:
      print('ERROR GLCM: missing input image or mask')
      return

    self.imageArray = sitk.GetArrayFromImage(inputImage)
    self.maskArray = sitk.GetArrayFromImage(inputMask)

    (self.matrix, self.matrixCoordinates) = \
      imageoperations.padTumorMaskToCube(self.imageArray,self.maskArray)

    self.targetVoxelArray = self.matrix[self.matrixCoordinates]
    self.coefficients = {}
    self.P_glcm = {}

    # binning
    self.matrix, self.histogram = imageoperations.binImage(self.binWidth, self.targetVoxelArray, self.matrix, self.matrixCoordinates)
    self.coefficients['Ng'] = self.histogram[1].shape[0]

    self.createGLCM()

    self.calculateCoefficients()

  def calculateGLCM(self, distances, angles):
    """
    Compute 13 GLCM matrices for the input image for every direction in 3D.

    (13 GLCMs for each neighboring voxel from a reference voxel centered in a 3x3 cube)
    """
    maxrows = self.matrix.shape[2]
    maxcols = self.matrix.shape[1]
    maxheight = self.matrix.shape[0]
    indices = zip(*self.matrixCoordinates)

    numIndices = len(indices)
    index = 0

    for h, c, r in indices:
      if index % 100 == 0:
        percentProgress = (float(index) / float(numIndices)) * 100.0
        print ('calculate GLCM: %.2f%s' % (percentProgress, '%'))
      index = index + 1
      for angles_idx, angle in enumerate(angles):
        for distances_idx, distance in enumerate(distances):
          i = self.matrix[h, c, r]
          i_idx = int(i-1)

          row = r + angle[2]
          col = c + angle[1]
          height = h + angle[0]

          if row >= 0 and row < maxrows and col >= 0 and col < maxcols:
            if tuple((height, col, row)) in indices:
              j = self.matrix[height, col, row]
              j_idx = int(j-1)
              self.P_glcm[i_idx, j_idx, distances_idx, angles_idx] += 1

  def createGLCM(self):
    """
    Generate container for GLCM Matrices: P_glcm as an array of shape: (i,j,gamma,a)

    i/j = total gray-level bins for image array,
    gamma = 1 (distance in voxels),
    a = 1...13 (directions in 3D)
    """
    Ng = self.coefficients['Ng']

    distances=numpy.array([1])
    angles = numpy.array([ (0, 1, 0),
                           (-1, 1, 0),
                           (-1, 0, 0),
                           (-1, -1, 0),
                           (0, 1, -1),
                           (0, 0, -1),
                           (0, -1, -1),
                           (-1, 0, -1),
                           (1, 0, -1),
                           (-1, 1, -1),
                           (1, -1, -1),
                           (-1, -1, -1),
                           (1, 1, -1) ])
    self.P_glcm = numpy.zeros( (Ng, Ng, distances.size, int(angles.shape[0])), dtype='float64' )
    self.calculateGLCM(distances, angles)

    # Normalize each glcm
    for q in xrange(int(angles.shape[0])):
      self.P_glcm[:,:,0,q] = self.P_glcm[:,:,0,q]/(self.P_glcm[:,:,0,q].sum())

  # check if ivector and jvector can be replaced
  def calculateCoefficients(self):
    """Calculate and fill in the coefficients dict"""
    Ng = self.coefficients['Ng']
    eps = numpy.spacing(1)


    NgVector = numpy.arange(1,self.P_glcm.shape[0]+1, dtype= 'float64')
    # shape = (Ng, Ng)
    i,j = numpy.meshgrid(NgVector, NgVector, indexing= 'ij')

    # shape = (2*Ng-1)
    kValuesSum = numpy.arange(2, (Ng*2)+1) - 1
    # shape = (Ng-1)
    kValuesDiff = numpy.arange(0,Ng) + 1
<commit_msg>BUG: fix calculation of kValuesSum, -Diff in glcm<commit_after>import numpy
import collections
from radiomics import base, imageoperations
import SimpleITK as sitk

class RadiomicsGLCM(base.RadiomicsFeaturesBase):
  """GLCM feature calculation."""
  def __init__(self, inputImage, inputMask, **kwargs):
    super(RadiomicsGLCM,self).__init__(inputImage, inputMask, **kwargs)

    if inputImage == None or inputMask == None:
      print('ERROR GLCM: missing input image or mask')
      return

    self.imageArray = sitk.GetArrayFromImage(inputImage)
    self.maskArray = sitk.GetArrayFromImage(inputMask)

    (self.matrix, self.matrixCoordinates) = \
      imageoperations.padTumorMaskToCube(self.imageArray,self.maskArray)

    self.targetVoxelArray = self.matrix[self.matrixCoordinates]
    self.coefficients = {}
    self.P_glcm = {}

    # binning
    self.matrix, self.histogram = imageoperations.binImage(self.binWidth, self.targetVoxelArray, self.matrix, self.matrixCoordinates)
    self.coefficients['Ng'] = self.histogram[1].shape[0]

    self.createGLCM()

    self.calculateCoefficients()

  def calculateGLCM(self, distances, angles):
    """
    Compute 13 GLCM matrices for the input image for every direction in 3D.

    (13 GLCMs for each neighboring voxel from a reference voxel centered in a 3x3 cube)
    """
    maxrows = self.matrix.shape[2]
    maxcols = self.matrix.shape[1]
    maxheight = self.matrix.shape[0]
    indices = zip(*self.matrixCoordinates)

    numIndices = len(indices)
    index = 0

    for h, c, r in indices:
      if index % 100 == 0:
        percentProgress = (float(index) / float(numIndices)) * 100.0
        print ('calculate GLCM: %.2f%s' % (percentProgress, '%'))
      index = index + 1
      for angles_idx, angle in enumerate(angles):
        for distances_idx, distance in enumerate(distances):
          i = self.matrix[h, c, r]
          i_idx = int(i-1)

          row = r + angle[2]
          col = c + angle[1]
          height = h + angle[0]

          if row >= 0 and row < maxrows and col >= 0 and col < maxcols:
            if tuple((height, col, row)) in indices:
              j = self.matrix[height, col, row]
              j_idx = int(j-1)
              self.P_glcm[i_idx, j_idx, distances_idx, angles_idx] += 1

  def createGLCM(self):
    """
    Generate container for GLCM Matrices: P_glcm as an array of shape: (i,j,gamma,a)

    i/j = total gray-level bins for image array,
    gamma = 1 (distance in voxels),
    a = 1...13 (directions in 3D)
    """
    Ng = self.coefficients['Ng']

    distances=numpy.array([1])
    angles = numpy.array([ (0, 1, 0),
                           (-1, 1, 0),
                           (-1, 0, 0),
                           (-1, -1, 0),
                           (0, 1, -1),
                           (0, 0, -1),
                           (0, -1, -1),
                           (-1, 0, -1),
                           (1, 0, -1),
                           (-1, 1, -1),
                           (1, -1, -1),
                           (-1, -1, -1),
                           (1, 1, -1) ])
    self.P_glcm = numpy.zeros( (Ng, Ng, distances.size, int(angles.shape[0])), dtype='float64' )
    self.calculateGLCM(distances, angles)

    # Normalize each glcm
    for q in xrange(int(angles.shape[0])):
      self.P_glcm[:,:,0,q] = self.P_glcm[:,:,0,q]/(self.P_glcm[:,:,0,q].sum())

  # check if ivector and jvector can be replaced
  def calculateCoefficients(self):
    """Calculate and fill in the coefficients dict"""
    Ng = self.coefficients['Ng']
    eps = numpy.spacing(1)


    NgVector = numpy.arange(1,self.P_glcm.shape[0]+1, dtype= 'float64')
    # shape = (Ng, Ng)
    i,j = numpy.meshgrid(NgVector, NgVector, indexing= 'ij')

    # shape = (2*Ng-1)
    kValuesSum = numpy.arange(2, (Ng*2)+1)
    # shape = (Ng-1)
    kValuesDiff = numpy.arange(0,Ng)

{'fraction_non_alphanumeric': 0.13425686172967374, 'fraction_numerical': 0.025116519937856033, 'mean_word_length': 2.1790123456790123, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2082476', 'n_tokens_mistral': 2686, 'n_tokens_neox': 2541, 'n_words': 643}