From e6f721774e93acc5368975bc5b310374d19db1da Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 20 Nov 2026 09:40:59 -0700 Subject: [PATCH 01/10] updated RTD config --- .readthedocs.yaml | 4 ++-- pyproject.toml | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 650261c..83de8a6 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -6,9 +6,9 @@ version: 2 build: - os: "ubuntu-22.04" + os: "ubuntu-26.04" tools: - python: "mambaforge-22.9" + python: "miniforge3-latest" sphinx: configuration: docs/source/conf.py diff --git a/pyproject.toml b/pyproject.toml index bb6678d..d4efd0f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,6 +24,8 @@ classifiers = [ "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", "Topic :: Scientific/Engineering", "Topic :: Software Development :: Libraries :: Python Modules" ] From 50ae8b35d8a0bfe258db72e2d65e4bc40aea797c Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 20 Nov 2026 09:43:13 -0700 Subject: [PATCH 02/10] support Python 3.13 and 3.14 + bump GH actions - test 3.13 and 3.14 - see #36 - bump GH actions - update docs and CHANGES --- .github/workflows/ci.yaml | 22 +++++++++++++++------- .github/workflows/deploy.yaml | 4 ++-- CHANGES | 5 +++++ README.rst | 2 +- docs/source/installation.txt | 2 +- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5c88345..47b8de0 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -23,29 +23,37 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macOS-latest, windows-latest] - python-version: ["3.9", "3.10", "3.11", "3.12"] + python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"] exclude: - os: macOS-latest python-version: "3.9" - os: macOS-latest - python-version: "3.10" + python-version: "3.10" - os: macOS-latest python-version: "3.11" + - os: macOS-latest + python-version: "3.12" + - os: macOS-latest + python-version: "3.13" - os: windows-latest python-version: "3.9" - os: windows-latest - python-version: "3.10" + python-version: "3.10" - os: windows-latest python-version: "3.11" + - os: windows-latest + python-version: "3.12" + - os: windows-latest + python-version: "3.13" steps: - name: git checkout - uses: actions/checkout@v4 + uses: actions/checkout@v7 with: fetch-tags: true - name: Setup Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v7 with: python-version: ${{ matrix.python-version }} @@ -72,9 +80,9 @@ jobs: pytest -v --cov=numkit --cov-report=xml --color=yes numkit/tests - name: Codecov - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v7 with: name: codecov-${{ matrix.os }}-py${{ matrix.python-version }} token: ${{ secrets.CODECOV_TOKEN }} - file: ./coverage.xml + files: ./coverage.xml fail_ci_if_error: false diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 53b2c19..d88e54d 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -22,7 +22,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v7 - name: testpypi_deploy uses: MDAnalysis/pypi-deployment@main @@ -44,7 +44,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v7 - name: pypi_deploy uses: MDAnalysis/pypi-deployment@main diff --git a/CHANGES b/CHANGES index 999837f..82921f2 100644 --- a/CHANGES +++ b/CHANGES @@ -2,6 +2,11 @@ CHANGELOG for numkit ====================== +????-??-?? 1.3.2 +orbeckst + +* added support/testing for Python 3.13 and 3.14 + 2024-09-18 1.3.1 hsk17, orbeckst diff --git a/README.rst b/README.rst index b06b5d7..269bf84 100644 --- a/README.rst +++ b/README.rst @@ -34,7 +34,7 @@ https://github.com/Becksteinlab/numkit :: Python support ============== -Python 3 support for releases 3.9 - 3.12 is tested on Linux, macOS, +Python 3 support for releases 3.9 - 3.14 is tested on Linux, macOS, and Windows. (Python 2.7 and older version of Python 3 were supported up to release 1.2.3.) diff --git a/docs/source/installation.txt b/docs/source/installation.txt index 98fef34..720dbc9 100644 --- a/docs/source/installation.txt +++ b/docs/source/installation.txt @@ -12,7 +12,7 @@ Install with :program:`pip` from the `numkit PyPi repository`_ with :: pip install numkit -Python 2.7 and recent Python 3 are all supported. +Python 3.9 - 3.14 are supported. .. _numkit PyPi repository: https://pypi.org/project/numkit/ From 1d91087ca366e4a9d3eebd45c96052b5817bc682 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 20 Nov 2026 09:44:27 -0700 Subject: [PATCH 03/10] enable dependabot for GH actions update --- .github/dependabot.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..5ace460 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" From 398eb7a5fa04e6f115ab171da0c3a4ea5e351b96 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 20 Nov 2026 09:53:39 -0700 Subject: [PATCH 04/10] use ruff for linting and formatting - add GH ruff workflow - noted ruff use in developer docs - removed pytest pep8 plugin (not supported anymore) - pinned ruff --- .github/workflows/ci.yaml | 2 +- .github/workflows/ruff.yaml | 32 ++++++++++++++++++++++++++++++++ docs/source/src.txt | 9 +++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/ruff.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 47b8de0..3aae551 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -63,7 +63,7 @@ jobs: - name: Install pytest, pytest plugins run: | python -m pip install wheel - python -m pip install pytest pytest-cov pytest-pep8 + python -m pip install pytest pytest-cov - name: Install main dependencies run: | diff --git a/.github/workflows/ruff.yaml b/.github/workflows/ruff.yaml new file mode 100644 index 0000000..d7866a4 --- /dev/null +++ b/.github/workflows/ruff.yaml @@ -0,0 +1,32 @@ +name: Ruff + +on: + push: + branches: + - "main" + pull_request: + branches: + - "main" + +concurrency: + group: "ruff-${{ github.ref }}-${{ github.head_ref }}" + cancel-in-progress: true + +jobs: + ruff: + name: Lint and format + runs-on: ubuntu-latest + steps: + - name: git checkout + uses: actions/checkout@v7 + + - name: Run ruff check + uses: astral-sh/ruff-action@v4.1.0 + with: + src: "./numkit" + + - name: Run ruff format + uses: astral-sh/ruff-action@v4.1.0 + with: + args: "format --check" + src: "./numkit" diff --git a/docs/source/src.txt b/docs/source/src.txt index 637b5a6..ec67d4d 100644 --- a/docs/source/src.txt +++ b/docs/source/src.txt @@ -27,6 +27,15 @@ Contributing Contributions in the form of `feedback, bug reports, suggestions`_ and pull requests are very welcome. +Code style is checked with Ruff_ for both linting and formatting. +Please run :: + + ruff check numkit + ruff format numkit + +before opening a pull request. + .. _numkit git repository: https://github.com/Becksteinlab/numkit .. _feedback, bug reports, suggestions: http://github.com/Becksteinlab/numkit/issues +.. _Ruff: https://docs.astral.sh/ruff/ From 2c96c560fa0abb2c84bf9d2d980d66485bdfc7f8 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 20 Nov 2026 09:53:58 -0700 Subject: [PATCH 05/10] reformatted all code with ruff --- docs/source/conf.py | 127 ++++++++++++------------ numkit/__init__.py | 4 +- numkit/fitting.py | 150 +++++++++++++++++++---------- numkit/integration.py | 116 ++++++++++++---------- numkit/observables.py | 151 +++++++++++++++++------------ numkit/tests/__init__.py | 1 - numkit/tests/test_fitting.py | 76 +++++++++------ numkit/tests/test_integration.py | 105 ++++++++++++-------- numkit/tests/test_module.py | 4 + numkit/tests/test_observables.py | 109 ++++++++++++--------- numkit/tests/test_timeseries.py | 52 ++++++---- numkit/timeseries.py | 160 ++++++++++++++++++++----------- 12 files changed, 644 insertions(+), 411 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 191bc26..3711220 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -14,6 +14,7 @@ import sys import os import datetime + # https://sphinx-rtd-theme.readthedocs.io/en/stable/ import sphinx_rtd_theme @@ -22,73 +23,77 @@ # is relative to the documentation root, use os.path.abspath to make it # absolute, like shown here. # make sure sphinx always uses the current branch -#sys.path.insert(0, os.path.abspath('../..')) +# sys.path.insert(0, os.path.abspath('../..')) # General configuration # --------------------- # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx', - 'sphinx.ext.mathjax', 'sphinx.ext.viewcode', - 'sphinx_rtd_theme'] +extensions = [ + "sphinx.ext.autodoc", + "sphinx.ext.intersphinx", + "sphinx.ext.mathjax", + "sphinx.ext.viewcode", + "sphinx_rtd_theme", +] -mathjax_path = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML' +mathjax_path = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML" # Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +templates_path = ["_templates"] # The suffix of source filenames. -source_suffix = '.txt' +source_suffix = ".txt" # The master toctree document. -master_doc = 'index' +master_doc = "index" # General substitutions. -project = u'numkit' +project = "numkit" now = datetime.datetime.now() -copyright = u'2009-{}, The Authors of numkit (see AUTHORS)'.format(now.year) +copyright = "2009-{}, The Authors of numkit (see AUTHORS)".format(now.year) # The default replacements for |version| and |release|, also used in various # other places throughout the built documents. # # Dynamically calculate the version (uses versioneer) -packageversion = __import__('numkit').__version__ +packageversion = __import__("numkit").__version__ # The short X.Y version. -version = '.'.join(packageversion.split('.')[:2]) +version = ".".join(packageversion.split(".")[:2]) # The full version, including alpha/beta/rc tags. release = packageversion # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: -#today = '' +# today = '' # Else, today_fmt is used as the format for a strftime call. -today_fmt = '%B %d, %Y' +today_fmt = "%B %d, %Y" # List of documents that shouldn't be included in the build. -#unused_docs = [] +# unused_docs = [] # List of directories, relative to source directories, that shouldn't be searched # for source files. exclude_trees = [] # The reST default role (used for this markup: `text`) to use for all documents. -#default_role = None +# default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. -#add_function_parentheses = True +# add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). -#add_module_names = True +# add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. -#show_authors = False +# show_authors = False # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' +pygments_style = "sphinx" # Options for HTML output @@ -96,37 +101,35 @@ # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. -html_theme = 'sphinx_rtd_theme' +html_theme = "sphinx_rtd_theme" html_theme_options = { - 'display_version': True, - 'prev_next_buttons_location': 'bottom', - 'style_external_links': False, + "display_version": True, + "prev_next_buttons_location": "bottom", + "style_external_links": False, # Toc options - 'collapse_navigation': True, - 'sticky_navigation': True, - 'navigation_depth': 4, - 'includehidden': True, - 'titles_only': False, + "collapse_navigation": True, + "sticky_navigation": True, + "navigation_depth": 4, + "includehidden": True, + "titles_only": False, } # Add any paths that contain custom themes here, relative to this directory. -html_theme_path = [ - sphinx_rtd_theme.get_html_theme_path() -] +html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". -#html_title = None +# html_title = None # A shorter title for the navigation bar. Default is the same as html_title. -#html_short_title = None +# html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. -#html_logo = "_static/logos/GromacsWrapper_logo_200x200.png" +# html_logo = "_static/logos/GromacsWrapper_logo_200x200.png" # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 @@ -136,90 +139,90 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +html_static_path = ["_static"] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. -html_last_updated_fmt = '%b %d, %Y' +html_last_updated_fmt = "%b %d, %Y" # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. -#html_use_smartypants = True +# html_use_smartypants = True # Custom sidebar templates, maps document names to template names. -#html_sidebars = {} +# html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. -#html_additional_pages = {} +# html_additional_pages = {} # If false, no module index is generated. -#html_use_modindex = True +# html_use_modindex = True # If false, no index is generated. -#html_use_index = True +# html_use_index = True # If true, the index is split into individual pages for each letter. -#html_split_index = False +# html_split_index = False # If true, the reST sources are included in the HTML build as _sources/. -#html_copy_source = True +# html_copy_source = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. -#html_use_opensearch = '' +# html_use_opensearch = '' # If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml"). -#html_file_suffix = '' +# html_file_suffix = '' # Output file base name for HTML help builder. -htmlhelp_basename = 'numkitdoc' +htmlhelp_basename = "numkitdoc" # Options for LaTeX output # ------------------------ # The paper size ('letter' or 'a4'). -#latex_paper_size = 'letter' +# latex_paper_size = 'letter' # The font size ('10pt', '11pt' or '12pt'). -#latex_font_size = '10pt' +# latex_font_size = '10pt' # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, document class [howto/manual]). latex_documents = [ - ('index', 'numkit.tex', u'numkit Documentation', - u'Oliver Beckstein', 'manual'), + ("index", "numkit.tex", "numkit Documentation", "Oliver Beckstein", "manual"), ] # The name of an image file (relative to this directory) to place at the top of # the title page. -#latex_logo = None +# latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. -#latex_use_parts = False +# latex_use_parts = False # Additional stuff for the LaTeX preamble. -#latex_preamble = '' +# latex_preamble = '' # Documents to append as an appendix to all manuals. -#latex_appendices = [] +# latex_appendices = [] # If false, no module index is generated. -#latex_use_modindex = True +# latex_use_modindex = True # Options for ext.intersphinx # --------------------------- # intersphinx: reference standard lib and RecSQL # http://sphinx.pocoo.org/latest/ext/intersphinx.html -intersphinx_mapping = {'python': ('https://docs.python.org', None), - 'numpy': ('https://numpy.org/doc/stable/', None), - 'scipy': ('https://docs.scipy.org/doc/scipy/reference/', None), - 'GromacsWrapper': ('https://gromacswrapper.readthedocs.org/en/latest', None), - } +intersphinx_mapping = { + "python": ("https://docs.python.org", None), + "numpy": ("https://numpy.org/doc/stable/", None), + "scipy": ("https://docs.scipy.org/doc/scipy/reference/", None), + "GromacsWrapper": ("https://gromacswrapper.readthedocs.org/en/latest", None), +} # Options for ext.autodoc @@ -229,5 +232,3 @@ # This value selects what content will be inserted into the main body of an autoclass directive. # "class", "init", "both" autoclass_content = "both" - - diff --git a/numkit/__init__.py b/numkit/__init__.py index d50c2b5..62166d9 100644 --- a/numkit/__init__.py +++ b/numkit/__init__.py @@ -32,9 +32,11 @@ """ -__all__ = ['fitting', 'timeseries', 'integration', 'observables'] +__all__ = ["fitting", "timeseries", "integration", "observables"] + class LowAccuracyWarning(Warning): """Warns that results may possibly have low accuracy.""" + from ._version import __version__ diff --git a/numkit/fitting.py b/numkit/fitting.py index df875d1..01ae135 100644 --- a/numkit/fitting.py +++ b/numkit/fitting.py @@ -4,10 +4,11 @@ import numpy import logging + logger = logging.getLogger("numkit.fitting") -def Pearson_r(x,y): +def Pearson_r(x, y): """Pearson's r (correlation coefficient). Pearson(x,y) --> correlation coefficient @@ -20,9 +21,10 @@ def Pearson_r(x,y): covxy = numpy.sum((x-Ex)*(y-Ey)) r = covxy/math.sqrt(numpy.sum((x-Ex)**2)*numpy.sum((y-Ey)**2)) """ - return numpy.corrcoef(x,y)[1,0] + return numpy.corrcoef(x, y)[1, 0] + -def linfit(x,y,dy=None): +def linfit(x, y, dy=None): """Fit a straight line y = a + bx to the data in *x* and *y*. Errors on y should be provided in dy in order to assess the @@ -69,49 +71,55 @@ def linfit(x,y,dy=None): raise ValueError("lengths of x and y must match: {0!s} != {1!s}".format(n, m)) try: - have_dy = (len(dy) > 0) + have_dy = len(dy) > 0 except TypeError: have_dy = False if not have_dy: dy = numpy.ones((n), numpy.float64) - x = numpy.asarray(x) - y = numpy.asarray(y) + x = numpy.asarray(x) + y = numpy.asarray(y) dy = numpy.asarray(dy) - s2 = dy*dy - S = numpy.add.reduce(1/s2) - Sx = numpy.add.reduce(x/s2) - Sy = numpy.add.reduce(y/s2) - Sxx = numpy.add.reduce(x*x/s2) - Sxy = numpy.add.reduce(x*y/s2) + s2 = dy * dy + S = numpy.add.reduce(1 / s2) + Sx = numpy.add.reduce(x / s2) + Sy = numpy.add.reduce(y / s2) + Sxx = numpy.add.reduce(x * x / s2) + Sxy = numpy.add.reduce(x * y / s2) - t = (x - Sx/S)/dy - Stt = numpy.add.reduce(t*t) + t = (x - Sx / S) / dy + Stt = numpy.add.reduce(t * t) - b = numpy.add.reduce(t*y/dy)/Stt - a = (Sy - Sx*b)/S + b = numpy.add.reduce(t * y / dy) / Stt + a = (Sy - Sx * b) / S - sa = numpy.sqrt((1 + (Sx*Sx)/(S*Stt))/S) - sb = numpy.sqrt(1/Stt) + sa = numpy.sqrt((1 + (Sx * Sx) / (S * Stt)) / S) + sb = numpy.sqrt(1 / Stt) - covab = -Sx/(S*Stt) - r = covab/(sa*sb) + covab = -Sx / (S * Stt) + r = covab / (sa * sb) - chi2 = numpy.add.reduce(((y-a-b*x)/dy)**2) + chi2 = numpy.add.reduce(((y - a - b * x) / dy) ** 2) if not have_dy: # estimate error if none were provided - sigmadata = numpy.sqrt(chi2/(n-2)) + sigmadata = numpy.sqrt(chi2 / (n - 2)) sa *= sigmadata sb *= sigmadata Q = 1.0 else: - Q = scipy.stats.distributions.chi2.sf(chi2, n-2) + Q = scipy.stats.distributions.chi2.sf(chi2, n - 2) - return {"intercept":a,"slope":b, - "sigma_intercept":sa,"sigma_slope":sb, - "parameter_correlation":r, "chi_square":chi2, "Q":Q} + return { + "intercept": a, + "slope": b, + "sigma_intercept": sa, + "sigma_slope": sb, + "parameter_correlation": r, + "chi_square": chi2, + "Q": Q, + } class FitFunc(object): @@ -135,19 +143,26 @@ class FitFunc(object): 1D-numpy array) with :meth:`FitFunc.fit`. """ - def __init__(self,x,y,parameters=None): + + def __init__(self, x, y, parameters=None): import scipy.optimize + _x = numpy.asarray(x) _y = numpy.asarray(y) p0 = self._get_initial_values(parameters) fitfunc = self.f_factory() - def errfunc(p,x,y): - return fitfunc(p,x) - y # residuals - p,msg = scipy.optimize.leastsq(errfunc,p0[:],args=(_x,_y)) + + def errfunc(p, x, y): + return fitfunc(p, x) - y # residuals + + p, msg = scipy.optimize.leastsq(errfunc, p0[:], args=(_x, _y)) try: p[0] self.parameters = p - except (TypeError,IndexError,): + except ( + TypeError, + IndexError, + ): # TypeError for int p, IndexError for numpy scalar (new scipy) self.parameters = [p] self.message = msg @@ -156,9 +171,13 @@ def f_factory(self): """Stub for fit function factory, which returns the fit function. Override for derived classes. """ - def fitfunc(p,x): + + def fitfunc(p, x): # return f(p,x); should be a numpy ufunc - raise NotImplementedError("base class must be extended for each fit function") + raise NotImplementedError( + "base class must be extended for each fit function" + ) + return fitfunc def _get_initial_values(self, parameters=None): @@ -167,7 +186,11 @@ def _get_initial_values(self, parameters=None): try: p0[:] = parameters except ValueError: - raise ValueError("Wrong number of custom initital values {0!r}, should be something like {1!r}".format(parameters, p0)) + raise ValueError( + "Wrong number of custom initital values {0!r}, should be something like {1!r}".format( + parameters, p0 + ) + ) return p0 def initial_values(self): @@ -175,43 +198,59 @@ def initial_values(self): # return [1.0, 2.0, 0.5] raise NotImplementedError("base class must be extended for each fit function") - def fit(self,x): + def fit(self, x): """Applies the fit to all *x* values""" fitfunc = self.f_factory() - return fitfunc(self.parameters,numpy.asarray(x)) + return fitfunc(self.parameters, numpy.asarray(x)) + class FitExp(FitFunc): """y = f(x) = exp(-p[0]*x)""" + def f_factory(self): - def fitfunc(p,x): - return numpy.exp(-p[0]*x) # exp(-B*x) + def fitfunc(p, x): + return numpy.exp(-p[0] * x) # exp(-B*x) + return fitfunc + def initial_values(self): return [1.0] + def __repr__(self): - return "" + return "" + class FitExp2(FitFunc): """y = f(x) = p[0]*exp(-p[1]*x) + (1-p[0])*exp(-p[2]*x)""" + def f_factory(self): - def fitfunc(p,x): - return p[0]*numpy.exp(-p[1]*x) + (1-p[0])*numpy.exp(-p[2]*x) + def fitfunc(p, x): + return p[0] * numpy.exp(-p[1] * x) + (1 - p[0]) * numpy.exp(-p[2] * x) + return fitfunc + def initial_values(self): - return [0.5,0.1,1e-4] + return [0.5, 0.1, 1e-4] + def __repr__(self): - return "" + return "" + class FitLin(FitFunc): """y = f(x) = p[0]*x + p[1]""" + def f_factory(self): - def fitfunc(p,x): - return p[0]*x + p[1] + def fitfunc(p, x): + return p[0] * x + p[1] + return fitfunc + def initial_values(self): - return [1.0,0.0] + return [1.0, 0.0] + def __repr__(self): - return "" + return "" + class FitGauss(FitFunc): r"""y = f(x) = p[2] * 1/sqrt(2*pi*p[1]**2) * exp(-(x-p[0])**2/(2*p[1]**2)) @@ -222,11 +261,20 @@ class FitGauss(FitFunc): * p[1] == standard deviation $\sigma$ * p[2] == scale $a$ """ + def f_factory(self): - def fitfunc(p,x): - return p[2] * 1.0/(p[1]*numpy.sqrt(2*numpy.pi)) * numpy.exp(-(x-p[0])**2/(2*p[1]**2)) + def fitfunc(p, x): + return ( + p[2] + * 1.0 + / (p[1] * numpy.sqrt(2 * numpy.pi)) + * numpy.exp(-((x - p[0]) ** 2) / (2 * p[1] ** 2)) + ) + return fitfunc + def initial_values(self): - return [0.0,1.0,0.0] + return [0.0, 1.0, 0.0] + def __repr__(self): - return "" + return "" diff --git a/numkit/integration.py b/numkit/integration.py index c040d2d..1792ad5 100644 --- a/numkit/integration.py +++ b/numkit/integration.py @@ -22,10 +22,13 @@ def tupleset(t, i, value): l[i] = value return tuple(l) + import logging + logger = logging.getLogger("numkit.integration") -def simps_error(dy, x=None, dx=1, axis=-1, even='avg'): + +def simps_error(dy, x=None, dx=1, axis=-1, even="avg"): """Error on integral evaluated with `Simpson's rule`_ from errors of points, *dy*. Evaluate the integral with :func:`scipy.integrate.simps`. For a @@ -66,40 +69,39 @@ def simps_error(dy, x=None, dx=1, axis=-1, even='avg'): shapex[axis] = x.shape[0] saveshape = x.shape returnshape = True - x=x.reshape(tuple(shapex)) + x = x.reshape(tuple(shapex)) elif len(x.shape) != len(dy.shape): - raise ValueError("If given, shape of x must be 1-d or the " - "same as dy.") + raise ValueError("If given, shape of x must be 1-d or the same as dy.") if x.shape[axis] != N: - raise ValueError("If given, length of x along axis must be the " \ - "same as dy.") + raise ValueError("If given, length of x along axis must be the same as dy.") else: last_dx = first_dx = dx avglast_dx = avgfirst_dx = dx returnshape = False if N % 2 == 0: - val = 0.0 # holds trapezoidal error**2 - result = 0.0 # holds Simpson error**2 + val = 0.0 # holds trapezoidal error**2 + result = 0.0 # holds Simpson error**2 correction = 0.0 # overlap correction for combining trapz and Simpson - slice1 = (slice(None),)*nd - slice2 = (slice(None),)*nd - if not even in ['avg', 'last', 'first']: - raise ValueError( - "Parameter 'even' must be 'avg', 'last', or 'first'.") - if even in ['avg', 'first']: + slice1 = (slice(None),) * nd + slice2 = (slice(None),) * nd + if not even in ["avg", "last", "first"]: + raise ValueError("Parameter 'even' must be 'avg', 'last', or 'first'.") + if even in ["avg", "first"]: # Compute using Simpson's rule on first intervals slice0 = tupleset(slice1, axis, -3) slice1 = tupleset(slice1, axis, -2) slice2 = tupleset(slice2, axis, -1) if x is not None: - last_dx = x[slice2] - x[slice1] + last_dx = x[slice2] - x[slice1] penult_dx = x[slice1] - x[slice0] - avglast_dx = 0.5*(penult_dx + last_dx) + avglast_dx = 0.5 * (penult_dx + last_dx) val += _trapz_2pt_error2(dy[slice1], dy[slice2], last_dx) - result += _simps_error2(dy,0,N-3,x,dx,axis) - correction += _trapz_simps_overlap2(dy[slice1], avglast_dx) # avglast_dx not strictly correct for x!=None! - if even in ['avg', 'last']: + result += _simps_error2(dy, 0, N - 3, x, dx, axis) + correction += _trapz_simps_overlap2( + dy[slice1], avglast_dx + ) # avglast_dx not strictly correct for x!=None! + if even in ["avg", "last"]: # Compute using Simpson's rule on last set of intervals slice1 = tupleset(slice1, axis, 0) slice2 = tupleset(slice2, axis, 1) @@ -107,11 +109,13 @@ def simps_error(dy, x=None, dx=1, axis=-1, even='avg'): if x is not None: first_dx = x[slice2] - x[slice1] second_dx = x[slice3] - x[slice2] - avgfirst_dx = 0.5*(second_dx + first_dx) + avgfirst_dx = 0.5 * (second_dx + first_dx) val += _trapz_2pt_error2(dy[slice1], dy[slice2], first_dx) - result += _simps_error2(dy,1,N-2,x,dx,axis) - correction += _trapz_simps_overlap2(dy[slice2], avgfirst_dx) # avgfirst_dx not strictly correct for x!=None! - if even == 'avg': + result += _simps_error2(dy, 1, N - 2, x, dx, axis) + correction += _trapz_simps_overlap2( + dy[slice2], avgfirst_dx + ) # avgfirst_dx not strictly correct for x!=None! + if even == "avg": # simply average the variances of 'odd' and 'even' because the two # data sets are not independent; hence it would be wrong to # estimate it as avg**2 = 0.5*sqrt(da1**2+da2**2) @@ -120,11 +124,12 @@ def simps_error(dy, x=None, dx=1, axis=-1, even='avg'): correction /= 2.0 result = result + val + correction # err_simps**2 + err_trapez**2 + correction else: - result = _simps_error2(dy,0,N-2,x,dx,axis) + result = _simps_error2(dy, 0, N - 2, x, dx, axis) if returnshape: x = x.reshape(saveshape) return numpy.sqrt(result) + def _trapz_simps_overlap2(dy, dx): """Correction term in the squared error when combining trapezoidal and Simpson's rule. @@ -135,9 +140,10 @@ def _trapz_simps_overlap2(dy, dx): (3+2)^2 = 3^2 + 2^2 + 12 <--- 12 is the "overlap" correction """ - return (dx/6)**2 * 12 * dy**2 + return (dx / 6) ** 2 * 12 * dy**2 -def _simps_error2(dy,start,stop,x,dx,axis): + +def _simps_error2(dy, start, stop, x, dx, axis): """Squared error on Simpson's rule integration. :Arguments: @@ -156,17 +162,19 @@ def _simps_error2(dy,start,stop,x,dx,axis): if start is None: start = 0 step = 2 - all = (slice(None),)*nd + all = (slice(None),) * nd # check that stop is appropriate !!! - slice2k = tupleset(all, axis, slice(start, stop, step)) # does NOT include last point stop+1 - slice2k1 = tupleset(all, axis, slice(start+1, stop+1, step)) - slice0 = tupleset(all, axis, slice(start, start+1)) # first point - sliceN = tupleset(all, axis, slice(stop+1, stop+2)) # last point - - Df2k = dy[slice2k] # 2k - Df2k1 = dy[slice2k1] # 2k+1 - Df0 = dy[slice0] # first = 0 - DfN = dy[sliceN] # last = N + slice2k = tupleset( + all, axis, slice(start, stop, step) + ) # does NOT include last point stop+1 + slice2k1 = tupleset(all, axis, slice(start + 1, stop + 1, step)) + slice0 = tupleset(all, axis, slice(start, start + 1)) # first point + sliceN = tupleset(all, axis, slice(stop + 1, stop + 2)) # last point + + Df2k = dy[slice2k] # 2k + Df2k1 = dy[slice2k1] # 2k+1 + Df0 = dy[slice0] # first = 0 + DfN = dy[sliceN] # last = N if x is None: # Even spaced Simpson's rule. # Simpson error propagation for I = (h/3)*(f_1 + 4f_2 + 2f_3 + 4f_4 + ... + 4f_{N-1} + f_N) @@ -177,39 +185,47 @@ def _simps_error2(dy,start,stop,x,dx,axis): # In order to work with nd-arrays I need to get the axis of '- 3*Df0**2 + DfN**2' # but my numpy-foo is weak and I don't know how to extract this axis nicely: Hence # sum the single term with the axis argument :-( - result = (dx/3.0)**2 * (numpy.add.reduce(((2*Df2k)**2 + (4*Df2k1)**2), axis) \ - + numpy.add.reduce(-3*Df0**2 + DfN**2, axis)) + result = (dx / 3.0) ** 2 * ( + numpy.add.reduce(((2 * Df2k) ** 2 + (4 * Df2k1) ** 2), axis) + + numpy.add.reduce(-3 * Df0**2 + DfN**2, axis) + ) else: - logger.warning("Approximating Simpson integration statistical error with the average spacing.") + logger.warning( + "Approximating Simpson integration statistical error with the average spacing." + ) dx = numpy.diff(x).mean() - result = _simps_error2(dy,start,stop,None,dx,axis) + result = _simps_error2(dy, start, stop, None, dx, axis) return result + def _trapz_2pt_error2(dy1, dy2, dx): """Squared error on the trapezoidal rule for two points. For errors dy1 and dy2 and spacing dx.""" - return (0.5*dx)**2 * (dy1**2 + dy2**2) + return (0.5 * dx) ** 2 * (dy1**2 + dy2**2) + -def _trapz_error2(dy,start,stop,x,dx,axis): +def _trapz_error2(dy, start, stop, x, dx, axis): # not tested nd = len(dy.shape) if start is None: start = 0 step = 1 - all = (slice(None),)*nd - slicek = tupleset(all, axis, slice(start, stop+2, step)) # all points; stop+2 ?? - slice0 = tupleset(all, axis, slice(start, start+1)) # first point - sliceN = tupleset(all, axis, slice(stop+1, stop+2)) # last point + all = (slice(None),) * nd + slicek = tupleset(all, axis, slice(start, stop + 2, step)) # all points; stop+2 ?? + slice0 = tupleset(all, axis, slice(start, start + 1)) # first point + sliceN = tupleset(all, axis, slice(stop + 1, stop + 2)) # last point - Dfk = dy[slicek] # 2k - Df0 = dy[slice0] # first = 0 - DfN = dy[sliceN] # last = N + Dfk = dy[slicek] # 2k + Df0 = dy[slice0] # first = 0 + DfN = dy[sliceN] # last = N if x is None: # from error propagation of I = (h/2) * (f_1 + 2f_2 + 2f_3 + ... + 2f_{N-1) + f_N) - result = (0.5*dx)**2 * (4 * numpy.add.reduce(Dfk**2, axis) - 3*(Df0**2 + DfN**2)) + result = (0.5 * dx) ** 2 * ( + 4 * numpy.add.reduce(Dfk**2, axis) - 3 * (Df0**2 + DfN**2) + ) else: raise NotImplementedError return result diff --git a/numkit/observables.py b/numkit/observables.py index 6b9991c..7e4e7c5 100644 --- a/numkit/observables.py +++ b/numkit/observables.py @@ -5,6 +5,7 @@ import numpy + class QID(frozenset): """Identity of a :class:`QuantityWithError`. @@ -25,17 +26,21 @@ class QID(frozenset): the terms of the expression. In a certain sense, the "history" of a quantity becomes its "identity". """ + def __new__(cls, iterable=None): if iterable is None: - self = None #super(QID,cls).__new__(cls) + self = None # super(QID,cls).__new__(cls) else: - self = super(QID,cls).__new__(cls, asiterable(iterable)) + self = super(QID, cls).__new__(cls, asiterable(iterable)) return self + def union(self, x): return super(QID, self).union(asiterable(x)) # hack... + def __repr__(self): return "QID({0!r})".format(list(self)) + class QuantityWithError(object): """Number with error and basic error propagation arithmetic. @@ -53,6 +58,7 @@ class QuantityWithError(object): .. _error propagation: http://mathworld.wolfram.com/ErrorPropagation.html """ + # A quantity has a unique id which is conserved through operations with # itself. "Sameness" of two quantities is tested on this id with # :meth:`QuantityWithError.isSame`. @@ -70,13 +76,13 @@ class QuantityWithError(object): # TODO: Add qid-conservation to special case "self"-operations. def __init__(self, value, error=None, qid=None, **kwargs): # value can be another QuantityWithError instance - val,err,otherqid = self._astuple(value) # use data of other instances + val, err, otherqid = self._astuple(value) # use data of other instances if not error is None: - pass # kwargs take precedence over any other error + pass # kwargs take precedence over any other error elif err != 0: - error = err # get from other instance + error = err # get from other instance else: - error = 0 # default for a quantity WITHOUT error + error = 0 # default for a quantity WITHOUT error self.value = val self.variance = error**2 @@ -87,17 +93,17 @@ def __init__(self, value, error=None, qid=None, **kwargs): # unique qids ("identity is their history") # - qid is implemented as a frozenset - _qid = set() # identity accumulates in qid, i.e. flattened history + _qid = set() # identity accumulates in qid, i.e. flattened history qid = qid or otherqid # kwargs qid takes precedence; otherqid is likely None if qid is None and error != 0: # generate qid for quantities WITH error - qid = id(self) # unique new id + qid = id(self) # unique new id _qid.update([q for q in asiterable(qid) if q is not None]) - self.qid = QID(_qid) # freeze so that we can use it as a key + self.qid = QID(_qid) # freeze so that we can use it as a key del _qid # TODO: covariance will need to be updated from other - self.covariance = {self.qid: self.variance} # record covariances with other - self.confidence = kwargs.pop('confidence', 0.99) # for comparisons/not used yet + self.covariance = {self.qid: self.variance} # record covariances with other + self.confidence = kwargs.pop("confidence", 0.99) # for comparisons/not used yet @property def error(self): @@ -113,7 +119,7 @@ def error(self): @error.setter def error(self, x): - self.variance = x*x + self.variance = x * x def isSame(self, other): """Check if *other* is 100% correlated with *self*. @@ -151,7 +157,7 @@ def isSame(self, other): return False def _dist(self, x, y): - return numpy.sqrt(x*x + y*y) + return numpy.sqrt(x * x + y * y) @staticmethod def asQuantityWithError(other): @@ -190,69 +196,89 @@ def __repr__(self): def __add__(self, other): """x.__add__(y) <--> x + y""" - val,err,qid = self._astuple(other) + val, err, qid = self._astuple(other) if self.isSame(other): - return QuantityWithError(self.value + val, numpy.abs(self.error+err), qid=self.qid) - return QuantityWithError(self.value + val, self._dist(self.error, err), qid=self.qid.union(qid)) + return QuantityWithError( + self.value + val, numpy.abs(self.error + err), qid=self.qid + ) + return QuantityWithError( + self.value + val, self._dist(self.error, err), qid=self.qid.union(qid) + ) def __radd__(self, other): """x.__radd__(y) <--> y + x""" - val,err,qid = self._astuple(other) + val, err, qid = self._astuple(other) if self.isSame(other): - return QuantityWithError(self.value + val, numpy.abs(self.error+err), qid=self.qid) - return QuantityWithError(val + self.value, self._dist(self.error, err), qid=self.qid.union(qid)) + return QuantityWithError( + self.value + val, numpy.abs(self.error + err), qid=self.qid + ) + return QuantityWithError( + val + self.value, self._dist(self.error, err), qid=self.qid.union(qid) + ) def __sub__(self, other): """x.__sub__(y) <--> x - y""" - val,err,qid = self._astuple(other) + val, err, qid = self._astuple(other) if self.isSame(other): # error should come out as 0 for a-a - return QuantityWithError(self.value - val, numpy.abs(self.error-err), qid=self.qid) - return QuantityWithError(self.value - val, self._dist(self.error, err), qid=self.qid.union(qid)) + return QuantityWithError( + self.value - val, numpy.abs(self.error - err), qid=self.qid + ) + return QuantityWithError( + self.value - val, self._dist(self.error, err), qid=self.qid.union(qid) + ) def __rsub__(self, other): """x.__rsub__(y) <--> y - x""" - val,err,qid = self._astuple(other) + val, err, qid = self._astuple(other) if self.isSame(other): # error should come out as 0 for a-a - return QuantityWithError(val - self.value, numpy.abs(self.error-err), qid=self.qid) - return QuantityWithError(val - self.value, self._dist(self.error, err), qid=self.qid.union(qid)) + return QuantityWithError( + val - self.value, numpy.abs(self.error - err), qid=self.qid + ) + return QuantityWithError( + val - self.value, self._dist(self.error, err), qid=self.qid.union(qid) + ) def __mul__(self, other): """x.__mul__(y) <--> x * y""" - val,err,qid = self._astuple(other) + val, err, qid = self._astuple(other) if self.isSame(other): # TODO: error not correct in the general case (?) - return QuantityWithError(self.value * val, numpy.abs(self.error*err), qid=self.qid) - error = self._dist(val*self.error, err*self.value) + return QuantityWithError( + self.value * val, numpy.abs(self.error * err), qid=self.qid + ) + error = self._dist(val * self.error, err * self.value) return QuantityWithError(self.value * val, error=error, qid=self.qid.union(qid)) def __rmul__(self, other): """x.__rmul__(y) <--> y * x""" - val,err,qid = self._astuple(other) + val, err, qid = self._astuple(other) if self.isSame(other): # TODO: error not correct in the general case (?) - return QuantityWithError(val * self.value, numpy.abs(self.error*err), qid=self.qid) - error = self._dist(val*self.error, err*self.value) + return QuantityWithError( + val * self.value, numpy.abs(self.error * err), qid=self.qid + ) + error = self._dist(val * self.error, err * self.value) return QuantityWithError(val * self.value, error=error, qid=self.qid.union(qid)) def __truediv__(self, other): """x.__div__(y) <--> x / y""" - val,err,qid = self._astuple(other) + val, err, qid = self._astuple(other) if self.isSame(other): # TODO: error not correct in the general case - return QuantityWithError(self.value/val, 0, qid=self.qid) - error = self._dist(self.error/val, err*self.value/val**2) - return QuantityWithError(self.value/val, error=error, qid=self.qid.union(qid)) + return QuantityWithError(self.value / val, 0, qid=self.qid) + error = self._dist(self.error / val, err * self.value / val**2) + return QuantityWithError(self.value / val, error=error, qid=self.qid.union(qid)) def __rtruediv__(self, other): """x.__rdiv__(y) <--> y / x""" - val,err,qid = self._astuple(other) + val, err, qid = self._astuple(other) if self.isSame(other): # TODO: error not correct in the general case - return QuantityWithError(val/self.value, 0, qid=self.qid) - error = self._dist(err/self.value, self.error*val/self.value**2) - return QuantityWithError(val/self.value, error=error, qid=self.qid.union(qid)) + return QuantityWithError(val / self.value, 0, qid=self.qid) + error = self._dist(err / self.value, self.error * val / self.value**2) + return QuantityWithError(val / self.value, error=error, qid=self.qid.union(qid)) # Python 2 compatible __div__ = __truediv__ @@ -265,34 +291,38 @@ def __neg__(self): def __pow__(self, other, z=None): """x.__pow__(y) <--> x**y""" if not z is None: - raise NotImplementedError("only pow(self, y) implemented, not pow(self,y,z)") - x,dx = self.value, self.error - y,dy,yqid = self._astuple(other) + raise NotImplementedError( + "only pow(self, y) implemented, not pow(self,y,z)" + ) + x, dx = self.value, self.error + y, dy, yqid = self._astuple(other) if self.isSame(other): # not sure if error correct for a**a**a ... f = numpy.power(x, y) - error = numpy.abs(dx*f*(1+numpy.log(x))) + error = numpy.abs(dx * f * (1 + numpy.log(x))) qid = self.qid else: - f = numpy.power(x,y) - error = self._dist(dx*f*y/x, dy*f*numpy.log(x)) + f = numpy.power(x, y) + error = self._dist(dx * f * y / x, dy * f * numpy.log(x)) qid = [self.qid, yqid] return QuantityWithError(f, error, qid=qid) def __rpow__(self, other, z=None): """x.__rpow__(y) <--> y**x""" if not z is None: - raise NotImplementedError("only rpow(self, y) implemented, not rpow(self,y,z)") - x,dx = self.value, self.error - y,dy,yqid = self._astuple(other) + raise NotImplementedError( + "only rpow(self, y) implemented, not rpow(self,y,z)" + ) + x, dx = self.value, self.error + y, dy, yqid = self._astuple(other) if self.isSame(other): # not sure if error correct for a**a**a ... f = numpy.power(y, x) - error = numpy.abs(dy*f*(1+numpy.log(y))) + error = numpy.abs(dy * f * (1 + numpy.log(y))) qid = self.qid else: f = numpy.power(y, x) - error = self._dist(dy*f*x/y, dx*f*numpy.log(y)) + error = self._dist(dy * f * x / y, dx * f * numpy.log(y)) qid = [self.qid, yqid] return QuantityWithError(f, error, qid=qid) @@ -303,26 +333,26 @@ def __abs__(self): def __cmp__(self, other): """x.__cmp__(other) <==> cmp(x.value,other.value)""" # TODO: make comparison error-aware, i.e. "==" for a given confidence interval - val,err,qid = self._astuple(other) + val, err, qid = self._astuple(other) result = cmp(self.value, val) return result def __eq__(self, other): """x.__eq__(other) <==> x.value == other.value""" # TODO: make comparison error-aware, i.e. "==" for a given confidence interval - val,err,qid = self._astuple(other) + val, err, qid = self._astuple(other) return self.value == val def __lt__(self, other): """x.__lt__(other) <==> x.value < other.value""" # TODO: make comparison error-aware, i.e. "==" for a given confidence interval - val,err,qid = self._astuple(other) + val, err, qid = self._astuple(other) return self.value < val def __le__(self, other): """x.__le__(other) <==> x.value <= other.value""" # TODO: make comparison error-aware, i.e. "==" for a given confidence interval - val,err,qid = self._astuple(other) + val, err, qid = self._astuple(other) return self.value <= val def __ge__(self, other): @@ -342,22 +372,25 @@ def copy(self): def deepcopy(self): """Create an exact copy with the same identity.""" - return QuantityWithError(self.value, self.error, qid=self.qid, confidence=self.confidence) + return QuantityWithError( + self.value, self.error, qid=self.qid, confidence=self.confidence + ) def iterable(obj): """Returns ``True`` if *obj* can be iterated over and is *not* a string.""" if type(obj) is str: - return False # avoid iterating over characters of a string + return False # avoid iterating over characters of a string - if hasattr(obj, 'next'): - return True # any iterator will do + if hasattr(obj, "next"): + return True # any iterator will do try: - len(obj) # anything else that might work + len(obj) # anything else that might work except TypeError: return False return True + def asiterable(obj): """Returns obj so that it can be iterated over; a string is *not* treated as iterable""" if not iterable(obj): diff --git a/numkit/tests/__init__.py b/numkit/tests/__init__.py index 56b672d..5d2f706 100644 --- a/numkit/tests/__init__.py +++ b/numkit/tests/__init__.py @@ -53,4 +53,3 @@ .. _SciPy testing guidelines: http://projects.scipy.org/numpy/wiki/TestingGuidelines#id11 """ - diff --git a/numkit/tests/test_fitting.py b/numkit/tests/test_fitting.py index 15c70e9..83e3e68 100644 --- a/numkit/tests/test_fitting.py +++ b/numkit/tests/test_fitting.py @@ -10,6 +10,7 @@ ==================================== """ + import numkit.fitting import numpy as np @@ -17,25 +18,29 @@ import pytest + @pytest.fixture(scope="module") def linear_data(m=-2.8, t=123.45, sigma=1.5): def f(x): - return m*x + t + return m * x + t + # make random numbers consistent np.random.seed(2018) X = np.linspace(-20, 20, 100) - dY = sigma*np.random.standard_normal(size=len(X)) + dY = sigma * np.random.standard_normal(size=len(X)) Y = f(X) + dY return X, Y, dY + @pytest.fixture(scope="module") def exp_decay_data(a=2.30785, sigma=1.5): def f(x): - return np.exp(-a*x) + return np.exp(-a * x) + # make random numbers consistent np.random.seed(2018) X = np.linspace(-4, 10, 100) - dY = sigma*np.random.standard_normal(size=len(X)) + dY = sigma * np.random.standard_normal(size=len(X)) Y = f(X) + dY return X, Y, dY @@ -43,45 +48,56 @@ def f(x): @pytest.fixture(scope="module") def gaussian_data(mu=-4.2, s=0.455, a=8.0, sigma=0.5): def f(x): - return a/(np.sqrt(2*np.pi*s**2)) * np.exp(-(x-mu)**2/(2*s**2)) + return a / (np.sqrt(2 * np.pi * s**2)) * np.exp(-((x - mu) ** 2) / (2 * s**2)) + # make random numbers consistent np.random.seed(2018) X = np.linspace(-10, 2, 100) - dY = sigma*np.random.standard_normal(size=len(X)) + dY = sigma * np.random.standard_normal(size=len(X)) Y = f(X) + dY return X, Y, dY - @pytest.fixture(scope="module") def linfit_noerror(linear_data): X, Y, _ = linear_data return numkit.fitting.linfit(X, Y) + @pytest.fixture(scope="module") def linfit_witherror(linear_data): X, Y, dY = linear_data return numkit.fitting.linfit(X, Y, dy=dY) -@pytest.mark.parametrize("quantity,expected", [ - ("intercept", 123.29089673580293), - ("slope", -2.8087043835295797), - ("sigma_intercept", 0.1389869156801219), - ("sigma_slope", 0.011916849634869048), - ("parameter_correlation", 0), - ("chi_square", 189.3101547566785), - ("Q", 1.0)]) + +@pytest.mark.parametrize( + "quantity,expected", + [ + ("intercept", 123.29089673580293), + ("slope", -2.8087043835295797), + ("sigma_intercept", 0.1389869156801219), + ("sigma_slope", 0.011916849634869048), + ("parameter_correlation", 0), + ("chi_square", 189.3101547566785), + ("Q", 1.0), + ], +) def test_linfit_noerror(linfit_noerror, quantity, expected): assert_almost_equal(linfit_noerror[quantity], expected) -@pytest.mark.parametrize("quantity,expected", [ - ("intercept", 123.4425486304815), - ("slope", -2.8047542762556232), - ("sigma_intercept", 0.007963180881154172), - ("sigma_slope", 0.0025757387307418587), - ("parameter_correlation", 0.7171768191864278), - ("chi_square", 96.28302496281943), - ("Q", 0.5301523165173313)]) + +@pytest.mark.parametrize( + "quantity,expected", + [ + ("intercept", 123.4425486304815), + ("slope", -2.8047542762556232), + ("sigma_intercept", 0.007963180881154172), + ("sigma_slope", 0.0025757387307418587), + ("parameter_correlation", 0.7171768191864278), + ("chi_square", 96.28302496281943), + ("Q", 0.5301523165173313), + ], +) def test_linfit_witherror(linfit_witherror, quantity, expected): assert_almost_equal(linfit_witherror[quantity], expected) @@ -91,10 +107,10 @@ def FitLin(linear_data): X, Y, _ = linear_data return numkit.fitting.FitLin(X, Y, parameters=(1, 0)) + class TestFitLin(object): ref_x = np.linspace(-9.3, 5.7, 5) - ref_y = np.array([149.4118475, 138.8792061, 128.3465646, - 117.8139232, 107.2812817]) + ref_y = np.array([149.4118475, 138.8792061, 128.3465646, 117.8139232, 107.2812817]) def test_parameters(self, FitLin): assert_almost_equal(FitLin.parameters, [-2.8087044, 123.2908967]) @@ -109,10 +125,12 @@ def FitExp(exp_decay_data): X, Y, _ = exp_decay_data return numkit.fitting.FitExp(X, Y, parameters=(1.0,)) + class TestFitExp(object): ref_x = np.linspace(-5, 5, 5) - ref_y = np.array([1.0267685e+05, 3.2043229e+02, 1.0000000e+00, - 3.1207841e-03, 9.7392934e-06]) + ref_y = np.array( + [1.0267685e05, 3.2043229e02, 1.0000000e00, 3.1207841e-03, 9.7392934e-06] + ) def test_parameters(self, FitExp): assert_almost_equal(FitExp.parameters, [2.3078684]) @@ -121,8 +139,8 @@ def test_fitting(self, FitExp): Y = FitExp.fit(self.ref_x) assert_allclose(Y, self.ref_y) + def test_FitGauss(gaussian_data): X, Y, _ = gaussian_data fit = numkit.fitting.FitGauss(X, Y, parameters=(-2, 1.0, 5.0)) - assert_almost_equal(fit.parameters, [-4.2093312, 0.4403894, 7.6803708]) - + assert_almost_equal(fit.parameters, [-4.2093312, 0.4403894, 7.6803708]) diff --git a/numkit/tests/test_integration.py b/numkit/tests/test_integration.py index 32306a6..c52e711 100644 --- a/numkit/tests/test_integration.py +++ b/numkit/tests/test_integration.py @@ -10,6 +10,7 @@ ==================================== """ + import pytest import numpy @@ -23,76 +24,105 @@ class Test_simps_error(object): # that one can easily calculate the correct errors @pytest.fixture def spacings(self): - even_x = numpy.linspace(0,10,10) - odd_x = numpy.linspace(0,10,11) + even_x = numpy.linspace(0, 10, 10) + odd_x = numpy.linspace(0, 10, 11) return { - 'even_x': even_x, # const spacing - 'even_dx': numpy.diff(even_x)[0], - 'even_dy': 1.0 * numpy.ones(10, dtype=float), # const error - 'odd_x': odd_x, # const spacing - 'odd_dx': numpy.diff(odd_x)[0], - 'odd_dy': 1.0 * numpy.ones(11, dtype=float), # const error + "even_x": even_x, # const spacing + "even_dx": numpy.diff(even_x)[0], + "even_dy": 1.0 * numpy.ones(10, dtype=float), # const error + "odd_x": odd_x, # const spacing + "odd_dx": numpy.diff(odd_x)[0], + "odd_dy": 1.0 * numpy.ones(11, dtype=float), # const error } def err_analytic_odd(self, spacings): # error for all dy and dx equal, using only Simpson's rule: # err**2 = (h/3)^2(df1^2 + (4df2)^2 + (2df3)^2 + ... + (4df_N-1)^2 + (dfN)^2) # err**2 = (h/3)^2 dy^2 (10(N-1)-(2^2-1)+1) - dy = spacings['odd_dy'][0] - N = spacings['odd_dy'].shape[0] - h = spacings['odd_dx'] - return numpy.sqrt((h/3)**2 * dy**2 * (10*(N-1)-2)) + dy = spacings["odd_dy"][0] + N = spacings["odd_dy"].shape[0] + h = spacings["odd_dx"] + return numpy.sqrt((h / 3) ** 2 * dy**2 * (10 * (N - 1) - 2)) def err_analytic_even(self, spacings): # err**2 = (h/6)^2((3df1)^2 + ((3+2)df2)^2 + (8df3)^2 + (4df4)^2 + (8df5)^2 + ... + (2dfN)^2) # special case for constant dy and constant spacing - dy = spacings['even_dy'][0] - N = spacings['even_dy'].shape[0] - h = spacings['even_dx'] - #err_analytic**2 = (h/6.0)**2 * dy**2 * (2*3**2 + 12 + (8**2+4**2)*0.5*(N-2) - (4**2-2**2) + 2**2) - return numpy.sqrt((h/6.0)**2 * dy**2 * (40.*N - 58.)) + dy = spacings["even_dy"][0] + N = spacings["even_dy"].shape[0] + h = spacings["even_dx"] + # err_analytic**2 = (h/6.0)**2 * dy**2 * (2*3**2 + 12 + (8**2+4**2)*0.5*(N-2) - (4**2-2**2) + 2**2) + return numpy.sqrt((h / 6.0) ** 2 * dy**2 * (40.0 * N - 58.0)) def test_odd_const_dx(self, spacings): - err_dx = numkit.integration.simps_error(spacings['odd_dy'], dx=spacings['odd_dx']) + err_dx = numkit.integration.simps_error( + spacings["odd_dy"], dx=spacings["odd_dx"] + ) err_analytic = self.err_analytic_odd(spacings) assert_equal(err_dx, err_analytic, err_msg="Simps error for const spacing dx") def test_odd_const_x(self, spacings): - err_x = numkit.integration.simps_error(spacings['odd_dy'], x=spacings['odd_x']) + err_x = numkit.integration.simps_error(spacings["odd_dy"], x=spacings["odd_x"]) err_analytic = self.err_analytic_odd(spacings) - assert_equal(err_x, err_analytic, err_msg="Simps error for const spacing dx, from abscissas") + assert_equal( + err_x, + err_analytic, + err_msg="Simps error for const spacing dx, from abscissas", + ) def test_odd_const_x_dx(self, spacings): - err_dx = numkit.integration.simps_error(spacings['odd_dy'], dx=spacings['odd_dx']) - err_x = numkit.integration.simps_error(spacings['odd_dy'], x=spacings['odd_x']) - assert_equal(err_x, err_dx, err_msg="Simps error for const spacing: either scalar dx or abscissas x") + err_dx = numkit.integration.simps_error( + spacings["odd_dy"], dx=spacings["odd_dx"] + ) + err_x = numkit.integration.simps_error(spacings["odd_dy"], x=spacings["odd_x"]) + assert_equal( + err_x, + err_dx, + err_msg="Simps error for const spacing: either scalar dx or abscissas x", + ) def test_even_first(self, spacings): err_analytic = self.err_analytic_even(spacings) - err = numkit.integration.simps_error(spacings['even_dy'], dx=spacings['even_dx'], even="first") - assert_almost_equal(err, err_analytic, - err_msg="Even # points, first: Simps+Trapezoid stat. error: const dx and const dy") + err = numkit.integration.simps_error( + spacings["even_dy"], dx=spacings["even_dx"], even="first" + ) + assert_almost_equal( + err, + err_analytic, + err_msg="Even # points, first: Simps+Trapezoid stat. error: const dx and const dy", + ) def test_even_last(self, spacings): err_analytic = self.err_analytic_even(spacings) - err = numkit.integration.simps_error(spacings['even_dy'], dx=spacings['even_dx'], even="last") - assert_almost_equal(err, err_analytic, - err_msg="Even # points, last: Trapezoid+Simps stat. error: const dx and const dy") + err = numkit.integration.simps_error( + spacings["even_dy"], dx=spacings["even_dx"], even="last" + ) + assert_almost_equal( + err, + err_analytic, + err_msg="Even # points, last: Trapezoid+Simps stat. error: const dx and const dy", + ) def test_even_avg(self, spacings): err_analytic = self.err_analytic_even(spacings) - err = numkit.integration.simps_error(spacings['even_dy'], dx=spacings['even_dx'], even="avg") - assert_almost_equal(err, err_analytic, - err_msg="Even # points, avg: Simps+Trapezoid stat. error: const dx and const dy") + err = numkit.integration.simps_error( + spacings["even_dy"], dx=spacings["even_dx"], even="avg" + ) + assert_almost_equal( + err, + err_analytic, + err_msg="Even # points, avg: Simps+Trapezoid stat. error: const dx and const dy", + ) def Xtest_h1h2(self): - x = numpy.array([0,1,2,4.,5]) - y = 2*x - dy = numpy.array([1.,1,1,1,1]) + x = numpy.array([0, 1, 2, 4.0, 5]) + y = 2 * x + dy = numpy.array([1.0, 1, 1, 1, 1]) - err = numkit.integration.simps_error(dy,x=x) + err = numkit.integration.simps_error(dy, x=x) # check correctness!! - assert_almost_equal(err, 2.7613402542968153, err_msg="Simps error for un-even spacing") + assert_almost_equal( + err, 2.7613402542968153, err_msg="Simps error for un-even spacing" + ) def test_uneven_spacing(): @@ -101,4 +131,3 @@ def test_uneven_spacing(): X = numpy.array([-1, 0, 2, 2.5, 5]) err = numkit.integration.simps_error(DY, x=X) assert err == pytest.approx(0.7632168761236874) - diff --git a/numkit/tests/test_module.py b/numkit/tests/test_module.py index 36f6ea0..52c2fee 100644 --- a/numkit/tests/test_module.py +++ b/numkit/tests/test_module.py @@ -1,12 +1,16 @@ import pytest import re + def test_version_exists(): import numkit + assert numkit.__version__ + def test_version_pep440_like(): import numkit + v = numkit.__version__ match = re.match(r"\d+\.\d+\.\d+", v) diff --git a/numkit/tests/test_observables.py b/numkit/tests/test_observables.py index ec8fe74..f7e2ce4 100644 --- a/numkit/tests/test_observables.py +++ b/numkit/tests/test_observables.py @@ -10,6 +10,7 @@ ==================================== """ + from __future__ import division import numkit.observables @@ -27,9 +28,16 @@ def test_None(): assert q is None @staticmethod - @pytest.mark.parametrize("identifiers", [ - (0, 1, 42), 42, ("a", "b", "a"), ("aba", ), "aba", - ]) + @pytest.mark.parametrize( + "identifiers", + [ + (0, 1, 42), + 42, + ("a", "b", "a"), + ("aba",), + "aba", + ], + ) def test_create(identifiers): q = numkit.observables.QID(identifiers) assert q == set(numkit.observables.asiterable(identifiers)) @@ -44,10 +52,12 @@ def test_union(): assert isinstance(q3, frozenset) assert q3 == set([1, 2, 3, 4, 2, 3, 5]) + @pytest.fixture def Q1(): return numkit.observables.QuantityWithError(1.0, 0.5) + @pytest.fixture def Q1b(): return numkit.observables.QuantityWithError(1.0, 0.5) @@ -57,9 +67,11 @@ def Q1b(): def Q2(): return numkit.observables.QuantityWithError(-1.0, 1.0) + @pytest.fixture() def Q(Q1, Q1b, Q2): - return {'Q1': Q1, 'Q1b': Q1b, 'Q2': Q2} + return {"Q1": Q1, "Q1b": Q1b, "Q2": Q2} + class TestQuantityWithError(object): def test_astuple(self, Q1): @@ -84,53 +96,65 @@ def test_set_error(self, Q2, error=2.0): Q2.error = error assert Q2.variance == error**2 - @pytest.mark.parametrize("q1,q2,same", [ - ("Q1", "Q1", True), - ("Q1", "Q1b", False), - ("Q1", "Q2", False), - ("Q2", "Q2", True), - ]) + @pytest.mark.parametrize( + "q1,q2,same", + [ + ("Q1", "Q1", True), + ("Q1", "Q1b", False), + ("Q1", "Q2", False), + ("Q2", "Q2", True), + ], + ) def test_sameness(self, Q, q1, q2, same): assert (Q[q1].isSame(Q[q2])) == same - @pytest.mark.parametrize("q1,q2,same", [ - ("Q1", "Q1", True), - ("Q1", "Q1b", True), - ("Q1", "Q2", False), - ("Q2", "Q2", True), - ]) + @pytest.mark.parametrize( + "q1,q2,same", + [ + ("Q1", "Q1", True), + ("Q1", "Q1b", True), + ("Q1", "Q2", False), + ("Q2", "Q2", True), + ], + ) def test_equality(self, Q, q1, q2, same): assert (Q[q1] == Q[q2]) == same - @pytest.mark.parametrize("op,ref", - [("Q1 {} Q2".format(op), - "Q1.value {} Q2.value".format(op)) for op in - (">", ">=", "<", "<=", "==")]) + @pytest.mark.parametrize( + "op,ref", + [ + ("Q1 {} Q2".format(op), "Q1.value {} Q2.value".format(op)) + for op in (">", ">=", "<", "<=", "==") + ], + ) def test_comparisons(self, Q1, Q2, op, ref): assert eval(op) == eval(ref) - - @pytest.mark.parametrize("op,value", [ - (lambda x, y: 5*x, True), - (lambda x, y: x*5, True), - (lambda x, y: -x, True), - (lambda x, y: abs(x), True), - (lambda x, y: x + x, True), - (lambda x, y: x - x, True), - (lambda x, y: x * x, True), - (lambda x, y: x * x * x, True), - (lambda x, y: x / x, True), - (lambda x, y: 5 + x, True), - (lambda x, y: 5 - x, True), - (lambda x, y: x - 5, True), - (lambda x, y: x**x, True), - pytest.param(lambda x, y: x**3, True, - marks=pytest.mark.xfail), # need to fix - (lambda x, y: x - 5*y, False), - (lambda x, y: x * y, False), - (lambda x, y: y / x, False), - (lambda x, y: x**y, False), - ] ) + @pytest.mark.parametrize( + "op,value", + [ + (lambda x, y: 5 * x, True), + (lambda x, y: x * 5, True), + (lambda x, y: -x, True), + (lambda x, y: abs(x), True), + (lambda x, y: x + x, True), + (lambda x, y: x - x, True), + (lambda x, y: x * x, True), + (lambda x, y: x * x * x, True), + (lambda x, y: x / x, True), + (lambda x, y: 5 + x, True), + (lambda x, y: 5 - x, True), + (lambda x, y: x - 5, True), + (lambda x, y: x**x, True), + pytest.param( + lambda x, y: x**3, True, marks=pytest.mark.xfail + ), # need to fix + (lambda x, y: x - 5 * y, False), + (lambda x, y: x * y, False), + (lambda x, y: y / x, False), + (lambda x, y: x**y, False), + ], + ) def test_sameness_algebra(self, Q1, Q2, op, value): assert Q1.isSame(op(Q1, Q2)) == value @@ -141,4 +165,3 @@ def test_copy(self, Q1): def test_deepcopy(self, Q1): other = Q1.deepcopy() assert Q1.isSame(other) - diff --git a/numkit/tests/test_timeseries.py b/numkit/tests/test_timeseries.py index 085310f..19e6bf9 100644 --- a/numkit/tests/test_timeseries.py +++ b/numkit/tests/test_timeseries.py @@ -11,14 +11,17 @@ import pytest + @pytest.fixture(scope="module") def data(): - X = np.linspace(-10,10,50000) - yerr = np.random.randn(len(X))*0.05 + X = np.linspace(-10, 10, 50000) + yerr = np.random.randn(len(X)) * 0.05 return np.vstack((X, np.sin(X) + yerr)) + binsizes = (5, 100, 1000, 10000) + class TestRegularizedFunction(object): @pytest.mark.parametrize("bins", binsizes) def test_max(self, data, bins): @@ -27,19 +30,22 @@ def test_max(self, data, bins): assert_almost_equal(np.max(Y), np.max(data[1])) @pytest.mark.filterwarnings("ignore:tcorrel") - @pytest.mark.parametrize("func", ( - "rms_histogrammed_function", - "mean_histogrammed_function", - "std_histogrammed_function", - "min_histogrammed_function", - "max_histogrammed_function", - "median_histogrammed_function", - "percentile_histogrammed_function", - "error_histogrammed_function", - "circmean_histogrammed_function", - "circstd_histogrammed_function", - "tc_histogrammed_function", - )) + @pytest.mark.parametrize( + "func", + ( + "rms_histogrammed_function", + "mean_histogrammed_function", + "std_histogrammed_function", + "min_histogrammed_function", + "max_histogrammed_function", + "median_histogrammed_function", + "percentile_histogrammed_function", + "error_histogrammed_function", + "circmean_histogrammed_function", + "circstd_histogrammed_function", + "tc_histogrammed_function", + ), + ) @pytest.mark.parametrize("bins", binsizes) def test_func(self, func, data, bins): function = getattr(numkit.timeseries, func) @@ -48,10 +54,13 @@ def test_func(self, func, data, bins): # assert_almost_equal(np.max(Y), np.max(data[1])) # add test for Y data (but has randomness) - @pytest.mark.parametrize("func", ( - "error_histogrammed_function", - "tc_histogrammed_function", - )) + @pytest.mark.parametrize( + "func", + ( + "error_histogrammed_function", + "tc_histogrammed_function", + ), + ) @pytest.mark.parametrize("bins", [1000, 10000]) def test_func_warns(self, func, data, bins): function = getattr(numkit.timeseries, func) @@ -59,8 +68,9 @@ def test_func_warns(self, func, data, bins): Y, X = function(data[0], data[1], bins=bins) -@pytest.mark.parametrize('window', ( - 'flat', 'hanning', 'hamming', 'bartlett', 'blackman')) +@pytest.mark.parametrize( + "window", ("flat", "hanning", "hamming", "bartlett", "blackman") +) def test_smooth(data, window): a = numkit.timeseries.smooth(data[1], window=window) assert len(a) == len(data[1]) diff --git a/numkit/timeseries.py b/numkit/timeseries.py index 56cf181..59ca74f 100644 --- a/numkit/timeseries.py +++ b/numkit/timeseries.py @@ -10,12 +10,15 @@ import warnings import logging + logger = logging.getLogger("numkit.timeseries") from numkit import LowAccuracyWarning -def autocorrelation_fft(series, remove_mean=True, paddingcorrection=True, - normalize=False, **kwargs): + +def autocorrelation_fft( + series, remove_mean=True, paddingcorrection=True, normalize=False, **kwargs +): """Calculate the auto correlation function. autocorrelation_fft(series,remove_mean=False,**kwargs) --> acf @@ -60,28 +63,32 @@ def autocorrelation_fft(series, remove_mean=True, paddingcorrection=True, *kwargs* other keyword arguments for :func:`scipy.signal.fftconvolve` """ - kwargs.setdefault('mode','full') + kwargs.setdefault("mode", "full") if len(series.shape) > 2: # var/mean below would need proper axis arguments to deal with high dim raise TypeError("series must be a 1D array at the moment") if remove_mean: - series = numpy.squeeze(series.astype(float)).copy() # must copy because de-meaning modifies it + series = numpy.squeeze( + series.astype(float) + ).copy() # must copy because de-meaning modifies it mean = series.mean() series -= mean else: - series = numpy.squeeze(series.astype(float)) # can deal with a view + series = numpy.squeeze(series.astype(float)) # can deal with a view - ac = scipy.signal.fftconvolve(series,series[::-1,...],**kwargs) + ac = scipy.signal.fftconvolve(series, series[::-1, ...], **kwargs) - origin = int(ac.shape[0]/2) # should work for both odd and even len(series) - ac = ac[origin:] # only use second half of the symmetric acf - assert len(ac) <= len(series), "Oops: len(ac)={0:d} len(series)={1:d}".format(len(ac), len(series)) - if paddingcorrection and not kwargs['mode'] == 'valid': # 'valid' was not 0-padded + origin = int(ac.shape[0] / 2) # should work for both odd and even len(series) + ac = ac[origin:] # only use second half of the symmetric acf + assert len(ac) <= len(series), "Oops: len(ac)={0:d} len(series)={1:d}".format( + len(ac), len(series) + ) + if paddingcorrection and not kwargs["mode"] == "valid": # 'valid' was not 0-padded # correct for 0 padding # XXX: reference? Where did I get this from? (But it makes sense.) - ac *= len(series)/(len(series) - 1.0*numpy.arange(len(ac))) + ac *= len(series) / (len(series) - 1.0 * numpy.arange(len(ac))) norm = ac[0] or 1.0 # to guard against ACFs of zero arrays if not normalize: @@ -93,10 +100,11 @@ def autocorrelation_fft(series, remove_mean=True, paddingcorrection=True, if remove_mean: norm /= numpy.var(series) else: - norm /= numpy.mean(series*series) + norm /= numpy.mean(series * series) except ZeroDivisionError: norm = 1.0 - return ac/norm + return ac / norm + def tcorrel(x, y, nstep=100, debug=False): """Calculate the correlation time and an estimate of the error of the mean . @@ -137,8 +145,10 @@ def tcorrel(x, y, nstep=100, debug=False): _x = x[::nstep] # do not run acf on all data: takes too long _y = y[::nstep] # and does not improve accuracy if len(_y) < 500: # 500 is a bit arbitrary - wmsg = "tcorrel(): Only %d datapoints for the chosen nstep=%d; " \ + wmsg = ( + "tcorrel(): Only %d datapoints for the chosen nstep=%d; " "ACF will possibly not be accurate." % (len(_y), nstep) + ) warnings.warn(wmsg, category=LowAccuracyWarning) logger.warning(wmsg) @@ -146,22 +156,22 @@ def tcorrel(x, y, nstep=100, debug=False): try: i0 = numpy.where(acf <= 0)[0][0] # first root of acf except IndexError: - i0 = -1 # use last value as best estimate + i0 = -1 # use last value as best estimate t0 = _x[i0] # integral of the _normalized_ acf norm = acf[0] or 1.0 # guard against a zero ACF - tc = scipy.integrate.simpson(acf[:i0]/norm, x=_x[:i0]) + tc = scipy.integrate.simpson(acf[:i0] / norm, x=_x[:i0]) # error estimate for the mean [Frenkel & Smit, p526] - sigma = numpy.sqrt(2*tc*acf[0]/(x[-1] - x[0])) + sigma = numpy.sqrt(2 * tc * acf[0] / (x[-1] - x[0])) - result = {'tc':tc, 't0':t0, 'sigma':sigma} + result = {"tc": tc, "t0": t0, "sigma": sigma} if debug: - result['t'] = _x[:i0] - result['acf'] = acf[:i0] + result["t"] = _x[:i0] + result["acf"] = acf[:i0] return result -def smooth(x, window_len=11, window='flat'): +def smooth(x, window_len=11, window="flat"): """smooth the data using a window with requested size. This method is based on the convolution of a scaled window with the signal. @@ -203,12 +213,13 @@ def smooth(x, window_len=11, window='flat'): Source: based on http://www.scipy.org/Cookbook/SignalSmooth """ - windows = {'flat': lambda n: numpy.ones(n, dtype=float), - 'hanning': numpy.hanning, - 'hamming': numpy.hamming, - 'bartlett': numpy.bartlett, - 'blackman': numpy.blackman, - } + windows = { + "flat": lambda n: numpy.ones(n, dtype=float), + "hanning": numpy.hanning, + "hamming": numpy.hamming, + "bartlett": numpy.bartlett, + "blackman": numpy.blackman, + } window_len = int(window_len) if isinstance(window, numpy.ndarray): @@ -218,7 +229,11 @@ def smooth(x, window_len=11, window='flat'): try: w = windows[window](window_len) except KeyError: - raise ValueError("Window {0!r} not supported; must be one of {1!r}".format(window, windows.keys())) + raise ValueError( + "Window {0!r} not supported; must be one of {1!r}".format( + window, windows.keys() + ) + ) if x.ndim != 1: raise ValueError("smooth only accepts 1 dimension arrays.") @@ -229,9 +244,10 @@ def smooth(x, window_len=11, window='flat'): if window_len < 3: return x - s = numpy.r_[x[window_len-1:0:-1], x, x[-1:-window_len:-1]] - y = numpy.convolve(w/w.sum(), s, mode='valid') - return y[(window_len-1)//2:-(window_len-1)//2] # take off repeats on ends + s = numpy.r_[x[window_len - 1 : 0 : -1], x, x[-1:-window_len:-1]] + y = numpy.convolve(w / w.sum(), s, mode="valid") + return y[(window_len - 1) // 2 : -(window_len - 1) // 2] # take off repeats on ends + def smoothing_window_length(resolution, t): """Compute the length of a smooting window of *resolution* time units. @@ -250,11 +266,12 @@ def smoothing_window_length(resolution, t): .. SeeAlso:: :func:`smooth` """ dt = numpy.mean(numpy.diff(t)) - N = int(resolution/dt) + N = int(resolution / dt) if N % 2 == 0: N += 1 return N + def mean_histogrammed_function(t, y, **kwargs): """Compute mean of data *y* in bins along *t*. @@ -264,6 +281,7 @@ def mean_histogrammed_function(t, y, **kwargs): """ return apply_histogrammed_function(numpy.mean, t, y, **kwargs) + def std_histogrammed_function(t, y, **kwargs): """Compute standard deviation of data *y* in bins along *t*. @@ -282,14 +300,17 @@ def rms_histogrammed_function(t, y, **kwargs): :func:`regularized_function` with *func* = ``sqrt(mean(y*y))`` """ - def rms(a, demean=kwargs.pop('demean', False)): + + def rms(a, demean=kwargs.pop("demean", False)): if len(a) == 0: return numpy.nan if demean: a -= numpy.mean(a) - return numpy.sqrt(numpy.mean(a*a)) + return numpy.sqrt(numpy.mean(a * a)) + return apply_histogrammed_function(rms, t, y, **kwargs) + def min_histogrammed_function(t, y, **kwargs): """Compute minimum of data *y* in bins along *t*. @@ -297,12 +318,15 @@ def min_histogrammed_function(t, y, **kwargs): :func:`regularized_function` with *func* = :func:`numpy.min` """ + def _min(a): if len(a) == 0: return numpy.nan return numpy.min(a) + return apply_histogrammed_function(_min, t, y, **kwargs) + def max_histogrammed_function(t, y, **kwargs): """Compute maximum of data *y* in bins along *t*. @@ -310,12 +334,15 @@ def max_histogrammed_function(t, y, **kwargs): :func:`regularized_function` with *func* = :func:`numpy.max` """ + def _max(a): if len(a) == 0: return numpy.nan return numpy.max(a) + return apply_histogrammed_function(_max, t, y, **kwargs) + def median_histogrammed_function(t, y, **kwargs): """Compute median of data *y* in bins along *t*. @@ -325,6 +352,7 @@ def median_histogrammed_function(t, y, **kwargs): """ return apply_histogrammed_function(numpy.median, t, y, **kwargs) + def percentile_histogrammed_function(t, y, **kwargs): """Compute the percentile *per* of data *y* in bins along *t*. @@ -343,43 +371,57 @@ def percentile_histogrammed_function(t, y, **kwargs): :func:`regularized_function` with :func:`scipy.stats.scoreatpercentile` """ - def percentile(a, per=kwargs.pop('per', 50.), limit=kwargs.pop('limit', ()), - demean=kwargs.pop('demean', False), interpolation_method='fraction'): + + def percentile( + a, + per=kwargs.pop("per", 50.0), + limit=kwargs.pop("limit", ()), + demean=kwargs.pop("demean", False), + interpolation_method="fraction", + ): if len(a) == 0: return numpy.nan if demean: a -= numpy.mean(a) return scipy.stats.scoreatpercentile(a, per, limit=limit) + return apply_histogrammed_function(percentile, t, y, **kwargs) + def tc_histogrammed_function(t, y, **kwargs): """Calculate the correlation time in each bin using :func:`tcorrel`. .. Warning:: Not well tested and fragile. """ dt = numpy.mean(numpy.diff(t)) + def get_tcorrel(a): if len(a) == 0: return numpy.nan - t = numpy.cumsum(dt*numpy.ones_like(a)) - dt + t = numpy.cumsum(dt * numpy.ones_like(a)) - dt results = tcorrel(t, a, nstep=1) - return results['tc'] + return results["tc"] + return apply_histogrammed_function(get_tcorrel, t, y, **kwargs) + def error_histogrammed_function(t, y, **kwargs): """Calculate the error in each bin using :func:`tcorrel`. .. Warning:: Not well tested and fragile. """ dt = numpy.mean(numpy.diff(t)) + def get_tcorrel(a): if len(a) == 0: return numpy.nan - t = numpy.cumsum(dt*numpy.ones_like(a)) - dt + t = numpy.cumsum(dt * numpy.ones_like(a)) - dt results = tcorrel(t, a, nstep=1) - return results['sigma'] + return results["sigma"] + return apply_histogrammed_function(get_tcorrel, t, y, **kwargs) + def circmean_histogrammed_function(t, y, **kwargs): """Compute circular mean of data *y* in bins along *t*. @@ -394,14 +436,17 @@ def circmean_histogrammed_function(t, y, **kwargs): .. Note:: Data are interpreted as angles in radians. """ - low = kwargs.pop('low', -numpy.pi) - high = kwargs.pop('high', numpy.pi) + low = kwargs.pop("low", -numpy.pi) + high = kwargs.pop("high", numpy.pi) + def _circmean(a, low=low, high=high): if len(a) == 0: return numpy.nan return scipy.stats.circmean(a, low=low, high=high) + return apply_histogrammed_function(_circmean, t, y, **kwargs) + def circstd_histogrammed_function(t, y, **kwargs): """Compute circular standard deviation of data *y* in bins along *t*. @@ -416,14 +461,17 @@ def circstd_histogrammed_function(t, y, **kwargs): .. Note:: Data are interpreted as angles in radians. """ - low = kwargs.pop('low', -numpy.pi) - high = kwargs.pop('high', numpy.pi) + low = kwargs.pop("low", -numpy.pi) + high = kwargs.pop("high", numpy.pi) + def _circstd(a, low=low, high=high): if len(a) == 0: return numpy.nan return scipy.stats.circstd(a, low=low, high=high) + return apply_histogrammed_function(_circstd, t, y, **kwargs) + def apply_histogrammed_function(func, t, y, **kwargs): """Compute *func* of data *y* in bins along *t*. @@ -438,7 +486,8 @@ def apply_histogrammed_function(func, t, y, **kwargs): """ F, e = regularized_function(t, y, func, **kwargs) - return F, 0.5*(e[:-1] + e[1:]) + return F, 0.5 * (e[:-1] + e[1:]) + def regularized_function(x, y, func, bins=100, range=None): """Compute *func()* over data aggregated in bins. @@ -482,10 +531,10 @@ def regularized_function(x, y, func, bins=100, range=None): raise TypeError("Can only deal with 1D arrays.") # setup of bins (taken from numpy.histogram) - if (range is not None): + if range is not None: mn, mx = range - if (mn > mx): - raise AttributeError('max must be larger than min in range parameter.') + if mn > mx: + raise AttributeError("max must be larger than min in range parameter.") if not numpy.iterable(bins): if range is None: @@ -494,19 +543,20 @@ def regularized_function(x, y, func, bins=100, range=None): if mn == mx: mn -= 0.5 mx += 0.5 - bins = numpy.linspace(mn, mx, bins+1, endpoint=True) + bins = numpy.linspace(mn, mx, bins + 1, endpoint=True) else: bins = numpy.asarray(bins) if (numpy.diff(bins) < 0).any(): - raise ValueError('bins must increase monotonically.') + raise ValueError("bins must increase monotonically.") sorting_index = numpy.argsort(_x) sx = _x[sorting_index] sy = _y[sorting_index] # boundaries in SORTED data that demarcate bins; position in bin_index is the bin number - bin_index = numpy.r_[sx.searchsorted(bins[:-1], 'left'), - sx.searchsorted(bins[-1], 'right')] + bin_index = numpy.r_[ + sx.searchsorted(bins[:-1], "left"), sx.searchsorted(bins[-1], "right") + ] # naive implementation: apply operator to each chunk = sy[start:stop] separately # @@ -514,6 +564,6 @@ def regularized_function(x, y, func, bins=100, range=None): # block = 65536 in numpy.histogram) because there does not seem to be a # general way to combine the chunks for different blocks, just think of # func=median - F = numpy.zeros(len(bins)-1) # final function - F[:] = [func(sy[start:stop]) for start,stop in zip(bin_index[:-1],bin_index[1:])] - return F,bins + F = numpy.zeros(len(bins) - 1) # final function + F[:] = [func(sy[start:stop]) for start, stop in zip(bin_index[:-1], bin_index[1:])] + return F, bins From 17c946393cec395e825e2fb8fa7498456a9053fa Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 20 Nov 2026 10:09:30 -0700 Subject: [PATCH 06/10] fix all ruff linter complaints - ruff safe autofixes - potentially unsafe, applied by cursor/auto after human review - comment out unused code that is useful for understanding the code - rewrite how kwargs are processed in histogrammed timeseries - removed unused __cmp__ method in observables (was only Python 2) --- numkit/__init__.py | 2 +- numkit/fitting.py | 15 +++++++------- numkit/observables.py | 25 ++++++++--------------- numkit/tests/__init__.py | 1 - numkit/tests/test_fitting.py | 12 +++++------ numkit/tests/test_integration.py | 10 ++++----- numkit/tests/test_module.py | 1 - numkit/tests/test_observables.py | 18 +++++++--------- numkit/tests/test_timeseries.py | 14 ++++++------- numkit/timeseries.py | 35 ++++++++++++++++---------------- 10 files changed, 55 insertions(+), 78 deletions(-) diff --git a/numkit/__init__.py b/numkit/__init__.py index 62166d9..821fb8a 100644 --- a/numkit/__init__.py +++ b/numkit/__init__.py @@ -32,7 +32,7 @@ """ -__all__ = ["fitting", "timeseries", "integration", "observables"] +__all__ = ["__version__", "fitting", "integration", "observables", "timeseries"] class LowAccuracyWarning(Warning): diff --git a/numkit/fitting.py b/numkit/fitting.py index 01ae135..1567864 100644 --- a/numkit/fitting.py +++ b/numkit/fitting.py @@ -2,9 +2,10 @@ # Copyright (c) 2010 Oliver Beckstein # Released under the "Modified BSD Licence" (see COPYING). -import numpy import logging +import numpy + logger = logging.getLogger("numkit.fitting") @@ -68,7 +69,7 @@ def linfit(x, y, dy=None): n = len(x) m = len(y) if n != m: - raise ValueError("lengths of x and y must match: {0!s} != {1!s}".format(n, m)) + raise ValueError(f"lengths of x and y must match: {n!s} != {m!s}") try: have_dy = len(dy) > 0 @@ -86,8 +87,8 @@ def linfit(x, y, dy=None): S = numpy.add.reduce(1 / s2) Sx = numpy.add.reduce(x / s2) Sy = numpy.add.reduce(y / s2) - Sxx = numpy.add.reduce(x * x / s2) - Sxy = numpy.add.reduce(x * y / s2) + # Sxx = numpy.add.reduce(x * x / s2) + # Sxy = numpy.add.reduce(x * y / s2) t = (x - Sx / S) / dy Stt = numpy.add.reduce(t * t) @@ -122,7 +123,7 @@ def linfit(x, y, dy=None): } -class FitFunc(object): +class FitFunc: """Fit a function f to data (x,y) using the method of least squares. The function is fitted when the object is created, using @@ -187,9 +188,7 @@ def _get_initial_values(self, parameters=None): p0[:] = parameters except ValueError: raise ValueError( - "Wrong number of custom initital values {0!r}, should be something like {1!r}".format( - parameters, p0 - ) + f"Wrong number of custom initital values {parameters!r}, should be something like {p0!r}" ) return p0 diff --git a/numkit/observables.py b/numkit/observables.py index 7e4e7c5..5f8f5e4 100644 --- a/numkit/observables.py +++ b/numkit/observables.py @@ -1,7 +1,6 @@ # numkit --- observables # Copyright (c) 2010 Oliver Beckstein # Released under the "Modified BSD Licence" (see COPYING). -from __future__ import division import numpy @@ -31,17 +30,17 @@ def __new__(cls, iterable=None): if iterable is None: self = None # super(QID,cls).__new__(cls) else: - self = super(QID, cls).__new__(cls, asiterable(iterable)) + self = super().__new__(cls, asiterable(iterable)) return self def union(self, x): - return super(QID, self).union(asiterable(x)) # hack... + return super().union(asiterable(x)) # hack... def __repr__(self): - return "QID({0!r})".format(list(self)) + return f"QID({list(self)!r})" -class QuantityWithError(object): +class QuantityWithError: """Number with error and basic error propagation arithmetic. The quantity is assumed to be a mean of an observable @@ -189,7 +188,7 @@ def astuple(self): return self.value, self.error def __repr__(self): - return "{0:g} ({1:g})".format(*self.astuple()) + return "{:g} ({:g})".format(*self.astuple()) # NOTE: all the special casing should really be done with the covariance # formulae. Also, check that a+a+a etc produces sensible output... @@ -329,30 +328,22 @@ def __rpow__(self, other, z=None): def __abs__(self): return QuantityWithError(self.value.__abs__(), self.error, qid=self.qid) - # only python 2 - def __cmp__(self, other): - """x.__cmp__(other) <==> cmp(x.value,other.value)""" - # TODO: make comparison error-aware, i.e. "==" for a given confidence interval - val, err, qid = self._astuple(other) - result = cmp(self.value, val) - return result - def __eq__(self, other): """x.__eq__(other) <==> x.value == other.value""" # TODO: make comparison error-aware, i.e. "==" for a given confidence interval - val, err, qid = self._astuple(other) + val, _err, _qid = self._astuple(other) return self.value == val def __lt__(self, other): """x.__lt__(other) <==> x.value < other.value""" # TODO: make comparison error-aware, i.e. "==" for a given confidence interval - val, err, qid = self._astuple(other) + val, _err, _qid = self._astuple(other) return self.value < val def __le__(self, other): """x.__le__(other) <==> x.value <= other.value""" # TODO: make comparison error-aware, i.e. "==" for a given confidence interval - val, err, qid = self._astuple(other) + val, _err, _qid = self._astuple(other) return self.value <= val def __ge__(self, other): diff --git a/numkit/tests/__init__.py b/numkit/tests/__init__.py index 5d2f706..64863e7 100644 --- a/numkit/tests/__init__.py +++ b/numkit/tests/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # numkit test cases # Part of GromacsWrapper # Copyright (c) Oliver Beckstein diff --git a/numkit/tests/test_fitting.py b/numkit/tests/test_fitting.py index 83e3e68..ee854e6 100644 --- a/numkit/tests/test_fitting.py +++ b/numkit/tests/test_fitting.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # numkit.integration test cases # Part of GromacsWrapper # Copyright (c) Oliver Beckstein @@ -11,12 +10,11 @@ """ -import numkit.fitting - import numpy as np -from numpy.testing import assert_equal, assert_almost_equal, assert_allclose - import pytest +from numpy.testing import assert_allclose, assert_almost_equal + +import numkit.fitting @pytest.fixture(scope="module") @@ -108,7 +106,7 @@ def FitLin(linear_data): return numkit.fitting.FitLin(X, Y, parameters=(1, 0)) -class TestFitLin(object): +class TestFitLin: ref_x = np.linspace(-9.3, 5.7, 5) ref_y = np.array([149.4118475, 138.8792061, 128.3465646, 117.8139232, 107.2812817]) @@ -126,7 +124,7 @@ def FitExp(exp_decay_data): return numkit.fitting.FitExp(X, Y, parameters=(1.0,)) -class TestFitExp(object): +class TestFitExp: ref_x = np.linspace(-5, 5, 5) ref_y = np.array( [1.0267685e05, 3.2043229e02, 1.0000000e00, 3.1207841e-03, 9.7392934e-06] diff --git a/numkit/tests/test_integration.py b/numkit/tests/test_integration.py index c52e711..abdfe26 100644 --- a/numkit/tests/test_integration.py +++ b/numkit/tests/test_integration.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # numkit.integration test cases # Part of GromacsWrapper # Copyright (c) Oliver Beckstein @@ -11,15 +10,14 @@ """ -import pytest - import numpy -from numpy.testing import assert_equal, assert_almost_equal +import pytest +from numpy.testing import assert_almost_equal, assert_equal import numkit.integration -class Test_simps_error(object): +class Test_simps_error: # special case with constant spacing and constant error (=1.0) so # that one can easily calculate the correct errors @pytest.fixture @@ -115,7 +113,7 @@ def test_even_avg(self, spacings): def Xtest_h1h2(self): x = numpy.array([0, 1, 2, 4.0, 5]) - y = 2 * x + # y = 2 * x dy = numpy.array([1.0, 1, 1, 1, 1]) err = numkit.integration.simps_error(dy, x=x) diff --git a/numkit/tests/test_module.py b/numkit/tests/test_module.py index 52c2fee..1fe009a 100644 --- a/numkit/tests/test_module.py +++ b/numkit/tests/test_module.py @@ -1,4 +1,3 @@ -import pytest import re diff --git a/numkit/tests/test_observables.py b/numkit/tests/test_observables.py index f7e2ce4..cea5a8f 100644 --- a/numkit/tests/test_observables.py +++ b/numkit/tests/test_observables.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # numkit.integration test cases # Part of GromacsWrapper # Copyright (c) Oliver Beckstein @@ -11,17 +10,14 @@ """ -from __future__ import division - -import numkit.observables - -import numpy as np -from numpy.testing import assert_equal, assert_almost_equal import pytest +from numpy.testing import assert_almost_equal + +import numkit.observables -class TestQID(object): +class TestQID: @staticmethod def test_None(): q = numkit.observables.QID() @@ -50,7 +46,7 @@ def test_union(): q3 = q1.union(q2) assert isinstance(q3, frozenset) - assert q3 == set([1, 2, 3, 4, 2, 3, 5]) + assert q3 == {1, 2, 3, 4, 5} @pytest.fixture @@ -73,7 +69,7 @@ def Q(Q1, Q1b, Q2): return {"Q1": Q1, "Q1b": Q1b, "Q2": Q2} -class TestQuantityWithError(object): +class TestQuantityWithError: def test_astuple(self, Q1): assert Q1.astuple() == (Q1.value, Q1.error) @@ -123,7 +119,7 @@ def test_equality(self, Q, q1, q2, same): @pytest.mark.parametrize( "op,ref", [ - ("Q1 {} Q2".format(op), "Q1.value {} Q2.value".format(op)) + (f"Q1 {op} Q2", f"Q1.value {op} Q2.value") for op in (">", ">=", "<", "<=", "==") ], ) diff --git a/numkit/tests/test_timeseries.py b/numkit/tests/test_timeseries.py index 19e6bf9..614b48c 100644 --- a/numkit/tests/test_timeseries.py +++ b/numkit/tests/test_timeseries.py @@ -1,15 +1,13 @@ -# -*- coding: utf-8 -*- # numkit.integration test cases # Part of GromacsWrapper # Copyright (c) Oliver Beckstein # Published under the Modified BSD Licence. -import numkit.timeseries - import numpy as np -from numpy.testing import assert_almost_equal, assert_equal - import pytest +from numpy.testing import assert_almost_equal + +import numkit.timeseries @pytest.fixture(scope="module") @@ -22,7 +20,7 @@ def data(): binsizes = (5, 100, 1000, 10000) -class TestRegularizedFunction(object): +class TestRegularizedFunction: @pytest.mark.parametrize("bins", binsizes) def test_max(self, data, bins): Y, X = numkit.timeseries.max_histogrammed_function(data[0], data[1], bins=bins) @@ -49,7 +47,7 @@ def test_max(self, data, bins): @pytest.mark.parametrize("bins", binsizes) def test_func(self, func, data, bins): function = getattr(numkit.timeseries, func) - Y, X = function(data[0], data[1], bins=bins) + _Y, X = function(data[0], data[1], bins=bins) assert len(X) == bins # assert_almost_equal(np.max(Y), np.max(data[1])) # add test for Y data (but has randomness) @@ -65,7 +63,7 @@ def test_func(self, func, data, bins): def test_func_warns(self, func, data, bins): function = getattr(numkit.timeseries, func) with pytest.warns(numkit.LowAccuracyWarning): - Y, X = function(data[0], data[1], bins=bins) + _Y, _X = function(data[0], data[1], bins=bins) @pytest.mark.parametrize( diff --git a/numkit/timeseries.py b/numkit/timeseries.py index 59ca74f..f2a2dab 100644 --- a/numkit/timeseries.py +++ b/numkit/timeseries.py @@ -2,15 +2,14 @@ # Copyright (c) 2010 Oliver Beckstein # Released under the "Modified BSD Licence" (see COPYING). +import logging +import warnings + import numpy -import scipy.signal import scipy.integrate +import scipy.signal import scipy.stats -import warnings - -import logging - logger = logging.getLogger("numkit.timeseries") from numkit import LowAccuracyWarning @@ -82,10 +81,8 @@ def autocorrelation_fft( origin = int(ac.shape[0] / 2) # should work for both odd and even len(series) ac = ac[origin:] # only use second half of the symmetric acf - assert len(ac) <= len(series), "Oops: len(ac)={0:d} len(series)={1:d}".format( - len(ac), len(series) - ) - if paddingcorrection and not kwargs["mode"] == "valid": # 'valid' was not 0-padded + assert len(ac) <= len(series), f"Oops: len(ac)={len(ac):d} len(series)={len(series):d}" + if paddingcorrection and kwargs["mode"] != "valid": # 'valid' was not 0-padded # correct for 0 padding # XXX: reference? Where did I get this from? (But it makes sense.) ac *= len(series) / (len(series) - 1.0 * numpy.arange(len(ac))) @@ -146,8 +143,8 @@ def tcorrel(x, y, nstep=100, debug=False): _y = y[::nstep] # and does not improve accuracy if len(_y) < 500: # 500 is a bit arbitrary wmsg = ( - "tcorrel(): Only %d datapoints for the chosen nstep=%d; " - "ACF will possibly not be accurate." % (len(_y), nstep) + f"tcorrel(): Only {len(_y):d} datapoints for the chosen nstep={nstep:d}; " + "ACF will possibly not be accurate." ) warnings.warn(wmsg, category=LowAccuracyWarning) logger.warning(wmsg) @@ -230,9 +227,7 @@ def smooth(x, window_len=11, window="flat"): w = windows[window](window_len) except KeyError: raise ValueError( - "Window {0!r} not supported; must be one of {1!r}".format( - window, windows.keys() - ) + f"Window {window!r} not supported; must be one of {windows.keys()!r}" ) if x.ndim != 1: @@ -300,8 +295,9 @@ def rms_histogrammed_function(t, y, **kwargs): :func:`regularized_function` with *func* = ``sqrt(mean(y*y))`` """ + demean = kwargs.pop("demean", False) - def rms(a, demean=kwargs.pop("demean", False)): + def rms(a, demean=demean): if len(a) == 0: return numpy.nan if demean: @@ -371,12 +367,15 @@ def percentile_histogrammed_function(t, y, **kwargs): :func:`regularized_function` with :func:`scipy.stats.scoreatpercentile` """ + per = kwargs.pop("per", 50.0) + limit = kwargs.pop("limit", ()) + demean = kwargs.pop("demean", False) def percentile( a, - per=kwargs.pop("per", 50.0), - limit=kwargs.pop("limit", ()), - demean=kwargs.pop("demean", False), + per=per, + limit=limit, + demean=demean, interpolation_method="fraction", ): if len(a) == 0: From acf55ba870de02ad5ebd7f66bd449acc8bb1fd44 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 20 Nov 2026 10:45:57 -0700 Subject: [PATCH 07/10] list auto/bulk formatting/linting in git blame ignore file --- .git-blame-ignore-revs | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .git-blame-ignore-revs diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..eee2932 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,7 @@ +# Commits to ignore in git blame (e.g. bulk formatting / lint fixes). +# Use with: git config blame.ignoreRevsFile .git-blame-ignore-revs +# GitHub also picks this up for the blame UI. + +# fixed all ruff linter and formatter complaints +2c96c560fa0abb2c84bf9d2d980d66485bdfc7f8 +17c946393cec395e825e2fb8fa7498456a9053fa From 81d2b3ad836281693b6ee61ca5395c560df64975 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 20 Nov 2026 11:06:05 -0700 Subject: [PATCH 08/10] drop support for Python 3.9 --- .github/workflows/ci.yaml | 6 +----- CHANGES | 1 + README.rst | 2 +- docs/source/installation.txt | 2 +- pyproject.toml | 3 +-- 5 files changed, 5 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 3aae551..9ecc954 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -23,10 +23,8 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macOS-latest, windows-latest] - python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"] + python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] exclude: - - os: macOS-latest - python-version: "3.9" - os: macOS-latest python-version: "3.10" - os: macOS-latest @@ -35,8 +33,6 @@ jobs: python-version: "3.12" - os: macOS-latest python-version: "3.13" - - os: windows-latest - python-version: "3.9" - os: windows-latest python-version: "3.10" - os: windows-latest diff --git a/CHANGES b/CHANGES index 82921f2..8afcbf0 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,7 @@ orbeckst * added support/testing for Python 3.13 and 3.14 +* dropped support for Python 3.9 2024-09-18 1.3.1 hsk17, orbeckst diff --git a/README.rst b/README.rst index 269bf84..1f5c1f7 100644 --- a/README.rst +++ b/README.rst @@ -34,7 +34,7 @@ https://github.com/Becksteinlab/numkit :: Python support ============== -Python 3 support for releases 3.9 - 3.14 is tested on Linux, macOS, +Python 3 support for releases 3.10 - 3.14 is tested on Linux, macOS, and Windows. (Python 2.7 and older version of Python 3 were supported up to release 1.2.3.) diff --git a/docs/source/installation.txt b/docs/source/installation.txt index 720dbc9..0a97f9a 100644 --- a/docs/source/installation.txt +++ b/docs/source/installation.txt @@ -12,7 +12,7 @@ Install with :program:`pip` from the `numkit PyPi repository`_ with :: pip install numkit -Python 3.9 - 3.14 are supported. +Python 3.10 - 3.14 are supported. .. _numkit PyPi repository: https://pypi.org/project/numkit/ diff --git a/pyproject.toml b/pyproject.toml index d4efd0f..e089128 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,6 @@ classifiers = [ "Operating System :: MacOS :: MacOS X", "Operating System :: Microsoft :: Windows", "Programming Language :: Python", - "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", @@ -29,7 +28,7 @@ classifiers = [ "Topic :: Scientific/Engineering", "Topic :: Software Development :: Libraries :: Python Modules" ] -requires-python = ">=3.9" +requires-python = ">=3.10" dependencies = [ "numpy>=1.9", From 47b902b85d07bc7472ae72dab3a966e541fd2891 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 20 Nov 2026 10:49:01 -0700 Subject: [PATCH 09/10] more ruff autoformatting --- docs/source/conf.py | 6 +----- numkit/tests/test_observables.py | 1 - numkit/timeseries.py | 8 ++++++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 3711220..30aa1ce 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # GromacsWrapper documentation build configuration file, created by # sphinx-quickstart on Tue Jun 23 19:38:56 2009. @@ -11,14 +10,11 @@ # All configuration values have a default value; values that are commented out # serve to show the default value. -import sys -import os import datetime # https://sphinx-rtd-theme.readthedocs.io/en/stable/ import sphinx_rtd_theme - # If your extensions are in another directory, add it here. If the directory # is relative to the documentation root, use os.path.abspath to make it # absolute, like shown here. @@ -52,7 +48,7 @@ # General substitutions. project = "numkit" now = datetime.datetime.now() -copyright = "2009-{}, The Authors of numkit (see AUTHORS)".format(now.year) +copyright = f"2009-{now.year}, The Authors of numkit (see AUTHORS)" # The default replacements for |version| and |release|, also used in various # other places throughout the built documents. diff --git a/numkit/tests/test_observables.py b/numkit/tests/test_observables.py index cea5a8f..6e33245 100644 --- a/numkit/tests/test_observables.py +++ b/numkit/tests/test_observables.py @@ -10,7 +10,6 @@ """ - import pytest from numpy.testing import assert_almost_equal diff --git a/numkit/timeseries.py b/numkit/timeseries.py index f2a2dab..81ebcc0 100644 --- a/numkit/timeseries.py +++ b/numkit/timeseries.py @@ -12,6 +12,8 @@ logger = logging.getLogger("numkit.timeseries") +import itertools + from numkit import LowAccuracyWarning @@ -81,7 +83,9 @@ def autocorrelation_fft( origin = int(ac.shape[0] / 2) # should work for both odd and even len(series) ac = ac[origin:] # only use second half of the symmetric acf - assert len(ac) <= len(series), f"Oops: len(ac)={len(ac):d} len(series)={len(series):d}" + assert len(ac) <= len(series), ( + f"Oops: len(ac)={len(ac):d} len(series)={len(series):d}" + ) if paddingcorrection and kwargs["mode"] != "valid": # 'valid' was not 0-padded # correct for 0 padding # XXX: reference? Where did I get this from? (But it makes sense.) @@ -564,5 +568,5 @@ def regularized_function(x, y, func, bins=100, range=None): # general way to combine the chunks for different blocks, just think of # func=median F = numpy.zeros(len(bins) - 1) # final function - F[:] = [func(sy[start:stop]) for start, stop in zip(bin_index[:-1], bin_index[1:])] + F[:] = [func(sy[start:stop]) for start, stop in itertools.pairwise(bin_index)] return F, bins From 8e0c055f4ee54f3f717ec1548d1588ff41e872a5 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 20 Nov 2026 11:12:44 -0700 Subject: [PATCH 10/10] add auto formatted commit to git ignore blame --- .git-blame-ignore-revs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index eee2932..4b03a62 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -5,3 +5,5 @@ # fixed all ruff linter and formatter complaints 2c96c560fa0abb2c84bf9d2d980d66485bdfc7f8 17c946393cec395e825e2fb8fa7498456a9053fa +47b902b85d07bc7472ae72dab3a966e541fd2891 +