Skip to content

Commit

Permalink
add random_seed to permute_data (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
amnona committed Jul 25, 2020
1 parent 5ad5b2a commit 8aecfea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Incompatible changes:
* Other backwards incompatible function API changes and code refactoring.

New features:
* Add random_seed option to tranforming.permute_data()
* Add bad_color parameter to heatmap() and derivative functions
* Add more methods for MS1Experiment
* Add q-values (correted p-values) to dsfdr and derivative functions. This is manifested in a new feature_metadata field ("qval") for results of diff_abundance() / correlation()
Expand Down
12 changes: 10 additions & 2 deletions calour/transforming.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def log_n(exp: Experiment, n=1, inplace=False) -> Experiment:
return exp


def permute_data(exp: Experiment, normalize=True, inplace=False) -> Experiment:
def permute_data(exp: Experiment, normalize=True, inplace=False, random_seed=None) -> Experiment:
'''Shuffle independently the abundances of each feature.
This creates a new experiment with no dependency between features.
Expand All @@ -259,19 +259,27 @@ def permute_data(exp: Experiment, normalize=True, inplace=False) -> Experiment:
normalize : bool, optional
True (default) to normalize each sample after completing the feature shuffling.
False to not normalize
random_seed : int, np.radnom.Generator instance or None, optional, default=None
set the random number generator seed for the random permutations
If int, random_seed is the seed used by the random number generator;
If Generator instance, random_seed is set to the random number generator;
If None, then fresh, unpredictable entropy will be pulled from the OS
Returns
-------
Experiment
With each feature shuffled independently
'''
# create the numpy.random.Generator
rng = np.random.default_rng(random_seed)

if not inplace:
exp = deepcopy(exp)

exp.sparse = False
for cfeature in range(exp.shape[1]):
np.random.shuffle(exp.data[:, cfeature])
rng.shuffle(exp.data[:, cfeature])
if normalize:
exp.normalize(np.mean(exp.data.sum(axis=1)), inplace=True)
return exp
Expand Down

0 comments on commit 8aecfea

Please sign in to comment.