Skip to content

Commit

Permalink
STYLE: Apply black Python formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Leengit authored and dzenanz committed Feb 4, 2022
1 parent 701c8c2 commit 3291538
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 147 deletions.
5 changes: 1 addition & 4 deletions Examples/RegistrationITKv4/ImageRegistration5.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@
)
print(
"Translation: %f, %f"
% (
initialParameters.GetElement(3),
initialParameters.GetElement(4),
)
% (initialParameters.GetElement(3), initialParameters.GetElement(4))
)


Expand Down
14 changes: 7 additions & 7 deletions Modules/Core/Mesh/wrapping/test/itkMeshArrayPixelTypeTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

# Create Vector Container and Store values in it for each Point
# For windows use itk.ULL
if hasattr(itk.VectorContainer, 'ULAD'):
if hasattr(itk.VectorContainer, "ULAD"):
IdentifierType = itk.UL
else:
IdentifierType = itk.ULL
Expand All @@ -41,16 +41,16 @@
pixel_data_reference.SetSize(PixelDataSize)
pixel_data_reference.Fill(0)
pixel_data_reference[0] = i
pixel_data_reference[4] = i+4
pixel_data_reference[4] = i + 4

# Set the point data container
mesh.SetPointData(v)

assert mesh.GetPointData().Size() == NumberOfPoints
assert mesh.GetPointData().ElementAt(0)[0] == 0
assert mesh.GetPointData().ElementAt(0)[4] == 4
assert mesh.GetPointData().ElementAt(2)[0] == 2+0
assert mesh.GetPointData().ElementAt(2)[4] == 2+4
assert mesh.GetPointData().ElementAt(2)[0] == 2 + 0
assert mesh.GetPointData().ElementAt(2)[4] == 2 + 4

# resize the PixelDataSize to see if it can be altered succesfully
PixelDataSize = 10
Expand All @@ -59,10 +59,10 @@
pixel_data_reference.SetSize(PixelDataSize)
pixel_data_reference.Fill(0)
pixel_data_reference[0] = i
pixel_data_reference[9] = i+10
pixel_data_reference[9] = i + 10

assert mesh.GetPointData().Size() == NumberOfPoints
assert mesh.GetPointData().ElementAt(0)[0] == 0
assert mesh.GetPointData().ElementAt(0)[9] == 10
assert mesh.GetPointData().ElementAt(2)[0] == 2+0
assert mesh.GetPointData().ElementAt(2)[9] == 2+10
assert mesh.GetPointData().ElementAt(2)[0] == 2 + 0
assert mesh.GetPointData().ElementAt(2)[9] == 2 + 10
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,7 @@

resample = itk.ResampleImageFilter[ImageType, ImageType].New()
interpolator = itk.WindowedSincInterpolateImageFunction[
ImageType,
3,
itk.HammingWindowFunction[
3,
],
ImageType, 3, itk.HammingWindowFunction[3,]
].New()
resample.SetInterpolator(interpolator)

Expand Down
5 changes: 3 additions & 2 deletions Utilities/Doxygen/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

# Writes the date and time in RFC 2822 format to stdout

import time
import time
import sys
sys.stdout.write(time.strftime('%a, %d %b %Y %H:%M:%S +0000', time.gmtime()))

sys.stdout.write(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()))
8 changes: 4 additions & 4 deletions Utilities/Maintenance/BuildHeaderTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
"itkBSplineDeformableTransform.h", # deprecated
"vtkCaptureScreen.h", # these includes require VTK
"itkMultiThreader.h", # Compatibility file, it should not be used
"itkEnableIf.h", # Compatibility file, it should not be used
"itkIsSame.h", # Compatibility file, it should not be used
"itkIsBaseOf.h", # Compatibility file, it should not be used
"itkIsConvertible.h", # Compatibility file, it should not be used
"itkEnableIf.h", # Compatibility file, it should not be used
"itkIsSame.h", # Compatibility file, it should not be used
"itkIsBaseOf.h", # Compatibility file, it should not be used
"itkIsConvertible.h", # Compatibility file, it should not be used
"itkViewImage.h", # Depends on VTK_RENDERING_BACKEND
"QuickView.h", # Depends on VTK_RENDERING_BACKEND
"itkBSplineDeformableTransformInitializer.h",
Expand Down
17 changes: 13 additions & 4 deletions Wrapping/Generators/Python/Tests/PyImageFilterTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,26 @@
import numpy as np
import functools

input_image = itk.image_from_array(np.random.randint(0,255,size=(6,6), dtype=np.uint8))
input_image = itk.image_from_array(
np.random.randint(0, 255, size=(6, 6), dtype=np.uint8)
)


def constant_output(py_image_filter, constant=42):
output = py_image_filter.GetOutput()
output.SetBufferedRegion(output.GetRequestedRegion())
output.Allocate()
output.FillBuffer(constant)


def constant_wrapper(f, constant=42):
@functools.wraps(f)
def wrapper(*args):
return f(*args, constant=constant)

return wrapper


py_filter = itk.PyImageFilter.New(input_image)
py_filter.SetPyGenerateData(constant_output)
py_filter.Update()
Expand All @@ -47,8 +53,9 @@ def wrapper(*args):
assert np.all(np.asarray(output_image) == 10)

# Functional interface
output_image = itk.py_image_filter(input_image,
py_generate_data=constant_wrapper(constant_output, 7))
output_image = itk.py_image_filter(
input_image, py_generate_data=constant_wrapper(constant_output, 7)
)
assert np.all(np.asarray(output_image) == 7)

# PyGenerateInputRequestedRegion
Expand All @@ -58,6 +65,7 @@ class CheckCalled(object):
def __call__(self, py_image_filter):
self.called = True


check_called = CheckCalled()
assert check_called.called == False

Expand All @@ -79,9 +87,10 @@ def __call__(self, py_image_filter):
def enlarge_output_requested_region(py_image_filter, data):
data.SetRequestedRegionToLargestPossibleRegion()


py_filter = itk.PyImageFilter.New(input_image)
py_filter.SetPyGenerateData(constant_output)
requested_region = itk.ImageRegion[2]([2,2], [3,3])
requested_region = itk.ImageRegion[2]([2, 2], [3, 3])
py_filter.GetOutput().SetRequestedRegion(requested_region)
py_filter.Update()
region = py_filter.GetOutput().GetRequestedRegion()
Expand Down
8 changes: 1 addition & 7 deletions Wrapping/Generators/Python/Tests/PythonTypeTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,7 @@ def create_and_test(t, create_method):
except Exception as e:
msg = (
"%s, %s: wrong Python class name: %s. "
"Exception while evaluating it: %s"
% (
t,
actual_type,
obj_type,
e,
)
"Exception while evaluating it: %s" % (t, actual_type, obj_type, e)
)
wrongType = 1
if wrongType:
Expand Down
10 changes: 7 additions & 3 deletions Wrapping/Generators/Python/Tests/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ def custom_callback(name, progress):
image.SetDirection(rotation)
serialize_deserialize = pickle.loads(pickle.dumps(image))
# verify_input_information checks origin, spacing, direction consistency
comparison = itk.comparison_image_filter(image, serialize_deserialize, verify_input_information=True)
comparison = itk.comparison_image_filter(
image, serialize_deserialize, verify_input_information=True
)
assert np.sum(comparison) == 0.0

# Make sure we can read unsigned short, unsigned int, and cast
Expand Down Expand Up @@ -416,11 +418,13 @@ def custom_callback(name, progress):
# and make sure that the matrix hasn't changed.
assert m_itk(0, 0) == 1
# Test __repr__
assert repr(m_itk) == "itkMatrixD33 ([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])"
assert (
repr(m_itk) == "itkMatrixD33 ([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])"
)
# Test __array__
assert np.array_equal(np.asarray(m_itk), np.eye(3))
# Test array like input
arr3 = [[0., 0.], [0., 0.]]
arr3 = [[0.0, 0.0], [0.0, 0.0]]
m_itk = itk.matrix_from_array(arr3)
m_itk.SetIdentity()
# Test that the array like has not changed,...
Expand Down
9 changes: 7 additions & 2 deletions Wrapping/Generators/Python/itk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,22 @@ def _dedup(seq):
# Check if the module installed its own init file and load it.
# ITK Modules __init__.py must be renamed to __init_{module_name}__.py before packaging
# the wheel to avoid overriding this file on installation.
module_init_file = os.path.join(os.path.dirname(__file__), "__init_" + module.lower() + "__.py")
module_init_file = os.path.join(
os.path.dirname(__file__), "__init_" + module.lower() + "__.py"
)
if not os.path.isfile(module_init_file):
continue

# Load the module definition from file path
spec = importlib.util.spec_from_file_location(f"{module}.__init__", module_init_file)
spec = importlib.util.spec_from_file_location(
f"{module}.__init__", module_init_file
)

# Import and execute the __init__ file (this call will add the module binaries to sys path)
loaded_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(loaded_module)


# After 'lifting' external symbols into this itk namespace,
# Now do the initialization, and conversion to LazyLoading if necessary
_initialize_module()
4 changes: 1 addition & 3 deletions Wrapping/Generators/Python/itk/support/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ def itk_load_swig_module(name: str, namespace=None):
template_feature.get_cpp_class_name(), swig_class
)
setattr(
this_module,
template_feature.get_python_class_name(),
swig_class,
this_module, template_feature.get_python_class_name(), swig_class
)
if namespace is not None:
current_value = namespace.get(
Expand Down
14 changes: 7 additions & 7 deletions Wrapping/Generators/Python/itk/support/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def vector_container_from_array(arr: ArrayLike, ttype=None) -> "itkt.VectorConta
arr = np.asarray(arr)

# Return VectorContainer with 64-bit index type
if os.name == 'nt':
if os.name == "nt":
IndexType = itk.ULL
else:
IndexType = itk.UL
Expand Down Expand Up @@ -825,19 +825,19 @@ def dict_from_image(image: "itkt.Image") -> Dict:
spacing=tuple(image.GetSpacing()),
size=tuple(image.GetBufferedRegion().GetSize()),
direction=np.asarray(image.GetDirection()),
data=pixel_arr
data=pixel_arr,
)


def image_from_dict(image_dict: Dict) -> "itkt.Image":
"""Deserialize an dictionary representing an itk.Image object."""
import itk

ImageType = image_type_from_wasm_type(image_dict['imageType'])
image = itk.PyBuffer[ImageType].GetImageViewFromArray(image_dict['data'])
image.SetOrigin(image_dict['origin'])
image.SetSpacing(image_dict['spacing'])
image.SetDirection(image_dict['direction'])
ImageType = image_type_from_wasm_type(image_dict["imageType"])
image = itk.PyBuffer[ImageType].GetImageViewFromArray(image_dict["data"])
image.SetOrigin(image_dict["origin"])
image.SetSpacing(image_dict["spacing"])
image.SetDirection(image_dict["direction"])
return image


Expand Down
Loading

0 comments on commit 3291538

Please sign in to comment.