Skip to content

Commit

Permalink
Fix ruff and mypy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nx10 committed May 21, 2024
1 parent dbfe14e commit eabbee3
Show file tree
Hide file tree
Showing 19 changed files with 566 additions and 384 deletions.
38 changes: 19 additions & 19 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ target-version = "py311"
select = ["ANN", "D", "E", "F", "I"]
ignore = [
"ANN101", # self should not be annotated.
"ANN102" # cls should not be annotated.
"ANN102", # cls should not be annotated.
]
fixable = ["ALL"]
unfixable = []
Expand Down
41 changes: 37 additions & 4 deletions src/fineslice/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
"""fineslice."""

from .affine import affine_invert, affine_identity, affine_translate, affine_scale, affine_shear, affine_rotate, \
affine_rotate_degrees
from .bounds import bounds_cube, bounds_where, bounds_manual
from .affine import (
affine_identity,
affine_invert,
affine_rotate,
affine_rotate_degrees,
affine_scale,
affine_shear,
affine_translate,
)
from .bounds import bounds_cube, bounds_manual, bounds_where
from .sampler_0d import sample_0d
from .sampler_1d import sample_1d
from .sampler_2d import sample_2d
from .sampler_3d import sample_3d
from .types import sampler_point_0d, sampler_point_1d, sampler_point_2d, sampler_point_3d
from .types import (
sampler_point_0d,
sampler_point_1d,
sampler_point_2d,
sampler_point_3d,
)

__all__ = [
"affine_identity",
"affine_invert",
"affine_rotate",
"affine_rotate_degrees",
"affine_scale",
"affine_shear",
"affine_translate",
"bounds_cube",
"bounds_manual",
"bounds_where",
"sample_0d",
"sample_1d",
"sample_2d",
"sample_3d",
"sampler_point_0d",
"sampler_point_1d",
"sampler_point_2d",
"sampler_point_3d",
]
127 changes: 73 additions & 54 deletions src/fineslice/affine.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""Affine transformation functions.
Create, modify, and apply affine transformations.
"""

from typing import Union

import numpy as np
Expand All @@ -8,78 +13,92 @@


def affine_invert(aff: Affine) -> Affine:
"""Invert affine matrix."""
return np.linalg.inv(aff)


def affine_identity(dtype=np.float64):
return np.array([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
], dtype=dtype)
def affine_identity(
dtype=np.float64, # noqa: ANN001
) -> Affine:
"""Create identity affine matrix."""
return np.array(
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], dtype=dtype
)


def affine_translate(aff: Affine, x: NpScalar = 0, y: NpScalar = 0, z: NpScalar = 0) -> Affine:
affine_translated = np.array([
[1, 0, 0, x],
[0, 1, 0, y],
[0, 0, 1, z],
[0, 0, 0, 1]
], dtype=aff.dtype)
def affine_translate(
aff: Affine, x: NpScalar = 0, y: NpScalar = 0, z: NpScalar = 0
) -> Affine:
"""Translate affine matrix."""
affine_translated = np.array(
[[1, 0, 0, x], [0, 1, 0, y], [0, 0, 1, z], [0, 0, 0, 1]], dtype=aff.dtype
)
return aff.dot(affine_translated)


def affine_scale(aff: Affine, x: NpScalar = 1, y: NpScalar = 1, z: NpScalar = 1) -> Affine:
affine_scaled = np.array([
[x, 0, 0, 0],
[0, y, 0, 0],
[0, 0, z, 0],
[0, 0, 0, 1]
], dtype=aff.dtype)
def affine_scale(
aff: Affine, x: NpScalar = 1, y: NpScalar = 1, z: NpScalar = 1
) -> Affine:
"""Scale affine matrix."""
affine_scaled = np.array(
[[x, 0, 0, 0], [0, y, 0, 0], [0, 0, z, 0], [0, 0, 0, 1]], dtype=aff.dtype
)
return aff.dot(affine_scaled)


def affine_shear(
aff: Affine,
xy: NpScalar = 0,
yx: NpScalar = 0,
xz: NpScalar = 0,
zx: NpScalar = 0,
yz: NpScalar = 0,
zy: NpScalar = 0
aff: Affine,
xy: NpScalar = 0,
yx: NpScalar = 0,
xz: NpScalar = 0,
zx: NpScalar = 0,
yz: NpScalar = 0,
zy: NpScalar = 0,
) -> Affine:
affine_scaled = np.array([
[1, xy, xz, 0],
[yx, 1, yz, 0],
[zx, zy, 1, 0],
[0, 0, 0, 1]
], dtype=aff.dtype)
"""Shear affine matrix."""
affine_scaled = np.array(
[[1, xy, xz, 0], [yx, 1, yz, 0], [zx, zy, 1, 0], [0, 0, 0, 1]], dtype=aff.dtype
)
return aff.dot(affine_scaled)


def affine_rotate(aff: Affine, x: NpScalar = 0, y: NpScalar = 0, z: NpScalar = 0) -> Affine:
affine_rx = np.array([
[1, 0, 0, 0],
[0, np.cos(x), np.sin(x), 0],
[0, -np.sin(x), np.cos(x), 0],
[0, 0, 0, 1]
], dtype=aff.dtype)
affine_ry = np.array([
[np.cos(y), 0, -np.sin(y), 0],
[0, 1, 0, 0],
[np.sin(y), 0, np.cos(y), 0],
[0, 0, 0, 1]
], dtype=aff.dtype)
affine_rz = np.array([
[np.cos(z), -np.sin(z), 0, 0],
[np.sin(z), np.cos(z), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
], dtype=aff.dtype)
def affine_rotate(
aff: Affine, x: NpScalar = 0, y: NpScalar = 0, z: NpScalar = 0
) -> Affine:
"""Rotate affine matrix (in radians)."""
affine_rx = np.array(
[
[1, 0, 0, 0],
[0, np.cos(x), np.sin(x), 0],
[0, -np.sin(x), np.cos(x), 0],
[0, 0, 0, 1],
],
dtype=aff.dtype,
)
affine_ry = np.array(
[
[np.cos(y), 0, -np.sin(y), 0],
[0, 1, 0, 0],
[np.sin(y), 0, np.cos(y), 0],
[0, 0, 0, 1],
],
dtype=aff.dtype,
)
affine_rz = np.array(
[
[np.cos(z), -np.sin(z), 0, 0],
[np.sin(z), np.cos(z), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
],
dtype=aff.dtype,
)

return aff.dot(affine_rx).dot(affine_ry).dot(affine_rz)


def affine_rotate_degrees(aff: Affine, x: NpScalar = 0, y: NpScalar = 0, z: NpScalar = 0) -> np.ndarray:
def affine_rotate_degrees(
aff: Affine, x: NpScalar = 0, y: NpScalar = 0, z: NpScalar = 0
) -> Affine:
"""Rotate affine matrix in degrees."""
return affine_rotate(aff, np.radians(x), np.radians(y), np.radians(z))
48 changes: 30 additions & 18 deletions src/fineslice/bounds.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
from typing import Union, Tuple
"""Create bounding cubes."""

from typing import Any, Tuple, Union

import numpy as np


def bounds_manual(p1, p2):
def bounds_manual(
p1: Any, # noqa: ANN401
p2: Any, # noqa: ANN401
) -> np.ndarray:
"""Create bounding cube from two points."""
return np.vstack((np.vstack((p1, p2)).T, (0, 0))) # TODO should this be (1,1) ??


def bounds_where(texture_bool: np.ndarray, affine: np.ndarray, margin=0.):
def bounds_where(
texture_bool: np.ndarray,
affine: np.ndarray,
margin: float = 0.0,
) -> np.ndarray:
"""Create bounding cube from a boolean texture."""
wpos = np.where(texture_bool)
wpos = np.vstack(
wpos + (np.ones(wpos[0].shape[0]),)
)
wpos = np.vstack(wpos + (np.ones(wpos[0].shape[0]),)) # type: ignore

wpos_trans = np.dot(affine, wpos)

return np.vstack([
np.min(wpos_trans, axis=1) - margin,
np.max(wpos_trans, axis=1) + margin
]).T
return np.vstack(
[np.min(wpos_trans, axis=1) - margin, np.max(wpos_trans, axis=1) + margin]
).T


def bounds_cube(
size: Union[float, Tuple[float, float, float]],
offset: Union[float, Tuple[float, float, float]] = 0.) -> np.ndarray:
size: Union[float, Tuple[float, float, float]],
offset: Union[float, Tuple[float, float, float]] = 0.0,
) -> np.ndarray:
"""Create a bounding cube with a specific size and offset."""
if isinstance(size, tuple):
size_x, size_y, size_z = size
else:
Expand All @@ -33,9 +43,11 @@ def bounds_cube(
else:
offset_x = offset_y = offset_z = offset

return np.array([
[offset_x-size_x, offset_x+size_x],
[offset_y-size_y, offset_y+size_y],
[offset_z-size_z, offset_z+size_z],
[1., 1.]
])
return np.array(
[
[offset_x - size_x, offset_x + size_x],
[offset_y - size_y, offset_y + size_y],
[offset_z - size_z, offset_z + size_z],
[1.0, 1.0],
]
)
Loading

0 comments on commit eabbee3

Please sign in to comment.