-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_comprehensive_scripts.py
More file actions
executable file
·328 lines (251 loc) · 11.5 KB
/
Copy pathtest_comprehensive_scripts.py
File metadata and controls
executable file
·328 lines (251 loc) · 11.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""
Comprehensive script testing for T-MIDAS.
Tests multiple scripts across different categories.
"""
import os
import tempfile
import shutil
import subprocess
import sys
import numpy as np
import pytest
# 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 comprehensive test data for various script types."""
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)
# Create multi-channel image
multichannel = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
multichannel_path = os.path.join(test_dir, "multichannel.tif")
write_image(multichannel, multichannel_path)
# Create 3D image stack
stack_3d = np.random.randint(0, 255, (10, 100, 100), dtype=np.uint8)
stack_path = os.path.join(test_dir, "stack.tif")
write_image(stack_3d, stack_path)
return test_dir
def run_script_test(script_name, args, test_dir, timeout=30):
"""Run a script test with given arguments."""
script_path = f"/opt/T-MIDAS/scripts/{script_name}"
if not os.path.exists(script_path):
print(f"Script {script_name} not found")
return False
try:
cmd = [sys.executable, script_path] + args
result = subprocess.run(cmd, capture_output=True, text=True,
timeout=timeout, cwd=test_dir)
if result.returncode != 0:
print(f"Script {script_name} failed: {result.stderr}")
return False
return True
except subprocess.TimeoutExpired:
print(f"Script {script_name} timed out")
return False
except Exception as e:
print(f"Script {script_name} error: {e}")
return False
class TestFileConversionScripts:
"""Test file conversion scripts."""
def setup_method(self):
self.test_dir = create_test_data()
def teardown_method(self):
shutil.rmtree(self.test_dir)
def test_split_color_channels(self):
"""Test splitting color channels."""
args = ["--input", self.test_dir, "--pattern", "multichannel.tif"]
success = run_script_test("split_color_channels.py", args, self.test_dir)
assert success, "split_color_channels script failed"
def test_merge_color_channels(self):
"""Test merging color channels."""
# First split, then merge
split_args = ["--input", self.test_dir, "--pattern", "multichannel.tif"]
run_script_test("split_color_channels.py", split_args, self.test_dir)
merge_args = ["--input", self.test_dir, "--pattern", "_ch", "--output", "merged.tif"]
success = run_script_test("merge_color_channels.py", merge_args, self.test_dir)
assert success, "merge_color_channels script failed"
def test_convert_instance_to_semantic(self):
"""Test instance to semantic conversion."""
args = ["--input", self.test_dir, "--pattern", "_labels.tif"]
success = run_script_test("convert_instance_to_semantic.py", args, self.test_dir)
assert success, "convert_instance_to_semantic script failed"
class TestSegmentationScripts:
"""Test segmentation-related scripts."""
def setup_method(self):
self.test_dir = create_test_data()
def teardown_method(self):
shutil.rmtree(self.test_dir)
def test_segmentation_semantic(self):
"""Test semantic segmentation."""
args = ["--input", self.test_dir, "--method", "otsu"]
success = run_script_test("segmentation_semantic.py", args, self.test_dir)
assert success, "segmentation_semantic script failed"
def test_segmentation_instances(self):
"""Test instance segmentation."""
args = ["--input", self.test_dir, "--method", "otsu"]
success = run_script_test("segmentation_instances.py", args, self.test_dir)
assert success, "segmentation_instances script failed"
def test_segmentation_spots(self):
"""Test spot segmentation."""
args = ["--input", self.test_dir]
success = run_script_test("segmentation_spots.py", args, self.test_dir)
assert success, "segmentation_spots script failed"
class TestROIScripts:
"""Test ROI analysis scripts."""
def setup_method(self):
self.test_dir = create_test_data()
def teardown_method(self):
shutil.rmtree(self.test_dir)
def test_roi_count_instances_2d(self):
"""Test 2D instance counting."""
args = ["--input", self.test_dir, "--label_pattern", "_labels.tif"]
success = run_script_test("ROI_count_instances_2D.py", args, self.test_dir)
assert success, "ROI_count_instances_2D script failed"
def test_roi_count_instances_3d(self):
"""Test 3D instance counting."""
args = ["--input", self.test_dir, "--label_pattern", "_labels.tif"]
success = run_script_test("ROI_count_instances_3D.py", args, self.test_dir)
assert success, "ROI_count_instances_3D script failed"
def test_get_class_regionprops(self):
"""Test class region properties."""
args = ["--input", self.test_dir, "--label_pattern", "_labels.tif"]
success = run_script_test("get_class_regionprops.py", args, self.test_dir)
assert success, "get_class_regionprops script failed"
class TestImageProcessingScripts:
"""Test image processing scripts."""
def setup_method(self):
self.test_dir = create_test_data()
def teardown_method(self):
shutil.rmtree(self.test_dir)
def test_deep_tissue_clahe(self):
"""Test CLAHE enhancement."""
args = ["--input", self.test_dir]
success = run_script_test("deep_tissue_clahe.py", args, self.test_dir)
assert success, "deep_tissue_clahe script failed"
def test_combine_labels(self):
"""Test label combination."""
args = ["--input", self.test_dir, "--pattern", "_labels.tif", "--output", "combined.tif"]
success = run_script_test("combine_labels.py", args, self.test_dir)
assert success, "combine_labels script failed"
def test_export_label(self):
"""Test label export."""
args = ["--input", self.test_dir, "--pattern", "_labels.tif"]
success = run_script_test("export_label.py", args, self.test_dir)
assert success, "export_label script failed"
class TestUtilityScripts:
"""Test utility scripts."""
def setup_method(self):
self.test_dir = create_test_data()
def teardown_method(self):
shutil.rmtree(self.test_dir)
def test_random_tile_sampler(self):
"""Test random tile sampling."""
args = ["--input", self.test_dir, "--tile_size", "50", "--num_tiles", "5"]
success = run_script_test("random_tile_sampler.py", args, self.test_dir)
assert success, "random_tile_sampler script failed"
def test_intersection(self):
"""Test image intersection."""
args = ["--input", self.test_dir, "--pattern1", ".tif", "--pattern2", "_labels.tif"]
success = run_script_test("intersection.py", args, self.test_dir)
assert success, "intersection script failed"
class TestCompressionScripts:
"""Test compression/decompression scripts."""
def setup_method(self):
self.test_dir = create_test_data()
def teardown_method(self):
shutil.rmtree(self.test_dir)
def test_zstd_compression(self):
"""Test ZSTD compression."""
args = ["--input", self.test_dir, "--pattern", ".tif"]
success = run_script_test("zstd_compression.py", args, self.test_dir)
assert success, "zstd_compression script failed"
def test_zstd_decompression(self):
"""Test ZSTD decompression."""
# First compress
compress_args = ["--input", self.test_dir, "--pattern", ".tif"]
run_script_test("zstd_compression.py", compress_args, self.test_dir)
# Then decompress
decompress_args = ["--input", self.test_dir, "--pattern", ".zst"]
success = run_script_test("zstd_decompression.py", decompress_args, self.test_dir)
assert success, "zstd_decompression script failed"
class TestValidationScripts:
"""Test validation scripts."""
def setup_method(self):
self.test_dir = create_test_data()
def teardown_method(self):
shutil.rmtree(self.test_dir)
def test_counts_validation(self):
"""Test counts validation."""
args = ["--input", self.test_dir, "--pattern", "_labels.tif"]
success = run_script_test("counts_validation.py", args, self.test_dir)
assert success, "counts_validation script failed"
def test_segmentation_validation_f1_score(self):
"""Test segmentation validation with F1 score."""
args = ["--input", self.test_dir, "--pattern", "_labels.tif"]
success = run_script_test("segmentation_validation_f1_score.py", args, self.test_dir)
assert success, "segmentation_validation_f1_score script failed"
def test_script_syntax_validation():
"""Test that all scripts have valid Python syntax."""
scripts_dir = "/opt/T-MIDAS/scripts"
failed_scripts = []
for filename in os.listdir(scripts_dir):
if filename.endswith('.py') and not filename.startswith('__'):
script_path = os.path.join(scripts_dir, filename)
try:
import py_compile
py_compile.compile(script_path, doraise=True)
except py_compile.PyCompileError as e:
failed_scripts.append((filename, str(e)))
if failed_scripts:
failure_msg = "Scripts with syntax errors:\n"
for script, error in failed_scripts:
failure_msg += f" {script}: {error}\n"
pytest.fail(failure_msg)
def test_script_import_validation():
"""Test that all scripts can import their dependencies."""
scripts_dir = "/opt/T-MIDAS/scripts"
failed_scripts = []
for filename in os.listdir(scripts_dir):
if filename.endswith('.py') and not filename.startswith('__'):
script_path = os.path.join(scripts_dir, filename)
try:
# Try to import the script as a module
spec = importlib.util.spec_from_file_location(filename[:-3], script_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
except Exception as e:
failed_scripts.append((filename, str(e)))
if failed_scripts:
failure_msg = "Scripts with import errors:\n"
for script, error in failed_scripts:
failure_msg += f" {script}: {error}\n"
pytest.fail(failure_msg)
if __name__ == "__main__":
# Run syntax and import validation
print("Running comprehensive script validation...")
# Syntax check
print("Checking syntax...")
try:
test_script_syntax_validation()
print("✓ All scripts have valid syntax")
except Exception as e:
print(f"❌ Syntax errors found: {e}")
# Import check
print("Checking imports...")
try:
import importlib.util
test_script_import_validation()
print("✓ All scripts can import dependencies")
except Exception as e:
print(f"❌ Import errors found: {e}")
print("Comprehensive validation complete!")