@@ -28,116 +28,126 @@ def img_and_meta(*args, side='left'):
28
28
return img_and_meta
29
29
30
30
31
- class TestImageMaths :
32
-
33
- def test_thresh (self ):
34
- key = jax .random .PRNGKey (0 )
35
- arg = jax .random .uniform (key , (10 , 10 , 10 ))
36
- grammar = ImageMathsGrammar (default_interpreter = TensorLeafInterpreter ())
37
- op_str = 'ARGa -thr (ARGb -bin[0.5])'
38
- f = grammar .compile (op_str )
39
- img_out , _ = f (arg , arg )
40
- img_dataobj = arg
41
- img_thr = (img_dataobj > 0.5 )
42
- img_ref = jnp .where (img_dataobj > img_thr , img_dataobj , 0 )
43
- assert (img_out == img_ref ).all ()
44
-
45
- def test_morphological_atoms (self ):
46
- grammar = ImageMathsGrammar (default_interpreter = TensorLeafInterpreter ())
47
-
48
- data = np .zeros ((10 , 10 ))
49
- data [2 :8 , 2 :8 ] = 1
50
- data [2 , 3 ] = 0
51
- data [4 , 6 ] = 0
52
-
53
- op_str = 'ARG -dil[1]'
54
- f = grammar .compile (op_str )
55
- dil , _ = f (data )
56
-
57
- op_str = 'ARG -ero[1]'
58
- f = grammar .compile (op_str )
59
- ero , _ = f (data )
60
-
61
- op_str = 'ARG -opening[1]'
62
- f = grammar .compile (op_str )
63
- opened , _ = f (data )
64
-
65
- op_str = 'ARG -closing[1]'
66
- f = grammar .compile (op_str )
67
- closed , _ = f (data )
68
-
69
- op_str = 'ARG -fillholes[1|5]'
70
- f = grammar .compile (op_str )
71
- filled , _ = f (data )
72
-
73
- fig , ax = plt .subplots (2 , 3 , figsize = (18 , 12 ))
74
- ax [0 , 0 ].imshow (data , cmap = 'gray' )
75
- ax [0 , 1 ].imshow (dil , cmap = 'gray' )
76
- ax [0 , 2 ].imshow (ero , cmap = 'gray' )
77
- ax [1 , 0 ].imshow (opened , cmap = 'gray' )
78
- ax [1 , 1 ].imshow (closed , cmap = 'gray' )
79
- ax [1 , 2 ].imshow (filled , cmap = 'gray' )
80
-
81
- results = resource_filename (
82
- 'gramform' ,
83
- '_results/'
84
- )
85
- fig .savefig (f'{ results } /imops_morphology.png' , bbox_inches = 'tight' )
86
-
87
- def test_logical_atoms (self ):
88
- grammar = ImageMathsGrammar (default_interpreter = TensorLeafInterpreter ())
89
-
90
- data0 = np .zeros ((10 , 10 ))
91
- data0 [:, :7 ] = 1
92
- data0 [:, :1 ] = 0
93
- data1 = np .zeros ((10 , 10 ))
94
- data1 [:, 3 :] = 1
95
- data1 [:, 9 :] = 0
96
-
97
- op_str = 'ARG -neg'
98
- f = grammar .compile (op_str )
99
- negation0 , _ = f (data0 )
100
- negation1 , _ = f (data1 )
101
-
102
- op_str = 'ARGa -or ARGb'
103
- f = grammar .compile (op_str )
104
- union , _ = f (data0 , data1 )
105
-
106
- op_str = 'ARGa -and ARGb'
107
- f = grammar .compile (op_str )
108
- intersection , _ = f (data0 , data1 )
109
-
110
- fig , ax = plt .subplots (2 , 3 , figsize = (18 , 12 ))
111
- ax [0 , 0 ].imshow (data0 , cmap = 'gray' )
112
- ax [0 , 1 ].imshow (data1 , cmap = 'gray' )
113
- ax [0 , 2 ].imshow (negation0 , cmap = 'gray' )
114
- ax [1 , 0 ].imshow (negation1 , cmap = 'gray' )
115
- ax [1 , 1 ].imshow (union , cmap = 'gray' )
116
- ax [1 , 2 ].imshow (intersection , cmap = 'gray' )
117
-
118
- results = resource_filename (
119
- 'gramform' ,
120
- '_results/'
121
- )
122
- fig .savefig (f'{ results } /imops_logic.png' , bbox_inches = 'tight' )
123
-
124
- def test_compositions (self ):
125
- grammar = ImageMathsGrammar (default_interpreter = TensorLeafInterpreter ())
126
-
127
- key = jax .random .PRNGKey (0 )
128
- key0 , key1 , key2 = jax .random .split (key , 3 )
129
- data0 = jax .random .uniform (key0 , (10 , 10 ))
130
- data1 = jax .random .uniform (key1 , (10 , 10 ))
131
- data2 = jax .random .uniform (key2 , (10 , 10 ))
132
-
133
- op_str = (
134
- r'ARGa -sub{1} -thr[0.1]{0.5} -mul ARGb '
135
- r'-add{1} -uthr[-1] (ARGc -div{2,...})'
136
- )
137
- f = grammar .compile (op_str )
138
- img_out , _ = f (data0 , data1 , data2 )
139
- img_ref = data0 - 1
140
- img_ref = jnp .where (img_ref > 0.5 , img_ref , 0.1 )
141
- img_ref = img_ref * data1 + 1
142
- img_ref = jnp .where (img_ref < 2 / data2 , img_ref , - 1 )
143
- assert (img_out == img_ref ).all ()
31
+ def test_thresh ():
32
+ key = jax .random .PRNGKey (0 )
33
+ arg = jax .random .uniform (key , (10 , 10 , 10 ))
34
+ grammar = ImageMathsGrammar (default_interpreter = TensorLeafInterpreter ())
35
+ op_str = 'ARGa -thr (ARGb -bin[0.5])'
36
+ f = grammar .compile (op_str )
37
+ img_out , _ = f (arg , arg )
38
+ img_dataobj = arg
39
+ img_thr = (img_dataobj > 0.5 )
40
+ img_ref = jnp .where (img_dataobj > img_thr , img_dataobj , 0 )
41
+ assert (img_out == img_ref ).all ()
42
+
43
+
44
+ def test_morphological_atoms ():
45
+ grammar = ImageMathsGrammar (default_interpreter = TensorLeafInterpreter ())
46
+
47
+ data = np .zeros ((10 , 10 ))
48
+ data [2 :8 , 2 :8 ] = 1
49
+ data [2 , 3 ] = 0
50
+ data [4 , 6 ] = 0
51
+
52
+ op_str = 'ARG -dil[1]'
53
+ f = grammar .compile (op_str )
54
+ dil , _ = f (data )
55
+
56
+ op_str = 'ARG -ero[1]'
57
+ f = grammar .compile (op_str )
58
+ ero , _ = f (data )
59
+
60
+ op_str = 'ARG -opening[1]'
61
+ f = grammar .compile (op_str )
62
+ opened , _ = f (data )
63
+
64
+ op_str = 'ARG -closing[1]'
65
+ f = grammar .compile (op_str )
66
+ closed , _ = f (data )
67
+
68
+ op_str = 'ARG -fillholes[1|5]'
69
+ f = grammar .compile (op_str )
70
+ filled , _ = f (data )
71
+
72
+ fig , ax = plt .subplots (2 , 3 , figsize = (18 , 12 ))
73
+ ax [0 , 0 ].imshow (data , cmap = 'gray' )
74
+ ax [0 , 1 ].imshow (dil , cmap = 'gray' )
75
+ ax [0 , 2 ].imshow (ero , cmap = 'gray' )
76
+ ax [1 , 0 ].imshow (opened , cmap = 'gray' )
77
+ ax [1 , 1 ].imshow (closed , cmap = 'gray' )
78
+ ax [1 , 2 ].imshow (filled , cmap = 'gray' )
79
+
80
+ results = resource_filename (
81
+ 'gramform' ,
82
+ '_results/'
83
+ )
84
+ fig .savefig (f'{ results } /imops_morphology.png' , bbox_inches = 'tight' )
85
+
86
+
87
+ def test_logical_atoms ():
88
+ grammar = ImageMathsGrammar (default_interpreter = TensorLeafInterpreter ())
89
+
90
+ data0 = np .zeros ((10 , 10 ))
91
+ data0 [:, :7 ] = 1
92
+ data0 [:, :1 ] = 0
93
+ data1 = np .zeros ((10 , 10 ))
94
+ data1 [:, 3 :] = 1
95
+ data1 [:, 9 :] = 0
96
+
97
+ op_str = 'ARG -neg'
98
+ f = grammar .compile (op_str )
99
+ negation0 , _ = f (data0 )
100
+ negation1 , _ = f (data1 )
101
+
102
+ op_str = 'ARGa -or ARGb'
103
+ f = grammar .compile (op_str )
104
+ union , _ = f (data0 , data1 )
105
+
106
+ op_str = 'ARGa -and ARGb'
107
+ f = grammar .compile (op_str )
108
+ intersection , _ = f (data0 , data1 )
109
+
110
+ fig , ax = plt .subplots (2 , 3 , figsize = (18 , 12 ))
111
+ ax [0 , 0 ].imshow (data0 , cmap = 'gray' )
112
+ ax [0 , 1 ].imshow (data1 , cmap = 'gray' )
113
+ ax [0 , 2 ].imshow (negation0 , cmap = 'gray' )
114
+ ax [1 , 0 ].imshow (negation1 , cmap = 'gray' )
115
+ ax [1 , 1 ].imshow (union , cmap = 'gray' )
116
+ ax [1 , 2 ].imshow (intersection , cmap = 'gray' )
117
+
118
+ results = resource_filename (
119
+ 'gramform' ,
120
+ '_results/'
121
+ )
122
+ fig .savefig (f'{ results } /imops_logic.png' , bbox_inches = 'tight' )
123
+
124
+
125
+ def test_compositions ():
126
+ grammar = ImageMathsGrammar (default_interpreter = TensorLeafInterpreter ())
127
+
128
+ key = jax .random .PRNGKey (0 )
129
+ key0 , key1 , key2 = jax .random .split (key , 3 )
130
+ data0 = jax .random .uniform (key0 , (10 , 10 ))
131
+ data1 = jax .random .uniform (key1 , (10 , 10 ))
132
+ data2 = jax .random .uniform (key2 , (10 , 10 ))
133
+
134
+ op_str = (
135
+ r'ARGa -sub{1} -thr[0.1]{0.5} -mul ARGb '
136
+ r'-add{1} -uthr[-1] (ARGc -div{2,...})'
137
+ )
138
+ f = grammar .compile (op_str )
139
+ img_out , _ = f (data0 , data1 , data2 )
140
+ img_ref = data0 - 1
141
+ img_ref = jnp .where (img_ref > 0.5 , img_ref , 0.1 )
142
+ img_ref = img_ref * data1 + 1
143
+ img_ref = jnp .where (img_ref < 2 / data2 , img_ref , - 1 )
144
+ assert (img_out == img_ref ).all ()
145
+
146
+
147
+ def test_remainder ():
148
+ grammar = ImageMathsGrammar (default_interpreter = TensorLeafInterpreter ())
149
+ op_str = 'ARGa -mod ARGb -mod 3'
150
+ f = grammar .compile (op_str )
151
+ img_out , _ = f (np .full ((10 , 10 ), 17 ), np .full ((10 , 10 ), 13 ))
152
+ assert img_out .shape == (10 , 10 )
153
+ assert (img_out == 1 ).all ()
0 commit comments