Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C matrices #158

Closed
wants to merge 31 commits into from
Closed

C matrices #158

wants to merge 31 commits into from

Conversation

JoostJM
Copy link
Collaborator

@JoostJM JoostJM commented Nov 16, 2016

Implement C for calculation of matrices
Currently supports:

  • GLCM
  • GLSZM

This greatly improves processing speed, while still keeping an all-python option available (currently enabled by setting radiomics.debugging to True.

Also included automatic linking and compiling to setup script. Needs additional package (cython) to work, but is now also included in setup script (if not installed, setup script installs it without error).

Lastly, added a new test, where the python generated matrices are compared to the C generated matrices for all test cases. No features are calculated here, but matrices are assessed element wise (maximum difference for any element is 1e-3, this is due to small machine precision errors possible in NGTDM).

@JoostJM JoostJM force-pushed the c-matrices branch 3 times, most recently from 33628e6 to f06982c Compare November 16, 2016 02:52
@JoostJM
Copy link
Collaborator Author

JoostJM commented Nov 16, 2016

Snapshot from profiling, C functions are highlighted:

snapshot c matrices

@JoostJM
Copy link
Collaborator Author

JoostJM commented Nov 22, 2016

Finished implementing time intensive features in C. Calculation time at profile now just over 6 seconds for 5 testcases. Still writing some docstrings.
Calculations now in C:

  • GLCM
  • GLDM (EXPERIMENTAL, REMOVED)
  • NGTDM (EXPERIMENTAL, REMOVED)
  • GLSZM
  • GLRLM
  • SurfaceArea

When GLDZM (EXPERIMENTAL, REMOVED) is merged into the master, I can also write a C function for this. This is very simple, as it would be identical to the one calculating the GLSZM, but instead of counting the processed voxels, it records the minimum distance in the zone.

@fedorov
Copy link
Collaborator

fedorov commented Nov 23, 2016

When I do python setup.py install on mac with this branch, I am getting the error below. Is there anything special/extra that I need to do?

@JoostJM did you set up a paid account on TravisCI to proceed with CI testing on Mac? @naucoin could help setting it up (see #126) once we have a paid account (or open source version of pyradiomics).

$ python setup.py install
/var/folders/yk/3nc16c9d241_ds1vfkdh1km80000gn/T/easy_install-64trmx/numpy-1.12.0b1/setup.py:366: UserWarning: Unrecognized setuptools command, proceeding with generating Cython sources and expanding templates
  run_build = parse_setuppy_commands()
Traceback (most recent call last):
  File "setup.py", line 28, in <module>
    zip_safe=False)
  File "/Users/fedorov/anaconda/lib/python2.7/distutils/core.py", line 111, in setup
    _setup_distribution = dist = klass(attrs)
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/dist.py", line 269, in __init__
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/dist.py", line 313, in fetch_build_eggs
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/pkg_resources/__init__.py", line 827, in resolve
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/pkg_resources/__init__.py", line 1072, in best_match
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/pkg_resources/__init__.py", line 1084, in obtain
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/dist.py", line 380, in fetch_build_egg
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/command/easy_install.py", line 640, in easy_install
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/command/easy_install.py", line 670, in install_item
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/command/easy_install.py", line 853, in install_eggs
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/command/easy_install.py", line 1081, in build_and_install
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/command/easy_install.py", line 1067, in run_setup
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/sandbox.py", line 246, in run_setup
  File "/Users/fedorov/anaconda/lib/python2.7/contextlib.py", line 35, in __exit__
    self.gen.throw(type, value, traceback)
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/sandbox.py", line 195, in setup_context
  File "/Users/fedorov/anaconda/lib/python2.7/contextlib.py", line 35, in __exit__
    self.gen.throw(type, value, traceback)
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/sandbox.py", line 166, in save_modules
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/sandbox.py", line 141, in resume
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/sandbox.py", line 154, in save_modules
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/sandbox.py", line 195, in setup_context
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/sandbox.py", line 243, in run_setup
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/sandbox.py", line 273, in run
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/sandbox.py", line 242, in runner
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/setuptools/sandbox.py", line 46, in _execfile
  File "/var/folders/yk/3nc16c9d241_ds1vfkdh1km80000gn/T/easy_install-64trmx/numpy-1.12.0b1/setup.py", line 391, in <module>

  File "/var/folders/yk/3nc16c9d241_ds1vfkdh1km80000gn/T/easy_install-64trmx/numpy-1.12.0b1/setup.py", line 383, in setup_package

  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/numpy/distutils/core.py", line 135, in setup
    config = configuration()
  File "/var/folders/yk/3nc16c9d241_ds1vfkdh1km80000gn/T/easy_install-64trmx/numpy-1.12.0b1/setup.py", line 166, in configuration

  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/numpy/distutils/misc_util.py", line 1002, in add_subpackage
    caller_level = 2)
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/numpy/distutils/misc_util.py", line 971, in get_subpackage
    caller_level = caller_level + 1)
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/numpy/distutils/misc_util.py", line 908, in _get_configuration_from_setup_py
    config = setup_module.configuration(*args)
  File "numpy/setup.py", line 10, in configuration
    packages=['radiomics'],
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/numpy/distutils/misc_util.py", line 1002, in add_subpackage
    caller_level = 2)
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/numpy/distutils/misc_util.py", line 971, in get_subpackage
    caller_level = caller_level + 1)
  File "/Users/fedorov/anaconda/lib/python2.7/site-packages/numpy/distutils/misc_util.py", line 883, in _get_configuration_from_setup_py
    ('.py', 'U', 1))
  File "/private/var/folders/yk/3nc16c9d241_ds1vfkdh1km80000gn/T/easy_install-64trmx/numpy-1.12.0b1/numpy/core/setup.py", line 12, in <module>
    install_requires=['cython',
ImportError: No module named _build_utils.apple_accelerate

@JoostJM
Copy link
Collaborator Author

JoostJM commented Nov 23, 2016

@jcfr, Can you resubmit your changes? I'm afraid I accidentally deleted them when I rebased this branch on the master, to incorporate the changes from the other PR's we just merged.

@JoostJM
Copy link
Collaborator Author

JoostJM commented Nov 23, 2016

@fedorov as to your error, did you have numpy and cython installed prior to running this script?

@jcfr
Copy link
Collaborator

jcfr commented Nov 23, 2016

Sure. Will do after lunch.

On Nov 23, 2016 1:17 PM, "Joost van Griethuysen" notifications@github.com
wrote:

@jcfr https://github.com/jcfr, Can you resubmit your changes? I'm
afraid I accidentally deleted them when I rebased this branch on the
master, to incorporate the changes from the other PR's we just merged.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#158 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AANXo3BRVwPw9amLYyGaeBd4Ryb94NWFks5rBIMegaJpZM4KzSOX
.

@jcfr
Copy link
Collaborator

jcfr commented Nov 23, 2016

@JoostJM @pieper @fedorov Here it is. Let me know what you think.

As a side note, @isuruf is helping to add scikit-build into conda forge. See conda-forge/staged-recipes#1989

@JoostJM
Copy link
Collaborator Author

JoostJM commented Nov 23, 2016

@jcfr My build is failing as I do not have CMake currently installed on my system. So this means your update needs the user to have CMake installed, as well as the extra dependencies. If we're okay with, it's fine with me.

@fedorov
Copy link
Collaborator

fedorov commented Nov 23, 2016

I am not worried about CMake as a dependency. I am worried about maintaining CMakeLists.txt files. But this might well be a necessary price to pay if you decide to use C.

@jcfr
Copy link
Collaborator

jcfr commented Nov 23, 2016

@JoostJM The following

pip install scikit-build cmake

should do it.

There is also more details in the commit message.

@JoostJM
Copy link
Collaborator Author

JoostJM commented Nov 23, 2016

@jcfr No, I already did that, I ran your line again and it just says requirements satisfied

@JoostJM
Copy link
Collaborator Author

JoostJM commented Nov 23, 2016

image

@fedorov
Copy link
Collaborator

fedorov commented Nov 23, 2016

I recall from somewhere that using MinGW is a bad idea ... welcome to the C world!

@jcfr
Copy link
Collaborator

jcfr commented Nov 23, 2016

@JoostJM

Do you have Visual Studio 2008 installed ? This is the official compiler for python 2.7

I have to head out and will look at it later. And also improve the documentation of scikit-build for alternative and how to use them. Example: https://www.microsoft.com/en-us/download/details.aspx?id=44266

((The issue with building from the Git shell is that there is a mingw compiler also installed ...))

@JoostJM
Copy link
Collaborator Author

JoostJM commented Nov 23, 2016

I have Visual Studio 2015, but my environmentvariables are set so that VS90COMNTOOLS points to this newer compiler, so without using cmake, my python usually just finds my compiler

@jcfr
Copy link
Collaborator

jcfr commented Nov 23, 2016

python setup.py build -G "Visual Studio 14 2015 Win64"

or

python setup.py bdist -G "Visual Studio 14 2015 Win64"

or

python setup.py bdist_wheel -G "Visual Studio 14 2015 Win64"

More details here: http://scikit-build.readthedocs.io/en/latest/usage.html#command-line-options

I will look into python setup.py install -G "Visual Studio 14 2015 Win64"

JoostJM and others added 9 commits December 21, 2016 23:02
Package is now built into `_skbuild`, update gitignore to ignore this folder.
Fix potential index out of range error in `fill_glszm` in tempData. This occurs when the entire tempData matrix is filled (Ns different regions. When the function fill_glszm then runs, it does not exit the while loop (as it encounters no -1 at the end of the matrix).
Specify the size of the tempData matrix = (Ns + 1) * 2, so that the last element will always be -1 (max idx computed by function calculate_glszm is still Ns * 2).

Additionally re-apply code style to C files
This approach allows to support use of radiomics package after installing
pyradiomics as a regular binary distribution
@JoostJM
Copy link
Collaborator Author

JoostJM commented Dec 21, 2016

Rebased this branch onto master after merging #173. As there is code changed in the removed classes by this branch, I stored a copy of the not-rebased branch here.

Applied the update where experimental/unstable classes are removed from the C-implementation and tests.

@jcfr
Copy link
Collaborator

jcfr commented Dec 22, 2016

Remove C code for calculating the matrix in these classes.
Remove the tests for these classes from test_cmatrices
Commit 	7da5b76 caused the dynamic lookup of feature classes in `featureextractor`  to break.
Fix the function and move this lookup functionality to `__init__.py`, as it is used more extensively throughout the package.
@JoostJM JoostJM force-pushed the c-matrices branch 2 times, most recently from 0fc72a0 to 81638df Compare December 23, 2016 09:19
jcfr added a commit that referenced this pull request Jan 18, 2017
This should be re-enabled after integrating #158
@JoostJM
Copy link
Collaborator Author

JoostJM commented Jan 19, 2017

@jcfr I updated my scikit-build and cmake on my computer at work, but I am still getting errors. It's an unresolved external reference error in the load-function of numpy (only fails when compiling with cmake though). Here is a section the setup output, do you know whats going wrong?

image

jcfr added a commit that referenced this pull request Feb 13, 2017
This should be re-enabled after integrating #158
@jcfr
Copy link
Collaborator

jcfr commented Feb 14, 2017

@JoostJM Note that I started to rebase and re-organized commits. I will follow up with some update.

Copy link
Collaborator

@jcfr jcfr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR will be re-organized and rebased on top of the #196 set of changes

@jcfr
Copy link
Collaborator

jcfr commented Feb 15, 2017

@JoostJM Here is what I have so far: https://github.com/Radiomics/pyradiomics/tree/c-matrices-rebased-jcfr

I squashed together changes that made sense. Still need some work.

Would be nice to integrate separately all changes that are not specific to C implementation.

For example: f6b77ed

@jcfr
Copy link
Collaborator

jcfr commented Feb 15, 2017

Closing. This is superseded by #200

@jcfr jcfr closed this Feb 15, 2017
@JoostJM JoostJM added this to Rejected PRs in PyRadiomics Performance Feb 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PyRadiomics Performance
  
Rejected PRs
Development

Successfully merging this pull request may close these issues.

None yet

5 participants