AIM-Harvard / pyradiomics

Open-source python package for the extraction of Radiomics features from 2D and 3D images and binary masks. Support: https://discourse.slicer.org/c/community/radiomics
http://pyradiomics.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.17k stars 497 forks source link

Creating an image map based on specific texture feature #534

Closed abhinandanbatra closed 2 years ago

abhinandanbatra commented 5 years ago

Hi, I was wondering if pyradiomics allows to create image map based on specific texture features. For example entropy, GLCM.

JoostJM commented 5 years ago

Yes, PyRadiomics support both segment-based (default, 1 value /p feature /p ROI) and voxel-based (1 value /p feature /p voxel in the ROI --> yields feature maps). See the documentation at pyradiomics.readthedocs.io for instructions on how to use it (especially sections "Usage" and "Customizing the extraction")

abhinandanbatra commented 5 years ago

Hey, Thanks for the response. Is there an example that I can use

abhinandanbatra commented 5 years ago

Hi, I am running voxel segment based feature extraction. Based on what I have understood you have to run voxel based extraction to extract feature maps. I am trying to run the voxel based extraction but the code crashes. Can you help. Here is the code that I am using

` extractor.disableAllFeatures() extractor.enableFeatureClassByName('firstorder') extractor.getFeatureNames('firstorder') print('Enabled features:\n\t', extractor._enabledFeatures)

        features = OrderedDict()
        features[Case_id] = extractor.execute(image, mask, label=4,voxelBased=True)

        result = features[Case_id]

        print('Result type:', type(result))  # result is returned in a Python ordered dictionary)
        print('')
        print('Calculated features for Case_{}:'.format(screen))
        for key, val in six.iteritems(result):
            if isinstance(val, sitk.Image): # Feature map
                sitk.WriteImage(val, key + '.nrrd', True)
                print("Stored feature in" % (key, key + ".nrrd"))
        Finalresult=six.iteritems(result)`
JoostJM commented 5 years ago

@abhinandanbatra, can you share some details as to the nature of the crash? I.e. the error and stacktrace would be helpful, and a PyRadiomics log file even more so.

JoostJM commented 5 years ago

As to an example, the PyRadiomics github repository contains a folder examples, which also contains an example for voxel-based extraction: helloVoxel.py

abhinandanbatra commented 5 years ago

@JoostJM . I was able to run the voxel based extraction. I opened the .nrrd files for first order texture in slicer and they all look same for all the features. Am I doing something wrong? Here is the code. Also, attached are the maps that I got Screen Shot 2019-10-09 at 4 57 08 PM Screen Shot 2019-10-09 at 4 57 20 PM

Will appreciate your help

`for Caseid in range(1,2): path = '/Users/abhinandanbatra/Desktop/Case{}/'.format(Case_id) print(path) for main in os.listdir(path): if fnmatch.fnmatch(main,'*_fatfraction.nii'): print(main) image=sitk.ReadImage(os.path.join(path,main))

print(image)

        print(("{}:{}".format("Image Dimensions",image.GetDimension())))
        print(("{}:{}".format("Image Size",image.GetSize())))
        SubjectImage=sitk.GetArrayFromImage(image)

        fig,ax=plt.subplots(nrows=1, ncols=2)
        ax[0].imshow((SubjectImage)[0,:,:], cmap="gray", interpolation='nearest')
        ax[0].set_title(main)
for screen in os.listdir(path):
    if fnmatch.fnmatch(screen,'*_label.nii'):
        print(screen)
        mask=sitk.ReadImage(os.path.join(path,screen))

print(mask)

        print(("{}:{}".format("Mask Dimensions",mask.GetDimension())))
        print(("{}:{}".format("Mask Size",mask.GetSize())))
        Segmentationimage=sitk.GetArrayFromImage(mask)
        ax[1].imshow((Segmentationimage)[0,:,:], cmap="gray", interpolation='nearest')
        ax[1].set_title(screen)

        extractor.disableAllFeatures()
        extractor.enableFeatureClassByName('firstorder')
        extractor.getFeatureNames('firstorder')
        print('Enabled features:\n\t', extractor._enabledFeatures)

        features = OrderedDict()
        features[Case_id] = extractor.execute(image, mask, label=4)
        featureVector = extractor.execute(image,mask, label=4, voxelBased=True)

        result = features[Case_id]

        print('Result type:', type(result))  # result is returned in a Python ordered dictionary)
        print('')
        print('Calculated features for Case_{}:'.format(screen))
        Finalresult=six.iteritems(result)
        for key, value in six.iteritems(featureVector):
            if isinstance(value, sitk.Image):
                sitk.WriteImage(value, key + '.nrrd',True)
                print("Stored feature %s in %s" % (key, key + ".nrrd"))
            else:  # Diagnostic information
                print("\t%s: %s" %(key, val))`
JoostJM commented 5 years ago

@abhinandanbatra, can you run using the PyRadiomics Command line and add logging ( pyradiomics <path/to/image> <path/to/mask> -p <path/to/params.yml> --mode voxel --log-file=pyradiomics.log --logging-level=DEBUG?

Additionally, how do you instantiate the extractor? Are you using just default settings? The feature maps look like flat regions (all values the same), but are the values of kurtosis equal to those of entropy? What values are you getting (you can use Slicer's dataprobe to see this)? Which version of PyRadiomics are you using? PyRadiomcs versions < 2.2.0 have a bug that caused first-order texture maps to be incorrect (81e713a).

abhinandanbatra commented 5 years ago

I am using default setting. I just changed the bin width 0.007 and correct mask to "True". If I run the features maps based on other feature set I get an output. I ran just for ngtdm and got the following output for busyness and strength

ngtdm busyness ngtdm strength

I checked my version as well and I think it is up to date. here is the screenshot when I do the install of pyradiomics again

Screen Shot 2019-10-11 at 1 52 20 PM

I ran the log file but it is empty

JoostJM commented 5 years ago

Look at your version of PyRadiomics, it's 2.1.1. If you upgrade to the latest version, your firstorder should then be fine.

abhinandanbatra commented 5 years ago

He Joost I upgraded pyradiomics but now I am getting this error

module 'radiomics.featureextractor' has no attribute 'RadiomicsFeaturesExtractor'

abhinandanbatra commented 5 years ago

hey sorry to bother you. i found the issue its extra "S" in features

JoostJM commented 5 years ago

@abhinandanbatra That's indeed a fairly recent change to make the class name more consistent with the module name.

AnkushJamthikar commented 9 months ago

Hi, I am working on 2D ultrasound images. Is it possible to generate the feature map for such images, since it has pixels and not voxels? I greatly appreciate any suggestions or guidance on this issue.

Thank you!

AnkushJamthikar commented 8 months ago

Hi I could generate the feature map for the 2D image by adding additional channel to the image so that i can be saved as (x,y,1). Doing this I could get the feature maps. But it seems like, for some of the features, my code is getting crashed.

I have no idea why this is happening of certain features only. Any solution to this?

Thank you!