Skip to content

Commit

Permalink
Unique function for center descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
lauraporta committed Nov 26, 2024
1 parent a54c129 commit a1ece6a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
17 changes: 8 additions & 9 deletions tests/test_regression/recreate_target/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,13 @@ def load_rotated_images(
np.ndarray
The stack of rotated images.
"""
# expected end of file name
center_suffix = "" if center is None else f"{center[0]}_{center[1]}_"

# load pre-computed images one by one
rotated_image_stack = []
for i in range(1, NUMBER_OF_FRAMES + 1):
image_path = (
ROTATED_IMAGES_PATH / f"rotated_frame_{center_suffix}{i}.png"
ROTATED_IMAGES_PATH
/ f"rotated_frame_{center_formatting(center)}{i}.png"
)
rotated_image = Image.open(image_path).convert("L")
rotated_image_stack.append(np.array(rotated_image))
Expand Down Expand Up @@ -178,14 +177,13 @@ def regenerate_rotator_images_for_testing(
rotated_image_stack = rotate_images(image_stack, angles, center=center)

# expected end of file name
center_suffix = "" if center is None else f"{center[0]}_{center[1]}_"

# Save rotated images
for i, rotated_frame in enumerate(rotated_image_stack):
rotated_image = Image.fromarray(rotated_frame.astype("uint8"))
save_image_in_test_folder(
"rotator",
f"rotated_frame_{center_suffix}{i + 1}.png",
f"rotated_frame_{center_formatting(center)}{i + 1}.png",
rotated_image,
)

Expand Down Expand Up @@ -215,14 +213,11 @@ def regenerate_derotated_images_for_testing(
rotated_image_stack, angles, center=center
)

# Save derotated images
center_suffix = "" if center is None else f"{center[0]}_{center[1]}_"

for i, derotated_frame in enumerate(derotated_image_stack):
derotated_image = Image.fromarray(derotated_frame.astype("uint8"))
save_image_in_test_folder(
"rotator_derotator",
f"derotated_frame_{center_suffix}{i + 1}.png",
f"derotated_frame_{center_formatting(center)}{i + 1}.png",
derotated_image,
)

Expand All @@ -232,3 +227,7 @@ def save_image_in_test_folder(folder: str, image_name: str, image: np.ndarray):
target_path = base_dir / f"images/{folder}/{image_name}"
target_path.parent.mkdir(parents=True, exist_ok=True)
image.save(target_path)


def center_formatting(center: Optional[Tuple[int, int]]) -> str:
return "" if center is None else f"{center[0]}_{center[1]}_"
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from derotation.simulate.basic_rotator import Rotator
from tests.test_regression.recreate_target.shared import (
ROTATED_IMAGES_PATH,
center_formatting,
rotate_images,
)

Expand Down Expand Up @@ -36,13 +37,13 @@ def test_rotator_by_line(image_stack, angles, center):
# Perform rotation
rotated_image_stack = rotate_images(image_stack, angles, center=center)

center_suffix = "default" if center is None else f"{center[0]}_{center[1]}"

# Check each rotated frame against precomputed expected images
for i, rotated_frame in enumerate(rotated_image_stack):
target_image = Image.open(
ROTATED_IMAGES_PATH / f"rotated_frame_{center_suffix}_{i + 1}.png"
ROTATED_IMAGES_PATH
/ f"rotated_frame_{center_formatting(center)}{i + 1}.png"
)

target_image = np.array(target_image.convert("L"))

# Compare each frame against the precomputed target image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from derotation.derotate_by_line import derotate_an_image_array_line_by_line
from tests.test_regression.recreate_target.shared import (
DEROTATED_IMAGES_PATH,
center_formatting,
load_rotated_images,
)

Expand All @@ -23,13 +24,10 @@ def test_derotator_by_line_with_center(angles, center):
rotated_images, angles, center=center
)

# Check each derotated frame against precomputed expected images
center_suffix = f"{center[0]}_{center[1]}"

for i, derotated_frame in enumerate(derotated_image_stack):
target_image = Image.open(
DEROTATED_IMAGES_PATH
/ f"derotated_frame_{center_suffix}_{i + 1}.png"
/ f"derotated_frame_{center_formatting(center)}{i + 1}.png"
)
target_image = np.array(target_image.convert("L"))

Expand Down

0 comments on commit a1ece6a

Please sign in to comment.