Skip to content

Commit 5450b72

Browse files
committed
format code
1 parent 9c05224 commit 5450b72

File tree

5 files changed

+63
-37
lines changed

5 files changed

+63
-37
lines changed

pypermut/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
""" PyPermut """
22

3-
__version__ = "0.1.0"
3+
__version__ = "0.2.0"

pypermut/core.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ def permute_measurements(Y, x,
6262
perms, n_perms, with_replacement = helpers._check_permutations(
6363
n_perms_requested=n_permutations,
6464
n_perms_max=n_perms_max,
65-
with_replacement=with_replacement)
65+
with_replacement=with_replacement,
66+
)
6667

6768
# loop on permutations to sample the null distribution
6869
null_dist = np.empty(n_perms, dtype=float)
@@ -73,7 +74,8 @@ def permute_measurements(Y, x,
7374
else:
7475
permuted_indices = get_permutation_measurements(
7576
n_meas,
76-
perms[i_perm])
77+
perms[i_perm],
78+
)
7779
xperm = x[permuted_indices] # permute x along measurements
7880

7981
stat = stat_func(np.c_[xperm, Y])
@@ -144,7 +146,8 @@ def permute_paired_samples(X, Y,
144146
perms, n_perms, with_replacement = helpers._check_permutations(
145147
n_perms_requested=n_permutations,
146148
n_perms_max=n_perms_max,
147-
with_replacement=with_replacement)
149+
with_replacement=with_replacement,
150+
)
148151

149152
# loop on permutations to sample the null distribution
150153
null_dist = np.empty(n_perms, dtype=float)
@@ -158,7 +161,8 @@ def permute_paired_samples(X, Y,
158161
else:
159162
perm_coeffs = get_permutation_2_paired_samples(
160163
n_meas,
161-
perms[i_perm])
164+
perms[i_perm],
165+
)
162166
# permute each pair: apply random coeffs -1 or +1 on differences
163167
Dperm = np.tile(perm_coeffs, D.shape[1]) * D
164168

@@ -233,7 +237,8 @@ def permute_unpaired_samples(args,
233237
perms, n_perms, with_replacement = helpers._check_permutations(
234238
n_perms_requested=n_permutations,
235239
n_perms_max=n_perms_max,
236-
with_replacement=with_replacement)
240+
with_replacement=with_replacement,
241+
)
237242
if not with_replacement:
238243
if len(args) != 2:
239244
raise ValueError('Without replacement strategy is available only '
@@ -249,7 +254,8 @@ def permute_unpaired_samples(args,
249254
else:
250255
permuted_indices = get_permutation_unpaired_samples(
251256
list_meas,
252-
list(combs[perms[i_perm]]))
257+
list(combs[perms[i_perm]]),
258+
)
253259
Cperm = C[permuted_indices] # permute measurements between samples
254260

255261
stat = stat_func(Cperm, list_meas)
@@ -406,8 +412,10 @@ def get_permutation_2_paired_samples(n_meas, perm_number):
406412
raise IndexError('Permutation number {} is out of range [0 ... {}].'
407413
.format(perm_number, 2**n_meas))
408414
# transform number into binary
409-
bin_coeffs = np.fromiter(np.binary_repr(perm_number, width=n_meas),
410-
dtype=int)
415+
bin_coeffs = np.fromiter(
416+
np.binary_repr(perm_number, width=n_meas),
417+
dtype=int,
418+
)
411419
# transform binaries 0/1 into coefficients 1/-1
412420
perm_coeffs = 1 - 2 * np.array(bin_coeffs)[:, np.newaxis]
413421

pypermut/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ def _check_permutations(n_perms_requested, n_perms_max, with_replacement):
6464
perms = np.random.choice(
6565
np.arange(0, n_perms_max),
6666
size=n_perms,
67-
replace=False)
67+
replace=False,
68+
)
6869
else:
6970
# => permutation test, using bootstrap with replacement
7071
perms = []

pypermut/stats.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import warnings
88
import numpy as np
9-
import scipy.stats as stats
9+
from scipy.stats import percentileofscore
1010
from . import helpers
1111
from . import core
1212
from . import mstats
@@ -129,7 +129,8 @@ def permutation_corr(Y, *,
129129
with_replacement,
130130
stat_func=corr_func,
131131
var_func=np.max,
132-
side=side)
132+
side=side,
133+
)
133134

134135
# compute the real R statistics
135136
Rstats = Rstats_ = corr_func(np.c_[x, Y])
@@ -138,8 +139,9 @@ def permutation_corr(Y, *,
138139
# compare them to the Rmax distribution with a right-sided test,
139140
# because significance is obtained for high R stats
140141
pvals = np.array([
141-
(100 - stats.percentileofscore(Rmax, R, kind='strict')) / 100
142-
for R in Rstats_])
142+
(100 - percentileofscore(Rmax, R, kind='strict')) / 100
143+
for R in Rstats_
144+
])
143145

144146
if return_dist:
145147
return Rstats, pvals, Rmax
@@ -223,7 +225,8 @@ def permutation_ttest_rel(X, Y, *,
223225
with_replacement=with_replacement,
224226
stat_func=mstats.studentt_rel,
225227
var_func=np.max,
226-
side=side)
228+
side=side,
229+
)
227230

228231
# compute the real t statistics
229232
tstats = tstats_ = mstats.studentt_rel(X - Y)
@@ -232,8 +235,9 @@ def permutation_ttest_rel(X, Y, *,
232235
# compare them to the tmax distribution with a right-sided test,
233236
# because significance is obtained for high t stats
234237
pvals = np.array([
235-
(100 - stats.percentileofscore(tmax, t, kind='strict')) / 100
236-
for t in tstats_])
238+
(100 - percentileofscore(tmax, t, kind='strict')) / 100
239+
for t in tstats_
240+
])
237241

238242
if return_dist:
239243
return tstats, pvals, tmax
@@ -320,18 +324,22 @@ def permutation_ttest_ind(X, Y, *,
320324
with_replacement=with_replacement,
321325
stat_func=stat_func,
322326
var_func=np.max,
323-
side=side)
327+
side=side,
328+
)
324329

325330
# compute the real t statistics
326-
tstats = tstats_ = stat_func(np.concatenate((X, Y), axis=0),
327-
[X.shape[0], Y.shape[0]])
331+
tstats = tstats_ = stat_func(
332+
np.concatenate((X, Y), axis=0),
333+
[X.shape[0], Y.shape[0]],
334+
)
328335
if side == 'two':
329336
tstats_ = np.abs(tstats_)
330337
# compare it to the tmax distribution with a right-sided test,
331338
# because significance is obtained for high t stats
332339
pvals = np.array([
333-
(100 - stats.percentileofscore(tmax, t, kind='strict')) / 100
334-
for t in tstats_])
340+
(100 - percentileofscore(tmax, t, kind='strict')) / 100
341+
for t in tstats_
342+
])
335343

336344
if return_dist:
337345
return tstats, pvals, tmax
@@ -407,14 +415,16 @@ def permutation_wilcoxon(X, Y, *,
407415
stat_func=mstats.wilcoxon,
408416
var_func=np.min,
409417
side='one',
410-
zero_method=zero_method)
418+
zero_method=zero_method,
419+
)
411420

412421
# compute the real T statistics
413422
Tstats = mstats.wilcoxon(X - Y, zero_method)
414423
# compare it to the Tmin distribution with a left-sided test,
415424
# because significance is obtained for low T stats
416425
pvals = np.array([
417-
stats.percentileofscore(Tmin, T, kind='weak') / 100 for T in Tstats])
426+
percentileofscore(Tmin, T, kind='weak') / 100 for T in Tstats
427+
])
418428

419429
if return_dist:
420430
return Tstats, pvals, Tmin
@@ -482,15 +492,19 @@ def permutation_mannwhitneyu(X, Y, *,
482492
with_replacement=with_replacement,
483493
stat_func=mstats.mannwhitneyu,
484494
var_func=np.min,
485-
side='one')
495+
side='one',
496+
)
486497

487498
# compute the real U statistics
488-
Ustats = mstats.mannwhitneyu(np.concatenate((X, Y), axis=0),
489-
[X.shape[0], Y.shape[0]])
499+
Ustats = mstats.mannwhitneyu(
500+
np.concatenate((X, Y), axis=0),
501+
[X.shape[0], Y.shape[0]],
502+
)
490503
# compare it to the Umin distribution with a left-sided test,
491504
# because significance is obtained for low U stats
492505
pvals = np.array([
493-
stats.percentileofscore(Umin, U, kind='weak') / 100 for U in Ustats])
506+
percentileofscore(Umin, U, kind='weak') / 100 for U in Ustats
507+
])
494508

495509
if return_dist:
496510
return Ustats, pvals, Umin
@@ -550,7 +564,8 @@ def permutation_f_oneway(*args, n=10000, return_dist=False):
550564
with_replacement=True,
551565
stat_func=mstats.f_oneway,
552566
var_func=np.max,
553-
side='one')
567+
side='one',
568+
)
554569

555570
# number of measurements for each sample / group
556571
list_meas = np.asarray(list(map(len, args)))
@@ -559,8 +574,8 @@ def permutation_f_oneway(*args, n=10000, return_dist=False):
559574
# compare it to the Fmax distribution with a right-sided test,
560575
# because significance is obtained for high F stats
561576
pvals = np.array([
562-
(100 - stats.percentileofscore(Fmax, F, kind='strict')) / 100
563-
for F in Fstats])
577+
(100 - percentileofscore(Fmax, F, kind='strict')) / 100 for F in Fstats
578+
])
564579

565580
if return_dist:
566581
return Fstats, pvals, Fmax
@@ -621,16 +636,17 @@ def permutation_kruskal(*args, n=10000, return_dist=False):
621636
with_replacement=True,
622637
stat_func=mstats.kruskal,
623638
var_func=np.max,
624-
side='one')
639+
side='one',
640+
)
625641

626642
list_meas = np.asarray(list(map(len, args)))
627643
# compute the real H statistics
628644
Hstats = mstats.kruskal(np.concatenate(args, axis=0), list_meas)
629645
# compare it to the Hmax distribution with a right-sided test,
630646
# because significance is obtained for high H stats
631647
pvals = np.array([
632-
(100 - stats.percentileofscore(Hmax, H, kind='strict')) / 100
633-
for H in Hstats])
648+
(100 - percentileofscore(Hmax, H, kind='strict')) / 100 for H in Hstats
649+
])
634650

635651
if return_dist:
636652
return Hstats, pvals, Hmax
@@ -714,8 +730,9 @@ def permutation_friedmanchisquare(*args, n=10000, return_dist=False):
714730
# compare it to the chimax distribution with a right-sided test,
715731
# because significance is obtained for high chi2 stats
716732
pvals = np.array([
717-
(100 - stats.percentileofscore(chi2max, chi2, kind='strict')) / 100
718-
for chi2 in chi2stats])
733+
(100 - percentileofscore(chi2max, chi2, kind='strict')) / 100
734+
for chi2 in chi2stats
735+
])
719736

720737
if return_dist:
721738
return chi2stats, pvals, chi2max

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pypermut"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
description = "PyPermut is a Python package implementing permutation tests"
55
authors = ["Quentin Barthelemy", "Anonymous", "Louis Mayaud"]
66
license = "BSD-3-Clause"

0 commit comments

Comments
 (0)