diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d41f6898e3..b7d088d87a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,6 +13,12 @@ repos: additional_dependencies: [isort==5.6.4] - id: nbqa-pyupgrade additional_dependencies: [pyupgrade==2.7.4] + +- repo: https://github.com/PyCQA/isort + rev: 5.6.4 + hooks: + - id: isort + name: isort (python) - repo: https://github.com/asottile/pyupgrade rev: v2.7.4 hooks: diff --git a/benchmarks/benchmarks/benchmarks.py b/benchmarks/benchmarks/benchmarks.py index 070c1bb66b..bbf8fbd5fc 100644 --- a/benchmarks/benchmarks/benchmarks.py +++ b/benchmarks/benchmarks/benchmarks.py @@ -17,10 +17,11 @@ import numpy as np import pandas as pd -import pymc3 as pm import theano import theano.tensor as tt +import pymc3 as pm + def glm_hierarchical_model(random_seed=123): """Sample glm hierarchical model to use in benchmarks""" diff --git a/docs/source/conf.py b/docs/source/conf.py index 2d3743c01b..9b23a323af 100755 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -12,8 +12,8 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys import os +import sys import pymc3 diff --git a/docs/source/sphinxext/gallery_generator.py b/docs/source/sphinxext/gallery_generator.py index b3fdd23637..7023e1abcc 100644 --- a/docs/source/sphinxext/gallery_generator.py +++ b/docs/source/sphinxext/gallery_generator.py @@ -4,11 +4,11 @@ Modified from the seaborn project, which modified the mpld3 project. """ import base64 +import glob import json import os -import glob -import shutil import runpy +import shutil import matplotlib diff --git a/pymc3/__init__.py b/pymc3/__init__.py index 1e913581b9..471eea3da7 100644 --- a/pymc3/__init__.py +++ b/pymc3/__init__.py @@ -38,38 +38,33 @@ def __set_compiler_flags(): __set_compiler_flags() +from . import gp, ode, sampling +from .backends import load_trace, save_trace +from .backends.tracetab import * from .blocking import * +from .data import * from .distributions import * from .distributions import transforms +from .exceptions import * from .glm import * -from . import gp from .math import ( + expand_packed_triangular, + invlogit, + invprobit, logaddexp, - logsumexp, logit, - invlogit, - expand_packed_triangular, + logsumexp, probit, - invprobit, ) from .model import * from .model_graph import model_to_graphviz -from . import ode -from .stats import * +from .plots import * from .sampling import * -from .step_methods import * from .smc import * +from .stats import * +from .step_methods import * +from .tests import test from .theanof import * from .tuning import * from .variational import * from .vartypes import * -from .exceptions import * -from . import sampling - -from .backends.tracetab import * -from .backends import save_trace, load_trace - -from .plots import * -from .tests import test - -from .data import * diff --git a/pymc3/backends/__init__.py b/pymc3/backends/__init__.py index 380d242633..d746b4408c 100644 --- a/pymc3/backends/__init__.py +++ b/pymc3/backends/__init__.py @@ -60,4 +60,4 @@ Saved backends can be loaded using `arviz.from_netcdf` """ -from ..backends.ndarray import NDArray, save_trace, load_trace, point_list_to_multitrace +from ..backends.ndarray import NDArray, load_trace, point_list_to_multitrace, save_trace diff --git a/pymc3/backends/base.py b/pymc3/backends/base.py index 2c57da6a5a..86bf4fb6f3 100644 --- a/pymc3/backends/base.py +++ b/pymc3/backends/base.py @@ -18,16 +18,17 @@ """ import itertools as itl import logging -from typing import List +import warnings + from abc import ABC +from typing import List import numpy as np -import warnings import theano.tensor as tt from ..model import modelcontext -from .report import SamplerReport, merge_reports from ..util import get_var_name +from .report import SamplerReport, merge_reports logger = logging.getLogger("pymc3") diff --git a/pymc3/backends/ndarray.py b/pymc3/backends/ndarray.py index c2904dad1d..00bcf77692 100644 --- a/pymc3/backends/ndarray.py +++ b/pymc3/backends/ndarray.py @@ -20,14 +20,16 @@ import json import os import shutil -from typing import Optional, Dict, Any, List import warnings +from typing import Any, Dict, List, Optional + import numpy as np + from pymc3.backends import base from pymc3.backends.base import MultiTrace -from pymc3.model import Model, modelcontext from pymc3.exceptions import TraceDirectoryError +from pymc3.model import Model, modelcontext def save_trace(trace: MultiTrace, directory: Optional[str] = None, overwrite=False) -> str: diff --git a/pymc3/backends/report.py b/pymc3/backends/report.py index 98937326da..5f4ffd3b47 100644 --- a/pymc3/backends/report.py +++ b/pymc3/backends/report.py @@ -12,15 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -import logging -import enum -from typing import Any, Optional import dataclasses +import enum +import logging -from ..util import is_transformed_name, get_untransformed_name +from typing import Any, Optional import arviz +from ..util import get_untransformed_name, is_transformed_name + logger = logging.getLogger("pymc3") diff --git a/pymc3/backends/tracetab.py b/pymc3/backends/tracetab.py index 0f9f750f16..ba7cbdaed1 100644 --- a/pymc3/backends/tracetab.py +++ b/pymc3/backends/tracetab.py @@ -15,9 +15,10 @@ """Functions for converting traces into a table-like format """ +import warnings + import numpy as np import pandas as pd -import warnings from ..util import get_default_varnames diff --git a/pymc3/blocking.py b/pymc3/blocking.py index f951fb2bb7..c676197e43 100644 --- a/pymc3/blocking.py +++ b/pymc3/blocking.py @@ -17,9 +17,10 @@ Classes for working with subsets of parameters. """ +import collections import copy + import numpy as np -import collections from .util import get_var_name diff --git a/pymc3/data.py b/pymc3/data.py index e1539a059f..78ab794c52 100644 --- a/pymc3/data.py +++ b/pymc3/data.py @@ -13,17 +13,19 @@ # limitations under the License. import collections -from copy import copy import io import os import pkgutil -from typing import Dict, List, Any + +from copy import copy +from typing import Any, Dict, List import numpy as np import pandas as pd -import pymc3 as pm -import theano.tensor as tt import theano +import theano.tensor as tt + +import pymc3 as pm __all__ = [ "get_data", diff --git a/pymc3/distributions/__init__.py b/pymc3/distributions/__init__.py index b4b56d749a..7ae5d3b097 100644 --- a/pymc3/distributions/__init__.py +++ b/pymc3/distributions/__init__.py @@ -12,98 +12,93 @@ # See the License for the specific language governing permissions and # limitations under the License. -from . import timeseries -from . import transforms -from . import shape_utils - -from .posterior_predictive import fast_sample_posterior_predictive - -from .continuous import Uniform -from .continuous import Flat -from .continuous import HalfFlat -from .continuous import TruncatedNormal -from .continuous import Normal -from .continuous import Beta -from .continuous import Kumaraswamy -from .continuous import Exponential -from .continuous import Laplace -from .continuous import StudentT -from .continuous import Cauchy -from .continuous import HalfCauchy -from .continuous import Gamma -from .continuous import Weibull -from .continuous import HalfStudentT -from .continuous import Lognormal -from .continuous import ChiSquared -from .continuous import HalfNormal -from .continuous import Wald -from .continuous import Pareto -from .continuous import InverseGamma -from .continuous import ExGaussian -from .continuous import VonMises -from .continuous import SkewNormal -from .continuous import Triangular -from .continuous import Gumbel -from .continuous import Logistic -from .continuous import LogitNormal -from .continuous import Interpolated -from .continuous import Rice -from .continuous import Moyal - -from .discrete import Binomial -from .discrete import BetaBinomial -from .discrete import Bernoulli -from .discrete import DiscreteWeibull -from .discrete import Poisson -from .discrete import NegativeBinomial -from .discrete import ConstantDist -from .discrete import Constant -from .discrete import ZeroInflatedPoisson -from .discrete import ZeroInflatedNegativeBinomial -from .discrete import ZeroInflatedBinomial -from .discrete import DiscreteUniform -from .discrete import Geometric -from .discrete import HyperGeometric -from .discrete import Categorical -from .discrete import OrderedLogistic - -from .distribution import DensityDist -from .distribution import Distribution -from .distribution import Continuous -from .distribution import Discrete -from .distribution import NoDistribution -from .distribution import TensorType -from .distribution import draw_values -from .distribution import generate_samples - -from .simulator import Simulator - -from .mixture import Mixture -from .mixture import NormalMixture -from .mixture import MixtureSameFamily - -from .multivariate import MvNormal -from .multivariate import MatrixNormal -from .multivariate import KroneckerNormal -from .multivariate import MvStudentT -from .multivariate import Dirichlet -from .multivariate import Multinomial -from .multivariate import Wishart -from .multivariate import WishartBartlett -from .multivariate import LKJCholeskyCov -from .multivariate import LKJCorr - -from .timeseries import AR1 -from .timeseries import AR -from .timeseries import GaussianRandomWalk -from .timeseries import GARCH11 -from .timeseries import MvGaussianRandomWalk -from .timeseries import MvStudentTRandomWalk - +from . import shape_utils, timeseries, transforms from .bart import BART - from .bound import Bound - +from .continuous import ( + Beta, + Cauchy, + ChiSquared, + ExGaussian, + Exponential, + Flat, + Gamma, + Gumbel, + HalfCauchy, + HalfFlat, + HalfNormal, + HalfStudentT, + Interpolated, + InverseGamma, + Kumaraswamy, + Laplace, + Logistic, + LogitNormal, + Lognormal, + Moyal, + Normal, + Pareto, + Rice, + SkewNormal, + StudentT, + Triangular, + TruncatedNormal, + Uniform, + VonMises, + Wald, + Weibull, +) +from .discrete import ( + Bernoulli, + BetaBinomial, + Binomial, + Categorical, + Constant, + ConstantDist, + DiscreteUniform, + DiscreteWeibull, + Geometric, + HyperGeometric, + NegativeBinomial, + OrderedLogistic, + Poisson, + ZeroInflatedBinomial, + ZeroInflatedNegativeBinomial, + ZeroInflatedPoisson, +) +from .distribution import ( + Continuous, + DensityDist, + Discrete, + Distribution, + NoDistribution, + TensorType, + draw_values, + generate_samples, +) +from .mixture import Mixture, MixtureSameFamily, NormalMixture +from .multivariate import ( + Dirichlet, + KroneckerNormal, + LKJCholeskyCov, + LKJCorr, + MatrixNormal, + Multinomial, + MvNormal, + MvStudentT, + Wishart, + WishartBartlett, +) +from .posterior_predictive import fast_sample_posterior_predictive +from .simulator import Simulator +from .timeseries import ( + AR, + AR1, + GARCH11, + GaussianRandomWalk, + MvGaussianRandomWalk, + MvStudentTRandomWalk, +) __all__ = [ "Uniform", diff --git a/pymc3/distributions/bart.py b/pymc3/distributions/bart.py index c512d84576..19b677c347 100644 --- a/pymc3/distributions/bart.py +++ b/pymc3/distributions/bart.py @@ -13,8 +13,9 @@ # limitations under the License. import numpy as np + from .distribution import NoDistribution -from .tree import Tree, SplitNode, LeafNode +from .tree import LeafNode, SplitNode, Tree __all__ = ["BART"] diff --git a/pymc3/distributions/bound.py b/pymc3/distributions/bound.py index bde9abc887..074a575eba 100644 --- a/pymc3/distributions/bound.py +++ b/pymc3/distributions/bound.py @@ -17,16 +17,15 @@ import numpy as np import theano.tensor as tt +from pymc3.distributions import transforms +from pymc3.distributions.dist_math import bound from pymc3.distributions.distribution import ( - Distribution, - Discrete, Continuous, + Discrete, + Distribution, draw_values, generate_samples, ) -from pymc3.distributions import transforms -from pymc3.distributions.dist_math import bound - from pymc3.theanof import floatX __all__ = ["Bound"] diff --git a/pymc3/distributions/continuous.py b/pymc3/distributions/continuous.py index 533aeff5b4..dd8a82bdf7 100644 --- a/pymc3/distributions/continuous.py +++ b/pymc3/distributions/continuous.py @@ -23,30 +23,31 @@ import theano import theano.tensor as tt +from scipy import stats +from scipy.interpolate import InterpolatedUnivariateSpline +from scipy.special import expit + +from pymc3.theanof import floatX + +from ..math import invlogit, logdiffexp, logit from . import transforms from .dist_math import ( + SplineWrapper, alltrue_elemwise, betaln, bound, + clipped_beta_rvs, gammaln, i0e, incomplete_beta, logpow, normal_lccdf, normal_lcdf, - SplineWrapper, std_cdf, zvalue, - clipped_beta_rvs, ) from .distribution import Continuous, draw_values, generate_samples from .special import log_i0 -from ..math import invlogit, logit, logdiffexp - -from pymc3.theanof import floatX -from scipy import stats -from scipy.special import expit -from scipy.interpolate import InterpolatedUnivariateSpline __all__ = [ "Uniform", diff --git a/pymc3/distributions/discrete.py b/pymc3/distributions/discrete.py index d88c9bcd21..8a6f9f7989 100644 --- a/pymc3/distributions/discrete.py +++ b/pymc3/distributions/discrete.py @@ -12,17 +12,19 @@ # See the License for the specific language governing permissions and # limitations under the License. +import warnings + import numpy as np import theano.tensor as tt + from scipy import stats -import warnings -from .dist_math import bound, factln, binomln, betaln, logpow, random_choice +from pymc3.math import log1pexp, logaddexp, logit, sigmoid, tround + +from ..theanof import floatX, intX, take_along_axis +from .dist_math import betaln, binomln, bound, factln, logpow, random_choice from .distribution import Discrete, draw_values, generate_samples from .shape_utils import broadcast_distribution_samples -from pymc3.math import tround, sigmoid, logaddexp, logit, log1pexp -from ..theanof import floatX, intX, take_along_axis - __all__ = [ "Binomial", diff --git a/pymc3/distributions/dist_math.py b/pymc3/distributions/dist_math.py index 1af8e86c72..b4ea71a837 100644 --- a/pymc3/distributions/dist_math.py +++ b/pymc3/distributions/dist_math.py @@ -18,21 +18,23 @@ @author: johnsalvatier """ import platform + import numpy as np import scipy.linalg import scipy.stats -import theano.tensor as tt import theano -from theano.scalar import UnaryScalarOp, upgrade_to_float_no_complex -from theano.tensor.slinalg import Cholesky +import theano.tensor as tt + +from theano import scan from theano.compile.builders import OpFromGraph +from theano.scalar import UnaryScalarOp, upgrade_to_float_no_complex from theano.scan import until -from theano import scan -from .shape_utils import to_tuple +from theano.tensor.slinalg import Cholesky -from .special import gammaln from pymc3.theanof import floatX +from .shape_utils import to_tuple +from .special import gammaln f = floatX c = -0.5 * np.log(2.0 * np.pi) diff --git a/pymc3/distributions/distribution.py b/pymc3/distributions/distribution.py index c8c895c188..062ceb00cd 100644 --- a/pymc3/distributions/distribution.py +++ b/pymc3/distributions/distribution.py @@ -12,31 +12,42 @@ # See the License for the specific language governing permissions and # limitations under the License. -import multiprocessing -import numbers import contextvars -import dill import inspect +import multiprocessing +import numbers import sys import types -from typing import TYPE_CHECKING import warnings +from typing import TYPE_CHECKING + +import dill + if TYPE_CHECKING: from typing import Optional, Callable import numpy as np +import theano import theano.tensor as tt + from theano import function -from ..util import get_repr_for_variable, get_var_name -import theano + from ..memoize import memoize -from ..model import Model, build_named_node_tree, FreeRV, ObservedRV, MultiObservedRV, ContextMeta +from ..model import ( + ContextMeta, + FreeRV, + Model, + MultiObservedRV, + ObservedRV, + build_named_node_tree, +) +from ..util import get_repr_for_variable, get_var_name from ..vartypes import string_types, theano_constant from .shape_utils import ( - to_tuple, - get_broadcastable_dist_samples, broadcast_dist_samples_shape, + get_broadcastable_dist_samples, + to_tuple, ) __all__ = [ diff --git a/pymc3/distributions/mixture.py b/pymc3/distributions/mixture.py index dcfd823fa1..794ae82322 100644 --- a/pymc3/distributions/mixture.py +++ b/pymc3/distributions/mixture.py @@ -12,29 +12,31 @@ # See the License for the specific language governing permissions and # limitations under the License. +import warnings + from collections.abc import Iterable + import numpy as np import theano import theano.tensor as tt -import warnings from ..math import logsumexp +from ..theanof import _conversion_map, take_along_axis +from .continuous import Normal, get_tau_sigma from .dist_math import bound, random_choice from .distribution import ( Discrete, Distribution, - draw_values, - generate_samples, _DrawValuesContext, _DrawValuesContextBlocker, + draw_values, + generate_samples, ) from .shape_utils import ( - to_tuple, broadcast_distribution_samples, get_broadcastable_dist_samples, + to_tuple, ) -from .continuous import get_tau_sigma, Normal -from ..theanof import _conversion_map, take_along_axis __all__ = ["Mixture", "NormalMixture", "MixtureSameFamily"] diff --git a/pymc3/distributions/multivariate.py b/pymc3/distributions/multivariate.py index 2d4c433d28..67780891b5 100755 --- a/pymc3/distributions/multivariate.py +++ b/pymc3/distributions/multivariate.py @@ -16,29 +16,36 @@ # -*- coding: utf-8 -*- import warnings + import numpy as np import scipy import theano import theano.tensor as tt -from scipy import stats, linalg - +from scipy import linalg, stats from theano.gof.op import get_test_value from theano.gof.utils import TestValueError -from theano.tensor.nlinalg import det, matrix_inverse, trace, eigh +from theano.tensor.nlinalg import det, eigh, matrix_inverse, trace from theano.tensor.slinalg import Cholesky + import pymc3 as pm from pymc3.theanof import floatX -from . import transforms -from .distribution import Continuous, Discrete, draw_values, generate_samples, _DrawValuesContext + +from ..math import kron_diag, kron_dot, kron_solve_lower, kronecker from ..model import Deterministic +from . import transforms from .continuous import ChiSquared, Normal +from .dist_math import bound, factln, logpow +from .distribution import ( + Continuous, + Discrete, + _DrawValuesContext, + draw_values, + generate_samples, +) +from .shape_utils import broadcast_dist_samples_to, to_tuple from .special import gammaln, multigammaln -from .dist_math import bound, logpow, factln -from .shape_utils import to_tuple, broadcast_dist_samples_to -from ..math import kron_dot, kron_diag, kron_solve_lower, kronecker - __all__ = [ "MvNormal", diff --git a/pymc3/distributions/posterior_predictive.py b/pymc3/distributions/posterior_predictive.py index 8efec54af7..f3d6e4f591 100644 --- a/pymc3/distributions/posterior_predictive.py +++ b/pymc3/distributions/posterior_predictive.py @@ -1,48 +1,50 @@ +import contextvars +import logging import numbers +import warnings + +from collections import UserDict +from contextlib import AbstractContextManager from typing import ( - List, - Dict, + TYPE_CHECKING, Any, + Callable, + Dict, + List, Optional, + Set, Tuple, Union, cast, - TYPE_CHECKING, - Callable, overload, - Set, ) -import warnings -import logging -from collections import UserDict -from contextlib import AbstractContextManager -import contextvars -from typing_extensions import Protocol, Literal import numpy as np import theano import theano.tensor as tt -from xarray import Dataset + from arviz import InferenceData +from typing_extensions import Literal, Protocol +from xarray import Dataset from ..backends.base import MultiTrace -from .distribution import ( - _DrawValuesContext, - _DrawValuesContextBlocker, - is_fast_drawable, - _compile_theano_function, - vectorized_ppc, -) +from ..exceptions import IncorrectArgumentsError from ..model import ( Model, - get_named_nodes_and_relations, - ObservedRV, MultiObservedRV, + ObservedRV, + get_named_nodes_and_relations, modelcontext, ) -from ..exceptions import IncorrectArgumentsError +from ..util import chains_and_samples, dataset_to_point_list, get_var_name from ..vartypes import theano_constant -from ..util import dataset_to_point_list, chains_and_samples, get_var_name +from .distribution import ( + _compile_theano_function, + _DrawValuesContext, + _DrawValuesContextBlocker, + is_fast_drawable, + vectorized_ppc, +) # Failing tests: # test_mixture_random_shape::test_mixture_random_shape diff --git a/pymc3/distributions/shape_utils.py b/pymc3/distributions/shape_utils.py index c41507620f..a339ae7c08 100644 --- a/pymc3/distributions/shape_utils.py +++ b/pymc3/distributions/shape_utils.py @@ -19,7 +19,6 @@ """ import numpy as np - __all__ = [ "to_tuple", "shapes_broadcasting", diff --git a/pymc3/distributions/simulator.py b/pymc3/distributions/simulator.py index 2662c86679..3a70538f03 100644 --- a/pymc3/distributions/simulator.py +++ b/pymc3/distributions/simulator.py @@ -13,10 +13,13 @@ # limitations under the License. import logging + import numpy as np -from .distribution import NoDistribution, draw_values + from scipy.spatial import cKDTree +from .distribution import NoDistribution, draw_values + __all__ = ["Simulator"] _log = logging.getLogger("pymc3") diff --git a/pymc3/distributions/special.py b/pymc3/distributions/special.py index 1401596486..ba4662b2df 100644 --- a/pymc3/distributions/special.py +++ b/pymc3/distributions/special.py @@ -14,8 +14,9 @@ import numpy as np import theano.tensor as tt -from theano.scalar.basic_scipy import GammaLn, Psi + from theano import scalar +from theano.scalar.basic_scipy import GammaLn, Psi __all__ = ["gammaln", "multigammaln", "psi", "log_i0"] diff --git a/pymc3/distributions/timeseries.py b/pymc3/distributions/timeseries.py index ab637cd547..09fe6c41cf 100644 --- a/pymc3/distributions/timeseries.py +++ b/pymc3/distributions/timeseries.py @@ -14,16 +14,15 @@ import warnings -from scipy import stats +import numpy as np import theano.tensor as tt + +from scipy import stats from theano import scan -import numpy as np -from .continuous import get_tau_sigma, Normal, Flat +from . import distribution, multivariate +from .continuous import Flat, Normal, get_tau_sigma from .shape_utils import to_tuple -from . import multivariate -from . import distribution - __all__ = [ "AR1", diff --git a/pymc3/distributions/transforms.py b/pymc3/distributions/transforms.py index 92a38178ec..c912a648b0 100644 --- a/pymc3/distributions/transforms.py +++ b/pymc3/distributions/transforms.py @@ -13,16 +13,17 @@ # limitations under the License. import warnings + +import numpy as np import theano.tensor as tt +from scipy.special import logit as nplogit + +from ..math import invlogit, logit, logsumexp from ..model import FreeRV -from ..theanof import gradient, floatX +from ..theanof import floatX, gradient from . import distribution -from ..math import logit, invlogit, logsumexp from .distribution import draw_values -import numpy as np -from scipy.special import logit as nplogit - __all__ = [ "Transform", diff --git a/pymc3/distributions/tree.py b/pymc3/distributions/tree.py index 3b2c098e89..81c727232a 100644 --- a/pymc3/distributions/tree.py +++ b/pymc3/distributions/tree.py @@ -12,10 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import numpy as np import math + from copy import deepcopy +import numpy as np + class Tree: """Full binary tree diff --git a/pymc3/examples/GHME_2013.py b/pymc3/examples/GHME_2013.py index a9176c50ca..ef08df1f17 100644 --- a/pymc3/examples/GHME_2013.py +++ b/pymc3/examples/GHME_2013.py @@ -1,6 +1,6 @@ +import matplotlib.pyplot as plt import numpy as np import pandas as pd -import matplotlib.pyplot as plt from pymc3 import HalfCauchy, Model, Normal, get_data, sample from pymc3.distributions.timeseries import GaussianRandomWalk diff --git a/pymc3/examples/LKJ_correlation.py b/pymc3/examples/LKJ_correlation.py index ec3f87288d..8a70c38a65 100644 --- a/pymc3/examples/LKJ_correlation.py +++ b/pymc3/examples/LKJ_correlation.py @@ -1,6 +1,8 @@ -import theano.tensor as tt import numpy as np +import theano.tensor as tt + from numpy.random import multivariate_normal + import pymc3 as pm # Generate some multivariate normal data: diff --git a/pymc3/examples/arbitrary_stochastic.py b/pymc3/examples/arbitrary_stochastic.py index 87850347be..1840e11612 100644 --- a/pymc3/examples/arbitrary_stochastic.py +++ b/pymc3/examples/arbitrary_stochastic.py @@ -1,7 +1,8 @@ import numpy as np -import pymc3 as pm import theano.tensor as tt +import pymc3 as pm + # custom log-liklihood def logp(failure, lam, value): diff --git a/pymc3/examples/arma_example.py b/pymc3/examples/arma_example.py index c5a26177ac..c183052256 100644 --- a/pymc3/examples/arma_example.py +++ b/pymc3/examples/arma_example.py @@ -1,7 +1,8 @@ -import pymc3 as pm +import numpy as np + from theano import scan, shared -import numpy as np +import pymc3 as pm """ ARMA example diff --git a/pymc3/examples/baseball.py b/pymc3/examples/baseball.py index 50e891c054..f861099a7f 100644 --- a/pymc3/examples/baseball.py +++ b/pymc3/examples/baseball.py @@ -3,9 +3,10 @@ # See http://mc-stan.org/documentation/case-studies/pool-binary-trials.html for more details # -import pymc3 as pm import numpy as np +import pymc3 as pm + def build_model(): data = np.loadtxt( diff --git a/pymc3/examples/custom_dists.py b/pymc3/examples/custom_dists.py index a9789bab9d..fb7605265e 100644 --- a/pymc3/examples/custom_dists.py +++ b/pymc3/examples/custom_dists.py @@ -10,9 +10,9 @@ import matplotlib.pyplot as plt import numpy as np +import theano.tensor as tt import pymc3 as pm -import theano.tensor as tt np.random.seed(42) theta_true = (25, 0.5) diff --git a/pymc3/examples/disaster_model.py b/pymc3/examples/disaster_model.py index f0459bc161..103206a6d7 100644 --- a/pymc3/examples/disaster_model.py +++ b/pymc3/examples/disaster_model.py @@ -9,10 +9,11 @@ """ -import pymc3 as pm import theano.tensor as tt + from numpy import arange, array +import pymc3 as pm __all__ = ["disasters_data", "switchpoint", "early_mean", "late_mean", "rate", "disasters"] diff --git a/pymc3/examples/disaster_model_theano_op.py b/pymc3/examples/disaster_model_theano_op.py index b8c2b4c7de..3b3597a287 100644 --- a/pymc3/examples/disaster_model_theano_op.py +++ b/pymc3/examples/disaster_model_theano_op.py @@ -5,10 +5,12 @@ """ -import pymc3 as pm -from theano.compile.ops import as_op import theano.tensor as tt + from numpy import arange, array, empty +from theano.compile.ops import as_op + +import pymc3 as pm __all__ = ["disasters_data", "switchpoint", "early_mean", "late_mean", "rate", "disasters"] diff --git a/pymc3/examples/garch_example.py b/pymc3/examples/garch_example.py index 33744ed132..bf22955512 100644 --- a/pymc3/examples/garch_example.py +++ b/pymc3/examples/garch_example.py @@ -1,6 +1,7 @@ -from pymc3 import Normal, sample, Model, Uniform, summary -import theano.tensor as tt import numpy as np +import theano.tensor as tt + +from pymc3 import Model, Normal, Uniform, sample, summary """ Example from Stan - slightly altered diff --git a/pymc3/examples/gelman_bioassay.py b/pymc3/examples/gelman_bioassay.py index 6fca8bc468..032080755e 100644 --- a/pymc3/examples/gelman_bioassay.py +++ b/pymc3/examples/gelman_bioassay.py @@ -1,5 +1,6 @@ +from numpy import array, ones + import pymc3 as pm -from numpy import ones, array # Samples for each dose level n = 5 * ones(4, dtype=int) diff --git a/pymc3/examples/gelman_schools.py b/pymc3/examples/gelman_schools.py index ef023c2096..984f305875 100644 --- a/pymc3/examples/gelman_schools.py +++ b/pymc3/examples/gelman_schools.py @@ -1,6 +1,7 @@ -from pymc3 import HalfCauchy, Normal, sample, Model, loo import numpy as np +from pymc3 import HalfCauchy, Model, Normal, loo, sample + """Original Stan model data { diff --git a/pymc3/examples/lasso_missing.py b/pymc3/examples/lasso_missing.py index 2ce3192af0..1757811edb 100644 --- a/pymc3/examples/lasso_missing.py +++ b/pymc3/examples/lasso_missing.py @@ -1,7 +1,9 @@ -import pymc3 as pm import pandas as pd + from numpy.ma import masked_values +import pymc3 as pm + # Import data, filling missing values with sentinels (-999) test_scores = pd.read_csv(pm.get_data("test_scores.csv")).fillna(-999) diff --git a/pymc3/examples/lightspeed_example.py b/pymc3/examples/lightspeed_example.py index 7dc76bb225..8edfea5961 100644 --- a/pymc3/examples/lightspeed_example.py +++ b/pymc3/examples/lightspeed_example.py @@ -1,4 +1,5 @@ import numpy as np + import pymc3 as pm light_speed = np.array( diff --git a/pymc3/examples/rankdata_ordered.py b/pymc3/examples/rankdata_ordered.py index cea237e140..ef5a0f5fae 100644 --- a/pymc3/examples/rankdata_ordered.py +++ b/pymc3/examples/rankdata_ordered.py @@ -1,7 +1,8 @@ import numpy as np -import pymc3 as pm import theano.tensor as tt +import pymc3 as pm + """ Using Ordered transformation to model ranking data inspired by the Stan implementation of Thurstonian model diff --git a/pymc3/examples/samplers_mvnormal.py b/pymc3/examples/samplers_mvnormal.py index 54db0f9146..2d5fb1a8ab 100644 --- a/pymc3/examples/samplers_mvnormal.py +++ b/pymc3/examples/samplers_mvnormal.py @@ -7,12 +7,14 @@ """ -import numpy as np import time + +import numpy as np import pandas as pd -import pymc3 as pm import theano.tensor as tt +import pymc3 as pm + # with this flag one can switch between defining the bivariate normal as # either a 2D MvNormal (USE_XY = False) split up the two dimensions into # two variables 'x' and 'y'. The latter is recommended because it highlights diff --git a/pymc3/examples/simpletest.py b/pymc3/examples/simpletest.py index c54c76a856..32c658eb42 100644 --- a/pymc3/examples/simpletest.py +++ b/pymc3/examples/simpletest.py @@ -1,6 +1,7 @@ -import pymc3 as pm import numpy as np +import pymc3 as pm + # import pydevd # pydevd.set_pm_excepthook() np.seterr(invalid="raise") diff --git a/pymc3/glm/__init__.py b/pymc3/glm/__init__.py index 6c1b75acbf..762538075e 100644 --- a/pymc3/glm/__init__.py +++ b/pymc3/glm/__init__.py @@ -12,5 +12,5 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .linear import LinearComponent, GLM from . import families +from .linear import GLM, LinearComponent diff --git a/pymc3/glm/families.py b/pymc3/glm/families.py index 1fd10f0418..814cb2e5fa 100644 --- a/pymc3/glm/families.py +++ b/pymc3/glm/families.py @@ -13,12 +13,14 @@ # limitations under the License. import numbers -import numpy as np + from copy import copy +import numpy as np import theano.tensor as tt -from ..model import modelcontext + from .. import distributions as pm_dists +from ..model import modelcontext __all__ = ["Normal", "StudentT", "Binomial", "Poisson", "NegativeBinomial"] diff --git a/pymc3/glm/linear.py b/pymc3/glm/linear.py index 71b20652cf..3529d96776 100644 --- a/pymc3/glm/linear.py +++ b/pymc3/glm/linear.py @@ -12,14 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import theano.tensor as tt import numpy as np -from ..distributions import Normal, Flat +import theano.tensor as tt + +from ..distributions import Flat, Normal +from ..model import Deterministic, Model from . import families -from ..model import Model, Deterministic from .utils import any_to_tensor_and_labels - __all__ = ["LinearComponent", "GLM"] diff --git a/pymc3/glm/utils.py b/pymc3/glm/utils.py index d36b8ecc62..889284b317 100644 --- a/pymc3/glm/utils.py +++ b/pymc3/glm/utils.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pandas as pd import numpy as np +import pandas as pd import theano.tensor as tt diff --git a/pymc3/gp/__init__.py b/pymc3/gp/__init__.py index a39f7fe93f..8704266ab8 100644 --- a/pymc3/gp/__init__.py +++ b/pymc3/gp/__init__.py @@ -12,7 +12,5 @@ # See the License for the specific language governing permissions and # limitations under the License. -from . import cov -from . import mean -from . import util -from .gp import Latent, Marginal, MarginalSparse, TP, LatentKron, MarginalKron +from . import cov, mean, util +from .gp import TP, Latent, LatentKron, Marginal, MarginalKron, MarginalSparse diff --git a/pymc3/gp/cov.py b/pymc3/gp/cov.py index 69d1c2f410..7a01a9eec5 100644 --- a/pymc3/gp/cov.py +++ b/pymc3/gp/cov.py @@ -13,12 +13,14 @@ # limitations under the License. import warnings + +from functools import reduce +from numbers import Number +from operator import add, mul + import numpy as np import theano import theano.tensor as tt -from functools import reduce -from operator import mul, add -from numbers import Number __all__ = [ "Constant", diff --git a/pymc3/gp/gp.py b/pymc3/gp/gp.py index aa2e2f058e..92aceb24f4 100644 --- a/pymc3/gp/gp.py +++ b/pymc3/gp/gp.py @@ -18,20 +18,23 @@ import numpy as np import theano.tensor as tt +from theano.tensor.nlinalg import eigh + import pymc3 as pm -from pymc3.gp.cov import Covariance, Constant + +from pymc3.distributions import draw_values +from pymc3.gp.cov import Constant, Covariance from pymc3.gp.mean import Zero from pymc3.gp.util import ( + cholesky, conditioned_vars, infer_shape, - stabilize, - cholesky, solve_lower, solve_upper, + stabilize, ) -from pymc3.distributions import draw_values -from theano.tensor.nlinalg import eigh -from ..math import cartesian, kron_dot, kron_diag, kron_solve_lower, kron_solve_upper + +from ..math import cartesian, kron_diag, kron_dot, kron_solve_lower, kron_solve_upper __all__ = ["Latent", "Marginal", "TP", "MarginalSparse", "LatentKron", "MarginalKron"] diff --git a/pymc3/gp/util.py b/pymc3/gp/util.py index b35ffeac48..698c300564 100644 --- a/pymc3/gp/util.py +++ b/pymc3/gp/util.py @@ -12,10 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -from scipy.cluster.vq import kmeans +import warnings + import numpy as np import theano.tensor as tt -import warnings +import theano.tensor.slinalg # pylint: disable=unused-import + +from scipy.cluster.vq import kmeans cholesky = tt.slinalg.cholesky solve_lower = tt.slinalg.Solve(A_structure="lower_triangular") diff --git a/pymc3/math.py b/pymc3/math.py index dba3f938a6..bba098678a 100644 --- a/pymc3/math.py +++ b/pymc3/math.py @@ -13,63 +13,67 @@ # limitations under the License. import sys + +from functools import partial, reduce + +import numpy as np +import scipy as sp +import scipy.sparse # pylint: disable=unused-import +import theano +import theano.sparse import theano.tensor as tt +import theano.tensor.slinalg # pylint: disable=unused-import + +from scipy.linalg import block_diag as scipy_block_diag +from theano.gof import Apply, Op # pylint: disable=unused-import -import theano from theano.tensor import ( + abs_, + and_, + ceil, + clip, + concatenate, constant, + cos, + cosh, + dot, + eq, + erf, + erfc, + erfcinv, + erfinv, + exp, flatten, - zeros_like, - ones_like, - stack, - concatenate, - sum, - prod, - lt, + floor, + ge, gt, le, - ge, - eq, + log, + lt, + maximum, + minimum, neq, - switch, - clip, - where, - and_, + ones_like, or_, - abs_, - exp, - log, - cos, + prod, + sgn, sin, - tan, - cosh, sinh, - tanh, sqr, sqrt, - erf, - erfc, - erfinv, - erfcinv, - dot, - maximum, - minimum, - sgn, - ceil, - floor, + stack, + sum, + switch, + tan, + tanh, + where, + zeros_like, ) -from theano.tensor.nlinalg import det, matrix_inverse, extract_diag, matrix_dot, trace -import theano.tensor.slinalg -import theano.sparse +from theano.tensor.nlinalg import det, extract_diag, matrix_dot, matrix_inverse, trace from theano.tensor.nnet import sigmoid -from theano.gof import Op, Apply -import numpy as np -import scipy as sp -import scipy.sparse -from scipy.linalg import block_diag as scipy_block_diag -from pymc3.theanof import floatX, largest_common_dtype, ix_ -from functools import reduce, partial + +from pymc3.theanof import floatX, ix_, largest_common_dtype # pylint: enable=unused-import diff --git a/pymc3/memoize.py b/pymc3/memoize.py index 55a540ca8d..8b5cfccf1b 100644 --- a/pymc3/memoize.py +++ b/pymc3/memoize.py @@ -12,9 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import collections import functools + import dill -import collections + from .util import biwrap CACHE_REGISTRY = [] diff --git a/pymc3/model.py b/pymc3/model.py index 8b52cdfa5a..9e9c2b4084 100644 --- a/pymc3/model.py +++ b/pymc3/model.py @@ -16,27 +16,31 @@ import itertools import threading import warnings -from typing import Optional, TypeVar, Type, List, Union, TYPE_CHECKING, Any, cast + from sys import modules +from typing import TYPE_CHECKING, Any, List, Optional, Type, TypeVar, Union, cast import numpy as np -from pandas import Series import scipy.sparse as sps -import theano.sparse as sparse import theano +import theano.sparse as sparse import theano.tensor as tt -from theano.tensor.var import TensorVariable + +from pandas import Series from theano.compile import SharedVariable +from theano.tensor.var import TensorVariable -from pymc3.theanof import set_theano_conf, floatX import pymc3 as pm + from pymc3.math import flatten_list -from .memoize import memoize, WithMemoization -from .theanof import gradient, hessian, inputvars, generator -from .vartypes import typefilter, discrete_types, continuous_types, isgenerator -from .blocking import DictToArrayBijection, ArrayOrdering -from .util import get_transformed_name, get_var_name +from pymc3.theanof import floatX, set_theano_conf + +from .blocking import ArrayOrdering, DictToArrayBijection from .exceptions import ImputationWarning +from .memoize import WithMemoization, memoize +from .theanof import generator, gradient, hessian, inputvars +from .util import get_transformed_name, get_var_name +from .vartypes import continuous_types, discrete_types, isgenerator, typefilter __all__ = [ "Model", diff --git a/pymc3/model_graph.py b/pymc3/model_graph.py index b2169dcb36..800cca4222 100644 --- a/pymc3/model_graph.py +++ b/pymc3/model_graph.py @@ -13,18 +13,19 @@ # limitations under the License. from collections import deque -from typing import Dict, Iterator, Set, Optional +from typing import Dict, Iterator, Optional, Set VarName = str -from theano.gof.graph import stack_search from theano.compile import SharedVariable +from theano.gof.graph import stack_search from theano.tensor import Tensor -from .util import get_default_varnames, get_var_name -from .model import ObservedRV import pymc3 as pm +from .model import ObservedRV +from .util import get_default_varnames, get_var_name + class ModelGraph: def __init__(self, model): diff --git a/pymc3/ode/ode.py b/pymc3/ode/ode.py index c6bf2c7eae..fe16a7a8ee 100644 --- a/pymc3/ode/ode.py +++ b/pymc3/ode/ode.py @@ -13,13 +13,16 @@ # limitations under the License. import logging + import numpy as np import scipy import theano import theano.tensor as tt + from theano.gof.op import get_test_value + +from ..exceptions import DtypeError, ShapeError from ..ode import utils -from ..exceptions import ShapeError, DtypeError _log = logging.getLogger("pymc3") floatX = theano.config.floatX diff --git a/pymc3/parallel_sampling.py b/pymc3/parallel_sampling.py index c1f13f9bbd..cb57d3f606 100644 --- a/pymc3/parallel_sampling.py +++ b/pymc3/parallel_sampling.py @@ -12,20 +12,23 @@ # See the License for the specific language governing permissions and # limitations under the License. -import multiprocessing -import multiprocessing.sharedctypes import ctypes -import time import logging +import multiprocessing +import multiprocessing.sharedctypes import pickle -from collections import namedtuple -import traceback import platform -from pymc3.exceptions import SamplingError +import time +import traceback + +from collections import namedtuple import numpy as np + from fastprogress.fastprogress import progress_bar +from pymc3.exceptions import SamplingError + from . import theanof logger = logging.getLogger("pymc3") diff --git a/pymc3/plots/__init__.py b/pymc3/plots/__init__.py index 219b9e75cf..2224c952b0 100644 --- a/pymc3/plots/__init__.py +++ b/pymc3/plots/__init__.py @@ -103,7 +103,6 @@ def compareplot(*args, **kwargs): from .posteriorplot import plot_posterior_predictive_glm - # Access to arviz plots: base plots provided by arviz for plot in az.plots.__all__: setattr(sys.modules[__name__], plot, map_args(getattr(az.plots, plot))) diff --git a/pymc3/sampling.py b/pymc3/sampling.py index bef3f21d73..85a530cfd2 100644 --- a/pymc3/sampling.py +++ b/pymc3/sampling.py @@ -14,60 +14,63 @@ """Functions for MCMC sampling.""" -from typing import Dict, List, Optional, cast, Union, Any -from typing import Iterable as TIterable -from collections.abc import Iterable -from collections import defaultdict -from copy import copy -import packaging -import pickle import logging +import pickle +import sys import time import warnings +from collections import defaultdict +from collections.abc import Iterable +from copy import copy +from typing import Any, Dict +from typing import Iterable as TIterable +from typing import List, Optional, Union, cast + import arviz -from arviz import InferenceData import numpy as np +import packaging import theano.gradient as tg -from theano.tensor import Tensor import xarray +from arviz import InferenceData +from fastprogress.fastprogress import progress_bar +from theano.tensor import Tensor + +import pymc3 as pm + +from pymc3.step_methods.hmc import quadpotential + from .backends.base import BaseTrace, MultiTrace from .backends.ndarray import NDArray from .distributions.distribution import draw_values from .distributions.posterior_predictive import fast_sample_posterior_predictive -from .model import modelcontext, Point, all_continuous, Model +from .exceptions import IncorrectArgumentsError, SamplingError +from .model import Model, Point, all_continuous, modelcontext +from .parallel_sampling import Draw, _cpu_count from .step_methods import ( NUTS, - HamiltonianMC, - Metropolis, - BinaryMetropolis, + PGBART, BinaryGibbsMetropolis, + BinaryMetropolis, CategoricalGibbsMetropolis, + CompoundStep, DEMetropolis, + HamiltonianMC, + Metropolis, Slice, - CompoundStep, arraystep, - PGBART, ) from .util import ( + chains_and_samples, check_start_vals, - update_start_vals, + dataset_to_point_list, + get_default_varnames, get_untransformed_name, is_transformed_name, - get_default_varnames, - dataset_to_point_list, - chains_and_samples, + update_start_vals, ) from .vartypes import discrete_types -from .exceptions import IncorrectArgumentsError, SamplingError -from .parallel_sampling import _cpu_count, Draw -from pymc3.step_methods.hmc import quadpotential -import pymc3 as pm -from fastprogress.fastprogress import progress_bar - - -import sys sys.setrecursionlimit(10000) diff --git a/pymc3/sampling_jax.py b/pymc3/sampling_jax.py index f7de7570d1..22e686ff88 100644 --- a/pymc3/sampling_jax.py +++ b/pymc3/sampling_jax.py @@ -37,9 +37,10 @@ def sample_tfp_nuts( num_tuning_epoch=2, num_compute_step_size=500, ): - from tensorflow_probability.substrates import jax as tfp import jax + from tensorflow_probability.substrates import jax as tfp + model = modelcontext(model) seed = jax.random.PRNGKey(random_seed) diff --git a/pymc3/smc/sample_smc.py b/pymc3/smc/sample_smc.py index cd97d6d8fa..9d84737bff 100644 --- a/pymc3/smc/sample_smc.py +++ b/pymc3/smc/sample_smc.py @@ -12,17 +12,19 @@ # See the License for the specific language governing permissions and # limitations under the License. -import time import logging +import multiprocessing as mp +import time import warnings + from collections.abc import Iterable -import multiprocessing as mp + import numpy as np -from .smc import SMC -from ..model import modelcontext from ..backends.base import MultiTrace +from ..model import modelcontext from ..parallel_sampling import _cpu_count +from .smc import SMC def sample_smc( diff --git a/pymc3/smc/smc.py b/pymc3/smc/smc.py index d606912295..546d9c0409 100644 --- a/pymc3/smc/smc.py +++ b/pymc3/smc/smc.py @@ -15,15 +15,16 @@ from collections import OrderedDict import numpy as np +import theano.tensor as tt + from scipy.special import logsumexp from scipy.stats import multivariate_normal from theano import function as theano_function -import theano.tensor as tt -from ..model import modelcontext, Point -from ..theanof import floatX, inputvars, make_shared_replacements, join_nonshared_inputs -from ..sampling import sample_prior_predictive from ..backends.ndarray import NDArray +from ..model import Point, modelcontext +from ..sampling import sample_prior_predictive +from ..theanof import floatX, inputvars, join_nonshared_inputs, make_shared_replacements class SMC: diff --git a/pymc3/step_methods/__init__.py b/pymc3/step_methods/__init__.py index a4afd48a3d..27266c4738 100644 --- a/pymc3/step_methods/__init__.py +++ b/pymc3/step_methods/__init__.py @@ -13,26 +13,23 @@ # limitations under the License. from .compound import CompoundStep - -from .hmc import HamiltonianMC, NUTS - -from .metropolis import Metropolis -from .metropolis import DEMetropolis, DEMetropolisZ -from .metropolis import BinaryMetropolis -from .metropolis import BinaryGibbsMetropolis -from .metropolis import CategoricalGibbsMetropolis -from .mlda import MLDA, MetropolisMLDA, DEMetropolisZMLDA, RecursiveDAProposal -from .metropolis import NormalProposal -from .metropolis import UniformProposal -from .metropolis import CauchyProposal -from .metropolis import LaplaceProposal -from .metropolis import PoissonProposal -from .metropolis import MultivariateNormalProposal - -from .gibbs import ElemwiseCategorical - -from .slicer import Slice - from .elliptical_slice import EllipticalSlice - +from .gibbs import ElemwiseCategorical +from .hmc import NUTS, HamiltonianMC +from .metropolis import ( + BinaryGibbsMetropolis, + BinaryMetropolis, + CategoricalGibbsMetropolis, + CauchyProposal, + DEMetropolis, + DEMetropolisZ, + LaplaceProposal, + Metropolis, + MultivariateNormalProposal, + NormalProposal, + PoissonProposal, + UniformProposal, +) +from .mlda import MLDA, DEMetropolisZMLDA, MetropolisMLDA, RecursiveDAProposal from .pgbart import PGBART +from .slicer import Slice diff --git a/pymc3/step_methods/arraystep.py b/pymc3/step_methods/arraystep.py index d3fc891270..d810460f23 100644 --- a/pymc3/step_methods/arraystep.py +++ b/pymc3/step_methods/arraystep.py @@ -12,14 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .compound import CompoundStep +from enum import IntEnum, unique + +import numpy as np + +from numpy.random import uniform + +from ..blocking import ArrayOrdering, DictToArrayBijection from ..model import modelcontext from ..theanof import inputvars -from ..blocking import ArrayOrdering, DictToArrayBijection from ..util import get_var_name -import numpy as np -from numpy.random import uniform -from enum import IntEnum, unique +from .compound import CompoundStep __all__ = ["ArrayStep", "ArrayStepShared", "metrop_select", "Competence"] diff --git a/pymc3/step_methods/compound.py b/pymc3/step_methods/compound.py index 15809ccda4..9e2975ab8b 100644 --- a/pymc3/step_methods/compound.py +++ b/pymc3/step_methods/compound.py @@ -18,6 +18,7 @@ @author: johnsalvatier """ from collections import namedtuple + import numpy as np diff --git a/pymc3/step_methods/elliptical_slice.py b/pymc3/step_methods/elliptical_slice.py index 65944cdf87..73f786e1eb 100644 --- a/pymc3/step_methods/elliptical_slice.py +++ b/pymc3/step_methods/elliptical_slice.py @@ -16,10 +16,10 @@ import numpy.random as nr import theano.tensor as tt -from .arraystep import ArrayStep, Competence +from ..distributions import draw_values from ..model import modelcontext from ..theanof import inputvars -from ..distributions import draw_values +from .arraystep import ArrayStep, Competence __all__ = ["EllipticalSlice"] diff --git a/pymc3/step_methods/gibbs.py b/pymc3/step_methods/gibbs.py index 4f1dc09f50..568be5e348 100644 --- a/pymc3/step_methods/gibbs.py +++ b/pymc3/step_methods/gibbs.py @@ -17,15 +17,26 @@ @author: john """ -from .arraystep import ArrayStep, Competence -from ..distributions.discrete import Categorical -from numpy import array, max, exp, cumsum, nested_iters, empty, searchsorted, ones, arange -from numpy.random import uniform from warnings import warn +from numpy import ( + arange, + array, + cumsum, + empty, + exp, + max, + nested_iters, + ones, + searchsorted, +) +from numpy.random import uniform from theano.gof.graph import inputs from theano.tensor import add + +from ..distributions.discrete import Categorical from ..model import modelcontext +from .arraystep import ArrayStep, Competence __all__ = ["ElemwiseCategorical"] diff --git a/pymc3/step_methods/hmc/base_hmc.py b/pymc3/step_methods/hmc/base_hmc.py index 734f51e055..ec0849df01 100644 --- a/pymc3/step_methods/hmc/base_hmc.py +++ b/pymc3/step_methods/hmc/base_hmc.py @@ -12,20 +12,22 @@ # See the License for the specific language governing permissions and # limitations under the License. -from collections import namedtuple +import logging import time +from collections import namedtuple + import numpy as np -import logging -from pymc3.model import modelcontext, Point -from pymc3.step_methods import arraystep -from pymc3.step_methods.hmc import integration -from pymc3.theanof import inputvars, floatX -from pymc3.tuning import guess_scaling -from .quadpotential import quad_potential, QuadPotentialDiagAdapt -from pymc3.step_methods import step_sizes + from pymc3.backends.report import SamplerWarning, WarningType from pymc3.exceptions import SamplingError +from pymc3.model import Point, modelcontext +from pymc3.step_methods import arraystep, step_sizes +from pymc3.step_methods.hmc import integration +from pymc3.theanof import floatX, inputvars +from pymc3.tuning import guess_scaling + +from .quadpotential import QuadPotentialDiagAdapt, quad_potential logger = logging.getLogger("pymc3") diff --git a/pymc3/step_methods/hmc/hmc.py b/pymc3/step_methods/hmc/hmc.py index 621e7454ce..129abbe123 100644 --- a/pymc3/step_methods/hmc/hmc.py +++ b/pymc3/step_methods/hmc/hmc.py @@ -14,11 +14,11 @@ import numpy as np -from ..arraystep import Competence -from pymc3.vartypes import discrete_types +from pymc3.step_methods.hmc.base_hmc import BaseHMC, DivergenceInfo, HMCStepData from pymc3.step_methods.hmc.integration import IntegrationError -from pymc3.step_methods.hmc.base_hmc import BaseHMC, HMCStepData, DivergenceInfo +from pymc3.vartypes import discrete_types +from ..arraystep import Competence __all__ = ["HamiltonianMC"] diff --git a/pymc3/step_methods/hmc/integration.py b/pymc3/step_methods/hmc/integration.py index 9cf37978a4..0043d6953a 100644 --- a/pymc3/step_methods/hmc/integration.py +++ b/pymc3/step_methods/hmc/integration.py @@ -15,8 +15,8 @@ from collections import namedtuple import numpy as np -from scipy import linalg +from scipy import linalg State = namedtuple("State", "q, p, v, q_grad, energy, model_logp") diff --git a/pymc3/step_methods/hmc/nuts.py b/pymc3/step_methods/hmc/nuts.py index 7ffbf106d4..3dbad28c0e 100644 --- a/pymc3/step_methods/hmc/nuts.py +++ b/pymc3/step_methods/hmc/nuts.py @@ -16,14 +16,15 @@ import numpy as np -from ..arraystep import Competence -from .base_hmc import BaseHMC, HMCStepData, DivergenceInfo -from .integration import IntegrationError from pymc3.backends.report import SamplerWarning, WarningType from pymc3.math import logbern, logdiffexp_numpy from pymc3.theanof import floatX from pymc3.vartypes import continuous_types + from ...distributions import BART +from ..arraystep import Competence +from .base_hmc import BaseHMC, DivergenceInfo, HMCStepData +from .integration import IntegrationError __all__ = ["NUTS"] diff --git a/pymc3/step_methods/hmc/quadpotential.py b/pymc3/step_methods/hmc/quadpotential.py index 7f6909ae1f..4c2e6acc7a 100644 --- a/pymc3/step_methods/hmc/quadpotential.py +++ b/pymc3/step_methods/hmc/quadpotential.py @@ -13,14 +13,15 @@ # limitations under the License. import warnings + import numpy as np -from numpy.random import normal import scipy.linalg -from scipy.sparse import issparse import theano -from pymc3.theanof import floatX +from numpy.random import normal +from scipy.sparse import issparse +from pymc3.theanof import floatX __all__ = [ "quad_potential", diff --git a/pymc3/step_methods/metropolis.py b/pymc3/step_methods/metropolis.py index e7634bd4b5..88622c7dfe 100644 --- a/pymc3/step_methods/metropolis.py +++ b/pymc3/step_methods/metropolis.py @@ -14,19 +14,21 @@ import numpy as np import numpy.random as nr -import theano import scipy.linalg +import theano + +import pymc3 as pm + +from pymc3.theanof import floatX from ..distributions import draw_values from .arraystep import ( + ArrayStep, ArrayStepShared, + Competence, PopulationArrayStepShared, - ArrayStep, metrop_select, - Competence, ) -import pymc3 as pm -from pymc3.theanof import floatX __all__ = [ "Metropolis", diff --git a/pymc3/step_methods/mlda.py b/pymc3/step_methods/mlda.py index aa74286701..2a8242e190 100644 --- a/pymc3/step_methods/mlda.py +++ b/pymc3/step_methods/mlda.py @@ -12,20 +12,23 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging +import warnings + +from typing import List, Optional, Type, Union + import arviz as az import numpy as np -import warnings -import logging -from typing import Union, List, Optional, Type import theano import theano.tensor as tt -from .arraystep import ArrayStepShared, metrop_select, Competence -from .compound import CompoundStep -from .metropolis import Proposal, Metropolis, DEMetropolisZ, delta_logp -from ..model import Model import pymc3 as pm +from ..model import Model +from .arraystep import ArrayStepShared, Competence, metrop_select +from .compound import CompoundStep +from .metropolis import DEMetropolisZ, Metropolis, Proposal, delta_logp + __all__ = [ "MetropolisMLDA", "DEMetropolisZMLDA", diff --git a/pymc3/step_methods/pgbart.py b/pymc3/step_methods/pgbart.py index 1d6a503c4c..15b8962b7a 100644 --- a/pymc3/step_methods/pgbart.py +++ b/pymc3/step_methods/pgbart.py @@ -15,13 +15,14 @@ import logging import numpy as np + from theano import function as theano_function -from .arraystep import ArrayStepShared, Competence from ..distributions import BART from ..distributions.tree import Tree from ..model import modelcontext -from ..theanof import inputvars, make_shared_replacements, join_nonshared_inputs +from ..theanof import inputvars, join_nonshared_inputs, make_shared_replacements +from .arraystep import ArrayStepShared, Competence _log = logging.getLogger("pymc3") diff --git a/pymc3/step_methods/sgmcmc.py b/pymc3/step_methods/sgmcmc.py index dff75b6621..daa1101652 100644 --- a/pymc3/step_methods/sgmcmc.py +++ b/pymc3/step_methods/sgmcmc.py @@ -12,14 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -from collections import OrderedDict import warnings -from .arraystep import ArrayStepShared -from ..model import modelcontext, inputvars -import theano.tensor as tt -from ..theanof import tt_rng, make_shared_replacements +from collections import OrderedDict + import theano +import theano.tensor as tt + +from ..model import inputvars, modelcontext +from ..theanof import make_shared_replacements, tt_rng +from .arraystep import ArrayStepShared __all__ = [] diff --git a/pymc3/step_methods/slicer.py b/pymc3/step_methods/slicer.py index f6e86e1fbe..f22026adad 100644 --- a/pymc3/step_methods/slicer.py +++ b/pymc3/step_methods/slicer.py @@ -17,10 +17,10 @@ import numpy as np import numpy.random as nr -from .arraystep import ArrayStep, Competence from ..model import modelcontext from ..theanof import inputvars from ..vartypes import continuous_types +from .arraystep import ArrayStep, Competence __all__ = ["Slice"] diff --git a/pymc3/step_methods/step_sizes.py b/pymc3/step_methods/step_sizes.py index 3603e30fa5..dbbbf4c16d 100644 --- a/pymc3/step_methods/step_sizes.py +++ b/pymc3/step_methods/step_sizes.py @@ -13,6 +13,7 @@ # limitations under the License. import numpy as np + from scipy import stats from pymc3.backends.report import SamplerWarning, WarningType diff --git a/pymc3/tests/backend_fixtures.py b/pymc3/tests/backend_fixtures.py index 919d53948c..6fd0b1318c 100644 --- a/pymc3/tests/backend_fixtures.py +++ b/pymc3/tests/backend_fixtures.py @@ -12,17 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -import numpy as np -import numpy.testing as npt +import collections import os import shutil -import collections -from pymc3.tests import models -from pymc3.backends import base +import numpy as np +import numpy.testing as npt import pytest import theano +from pymc3.backends import base +from pymc3.tests import models + class ModelBackendSetupTestCase: """Set up a backend trace. diff --git a/pymc3/tests/conftest.py b/pymc3/tests/conftest.py index 89484881dd..2964625691 100644 --- a/pymc3/tests/conftest.py +++ b/pymc3/tests/conftest.py @@ -13,9 +13,10 @@ # limitations under the License. import numpy as np +import pytest import theano + import pymc3 as pm -import pytest @pytest.fixture(scope="function", autouse=True) diff --git a/pymc3/tests/helpers.py b/pymc3/tests/helpers.py index 44c98e5e96..845eabd2a5 100644 --- a/pymc3/tests/helpers.py +++ b/pymc3/tests/helpers.py @@ -12,13 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -from logging.handlers import BufferingHandler import contextlib + +from logging.handlers import BufferingHandler + import numpy.random as nr -from theano.sandbox.rng_mrg import MRG_RandomStreams -from ..theanof import set_tt_rng, tt_rng import theano + from theano.gradient import verify_grad as tt_verify_grad +from theano.sandbox.rng_mrg import MRG_RandomStreams + +from ..theanof import set_tt_rng, tt_rng class SeededTest: diff --git a/pymc3/tests/models.py b/pymc3/tests/models.py index d9a66cee33..5f60734859 100644 --- a/pymc3/tests/models.py +++ b/pymc3/tests/models.py @@ -12,13 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -from pymc3 import Model, Normal, Categorical, Metropolis -import numpy as np -import pymc3 as pm from itertools import product -import theano.tensor as tt + +import numpy as np import theano +import theano.tensor as tt + from theano.compile.ops import as_op + +import pymc3 as pm + +from pymc3 import Categorical, Metropolis, Model, Normal from pymc3.theanof import floatX_array diff --git a/pymc3/tests/sampler_fixtures.py b/pymc3/tests/sampler_fixtures.py index 422b270d1d..2f71e7ffcc 100644 --- a/pymc3/tests/sampler_fixtures.py +++ b/pymc3/tests/sampler_fixtures.py @@ -12,13 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pymc3 as pm -from pymc3.util import get_var_name import numpy as np import numpy.testing as npt -from scipy import stats import theano.tensor as tt +from scipy import stats + +import pymc3 as pm + +from pymc3.util import get_var_name + from .helpers import SeededTest diff --git a/pymc3/tests/test_coords.py b/pymc3/tests/test_coords.py index 2f7f24b320..18d4a94e09 100644 --- a/pymc3/tests/test_coords.py +++ b/pymc3/tests/test_coords.py @@ -1,6 +1,7 @@ -import pymc3 as pm import numpy as np +import pymc3 as pm + def test_coords(): chains = 2 diff --git a/pymc3/tests/test_data_container.py b/pymc3/tests/test_data_container.py index 5b384ccc99..0fb54499c0 100644 --- a/pymc3/tests/test_data_container.py +++ b/pymc3/tests/test_data_container.py @@ -12,13 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pymc3 as pm -from ..theanof import floatX -from .helpers import SeededTest import numpy as np import pandas as pd import pytest +import pymc3 as pm + +from ..theanof import floatX +from .helpers import SeededTest + class TestData(SeededTest): def test_deterministic(self): diff --git a/pymc3/tests/test_dist_math.py b/pymc3/tests/test_dist_math.py index 599bb2bc88..454b1ae4bf 100644 --- a/pymc3/tests/test_dist_math.py +++ b/pymc3/tests/test_dist_math.py @@ -14,23 +14,25 @@ import numpy as np import numpy.testing as npt -import theano.tensor as tt +import pytest import theano +import theano.tensor as tt + +from scipy import interpolate, stats + import pymc3 as pm -from scipy import stats, interpolate -import pytest -from ..theanof import floatX from ..distributions import Discrete from ..distributions.dist_math import ( - bound, - factln, - alltrue_scalar, MvNormalLogp, SplineWrapper, - i0e, + alltrue_scalar, + bound, clipped_beta_rvs, + factln, + i0e, ) +from ..theanof import floatX from .helpers import verify_grad diff --git a/pymc3/tests/test_distribution_defaults.py b/pymc3/tests/test_distribution_defaults.py index 5ae8b3bf82..1192aa618c 100644 --- a/pymc3/tests/test_distribution_defaults.py +++ b/pymc3/tests/test_distribution_defaults.py @@ -12,12 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ..model import Model -from ..distributions import DiscreteUniform, Continuous, Categorical - import numpy as np import pytest +from ..distributions import Categorical, Continuous, DiscreteUniform +from ..model import Model + class DistTest(Continuous): def __init__(self, a, b, *args, **kwargs): diff --git a/pymc3/tests/test_distributions.py b/pymc3/tests/test_distributions.py index f7210264f1..a69079f6da 100644 --- a/pymc3/tests/test_distributions.py +++ b/pymc3/tests/test_distributions.py @@ -15,85 +15,87 @@ import itertools import sys -from .helpers import SeededTest, select_by_precision -from ..vartypes import continuous_types -from ..model import Model, Point, Deterministic +import numpy as np +import numpy.random as nr +import pytest +import scipy.stats +import scipy.stats.distributions as sp +import theano +import theano.tensor as tt + +from numpy import array, exp, inf, log +from numpy.testing import assert_allclose, assert_almost_equal, assert_equal +from scipy import integrate +from scipy.special import logit + +import pymc3 as pm + +from pymc3.theanof import floatX + from ..blocking import DictToVarBijection from ..distributions import ( - DensityDist, - Categorical, - Multinomial, - VonMises, - Dirichlet, - MvStudentT, - MvNormal, - MatrixNormal, - ZeroInflatedPoisson, - ZeroInflatedNegativeBinomial, - Constant, - Poisson, + AR1, Bernoulli, Beta, BetaBinomial, - HalfStudentT, - StudentT, - Weibull, - Pareto, - InverseGamma, - Gamma, + Binomial, + Bound, + Categorical, Cauchy, - HalfCauchy, - Lognormal, - Laplace, - NegativeBinomial, - Geometric, - Exponential, - ExGaussian, - Normal, - TruncatedNormal, - Flat, - LKJCorr, - Wald, ChiSquared, - HalfNormal, + Constant, + DensityDist, + Dirichlet, DiscreteUniform, - Bound, - Uniform, - Triangular, - Binomial, - SkewNormal, DiscreteWeibull, + ExGaussian, + Exponential, + Flat, + Gamma, + Geometric, Gumbel, - Logistic, - OrderedLogistic, - LogitNormal, - Interpolated, - ZeroInflatedBinomial, + HalfCauchy, HalfFlat, - AR1, + HalfNormal, + HalfStudentT, + HyperGeometric, + Interpolated, + InverseGamma, KroneckerNormal, - Rice, Kumaraswamy, + Laplace, + LKJCorr, + Logistic, + LogitNormal, + Lognormal, + MatrixNormal, Moyal, - HyperGeometric, + Multinomial, + MvNormal, + MvStudentT, + NegativeBinomial, + Normal, + OrderedLogistic, + Pareto, + Poisson, + Rice, + SkewNormal, + StudentT, + Triangular, + TruncatedNormal, + Uniform, + VonMises, + Wald, + Weibull, + ZeroInflatedBinomial, + ZeroInflatedNegativeBinomial, + ZeroInflatedPoisson, + continuous, ) - -from ..distributions import continuous -from pymc3.theanof import floatX -import pymc3 as pm -from numpy import array, inf, log, exp -from numpy.testing import assert_almost_equal, assert_allclose, assert_equal -import numpy.random as nr -import numpy as np -import pytest - -from scipy import integrate -import scipy.stats.distributions as sp -import scipy.stats -from scipy.special import logit -import theano -import theano.tensor as tt from ..math import kronecker +from ..model import Deterministic, Model, Point +from ..vartypes import continuous_types +from .helpers import SeededTest, select_by_precision def get_lkj_cases(): diff --git a/pymc3/tests/test_distributions_random.py b/pymc3/tests/test_distributions_random.py index 2b9358787c..d37375c45e 100644 --- a/pymc3/tests/test_distributions_random.py +++ b/pymc3/tests/test_distributions_random.py @@ -13,45 +13,49 @@ # limitations under the License. import itertools -import pytest +import sys + import numpy as np +import numpy.random as nr import numpy.testing as npt +import pytest import scipy.stats as st -from scipy.special import expit -from scipy import linalg -import numpy.random as nr import theano -import sys + +from scipy import linalg +from scipy.special import expit import pymc3 as pm + from pymc3.distributions.dist_math import clipped_beta_rvs from pymc3.distributions.distribution import ( - draw_values, _DrawValuesContext, _DrawValuesContextBlocker, + draw_values, to_tuple, ) + from .helpers import SeededTest from .test_distributions import ( - build_model, Domain, - product, - R, - Rplus, - Rplusbig, - Runif, - Rplusdunif, - Unit, + I, Nat, NatSmall, - I, - Simplex, - Vector, PdMatrix, PdMatrixChol, PdMatrixCholUpper, - RealMatrix, + R, RandomPdMatrix, + RealMatrix, + Rplus, + Rplusbig, + Rplusdunif, + Runif, + Simplex, + Unit, + Vector, + build_model, + product, ) diff --git a/pymc3/tests/test_distributions_timeseries.py b/pymc3/tests/test_distributions_timeseries.py index c55f71a388..9c47e511f0 100644 --- a/pymc3/tests/test_distributions_timeseries.py +++ b/pymc3/tests/test_distributions_timeseries.py @@ -12,14 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ..model import Model -from ..distributions.continuous import Flat, Normal -from ..distributions.timeseries import EulerMaruyama, AR1, AR, GARCH11 -from ..sampling import sample, sample_posterior_predictive, fast_sample_posterior_predictive -from ..theanof import floatX - import numpy as np import pytest + +from ..distributions.continuous import Flat, Normal +from ..distributions.timeseries import AR, AR1, GARCH11, EulerMaruyama +from ..model import Model +from ..sampling import ( + fast_sample_posterior_predictive, + sample, + sample_posterior_predictive, +) +from ..theanof import floatX from .helpers import select_by_precision pytestmark = pytest.mark.usefixtures("seeded_test") diff --git a/pymc3/tests/test_examples.py b/pymc3/tests/test_examples.py index d31b2bfd3a..c24abfdf2b 100644 --- a/pymc3/tests/test_examples.py +++ b/pymc3/tests/test_examples.py @@ -15,13 +15,16 @@ import matplotlib import numpy as np import pandas as pd -import pymc3 as pm -import theano.tensor as tt import pytest import theano -from pymc3.theanof import floatX +import theano.tensor as tt + from packaging import version +import pymc3 as pm + +from pymc3.theanof import floatX + from .helpers import SeededTest if version.parse(matplotlib.__version__) < version.parse("3.3"): diff --git a/pymc3/tests/test_glm.py b/pymc3/tests/test_glm.py index 3d66211b8d..eea40aed33 100644 --- a/pymc3/tests/test_glm.py +++ b/pymc3/tests/test_glm.py @@ -13,13 +13,26 @@ # limitations under the License. import numpy as np +import pandas as pd + from numpy.testing import assert_equal -from .helpers import SeededTest import pymc3 -from pymc3 import Model, Uniform, Normal, find_MAP, Slice, sample -from pymc3 import families, GLM, LinearComponent -import pandas as pd + +from pymc3 import ( + GLM, + LinearComponent, + Model, + Normal, + Slice, + Uniform, + families, + find_MAP, + sample, +) + +from .helpers import SeededTest + # Generate data def generate_data(intercept, slope, size=700): diff --git a/pymc3/tests/test_gp.py b/pymc3/tests/test_gp.py index 7aed383422..a57b84b234 100644 --- a/pymc3/tests/test_gp.py +++ b/pymc3/tests/test_gp.py @@ -14,14 +14,17 @@ # pylint:disable=unused-variable from functools import reduce -from ..math import cartesian, kronecker from operator import add -import pymc3 as pm -import theano -import theano.tensor as tt + import numpy as np import numpy.testing as npt import pytest +import theano +import theano.tensor as tt + +import pymc3 as pm + +from ..math import cartesian, kronecker np.random.seed(101) diff --git a/pymc3/tests/test_hmc.py b/pymc3/tests/test_hmc.py index 384501f2fd..f06cce6237 100644 --- a/pymc3/tests/test_hmc.py +++ b/pymc3/tests/test_hmc.py @@ -12,15 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging + import numpy as np import numpy.testing as npt -from . import models -from pymc3.step_methods.hmc.base_hmc import BaseHMC import pymc3 -import logging + +from pymc3.step_methods.hmc.base_hmc import BaseHMC from pymc3.theanof import floatX +from . import models + logger = logging.getLogger("pymc3") diff --git a/pymc3/tests/test_math.py b/pymc3/tests/test_math.py index 7783b65261..e0bfeb6eff 100644 --- a/pymc3/tests/test_math.py +++ b/pymc3/tests/test_math.py @@ -14,26 +14,28 @@ import numpy as np import numpy.testing as npt +import pytest import theano import theano.tensor as tt + from pymc3.math import ( LogDet, - logdet, - probit, - invprobit, - expand_packed_triangular, - log1pexp, - log1mexp, - log1mexp_numpy, - kronecker, cartesian, + expand_packed_triangular, + invprobit, kron_dot, kron_solve_lower, + kronecker, + log1mexp, + log1mexp_numpy, + log1pexp, + logdet, + probit, ) -from .helpers import SeededTest, verify_grad -import pytest from pymc3.theanof import floatX +from .helpers import SeededTest, verify_grad + def test_kronecker(): np.random.seed(1) diff --git a/pymc3/tests/test_memo.py b/pymc3/tests/test_memo.py index 4d1955a442..6653662e32 100644 --- a/pymc3/tests/test_memo.py +++ b/pymc3/tests/test_memo.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import numpy as np + import pymc3 as pm from pymc3 import memoize diff --git a/pymc3/tests/test_minibatches.py b/pymc3/tests/test_minibatches.py index e2254da847..34dadaa8eb 100644 --- a/pymc3/tests/test_minibatches.py +++ b/pymc3/tests/test_minibatches.py @@ -17,12 +17,14 @@ import numpy as np import pytest +import theano + from scipy import stats as stats from theano import tensor as tt -import theano import pymc3 as pm -from pymc3 import floatX, GeneratorAdapter, generator, tt_rng, Normal + +from pymc3 import GeneratorAdapter, Normal, floatX, generator, tt_rng from pymc3.tests.helpers import select_by_precision from pymc3.theanof import GeneratorOp diff --git a/pymc3/tests/test_missing.py b/pymc3/tests/test_missing.py index adb54822d2..65248f6b2d 100644 --- a/pymc3/tests/test_missing.py +++ b/pymc3/tests/test_missing.py @@ -12,11 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pytest -from numpy import ma, array import numpy import pandas as pd -from pymc3 import Model, Normal, sample_prior_predictive, sample, ImputationWarning +import pytest + +from numpy import array, ma + +from pymc3 import ImputationWarning, Model, Normal, sample, sample_prior_predictive def test_missing(): diff --git a/pymc3/tests/test_mixture.py b/pymc3/tests/test_mixture.py index 9f016ad333..b36d0dbe25 100644 --- a/pymc3/tests/test_mixture.py +++ b/pymc3/tests/test_mixture.py @@ -12,32 +12,36 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pytest import numpy as np +import pytest +import scipy.stats as st +import theano + from numpy.testing import assert_allclose +from scipy.special import logsumexp +from theano import tensor as tt -from .helpers import SeededTest import pymc3 as pm + from pymc3 import ( Dirichlet, + Exponential, Gamma, - Normal, Lognormal, - Poisson, - Exponential, + Metropolis, Mixture, - NormalMixture, + Model, MvNormal, + Normal, + NormalMixture, + Poisson, sample, - Metropolis, - Model, ) -import scipy.stats as st -from scipy.special import logsumexp -from pymc3.theanof import floatX -import theano -from theano import tensor as tt from pymc3.distributions.shape_utils import to_tuple +from pymc3.theanof import floatX + +from .helpers import SeededTest + # Generate data def generate_normal_mixture_data(w, mu, sd, size=1000): diff --git a/pymc3/tests/test_model.py b/pymc3/tests/test_model.py index 1cea9840fd..5a57306721 100644 --- a/pymc3/tests/test_model.py +++ b/pymc3/tests/test_model.py @@ -12,19 +12,22 @@ # See the License for the specific language governing permissions and # limitations under the License. +import pickle +import unittest + +import numpy as np +import numpy.testing as npt +import pandas as pd import pytest import theano import theano.tensor as tt -import numpy as np -import pickle -import pandas as pd -import numpy.testing as npt -import unittest import pymc3 as pm + +from pymc3 import Deterministic, Potential from pymc3.distributions import HalfCauchy, Normal, transforms -from pymc3 import Potential, Deterministic from pymc3.model import ValueGradFunction + from .helpers import select_by_precision diff --git a/pymc3/tests/test_model_func.py b/pymc3/tests/test_model_func.py index 5c167c099a..3879fc2199 100644 --- a/pymc3/tests/test_model_func.py +++ b/pymc3/tests/test_model_func.py @@ -12,12 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pymc3 as pm import numpy as np import scipy.stats as sp -from .checks import close_to -from .models import simple_model, mv_simple +import pymc3 as pm + +from .checks import close_to +from .models import mv_simple, simple_model tol = 2.0 ** -11 diff --git a/pymc3/tests/test_model_graph.py b/pymc3/tests/test_model_graph.py index 4ecf10c8b5..09b673e973 100644 --- a/pymc3/tests/test_model_graph.py +++ b/pymc3/tests/test_model_graph.py @@ -13,9 +13,12 @@ # limitations under the License. import numpy as np -import pymc3 as pm import theano as th + +import pymc3 as pm + from pymc3.model_graph import ModelGraph, model_to_graphviz + from .helpers import SeededTest diff --git a/pymc3/tests/test_model_helpers.py b/pymc3/tests/test_model_helpers.py index 676cd75e7a..62312c4428 100644 --- a/pymc3/tests/test_model_helpers.py +++ b/pymc3/tests/test_model_helpers.py @@ -16,12 +16,12 @@ import numpy.ma as ma import numpy.testing as npt import pandas as pd -import pymc3 as pm import scipy.sparse as sps - import theano -import theano.tensor as tt import theano.sparse as sparse +import theano.tensor as tt + +import pymc3 as pm class TestHelperFunc: diff --git a/pymc3/tests/test_modelcontext.py b/pymc3/tests/test_modelcontext.py index d7cc4a6166..b7d44ca63c 100644 --- a/pymc3/tests/test_modelcontext.py +++ b/pymc3/tests/test_modelcontext.py @@ -13,9 +13,14 @@ # limitations under the License. import threading + from pytest import raises + from pymc3 import Model, Normal -from pymc3.distributions.distribution import _DrawValuesContext, _DrawValuesContextBlocker +from pymc3.distributions.distribution import ( + _DrawValuesContext, + _DrawValuesContextBlocker, +) from pymc3.model import modelcontext diff --git a/pymc3/tests/test_models_linear.py b/pymc3/tests/test_models_linear.py index b95795a35a..fdd1b920b9 100644 --- a/pymc3/tests/test_models_linear.py +++ b/pymc3/tests/test_models_linear.py @@ -13,11 +13,13 @@ # limitations under the License. import numpy as np -from .helpers import SeededTest -from pymc3 import Model, Uniform, Normal, find_MAP, Slice, sample -from pymc3.glm import LinearComponent, GLM import pytest +from pymc3 import Model, Normal, Slice, Uniform, find_MAP, sample +from pymc3.glm import GLM, LinearComponent + +from .helpers import SeededTest + # Generate data def generate_data(intercept, slope, size=700): diff --git a/pymc3/tests/test_models_utils.py b/pymc3/tests/test_models_utils.py index 4b1e5f42bc..84d25b3c2b 100644 --- a/pymc3/tests/test_models_utils.py +++ b/pymc3/tests/test_models_utils.py @@ -14,9 +14,10 @@ import numpy as np import pandas as pd +import pytest import theano.tensor as tt + from pymc3.glm import utils -import pytest class TestUtils: diff --git a/pymc3/tests/test_ndarray_backend.py b/pymc3/tests/test_ndarray_backend.py index 7a0a0bfc5d..1b13aa0b0f 100644 --- a/pymc3/tests/test_ndarray_backend.py +++ b/pymc3/tests/test_ndarray_backend.py @@ -14,11 +14,12 @@ import numpy as np import numpy.testing as npt -from pymc3.tests import backend_fixtures as bf -from pymc3.backends import base, ndarray -import pymc3 as pm import pytest +import pymc3 as pm + +from pymc3.backends import base, ndarray +from pymc3.tests import backend_fixtures as bf STATS1 = [{"a": np.float64, "b": np.bool}] diff --git a/pymc3/tests/test_ode.py b/pymc3/tests/test_ode.py index 0fbc21232d..b498ce1d33 100644 --- a/pymc3/tests/test_ode.py +++ b/pymc3/tests/test_ode.py @@ -12,14 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ..ode import DifferentialEquation -from ..ode.utils import augment_system import numpy as np +import pytest +import theano + from scipy.integrate import odeint from scipy.stats import norm + import pymc3 as pm -import theano -import pytest + +from ..ode import DifferentialEquation +from ..ode.utils import augment_system def test_gradients(): diff --git a/pymc3/tests/test_parallel_sampling.py b/pymc3/tests/test_parallel_sampling.py index 21715d6591..4d5403a940 100644 --- a/pymc3/tests/test_parallel_sampling.py +++ b/pymc3/tests/test_parallel_sampling.py @@ -14,12 +14,13 @@ import multiprocessing import os +import numpy as np import pytest -import pymc3.parallel_sampling as ps -import pymc3 as pm import theano import theano.tensor as tt -import numpy as np + +import pymc3 as pm +import pymc3.parallel_sampling as ps def test_context(): diff --git a/pymc3/tests/test_pickling.py b/pymc3/tests/test_pickling.py index 7cb56c4a31..20eaa1d5d0 100644 --- a/pymc3/tests/test_pickling.py +++ b/pymc3/tests/test_pickling.py @@ -14,6 +14,7 @@ import pickle import traceback + from .models import simple_model diff --git a/pymc3/tests/test_posdef_sym.py b/pymc3/tests/test_posdef_sym.py index a71c500649..a7aa714357 100644 --- a/pymc3/tests/test_posdef_sym.py +++ b/pymc3/tests/test_posdef_sym.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from pymc3.distributions import multivariate as mv -import theano import numpy as np +import theano + +from pymc3.distributions import multivariate as mv def test_posdef_symmetric1(): diff --git a/pymc3/tests/test_posterior_predictive.py b/pymc3/tests/test_posterior_predictive.py index 7e812c7955..7a19ac4a59 100644 --- a/pymc3/tests/test_posterior_predictive.py +++ b/pymc3/tests/test_posterior_predictive.py @@ -1,8 +1,9 @@ -import pymc3 as pm -from pymc3.distributions.posterior_predictive import _TraceDict import numpy as np +import pymc3 as pm + from pymc3.backends.ndarray import point_list_to_multitrace +from pymc3.distributions.posterior_predictive import _TraceDict def test_translate_point_list(): diff --git a/pymc3/tests/test_posteriors.py b/pymc3/tests/test_posteriors.py index c4281a32ab..e484a9a4b0 100644 --- a/pymc3/tests/test_posteriors.py +++ b/pymc3/tests/test_posteriors.py @@ -13,9 +13,10 @@ # limitations under the License. import pytest -from . import sampler_fixtures as sf import theano +from . import sampler_fixtures as sf + @pytest.mark.xfail(condition=(theano.config.floatX == "float32"), reason="Fails on float32") class TestNUTSUniform(sf.NutsFixture, sf.UniformFixture): diff --git a/pymc3/tests/test_quadpotential.py b/pymc3/tests/test_quadpotential.py index c964f05f83..d91a80b5e9 100644 --- a/pymc3/tests/test_quadpotential.py +++ b/pymc3/tests/test_quadpotential.py @@ -13,13 +13,14 @@ # limitations under the License. import numpy as np +import numpy.testing as npt +import pytest import scipy.sparse -from pymc3.step_methods.hmc import quadpotential import pymc3 + +from pymc3.step_methods.hmc import quadpotential from pymc3.theanof import floatX -import pytest -import numpy.testing as npt def test_elemwise_posdef(): diff --git a/pymc3/tests/test_random.py b/pymc3/tests/test_random.py index 5e47961a5e..b21d58c081 100644 --- a/pymc3/tests/test_random.py +++ b/pymc3/tests/test_random.py @@ -12,15 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pymc3 as pm import numpy as np -from numpy import random as nr import numpy.testing as npt import pytest -import theano.tensor as tt import theano +import theano.tensor as tt + +from numpy import random as nr + +import pymc3 as pm from pymc3.distributions.distribution import _draw_value, draw_values + from .helpers import SeededTest diff --git a/pymc3/tests/test_sampling.py b/pymc3/tests/test_sampling.py index 2fd2d916c3..a167b4f759 100644 --- a/pymc3/tests/test_sampling.py +++ b/pymc3/tests/test_sampling.py @@ -12,23 +12,27 @@ # See the License for the specific language governing permissions and # limitations under the License. +import unittest.mock as mock + from contextlib import ExitStack as does_not_raise from itertools import combinations from typing import Tuple -import numpy as np -import unittest.mock as mock -import numpy.testing as npt import arviz as az -import pymc3 as pm +import numpy as np +import numpy.testing as npt +import pytest +import theano import theano.tensor as tt + +from scipy import stats from theano import shared -import theano -from pymc3.tests.models import simple_init -from pymc3.tests.helpers import SeededTest + +import pymc3 as pm + from pymc3.exceptions import IncorrectArgumentsError, SamplingError -from scipy import stats -import pytest +from pymc3.tests.helpers import SeededTest +from pymc3.tests.models import simple_init @pytest.mark.xfail(condition=(theano.config.floatX == "float32"), reason="Fails on float32") diff --git a/pymc3/tests/test_shape_handling.py b/pymc3/tests/test_shape_handling.py index 50ed4a24fd..f5d830617a 100644 --- a/pymc3/tests/test_shape_handling.py +++ b/pymc3/tests/test_shape_handling.py @@ -12,17 +12,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pytest import numpy as np +import pytest + from theano import tensor as tt + import pymc3 as pm + from pymc3.distributions.shape_utils import ( - to_tuple, - shapes_broadcasting, broadcast_dist_samples_shape, - get_broadcastable_dist_samples, - broadcast_distribution_samples, broadcast_dist_samples_to, + broadcast_distribution_samples, + get_broadcastable_dist_samples, + shapes_broadcasting, + to_tuple, ) test_shapes = [ diff --git a/pymc3/tests/test_shared.py b/pymc3/tests/test_shared.py index ded4861934..6be4d26577 100644 --- a/pymc3/tests/test_shared.py +++ b/pymc3/tests/test_shared.py @@ -12,11 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pymc3 as pm -from .helpers import SeededTest import numpy as np import theano +import pymc3 as pm + +from .helpers import SeededTest + class TestShared(SeededTest): def test_deterministic(self): diff --git a/pymc3/tests/test_smc.py b/pymc3/tests/test_smc.py index 532a2844ac..c17db977a2 100644 --- a/pymc3/tests/test_smc.py +++ b/pymc3/tests/test_smc.py @@ -12,9 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pymc3 as pm import numpy as np import theano.tensor as tt + +import pymc3 as pm + from .helpers import SeededTest diff --git a/pymc3/tests/test_special_functions.py b/pymc3/tests/test_special_functions.py index 9997135479..b7e5fe2582 100644 --- a/pymc3/tests/test_special_functions.py +++ b/pymc3/tests/test_special_functions.py @@ -12,12 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -from theano import function +import numpy as np +import scipy.special as ss import theano.tensor as tt +from theano import function + import pymc3.distributions.special as ps -import scipy.special as ss -import numpy as np from .checks import close_to diff --git a/pymc3/tests/test_starting.py b/pymc3/tests/test_starting.py index 5cc1ae3d77..740cd696cc 100644 --- a/pymc3/tests/test_starting.py +++ b/pymc3/tests/test_starting.py @@ -12,15 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .checks import close_to import numpy as np -from pymc3.tuning import starting -from pymc3 import Model, Uniform, Normal, Beta, Binomial, find_MAP, Point -from .models import simple_model, non_normal, simple_arbitrary_det -from .helpers import select_by_precision from pytest import raises +from pymc3 import Beta, Binomial, Model, Normal, Point, Uniform, find_MAP +from pymc3.tuning import starting + +from .checks import close_to +from .helpers import select_by_precision +from .models import non_normal, simple_arbitrary_det, simple_model + def test_accuracy_normal(): _, model, (mu, _) = simple_model() diff --git a/pymc3/tests/test_step.py b/pymc3/tests/test_step.py index e115bdcb17..eb1fe5e256 100644 --- a/pymc3/tests/test_step.py +++ b/pymc3/tests/test_step.py @@ -13,54 +13,64 @@ # limitations under the License. import shutil -import tempfile import sys +import tempfile -from .checks import close_to -from .models import ( - simple_categorical, - mv_simple, - mv_simple_coarse, - mv_simple_very_coarse, - mv_simple_discrete, - mv_prior_simple, - simple_2model_continuous, +from math import isclose + +import arviz as az +import numpy as np +import numpy.testing as npt +import pytest +import theano +import theano.tensor as tt + +from numpy.testing import assert_array_almost_equal + +from pymc3.data import Data +from pymc3.distributions import ( + Bernoulli, + Beta, + Binomial, + Categorical, + HalfNormal, + MvNormal, + Normal, ) -from pymc3.sampling import assign_step_methods, sample from pymc3.exceptions import SamplingError from pymc3.model import Model, Potential, set_data - +from pymc3.sampling import assign_step_methods, sample from pymc3.step_methods import ( + MLDA, NUTS, BinaryGibbsMetropolis, CategoricalGibbsMetropolis, - Metropolis, - Slice, CompoundStep, - NormalProposal, - UniformProposal, - MultivariateNormalProposal, - RecursiveDAProposal, - HamiltonianMC, - EllipticalSlice, DEMetropolis, DEMetropolisZ, - MLDA, + EllipticalSlice, + HamiltonianMC, + Metropolis, + MultivariateNormalProposal, + NormalProposal, + RecursiveDAProposal, + Slice, + UniformProposal, ) from pymc3.step_methods.mlda import extract_Q_estimate from pymc3.theanof import floatX -from pymc3.distributions import Binomial, Normal, Bernoulli, Categorical, Beta, HalfNormal, MvNormal -from pymc3.data import Data -from numpy.testing import assert_array_almost_equal -import arviz as az -import numpy as np -import numpy.testing as npt -import pytest -import theano -import theano.tensor as tt +from .checks import close_to from .helpers import select_by_precision -from math import isclose +from .models import ( + mv_prior_simple, + mv_simple, + mv_simple_coarse, + mv_simple_discrete, + mv_simple_very_coarse, + simple_2model_continuous, + simple_categorical, +) class TestStepMethods: # yield test doesn't work subclassing object diff --git a/pymc3/tests/test_theanof.py b/pymc3/tests/test_theanof.py index 82dbf51750..8d51554adb 100644 --- a/pymc3/tests/test_theanof.py +++ b/pymc3/tests/test_theanof.py @@ -13,17 +13,17 @@ # limitations under the License. import collections -import pytest + from itertools import product +import numpy as np +import pytest import theano import theano.tensor as tt -import numpy as np -from pymc3.theanof import set_theano_conf, take_along_axis, _conversion_map +from pymc3.theanof import _conversion_map, set_theano_conf, take_along_axis from pymc3.vartypes import int_types - FLOATX = str(theano.config.floatX) INTX = str(_conversion_map[FLOATX]) diff --git a/pymc3/tests/test_tracetab.py b/pymc3/tests/test_tracetab.py index cdfdafd5ab..cff6b33b3c 100644 --- a/pymc3/tests/test_tracetab.py +++ b/pymc3/tests/test_tracetab.py @@ -14,9 +14,9 @@ import numpy.testing as npt -from pymc3.tests import backend_fixtures as bf from pymc3.backends import ndarray from pymc3.backends import tracetab as ttab +from pymc3.tests import backend_fixtures as bf class TestTraceToDf(bf.ModelBackendSampledTestCase): diff --git a/pymc3/tests/test_transforms.py b/pymc3/tests/test_transforms.py index c375a30449..5e4840d0f5 100644 --- a/pymc3/tests/test_transforms.py +++ b/pymc3/tests/test_transforms.py @@ -12,29 +12,29 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pymc3.distributions.transforms as tr -import pymc3 as pm import numpy as np import pytest - import theano import theano.tensor as tt + +import pymc3 as pm +import pymc3.distributions.transforms as tr + +from ..theanof import jacobian +from .checks import close_to, close_to_logical from .helpers import SeededTest from .test_distributions import ( - Simplex, - Rplusbig, - Rminusbig, - Unit, - R, - Vector, - MultiSimplex, Circ, + MultiSimplex, + R, + Rminusbig, + Rplusbig, + Simplex, SortedVector, + Unit, UnitSortedVector, + Vector, ) -from .checks import close_to, close_to_logical -from ..theanof import jacobian - # some transforms (stick breaking) require additon of small slack in order to be numerically # stable. The minimal addable slack for float32 is higher thus we need to be less strict diff --git a/pymc3/tests/test_tuning.py b/pymc3/tests/test_tuning.py index ad73c39ace..1d9cf0993a 100644 --- a/pymc3/tests/test_tuning.py +++ b/pymc3/tests/test_tuning.py @@ -13,9 +13,12 @@ # limitations under the License. import numpy as np + from numpy import inf + from pymc3.step_methods.metropolis import tune -from pymc3.tuning import scaling, find_MAP +from pymc3.tuning import find_MAP, scaling + from . import models diff --git a/pymc3/tests/test_types.py b/pymc3/tests/test_types.py index 644767f081..98153fca7c 100644 --- a/pymc3/tests/test_types.py +++ b/pymc3/tests/test_types.py @@ -14,16 +14,15 @@ from copy import copy +import numpy as np import theano -from pymc3.sampling import sample -from pymc3.model import Model -from pymc3.step_methods import NUTS, Metropolis, Slice, HamiltonianMC, MLDA from pymc3.distributions import Normal +from pymc3.model import Model +from pymc3.sampling import sample +from pymc3.step_methods import MLDA, NUTS, HamiltonianMC, Metropolis, Slice from pymc3.theanof import change_flags -import numpy as np - class TestType: samplers = (Metropolis, Slice, HamiltonianMC, NUTS) diff --git a/pymc3/tests/test_updates.py b/pymc3/tests/test_updates.py index 807171c6aa..371ab354b5 100644 --- a/pymc3/tests/test_updates.py +++ b/pymc3/tests/test_updates.py @@ -12,20 +12,22 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pytest import numpy as np +import pytest import theano + from theano.configparser import change_flags + from pymc3.variational.updates import ( - sgd, - momentum, - nesterov_momentum, - adagrad, - rmsprop, adadelta, + adagrad, + adagrad_window, adam, adamax, - adagrad_window, + momentum, + nesterov_momentum, + rmsprop, + sgd, ) _a = theano.shared(1.0) diff --git a/pymc3/tests/test_util.py b/pymc3/tests/test_util.py index 63fda20457..2132de5319 100644 --- a/pymc3/tests/test_util.py +++ b/pymc3/tests/test_util.py @@ -12,14 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. +import numpy as np import pytest -import pymc3 as pm -import numpy as np from numpy.testing import assert_almost_equal -from .helpers import SeededTest + +import pymc3 as pm + from pymc3.distributions.transforms import Transform +from .helpers import SeededTest + class TestTransformName: cases = [("var", "var_test__"), ("var_test_", "var_test__test__")] diff --git a/pymc3/tests/test_variational_inference.py b/pymc3/tests/test_variational_inference.py index 07437f642c..fb88649ef3 100644 --- a/pymc3/tests/test_variational_inference.py +++ b/pymc3/tests/test_variational_inference.py @@ -12,36 +12,34 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pytest import functools import io import operator + import numpy as np +import pytest import theano import theano.tensor as tt - import pymc3 as pm import pymc3.memoize import pymc3.util -from pymc3.theanof import ( - change_flags, - intX, -) + +from pymc3.theanof import change_flags, intX +from pymc3.variational import flows, opvi from pymc3.variational.approximations import ( - MeanFieldGroup, - FullRankGroup, - NormalizingFlowGroup, + Empirical, EmpiricalGroup, - MeanField, FullRank, + FullRankGroup, + MeanField, + MeanFieldGroup, NormalizingFlow, - Empirical, + NormalizingFlowGroup, ) -from pymc3.variational.inference import ADVI, FullRankADVI, SVGD, NFVI, ASVGD, fit -from pymc3.variational import flows +from pymc3.variational.inference import ADVI, ASVGD, NFVI, SVGD, FullRankADVI, fit from pymc3.variational.opvi import Approximation, Group -from pymc3.variational import opvi + from . import models from .helpers import not_raises diff --git a/pymc3/theanof.py b/pymc3/theanof.py index c11ddae0dd..9294c0213b 100644 --- a/pymc3/theanof.py +++ b/pymc3/theanof.py @@ -14,7 +14,9 @@ import numpy as np import theano -from theano import scalar, tensor as tt + +from theano import scalar +from theano import tensor as tt from theano.configparser import change_flags from theano.gof import Op from theano.gof.graph import inputs @@ -22,7 +24,7 @@ from .blocking import ArrayOrdering from .data import GeneratorAdapter -from .vartypes import typefilter, continuous_types, int_types +from .vartypes import continuous_types, int_types, typefilter __all__ = [ "gradient", diff --git a/pymc3/tuning/__init__.py b/pymc3/tuning/__init__.py index 94330badd1..37071f76f2 100644 --- a/pymc3/tuning/__init__.py +++ b/pymc3/tuning/__init__.py @@ -12,5 +12,5 @@ # See the License for the specific language governing permissions and # limitations under the License. +from .scaling import find_hessian, guess_scaling, trace_cov from .starting import find_MAP -from .scaling import find_hessian, trace_cov, guess_scaling diff --git a/pymc3/tuning/scaling.py b/pymc3/tuning/scaling.py index 47aa78e21e..7484366309 100644 --- a/pymc3/tuning/scaling.py +++ b/pymc3/tuning/scaling.py @@ -13,10 +13,12 @@ # limitations under the License. import numpy as np + from numpy import exp, log, sqrt -from ..model import modelcontext, Point + +from ..blocking import ArrayOrdering, DictToArrayBijection +from ..model import Point, modelcontext from ..theanof import hessian_diag, inputvars -from ..blocking import DictToArrayBijection, ArrayOrdering from ..util import get_var_name __all__ = ["find_hessian", "trace_cov", "guess_scaling"] diff --git a/pymc3/tuning/starting.py b/pymc3/tuning/starting.py index 6c83af33f0..f3a25a48fb 100644 --- a/pymc3/tuning/starting.py +++ b/pymc3/tuning/starting.py @@ -17,21 +17,29 @@ @author: johnsalvatier """ -from scipy.optimize import minimize +import warnings + +from inspect import getargspec + import numpy as np +import theano.gradient as tg + +from fastprogress.fastprogress import ProgressBar, progress_bar from numpy import isfinite, nan_to_num -from fastprogress.fastprogress import progress_bar -from fastprogress.fastprogress import ProgressBar +from scipy.optimize import minimize + import pymc3 as pm -from ..vartypes import discrete_types, typefilter -from ..model import modelcontext, Point -from ..theanof import inputvars -import theano.gradient as tg -from ..blocking import DictToArrayBijection, ArrayOrdering -from ..util import check_start_vals, update_start_vals, get_default_varnames, get_var_name -import warnings -from inspect import getargspec +from ..blocking import ArrayOrdering, DictToArrayBijection +from ..model import Point, modelcontext +from ..theanof import inputvars +from ..util import ( + check_start_vals, + get_default_varnames, + get_var_name, + update_start_vals, +) +from ..vartypes import discrete_types, typefilter __all__ = ["find_MAP"] diff --git a/pymc3/util.py b/pymc3/util.py index c18296b17f..54e19a6c80 100644 --- a/pymc3/util.py +++ b/pymc3/util.py @@ -12,18 +12,19 @@ # See the License for the specific language governing permissions and # limitations under the License. -import re import functools -from typing import List, Dict, Tuple, Union +import re import warnings +from typing import Dict, List, Tuple, Union + +import arviz import numpy as np import xarray -import arviz -from pymc3.exceptions import SamplingError from theano.tensor import TensorVariable +from pymc3.exceptions import SamplingError LATEX_ESCAPE_RE = re.compile(r"(%|_|\$|#|&)", re.MULTILINE) diff --git a/pymc3/variational/__init__.py b/pymc3/variational/__init__.py index 9dbe93a94a..663fa93774 100644 --- a/pymc3/variational/__init__.py +++ b/pymc3/variational/__init__.py @@ -13,34 +13,50 @@ # limitations under the License. # commonly used -from . import updates +from . import ( + approximations, + callbacks, + flows, + inference, + operators, + opvi, + test_functions, + updates, +) +from .approximations import ( + Empirical, + FullRank, + MeanField, + NormalizingFlow, + sample_approx, +) +from .inference import ( + ADVI, + ASVGD, + NFVI, + SVGD, + FullRankADVI, + ImplicitGradient, + Inference, + KLqp, + fit, +) +from .opvi import Approximation, Group + +# special +from .stein import Stein from .updates import ( - sgd, - apply_momentum, - momentum, - apply_nesterov_momentum, - adagrad_window, - nesterov_momentum, - adagrad, - rmsprop, adadelta, + adagrad, + adagrad_window, adam, adamax, + apply_momentum, + apply_nesterov_momentum, + momentum, + nesterov_momentum, norm_constraint, + rmsprop, + sgd, total_norm_constraint, ) - -from . import inference -from .inference import ADVI, FullRankADVI, SVGD, ASVGD, NFVI, Inference, KLqp, ImplicitGradient, fit - -from . import approximations -from .approximations import MeanField, FullRank, Empirical, NormalizingFlow, sample_approx -from . import opvi -from .opvi import Group, Approximation - -# special -from .stein import Stein -from . import flows -from . import operators -from . import test_functions -from . import callbacks diff --git a/pymc3/variational/approximations.py b/pymc3/variational/approximations.py index 4724259d27..e3bbbfb8e5 100644 --- a/pymc3/variational/approximations.py +++ b/pymc3/variational/approximations.py @@ -14,17 +14,19 @@ import numpy as np import theano + from theano import tensor as tt import pymc3 as pm + from pymc3.distributions.dist_math import rho2sigma -from . import opvi -from pymc3.variational.opvi import Group, Approximation, node_property -from pymc3.util import update_start_vals -from pymc3.theanof import change_flags from pymc3.math import batched_diag +from pymc3.theanof import change_flags +from pymc3.util import update_start_vals from pymc3.variational import flows +from pymc3.variational.opvi import Approximation, Group, node_property +from . import opvi __all__ = ["MeanField", "FullRank", "Empirical", "NormalizingFlow", "sample_approx"] diff --git a/pymc3/variational/flows.py b/pymc3/variational/flows.py index 8ef7e9bff9..df63e15d76 100644 --- a/pymc3/variational/flows.py +++ b/pymc3/variational/flows.py @@ -14,13 +14,14 @@ import numpy as np import theano + from theano import tensor as tt from ..distributions.dist_math import rho2sigma -from ..theanof import change_flags from ..memoize import WithMemoization -from .opvi import node_property, collect_shared_to_list +from ..theanof import change_flags from . import opvi +from .opvi import collect_shared_to_list, node_property __all__ = ["Formula", "PlanarFlow", "HouseholderFlow", "RadialFlow", "LocFlow", "ScaleFlow"] diff --git a/pymc3/variational/inference.py b/pymc3/variational/inference.py index 3810759d09..b8d766f019 100644 --- a/pymc3/variational/inference.py +++ b/pymc3/variational/inference.py @@ -12,22 +12,25 @@ # See the License for the specific language governing permissions and # limitations under the License. +import collections import logging import warnings -import collections import numpy as np + from fastprogress.fastprogress import progress_bar import pymc3 as pm + from pymc3.variational import test_functions from pymc3.variational.approximations import ( - MeanField, - FullRank, Empirical, + FullRank, + MeanField, NormalizingFlow, ) from pymc3.variational.operators import KL, KSD + from . import opvi logger = logging.getLogger(__name__) diff --git a/pymc3/variational/operators.py b/pymc3/variational/operators.py index 7e9e94d976..92992d6977 100644 --- a/pymc3/variational/operators.py +++ b/pymc3/variational/operators.py @@ -13,11 +13,14 @@ # limitations under the License. from theano import tensor as tt + +import pymc3 as pm + from pymc3.theanof import change_flags -from . import opvi -from pymc3.variational.opvi import Operator, ObjectiveFunction +from pymc3.variational.opvi import ObjectiveFunction, Operator from pymc3.variational.stein import Stein -import pymc3 as pm + +from . import opvi __all__ = ["KL", "KSD"] diff --git a/pymc3/variational/opvi.py b/pymc3/variational/opvi.py index b32de3807f..7e2b9bc60e 100644 --- a/pymc3/variational/opvi.py +++ b/pymc3/variational/opvi.py @@ -54,14 +54,16 @@ import theano.tensor as tt import pymc3 as pm + from pymc3.util import get_transformed -from .updates import adagrad_window -from ..blocking import ArrayOrdering, DictToArrayBijection, VarMap + from ..backends import NDArray +from ..blocking import ArrayOrdering, DictToArrayBijection, VarMap +from ..memoize import WithMemoization, memoize from ..model import modelcontext -from ..theanof import tt_rng, change_flags, identity +from ..theanof import change_flags, identity, tt_rng from ..util import get_default_varnames -from ..memoize import WithMemoization, memoize +from .updates import adagrad_window __all__ = ["ObjectiveFunction", "Operator", "TestFunction", "Group", "Approximation"] diff --git a/pymc3/variational/stein.py b/pymc3/variational/stein.py index d610fa4da3..cf836be734 100644 --- a/pymc3/variational/stein.py +++ b/pymc3/variational/stein.py @@ -14,10 +14,11 @@ import theano import theano.tensor as tt + +from pymc3.memoize import WithMemoization, memoize +from pymc3.theanof import change_flags, floatX from pymc3.variational.opvi import node_property from pymc3.variational.test_functions import rbf -from pymc3.theanof import floatX, change_flags -from pymc3.memoize import WithMemoization, memoize __all__ = ["Stein"] diff --git a/pymc3/variational/test_functions.py b/pymc3/variational/test_functions.py index cd0ffb0ca9..90d56ad86e 100644 --- a/pymc3/variational/test_functions.py +++ b/pymc3/variational/test_functions.py @@ -13,9 +13,11 @@ # limitations under the License. from theano import tensor as tt -from .opvi import TestFunction + from pymc3.theanof import floatX +from .opvi import TestFunction + __all__ = ["rbf"] diff --git a/pymc3/variational/updates.py b/pymc3/variational/updates.py index c555dfb345..a2baa462c5 100755 --- a/pymc3/variational/updates.py +++ b/pymc3/variational/updates.py @@ -109,14 +109,14 @@ Taken from the Lasagne project: http://lasagne.readthedocs.io/en/latest/ """ -from functools import partial from collections import OrderedDict -import numpy as np +from functools import partial +import numpy as np import theano import theano.tensor as tt -import pymc3 as pm +import pymc3 as pm __all__ = [ "sgd", diff --git a/pymc3/vartypes.py b/pymc3/vartypes.py index ec28eb1145..9ddd88c21a 100644 --- a/pymc3/vartypes.py +++ b/pymc3/vartypes.py @@ -12,9 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from theano.tensor import Constant as tensor_constant from theano.gof.graph import Constant as graph_constant - +from theano.tensor import Constant as tensor_constant __all__ = [ "bool_types", diff --git a/scripts/check_all_tests_are_covered.py b/scripts/check_all_tests_are_covered.py index 54221fa21b..2882f57346 100644 --- a/scripts/check_all_tests_are_covered.py +++ b/scripts/check_all_tests_are_covered.py @@ -7,10 +7,10 @@ You can run it manually with `pre-commit run check-no-tests-are-ignored --all`. """ -from pathlib import Path - import re +from pathlib import Path + if __name__ == "__main__": pytest_ci_job = Path(".github") / "workflows/pytest.yml" txt = pytest_ci_job.read_text() diff --git a/scripts/check_toc_is_complete.py b/scripts/check_toc_is_complete.py index ea82ab1a3a..658d6c3a72 100644 --- a/scripts/check_toc_is_complete.py +++ b/scripts/check_toc_is_complete.py @@ -5,10 +5,11 @@ You can run it manually with `pre-commit run check-toc --all`. """ -from pathlib import Path import argparse import ast +from pathlib import Path + if __name__ == "__main__": toc_examples = (Path("docs") / "source/notebooks/table_of_contents_examples.js").read_text() toc_tutorials = (Path("docs") / "source/notebooks/table_of_contents_tutorials.js").read_text() diff --git a/setup.py b/setup.py index 9d5071f64b..72ff10c0a3 100755 --- a/setup.py +++ b/setup.py @@ -12,12 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -#!/usr/bin/env python -from codecs import open -from os.path import realpath, dirname, join -from setuptools import setup, find_packages import re +from codecs import open +from os.path import dirname, join, realpath + +from setuptools import find_packages, setup + DISTNAME = "pymc3" DESCRIPTION = "Probabilistic Programming in Python: Bayesian Modeling and Probabilistic Machine Learning with Theano" AUTHOR = "PyMC Developers"