pyradiomics
官方文档里有几个示例文件,里面涉及了包括yaml文件设置、feature extraction、可视化等一系列影像组学常规操作,是非常好的学习资料。源文件链接:https://github.com/AIM-Harvard/pyradiomics
今天学习名称为''FilteringEffects"的这份文档。
Exploring the effects of filtering on Radiomics features
0 导入所需的包
# Radiomics package
from radiomics import featureextractor
import six, numpy as np
- 加载数据(Setting up data)
import os
import SimpleITK as sitk
from radiomics import getTestCase
# repositoryRoot points to the root of the repository. The following line gets that location if this Notebook is run
# from it's default location in \pyradiomics\examples\Notebooks
repositoryRoot = os.path.abspath(os.path.join(os.getcwd(), ".."))
imagepath, labelpath = getTestCase('brain1', repositoryRoot)
image = sitk.ReadImage(imagepath)
label = sitk.ReadImage(labelpath)
- 查看图片(Show the images)
# Display the images
%matplotlib inline
import matplotlib.pyplot as plt
plt.figure(figsize=(20,20))
# First image
plt.subplot(1,2,1)
plt.imshow(sitk.GetArrayFromImage(image)[12,:,:], cmap="gray")
plt.title("Brain")
plt.subplot(1,2,2)
plt.imshow(sitk.GetArrayFromImage(label)[12,:,:])
plt.title("Segmentation")
plt.show()
image.png
- 提取特征(Extract the features)
import os
# Instantiate the extractor
params = os.path.join(os.getcwd(), '..', 'examples', 'exampleSettings', 'Params.yaml')
extractor = featureextractor.RadiomicsFeatureExtractor(params)
extractor.enableFeatureClassByName('shape', enabled=False) # disable shape as it is independent of gray value
# Construct a set of SimpleITK filter objects
filters = {
"AdditiveGaussianNoise" : sitk.AdditiveGaussianNoiseImageFilter(),
"Bilateral" : sitk.BilateralImageFilter(),
"BinomialBlur" : sitk.BinomialBlurImageFilter(),
"BoxMean" : sitk.BoxMeanImageFilter(),
"BoxSigmaImageFilter" : sitk.BoxSigmaImageFilter(),
"CurvatureFlow" : sitk.CurvatureFlowImageFilter(),
"DiscreteGaussian" : sitk.DiscreteGaussianImageFilter(),
"LaplacianSharpening" : sitk.LaplacianSharpeningImageFilter(),
"Mean" : sitk.MeanImageFilter(),
"Median" : sitk.MedianImageFilter(),
"Normalize" : sitk.NormalizeImageFilter(),
"RecursiveGaussian" : sitk.RecursiveGaussianImageFilter(),
"ShotNoise" : sitk.ShotNoiseImageFilter(),
"SmoothingRecursiveGaussian" : sitk.SmoothingRecursiveGaussianImageFilter(),
"SpeckleNoise" : sitk.SpeckleNoiseImageFilter(),
}
# Filter
results = {}
results["baseline"] = extractor.execute(image, label)
for key, value in six.iteritems(filters):
print ( "filtering with " + key )
filtered_image = value.Execute(image)
results[key] = extractor.execute(filtered_image, label)
image.png
- 数据准备(Prepare for analysis)
# Keep an index of filters and features
filter_index = list(sorted(filters.keys()))
feature_names = list(sorted(filter ( lambda k: k.startswith("original_"), results[filter_index[0]] )))
- 比较分析(Look at the features with highest and lowest coefficient of variation)
# Pull in scipy to help find cv
import scipy.stats
features = {}
cv = {}
for key in feature_names:
a = np.array([])
for f in filter_index:
a = np.append(a, results[f][key])
features[key] = a
cv[key] = scipy.stats.variation(a)
# a sorted view of cv
cv_sorted = sorted(cv, key=cv.get, reverse=True)
# Print the top 10
print ("\n")
print ("Top 10 features with largest coefficient of variation")
for i in range(0,10):
print ("Feature: {:<50} CV: {}".format ( cv_sorted[i], cv[cv_sorted[i]]))
print ("\n")
print ("Bottom 10 features with _smallest_ coefficient of variation")
for i in range(-11,-1):
print ("Feature: {:<50} CV: {}".format ( cv_sorted[i], cv[cv_sorted[i]]))
image.png
网友评论