@@ -45,14 +45,43 @@ def background_array():
45
45
# FIXME: This isn't a very good example
46
46
@pytest .mark .slow
47
47
@pytest .mark .parametrize (
48
- "free_cpus " ,
48
+ "cpus_to_leave_available " ,
49
49
[
50
- pytest .param ("no_free_cpus" , id = "No free CPUs " ),
51
- pytest .param ("run_on_one_cpu_only" , id = "One CPU" ),
50
+ pytest .param (0 , id = "Leave no CPUS free " ),
51
+ pytest .param (- 1 , id = "Only use one CPU" ),
52
52
],
53
53
)
54
- def test_detection_full (signal_array , background_array , free_cpus , request ):
55
- n_free_cpus = request .getfixturevalue (free_cpus )
54
+ def test_detection_full (
55
+ signal_array , background_array , cpus_to_leave_available : int
56
+ ):
57
+ """
58
+ cpus_to_leave_available is interpreted as follows:
59
+
60
+ - For values >=0, this is the number of CPUs to leave available
61
+ to the system when running this test.
62
+ - For values <0, this is HOW MANY CPUS to request be used to
63
+ run the test.
64
+
65
+ In each case, we check that we will be running on at least one CPU,
66
+ and not requesting more CPUs than the system can provide.
67
+ """
68
+ # Determine the number of CPUs to leave available
69
+ system_cpus = os .cpu_count ()
70
+ # How many CPUs do we want to leave free?
71
+ if cpus_to_leave_available >= 0 :
72
+ n_free_cpus = cpus_to_leave_available
73
+ else :
74
+ # Number of CPUs to keep free is <0, interpret as
75
+ # number of CPUs _to use_. Thus;
76
+ # n_free_cpus = system_cpus - |cpus_to_leave_available|
77
+ n_free_cpus = system_cpus - abs (cpus_to_leave_available )
78
+ # Check that there are enough CPUs
79
+ if not 0 <= n_free_cpus < system_cpus :
80
+ RuntimeError (
81
+ f"Not enough CPUS available (you want to leave { n_free_cpus } "
82
+ f"available, but there are only { system_cpus } on the system)."
83
+ )
84
+
56
85
cells_test = main (
57
86
signal_array ,
58
87
background_array ,
@@ -80,10 +109,13 @@ def test_detection_full(signal_array, background_array, free_cpus, request):
80
109
81
110
82
111
def test_detection_small_planes (
83
- signal_array , background_array , no_free_cpus , mocker
112
+ signal_array ,
113
+ background_array ,
114
+ mocker ,
115
+ cpus_to_leave_free : int = 0 ,
84
116
):
85
117
# Check that processing works when number of planes < number of processes
86
- nproc = get_num_processes (no_free_cpus )
118
+ nproc = get_num_processes (cpus_to_leave_free )
87
119
n_planes = 2
88
120
89
121
# Don't want to bother classifying in this test, so mock classifcation
@@ -100,11 +132,13 @@ def test_detection_small_planes(
100
132
background_array [0 :n_planes ],
101
133
voxel_sizes ,
102
134
ball_z_size = 5 ,
103
- n_free_cpus = no_free_cpus ,
135
+ n_free_cpus = cpus_to_leave_free ,
104
136
)
105
137
106
138
107
- def test_callbacks (signal_array , background_array , no_free_cpus ):
139
+ def test_callbacks (
140
+ signal_array , background_array , cpus_to_leave_free : int = 0
141
+ ):
108
142
# 20 is minimum number of planes needed to find > 0 cells
109
143
signal_array = signal_array [0 :20 ]
110
144
background_array = background_array [0 :20 ]
@@ -129,7 +163,7 @@ def detect_finished_callback(points):
129
163
detect_callback = detect_callback ,
130
164
classify_callback = classify_callback ,
131
165
detect_finished_callback = detect_finished_callback ,
132
- n_free_cpus = no_free_cpus ,
166
+ n_free_cpus = cpus_to_leave_free ,
133
167
)
134
168
135
169
np .testing .assert_equal (planes_done , np .arange (len (signal_array )))
@@ -147,13 +181,13 @@ def test_floating_point_error(signal_array, background_array):
147
181
main (signal_array , background_array , voxel_sizes )
148
182
149
183
150
- def test_synthetic_data (synthetic_bright_spots , no_free_cpus ):
184
+ def test_synthetic_data (synthetic_bright_spots , cpus_to_leave_free : int = 0 ):
151
185
signal_array , background_array = synthetic_bright_spots
152
186
detected = main (
153
187
signal_array ,
154
188
background_array ,
155
189
voxel_sizes ,
156
- n_free_cpus = no_free_cpus ,
190
+ n_free_cpus = cpus_to_leave_free ,
157
191
)
158
192
assert len (detected ) == 8
159
193
0 commit comments