|
3 | 3 | import simplnx as nx |
4 | 4 | import simplnx_test_dirs as nxtest |
5 | 5 | import simplnx_utilities |
6 | | - |
7 | | -# --------------------------------------------------------------------------- |
8 | | -# Test 1: Generate a full script from a pipeline with basic filters |
9 | | -# --------------------------------------------------------------------------- |
10 | | -print("=== Test 1: generate_python_pipeline with basic filters ===") |
11 | | - |
12 | | -data_structure = nx.DataStructure() |
13 | | - |
14 | | -pipeline = nx.Pipeline() |
15 | | -pipeline.append(nx.CreateImageGeometryFilter(), { |
16 | | - "dimensions": [10, 10, 10], |
17 | | - "origin": [0.0, 0.0, 0.0], |
18 | | - "spacing": [1.0, 1.0, 1.0], |
19 | | - "output_image_geometry_path": nx.DataPath("ImageGeom"), |
20 | | - "cell_attribute_matrix_name": "CellData", |
21 | | -}) |
22 | | - |
23 | | -code = simplnx_utilities.generate_python_pipeline(pipeline) |
24 | | -print(code) |
25 | | - |
26 | | -# Verify the code is syntactically valid Python |
27 | | -compile(code, "<generated_pipeline>", "exec") |
28 | | -print("Test 1 PASSED: generated code compiles successfully") |
29 | | - |
30 | | -# --------------------------------------------------------------------------- |
31 | | -# Test 2: Generate filter snippets |
32 | | -# --------------------------------------------------------------------------- |
33 | | -print("\n=== Test 2: generate_python_filters ===") |
34 | | - |
35 | | -filters = [pipeline[i] for i in range(len(pipeline))] |
36 | | -snippet = simplnx_utilities.generate_python_filters(filters) |
37 | | -print(snippet) |
38 | | - |
39 | | -# Snippets reference data_structure and check_filter_result, so wrap them |
40 | | -# in a function body to make them compile without executing |
41 | | -wrapped = ( |
42 | | - "def _snippet(data_structure, check_filter_result):\n" |
43 | | - + "\n".join(" " + line for line in snippet.splitlines()) |
44 | | -) |
45 | | -compile(wrapped, "<generated_snippet>", "exec") |
46 | | -print("Test 2 PASSED: generated snippet compiles successfully") |
47 | | - |
48 | | -# --------------------------------------------------------------------------- |
49 | | -# Test 3: Multi-filter pipeline with threshold parameter |
50 | | -# --------------------------------------------------------------------------- |
51 | | -print("\n=== Test 3: pipeline with ArrayThresholdSet parameter ===") |
52 | | - |
53 | | -pipeline2 = nx.Pipeline() |
54 | | -pipeline2.append(nx.CreateImageGeometryFilter(), { |
55 | | - "dimensions": [10, 10, 10], |
56 | | - "origin": [0.0, 0.0, 0.0], |
57 | | - "spacing": [1.0, 1.0, 1.0], |
58 | | - "output_image_geometry_path": nx.DataPath("ImageGeom"), |
59 | | - "cell_attribute_matrix_name": "CellData", |
60 | | -}) |
61 | | -pipeline2.append(nx.CreateDataArrayFilter(), { |
62 | | - "component_count": 1, |
63 | | - "initialization_value_str": "0", |
64 | | - "numeric_type_index": nx.NumericType.float32, |
65 | | - "output_array_path": nx.DataPath("ImageGeom/CellData/Quality"), |
66 | | - "tuple_dimensions": [[10, 10, 10]], |
67 | | -}) |
68 | | - |
69 | | -threshold = nx.ArrayThreshold() |
70 | | -threshold.array_path = nx.DataPath("ImageGeom/CellData/Quality") |
71 | | -threshold.comparison = nx.ArrayThreshold.ComparisonType.GreaterThan |
72 | | -threshold.value = 0.5 |
73 | | - |
74 | | -threshold_set = nx.ArrayThresholdSet() |
75 | | -threshold_set.thresholds = [threshold] |
76 | | - |
77 | | -pipeline2.append(nx.MultiThresholdObjectsFilter(), { |
78 | | - "array_thresholds_object": threshold_set, |
79 | | - "created_mask_type": nx.DataType.boolean, |
80 | | - "output_data_array_name": "Mask", |
81 | | -}) |
82 | | - |
83 | | -code2 = simplnx_utilities.generate_python_pipeline(pipeline2) |
84 | | -print(code2) |
85 | | -compile(code2, "<generated_pipeline_thresholds>", "exec") |
86 | | -print("Test 3 PASSED: threshold pipeline code compiles successfully") |
87 | | - |
88 | | -print("\n===> All pipeline_to_python tests passed") |
| 6 | +import unittest |
| 7 | + |
| 8 | +class PipelineConversionTest(unittest.TestCase): |
| 9 | + # --------------------------------------------------------------------------- |
| 10 | + # Test 1: Generate a full script from a pipeline with basic filters |
| 11 | + # --------------------------------------------------------------------------- |
| 12 | + def test_GenerateFullPipeline(self): |
| 13 | + print("=== Test 1: generate_python_pipeline with basic filters ===") |
| 14 | + |
| 15 | + pipeline = nx.Pipeline() |
| 16 | + pipeline.append(nx.CreateImageGeometryFilter(), { |
| 17 | + "dimensions": [10, 10, 10], |
| 18 | + "origin": [0.0, 0.0, 0.0], |
| 19 | + "spacing": [1.0, 1.0, 1.0], |
| 20 | + "output_image_geometry_path": nx.DataPath("ImageGeom"), |
| 21 | + "cell_attribute_matrix_name": "CellData", |
| 22 | + }) |
| 23 | + |
| 24 | + code = simplnx_utilities.generate_python_pipeline(pipeline) |
| 25 | + print(code) |
| 26 | + |
| 27 | + # Verify the code is syntactically valid Python |
| 28 | + compile(code, "<generated_pipeline>", "exec") |
| 29 | + print("Test 1 PASSED: generated code compiles successfully") |
| 30 | + |
| 31 | + # --------------------------------------------------------------------------- |
| 32 | + # Test 2: Generate filter snippets |
| 33 | + # --------------------------------------------------------------------------- |
| 34 | + def test_GenerateFilterSnippets(self): |
| 35 | + print("\n=== Test 2: generate_python_filters ===") |
| 36 | + |
| 37 | + pipeline = nx.Pipeline() |
| 38 | + pipeline.append(nx.CreateImageGeometryFilter(), { |
| 39 | + "dimensions": [10, 10, 10], |
| 40 | + "origin": [0.0, 0.0, 0.0], |
| 41 | + "spacing": [1.0, 1.0, 1.0], |
| 42 | + "output_image_geometry_path": nx.DataPath("ImageGeom"), |
| 43 | + "cell_attribute_matrix_name": "CellData", |
| 44 | + }) |
| 45 | + |
| 46 | + filters = [pipeline[i] for i in range(len(pipeline))] |
| 47 | + snippet = simplnx_utilities.generate_python_filters(filters) |
| 48 | + print(snippet) |
| 49 | + |
| 50 | + # Snippets reference data_structure and check_filter_result, so wrap them |
| 51 | + # in a function body to make them compile without executing |
| 52 | + wrapped = ( |
| 53 | + "def _snippet(data_structure, check_filter_result):\n" |
| 54 | + + "\n".join(" " + line for line in snippet.splitlines()) |
| 55 | + ) |
| 56 | + compile(wrapped, "<generated_snippet>", "exec") |
| 57 | + print("Test 2 PASSED: generated snippet compiles successfully") |
| 58 | + |
| 59 | + # --------------------------------------------------------------------------- |
| 60 | + # Test 3: Multi-filter pipeline with threshold parameter |
| 61 | + # --------------------------------------------------------------------------- |
| 62 | + def test_GenerateMultiFilterPipelineWithThresholdParameter(self): |
| 63 | + print("\n=== Test 3: pipeline with ArrayThresholdSet parameter ===") |
| 64 | + |
| 65 | + pipeline2 = nx.Pipeline() |
| 66 | + pipeline2.append(nx.CreateImageGeometryFilter(), { |
| 67 | + "dimensions": [10, 10, 10], |
| 68 | + "origin": [0.0, 0.0, 0.0], |
| 69 | + "spacing": [1.0, 1.0, 1.0], |
| 70 | + "output_image_geometry_path": nx.DataPath("ImageGeom"), |
| 71 | + "cell_attribute_matrix_name": "CellData", |
| 72 | + }) |
| 73 | + pipeline2.append(nx.CreateDataArrayFilter(), { |
| 74 | + "component_count": 1, |
| 75 | + "initialization_value_str": "0", |
| 76 | + "numeric_type_index": nx.NumericType.float32, |
| 77 | + "output_array_path": nx.DataPath("ImageGeom/CellData/Quality"), |
| 78 | + "tuple_dimensions": [[10, 10, 10]], |
| 79 | + }) |
| 80 | + |
| 81 | + threshold = nx.ArrayThreshold() |
| 82 | + threshold.array_path = nx.DataPath("ImageGeom/CellData/Quality") |
| 83 | + threshold.comparison = nx.ArrayThreshold.ComparisonType.GreaterThan |
| 84 | + threshold.value = 0.5 |
| 85 | + |
| 86 | + threshold_set = nx.ArrayThresholdSet() |
| 87 | + threshold_set.thresholds = [threshold] |
| 88 | + |
| 89 | + pipeline2.append(nx.MultiThresholdObjectsFilter(), { |
| 90 | + "array_thresholds_object": threshold_set, |
| 91 | + "created_mask_type": nx.DataType.boolean, |
| 92 | + "output_data_array_name": "Mask", |
| 93 | + }) |
| 94 | + |
| 95 | + code = simplnx_utilities.generate_python_pipeline(pipeline2) |
| 96 | + print(code) |
| 97 | + compile(code, "<generated_pipeline_thresholds>", "exec") |
| 98 | + print("Test 3 PASSED: threshold pipeline code compiles successfully") |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + unittest.main() |
0 commit comments