Skip to content

Commit

Permalink
MAINT: beta parameter fixes
Browse files Browse the repository at this point in the history
- remove `-n-jobs` in favor of `--parallel`
- fix pass-through of beta metric modifier parameters

fixes #23
  • Loading branch information
gregcaporaso committed Aug 9, 2024
1 parent 8b314b0 commit 653c2e1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
50 changes: 35 additions & 15 deletions q2_boots/_beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@

from q2_boots._resample import resample

_METRIC_MOD_DEFAULTS = {
'bypass_tips': False,
'pseudocount': 1,
'alpha': None,
'variance_adjusted': False
}


def beta_average(data: skbio.DistanceMatrix,
average_method: str) -> skbio.DistanceMatrix:
Expand All @@ -42,13 +49,19 @@ def beta_average(data: skbio.DistanceMatrix,
"mean, and medoid.")


def beta_collection(ctx, table, metric, sampling_depth, n, replacement,
phylogeny=None, bypass_tips=False, n_jobs=1,
pseudocount=1, alpha=None, variance_adjusted=False):
def beta_collection(
ctx, table, metric, sampling_depth, n, replacement,
phylogeny=None,
bypass_tips=_METRIC_MOD_DEFAULTS['bypass_tips'],
pseudocount=_METRIC_MOD_DEFAULTS['pseudocount'],
alpha=_METRIC_MOD_DEFAULTS['alpha'],
variance_adjusted=_METRIC_MOD_DEFAULTS['variance_adjusted']):
_validate_beta_metric(metric, phylogeny)

resample_function = resample
beta_metric_function = _get_beta_metric_function(ctx, metric, phylogeny)
beta_metric_function = _get_beta_metric_function(
ctx, metric, phylogeny, bypass_tips, pseudocount, alpha,
variance_adjusted)

tables = resample_function(ctx=ctx,
table=table,
Expand All @@ -61,8 +74,11 @@ def beta_collection(ctx, table, metric, sampling_depth, n, replacement,


def beta(ctx, table, metric, sampling_depth, n, replacement,
average_method='non-metric-median', phylogeny=None, bypass_tips=False,
n_jobs=1, pseudocount=1, alpha=None, variance_adjusted=False):
average_method='non-metric-median', phylogeny=None,
bypass_tips=_METRIC_MOD_DEFAULTS['bypass_tips'],
pseudocount=_METRIC_MOD_DEFAULTS['pseudocount'],
alpha=_METRIC_MOD_DEFAULTS['alpha'],
variance_adjusted=_METRIC_MOD_DEFAULTS['variance_adjusted']):
beta_collection_function = beta_collection
beta_average_action = ctx.get_action('boots', 'beta_average')
dms = beta_collection_function(ctx=ctx,
Expand All @@ -73,7 +89,6 @@ def beta(ctx, table, metric, sampling_depth, n, replacement,
n=n,
pseudocount=pseudocount,
replacement=replacement,
n_jobs=n_jobs,
variance_adjusted=variance_adjusted,
alpha=alpha,
bypass_tips=bypass_tips)
Expand Down Expand Up @@ -114,18 +129,23 @@ def _validate_beta_metric(metric, phylogeny):
raise ValueError(f'Metric {metric} requires a phylogenetic tree.')


def _get_beta_metric_function(ctx, metric, phylogeny):
def _get_beta_metric_function(
ctx, metric, phylogeny,
bypass_tips=_METRIC_MOD_DEFAULTS['bypass_tips'],
pseudocount=_METRIC_MOD_DEFAULTS['pseudocount'],
alpha=_METRIC_MOD_DEFAULTS['alpha'],
variance_adjusted=_METRIC_MOD_DEFAULTS['variance_adjusted']):
if _is_phylogenetic_beta_metric(metric):
beta_metric_function = q2div_beta_phylogenetic
beta_metric_function = functools.partial(beta_metric_function,
ctx=ctx,
phylogeny=phylogeny,
metric=metric)
beta_metric_function = functools.partial(
beta_metric_function, ctx=ctx, phylogeny=phylogeny, metric=metric,
bypass_tips=bypass_tips, alpha=alpha,
variance_adjusted=variance_adjusted)
else:
beta_metric_function = q2div_beta
beta_metric_function = functools.partial(beta_metric_function,
ctx=ctx,
metric=metric)
beta_metric_function = functools.partial(
beta_metric_function, ctx=ctx, metric=metric,
pseudocount=pseudocount)
return beta_metric_function


Expand Down
2 changes: 1 addition & 1 deletion q2_boots/_core_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def core_metrics(ctx, table, sampling_depth, metadata, n, replacement,
n_jobs=1, phylogeny=None, alpha_average_method='median',
phylogeny=None, alpha_average_method='median',
beta_average_method='non-metric-median'):

resample_function = resample
Expand Down
16 changes: 2 additions & 14 deletions q2_boots/plugin_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------

from qiime2.plugin import (Plugin, Int, Range, Collection,
Str, Choices, Bool, Float, Threads,
Metadata, Visualization)
from qiime2.plugin import (Plugin, Int, Range, Collection, Str, Choices, Bool,
Float, Metadata, Visualization)

from q2_types.feature_table import (
FeatureTable, Frequency, RelativeFrequency, PresenceAbsence
Expand Down Expand Up @@ -252,27 +251,18 @@
beta_metrics['PHYLO']['UNIMPL']),
'pseudocount': Int % Range(1, None),
'replacement': Bool,
'n_jobs': Threads,
'n': Int % Range(1, None),
'sampling_depth': Int % Range(1, None),
'bypass_tips': Bool,
'variance_adjusted': Bool,
'alpha': Float % Range(0, 1, inclusive_end=True)
}

_n_jobs_description = (
'The number of CPU threads to use in performing this calculation. '
'May not exceed the number of available physical cores. If threads = '
'\'auto\', one thread will be created for each identified CPU core on the '
'host.'
)

_beta_collection_parameter_descriptions = {
'metric': 'The beta diversity metric to be computed.',
'pseudocount': ('A pseudocount to handle zeros for compositional '
'metrics. This is ignored for other metrics.'),
'replacement': _replacement_description,
'n_jobs': _n_jobs_description,
'n': _n_description,
'sampling_depth': _sampling_depth_description,
'bypass_tips': ('Ignore tips of tree in phylogenetic diversity '
Expand Down Expand Up @@ -341,7 +331,6 @@
inputs=_diversity_inputs,
parameters={
'metadata': Metadata,
'n_jobs': Threads,
'n': Int % Range(1, None),
'sampling_depth': Int % Range(1, None),
'alpha_average_method': Str % Choices('mean', 'median'),
Expand All @@ -360,7 +349,6 @@
input_descriptions=_diversity_input_descriptions,
parameter_descriptions={
'metadata': 'The sample metadata used in generating Emperor plots.',
'n_jobs': _n_jobs_description,
'n': _n_description,
'sampling_depth': _sampling_depth_description,
'alpha_average_method': 'Method to use for averaging alpha diversity.',
Expand Down
3 changes: 0 additions & 3 deletions q2_boots/tests/test_core_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def test_core_metrics_wo_replacement(self):
output = self.core_metrics(table=self.table1,
sampling_depth=2,
metadata=self.metadata,
n_jobs=1,
replacement=False,
n=10)
# n tables returned
Expand Down Expand Up @@ -78,7 +77,6 @@ def test_core_metrics_w_replacement(self):
output = self.core_metrics(table=self.table1,
sampling_depth=2,
metadata=self.metadata,
n_jobs=1,
replacement=True,
n=99)
# n tables returned
Expand Down Expand Up @@ -146,7 +144,6 @@ def test_core_metrics_phylogenetic(self):
phylogeny=self.phylogeny,
sampling_depth=2,
metadata=self.metadata,
n_jobs=1,
replacement=False,
n=10)
# n tables returned
Expand Down

0 comments on commit 653c2e1

Please sign in to comment.