diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c6d3e3eff..e786b4c87 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -38,6 +38,11 @@ repos: args: ["--fix", "--show-fixes", "--exit-non-zero-on-fix"] - id: ruff-format + - repo: https://github.com/pycqa/isort + rev: c235f5e450b4b84e58d114ed4c589cbf454175a3 # frozen: 5.13.2 + hooks: + - id: isort + - repo: https://github.com/codespell-project/codespell rev: "6e41aba91fb32e9feb741a6258eefeb9c6e4a482" # frozen: v2.2.6 hooks: diff --git a/advanced/advanced_numpy/examples/mandel-answer.pyx b/advanced/advanced_numpy/examples/mandel-answer.pyx index e6ff63ca1..383e012b5 100644 --- a/advanced/advanced_numpy/examples/mandel-answer.pyx +++ b/advanced/advanced_numpy/examples/mandel-answer.pyx @@ -42,11 +42,8 @@ cdef void mandel_single_point(double complex *z_in, # Pulls definitions from the NumPy C headers. # ------------------------------------------- -from numpy cimport import_array, import_ufunc -from numpy cimport (PyUFunc_FromFuncAndData, - PyUFuncGenericFunction) -from numpy cimport NPY_CDOUBLE -from numpy cimport PyUFunc_DD_D +from numpy cimport (NPY_CDOUBLE, PyUFunc_DD_D, PyUFunc_FromFuncAndData, + PyUFuncGenericFunction, import_array, import_ufunc) # Required module initialization # ------------------------------ diff --git a/advanced/advanced_numpy/examples/mandel.pyx b/advanced/advanced_numpy/examples/mandel.pyx index d585e344d..a61c912f5 100644 --- a/advanced/advanced_numpy/examples/mandel.pyx +++ b/advanced/advanced_numpy/examples/mandel.pyx @@ -61,31 +61,17 @@ cdef void mandel_single_point(double complex *z_in, # Pulls definitions from the NumPy C headers. # ------------------------------------------- -from numpy cimport import_array, import_ufunc -from numpy cimport (PyUFunc_FromFuncAndData, - PyUFuncGenericFunction) -from numpy cimport NPY_CDOUBLE, NP_DOUBLE, NPY_LONG +from numpy cimport (NP_DOUBLE, NPY_CDOUBLE, NPY_LONG, PyUFunc_d_d, PyUFunc_D_D, + PyUFunc_dd_d, PyUFunc_DD_D, PyUFunc_f_f, PyUFunc_F_F, + PyUFunc_f_f_As_d_d, PyUFunc_F_F_As_D_D, PyUFunc_ff_f, + PyUFunc_FF_F, PyUFunc_ff_f_As_dd_d, PyUFunc_FF_F_As_DD_D, + PyUFunc_FromFuncAndData, PyUFunc_g_g, PyUFunc_G_G, + PyUFunc_gg_g, PyUFunc_GG_G, PyUFuncGenericFunction, + import_array, import_ufunc) # Import all pre-defined loop functions # you won't need all of them - keep the relevant ones -from numpy cimport ( - PyUFunc_f_f_As_d_d, - PyUFunc_d_d, - PyUFunc_f_f, - PyUFunc_g_g, - PyUFunc_F_F_As_D_D, - PyUFunc_F_F, - PyUFunc_D_D, - PyUFunc_G_G, - PyUFunc_ff_f_As_dd_d, - PyUFunc_ff_f, - PyUFunc_dd_d, - PyUFunc_gg_g, - PyUFunc_FF_F_As_DD_D, - PyUFunc_DD_D, - PyUFunc_FF_F, - PyUFunc_GG_G) # Required module initialization diff --git a/advanced/advanced_numpy/examples/mandelplot.py b/advanced/advanced_numpy/examples/mandelplot.py index fa9bb2044..7334ad6dd 100644 --- a/advanced/advanced_numpy/examples/mandelplot.py +++ b/advanced/advanced_numpy/examples/mandelplot.py @@ -6,9 +6,10 @@ """ -import numpy as np import mandel +import numpy as np + x = np.linspace(-1.7, 0.6, 1000) y = np.linspace(-1.4, 1.4, 1000) c = x[None, :] + 1j * y[:, None] diff --git a/advanced/advanced_numpy/examples/pilbuffer-answer.py b/advanced/advanced_numpy/examples/pilbuffer-answer.py index 62e6255e8..f1cb73655 100644 --- a/advanced/advanced_numpy/examples/pilbuffer-answer.py +++ b/advanced/advanced_numpy/examples/pilbuffer-answer.py @@ -6,9 +6,10 @@ the buffer interface. """ -import numpy as np import Image +import numpy as np + # Let's make a sample image, RGBA format x = np.zeros((200, 200, 4), dtype=np.int8) diff --git a/advanced/advanced_numpy/examples/pilbuffer.py b/advanced/advanced_numpy/examples/pilbuffer.py index d1a60a960..1aa4ce3e3 100644 --- a/advanced/advanced_numpy/examples/pilbuffer.py +++ b/advanced/advanced_numpy/examples/pilbuffer.py @@ -5,9 +5,10 @@ Skeletton of the code to do an exercise using the buffer protocole. """ -import numpy as np import Image +import numpy as np + # Let's make a sample image, RGBA format x = np.zeros((200, 200, 4), dtype=np.int8) diff --git a/advanced/advanced_numpy/examples/setup.py b/advanced/advanced_numpy/examples/setup.py index 9b799488b..96a7b9362 100644 --- a/advanced/advanced_numpy/examples/setup.py +++ b/advanced/advanced_numpy/examples/setup.py @@ -6,10 +6,11 @@ C sources. """ +from distutils.sysconfig import get_python_inc import os -import sys from os.path import join -from distutils.sysconfig import get_python_inc +import sys + import numpy from numpy.distutils.misc_util import get_numpy_include_dirs diff --git a/advanced/advanced_numpy/examples/setup_myobject.py b/advanced/advanced_numpy/examples/setup_myobject.py index d3e930fc3..f86a1ed51 100644 --- a/advanced/advanced_numpy/examples/setup_myobject.py +++ b/advanced/advanced_numpy/examples/setup_myobject.py @@ -6,7 +6,7 @@ """ -from distutils.core import setup, Extension +from distutils.core import Extension, setup setup( name="myobject", diff --git a/advanced/advanced_numpy/examples/wavreader.py b/advanced/advanced_numpy/examples/wavreader.py index 5e32b01b3..8322cb6fa 100644 --- a/advanced/advanced_numpy/examples/wavreader.py +++ b/advanced/advanced_numpy/examples/wavreader.py @@ -6,6 +6,7 @@ """ import sys + import numpy as np wav_header_dtype = np.dtype( diff --git a/advanced/image_processing/examples/plot_face.py b/advanced/image_processing/examples/plot_face.py index 560da8eeb..68ba89d48 100644 --- a/advanced/image_processing/examples/plot_face.py +++ b/advanced/image_processing/examples/plot_face.py @@ -5,9 +5,10 @@ Small example to plot a raccoon face. """ -import scipy as sp import imageio.v3 as iio +import scipy as sp + f = sp.datasets.face() iio.imwrite("face.png", f) # uses the Image module (PIL) diff --git a/advanced/image_processing/examples/plot_face_tv_denoise.py b/advanced/image_processing/examples/plot_face_tv_denoise.py index 2f0e2c187..96d656df8 100644 --- a/advanced/image_processing/examples/plot_face_tv_denoise.py +++ b/advanced/image_processing/examples/plot_face_tv_denoise.py @@ -8,7 +8,6 @@ import numpy as np import scipy as sp import matplotlib.pyplot as plt - from skimage.restoration import denoise_tv_chambolle rng = np.random.default_rng(27446968) diff --git a/advanced/image_processing/examples/plot_spectral_clustering.py b/advanced/image_processing/examples/plot_spectral_clustering.py index 9f72018ad..e481fe30c 100644 --- a/advanced/image_processing/examples/plot_spectral_clustering.py +++ b/advanced/image_processing/examples/plot_spectral_clustering.py @@ -7,9 +7,8 @@ import numpy as np import matplotlib.pyplot as plt - -from sklearn.feature_extraction import image from sklearn.cluster import spectral_clustering +from sklearn.feature_extraction import image ################################################################################ l = 100 diff --git a/advanced/image_processing/examples/plot_watershed_segmentation.py b/advanced/image_processing/examples/plot_watershed_segmentation.py index a08e111d6..a179a3a53 100644 --- a/advanced/image_processing/examples/plot_watershed_segmentation.py +++ b/advanced/image_processing/examples/plot_watershed_segmentation.py @@ -6,10 +6,10 @@ """ import numpy as np -from skimage.segmentation import watershed -from skimage.feature import peak_local_max -import matplotlib.pyplot as plt import scipy as sp +import matplotlib.pyplot as plt +from skimage.feature import peak_local_max +from skimage.segmentation import watershed # Generate an initial image with two overlapping circles x, y = np.indices((80, 80)) diff --git a/advanced/interfacing_with_c/ctypes/cos_module.py b/advanced/interfacing_with_c/ctypes/cos_module.py index 23e60a624..123c597ad 100644 --- a/advanced/interfacing_with_c/ctypes/cos_module.py +++ b/advanced/interfacing_with_c/ctypes/cos_module.py @@ -1,11 +1,11 @@ """Example of wrapping cos function from math.h using ctypes.""" import ctypes +# OSX or linux +from ctypes.util import find_library # find and load the library -# OSX or linux -from ctypes.util import find_library libm = ctypes.cdll.LoadLibrary(find_library("m")) diff --git a/advanced/interfacing_with_c/ctypes_numpy/cos_doubles.py b/advanced/interfacing_with_c/ctypes_numpy/cos_doubles.py index 92d7d998d..65375021a 100644 --- a/advanced/interfacing_with_c/ctypes_numpy/cos_doubles.py +++ b/advanced/interfacing_with_c/ctypes_numpy/cos_doubles.py @@ -1,9 +1,10 @@ """Example of wrapping a C library function that accepts a C double array as input using the numpy.ctypeslib.""" +from ctypes import c_int + import numpy as np import numpy.ctypeslib as npct -from ctypes import c_int # input type for the cos_doubles function # must be a double array, with single dimension that is contiguous diff --git a/advanced/interfacing_with_c/cython/setup.py b/advanced/interfacing_with_c/cython/setup.py index 332f8f94f..c03e4010f 100644 --- a/advanced/interfacing_with_c/cython/setup.py +++ b/advanced/interfacing_with_c/cython/setup.py @@ -1,4 +1,5 @@ -from distutils.core import setup, Extension +from distutils.core import Extension, setup + from Cython.Distutils import build_ext setup( diff --git a/advanced/interfacing_with_c/cython_numpy/setup.py b/advanced/interfacing_with_c/cython_numpy/setup.py index 78d6f28df..3bc9e4498 100644 --- a/advanced/interfacing_with_c/cython_numpy/setup.py +++ b/advanced/interfacing_with_c/cython_numpy/setup.py @@ -1,7 +1,9 @@ -from distutils.core import setup, Extension -import numpy +from distutils.core import Extension, setup + from Cython.Distutils import build_ext +import numpy + setup( cmdclass={"build_ext": build_ext}, ext_modules=[ diff --git a/advanced/interfacing_with_c/cython_simple/cos_module.pyx b/advanced/interfacing_with_c/cython_simple/cos_module.pyx index 594e0ebe5..aac9ba3c5 100644 --- a/advanced/interfacing_with_c/cython_simple/cos_module.pyx +++ b/advanced/interfacing_with_c/cython_simple/cos_module.pyx @@ -2,5 +2,6 @@ from libc.math cimport cos + def cos_func(arg): return cos(arg) diff --git a/advanced/interfacing_with_c/cython_simple/setup.py b/advanced/interfacing_with_c/cython_simple/setup.py index 332f8f94f..c03e4010f 100644 --- a/advanced/interfacing_with_c/cython_simple/setup.py +++ b/advanced/interfacing_with_c/cython_simple/setup.py @@ -1,4 +1,5 @@ -from distutils.core import setup, Extension +from distutils.core import Extension, setup + from Cython.Distutils import build_ext setup( diff --git a/advanced/interfacing_with_c/numpy_c_api/setup.py b/advanced/interfacing_with_c/numpy_c_api/setup.py index bb4ff1528..4a0f04fe5 100644 --- a/advanced/interfacing_with_c/numpy_c_api/setup.py +++ b/advanced/interfacing_with_c/numpy_c_api/setup.py @@ -1,4 +1,5 @@ -from distutils.core import setup, Extension +from distutils.core import Extension, setup + import numpy # define the extension module diff --git a/advanced/interfacing_with_c/numpy_c_api/test_cos_module_np.py b/advanced/interfacing_with_c/numpy_c_api/test_cos_module_np.py index fa29b7693..378fd5b63 100644 --- a/advanced/interfacing_with_c/numpy_c_api/test_cos_module_np.py +++ b/advanced/interfacing_with_c/numpy_c_api/test_cos_module_np.py @@ -1,4 +1,5 @@ import cos_module_np + import numpy as np import matplotlib.pyplot as plt diff --git a/advanced/interfacing_with_c/numpy_shared/test_cos_doubles.py b/advanced/interfacing_with_c/numpy_shared/test_cos_doubles.py index 752ac15ef..4882e7aeb 100644 --- a/advanced/interfacing_with_c/numpy_shared/test_cos_doubles.py +++ b/advanced/interfacing_with_c/numpy_shared/test_cos_doubles.py @@ -1,6 +1,7 @@ +import cos_doubles + import numpy as np import matplotlib.pyplot as plt -import cos_doubles x = np.arange(0, 2 * np.pi, 0.1) y = np.empty_like(x) diff --git a/advanced/interfacing_with_c/python_c_api/setup.py b/advanced/interfacing_with_c/python_c_api/setup.py index f4d70524f..b6fabf17d 100644 --- a/advanced/interfacing_with_c/python_c_api/setup.py +++ b/advanced/interfacing_with_c/python_c_api/setup.py @@ -1,4 +1,4 @@ -from distutils.core import setup, Extension +from distutils.core import Extension, setup # define the extension module cos_module = Extension("cos_module", sources=["cos_module.c"]) diff --git a/advanced/interfacing_with_c/swig/setup.py b/advanced/interfacing_with_c/swig/setup.py index b257c15f3..a39ad63ca 100644 --- a/advanced/interfacing_with_c/swig/setup.py +++ b/advanced/interfacing_with_c/swig/setup.py @@ -1,3 +1,3 @@ -from distutils.core import setup, Extension +from distutils.core import Extension, setup setup(ext_modules=[Extension("_cos_module", sources=["cos_module.c", "cos_module.i"])]) diff --git a/advanced/interfacing_with_c/swig_numpy/setup.py b/advanced/interfacing_with_c/swig_numpy/setup.py index 874295cb3..876b83d89 100644 --- a/advanced/interfacing_with_c/swig_numpy/setup.py +++ b/advanced/interfacing_with_c/swig_numpy/setup.py @@ -1,4 +1,5 @@ -from distutils.core import setup, Extension +from distutils.core import Extension, setup + import numpy setup( diff --git a/advanced/mathematical_optimization/examples/helper/compare_optimizers.py b/advanced/mathematical_optimization/examples/helper/compare_optimizers.py index f298e73ab..d1aed9576 100644 --- a/advanced/mathematical_optimization/examples/helper/compare_optimizers.py +++ b/advanced/mathematical_optimization/examples/helper/compare_optimizers.py @@ -9,20 +9,14 @@ import pickle import sys +from cost_functions import (CountingFunction, LoggingFunction, mk_gauss, + mk_quad, rosenbrock, rosenbrock_hessian, + rosenbrock_prime) + import numpy as np import scipy as sp from joblib import Memory -from cost_functions import ( - mk_quad, - mk_gauss, - rosenbrock, - rosenbrock_prime, - rosenbrock_hessian, - LoggingFunction, - CountingFunction, -) - def my_partial(**kwargs): function = sp.optimize.minimize diff --git a/advanced/mathematical_optimization/examples/plot_1d_optim.py b/advanced/mathematical_optimization/examples/plot_1d_optim.py index cedc15395..05c30f9ec 100644 --- a/advanced/mathematical_optimization/examples/plot_1d_optim.py +++ b/advanced/mathematical_optimization/examples/plot_1d_optim.py @@ -6,8 +6,8 @@ """ import numpy as np -import matplotlib.pyplot as plt import scipy as sp +import matplotlib.pyplot as plt x = np.linspace(-1, 3, 100) x_0 = np.exp(-1) diff --git a/advanced/mathematical_optimization/examples/plot_constraints.py b/advanced/mathematical_optimization/examples/plot_constraints.py index d1388d15b..a43e9cb9d 100644 --- a/advanced/mathematical_optimization/examples/plot_constraints.py +++ b/advanced/mathematical_optimization/examples/plot_constraints.py @@ -6,8 +6,8 @@ """ import numpy as np -import matplotlib.pyplot as plt import scipy as sp +import matplotlib.pyplot as plt x, y = np.mgrid[-2.9:5.8:0.05, -2.5:5:0.05] x = x.T diff --git a/advanced/mathematical_optimization/examples/plot_gradient_descent.py b/advanced/mathematical_optimization/examples/plot_gradient_descent.py index 812215b9a..6e5e74ff5 100644 --- a/advanced/mathematical_optimization/examples/plot_gradient_descent.py +++ b/advanced/mathematical_optimization/examples/plot_gradient_descent.py @@ -6,23 +6,17 @@ evolution of the optimizer. """ +import os +import sys + import numpy as np -import matplotlib.pyplot as plt import scipy as sp - -import sys -import os +import matplotlib.pyplot as plt sys.path.append(os.path.abspath("helper")) -from cost_functions import ( - mk_quad, - mk_gauss, - rosenbrock, - rosenbrock_prime, - rosenbrock_hessian, - LoggingFunction, - CountingFunction, -) +from cost_functions import (CountingFunction, LoggingFunction, mk_gauss, + mk_quad, rosenbrock, rosenbrock_hessian, + rosenbrock_prime) x_min, x_max = -1, 2 y_min, y_max = 2.25 / 3 * x_min - 0.2, 2.25 / 3 * x_max - 0.2 diff --git a/advanced/mathematical_optimization/examples/plot_non_bounds_constraints.py b/advanced/mathematical_optimization/examples/plot_non_bounds_constraints.py index 08af6f4d5..f9a3a3272 100644 --- a/advanced/mathematical_optimization/examples/plot_non_bounds_constraints.py +++ b/advanced/mathematical_optimization/examples/plot_non_bounds_constraints.py @@ -7,8 +7,8 @@ """ import numpy as np -import matplotlib.pyplot as plt import scipy as sp +import matplotlib.pyplot as plt x, y = np.mgrid[-2.03:4.2:0.04, -1.6:3.2:0.04] x = x.T diff --git a/advanced/optimizing/demo.py b/advanced/optimizing/demo.py index e5eb025b8..78af69022 100644 --- a/advanced/optimizing/demo.py +++ b/advanced/optimizing/demo.py @@ -1,10 +1,10 @@ # For this example to run, you also need the 'ica.py' file +from ica import fastica + import numpy as np import scipy as sp -from ica import fastica - # @profile # uncomment this line to run with line_profiler def test(): diff --git a/advanced/optimizing/demo_opt.py b/advanced/optimizing/demo_opt.py index 410e52ddf..08f5af890 100644 --- a/advanced/optimizing/demo_opt.py +++ b/advanced/optimizing/demo_opt.py @@ -1,10 +1,10 @@ # For this example to run, you also need the 'ica.py' file +from ica import fastica + import numpy as np import scipy as sp -from ica import fastica - def test(): rng = np.random.default_rng() diff --git a/advanced/optimizing/ica.py b/advanced/optimizing/ica.py index 298ee703c..a9542d078 100644 --- a/advanced/optimizing/ica.py +++ b/advanced/optimizing/ica.py @@ -3,9 +3,9 @@ # License: GPL unless permission obtained otherwise # Look at algorithms in Tables 8.3 and 8.4 page 196 in the book: Independent Component Analysis, by Aapo et al. -import numpy as np import types +import numpy as np __all__ = ["fastica"] diff --git a/advanced/scipy_sparse/examples/lobpcg_sakurai.py b/advanced/scipy_sparse/examples/lobpcg_sakurai.py index 1ec24b2af..c686a5795 100644 --- a/advanced/scipy_sparse/examples/lobpcg_sakurai.py +++ b/advanced/scipy_sparse/examples/lobpcg_sakurai.py @@ -6,6 +6,7 @@ """ import time + import numpy as np import scipy as sp import matplotlib.pyplot as plt diff --git a/advanced/scipy_sparse/examples/pyamg_with_lobpcg.py b/advanced/scipy_sparse/examples/pyamg_with_lobpcg.py index a7eb4a271..af39ce714 100644 --- a/advanced/scipy_sparse/examples/pyamg_with_lobpcg.py +++ b/advanced/scipy_sparse/examples/pyamg_with_lobpcg.py @@ -7,12 +7,12 @@ Dirichlet boundary conditions. """ -import scipy as sp -import matplotlib.pyplot as plt - from pyamg import smoothed_aggregation_solver from pyamg.gallery import poisson +import scipy as sp +import matplotlib.pyplot as plt + N = 100 K = 9 A = poisson((N, N), format="csr") diff --git a/conf.py b/conf.py index 464943baf..ad3ebf60d 100644 --- a/conf.py +++ b/conf.py @@ -1,10 +1,10 @@ from datetime import date -from subprocess import PIPE, Popen import os +from subprocess import PIPE, Popen -import sphinx_gallery from pygments import formatters from sphinx import highlighting +import sphinx_gallery # General configuration # --------------------- diff --git a/intro/matplotlib/examples/plot_gridspec.py b/intro/matplotlib/examples/plot_gridspec.py index c01e36af1..cb19cb143 100644 --- a/intro/matplotlib/examples/plot_gridspec.py +++ b/intro/matplotlib/examples/plot_gridspec.py @@ -5,8 +5,8 @@ An example demoing gridspec """ -import matplotlib.pyplot as plt from matplotlib import gridspec +import matplotlib.pyplot as plt plt.figure(figsize=(6, 4)) G = gridspec.GridSpec(3, 3) diff --git a/intro/matplotlib/examples/plot_subplot-vertical.py b/intro/matplotlib/examples/plot_subplot-vertical.py index 4f93f4e97..c3d85ce59 100644 --- a/intro/matplotlib/examples/plot_subplot-vertical.py +++ b/intro/matplotlib/examples/plot_subplot-vertical.py @@ -7,7 +7,6 @@ import matplotlib.pyplot as plt - plt.figure(figsize=(6, 4)) plt.subplot(1, 2, 1) plt.xticks([]) diff --git a/intro/matplotlib/examples/plot_text.py b/intro/matplotlib/examples/plot_text.py index 0871b7833..f9cfadf71 100644 --- a/intro/matplotlib/examples/plot_text.py +++ b/intro/matplotlib/examples/plot_text.py @@ -8,7 +8,6 @@ import numpy as np import matplotlib.pyplot as plt - eqs = [] eqs.append( r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2} \int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_{\rho_1 \sigma_2} }{U^{0\beta}_{\rho_1 \sigma_2}}\right]$" diff --git a/intro/matplotlib/examples/pretty_plots/plot_boxplot_ext.py b/intro/matplotlib/examples/pretty_plots/plot_boxplot_ext.py index ff844eba2..10753261c 100644 --- a/intro/matplotlib/examples/pretty_plots/plot_boxplot_ext.py +++ b/intro/matplotlib/examples/pretty_plots/plot_boxplot_ext.py @@ -9,7 +9,6 @@ import numpy as np import matplotlib.pyplot as plt - fig = plt.figure(figsize=(8, 5)) axes = plt.subplot(111) diff --git a/intro/numpy/examples/plot_mandelbrot.py b/intro/numpy/examples/plot_mandelbrot.py index bf0921d26..b9a6248de 100644 --- a/intro/numpy/examples/plot_mandelbrot.py +++ b/intro/numpy/examples/plot_mandelbrot.py @@ -6,10 +6,11 @@ """ +import warnings + import numpy as np -import matplotlib.pyplot as plt from numpy import newaxis -import warnings +import matplotlib.pyplot as plt def compute_mandelbrot(N_max, some_threshold, nx, ny): diff --git a/intro/numpy/solutions/2_4_mandelbrot.py b/intro/numpy/solutions/2_4_mandelbrot.py index 415ca0e25..e424c4a53 100644 --- a/intro/numpy/solutions/2_4_mandelbrot.py +++ b/intro/numpy/solutions/2_4_mandelbrot.py @@ -3,8 +3,8 @@ """ import numpy as np -import matplotlib.pyplot as plt from numpy import newaxis +import matplotlib.pyplot as plt def compute_mandelbrot(N_max, some_threshold, nx, ny): diff --git a/intro/numpy/solutions/2_a_call_fortran.py b/intro/numpy/solutions/2_a_call_fortran.py index c72fb8348..3e813bcd2 100644 --- a/intro/numpy/solutions/2_a_call_fortran.py +++ b/intro/numpy/solutions/2_a_call_fortran.py @@ -1,6 +1,7 @@ -import numpy as np import fortran_module +import numpy as np + def some_function(input): """ diff --git a/intro/scipy/examples/plot_image_filters.py b/intro/scipy/examples/plot_image_filters.py index 52a3764b8..05c104892 100644 --- a/intro/scipy/examples/plot_image_filters.py +++ b/intro/scipy/examples/plot_image_filters.py @@ -12,11 +12,10 @@ face = sp.datasets.face(gray=True) face = face[:512, -512:] # crop out square on right +import numpy as np # Apply a variety of filters import matplotlib.pyplot as plt -import numpy as np - noisy_face = np.copy(face).astype(float) rng = np.random.default_rng() noisy_face += face.std() * 0.5 * rng.standard_normal(face.shape) diff --git a/intro/scipy/solutions/path_site.py b/intro/scipy/solutions/path_site.py index fab917f36..de95141d3 100644 --- a/intro/scipy/solutions/path_site.py +++ b/intro/scipy/solutions/path_site.py @@ -1,8 +1,8 @@ """Script to search the PYTHONPATH for the module site.py""" +import glob import os import sys -import glob def find_module(module): diff --git a/intro/scipy/summary-exercises/examples/plot_optimize_lidar_complex_data_fit.py b/intro/scipy/summary-exercises/examples/plot_optimize_lidar_complex_data_fit.py index 89a596dcd..ba5873788 100644 --- a/intro/scipy/summary-exercises/examples/plot_optimize_lidar_complex_data_fit.py +++ b/intro/scipy/summary-exercises/examples/plot_optimize_lidar_complex_data_fit.py @@ -6,8 +6,8 @@ """ import numpy as np -import matplotlib.pyplot as plt import scipy as sp +import matplotlib.pyplot as plt def model(t, coeffs): diff --git a/intro/scipy/summary-exercises/examples/plot_optimize_lidar_data_fit.py b/intro/scipy/summary-exercises/examples/plot_optimize_lidar_data_fit.py index 8eafe5e6f..c327e082b 100644 --- a/intro/scipy/summary-exercises/examples/plot_optimize_lidar_data_fit.py +++ b/intro/scipy/summary-exercises/examples/plot_optimize_lidar_data_fit.py @@ -6,8 +6,8 @@ """ import numpy as np -import matplotlib.pyplot as plt import scipy as sp +import matplotlib.pyplot as plt def model(t, coeffs): diff --git a/packages/scikit-image/examples/plot_boundaries.py b/packages/scikit-image/examples/plot_boundaries.py index 7c6df30f0..d01878b0a 100644 --- a/packages/scikit-image/examples/plot_boundaries.py +++ b/packages/scikit-image/examples/plot_boundaries.py @@ -5,10 +5,9 @@ Visualize segmentation contours on original grayscale image. """ -from skimage import data, segmentation -from skimage import filters -import matplotlib.pyplot as plt import numpy as np +import matplotlib.pyplot as plt +from skimage import data, filters, segmentation coins = data.coins() mask = coins > filters.threshold_otsu(coins) diff --git a/packages/scikit-image/examples/plot_equalize_hist.py b/packages/scikit-image/examples/plot_equalize_hist.py index 9696b5e1c..3f67a1f0e 100644 --- a/packages/scikit-image/examples/plot_equalize_hist.py +++ b/packages/scikit-image/examples/plot_equalize_hist.py @@ -5,8 +5,8 @@ Histogram equalizing makes images have a uniform histogram. """ -from skimage import data, exposure import matplotlib.pyplot as plt +from skimage import data, exposure camera = data.camera() camera_equalized = exposure.equalize_hist(camera) diff --git a/packages/scikit-image/examples/plot_features.py b/packages/scikit-image/examples/plot_features.py index 74cda9f2d..a139ddc5b 100644 --- a/packages/scikit-image/examples/plot_features.py +++ b/packages/scikit-image/examples/plot_features.py @@ -6,11 +6,9 @@ """ import matplotlib.pyplot as plt - from skimage import data -from skimage.feature import corner_harris, corner_subpix, corner_peaks -from skimage.transform import warp, AffineTransform - +from skimage.feature import corner_harris, corner_peaks, corner_subpix +from skimage.transform import AffineTransform, warp tform = AffineTransform(scale=(1.3, 1.1), rotation=1, shear=0.7, translation=(210, 50)) image = warp(data.checkerboard(), tform.inverse, output_shape=(350, 350)) diff --git a/packages/scikit-image/examples/plot_filter_coins.py b/packages/scikit-image/examples/plot_filter_coins.py index f44d8324d..22ee72bba 100644 --- a/packages/scikit-image/examples/plot_filter_coins.py +++ b/packages/scikit-image/examples/plot_filter_coins.py @@ -8,9 +8,7 @@ import numpy as np import matplotlib.pyplot as plt -from skimage import data -from skimage import filters -from skimage import restoration +from skimage import data, filters, restoration coins = data.coins() gaussian_filter_coins = filters.gaussian(coins, sigma=2) diff --git a/packages/scikit-image/examples/plot_labels.py b/packages/scikit-image/examples/plot_labels.py index 1b99701fd..1b71e29dc 100644 --- a/packages/scikit-image/examples/plot_labels.py +++ b/packages/scikit-image/examples/plot_labels.py @@ -6,10 +6,9 @@ the dedicated skimage.measure.label function. """ -from skimage import measure -from skimage import filters -import matplotlib.pyplot as plt import numpy as np +import matplotlib.pyplot as plt +from skimage import filters, measure n = 12 l = 256 diff --git a/packages/scikit-image/examples/plot_segmentations.py b/packages/scikit-image/examples/plot_segmentations.py index 16896c987..6624d9b89 100644 --- a/packages/scikit-image/examples/plot_segmentations.py +++ b/packages/scikit-image/examples/plot_segmentations.py @@ -11,12 +11,11 @@ """ import numpy as np -from skimage.segmentation import watershed -from skimage.feature import peak_local_max -from skimage import measure -from skimage.segmentation import random_walker -import matplotlib.pyplot as plt import scipy as sp +import matplotlib.pyplot as plt +from skimage import measure +from skimage.feature import peak_local_max +from skimage.segmentation import random_walker, watershed # Generate an initial image with two overlapping circles x, y = np.indices((80, 80)) diff --git a/packages/scikit-image/examples/plot_sobel.py b/packages/scikit-image/examples/plot_sobel.py index c1d7a3195..fae0350f9 100644 --- a/packages/scikit-image/examples/plot_sobel.py +++ b/packages/scikit-image/examples/plot_sobel.py @@ -6,9 +6,8 @@ horizontal gradients. """ -from skimage import data -from skimage import filters import matplotlib.pyplot as plt +from skimage import data, filters text = data.text() hsobel_text = filters.sobel_h(text) diff --git a/packages/scikit-image/examples/plot_threshold.py b/packages/scikit-image/examples/plot_threshold.py index b03c75df4..e2227af53 100644 --- a/packages/scikit-image/examples/plot_threshold.py +++ b/packages/scikit-image/examples/plot_threshold.py @@ -6,9 +6,7 @@ """ import matplotlib.pyplot as plt -from skimage import data -from skimage import filters -from skimage import exposure +from skimage import data, exposure, filters camera = data.camera() val = filters.threshold_otsu(camera) diff --git a/packages/scikit-learn/examples/plot_ML_flow_chart.py b/packages/scikit-learn/examples/plot_ML_flow_chart.py index b986ab8de..878e74552 100644 --- a/packages/scikit-learn/examples/plot_ML_flow_chart.py +++ b/packages/scikit-learn/examples/plot_ML_flow_chart.py @@ -6,8 +6,8 @@ """ import numpy as np +from matplotlib.patches import Arrow, Circle, FancyArrow, Polygon, Rectangle import matplotlib.pyplot as plt -from matplotlib.patches import Circle, Rectangle, Polygon, Arrow, FancyArrow def create_base(box_bg="#CCCCCC", arrow1="#88CCFF", arrow2="#88FF88", supervised=True): diff --git a/packages/scikit-learn/examples/plot_bias_variance.py b/packages/scikit-learn/examples/plot_bias_variance.py index 16a54b54c..e5697884c 100644 --- a/packages/scikit-learn/examples/plot_bias_variance.py +++ b/packages/scikit-learn/examples/plot_bias_variance.py @@ -20,10 +20,10 @@ def generating_func(x, rng=None, error=0.5): return rng.normal(10 - 1.0 / (x + 0.1), error) +from sklearn.linear_model import LinearRegression ############################################################ # A polynomial regression from sklearn.pipeline import make_pipeline -from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures ############################################################ diff --git a/packages/scikit-learn/examples/plot_compare_classifiers.py b/packages/scikit-learn/examples/plot_compare_classifiers.py index 40aec4156..7a5c330c4 100644 --- a/packages/scikit-learn/examples/plot_compare_classifiers.py +++ b/packages/scikit-learn/examples/plot_compare_classifiers.py @@ -6,10 +6,10 @@ digits data. """ -from sklearn import model_selection, datasets, metrics -from sklearn.svm import LinearSVC +from sklearn import datasets, metrics, model_selection from sklearn.naive_bayes import GaussianNB from sklearn.neighbors import KNeighborsClassifier +from sklearn.svm import LinearSVC digits = datasets.load_digits() X = digits.data diff --git a/packages/scikit-learn/examples/plot_digits_simple_classif.py b/packages/scikit-learn/examples/plot_digits_simple_classif.py index 0f823dd54..52ac0185e 100644 --- a/packages/scikit-learn/examples/plot_digits_simple_classif.py +++ b/packages/scikit-learn/examples/plot_digits_simple_classif.py @@ -45,8 +45,8 @@ # Classify with Gaussian naive Bayes # ---------------------------------- -from sklearn.naive_bayes import GaussianNB from sklearn.model_selection import train_test_split +from sklearn.naive_bayes import GaussianNB # split the data into training and validation sets X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target) diff --git a/packages/scikit-learn/examples/plot_iris_knn.py b/packages/scikit-learn/examples/plot_iris_knn.py index ea52c92ac..c7518c278 100644 --- a/packages/scikit-learn/examples/plot_iris_knn.py +++ b/packages/scikit-learn/examples/plot_iris_knn.py @@ -7,9 +7,9 @@ """ import numpy as np -import matplotlib.pyplot as plt -from sklearn import neighbors, datasets from matplotlib.colors import ListedColormap +import matplotlib.pyplot as plt +from sklearn import datasets, neighbors # Create color maps for 3-class classification problem, as with iris cmap_light = ListedColormap(["#FFAAAA", "#AAFFAA", "#AAAAFF"]) diff --git a/packages/scikit-learn/examples/plot_linear_model_cv.py b/packages/scikit-learn/examples/plot_linear_model_cv.py index bc7f3a467..7b52b330e 100644 --- a/packages/scikit-learn/examples/plot_linear_model_cv.py +++ b/packages/scikit-learn/examples/plot_linear_model_cv.py @@ -14,10 +14,10 @@ X, y = data.data, data.target print(X.shape) +from sklearn.linear_model import Lasso, Ridge ############################################################ # Compute the cross-validation score with the default hyper-parameters from sklearn.model_selection import cross_val_score -from sklearn.linear_model import Ridge, Lasso for Model in [Ridge, Lasso]: model = Model() diff --git a/packages/scikit-learn/examples/plot_polynomial_regression.py b/packages/scikit-learn/examples/plot_polynomial_regression.py index a2037771d..39a3a3e02 100644 --- a/packages/scikit-learn/examples/plot_polynomial_regression.py +++ b/packages/scikit-learn/examples/plot_polynomial_regression.py @@ -8,9 +8,8 @@ """ import numpy as np -import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap - +import matplotlib.pyplot as plt from sklearn import linear_model # Create color maps for 3-class classification problem, as with iris diff --git a/packages/scikit-learn/examples/plot_separator.py b/packages/scikit-learn/examples/plot_separator.py index abd4c4c6e..ee5503027 100644 --- a/packages/scikit-learn/examples/plot_separator.py +++ b/packages/scikit-learn/examples/plot_separator.py @@ -8,8 +8,8 @@ import numpy as np import matplotlib.pyplot as plt -from sklearn.linear_model import SGDClassifier from sklearn.datasets import make_blobs +from sklearn.linear_model import SGDClassifier # we create 50 separable synthetic points X, Y = make_blobs(n_samples=50, centers=2, random_state=0, cluster_std=0.60) diff --git a/packages/scikit-learn/examples/plot_svm_non_linear.py b/packages/scikit-learn/examples/plot_svm_non_linear.py index e71e8dff2..275b859fd 100644 --- a/packages/scikit-learn/examples/plot_svm_non_linear.py +++ b/packages/scikit-learn/examples/plot_svm_non_linear.py @@ -8,10 +8,8 @@ import numpy as np import matplotlib.pyplot as plt - from sklearn import svm - rng = np.random.default_rng(27446968) ############################################################################## diff --git a/packages/scikit-learn/examples/plot_variance_linear_regr.py b/packages/scikit-learn/examples/plot_variance_linear_regr.py index 4fdf0fc0e..9ab2c14b3 100644 --- a/packages/scikit-learn/examples/plot_variance_linear_regr.py +++ b/packages/scikit-learn/examples/plot_variance_linear_regr.py @@ -7,7 +7,6 @@ """ import numpy as np - # Smaller figures import matplotlib.pyplot as plt diff --git a/packages/statistics/examples/plot_airfare.py b/packages/statistics/examples/plot_airfare.py index 0b757f527..2102a0d35 100644 --- a/packages/statistics/examples/plot_airfare.py +++ b/packages/statistics/examples/plot_airfare.py @@ -18,10 +18,11 @@ # Standard library imports import os +import requests + ############################################################################## # Load the data import pandas -import requests if not os.path.exists("airfares.txt"): # Download the file if it is not present diff --git a/packages/statistics/examples/plot_iris_analysis.py b/packages/statistics/examples/plot_iris_analysis.py index f00ae0259..43cdfec32 100644 --- a/packages/statistics/examples/plot_iris_analysis.py +++ b/packages/statistics/examples/plot_iris_analysis.py @@ -12,10 +12,8 @@ """ import matplotlib.pyplot as plt - import pandas from pandas import plotting - from statsmodels.formula.api import ols # Load the data diff --git a/packages/statistics/examples/plot_paired_boxplots.py b/packages/statistics/examples/plot_paired_boxplots.py index cfcd0fc74..9c1310771 100644 --- a/packages/statistics/examples/plot_paired_boxplots.py +++ b/packages/statistics/examples/plot_paired_boxplots.py @@ -10,9 +10,8 @@ """ -import pandas - import matplotlib.pyplot as plt +import pandas data = pandas.read_csv("brain_size.csv", sep=";", na_values=".") diff --git a/packages/statistics/examples/plot_regression.py b/packages/statistics/examples/plot_regression.py index c0bb5f564..2bed1eba5 100644 --- a/packages/statistics/examples/plot_regression.py +++ b/packages/statistics/examples/plot_regression.py @@ -11,10 +11,8 @@ import numpy as np import matplotlib.pyplot as plt import pandas - # For statistics. Requires statsmodels 5.0 or more from statsmodels.formula.api import ols - # Analysis of Variance (ANOVA) on linear models from statsmodels.stats.anova import anova_lm diff --git a/packages/statistics/examples/plot_regression_3d.py b/packages/statistics/examples/plot_regression_3d.py index 95f7a343b..622418ae0 100644 --- a/packages/statistics/examples/plot_regression_3d.py +++ b/packages/statistics/examples/plot_regression_3d.py @@ -12,14 +12,11 @@ import numpy as np import matplotlib.pyplot as plt -import pandas - # For 3d plots. This import is necessary to have 3D plotting below from mpl_toolkits.mplot3d import Axes3D - +import pandas # For statistics. Requires statsmodels 5.0 or more from statsmodels.formula.api import ols - # Analysis of Variance (ANOVA) on linear models from statsmodels.stats.anova import anova_lm diff --git a/packages/statistics/examples/plot_wage_data.py b/packages/statistics/examples/plot_wage_data.py index 6364d85ce..b0d25fbcd 100644 --- a/packages/statistics/examples/plot_wage_data.py +++ b/packages/statistics/examples/plot_wage_data.py @@ -17,12 +17,12 @@ # Standard library imports import os -import matplotlib.pyplot as plt +import requests +import matplotlib.pyplot as plt ############################################################################## # Load the data import pandas -import requests if not os.path.exists("wages.txt"): # Download the file if it is not present diff --git a/packages/statistics/examples/plot_wage_education_gender.py b/packages/statistics/examples/plot_wage_education_gender.py index e29e97117..8d460144d 100644 --- a/packages/statistics/examples/plot_wage_education_gender.py +++ b/packages/statistics/examples/plot_wage_education_gender.py @@ -13,13 +13,13 @@ """ +import os +import urllib + ############################################################################## # Load and massage the data import pandas -import urllib -import os - if not os.path.exists("wages.txt"): # Download the file if it is not present urllib.urlretrieve("http://lib.stat.cmu.edu/datasets/CPS_85_Wages", "wages.txt") diff --git a/packages/statistics/examples/solutions/plot_brain_size.py b/packages/statistics/examples/solutions/plot_brain_size.py index b95b2ca2e..3bbf2214c 100644 --- a/packages/statistics/examples/solutions/plot_brain_size.py +++ b/packages/statistics/examples/solutions/plot_brain_size.py @@ -32,8 +32,8 @@ # This plotting is useful to get an intuitions on the relationships between # our different variables -from pandas import plotting import matplotlib.pyplot as plt +from pandas import plotting # Fill in the missing values for Height for plotting data["Height"].fillna(method="pad", inplace=True) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 000000000..e05c696aa --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,7 @@ +[tool.isort] +known_spcore = "numpy, scipy" +known_mpl = "matplotlib, mpl_toolkits" +known_pydatas = "pandas, seabborn, skimage, sklearn, statsmodels, sympy" +sections = "FUTURE,STDLIB,THIRDPARTY,SPCORE,MPL,FIRSTPARTY,PYDATAS,LOCALFOLDER" +no_lines_before= "MPL,FIRSTPARTY,PYDATAS" +force_sort_within_sections = true diff --git a/pyximages/numpy_fancy_indexing.py b/pyximages/numpy_fancy_indexing.py index e331a986f..ea814e60f 100644 --- a/pyximages/numpy_fancy_indexing.py +++ b/pyximages/numpy_fancy_indexing.py @@ -5,9 +5,10 @@ """ +from math import cos, radians, sin import os import sys -from math import cos, radians, sin + from pyx import canvas, color, path, text, unit diff --git a/pyximages/numpy_indexing.py b/pyximages/numpy_indexing.py index 94932928d..222ddf550 100644 --- a/pyximages/numpy_indexing.py +++ b/pyximages/numpy_indexing.py @@ -5,9 +5,10 @@ """ +from math import cos, radians, sin import os import sys -from math import cos, radians, sin + from pyx import canvas, color, path, text, unit