Skip to content

Commit

Permalink
Merge pull request #230 from scottclowe/api_rename-maxes
Browse files Browse the repository at this point in the history
API: Rename maxiter and maxtries to max_iter and max_tries
  • Loading branch information
scottclowe committed Jul 9, 2021
2 parents 9b3c202 + b47e5aa commit b9f8b79
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 43 deletions.
40 changes: 20 additions & 20 deletions fissa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def separate_trials(
raw,
roi_label=None,
alpha=0.1,
maxiter=20000,
max_iter=20000,
tol=1e-4,
maxtries=1,
max_tries=1,
method="nmf",
verbosity=1,
):
Expand Down Expand Up @@ -147,7 +147,7 @@ def separate_trials(
.. versionadded:: 1.0.0
maxiter : int, optional
max_iter : int, optional
Number of maximally allowed iterations.
.. versionadded:: 1.0.0
Expand All @@ -157,7 +157,7 @@ def separate_trials(
.. versionadded:: 1.0.0
maxtries : int, optional
max_tries : int, optional
Maximum number of tries before algorithm should terminate.
.. versionadded:: 1.0.0
Expand Down Expand Up @@ -208,9 +208,9 @@ def separate_trials(
Xsep, Xmatch, Xmixmat, convergence = npil.separate(
X,
method,
maxiter=maxiter,
max_iter=max_iter,
tol=tol,
maxtries=maxtries,
max_tries=max_tries,
alpha=alpha,
verbosity=verbosity,
)
Expand Down Expand Up @@ -292,7 +292,7 @@ class Experiment:
Sparsity regularizaton weight for NMF algorithm. Set to zero to
remove regularization. Default is ``0.1``.
maxiter : int, optional
max_iter : int, optional
Number of maximally allowed iterations of separation algorithm.
.. versionadded:: 1.0.0
Expand All @@ -302,7 +302,7 @@ class Experiment:
.. versionadded:: 1.0.0
maxtries : int, optional
max_tries : int, optional
Maximum number of tries before separation algorithm should terminate.
.. versionadded:: 1.0.0
Expand Down Expand Up @@ -495,9 +495,9 @@ def __init__(
nRegions=4,
expansion=1,
alpha=0.1,
maxiter=20000,
max_iter=20000,
tol=1e-4,
maxtries=1,
max_tries=1,
ncores_preparation=None,
ncores_separation=None,
method="nmf",
Expand Down Expand Up @@ -542,9 +542,9 @@ def __init__(
self.nRegions = nRegions
self.expansion = expansion
self.alpha = alpha
self.maxiter = maxiter
self.max_iter = max_iter
self.tol = tol
self.maxtries = maxtries
self.max_tries = max_tries
self.nTrials = len(self.images) # number of trials
self.ncores_preparation = ncores_preparation
self.ncores_separation = ncores_separation
Expand Down Expand Up @@ -583,9 +583,9 @@ def __str__(self):
"nRegions",
"expansion",
"alpha",
"maxiter",
"max_iter",
"tol",
"maxtries",
"max_tries",
"ncores_preparation",
"ncores_separation",
"method",
Expand All @@ -611,9 +611,9 @@ def __repr__(self):
"nRegions",
"expansion",
"alpha",
"maxiter",
"max_iter",
"tol",
"maxtries",
"max_tries",
"ncores_preparation",
"ncores_separation",
"method",
Expand Down Expand Up @@ -952,9 +952,9 @@ def separate(self, redo_prep=False, redo_sep=False):
_separate_cfg = functools.partial(
separate_trials,
alpha=self.alpha,
maxiter=self.maxiter,
max_iter=self.max_iter,
tol=self.tol,
maxtries=self.maxtries,
max_tries=self.max_tries,
method=self.method,
verbosity=self.verbosity - 2,
)
Expand All @@ -981,9 +981,9 @@ def separate(self, redo_prep=False, redo_sep=False):
self.raw,
range(n_roi),
itertools.repeat(self.alpha, n_roi),
itertools.repeat(self.maxiter, n_roi),
itertools.repeat(self.max_iter, n_roi),
itertools.repeat(self.tol, n_roi),
itertools.repeat(self.maxtries, n_roi),
itertools.repeat(self.max_tries, n_roi),
itertools.repeat(self.method, n_roi),
itertools.repeat(self.verbosity, n_roi),
),
Expand Down
34 changes: 21 additions & 13 deletions fissa/neuropil.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def separate(
S,
sep_method="nmf",
n=None,
maxiter=10000,
max_iter=10000,
tol=1e-4,
random_state=892,
maxtries=10,
max_tries=10,
W0=None,
H0=None,
alpha=0.1,
Expand Down Expand Up @@ -50,16 +50,24 @@ def separate(
method, ``n`` is the number of input signals; for the ICA method,
we use PCA to estimate how many components would explain at least 99%
of the variance and adopt this value for ``n``.
maxiter : int, optional
max_iter : int, optional
Number of maximally allowed iterations. Default is ``10000``.
.. versionchanged:: 1.0.0
Argument `maxiter` renamed to `max_iter`.
tol : float, optional
Error tolerance for termination. Default is ``1e-4``.
random_state : int or None, optional
Initial state for the random number generator. Set to ``None`` to use
the numpy.random default. Default seed is ``892``.
maxtries : int, optional
max_tries : int, optional
Maximum number of tries before algorithm should terminate.
Default is ``10``.
.. versionchanged:: 1.0.0
Argument `maxtries` renamed to `max_tries`.
W0 : :term:`array_like`, optional
Optional starting condition for ``W`` in NMF algorithm.
(Ignored when using the ICA method.)
Expand Down Expand Up @@ -136,7 +144,7 @@ def separate(
else:
n = S.shape[0]

for i_try in range(maxtries):
for i_try in range(max_tries):

if sep_method.lower() in {"ica", "fastica"}:
# Use sklearn's implementation of ICA.
Expand All @@ -146,7 +154,7 @@ def separate(
estimator = sklearn.decomposition.FastICA(
n_components=n,
whiten=True,
max_iter=maxiter,
max_iter=max_iter,
tol=tol,
random_state=random_state,
)
Expand All @@ -163,7 +171,7 @@ def separate(
alpha=alpha,
l1_ratio=0.5,
tol=tol,
max_iter=maxiter,
max_iter=max_iter,
random_state=random_state,
)

Expand All @@ -182,7 +190,7 @@ def separate(
estimator = getattr(sklearn.decomposition, sep_method)(
n_components=n,
tol=tol,
max_iter=maxiter,
max_iter=max_iter,
random_state=random_state,
)
S_sep = estimator.fit_transform(S.T)
Expand All @@ -191,7 +199,7 @@ def separate(
raise ValueError('Unknown separation method "{}".'.format(sep_method))

# check if max number of iterations was reached
if estimator.n_iter_ < maxiter:
if estimator.n_iter_ < max_iter:
if verbosity >= 1:
print(
"{} converged after {} iterations.".format(
Expand All @@ -205,14 +213,14 @@ def separate(
i_try + 1, estimator.n_iter_
)
)
if i_try + 1 < maxtries:
if i_try + 1 < max_tries:
if verbosity >= 1:
print("Trying a new random state.")
# Change to a new random_state
if random_state is not None:
random_state = (random_state + 1) % 2 ** 32

if estimator.n_iter_ == maxiter:
if estimator.n_iter_ == max_iter:
if verbosity >= 1:
print(
"Warning: maximum number of allowed tries reached at {} iterations"
Expand Down Expand Up @@ -256,10 +264,10 @@ def separate(

# save the algorithm convergence info
convergence = {}
convergence["max_iterations"] = maxiter
convergence["max_iterations"] = max_iter
convergence["random_state"] = random_state
convergence["iterations"] = estimator.n_iter_
convergence["converged"] = estimator.n_iter_ != maxiter
convergence["converged"] = estimator.n_iter_ != max_iter

# scale back to raw magnitudes
S_matched *= median
Expand Down
12 changes: 6 additions & 6 deletions fissa/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ def test_str_contains_stuff(self):
"nRegions": 7,
"expansion": 0.813962,
"alpha": 0.212827,
"maxiter": 234789,
"max_iter": 234789,
"tol": 3.121e-4,
"maxtries": 28,
"max_tries": 28,
"ncores_preparation": 1,
"ncores_separation": None,
"method": "nmf",
Expand All @@ -263,9 +263,9 @@ def test_repr_contains_stuff(self):
"nRegions": 7,
"expansion": 0.813962,
"alpha": 0.212827,
"maxiter": 234789,
"max_iter": 234789,
"tol": 3.121e-4,
"maxtries": 28,
"max_tries": 28,
"ncores_preparation": 1,
"ncores_separation": None,
"method": "nmf",
Expand All @@ -278,9 +278,9 @@ def test_repr_eval(self):
"nRegions": 7,
"expansion": 0.813962,
"alpha": 0.212827,
"maxiter": 234789,
"max_iter": 234789,
"tol": 3.121e-4,
"maxtries": 28,
"max_tries": 28,
"ncores_preparation": 1,
"ncores_separation": None,
"method": "nmf",
Expand Down
8 changes: 4 additions & 4 deletions fissa/tests/test_neuropil.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ def run_method(self, method, expected_converged=None, **kwargs):
self.assert_equal(convergence["converged"], expected_converged)

def test_method(self):
self.run_method(self.method, expected_converged=True, maxtries=1)
self.run_method(self.method, expected_converged=True, max_tries=1)

def test_reduce_dim(self):
self.run_method(self.method, expected_converged=True, maxtries=1, n=2)
self.run_method(self.method, expected_converged=True, max_tries=1, n=2)

def test_manual_seed(self):
self.run_method(
self.method,
expected_converged=True,
maxtries=1,
max_tries=1,
random_state=0,
)

def test_retry(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
self.run_method(self.method, maxiter=1, maxtries=3)
self.run_method(self.method, max_iter=1, max_tries=3)


class TestNeuropilNMF(BaseTestCase, NeuropilMixin):
Expand Down

0 comments on commit b9f8b79

Please sign in to comment.