Adding test scripts
This commit is contained in:
109
tests/test_16bit_tiff_loading.py
Normal file
109
tests/test_16bit_tiff_loading.py
Normal file
@@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for 16-bit TIFF loading and normalization.
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import tifffile
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add parent directory to path to import modules
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
from src.utils.image import Image
|
||||
|
||||
|
||||
def create_test_16bit_tiff(output_path: str) -> str:
|
||||
"""Create a test 16-bit grayscale TIFF file.
|
||||
|
||||
Args:
|
||||
output_path: Path where to save the test TIFF
|
||||
|
||||
Returns:
|
||||
Path to the created TIFF file
|
||||
"""
|
||||
# Create a 16-bit grayscale test image (100x100)
|
||||
# With values ranging from 0 to 65535 (full 16-bit range)
|
||||
height, width = 100, 100
|
||||
|
||||
# Create a gradient pattern
|
||||
test_data = np.zeros((height, width), dtype=np.uint16)
|
||||
for i in range(height):
|
||||
for j in range(width):
|
||||
# Create a diagonal gradient
|
||||
test_data[i, j] = int((i + j) / (height + width - 2) * 65535)
|
||||
|
||||
# Save as TIFF
|
||||
tifffile.imwrite(output_path, test_data)
|
||||
print(f"Created test 16-bit TIFF: {output_path}")
|
||||
print(f" Shape: {test_data.shape}")
|
||||
print(f" Dtype: {test_data.dtype}")
|
||||
print(f" Min value: {test_data.min()}")
|
||||
print(f" Max value: {test_data.max()}")
|
||||
|
||||
return output_path
|
||||
|
||||
|
||||
def test_image_loading():
|
||||
"""Test loading 16-bit TIFF with the Image class."""
|
||||
print("\n=== Testing Image Loading ===")
|
||||
|
||||
# Create temporary test file
|
||||
with tempfile.NamedTemporaryFile(suffix=".tif", delete=False) as tmp:
|
||||
test_path = tmp.name
|
||||
|
||||
try:
|
||||
# Create test image
|
||||
create_test_16bit_tiff(test_path)
|
||||
|
||||
# Load with Image class
|
||||
print("\nLoading with Image class...")
|
||||
img = Image(test_path)
|
||||
|
||||
print(f"Successfully loaded image:")
|
||||
print(f" Width: {img.width}")
|
||||
print(f" Height: {img.height}")
|
||||
print(f" Channels: {img.channels}")
|
||||
print(f" Dtype: {img.dtype}")
|
||||
print(f" Format: {img.format}")
|
||||
|
||||
# Test normalization
|
||||
print("\nTesting normalization to float32 [0-1]...")
|
||||
normalized = img.to_normalized_float32()
|
||||
|
||||
print(f"Normalized image:")
|
||||
print(f" Shape: {normalized.shape}")
|
||||
print(f" Dtype: {normalized.dtype}")
|
||||
print(f" Min value: {normalized.min():.6f}")
|
||||
print(f" Max value: {normalized.max():.6f}")
|
||||
print(f" Mean value: {normalized.mean():.6f}")
|
||||
|
||||
# Verify normalization
|
||||
assert normalized.dtype == np.float32, "Dtype should be float32"
|
||||
assert (
|
||||
0.0 <= normalized.min() <= normalized.max() <= 1.0
|
||||
), "Values should be in [0, 1]"
|
||||
|
||||
print("\n✓ All tests passed!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n✗ Test failed with error: {e}")
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
finally:
|
||||
# Cleanup
|
||||
if os.path.exists(test_path):
|
||||
os.remove(test_path)
|
||||
print(f"\nCleaned up test file: {test_path}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_image_loading()
|
||||
sys.exit(0 if success else 1)
|
||||
Reference in New Issue
Block a user