-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_scripts.py
More file actions
executable file
·178 lines (141 loc) · 5 KB
/
test_scripts.py
File metadata and controls
executable file
·178 lines (141 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
Script execution tests to verify refactored scripts work correctly.
"""
import os
import tempfile
import shutil
import subprocess
import sys
import numpy as np
# Add tmidas to path
sys.path.insert(0, '/opt/T-MIDAS')
from tmidas.utils.io_utils import read_image, write_image
def create_test_data():
"""Create test data for script testing."""
test_dir = tempfile.mkdtemp()
# Create sample image
sample_image = np.random.randint(0, 255, (100, 100), dtype=np.uint8)
image_path = os.path.join(test_dir, "sample.tif")
write_image(sample_image, image_path)
# Create sample labels
labels = np.zeros((100, 100), dtype=np.uint32)
labels[10:30, 10:30] = 1 # 400 pixels
labels[50:70, 50:70] = 2 # 400 pixels
labels[80:85, 80:85] = 3 # 25 pixels (small)
labels_path = os.path.join(test_dir, "sample_labels.tif")
write_image(labels, labels_path)
return test_dir
def test_remove_small_labels_script():
"""Test the remove_small_labels script execution."""
test_dir = create_test_data()
try:
# Run the script
cmd = [
sys.executable,
"/opt/T-MIDAS/scripts/remove_small_labels.py",
"--input", test_dir,
"--label_suffix", "_labels.tif",
"--min_size", "100",
"--output_type", "instance"
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if result.returncode != 0:
print(f"Script failed with error: {result.stderr}")
return False
# Check if output file was created
output_path = os.path.join(test_dir, "sample_labels.tif")
if not os.path.exists(output_path):
print("Output file was not created")
return False
# Verify the output
output_labels = read_image(output_path)
if output_labels is None:
print("Could not read output labels")
return False
# Check that small labels were removed (label 3 should be gone)
unique_labels = np.unique(output_labels)
if 3 in unique_labels:
print("Small label was not removed")
return False
print("✓ remove_small_labels script test passed")
return True
except subprocess.TimeoutExpired:
print("Script timed out")
return False
except Exception as e:
print(f"Script test failed with exception: {e}")
return False
finally:
shutil.rmtree(test_dir)
def test_get_basic_regionprops_script():
"""Test the get_basic_regionprops script execution."""
test_dir = create_test_data()
try:
# Run the script
cmd = [
sys.executable,
"/opt/T-MIDAS/scripts/get_basic_regionprops.py",
"--input", test_dir,
"--label_pattern", "_labels.tif",
"--channel", "-1" # No intensity channel
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if result.returncode != 0:
print(f"Script failed with error: {result.stderr}")
return False
# Check if CSV was created
csv_path = os.path.join(test_dir, "regionprops.csv")
if not os.path.exists(csv_path):
print("CSV output file was not created")
return False
# Check CSV content
with open(csv_path, 'r') as f:
content = f.read()
if len(content.strip()) == 0:
print("CSV file is empty")
return False
print("✓ get_basic_regionprops script test passed")
return True
except subprocess.TimeoutExpired:
print("Script timed out")
return False
except Exception as e:
print(f"Script test failed with exception: {e}")
return False
finally:
shutil.rmtree(test_dir)
def test_segmentation_sam_script():
"""Test the segmentation_SAM_2D script (basic syntax check only)."""
# This script requires napari and user interaction, so we just check syntax
try:
import py_compile
py_compile.compile("/opt/T-MIDAS/scripts/segmentation_SAM_2D.py", doraise=True)
print("✓ segmentation_SAM_2D script syntax check passed")
return True
except py_compile.PyCompileError as e:
print(f"Script syntax error: {e}")
return False
def run_all_script_tests():
"""Run all script execution tests."""
print("Running script execution tests...")
tests = [
test_remove_small_labels_script,
test_get_basic_regionprops_script,
test_segmentation_sam_script,
]
passed = 0
total = len(tests)
for test in tests:
if test():
passed += 1
print()
print(f"Results: {passed}/{total} tests passed")
if passed == total:
print("🎉 All script tests passed!")
return True
else:
print("❌ Some script tests failed")
return False
if __name__ == "__main__":
success = run_all_script_tests()
sys.exit(0 if success else 1)