7
7
from scipy .sparse .csgraph import connected_components
8
8
import pytest
9
9
10
- from sklearn .utils .fixes import sp_version , parse_version
11
10
from sklearn .feature_extraction .image import (
12
11
img_to_graph ,
13
12
grid_to_graph ,
18
17
)
19
18
20
19
21
- @pytest .fixture (scope = "module" )
22
- def raccoon_face ():
23
- if sp_version .release >= parse_version ("1.10" ).release :
24
- pytest .importorskip ("pooch" )
25
- from scipy .datasets import face
26
- else :
27
- from scipy .misc import face
28
-
29
- return face (gray = True )
30
-
31
-
32
20
def test_img_to_graph ():
33
21
x , y = np .mgrid [:4 , :4 ] - 10
34
22
grad_x = img_to_graph (x )
@@ -93,8 +81,8 @@ def test_grid_to_graph():
93
81
assert A .dtype == np .float64
94
82
95
83
96
- def test_connect_regions (raccoon_face ):
97
- face = raccoon_face . copy ()
84
+ def test_connect_regions (raccoon_face_fxt ):
85
+ face = raccoon_face_fxt
98
86
# subsample by 4 to reduce run time
99
87
face = face [::4 , ::4 ]
100
88
for thr in (50 , 150 ):
@@ -103,8 +91,8 @@ def test_connect_regions(raccoon_face):
103
91
assert ndimage .label (mask )[1 ] == connected_components (graph )[0 ]
104
92
105
93
106
- def test_connect_regions_with_grid (raccoon_face ):
107
- face = raccoon_face . copy ()
94
+ def test_connect_regions_with_grid (raccoon_face_fxt ):
95
+ face = raccoon_face_fxt
108
96
109
97
# subsample by 4 to reduce run time
110
98
face = face [::4 , ::4 ]
@@ -118,33 +106,27 @@ def test_connect_regions_with_grid(raccoon_face):
118
106
assert ndimage .label (mask )[1 ] == connected_components (graph )[0 ]
119
107
120
108
121
- def _downsampled_face ():
122
- if sp_version .release >= parse_version ("1.10" ).release :
123
- pytest .importorskip ("pooch" )
124
- from scipy .datasets import face as raccoon_face
125
- else :
126
- from scipy .misc import face as raccoon_face
127
-
128
- face = raccoon_face (gray = True )
129
- face = face .astype (np .float32 )
109
+ @pytest .fixture
110
+ def downsampled_face (raccoon_face_fxt ):
111
+ face = raccoon_face_fxt
130
112
face = face [::2 , ::2 ] + face [1 ::2 , ::2 ] + face [::2 , 1 ::2 ] + face [1 ::2 , 1 ::2 ]
131
113
face = face [::2 , ::2 ] + face [1 ::2 , ::2 ] + face [::2 , 1 ::2 ] + face [1 ::2 , 1 ::2 ]
132
114
face = face .astype (np .float32 )
133
115
face /= 16.0
134
116
return face
135
117
136
118
137
- def _orange_face (face = None ):
138
- face = _downsampled_face () if face is None else face
119
+ @pytest .fixture
120
+ def orange_face (downsampled_face ):
121
+ face = downsampled_face
139
122
face_color = np .zeros (face .shape + (3 ,))
140
123
face_color [:, :, 0 ] = 256 - face
141
124
face_color [:, :, 1 ] = 256 - face / 2
142
125
face_color [:, :, 2 ] = 256 - face / 4
143
126
return face_color
144
127
145
128
146
- def _make_images (face = None ):
147
- face = _downsampled_face () if face is None else face
129
+ def _make_images (face ):
148
130
# make a collection of faces
149
131
images = np .zeros ((3 ,) + face .shape )
150
132
images [0 ] = face
@@ -153,12 +135,12 @@ def _make_images(face=None):
153
135
return images
154
136
155
137
156
- downsampled_face = _downsampled_face ()
157
- orange_face = _orange_face (downsampled_face )
158
- face_collection = _make_images (downsampled_face )
138
+ @ pytest . fixture
139
+ def downsampled_face_collection (downsampled_face ):
140
+ return _make_images (downsampled_face )
159
141
160
142
161
- def test_extract_patches_all ():
143
+ def test_extract_patches_all (downsampled_face ):
162
144
face = downsampled_face
163
145
i_h , i_w = face .shape
164
146
p_h , p_w = 16 , 16
@@ -167,7 +149,7 @@ def test_extract_patches_all():
167
149
assert patches .shape == (expected_n_patches , p_h , p_w )
168
150
169
151
170
- def test_extract_patches_all_color ():
152
+ def test_extract_patches_all_color (orange_face ):
171
153
face = orange_face
172
154
i_h , i_w = face .shape [:2 ]
173
155
p_h , p_w = 16 , 16
@@ -176,7 +158,7 @@ def test_extract_patches_all_color():
176
158
assert patches .shape == (expected_n_patches , p_h , p_w , 3 )
177
159
178
160
179
- def test_extract_patches_all_rect ():
161
+ def test_extract_patches_all_rect (downsampled_face ):
180
162
face = downsampled_face
181
163
face = face [:, 32 :97 ]
182
164
i_h , i_w = face .shape
@@ -187,7 +169,7 @@ def test_extract_patches_all_rect():
187
169
assert patches .shape == (expected_n_patches , p_h , p_w )
188
170
189
171
190
- def test_extract_patches_max_patches ():
172
+ def test_extract_patches_max_patches (downsampled_face ):
191
173
face = downsampled_face
192
174
i_h , i_w = face .shape
193
175
p_h , p_w = 16 , 16
@@ -205,15 +187,15 @@ def test_extract_patches_max_patches():
205
187
extract_patches_2d (face , (p_h , p_w ), max_patches = - 1.0 )
206
188
207
189
208
- def test_extract_patch_same_size_image ():
190
+ def test_extract_patch_same_size_image (downsampled_face ):
209
191
face = downsampled_face
210
192
# Request patches of the same size as image
211
193
# Should return just the single patch a.k.a. the image
212
194
patches = extract_patches_2d (face , face .shape , max_patches = 2 )
213
195
assert patches .shape [0 ] == 1
214
196
215
197
216
- def test_extract_patches_less_than_max_patches ():
198
+ def test_extract_patches_less_than_max_patches (downsampled_face ):
217
199
face = downsampled_face
218
200
i_h , i_w = face .shape
219
201
p_h , p_w = 3 * i_h // 4 , 3 * i_w // 4
@@ -224,7 +206,7 @@ def test_extract_patches_less_than_max_patches():
224
206
assert patches .shape == (expected_n_patches , p_h , p_w )
225
207
226
208
227
- def test_reconstruct_patches_perfect ():
209
+ def test_reconstruct_patches_perfect (downsampled_face ):
228
210
face = downsampled_face
229
211
p_h , p_w = 16 , 16
230
212
@@ -233,7 +215,7 @@ def test_reconstruct_patches_perfect():
233
215
np .testing .assert_array_almost_equal (face , face_reconstructed )
234
216
235
217
236
- def test_reconstruct_patches_perfect_color ():
218
+ def test_reconstruct_patches_perfect_color (orange_face ):
237
219
face = orange_face
238
220
p_h , p_w = 16 , 16
239
221
@@ -242,14 +224,14 @@ def test_reconstruct_patches_perfect_color():
242
224
np .testing .assert_array_almost_equal (face , face_reconstructed )
243
225
244
226
245
- def test_patch_extractor_fit ():
246
- faces = face_collection
227
+ def test_patch_extractor_fit (downsampled_face_collection ):
228
+ faces = downsampled_face_collection
247
229
extr = PatchExtractor (patch_size = (8 , 8 ), max_patches = 100 , random_state = 0 )
248
230
assert extr == extr .fit (faces )
249
231
250
232
251
- def test_patch_extractor_max_patches ():
252
- faces = face_collection
233
+ def test_patch_extractor_max_patches (downsampled_face_collection ):
234
+ faces = downsampled_face_collection
253
235
i_h , i_w = faces .shape [1 :3 ]
254
236
p_h , p_w = 8 , 8
255
237
@@ -272,15 +254,15 @@ def test_patch_extractor_max_patches():
272
254
assert patches .shape == (expected_n_patches , p_h , p_w )
273
255
274
256
275
- def test_patch_extractor_max_patches_default ():
276
- faces = face_collection
257
+ def test_patch_extractor_max_patches_default (downsampled_face_collection ):
258
+ faces = downsampled_face_collection
277
259
extr = PatchExtractor (max_patches = 100 , random_state = 0 )
278
260
patches = extr .transform (faces )
279
261
assert patches .shape == (len (faces ) * 100 , 19 , 25 )
280
262
281
263
282
- def test_patch_extractor_all_patches ():
283
- faces = face_collection
264
+ def test_patch_extractor_all_patches (downsampled_face_collection ):
265
+ faces = downsampled_face_collection
284
266
i_h , i_w = faces .shape [1 :3 ]
285
267
p_h , p_w = 8 , 8
286
268
expected_n_patches = len (faces ) * (i_h - p_h + 1 ) * (i_w - p_w + 1 )
@@ -289,7 +271,7 @@ def test_patch_extractor_all_patches():
289
271
assert patches .shape == (expected_n_patches , p_h , p_w )
290
272
291
273
292
- def test_patch_extractor_color ():
274
+ def test_patch_extractor_color (orange_face ):
293
275
faces = _make_images (orange_face )
294
276
i_h , i_w = faces .shape [1 :3 ]
295
277
p_h , p_w = 8 , 8
@@ -347,7 +329,7 @@ def test_extract_patches_strided():
347
329
).all ()
348
330
349
331
350
- def test_extract_patches_square ():
332
+ def test_extract_patches_square (downsampled_face ):
351
333
# test same patch size for all dimensions
352
334
face = downsampled_face
353
335
i_h , i_w = face .shape
@@ -366,7 +348,7 @@ def test_width_patch():
366
348
extract_patches_2d (x , (1 , 4 ))
367
349
368
350
369
- def test_patch_extractor_wrong_input ():
351
+ def test_patch_extractor_wrong_input (orange_face ):
370
352
"""Check that an informative error is raised if the patch_size is not valid."""
371
353
faces = _make_images (orange_face )
372
354
err_msg = "patch_size must be a tuple of two integers"
0 commit comments