Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly allow setting a random seed for subsample #916

Merged
merged 8 commits into from
Apr 4, 2023
Prev Previous commit
Next Next commit
API: allow setting a seed with subsample
  • Loading branch information
wasade committed Mar 24, 2023
commit 65452172be331882cd7e639023e2025245fe9593
8 changes: 7 additions & 1 deletion biom/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -2871,7 +2871,8 @@ def max(self, axis='sample'):

return max_val

def subsample(self, n, axis='sample', by_id=False, with_replacement=False):
def subsample(self, n, axis='sample', by_id=False, with_replacement=False,
seed=None):
"""Randomly subsample without replacement.

Parameters
Expand All @@ -2889,6 +2890,8 @@ def subsample(self, n, axis='sample', by_id=False, with_replacement=False):
If `False` (default), subsample without replacement. If `True`,
resample with replacement via the multinomial distribution.
Should not be `True` if `by_id` is `True`.
seed : int, optional
If provided, set the numpy random seed with this value

Returns
-------
Expand Down Expand Up @@ -2941,6 +2944,9 @@ def subsample(self, n, axis='sample', by_id=False, with_replacement=False):
[('S1', 'S2'), ('S1', 'S3'), ('S2', 'S3')]

"""
if seed is not None:
np.random.seed(seed)

if n < 0:
raise ValueError("n cannot be negative.")

Expand Down