Skip to content

Commit 7637539

Browse files
committed
ENH: Add CLI scripts to setup
Rename the scripts to pyradiomics and pyradiomicsbatch (easy to remeber, but unique to package, for use in the commandline). Additionally, move them to separate folder in the package, and add them to the setup script so the CLI gets distributed with the installation, not just the git repo. Additionally, add the use of these scripts to the `Usage` section of the documentation and adapt this section further to represent the more standard use of the toolbox, including interactive extraction using the `featureextractor` module. Finally, adapt the examples in the bin folder to make use of the example data stored in the repository, this removes the need to install the example data when pyradiomics is installed.
1 parent 610dffc commit 7637539

File tree

9 files changed

+134
-12
lines changed

9 files changed

+134
-12
lines changed

bin/Notebooks/helloFeatureClass.ipynb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@
3838
"## Getting the test case"
3939
]
4040
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {},
44+
"source": [
45+
"Testing data is contained in the pyradiomics/data folder, while this file is in the pyradiomics/bin/Notebooks folder. \n",
46+
"\n",
47+
"The next line of code gets the location of the current path and gets the location of the data as a relative path by going up two folders (os.path.sep + \"..\") and then move into the data folders (os.path.sep + \"data\").\n",
48+
"\n",
49+
"For this to work, the current active directory should be pyradiomics/bin/notebooks, which is the case if this file is run from the pyradiomics/bin/Notebooks folder."
50+
]
51+
},
4152
{
4253
"cell_type": "code",
4354
"execution_count": 2,

bin/Notebooks/helloRadiomics.ipynb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@
7777
"## Getting the testcase"
7878
]
7979
},
80+
{
81+
"cell_type": "markdown",
82+
"metadata": {},
83+
"source": [
84+
"Testing data is contained in the pyradiomics/data folder, while this file is in the pyradiomics/bin/Notebooks folder. \n",
85+
"\n",
86+
"The next line of code gets the location of the current path and gets the location of the data as a relative path by going up two folders (os.path.sep + \"..\") and then move into the data folders (os.path.sep + \"data\").\n",
87+
"\n",
88+
"For this to work, the current active directory should be pyradiomics/bin/notebooks, which is the case if this file is run from the pyradiomics/bin/Notebooks folder."
89+
]
90+
},
8091
{
8192
"cell_type": "code",
8293
"execution_count": 3,
@@ -86,7 +97,7 @@
8697
"outputs": [],
8798
"source": [
8899
"testCase = 'brain1'\n",
89-
"dataDir = os.path.abspath(radiomics.__path__[0] + os.path.sep + \"..\" + os.path.sep + \"data\")\n",
100+
"dataDir = os.path.abspath(\"\") + os.path.sep + \"..\" + os.path.sep + \"..\" + os.path.sep + \"data\"\n",
90101
"imageName = str(dataDir + os.path.sep + testCase +'_image.nrrd')\n",
91102
"maskName = str(dataDir + os.path.sep + testCase + '_label.nrrd')\n",
92103
"\n",
@@ -421,7 +432,7 @@
421432
"Computed general_info_ImageSpacing: (0.7812499999999999; 0.7812499999999999; 6.499999999999998)\n",
422433
"Computed general_info_InputImages: {'original': {}}\n",
423434
"Computed general_info_MaskHash: 9dc2c3137b31fd872997d92c9a92d5178126d9d3\n",
424-
"Computed general_info_Version: 0.post402.dev0+gb4e35ec\n",
435+
"Computed general_info_Version: v1.0.post11.dev0+g610dffc\n",
425436
"Computed general_info_VolumeNum: 2\n",
426437
"Computed general_info_VoxelNum: 4137\n",
427438
"Computed original_firstorder_InterquartileRange: 253.0\n",
@@ -453,6 +464,17 @@
453464
"for featureName in featureVector.keys():\n",
454465
" print \"Computed %s: %s\" % (featureName, featureVector[featureName])"
455466
]
467+
},
468+
{
469+
"cell_type": "code",
470+
"execution_count": null,
471+
"metadata": {
472+
"collapsed": true
473+
},
474+
"outputs": [],
475+
"source": [
476+
""
477+
]
456478
}
457479
],
458480
"metadata": {

bin/helloFeatureClass.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from radiomics import firstorder, glcm, imageoperations, shape, glrlm, glszm
55

66
# testBinWidth = 25 this is the default bin size
7-
# testResampledPixelSpacing = (3,3,3) no resampling for now.
7+
# testResampledPixelSpacing = [3,3,3] no resampling for now.
88

9-
dataDir = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + ".." + os.path.sep + "data"
9+
dataDir = os.path.abspath("") + os.path.sep + ".." + os.path.sep + "data" # assumes this file in in pyradiomics/bin
1010
# imageName = str(dataDir + os.path.sep + 'prostate_phantom_subvolume.nrrd')
1111
# maskName = str(dataDir + os.path.sep + 'prostate_phantom_subvolume-label.nrrd')
1212
imageName = str(dataDir + os.path.sep + 'breast1_image.nrrd')

bin/helloRadiomics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from radiomics import featureextractor
55
import radiomics
66

7-
dataDir = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + ".." + os.path.sep + "data"
7+
dataDir = os.path.abspath("") + os.path.sep + ".." + os.path.sep + "data" # assumes this file in in pyradiomics/bin
88
# imageName = str(dataDir + os.path.sep + 'prostate_phantom_subvolume.nrrd')
99
# maskName = str(dataDir + os.path.sep + 'prostate_phantom_subvolume-label.nrrd')
1010
imageName = str(dataDir + os.path.sep + 'brain1_image.nrrd')

docs/usage.rst

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,95 @@ Usage
66
Example
77
-------
88

9-
* Run the helloRadiomics example, using sample data provided in ``pyradiomics/data``:
9+
* PyRadiomics example code and data is available in the `Github repository <https://github.com/Radiomics/pyradiomics>`_
10+
11+
* The sample sample data is provided in ``pyradiomics/data``.
12+
13+
* Use jupyter to run the helloRadiomics example, located in ``pyradiomics/bin/Notebooks``.
14+
15+
* If jupyter is not installed, run the python script alternative (``pyradiomics/bin/helloRadiomics.py``):
16+
17+
* ``python helloRadiomics.py``
18+
19+
----------------
20+
Command Line Use
21+
----------------
22+
23+
* PyRadiomics has 2 commandline scripts, ``pyradiomics`` is for single image feature extraction and ``pyradiomicsbatch``
24+
is for feature extraction from a batch of images and segmentations.
25+
26+
* Both scripts can be run directly from a command line window, anywhere in your system.
27+
28+
* To extract features from a single image and segmentation run::
29+
30+
pyradiomics <path/to/image> <path/to/segmentation>
31+
32+
* To extract features from a batch run::
33+
34+
pyradiomicsbatch <path/to/input> <path/to/output>
35+
36+
* The input file for batch processing is a CSV file where each row represents one combination of an image and a
37+
segmentation and contains 5 elements: 1) patient ID, 2) sequence name (image identifier), 3) reader (segmentation
38+
identifier), 4) path/to/image, 5) path/to/mask.
39+
40+
* For more information on passing parameter files, setting up logging and controlling output format, run::
41+
42+
pyradiomics -h
43+
pyradiomicsbatch -h
1044

11-
* ``python bin/helloRadiomics.py``
1245

1346
---------------
1447
Interactive Use
1548
---------------
1649

17-
* Add pyradiomics to the environment variable PYTHONPATH:
50+
* (LINUX) Add pyradiomics to the environment variable PYTHONPATH:
51+
52+
* ``setenv PYTHONPATH /path/to/pyradiomics/radiomics``
53+
54+
* Start the python interactive session:
55+
56+
* ``python``
57+
58+
* Import the necessary classes::
59+
60+
from radiomics import featureextractor
61+
import sys, os
62+
63+
* Set up a pyradiomics directory variable::
64+
65+
dataDir = '/path/to/pyradiomics'
66+
67+
* You will find sample data files brain1_image.nrrd and brain1_label.nrrd in that directory.
68+
69+
* Store the path of your image and mask in two variables::
70+
71+
imageName = os.path.join(dataDir, "data", 'brain1_image.nrrd')
72+
maskName = os.path.join(dataDir, "data", 'brain1_label.nrrd')
73+
74+
* Also store the path to the file containing the extraction settings::
75+
76+
params = os.path.join(dataDir, "bin", "Params.yaml")
77+
78+
* Instantiate the feature extractor class with the parameter file::
79+
80+
extractor = featureextractor.RadiomicsFeaturesExtractor(params)
81+
82+
* Calculate the features::
83+
84+
result = extractor.execute(imageName, maskName)
85+
for key, val in result.iteritems():
86+
print "\t%s: %s" %(key, val)
87+
88+
* See the :ref:`feature extractor class<radiomics-featureextractor-label>` for more information on using this core class.
89+
90+
------------------------------
91+
Using feature classes directly
92+
------------------------------
93+
94+
* This represents an example where feature classes are used directly, circumventing checks and preprocessing done by
95+
the radiomics feature extractor class, and is not intended as standard use example.
96+
97+
* (LINUX) Add pyradiomics to the environment variable PYTHONPATH:
1898

1999
* ``setenv PYTHONPATH /path/to/pyradiomics/radiomics``
20100

@@ -46,6 +126,6 @@ Interactive Use
46126
firstOrderFeatures = firstorder.RadiomicsFirstOrder(image,mask)
47127
firstOrderFeatures.calculateFeatures()
48128
for (key,val) in firstOrderFeatures.featureValues.iteritems():
49-
print ' ',key,':',val
129+
print "\t%s: %s" % (key, val)
50130

51131
* See the :ref:`radomics package<radiomics-firstorder-label>` for more features that you can calculate.

radiomics/scripts/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
import argparse
24
import sys
35
import os.path
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
import argparse
24
import sys
35
import os.path
@@ -73,6 +75,7 @@ def main():
7375
# Extract features
7476
logging.info('Extracting features with kwarg settings: %s', str(extractor.kwargs))
7577

78+
headers = False
7679
for idx, entry in enumerate(flists, start=1):
7780

7881
logging.info("(%d/%d) Processing Patient: %s, Study: %s, Reader: %s", idx, len(flists), entry[0], entry[1],
@@ -94,7 +97,9 @@ def main():
9497

9598
if args.format == 'csv':
9699
writer = csv.writer(args.outFile, lineterminator='\n')
97-
if idx == 1: writer.writerow(featureVector.keys())
100+
if not headers:
101+
writer.writerow(featureVector.keys())
102+
headers = True
98103
writer.writerow(featureVector.values())
99104
elif args.format == 'json':
100105
json.dump(featureVector, args.out)

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
setup(name='pyradiomics',
99
version=versioneer.get_version(),
1010
cmdclass=versioneer.get_cmdclass(),
11-
packages=['radiomics'],
11+
packages=['radiomics', 'radiomics.scripts'],
12+
entry_points={'console_scripts': ['pyradiomics=radiomics.scripts.commandline:main',
13+
'pyradiomicsbatch=radiomics.scripts.commandlinebatch:main']},
1214
data_files=[
13-
('data', ['data/paramSchema.yaml', 'data/schemaFuncs.py', 'data/brain1_image.nrrd', 'data/brain1_label.nrrd'])],
15+
('data', ['data/paramSchema.yaml', 'data/schemaFuncs.py'])],
1416
description='Radiomics features library for python',
1517
url='http://github.com/Radiomics/pyradiomics',
1618
author='pyradiomics community',

0 commit comments

Comments
 (0)