Exemplo n.º 1
0
    def _calculateCMatrix(self):
        self.logger.debug('Calculating GLRLM matrix in C')
        P_glrlm, angles = cMatrices.calculate_glrlm(
            self.matrix, self.maskArray, self.coefficients['Ng'],
            self.coefficients['Nr'], self.kwargs.get('force2D', False),
            self.kwargs.get('force2Ddimension', 0))
        P_glrlm = self._applyMatrixOptions(P_glrlm, angles)

        return P_glrlm
Exemplo n.º 2
0
    def _calculateMatrix(self):
        self.logger.debug('Calculating GLRLM matrix in C')
        P_glrlm, angles = cMatrices.calculate_glrlm(
            self.matrix, self.maskArray, self.coefficients['Ng'],
            self.coefficients['Nr'], self.settings.get('force2D', False),
            self.settings.get('force2Ddimension', 0))

        self.logger.debug('Process calculated matrix')

        # Optionally apply a weighting factor
        if self.weightingNorm is not None:
            self.logger.debug('Applying weighting (%s)', self.weightingNorm)
            # Correct the number of voxels for the number of times it is used (once per angle), affects run percentage
            self.coefficients['Np'] *= len(angles)

            pixelSpacing = self.inputImage.GetSpacing()[::-1]
            weights = numpy.empty(len(angles))
            for a_idx, a in enumerate(angles):
                if self.weightingNorm == 'infinity':
                    weights[a_idx] = max(numpy.abs(a) * pixelSpacing)
                elif self.weightingNorm == 'euclidean':
                    weights[a_idx] = numpy.sqrt(
                        numpy.sum((numpy.abs(a) * pixelSpacing)**2))
                elif self.weightingNorm == 'manhattan':
                    weights[a_idx] = numpy.sum(numpy.abs(a) * pixelSpacing)
                elif self.weightingNorm == 'no_weighting':
                    weights[a_idx] = 1
                else:
                    self.logger.warning(
                        'weigthing norm "%s" is unknown, weighting factor is set to 1',
                        self.weightingNorm)
                    weights[a_idx] = 1

            P_glrlm = numpy.sum(P_glrlm * weights[None, None, :],
                                2,
                                keepdims=True)

        Nz = numpy.sum(P_glrlm, (0, 1))

        # Delete empty angles if no weighting is applied
        if P_glrlm.shape[2] > 1:
            emptyAngles = numpy.where(Nz == 0)
            if len(emptyAngles[0]) > 0:  # One or more angles are 'empty'
                self.logger.debug('Deleting %d empty angles:\n%s',
                                  len(emptyAngles[0]), angles[emptyAngles])
                P_glrlm = numpy.delete(P_glrlm, emptyAngles, 2)
                Nz = numpy.delete(Nz, emptyAngles, 0)
            else:
                self.logger.debug('No empty angles')

        self.coefficients['Nz'] = Nz

        return P_glrlm
Exemplo n.º 3
0
  def _calculateCMatrix(self):
    self.logger.debug('Calculating GLRLM matrix in C')

    Ng = self.coefficients['Ng']
    Nr = self.coefficients['Nr']

    size = numpy.max(self.matrixCoordinates, 1) - numpy.min(self.matrixCoordinates, 1) + 1
    angles = imageoperations.generateAngles(size, **self.kwargs)

    P_glrlm = cMatrices.calculate_glrlm(self.matrix, self.maskArray, angles, Ng, Nr)
    P_glrlm = self._applyMatrixOptions(P_glrlm, angles)

    return P_glrlm
Exemplo n.º 4
0
    def _calculateCMatrix(self):
        Ng = self.coefficients['Ng']
        Nr = self.coefficients['Nr']

        size = numpy.max(self.matrixCoordinates, 1) - numpy.min(
            self.matrixCoordinates, 1) + 1
        angles = imageoperations.generateAngles(size)

        P_glrlm = cMatrices.calculate_glrlm(self.matrix, self.maskArray,
                                            angles, Ng, Nr)
        P_glrlm = self._applyMatrixOptions(P_glrlm, angles)

        return P_glrlm
Exemplo n.º 5
0
  def _calculateCMatrix(self):
    self.logger.debug('Calculating GLRLM matrix in C')

    Ng = self.coefficients['Ng']
    Nr = self.coefficients['Nr']

    size = numpy.max(self.matrixCoordinates, 1) - numpy.min(self.matrixCoordinates, 1) + 1
    # Do not pass kwargs directly, as distances may be specified, which must be forced to [1] for this class
    angles = imageoperations.generateAngles(size,
                                            force2Dextraction=self.kwargs.get('force2D', False),
                                            force2Ddimension=self.kwargs.get('force2Ddimension', 0))

    P_glrlm = cMatrices.calculate_glrlm(self.matrix, self.maskArray, angles, Ng, Nr)
    P_glrlm = self._applyMatrixOptions(P_glrlm, angles)

    return P_glrlm
Exemplo n.º 6
0
    def _calculateMatrix(self, voxelCoordinates=None):
        self.logger.debug('Calculating GLRLM matrix in C')

        Ng = self.coefficients['Ng']
        Nr = numpy.max(self.imageArray.shape)

        matrix_args = [
            self.imageArray, self.maskArray, Ng, Nr,
            self.settings.get('force2D', False),
            self.settings.get('force2Ddimension', 0)
        ]
        if self.voxelBased:
            matrix_args += [
                self.settings.get('kernelRadius', 1), voxelCoordinates
            ]

        P_glrlm, angles = cMatrices.calculate_glrlm(
            *matrix_args)  # shape (Nvox, Ng, Nr, Na)

        self.logger.debug('Process calculated matrix')

        # Delete rows that specify gray levels not present in the ROI
        NgVector = range(1, Ng + 1)  # All possible gray values
        GrayLevels = self.coefficients[
            'grayLevels']  # Gray values present in ROI
        emptyGrayLevels = numpy.array(
            list(set(NgVector) - set(GrayLevels)),
            dtype=int)  # Gray values NOT present in ROI

        P_glrlm = numpy.delete(P_glrlm, emptyGrayLevels - 1, 1)

        # Optionally apply a weighting factor
        if self.weightingNorm is not None:
            self.logger.debug('Applying weighting (%s)', self.weightingNorm)

            pixelSpacing = self.inputImage.GetSpacing()[::-1]
            weights = numpy.empty(len(angles))
            for a_idx, a in enumerate(angles):
                if self.weightingNorm == 'infinity':
                    weights[a_idx] = max(numpy.abs(a) * pixelSpacing)
                elif self.weightingNorm == 'euclidean':
                    weights[a_idx] = numpy.sqrt(
                        numpy.sum((numpy.abs(a) * pixelSpacing)**2))
                elif self.weightingNorm == 'manhattan':
                    weights[a_idx] = numpy.sum(numpy.abs(a) * pixelSpacing)
                elif self.weightingNorm == 'no_weighting':
                    weights[a_idx] = 1
                else:
                    self.logger.warning(
                        'weigthing norm "%s" is unknown, weighting factor is set to 1',
                        self.weightingNorm)
                    weights[a_idx] = 1

            P_glrlm = numpy.sum(P_glrlm * weights[None, None, None, :],
                                3,
                                keepdims=True)

        Nr = numpy.sum(P_glrlm, (1, 2))

        # Delete empty angles if no weighting is applied
        if P_glrlm.shape[3] > 1:
            emptyAngles = numpy.where(numpy.sum(Nr, 0) == 0)
            if len(emptyAngles[0]) > 0:  # One or more angles are 'empty'
                self.logger.debug('Deleting %d empty angles:\n%s',
                                  len(emptyAngles[0]), angles[emptyAngles])
                P_glrlm = numpy.delete(P_glrlm, emptyAngles, 3)
                Nr = numpy.delete(Nr, emptyAngles, 1)
            else:
                self.logger.debug('No empty angles')

        Nr[Nr == 0] = numpy.nan  # set sum to numpy.spacing(1) if sum is 0?
        self.coefficients['Nr'] = Nr

        return P_glrlm