pyradiomics
官方文档里有几个示例文件,里面涉及了包括yaml文件设置、feature extraction、可视化等一系列影像组学常规操作,是非常好的学习资料。源码请参考:https://github.com/AIM-Harvard/pyradiomics
今天学习标题名为"PyRadiomics toolbox in Python"的这份文档。
0 加载module和函数
# Radiomics package
from radiomics import featureextractor
import six, numpy as np
1 获取图像及其掩模文件(Setting up data)
import os
import SimpleITK as sitk
from radiomics import getTestCase
imagepath_1, labelpath_1 = getTestCase('brain1')
imagepath_2, labelpath_2 = getTestCase('brain2')
image_1 = sitk.ReadImage(imagepath_1)
label_1 = sitk.ReadImage(labelpath_1)
image_2 = sitk.ReadImage(imagepath_2)
label_2 = sitk.ReadImage(labelpath_2)
- 查看图像(Show the images)
# Display the images
%matplotlib inline
import matplotlib.pyplot as plt
plt.figure(figsize=(20,20))
# First image
plt.subplot(2,2,1)
plt.imshow(sitk.GetArrayFromImage(image_1)[12,:,:], cmap="gray")
plt.title("Brain #1")
plt.subplot(2,2,2)
plt.imshow(sitk.GetArrayFromImage(label_1)[12,:,:])
plt.title("Segmentation #1")
# Second image
plt.subplot(2,2,3)
plt.imshow(sitk.GetArrayFromImage(image_2)[8,:,:], cmap="gray")
plt.title("Brain #2")
plt.subplot(2,2,4)
plt.imshow(sitk.GetArrayFromImage(label_2)[8,:,:])
plt.title("Segmentation #2")
plt.show()
rad_fig1.png
- 提取特征值(Extract the features)
import os
# Instantiate the extractor
params = os.path.join(os.getcwd(), '..', 'examples', 'exampleSettings', 'Params.yaml')
extractor = featureextractor.RadiomicsFeatureExtractor(params)
result_1 = extractor.execute(image_1, label_1)
result_2 = extractor.execute(image_2, label_2)
- 作图准备(Prepare for plotting)
# Make an array of the values
feature_1 = np.array([])
feature_2 = np.array([])
for key, value in six.iteritems(result_1):
if key.startswith("original_"): # excluding meta-features
feature_1 = np.append ( feature_1, result_1[key])
feature_2 = np.append ( feature_2, result_2[key])
- 绘图(可视化brain1和2的特征值,及其差异)
plt.figure(figsize=(20,20))
plt.subplot(3,1,1)
plt.plot(feature_1)
plt.yscale('log')
plt.title ( "Features from brain #1")
plt.subplot(3,1,2)
plt.plot(feature_2)
plt.yscale('log')
plt.title ( "Features from brain #1")
plt.subplot(3,1,3)
plt.plot(feature_1 - feature_2)
plt.yscale('log')
plt.title ( "Difference")
plt.show()
rad_fig2.png
Notes:不晓得为什么,没有画出和源码里一样的图。尝试在不同环境下Windows和MacOs使用pyradiomics也不奏效。
网友评论