Skip to content

Commit cb637aa

Browse files
authoredFeb 13, 2024
MAINT: add threads/jobs primitives (#62)
1 parent 5a1b4a6 commit cb637aa

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed
 

‎q2_diversity_lib/_util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _validate_requested_cpus(wrapped_function, *args, **kwargs):
104104

105105
cpus_requested = b_a_arguments[param_name]
106106

107-
if cpus_requested == 'auto':
107+
if cpus_requested == 0:
108108
# mutate bound_arguments.arguments 'auto' to the requested # of cpus...
109109
b_a_arguments[param_name] = cpus_available
110110
# ...and update cpus requested to prevent TypeError

‎q2_diversity_lib/plugin_setup.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# ----------------------------------------------------------------------------
88

99
from qiime2.plugin import (Plugin, Citations, Bool, Int, Range, Choices, Str,
10-
Float, List)
10+
Float, List, Threads)
1111
from q2_types.feature_table import (FeatureTable, Frequency, RelativeFrequency,
1212
PresenceAbsence)
1313
from q2_types.tree import Phylogeny, Rooted
@@ -56,7 +56,7 @@
5656
inputs={'table': FeatureTable[Frequency | RelativeFrequency
5757
| PresenceAbsence],
5858
'phylogeny': Phylogeny[Rooted]},
59-
parameters={'threads': Int % Range(1, None) | Str % Choices(['auto'])},
59+
parameters={'threads': Threads},
6060
outputs=[('vector', SampleData[AlphaDiversity])],
6161
input_descriptions={
6262
'table': "The feature table containing the samples for which Faith's "
@@ -146,7 +146,7 @@
146146
plugin.methods.register_function(
147147
function=beta.bray_curtis,
148148
inputs={'table': FeatureTable[Frequency | RelativeFrequency]},
149-
parameters={'n_jobs': Int % Range(1, None) | Str % Choices(['auto'])},
149+
parameters={'n_jobs': Threads},
150150
outputs=[('distance_matrix', DistanceMatrix)],
151151
input_descriptions={
152152
'table': "The feature table containing the samples for which "
@@ -174,7 +174,7 @@
174174
function=beta.jaccard,
175175
inputs={'table': FeatureTable[Frequency | RelativeFrequency
176176
| PresenceAbsence]},
177-
parameters={'n_jobs': Int % Range(1, None) | Str % Choices(['auto'])},
177+
parameters={'n_jobs': Threads},
178178
outputs=[('distance_matrix', DistanceMatrix)],
179179
input_descriptions={
180180
'table': "The feature table containing the samples for which "
@@ -202,7 +202,7 @@
202202
inputs={'table': FeatureTable[Frequency | RelativeFrequency
203203
| PresenceAbsence],
204204
'phylogeny': Phylogeny[Rooted]},
205-
parameters={'threads': Int % Range(1, None) | Str % Choices(['auto']),
205+
parameters={'threads': Threads,
206206
'bypass_tips': Bool},
207207
outputs=[('distance_matrix', DistanceMatrix)],
208208
input_descriptions={
@@ -246,7 +246,7 @@
246246
function=beta.weighted_unifrac,
247247
inputs={'table': FeatureTable[Frequency | RelativeFrequency],
248248
'phylogeny': Phylogeny[Rooted]},
249-
parameters={'threads': Int % Range(1, None) | Str % Choices(['auto']),
249+
parameters={'threads': Threads,
250250
'bypass_tips': Bool},
251251
outputs=[('distance_matrix', DistanceMatrix)],
252252
input_descriptions={
@@ -310,8 +310,7 @@
310310
function=beta.beta_passthrough,
311311
inputs={'table': FeatureTable[Frequency]},
312312
parameters={'metric': Str % Choices(beta.METRICS['NONPHYLO']['UNIMPL']),
313-
'pseudocount': Int % Range(1, None),
314-
'n_jobs': Int % Range(1, None) | Str % Choices(['auto'])},
313+
'pseudocount': Int % Range(1, None), 'n_jobs': Threads},
315314
outputs=[('distance_matrix', DistanceMatrix)],
316315
input_descriptions={
317316
'table': 'The feature table containing the samples over which beta '
@@ -347,7 +346,7 @@
347346
inputs={'table': FeatureTable[Frequency],
348347
'phylogeny': Phylogeny[Rooted]},
349348
parameters={'metric': Str % Choices(beta.METRICS['PHYLO']['UNIMPL']),
350-
'threads': Int % Range(1, None) | Str % Choices(['auto']),
349+
'threads': Threads,
351350
'variance_adjusted': Bool,
352351
'alpha': Float % Range(0, 1, inclusive_end=True),
353352
'bypass_tips': Bool},
@@ -412,13 +411,12 @@
412411
]
413412
)
414413

415-
416414
plugin.methods.register_function(
417415
function=beta.beta_phylogenetic_meta_passthrough,
418416
inputs={'tables': List[FeatureTable[Frequency]],
419417
'phylogenies': List[Phylogeny[Rooted]]},
420418
parameters={'metric': Str % Choices(beta.METRICS['PHYLO']['UNIMPL']),
421-
'threads': Int % Range(1, None) | Str % Choices(['auto']),
419+
'threads': Threads,
422420
'variance_adjusted': Bool,
423421
'alpha': Float % Range(0, 1, inclusive_end=True),
424422
'bypass_tips': Bool,

‎q2_diversity_lib/tests/test_util.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,13 @@ def test_n_jobs_passed_as_default(self, mock_process):
234234
self.assertEqual(self.function_w_n_jobs_param(), 3)
235235

236236
@mock.patch("q2_diversity_lib._util.psutil.Process")
237-
def test_auto_passed_to_cpu_request(self, mock_process):
237+
def test_zero_passed_to_cpu_request(self, mock_process):
238238
mock_process = psutil.Process()
239239
mock_process.cpu_affinity = mock.MagicMock(return_value=[0, 1, 2])
240-
self.assertEqual(self.function_w_n_jobs_param('auto'), 3)
241-
self.assertEqual(self.function_w_n_jobs_param(n_jobs='auto'), 3)
242-
self.assertEqual(self.function_w_threads_param('auto'), 3)
243-
self.assertEqual(self.function_w_threads_param(threads='auto'), 3)
240+
self.assertEqual(self.function_w_n_jobs_param(0), 3)
241+
self.assertEqual(self.function_w_n_jobs_param(n_jobs=0), 3)
242+
self.assertEqual(self.function_w_threads_param(0), 3)
243+
self.assertEqual(self.function_w_threads_param(threads=0), 3)
244244

245245
@mock.patch("q2_diversity_lib._util.psutil.Process")
246246
def test_cpu_request_through_framework(self, mock_process):

0 commit comments

Comments
 (0)
Please sign in to comment.