Forked from emmanuelle/skimage_3d_compatibility.py
Last active
April 16, 2019 12:47
-
-
Save GenevieveBuckley/bda7ab23b18a7672a4cc8364b68aa1c1 to your computer and use it in GitHub Desktop.
Inspect which functions of scikit-image are compatible with 3-D arrays.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import inspect | |
from skimage import exposure, feature, filters, measure, morphology, \ | |
restoration, segmentation, transform, util | |
def only_one_nondefault(args): | |
""" | |
Returns True if the function has only one non-keyword parameter, | |
False otherwise. | |
""" | |
defaults = 0 if args.defaults is None else len(args.defaults) | |
if len(args.args) >= 1 and (len(args.args) - defaults <= 1): | |
return True | |
else: | |
return False | |
test_3d_float = np.ones((20, 20, 10)) | |
test_3d_float[:5, :5, :5] = 0. | |
test_3d_int = np.ones((20, 20, 10), dtype=np.uint8) | |
test_3d_int[:5, :5, :5] = 0 | |
compatible_3d = [] | |
not_3d = [] | |
several_arguments = [] | |
functions = [] | |
for submodule in [exposure, feature, filters, measure, morphology, | |
restoration, segmentation, transform, util]: | |
functions += inspect.getmembers(submodule, inspect.isfunction) | |
for function in functions: | |
args = inspect.getfullargspec(function[1]) | |
only_one_argument = only_one_nondefault(args) | |
if only_one_argument: | |
try: | |
function[1](test_3d_float) | |
compatible_3d.append(function[0]) | |
except ValueError: | |
not_3d.append(function[0]) | |
except TypeError: | |
try: | |
function[1](test_3d_int) | |
compatible_3d.append(function[0]) | |
except ValueError: | |
not_3d.append(function[0]) | |
except: | |
compatible_3d.append(function[0]) | |
except: | |
compatible_3d.append(function[0]) | |
else: | |
several_arguments.append(function[0]) | |
print("total number of functions: ", len(functions)) | |
print("number of functions with several arguments: ", len(several_arguments)) | |
print("number of functions compatible with 3-D arrays: ", len(compatible_3d)) | |
print("number of functions not compatible with 3-D arrays: ", len(not_3d)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment