<commit_before>#!/usr/bin/env python

from __future__ import print_function
import SimpleITK as sitk
import six
from radiomics import getTestCase, imageoperations, featureextractor, getTestCase
from radiomics.featureextractor import RadiomicsFeaturesExtractor


def extract(imagePath, maskPath, paramfilepath, *args):
  r"""
  RKP - 3/20/2018
  Wrapper class for calculation of a radiomics features using pyradiomics.

  """

  # testWaveletFilter = False
  # testLoGFilter = False

  image, mask = RadiomicsFeaturesExtractor().loadImage(imagePath, maskPath)

  if imagePath is None or maskPath is None:  # Something went wrong, in this case PyRadiomics will also log an error
    print('Error getting testcase!')
    exit()

  params = paramfilepath
  extractor = featureextractor.RadiomicsFeaturesExtractor(params)


  result = extractor.execute(imagePath, maskPath)
  for key, val in six.iteritems(result):
     strval = str(key)
     if strval.find("-") == -1:
       continue
     else:
       strval = strval.replace("-", "_")
       result[strval] = result.pop(key)


  if len(args) == 1 and args[0] == 'LoG':
    resultdict = {}
    for decompositionImage, decompositionName, inputKwargs in imageoperations.getLoGImage(image):
      resultArray = sitk.GetArrayFromImage(decompositionImage)
      # imgName = decompositionName + '.nrrd'
      # sitk.WriteImage(decompositionImage, imgName)
      resultdict[decompositionName] = resultArray
    return resultdict

  elif len(args) == 1 and args[0] == 'wavelet':
    resultdict = {}
    for decompositionImage, decompositionName, inputKwargs in imageoperations.getWaveletImage(image):
      resultArray=sitk.GetArrayFromImage(decompositionImage)
      # imgName = decompositionName+'.nrrd'
      # sitk.WriteImage(decompositionImage,imgName)
      resultdict[decompositionName] = resultArray
    return resultdict

  else:
    return result

#
# def main():
#
#   imagePath = 'C:\\Users\\pandyar1\\AppData\\Local\\Temp\\scan.nrrd'
#   maskPath = 'C:\\Users\\pandyar1\\AppData\\Local\\Temp\\mask.nrrd'
#   paramFilePath = 'W:\\Rutu\\CERR-testing\\Unit_Testing\\tests_for_cerr\pyradParams.yaml'
#   result = extract(imagePath, maskPath, paramFilePath)
#
#
# main()




<commit_msg>added computation of wavelet/LoG filtered images for comparison with CERR<commit_after>#!/usr/bin/env python

from __future__ import print_function
import SimpleITK as sitk
import numpy
import scipy
#import matplotlib
import matlab
#import matlab.engine
import six
from radiomics import getTestCase, imageoperations, featureextractor, getTestCase
from radiomics.featureextractor import RadiomicsFeaturesExtractor


def extract(imagePath, maskPath, paramfilepath, preprocessingFilter):
  r"""
  RKP - 3/20/2018
  Wrapper class for calculation of a radiomics features using pyradiomics.

  """


  image, mask = RadiomicsFeaturesExtractor().loadImage(imagePath, maskPath)

  if imagePath is None or maskPath is None:  # Something went wrong, in this case PyRadiomics will also log an error
    exit()

  params = paramfilepath

#if wavelet or LoG specified in argument, return only the preprocessed image arrays
  if preprocessingFilter == 'LoG':
    resultdict = {}
    extractor = featureextractor.RadiomicsFeaturesExtractor(params)
    result = extractor.execute(imagePath, maskPath)
    sigmaValues = [1.0]
    for logImage, imageTypeName, inputKwargs in imageoperations.getLoGImage(image, sigma=sigmaValues):
      resultArray = sitk.GetArrayFromImage(logImage)
      resultArray = matlab.double(resultArray.tolist())
      # imgName = decompositionName + '.nrrd'
      # sitk.WriteImage(decompositionImage, imgName)
      #resultdict[decompositionName] = resultArray
    return resultArray

  elif preprocessingFilter == 'wavelet':
    resultdict = {}
    for decompositionImage in imageoperations.getWaveletImage(image):
      resultArray=sitk.GetArrayFromImage(decompositionImage)
      # imgName = decompositionName+'.nrrd'
      # sitk.WriteImage(decompositionImage,imgName)
      resultArray = matlab.double(resultArray.tolist())
      resultdict[decompositionName] = resultArray
    for key, val in six.iteritems(resultdict):
      strval = str(key)
      if strval.find("-") == -1:
        continue
      else:
        strval = strval.replace("-", "_")
        strval = strval.replace("-", "_")
        resultdict[strval] = resultdict.pop(key)

    return resultdict
#perform feature extraction on original images and return the result
  else:    
    extractor = featureextractor.RadiomicsFeaturesExtractor(params)

    result = extractor.execute(imagePath, maskPath)
    for key, val in six.iteritems(result):
      strval = str(key)
      if strval.find("-") == -1:
        continue
      else:
        strval = strval.replace("-", "_")
        strval = strval.replace("-", "_")
        result[strval] = result.pop(key)
    return result

#
def main():

  imagePath = 'C:\\Users\\pandyar1\\AppData\\Local\\Temp\\scan.nrrd'
  maskPath = 'C:\\Users\\pandyar1\\AppData\\Local\\Temp\\mask.nrrd'
  paramFilePath = 'W:\\Rutu\\CERR-testing\\Unit_Testing\\tests_for_cerr\pyradParams.yaml'
  result = extract(imagePath, maskPath, paramFilePath, 'LoG')


main()





{'fraction_non_alphanumeric': 0.10491929285165258, 'fraction_numerical': 0.005188316679477325, 'mean_word_length': 3.878163074039363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6031394', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1566, 'n_words': 374}