Skip to main content

i love freedom, free my hand.

Project description

ifree

Python package PyPI PyPI - Python Version License

ifree is a small python package for making you free.

  • Radiomics Feature Extraction
  • Feature Pre-Processing and Selecting,
  • Dicom Processing
  • Crop CT/MR according to RT Structure
  • Calculate dose volume histogram (DVH)

One of the main benefits of ifree is that it handles data easily (see example below).

Installation

ifree can be installed with pip:

pip install ifree

For the bleeding edge version, install directly from github (consider adding --force-reinstall):

pip install git+git://github.com/linzhenyuyuchen/ifree.git

or by cloning the repo:

git clone https://github.com/linzhenyuyuchen/ifree.git
cd ifree
python setup.py install

Example

Feature Processing

Radiomics Feature Extractor

radiomics.yaml

# https://github.com/Radiomics/pyradiomics/blob/master/examples/exampleSettings/exampleMR_NoResampling.yaml
# https://github.com/Radiomics/pyradiomics/blob/master/examples/exampleSettings/MR_2D_extraction.yaml
imageType:
  Original: {}
  LoG:
   # If the in-plane spacing is large (> 2mm), consider removing sigma value 1.
   sigma: [ 1.0, 3.0, 5.0 ]
  Wavelet: {}

setting:
  label: 1 # 255
#  interpolator: 'sitkBSpline'
#  resampledPixelSpacing: [2, 2, 2]

#  normalize: true
#  normalizeScale: 100  # This allows you to use more or less the same bin width.

featureClass:
  firstorder:
  shape:
  glcm:
  glrlm:
  glszm:
  gldm:
  ngtdm:
from ifree import processing

idx = list()
imagePaths = list()
maskPaths = list()
paramPath = "./radiomics.yaml"
outputPath = "./features.csv"

helper = processing.FeatureExtractor(idx, imagePaths, maskPaths, paramPath, outputPath)
helper.extract(force=True)

Feature Pre-Processing

df_train = pd.DataFrame()
df_test = pd.DataFrame()
helper = processing.FeatureProcess(df_train, df_test)
# preprocessing methods
helper.simpleImpute(strategy='mean') # mean, median, most_frequent, constant
helper.standardScale()
helper.normalizer()
helper.minMaxScaler()
helper.pca(n_components=10)
array_train = helper.X_train
array_test = helper.X_test

Feature Selector

x_array = np.array()
y_list = list()
featureNames = list()
selector = processing.FeatureSelector(x_array, y_list, featureNames)
# selection methods
x_array_new, y_array_new = selector.univarSelector(top_k=600, method_name="f_classif", inplace=True)
print(x_array_new.shape)
x_array_new, y_array_new = selector.ttestSelector(inplace=True)
print(x_array_new.shape)
x_array_new, y_array_new = selector.mannSelector(inplace=True)
print(x_array_new.shape)
x_array_new, y_array_new = selector.modelSelector(inplace=True)
print(x_array_new.shape)
# name of selected features 
print(selector.featureNames)

Dicom Processing

Crop CT/MR according to RT

from ifree import dicom

# get paths for CT, MR, DOSE and RT
fileDir = "./p/"
ctfiles, rtfile, mrfiles, dosefile, patientID, patientName = dicom.GetFilePath(fileDir)

# get MRs or CTs related to RT and copy them to new dir
patientNames, patientIDs, id2mrs, id2rt, id2dose = dicom.archiveFiles(old_Dir, new_Dir, copyed=True)

# crop ROI-MASK and its CT or MR
roiName = "ctv"
newDir = "./p/"
newSize = [100, 100, 100] # leave None to get origin size
idx_done_list = dicom.cropROI(id2mrs, id2rt, roiName, newDir, newSize)

idx_done_list = dicom.cropDose(id2dose, id2rt, roiName, newDir, newSize)

Get Dose-Volume Histogram (DVH)

# Get Dose-Volume Histogram (DVH)
rtssfile = "./RT.dicom"
rtdosefile = "./RS.dicom"
# percents is the space between 0-100
roiname = "Body"
data, percents = dicom.get_dvh_of_key(rtssfile, rtdosefile)
dose_list, max_value, min_value, mean_value = data[roiname]

# saved to dataframe
dose_features = {}
roiname = "Body"
for i, (pid, rtssfile) in enumerate(id2rt.item()):
    rtdosefile = id2dose[pid]
    # percents is the space between 0-100
    data, percents = dicom.get_dvh_of_key(rtssfile, rtdosefile)
    dose_list, max_value, min_value, mean_value = data[roiname]
    tmp = {"pid": pid, "dose_max": max_value, "dose_min": min_value, "dose_mean": mean_value,}
    for i, pc in enumerate(percents):
        tmp[f"dose_{pc}"] = dose_list[i]
    dose_features[i] = tmp
df_dose_features = pd.DataFrame.from_dict(dose_features, orient='index')
df_dose_features.head()

For more examples, see the examples folder.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ifree-1.1.1.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ifree-1.1.1-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file ifree-1.1.1.tar.gz.

File metadata

  • Download URL: ifree-1.1.1.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.6

File hashes

Hashes for ifree-1.1.1.tar.gz
Algorithm Hash digest
SHA256 9552fe2f194d1a21664351717d484311180ad7b4f07ac482583263bce1425e97
MD5 c843b6c58f6b49ec92d8ed45fba4409d
BLAKE2b-256 27da323548ab4d622932ccbc8bffeb2289da13bc80198e591d12fd9233811f90

See more details on using hashes here.

File details

Details for the file ifree-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: ifree-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.6

File hashes

Hashes for ifree-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 85d74eb36830f21c5a11c8d30df5318caf3ccb23ca36f8bcd89ed949d0032cdb
MD5 1795809e12d538646689902f1a0f076a
BLAKE2b-256 ad5a1f189507ea408f834e55d49d86db06388eff23ab7d209f612e7f3be55b46

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page