Skip to content

Commit e9672a8

Browse files
committed
style(python): run black on itkwasm
1 parent ea18da8 commit e9672a8

40 files changed

+643
-386
lines changed

packages/core/python/itkwasm/itkwasm/__init__.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,41 @@
2121
from .environment_dispatch import environment_dispatch, function_factory
2222
from .cast_image import cast_image
2323
from .image_from_array import image_from_array
24-
from .to_numpy_array import array_like_to_numpy_array, array_like_to_bytes, buffer_to_numpy_array
24+
from .to_numpy_array import (
25+
array_like_to_numpy_array,
26+
array_like_to_bytes,
27+
buffer_to_numpy_array,
28+
)
2529
from .to_cupy_array import array_like_to_cupy_array
2630

2731
__all__ = [
28-
"InterfaceTypes",
29-
"Pipeline",
30-
"PipelineInput",
31-
"PipelineOutput",
32-
"Image",
33-
"ImageType",
34-
"ImageRegion",
35-
"PointSet",
36-
"PointSetType",
37-
"Mesh",
38-
"MeshType",
39-
"PolyData",
40-
"PolyDataType",
41-
"BinaryFile",
42-
"BinaryStream",
43-
"TextFile",
44-
"TextStream",
45-
"JsonCompatible",
46-
"FloatTypes",
47-
"IntTypes",
48-
"PixelTypes",
49-
"environment_dispatch",
50-
"function_factory",
51-
"cast_image",
52-
"image_from_array",
53-
"array_like_to_numpy_array",
54-
"array_like_to_bytes",
55-
"array_like_to_cupy_array",
56-
"buffer_to_numpy_array",
57-
]
32+
"InterfaceTypes",
33+
"Pipeline",
34+
"PipelineInput",
35+
"PipelineOutput",
36+
"Image",
37+
"ImageType",
38+
"ImageRegion",
39+
"PointSet",
40+
"PointSetType",
41+
"Mesh",
42+
"MeshType",
43+
"PolyData",
44+
"PolyDataType",
45+
"BinaryFile",
46+
"BinaryStream",
47+
"TextFile",
48+
"TextStream",
49+
"JsonCompatible",
50+
"FloatTypes",
51+
"IntTypes",
52+
"PixelTypes",
53+
"environment_dispatch",
54+
"function_factory",
55+
"cast_image",
56+
"image_from_array",
57+
"array_like_to_numpy_array",
58+
"array_like_to_bytes",
59+
"array_like_to_cupy_array",
60+
"buffer_to_numpy_array",
61+
]
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from dataclasses import dataclass
22
from pathlib import PurePosixPath
33

4+
45
@dataclass
56
class BinaryFile:
6-
path: PurePosixPath
7+
path: PurePosixPath
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from dataclasses import dataclass
22

3+
34
@dataclass
45
class BinaryStream:
5-
data: bytes
6+
data: bytes

packages/core/python/itkwasm/itkwasm/cast_image.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
from .int_types import IntTypes
1010
from .float_types import FloatTypes
1111

12-
def cast_image(input_image: Image,
13-
pixel_type: Optional[PixelTypes]=None,
14-
component_type: Optional[Union[IntTypes, FloatTypes]]=None) -> Image:
12+
13+
def cast_image(
14+
input_image: Image,
15+
pixel_type: Optional[PixelTypes] = None,
16+
component_type: Optional[Union[IntTypes, FloatTypes]] = None,
17+
) -> Image:
1518
"""Cast an image to another pixel type and / or component type.
1619
1720
:param input_image: Image to be cast.
@@ -72,6 +75,6 @@ def cast_image(input_image: Image,
7275
elif component_type == FloatTypes.Float64:
7376
output_image.data = input_image.data.astype(np.float64)
7477
else:
75-
raise ValueError('Unsupported component type')
78+
raise ValueError("Unsupported component type")
7679

7780
return output_image

packages/core/python/itkwasm/itkwasm/environment_dispatch.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
import sys
33
import importlib
44
import sys
5+
56
if sys.version_info < (3, 10):
67
from importlib_metadata import entry_points
78
else:
89
from importlib.metadata import entry_points
910

11+
1012
class FunctionFactory:
1113
def __init__(self):
1214
self._registered: Dict[Tuple[str, str], Set[Callable]] = {}
1315
self._priorities: Dict[Callable, int] = {}
1416
self._has_entry_point_lookup: Set[Tuple[str, str]] = set()
1517

16-
def register(self, interface_package: str, func_name: str, func: Callable, priority: int=1)-> None:
18+
def register(self, interface_package: str, func_name: str, func: Callable, priority: int = 1) -> None:
1719
key = (interface_package, func_name)
1820
registered = self._registered.get(key, set())
1921
registered.add(func)
@@ -23,9 +25,9 @@ def register(self, interface_package: str, func_name: str, func: Callable, prior
2325
def lookup(self, interface_package: str, func_name: str) -> Optional[Set[Callable]]:
2426
key = (interface_package, func_name)
2527
if not key in self._has_entry_point_lookup:
26-
discovered_funcs = entry_points(group=f'{interface_package}.{func_name}')
28+
discovered_funcs = entry_points(group=f"{interface_package}.{func_name}")
2729
for ep in discovered_funcs:
28-
priority = ep.name.partition('.priority.')[2]
30+
priority = ep.name.partition(".priority.")[2]
2931
if priority:
3032
priority = int(priority)
3133
else:
@@ -40,17 +42,20 @@ def highest_priority(self, interface_package: str, func_name: str) -> Optional[C
4042
registered = self.lookup(interface_package, func_name)
4143
if registered is None:
4244
return None
43-
highest = max(self._registered[(interface_package, func_name)], key=lambda x: self._priorities[x])
45+
highest = max(
46+
self._registered[(interface_package, func_name)],
47+
key=lambda x: self._priorities[x],
48+
)
4449
if self._priorities[highest] < 1:
4550
return None
4651
return highest
4752

48-
def set_priority(self, func: Callable, priority: int)-> None:
53+
def set_priority(self, func: Callable, priority: int) -> None:
4954
if func not in self._priorities:
5055
raise ValueError(f"Function {func} has not been registered")
5156
self._priorities[func] = priority
5257

53-
def get_priority(self, func: Callable)-> int:
58+
def get_priority(self, func: Callable) -> int:
5459
if func not in self._priorities:
5560
raise ValueError(f"Function {func} has not been registered")
5661
return self._priorities[func]
@@ -61,22 +66,24 @@ def disable(self, interface_package: str, func_name: str):
6166
for func in registered:
6267
self._priorities[func] = -1
6368

69+
6470
function_factory = FunctionFactory()
6571

72+
6673
def environment_dispatch(interface_package: str, func_name: str) -> Callable:
6774
factory_func = function_factory.highest_priority(interface_package, func_name)
6875
if factory_func is not None:
6976
return factory_func
7077

7178
if sys.platform != "emscripten":
72-
if func_name.endswith('_async'):
73-
raise ValueError('async function are only implemented for emscripten')
79+
if func_name.endswith("_async"):
80+
raise ValueError("async function are only implemented for emscripten")
7481
package = f"{interface_package}_wasi"
7582
else:
76-
if not func_name.endswith('_async'):
77-
raise ValueError('emscripten only implements the _async version of this function')
83+
if not func_name.endswith("_async"):
84+
raise ValueError("emscripten only implements the _async version of this function")
7885
package = f"{interface_package}_emscripten"
7986
mod = importlib.import_module(package)
8087
func = getattr(mod, func_name)
8188

82-
return func
89+
return func
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from enum import Enum
22

3+
34
class FloatTypes(str, Enum):
4-
Float32 = 'float32'
5-
Float64 = 'float64'
6-
SpacePrecisionType = 'float64'
5+
Float32 = "float32"
6+
Float64 = "float64"
7+
SpacePrecisionType = "float64"
78

89
def __str__(self):
910
return str(self.value)
1011

1112
def __repr__(self):
12-
return f'"{str(self.value)}"'
13+
return f'"{str(self.value)}"'

packages/core/python/itkwasm/itkwasm/image.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,29 @@
1212
from .float_types import FloatTypes
1313
from .pixel_types import PixelTypes
1414

15+
1516
@dataclass
1617
class ImageType:
1718
dimension: int = 2
1819
componentType: Union[IntTypes, FloatTypes] = IntTypes.UInt8
1920
pixelType: PixelTypes = PixelTypes.Scalar
2021
components: int = 1
2122

23+
2224
def _default_direction() -> ArrayLike:
2325
return np.empty((0,), np.float64)
2426

27+
2528
@dataclass
2629
class ImageRegion:
2730
index: Sequence[int] = field(default_factory=list)
2831
size: Sequence[int] = field(default_factory=list)
2932

33+
3034
@dataclass
3135
class Image:
3236
imageType: Union[ImageType, Dict] = field(default_factory=ImageType)
33-
name: str = 'Image'
37+
name: str = "Image"
3438
origin: Sequence[float] = field(default_factory=list)
3539
spacing: Sequence[float] = field(default_factory=list)
3640
direction: ArrayLike = field(default_factory=_default_direction)
@@ -45,16 +49,28 @@ def __post_init__(self):
4549

4650
dimension = self.imageType.dimension
4751
if len(self.origin) == 0:
48-
self.origin += [0.0,] * dimension
52+
self.origin += [
53+
0.0,
54+
] * dimension
4955

5056
if len(self.spacing) == 0:
51-
self.spacing += [1.0,] * dimension
57+
self.spacing += [
58+
1.0,
59+
] * dimension
5260

5361
if len(self.direction) == 0:
5462
self.direction = np.eye(dimension).astype(np.float64)
5563

5664
if len(self.size) == 0:
57-
self.size += [1,] * dimension
65+
self.size += [
66+
1,
67+
] * dimension
5868

5969
if self.bufferedRegion is None:
60-
self.bufferedRegion = ImageRegion(index=[0,]*dimension, size=self.size)
70+
self.bufferedRegion = ImageRegion(
71+
index=[
72+
0,
73+
]
74+
* dimension,
75+
size=self.size,
76+
)

packages/core/python/itkwasm/itkwasm/image_from_array.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Optional
2+
23
try:
34
from numpy.typing import ArrayLike
45
except ImportError:
@@ -9,6 +10,7 @@
910

1011
from .pixel_types import PixelTypes
1112

13+
1214
def image_from_array(arr, is_vector: bool = False, image_type: Optional[ImageType] = None) -> Image:
1315
"""Convert a numpy array-like to an itkwasm Image.
1416
@@ -33,7 +35,7 @@ def image_from_array(arr, is_vector: bool = False, image_type: Optional[ImageTyp
3335
image_type = ImageType(
3436
dimension=dimension,
3537
componentType=_dtype_to_component_type(arr.dtype),
36-
pixelType=PixelTypes.Scalar if not is_vector else PixelTypes.VariableLengthVector,
38+
pixelType=(PixelTypes.Scalar if not is_vector else PixelTypes.VariableLengthVector),
3739
components=arr.shape[-1] if is_vector else 1,
3840
)
3941

packages/core/python/itkwasm/itkwasm/int_types.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
from enum import Enum
22

3+
34
class IntTypes(str, Enum):
4-
Int8 = 'int8'
5-
UInt8 = 'uint8'
6-
Int16 = 'int16'
7-
UInt16 = 'uint16'
8-
Int32 = 'int32'
9-
UInt32 = 'uint32'
10-
Int64 = 'int64'
11-
UInt64 = 'uint64'
5+
Int8 = "int8"
6+
UInt8 = "uint8"
7+
Int16 = "int16"
8+
UInt16 = "uint16"
9+
Int32 = "int32"
10+
UInt32 = "uint32"
11+
Int64 = "int64"
12+
UInt64 = "uint64"
1213

13-
SizeValueType = 'uint64'
14-
IdentifierType = 'uint64'
15-
IndexValueType = 'int64'
16-
OffsetValueType = 'int64'
14+
SizeValueType = "uint64"
15+
IdentifierType = "uint64"
16+
IndexValueType = "int64"
17+
OffsetValueType = "int64"
1718

1819
def __str__(self):
1920
return str(self.value)
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from enum import Enum
22

3+
34
class InterfaceTypes(Enum):
4-
TextFile = 'InterfaceTextFile'
5-
BinaryFile = 'InterfaceBinaryFile'
6-
TextStream = 'InterfaceTextStream'
7-
BinaryStream = 'InterfaceBinaryStream'
8-
Image = 'InterfaceImage'
9-
Mesh = 'InterfaceMesh'
10-
PolyData = 'InterfacePolyData'
11-
JsonCompatible = 'InterfaceJsonCompatible'
5+
TextFile = "InterfaceTextFile"
6+
BinaryFile = "InterfaceBinaryFile"
7+
TextStream = "InterfaceTextStream"
8+
BinaryStream = "InterfaceBinaryStream"
9+
Image = "InterfaceImage"
10+
Mesh = "InterfaceMesh"
11+
PolyData = "InterfacePolyData"
12+
JsonCompatible = "InterfaceJsonCompatible"

0 commit comments

Comments
 (0)