From b04cf13757c9ec6f45745258ae302e6527478fa4 Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sun, 1 Sep 2024 11:25:07 -0400 Subject: [PATCH 01/16] update python deps and ci --- .github/workflows/tox_run.yml | 36 ++++++++--------------------------- setup.cfg | 32 ++++++++++++++++++------------- setup.py | 7 ++++--- 3 files changed, 31 insertions(+), 44 deletions(-) diff --git a/.github/workflows/tox_run.yml b/.github/workflows/tox_run.yml index 3e198ae9..806e127b 100644 --- a/.github/workflows/tox_run.yml +++ b/.github/workflows/tox_run.yml @@ -19,7 +19,7 @@ jobs: strategy: max-parallel: 3 matrix: - python-version: [3.7, 3.8, 3.9] + python-version: [3.9, "3.10", 3.11] fail-fast: false steps: @@ -40,7 +40,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.7"] + python-version: ["3.10"] fail-fast: false steps: @@ -57,31 +57,11 @@ jobs: run: tox -e coverage - py310: - runs-on: ubuntu-latest - strategy: - matrix: - python-version: ["3.10"] - fail-fast: false - - steps: - - uses: actions/checkout@v4 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install tox - - name: Test with tox - run: tox -e py310 - - py311: + py312: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.11"] + python-version: ["3.12"] fail-fast: false steps: @@ -95,13 +75,13 @@ jobs: python -m pip install --upgrade pip pip install tox - name: Test with tox - run: tox -e py311 + run: tox -e py312 minimum_numpy: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.7"] + python-version: ["3.9"] fail-fast: false steps: @@ -121,10 +101,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: Set up Python 3.9 + - name: Set up Python 3.11 uses: actions/setup-python@v5 with: - python-version: 3.9 + python-version: 3.11 - name: Install dependencies run: | python -m pip install --upgrade pip diff --git a/setup.cfg b/setup.cfg index 1039c9f5..934f39e3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -31,15 +31,14 @@ skip = *.po,*.ts,**/_version.py,docs/source/generated/* ignore-words-list = ND,nd,nin,dout [tox:tox] -envlist = py37,py38,py39,,format,min_numpy +envlist = py39,py310,py311,py312,format,min_numpy [gh-actions] python = - 3.7: py37 - 3.8: py38 3.9: py39 3.10: py310 3.11: py311 + 3.12: py312 [testenv] deps = @@ -55,12 +54,24 @@ extras = rnn [testenv:min_numpy] deps = numpy==1.20 {[testenv]deps} -basepython = python3.7 +basepython = python3.9 commands = pytest -n auto --hypothesis-profile ci \ {posargs} extras = -[testenv:py310] # exclude numba dependency for now +[testenv:py310] +deps = {[testenv]deps} +commands = pytest -n auto --hypothesis-profile ci \ + {posargs} +extras = + +[testenv:py311] +deps = {[testenv]deps} +commands = pytest -n auto --hypothesis-profile ci \ + {posargs} +extras = + +[testenv:py312] # exclude numba dependency for now deps = pytest pytest-xdist hypothesis @@ -69,17 +80,12 @@ commands = pytest -n auto --hypothesis-profile ci \ {posargs} extras = -[testenv:py311] # exclude numba dependency for now -deps = {[testenv:py310]deps} -commands = pytest -n auto --hypothesis-profile ci \ - {posargs} -extras = [testenv:coverage] setenv = NUMBA_DISABLE_JIT=1 MYGRAD_COVERAGE_MODE=1 usedevelop = true -basepython = python3.7 +basepython = python3.10 deps = {[testenv]deps} coverage pytest-cov @@ -103,7 +109,7 @@ deps = pytest hypothesis scipy pytest-xdist -basepython = python3.8 +basepython = python3.11 commands = pytest -n auto --hypothesis-profile ci \ {posargs} extras = @@ -111,7 +117,7 @@ extras = [testenv:enforce-format] skip_install=true -basepython=python3.9 +basepython=python3.11 deps=black isort flake8 diff --git a/setup.py b/setup.py index d83a37a5..07af3363 100644 --- a/setup.py +++ b/setup.py @@ -14,9 +14,10 @@ "Intended Audience :: Education", "Programming Language :: Python", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", "Topic :: Scientific/Engineering", ] @@ -56,7 +57,7 @@ }, url=URL, download_url="https://github.com/rsokl/mygrad/tarball/v" + versioneer.get_version(), - python_requires=">=3.7", + python_requires=">=3.9", packages=find_packages(where="src", exclude=["tests", "tests.*"]), package_dir={"": "src"}, ) From e3120fa15be0d1a1ea3ab1ea828e0eb7d7c3f68f Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 10:05:26 -0400 Subject: [PATCH 02/16] numpy 2.0 compat --- src/mygrad/_numpy_version.py | 3 +++ src/mygrad/linalg/funcs.py | 6 +++++- src/mygrad/math/consts.py | 19 +------------------ src/mygrad/math/misc/funcs.py | 5 +++-- src/mygrad/operation_base.py | 4 ++-- src/mygrad/tensor_base.py | 20 +++++++++++++++++--- src/mygrad/typing/_array_like.py | 6 ++++-- tests/math/test_constants.py | 8 -------- tests/tensor_base/test_tensor.py | 5 ++++- tests/test_indexing_routines.py | 11 +++++++---- tests/test_numpy_overrides.py | 1 - 11 files changed, 46 insertions(+), 42 deletions(-) create mode 100644 src/mygrad/_numpy_version.py diff --git a/src/mygrad/_numpy_version.py b/src/mygrad/_numpy_version.py new file mode 100644 index 00000000..b4061981 --- /dev/null +++ b/src/mygrad/_numpy_version.py @@ -0,0 +1,3 @@ +import numpy + +NP_IS_V2 = numpy.version.version.startswith("2.") diff --git a/src/mygrad/linalg/funcs.py b/src/mygrad/linalg/funcs.py index 08b9598a..030b7904 100644 --- a/src/mygrad/linalg/funcs.py +++ b/src/mygrad/linalg/funcs.py @@ -2,7 +2,11 @@ from typing import Optional, Sequence, Tuple, Union import numpy as np -from numpy.core.einsumfunc import _parse_einsum_input + +try: + from numpy._core.einsumfunc import _parse_einsum_input +except ImportError: + from numpy.core.einsumfunc import _parse_einsum_input from mygrad.math.misc.funcs import absolute from mygrad.math.sequential.funcs import max as mg_max, min as mg_min diff --git a/src/mygrad/math/consts.py b/src/mygrad/math/consts.py index 92307d76..c3edccd8 100644 --- a/src/mygrad/math/consts.py +++ b/src/mygrad/math/consts.py @@ -1,35 +1,18 @@ import numpy as np __all__ = [ - "Inf", - "Infinity", - "NAN", - "NINF", - "NZERO", - "NaN", - "PINF", - "PZERO", "e", "euler_gamma", "inf", - "infty", "nan", "newaxis", "pi", ] -Inf = np.Inf -Infinity = np.Infinity -NAN = np.NAN -NINF = np.NINF -NZERO = np.NZERO -NaN = np.NaN -PINF = np.PINF -PZERO = np.PZERO + e = np.e euler_gamma = np.euler_gamma inf = np.inf -infty = np.infty nan = np.nan newaxis = np.newaxis pi = float(np.pi) diff --git a/src/mygrad/math/misc/funcs.py b/src/mygrad/math/misc/funcs.py index 85750e25..0fa6ae8b 100644 --- a/src/mygrad/math/misc/funcs.py +++ b/src/mygrad/math/misc/funcs.py @@ -4,6 +4,7 @@ from numpy import ndarray import mygrad as mg +from mygrad._numpy_version import NP_IS_V2 from mygrad.math.misc.ops import MatMul from mygrad.tensor_base import Tensor, implements_numpy_override from mygrad.typing import ArrayLike, DTypeLikeReals, Mask @@ -501,7 +502,7 @@ def clip( Tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> mg.clip(a, [3, 4, 1, 1, 1, 4, 4, 4, 4, 4], 8) Tensor([3, 4, 2, 3, 4, 5, 6, 7, 8, 8])""" - if a_min is None and a_max is None: + if not NP_IS_V2 and a_min is None and a_max is None: raise ValueError("`a_min` and `a_max` cannot both be set to `None`") if a_min is not None: @@ -509,7 +510,7 @@ def clip( if a_max is not None: a = minimum(a_max, a, out=out, constant=constant) - return a + return mg.astensor(a) @ufunc_creator(MatMul) diff --git a/src/mygrad/operation_base.py b/src/mygrad/operation_base.py index ba4cc81d..28a2dce0 100644 --- a/src/mygrad/operation_base.py +++ b/src/mygrad/operation_base.py @@ -96,7 +96,7 @@ def grad_post_process_fn( if out.ndim == 0: # sum-reduction to a scalar produces a float - out = np.array(out, copy=False) + out = np.asarray(out) return out @abstractmethod @@ -197,7 +197,7 @@ def backward( f"numpy arrays, got a gradient of type: {type(backed_grad)}" ) - backed_grad = np.array(backed_grad, copy=False) + backed_grad = np.asarray(backed_grad) if self.where is not True: backed_grad = backed_grad * self.where diff --git a/src/mygrad/tensor_base.py b/src/mygrad/tensor_base.py index a5d2565a..e06095f3 100644 --- a/src/mygrad/tensor_base.py +++ b/src/mygrad/tensor_base.py @@ -3,6 +3,7 @@ attributes and special methods. Public math methods, e.g. ``sum``, ``mean``, etc., are bound to the Tensor class in ``mygrad.__init__.py``. """ + from collections import deque from numbers import Integral, Number from typing import ( @@ -734,8 +735,10 @@ def __array_function__( else: # pragma: no cover return NotImplemented - def __array__(self, dtype: DTypeLike = None) -> np.ndarray: - return np.array(self.data, dtype=dtype, copy=False) + def __array__( + self, dtype: DTypeLike = None, copy: Optional[bool] = None + ) -> np.ndarray: + return np.asarray(self.data, dtype=dtype, copy=copy) def __init__( self, @@ -795,7 +798,18 @@ def __init__( self._creator: Optional[Operation] = _creator - self.data = np.array(x, dtype=dtype, copy=copy, ndmin=ndmin) # type: np.ndarray + if copy is False: + self.data = np.asarray(x, dtype=dtype) # type: np.ndarray + if not isinstance(ndmin, Integral): + raise TypeError( + f"'{type(ndmin)}' object cannot be interpreted as an integer" + ) + if ndmin and self.data.ndim < ndmin: + self.data = self.data[(*(None for _ in range(ndmin - self.data.ndim)),)] + else: + self.data = np.array( + x, dtype=dtype, copy=copy, ndmin=ndmin + ) # type: np.ndarray dtype = self.data.dtype.type is_float = issubclass(dtype, np.floating) # faster than `numpy.issubdtype` diff --git a/src/mygrad/typing/_array_like.py b/src/mygrad/typing/_array_like.py index e3819e49..dc656293 100644 --- a/src/mygrad/typing/_array_like.py +++ b/src/mygrad/typing/_array_like.py @@ -1,5 +1,5 @@ import sys -from typing import TYPE_CHECKING, List, Sequence, Tuple, TypeVar, Union +from typing import TYPE_CHECKING, List, Optional, Sequence, Tuple, TypeVar, Union import numpy as np from typing_extensions import runtime_checkable @@ -39,7 +39,9 @@ def __array__(self, dtype: None = ...) -> np.ndarray: @runtime_checkable class ImplementsArray(Protocol): - def __array__(self, dtype: None = ...) -> np.ndarray: + def __array__( + self, dtype: None = ..., copy: Optional[bool] = ... + ) -> np.ndarray: ... diff --git a/tests/math/test_constants.py b/tests/math/test_constants.py index 765f1314..5a5a74a2 100644 --- a/tests/math/test_constants.py +++ b/tests/math/test_constants.py @@ -9,17 +9,9 @@ @pytest.mark.parametrize( ("mygrad_constant", "numpy_constant"), [ - (mg.Infinity, np.Infinity), - (mg.NAN, np.NAN), - (mg.NINF, np.NINF), - (mg.NZERO, np.NZERO), - (mg.NaN, np.NaN), - (mg.PINF, np.PINF), - (mg.PZERO, np.PZERO), (mg.e, np.e), (mg.euler_gamma, np.euler_gamma), (mg.inf, np.inf), - (mg.infty, np.infty), (mg.nan, np.nan), (mg.newaxis, np.newaxis), ], diff --git a/tests/tensor_base/test_tensor.py b/tests/tensor_base/test_tensor.py index 9e8a8794..a3a6bc97 100644 --- a/tests/tensor_base/test_tensor.py +++ b/tests/tensor_base/test_tensor.py @@ -57,7 +57,10 @@ def test_input_constant_checking(constant): def test_ndmin(x: np.ndarray, copy: bool, dtype, ndmin: int): """Ensure Tensor(..., ndmin=) mirrors numpy behavior and produces appropriate view behavior""" - arr = np.array(x, copy=copy, ndmin=ndmin, dtype=dtype) + try: + arr = np.array(x, copy=copy, ndmin=ndmin, dtype=dtype) + except ValueError: + assume(False) tensor = Tensor(x, copy=copy, ndmin=ndmin, dtype=dtype) assert_equal(tensor, arr) diff --git a/tests/test_indexing_routines.py b/tests/test_indexing_routines.py index 81bab6f4..cd4912c3 100644 --- a/tests/test_indexing_routines.py +++ b/tests/test_indexing_routines.py @@ -2,7 +2,7 @@ import hypothesis.strategies as st import numpy as np import pytest -from hypothesis import given +from hypothesis import assume, given import mygrad as mg from mygrad import where @@ -50,9 +50,12 @@ def test_where_condition_only_fwd(condition): tensor_condition = ( mg.Tensor(condition) if isinstance(condition, np.ndarray) else condition ) - assert all( - np.all(x == y) for x, y in zip(np.where(tensor_condition), np.where(condition)) - ) + try: + c = np.where(condition) + except ValueError: + assume(False) + tc = np.where(tensor_condition) + assert all(np.all(x == y) for x, y in zip(tc, c)) @given( diff --git a/tests/test_numpy_overrides.py b/tests/test_numpy_overrides.py index 1876ff40..d2b01877 100644 --- a/tests/test_numpy_overrides.py +++ b/tests/test_numpy_overrides.py @@ -49,7 +49,6 @@ def test_bincount(): def test_can_cast(): - assert np.can_cast(mg.tensor(1000.0), np.float32) is True assert np.can_cast(mg.tensor([1000.0]), np.float32) is False From c50b3df4339e8bf82cf886e723a140eba70976af Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 10:38:21 -0400 Subject: [PATCH 03/16] format and clip --- .pre-commit-config.yaml | 8 ++++---- setup.cfg | 2 +- src/mygrad/_utils/graph_tracking.py | 1 + src/mygrad/_utils/lock_management.py | 1 + src/mygrad/math/misc/funcs.py | 3 +-- src/mygrad/operation_base.py | 1 + src/mygrad/tensor_manip/tensor_joining/ops.py | 8 +++++--- tests/custom_strategies/__init__.py | 1 + tests/math/binary/test_binary_funcs.py | 1 + tests/nnet/layers/test_conv.py | 1 + tests/nnet/layers/test_gru.py | 8 +++++--- tests/tensor_base/test_chainrule.py | 12 ++++++------ tests/tensor_ops/test_setitem.py | 16 +++++++++------- tests/utils/test_utils.py | 1 - tests/wrappers/uber.py | 10 ++++++---- 15 files changed, 43 insertions(+), 31 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 105214c6..0b47003f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,13 +1,13 @@ repos: - repo: https://github.com/psf/black - rev: 22.10.0 + rev: 24.8.0 hooks: - id: black - repo: https://github.com/PyCQA/flake8 - rev: 6.0.0 + rev: 7.0.0 hooks: - id: flake8 -- repo: https://github.com/pre-commit/mirrors-isort - rev: v5.10.1 +- repo: https://github.com/pycqa/isort + rev: 5.13.2 hooks: - id: isort \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 934f39e3..e3ef3b8c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -52,7 +52,7 @@ extras = rnn [testenv:min_numpy] -deps = numpy==1.20 +deps = numpy==1.24 {[testenv]deps} basepython = python3.9 commands = pytest -n auto --hypothesis-profile ci \ diff --git a/src/mygrad/_utils/graph_tracking.py b/src/mygrad/_utils/graph_tracking.py index fc9491d5..c659b602 100644 --- a/src/mygrad/_utils/graph_tracking.py +++ b/src/mygrad/_utils/graph_tracking.py @@ -1,6 +1,7 @@ """ Provides user interface for suspending computational graph tracking and back-propagation """ + from functools import wraps from typing import Any, Callable, TypeVar, cast diff --git a/src/mygrad/_utils/lock_management.py b/src/mygrad/_utils/lock_management.py index ebe18874..821a71f8 100644 --- a/src/mygrad/_utils/lock_management.py +++ b/src/mygrad/_utils/lock_management.py @@ -1,6 +1,7 @@ """ Provides utilities responsible for locking/releasing array writeability. """ + import os from collections import Counter, defaultdict from typing import ( diff --git a/src/mygrad/math/misc/funcs.py b/src/mygrad/math/misc/funcs.py index 0fa6ae8b..d50c9b69 100644 --- a/src/mygrad/math/misc/funcs.py +++ b/src/mygrad/math/misc/funcs.py @@ -4,7 +4,6 @@ from numpy import ndarray import mygrad as mg -from mygrad._numpy_version import NP_IS_V2 from mygrad.math.misc.ops import MatMul from mygrad.tensor_base import Tensor, implements_numpy_override from mygrad.typing import ArrayLike, DTypeLikeReals, Mask @@ -502,7 +501,7 @@ def clip( Tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> mg.clip(a, [3, 4, 1, 1, 1, 4, 4, 4, 4, 4], 8) Tensor([3, 4, 2, 3, 4, 5, 6, 7, 8, 8])""" - if not NP_IS_V2 and a_min is None and a_max is None: + if np.__version__ < "2.1.0" and a_min is None and a_max is None: # pragma: no cover raise ValueError("`a_min` and `a_max` cannot both be set to `None`") if a_min is not None: diff --git a/src/mygrad/operation_base.py b/src/mygrad/operation_base.py index 28a2dce0..3be681d1 100644 --- a/src/mygrad/operation_base.py +++ b/src/mygrad/operation_base.py @@ -1,6 +1,7 @@ """ Defines the base class for mathematical operations capable of back-propagating gradients to their input tensors.""" + from abc import ABC, abstractmethod from numbers import Real from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Union diff --git a/src/mygrad/tensor_manip/tensor_joining/ops.py b/src/mygrad/tensor_manip/tensor_joining/ops.py index 7a9b9a40..0b6f8a75 100644 --- a/src/mygrad/tensor_manip/tensor_joining/ops.py +++ b/src/mygrad/tensor_manip/tensor_joining/ops.py @@ -52,9 +52,11 @@ def backward_var(self, grad, index, **kwargs) -> np.ndarray: return grad[ tuple( - slice(None, None, None) - if dim != self.axis - else slice(self.indices[index], self.indices[index + 1]) + ( + slice(None, None, None) + if dim != self.axis + else slice(self.indices[index], self.indices[index + 1]) + ) for dim in range(var.data.ndim) ) ] diff --git a/tests/custom_strategies/__init__.py b/tests/custom_strategies/__init__.py index ef2e7781..f91b1138 100644 --- a/tests/custom_strategies/__init__.py +++ b/tests/custom_strategies/__init__.py @@ -1,4 +1,5 @@ """ Custom hypothesis search strategies """ + import math from collections import defaultdict from functools import lru_cache, partial, reduce diff --git a/tests/math/binary/test_binary_funcs.py b/tests/math/binary/test_binary_funcs.py index ce27d96a..345637a4 100644 --- a/tests/math/binary/test_binary_funcs.py +++ b/tests/math/binary/test_binary_funcs.py @@ -1,4 +1,5 @@ """ Test all binary arithmetic operations, checks for appropriate broadcast behavior""" + from functools import partial from numbers import Number from typing import Any, Dict diff --git a/tests/nnet/layers/test_conv.py b/tests/nnet/layers/test_conv.py index 4ea0cb0a..5ad76c02 100644 --- a/tests/nnet/layers/test_conv.py +++ b/tests/nnet/layers/test_conv.py @@ -1,4 +1,5 @@ """ Test conv fwd-prop and back-prop for ND convs""" + from typing import Tuple import hypothesis.extra.numpy as hnp diff --git a/tests/nnet/layers/test_gru.py b/tests/nnet/layers/test_gru.py index 670b313f..d3661691 100644 --- a/tests/nnet/layers/test_gru.py +++ b/tests/nnet/layers/test_gru.py @@ -39,9 +39,11 @@ def test_nonconstant_s0_raises(s0, dropout: float, out_constant: bool): Uz, Ur, Uh = Tensor(np.random.rand(3, C, D)) bz, br, bh = Tensor(np.random.rand(3, D)) - with does_not_raise() if ( - out_constant or s0 is None or isinstance(s0, np.ndarray) or s0.constant - ) else pytest.raises(ValueError): + with ( + does_not_raise() + if (out_constant or s0 is None or isinstance(s0, np.ndarray) or s0.constant) + else pytest.raises(ValueError) + ): gru( X, Uz, diff --git a/tests/tensor_base/test_chainrule.py b/tests/tensor_base/test_chainrule.py index 26a935e4..7b99cdff 100644 --- a/tests/tensor_base/test_chainrule.py +++ b/tests/tensor_base/test_chainrule.py @@ -209,9 +209,11 @@ def test_linear_graph( # dL/d2 = dL/d4 * d4/d3 * d3/d2 * d2/d1 _check_grad( v1, - None - if (v4.constant or v3.constant or v2.constant) - else grad * (2 * np.exp(v2.data)) * (2 * v1.data), + ( + None + if (v4.constant or v3.constant or v2.constant) + else grad * (2 * np.exp(v2.data)) * (2 * v1.data) + ), ) # check the backprop clears graph & clear graph always propagates through the graph @@ -501,9 +503,7 @@ def test_dynamic_interesting_graph( ) _check_grad(v3, v3_grad) - v2_grad = ( - None if (v5.constant or v4.constant) else grad * v3.data**2 * v1.data**2 - ) + v2_grad = None if (v5.constant or v4.constant) else grad * v3.data**2 * v1.data**2 _check_grad(v2, v2_grad) v1_grad = None if v5.constant else grad * v4.data * v3.data diff --git a/tests/tensor_ops/test_setitem.py b/tests/tensor_ops/test_setitem.py index 8cb2ffa6..79661203 100644 --- a/tests/tensor_ops/test_setitem.py +++ b/tests/tensor_ops/test_setitem.py @@ -415,13 +415,15 @@ def test_setitem_bool_basic_index(): index_strat=lambda x: hnp.arrays(shape=(2, 3), dtype=bool).map( lambda _x: (_x[0], _x[1]) ), - value_strat=lambda o: hnp.arrays( - shape=broadcastable_shapes(o.shape, max_dims=o.ndim, max_side=max(o.shape)), - dtype=float, - elements=st.floats(-10.0, 10.0), - ) - if o.shape and o.size - else st.floats(-10.0, 10.0).map(np.asarray), + value_strat=lambda o: ( + hnp.arrays( + shape=broadcastable_shapes(o.shape, max_dims=o.ndim, max_side=max(o.shape)), + dtype=float, + elements=st.floats(-10.0, 10.0), + ) + if o.shape and o.size + else st.floats(-10.0, 10.0).map(np.asarray) + ), ) def test_setitem_bool_axes_index(): """index consists of boolean arrays specified for each axis""" diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index ec9f3025..456d26b0 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -1,6 +1,5 @@ """ Test `numerical_gradient`, `numerical_derivative`, and `broadcast_check`""" - import hypothesis.extra.numpy as hnp import hypothesis.strategies as st import numpy as np diff --git a/tests/wrappers/uber.py b/tests/wrappers/uber.py index 1d861573..72afe6d0 100644 --- a/tests/wrappers/uber.py +++ b/tests/wrappers/uber.py @@ -302,10 +302,12 @@ def wrapper(shapes: hnp.BroadcastableShapes, constant, data: st.DataObject): if self.permit_0d_array_as_float: # potentially cast a 0D array as a float arrs = tuple( - arr.item() - if arr.ndim == 0 - and data.draw(st.booleans(), label=f"arr-{n} to float") - else arr + ( + arr.item() + if arr.ndim == 0 + and data.draw(st.booleans(), label=f"arr-{n} to float") + else arr + ) for n, arr in enumerate(arrs) ) From 6f70e82816ea060f49c5db31a25e9524319e8abd Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 12:19:25 -0400 Subject: [PATCH 04/16] migrate to pyproject --- MANIFEST.in | 5 +- pyproject.toml | 183 +++ setup.py | 63 - src/mygrad/__init__.py | 25 +- src/mygrad/_version.py | 556 -------- src/mygrad/py.typed | 0 tests/tensor_ops/test_sequence_ops.py | 2 +- versioneer.py | 1886 ------------------------- 8 files changed, 203 insertions(+), 2517 deletions(-) create mode 100644 pyproject.toml delete mode 100644 setup.py delete mode 100644 src/mygrad/_version.py create mode 100644 src/mygrad/py.typed delete mode 100644 versioneer.py diff --git a/MANIFEST.in b/MANIFEST.in index 3f2582a1..2b6e5aad 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,3 @@ -include versioneer.py -include mygrad/_version.py +recursive-exclude tests * +recursive-exclude project_tooling * +global-exclude *.py[cod] diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..f149af2b --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,183 @@ +[build-system] +requires = [ + "setuptools >= 35.0.2", + "wheel >= 0.29.0", + "setuptools_scm[toml]==7.0.5", +] +build-backend = "setuptools.build_meta" + +[project] +name = "mygrad" +dynamic = ["version"] +description = "Drop-in automatic differentiation to NumPy" +readme = "README.md" +requires-python = ">=3.9" +dependencies = ["numpy >= 1.24, !=1.25.0", "typing-extensions >= 4.1.0, !=4.6.0"] +license = { text = "MIT" } + +authors = [ + { name = "Ryan Soklaski", email = "rsoklaski@gmail.com" }, +] +maintainers = [{ name = "Ryan Soklaski", email = "rsoklaski@gmail.com" }] + +classifiers = [ + "Development Status :: 5 - Production/Stable", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Intended Audience :: Science/Research", + "Intended Audience :: Education", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Topic :: Scientific/Engineering", +] + + +[project.optional-dependencies] +test = ["pytest >= 3.8", "hypothesis >= 6.17.1", "scipy"] +rnn = ["numba>=0.34.0"] + + + +[project.urls] +"Homepage" = "https://mygrad.readthedocs.io/en/latest/" +"Bug Reports" = "https://github.com/rsokl/MyGrad/issues" +"Source" = "https://github.com/rsokl/MyGrad" + + +[tool.setuptools_scm] +write_to = "src/mygrad/_version.py" +version_scheme = "no-guess-dev" + + +[tool.setuptools] +package-dir = { "" = "src" } + +[tool.setuptools.packages.find] +where = ["src"] +exclude = ["tests*", "tests.*"] + +[tool.setuptools.package-data] +mygrad = ["py.typed"] + + +[tool.isort] +known_third_party = ["graphviz", "hypothesis", "numpy", "numba", "pytest", "scipy"] +known_first_party = ["mygrad", "tests"] +profile = "black" +src_paths=["src/mygrad","tests"] + +[tool.coverage.run] +omit = ["src/mygrad/_version.py", "src/mygrad/computational_graph.py"] + +[tool.coverage.report] +omit = ["src/mygrad/_version.py"] + +[tool.codespell] +skip = ["*.po","*.ts","**/_version.py","docs/source/generated/*"] +ignore-words-list = ["ND","nd","nin","dout"] + +[tool.tox] +legacy_tox_ini = """ +[tox] +envlist = py39,py310,py311,py312,format,min_numpy + +[gh-actions] +python = + 3.9: py39 + 3.10: py310 + 3.11: py311 + 3.12: py312 + +[testenv] +deps = + pytest + hypothesis + scipy + pytest-xdist +commands = pytest -n auto --hypothesis-profile ci \ + {posargs} +extras = rnn + + +[testenv:min_numpy] +deps = numpy==1.24 + {[testenv]deps} +basepython = python3.9 +commands = pytest -n auto --hypothesis-profile ci \ + {posargs} +extras = + +[testenv:py310] +deps = {[testenv]deps} +commands = pytest -n auto --hypothesis-profile ci \ + {posargs} +extras = + +[testenv:py311] +deps = {[testenv]deps} +commands = pytest -n auto --hypothesis-profile ci \ + {posargs} +extras = + +[testenv:py312] # exclude numba dependency for now +deps = pytest + pytest-xdist + hypothesis + scipy +commands = pytest -n auto --hypothesis-profile ci \ + {posargs} +extras = + + +[testenv:coverage] +setenv = NUMBA_DISABLE_JIT=1 + MYGRAD_COVERAGE_MODE=1 +usedevelop = true +basepython = python3.10 +deps = {[testenv]deps} + coverage + pytest-cov +commands = pytest -n auto --cov-report term-missing --cov-config=setup.cfg --cov-fail-under=100 --cov=mygrad tests + + +[testenv:format] +deps = + autoflake + black + isort +commands = + autoflake --recursive --in-place --remove-duplicate-keys --remove-unused-variables . + isort . + black . + + +[testenv:pre-release] # test against pre-releases of dependencies +pip_pre = true +deps = pytest + hypothesis + scipy + pytest-xdist +basepython = python3.11 +commands = pytest -n auto --hypothesis-profile ci \ + {posargs} +extras = + + +[testenv:enforce-format] +skip_install=true +basepython=python3.11 +deps=black + isort + flake8 + pytest + codespell +commands= + black src/ tests/ --diff --check + isort src/ tests/ --diff --check + flake8 src/ tests/ + codespell src/ docs/ +""" \ No newline at end of file diff --git a/setup.py b/setup.py deleted file mode 100644 index 07af3363..00000000 --- a/setup.py +++ /dev/null @@ -1,63 +0,0 @@ -import versioneer -from setuptools import find_packages, setup - -DISTNAME = "mygrad" -LICENSE = "MIT" -AUTHOR = "Ryan Soklaski" -AUTHOR_EMAIL = "rsoklaski@gmail.com" -URL = "https://github.com/rsokl/MyGrad" -CLASSIFIERS = [ - "Development Status :: 5 - Production/Stable", - "License :: OSI Approved :: MIT License", - "Operating System :: OS Independent", - "Intended Audience :: Science/Research", - "Intended Audience :: Education", - "Programming Language :: Python", - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", - "Programming Language :: Python :: 3.12", - "Topic :: Scientific/Engineering", -] - -# tests/tensor_ops/test_getitem.py::test_getitem_advindex_bool_bkwdprop segfaults -INSTALL_REQUIRES = ["numpy >= 1.20, !=1.25.0", "typing-extensions >= 4.1.0, !=4.6.0"] -TESTS_REQUIRE = ["pytest >= 3.8", "hypothesis >= 6.17.1", "scipy"] - -DESCRIPTION = "Brings drop-in automatic differentiation to NumPy" -LONG_DESCRIPTION = """ -MyGrad is a lightweight library that adds automatic differentiation to NumPy – its only dependency is NumPy! -It's primary goal is to make automatic differentiation an accessible and easy to use across the Python/NumPy ecosystem. - -MyGrad introduces a tensor object, which behaves like NumPy's ndarray object, but that builds a computational -graph, which enables MyGrad to perform reverse-mode differentiation (i.e. "backpropagation"). By exploiting -NumPy's mechanisms for ufunc/function overrides, MyGrad's tensor works "natively" with NumPy's suite of mathematical -functions so that they can be chained together into a differentiable computational graph. - -NumPy's systems for broadcasting operations, producing views of arrays, performing in-place operations, and permitting -both "basic" and "advanced" indexing of arrays are all supported by MyGrad to a high-fidelity. -""" - - -setup( - name=DISTNAME, - version=versioneer.get_version(), - cmdclass=versioneer.get_cmdclass(), - license=LICENSE, - author=AUTHOR, - author_email=AUTHOR_EMAIL, - classifiers=CLASSIFIERS, - description=DESCRIPTION, - long_description=LONG_DESCRIPTION, - install_requires=INSTALL_REQUIRES, - tests_require=TESTS_REQUIRE, - extras_require={ - "rnn": ["numba>=0.34.0"] # GRU and vanilla RNN require numba-acceleration - }, - url=URL, - download_url="https://github.com/rsokl/mygrad/tarball/v" + versioneer.get_version(), - python_requires=">=3.9", - packages=find_packages(where="src", exclude=["tests", "tests.*"]), - package_dir={"": "src"}, -) diff --git a/src/mygrad/__init__.py b/src/mygrad/__init__.py index 5e9bd813..371b4335 100644 --- a/src/mygrad/__init__.py +++ b/src/mygrad/__init__.py @@ -1,9 +1,5 @@ -from mygrad.tensor_base import ( # isort:skip # avoid an import cycle - asarray, - astensor, - tensor, - Tensor, -) +from typing import TYPE_CHECKING + from mygrad._dtype_mirrors import * from mygrad._utils.graph_tracking import no_autodiff from mygrad._utils.lock_management import ( @@ -35,11 +31,22 @@ from . import random from ._io import load, save -from ._version import get_versions -__version__ = get_versions()["version"] -del get_versions +from mygrad.tensor_base import ( # isort:skip # avoid an import cycle + asarray, + astensor, + tensor, + Tensor, +) + +if not TYPE_CHECKING: + try: + from ._version import version as __version__ + except ImportError: + __version__ = "unknown version" +else: # pragma: no cover + __version__: str setattr(Tensor, "clip", clip) execute_op = Tensor._op diff --git a/src/mygrad/_version.py b/src/mygrad/_version.py deleted file mode 100644 index 24f63b53..00000000 --- a/src/mygrad/_version.py +++ /dev/null @@ -1,556 +0,0 @@ -# This file helps to compute a version number in source trees obtained from -# git-archive tarball (such as those provided by githubs download-from-tag -# feature). Distribution tarballs (built by setup.py sdist) and build -# directories (produced by setup.py build) will contain a much shorter file -# that just contains the computed version number. - -# This file is released into the public domain. Generated by -# versioneer-0.18 (https://github.com/warner/python-versioneer) - -"""Git implementation of _version.py.""" - -import errno -import os -import re -import subprocess -import sys - - -def get_keywords(): - """Get the keywords needed to look up the version information.""" - # these strings will be replaced by git during git-archive. - # setup.py/versioneer.py will grep for the variable names, so they must - # each be defined on a line of their own. _version.py will just call - # get_keywords(). - git_refnames = "$Format:%d$" - git_full = "$Format:%H$" - git_date = "$Format:%ci$" - keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} - return keywords - - -class VersioneerConfig: - """Container for Versioneer configuration parameters.""" - - -def get_config(): - """Create, populate and return the VersioneerConfig() object.""" - # these strings are filled in when 'setup.py versioneer' creates - # _version.py - cfg = VersioneerConfig() - cfg.VCS = "git" - cfg.style = "pep440" - cfg.tag_prefix = "v" - cfg.parentdir_prefix = "mygrad-" - cfg.versionfile_source = "mygrad/_version.py" - cfg.verbose = False - return cfg - - -class NotThisMethod(Exception): - """Exception raised if a method is not valid for the current scenario.""" - - -LONG_VERSION_PY = {} -HANDLERS = {} - - -def register_vcs_handler(vcs, method): # decorator - """Decorator to mark a method as the handler for a particular VCS.""" - - def decorate(f): - """Store f in HANDLERS[vcs][method].""" - if vcs not in HANDLERS: - HANDLERS[vcs] = {} - HANDLERS[vcs][method] = f - return f - - return decorate - - -def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=None): - """Call the given command(s).""" - assert isinstance(commands, list) - p = None - for c in commands: - try: - dispcmd = str([c] + args) - # remember shell=False, so use git.cmd on windows, not just git - p = subprocess.Popen( - [c] + args, - cwd=cwd, - env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr else None), - ) - break - except EnvironmentError: - e = sys.exc_info()[1] - if e.errno == errno.ENOENT: - continue - if verbose: - print("unable to run %s" % dispcmd) - print(e) - return None, None - else: - if verbose: - print("unable to find command, tried {}".format(commands)) - return None, None - stdout = p.communicate()[0].strip() - if sys.version_info[0] >= 3: - stdout = stdout.decode() - if p.returncode != 0: - if verbose: - print("unable to run %s (error)" % dispcmd) - print("stdout was %s" % stdout) - return None, p.returncode - return stdout, p.returncode - - -def versions_from_parentdir(parentdir_prefix, root, verbose): - """Try to determine the version from the parent directory name. - - Source tarballs conventionally unpack into a directory that includes both - the project name and a version string. We will also support searching up - two directory levels for an appropriately named parent directory - """ - rootdirs = [] - - for i in range(3): - dirname = os.path.basename(root) - if dirname.startswith(parentdir_prefix): - return { - "version": dirname[len(parentdir_prefix) :], - "full-revisionid": None, - "dirty": False, - "error": None, - "date": None, - } - else: - rootdirs.append(root) - root = os.path.dirname(root) # up a level - - if verbose: - print( - "Tried directories %s but none started with prefix %s" - % (rootdirs, parentdir_prefix) - ) - raise NotThisMethod("rootdir doesn't start with parentdir_prefix") - - -@register_vcs_handler("git", "get_keywords") -def git_get_keywords(versionfile_abs): - """Extract version information from the given file.""" - # the code embedded in _version.py can just fetch the value of these - # keywords. When used from setup.py, we don't want to import _version.py, - # so we do it with a regexp instead. This function is not used from - # _version.py. - keywords = {} - try: - f = open(versionfile_abs, "r") - for line in f.readlines(): - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - f.close() - except EnvironmentError: - pass - return keywords - - -@register_vcs_handler("git", "keywords") -def git_versions_from_keywords(keywords, tag_prefix, verbose): - """Get version information from git keywords.""" - if not keywords: - raise NotThisMethod("no keywords at all, weird") - date = keywords.get("date") - if date is not None: - # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant - # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 - # -like" string, which we must then edit to make compliant), because - # it's been around since git-1.5.3, and it's too difficult to - # discover which version we're using, or to work around using an - # older one. - date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - refnames = keywords["refnames"].strip() - if refnames.startswith("$Format"): - if verbose: - print("keywords are unexpanded, not using") - raise NotThisMethod("unexpanded keywords, not a git-archive tarball") - refs = {r.strip() for r in refnames.strip("()").split(",")} - # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of - # just "foo-1.0". If we see a "tag: " prefix, prefer those. - TAG = "tag: " - tags = {r[len(TAG) :] for r in refs if r.startswith(TAG)} - if not tags: - # Either we're using git < 1.8.3, or there really are no tags. We use - # a heuristic: assume all version tags have a digit. The old git %d - # expansion behaves like git log --decorate=short and strips out the - # refs/heads/ and refs/tags/ prefixes that would let us distinguish - # between branches and tags. By ignoring refnames without digits, we - # filter out many common branch names like "release" and - # "stabilization", as well as "HEAD" and "master". - tags = {r for r in refs if re.search(r"\d", r)} - if verbose: - print("discarding '%s', no digits" % ",".join(refs - tags)) - if verbose: - print("likely tags: %s" % ",".join(sorted(tags))) - for ref in sorted(tags): - # sorting will prefer e.g. "2.0" over "2.0rc1" - if ref.startswith(tag_prefix): - r = ref[len(tag_prefix) :] - if verbose: - print("picking %s" % r) - return { - "version": r, - "full-revisionid": keywords["full"].strip(), - "dirty": False, - "error": None, - "date": date, - } - # no suitable tags, so version is "0+unknown", but full hex is still there - if verbose: - print("no suitable tags, using unknown + full revision id") - return { - "version": "0+unknown", - "full-revisionid": keywords["full"].strip(), - "dirty": False, - "error": "no suitable tags", - "date": None, - } - - -@register_vcs_handler("git", "pieces_from_vcs") -def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): - """Get version from 'git describe' in the root of the source tree. - - This only gets called if the git-archive 'subst' keywords were *not* - expanded, and _version.py hasn't already been rewritten with a short - version string, meaning we're inside a checked out source tree. - """ - GITS = ["git"] - if sys.platform == "win32": - GITS = ["git.cmd", "git.exe"] - - out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True) - if rc != 0: - if verbose: - print("Directory %s not under git control" % root) - raise NotThisMethod("'git rev-parse --git-dir' returned error") - - # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] - # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = run_command( - GITS, - [ - "describe", - "--tags", - "--dirty", - "--always", - "--long", - "--match", - "%s*" % tag_prefix, - ], - cwd=root, - ) - # --long was added in git-1.5.5 - if describe_out is None: - raise NotThisMethod("'git describe' failed") - describe_out = describe_out.strip() - full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) - if full_out is None: - raise NotThisMethod("'git rev-parse' failed") - full_out = full_out.strip() - - pieces = {} - pieces["long"] = full_out - pieces["short"] = full_out[:7] # maybe improved later - pieces["error"] = None - - # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] - # TAG might have hyphens. - git_describe = describe_out - - # look for -dirty suffix - dirty = git_describe.endswith("-dirty") - pieces["dirty"] = dirty - if dirty: - git_describe = git_describe[: git_describe.rindex("-dirty")] - - # now we have TAG-NUM-gHEX or HEX - - if "-" in git_describe: - # TAG-NUM-gHEX - mo = re.search(r"^(.+)-(\d+)-g([0-9a-f]+)$", git_describe) - if not mo: - # unparseable. Maybe git-describe is misbehaving? - pieces["error"] = "unable to parse git-describe output: '%s'" % describe_out - return pieces - - # tag - full_tag = mo.group(1) - if not full_tag.startswith(tag_prefix): - if verbose: - fmt = "tag '%s' doesn't start with prefix '%s'" - print(fmt % (full_tag, tag_prefix)) - pieces["error"] = "tag '%s' doesn't start with prefix '%s'" % ( - full_tag, - tag_prefix, - ) - return pieces - pieces["closest-tag"] = full_tag[len(tag_prefix) :] - - # distance: number of commits since tag - pieces["distance"] = int(mo.group(2)) - - # commit: short hex revision ID - pieces["short"] = mo.group(3) - - else: - # HEX: no tags - pieces["closest-tag"] = None - count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], cwd=root) - pieces["distance"] = int(count_out) # total number of commits - - # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[ - 0 - ].strip() - pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - - return pieces - - -def plus_or_dot(pieces): - """Return a + if we don't already have one, else return a .""" - if "+" in pieces.get("closest-tag", ""): - return "." - return "+" - - -def render_pep440(pieces): - """Build up version string, with post-release "local version identifier". - - Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you - get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty - - Exceptions: - 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += plus_or_dot(pieces) - rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0+untagged.%d.g%s" % (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def render_pep440_pre(pieces): - """TAG[.post.devDISTANCE] -- No -dirty. - - Exceptions: - 1: no tags. 0.post.devDISTANCE - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += ".post.dev%d" % pieces["distance"] - else: - # exception #1 - rendered = "0.post.dev%d" % pieces["distance"] - return rendered - - -def render_pep440_post(pieces): - """TAG[.postDISTANCE[.dev0]+gHEX] . - - The ".dev0" means dirty. Note that .dev0 sorts backwards - (a dirty tree will appear "older" than the corresponding clean one), - but you shouldn't be releasing software with -dirty anyways. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "g%s" % pieces["short"] - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += "+g%s" % pieces["short"] - return rendered - - -def render_pep440_old(pieces): - """TAG[.postDISTANCE[.dev0]] . - - The ".dev0" means dirty. - - Eexceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - return rendered - - -def render_git_describe(pieces): - """TAG[-DISTANCE-gHEX][-dirty]. - - Like 'git describe --tags --dirty --always'. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render_git_describe_long(pieces): - """TAG-DISTANCE-gHEX[-dirty]. - - Like 'git describe --tags --dirty --always -long'. - The distance/hash is unconditional. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render(pieces, style): - """Render the given version pieces into the requested style.""" - if pieces["error"]: - return { - "version": "unknown", - "full-revisionid": pieces.get("long"), - "dirty": None, - "error": pieces["error"], - "date": None, - } - - if not style or style == "default": - style = "pep440" # the default - - if style == "pep440": - rendered = render_pep440(pieces) - elif style == "pep440-pre": - rendered = render_pep440_pre(pieces) - elif style == "pep440-post": - rendered = render_pep440_post(pieces) - elif style == "pep440-old": - rendered = render_pep440_old(pieces) - elif style == "git-describe": - rendered = render_git_describe(pieces) - elif style == "git-describe-long": - rendered = render_git_describe_long(pieces) - else: - raise ValueError("unknown style '%s'" % style) - - return { - "version": rendered, - "full-revisionid": pieces["long"], - "dirty": pieces["dirty"], - "error": None, - "date": pieces.get("date"), - } - - -def get_versions(): - """Get version information or return default if unable to do so.""" - # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have - # __file__, we can work backwards from there to the root. Some - # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which - # case we can only use expanded keywords. - - cfg = get_config() - verbose = cfg.verbose - - try: - return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, verbose) - except NotThisMethod: - pass - - try: - root = os.path.realpath(__file__) - # versionfile_source is the relative path from the top of the source - # tree (where the .git directory might live) to this file. Invert - # this to find the root from __file__. - for i in cfg.versionfile_source.split("/"): - root = os.path.dirname(root) - except NameError: - return { - "version": "0+unknown", - "full-revisionid": None, - "dirty": None, - "error": "unable to find root of source tree", - "date": None, - } - - try: - pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) - return render(pieces, cfg.style) - except NotThisMethod: - pass - - try: - if cfg.parentdir_prefix: - return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) - except NotThisMethod: - pass - - return { - "version": "0+unknown", - "full-revisionid": None, - "dirty": None, - "error": "unable to compute version", - "date": None, - } diff --git a/src/mygrad/py.typed b/src/mygrad/py.typed new file mode 100644 index 00000000..e69de29b diff --git a/tests/tensor_ops/test_sequence_ops.py b/tests/tensor_ops/test_sequence_ops.py index 1505cf91..0d0f4b45 100644 --- a/tests/tensor_ops/test_sequence_ops.py +++ b/tests/tensor_ops/test_sequence_ops.py @@ -58,7 +58,7 @@ def test_sequential_arithmetic( f = mygrad_func(*tensors) f1 = true_func(tensors_copy) - assert_allclose(f.data, f1.data) + assert_allclose(f.data, f1.data, rtol=1e-05) f.sum().backward() f1.sum().backward() diff --git a/versioneer.py b/versioneer.py deleted file mode 100644 index 8aed6933..00000000 --- a/versioneer.py +++ /dev/null @@ -1,1886 +0,0 @@ -# Version: 0.18 - -"""The Versioneer - like a rocketeer, but for versions. - -The Versioneer -============== - -* like a rocketeer, but for versions! -* https://github.com/warner/python-versioneer -* Brian Warner -* License: Public Domain -* Compatible With: python2.6, 2.7, 3.2, 3.3, 3.4, 3.5, 3.6, and pypy -* [![Latest Version] -(https://pypip.in/version/versioneer/badge.svg?style=flat) -](https://pypi.python.org/pypi/versioneer/) -* [![Build Status] -(https://travis-ci.org/warner/python-versioneer.png?branch=master) -](https://travis-ci.org/warner/python-versioneer) - -This is a tool for managing a recorded version number in distutils-based -python projects. The goal is to remove the tedious and error-prone "update -the embedded version string" step from your release process. Making a new -release should be as easy as recording a new tag in your version-control -system, and maybe making new tarballs. - - -## Quick Install - -* `pip install versioneer` to somewhere to your $PATH -* add a `[versioneer]` section to your setup.cfg (see below) -* run `versioneer install` in your source tree, commit the results - -## Version Identifiers - -Source trees come from a variety of places: - -* a version-control system checkout (mostly used by developers) -* a nightly tarball, produced by build automation -* a snapshot tarball, produced by a web-based VCS browser, like github's - "tarball from tag" feature -* a release tarball, produced by "setup.py sdist", distributed through PyPI - -Within each source tree, the version identifier (either a string or a number, -this tool is format-agnostic) can come from a variety of places: - -* ask the VCS tool itself, e.g. "git describe" (for checkouts), which knows - about recent "tags" and an absolute revision-id -* the name of the directory into which the tarball was unpacked -* an expanded VCS keyword ($Id$, etc) -* a `_version.py` created by some earlier build step - -For released software, the version identifier is closely related to a VCS -tag. Some projects use tag names that include more than just the version -string (e.g. "myproject-1.2" instead of just "1.2"), in which case the tool -needs to strip the tag prefix to extract the version identifier. For -unreleased software (between tags), the version identifier should provide -enough information to help developers recreate the same tree, while also -giving them an idea of roughly how old the tree is (after version 1.2, before -version 1.3). Many VCS systems can report a description that captures this, -for example `git describe --tags --dirty --always` reports things like -"0.7-1-g574ab98-dirty" to indicate that the checkout is one revision past the -0.7 tag, has a unique revision id of "574ab98", and is "dirty" (it has -uncommitted changes. - -The version identifier is used for multiple purposes: - -* to allow the module to self-identify its version: `myproject.__version__` -* to choose a name and prefix for a 'setup.py sdist' tarball - -## Theory of Operation - -Versioneer works by adding a special `_version.py` file into your source -tree, where your `__init__.py` can import it. This `_version.py` knows how to -dynamically ask the VCS tool for version information at import time. - -`_version.py` also contains `$Revision$` markers, and the installation -process marks `_version.py` to have this marker rewritten with a tag name -during the `git archive` command. As a result, generated tarballs will -contain enough information to get the proper version. - -To allow `setup.py` to compute a version too, a `versioneer.py` is added to -the top level of your source tree, next to `setup.py` and the `setup.cfg` -that configures it. This overrides several distutils/setuptools commands to -compute the version when invoked, and changes `setup.py build` and `setup.py -sdist` to replace `_version.py` with a small static file that contains just -the generated version data. - -## Installation - -See [INSTALL.md](./INSTALL.md) for detailed installation instructions. - -## Version-String Flavors - -Code which uses Versioneer can learn about its version string at runtime by -importing `_version` from your main `__init__.py` file and running the -`get_versions()` function. From the "outside" (e.g. in `setup.py`), you can -import the top-level `versioneer.py` and run `get_versions()`. - -Both functions return a dictionary with different flavors of version -information: - -* `['version']`: A condensed version string, rendered using the selected - style. This is the most commonly used value for the project's version - string. The default "pep440" style yields strings like `0.11`, - `0.11+2.g1076c97`, or `0.11+2.g1076c97.dirty`. See the "Styles" section - below for alternative styles. - -* `['full-revisionid']`: detailed revision identifier. For Git, this is the - full SHA1 commit id, e.g. "1076c978a8d3cfc70f408fe5974aa6c092c949ac". - -* `['date']`: Date and time of the latest `HEAD` commit. For Git, it is the - commit date in ISO 8601 format. This will be None if the date is not - available. - -* `['dirty']`: a boolean, True if the tree has uncommitted changes. Note that - this is only accurate if run in a VCS checkout, otherwise it is likely to - be False or None - -* `['error']`: if the version string could not be computed, this will be set - to a string describing the problem, otherwise it will be None. It may be - useful to throw an exception in setup.py if this is set, to avoid e.g. - creating tarballs with a version string of "unknown". - -Some variants are more useful than others. Including `full-revisionid` in a -bug report should allow developers to reconstruct the exact code being tested -(or indicate the presence of local changes that should be shared with the -developers). `version` is suitable for display in an "about" box or a CLI -`--version` output: it can be easily compared against release notes and lists -of bugs fixed in various releases. - -The installer adds the following text to your `__init__.py` to place a basic -version in `YOURPROJECT.__version__`: - - from ._version import get_versions - __version__ = get_versions()['version'] - del get_versions - -## Styles - -The setup.cfg `style=` configuration controls how the VCS information is -rendered into a version string. - -The default style, "pep440", produces a PEP440-compliant string, equal to the -un-prefixed tag name for actual releases, and containing an additional "local -version" section with more detail for in-between builds. For Git, this is -TAG[+DISTANCE.gHEX[.dirty]] , using information from `git describe --tags ---dirty --always`. For example "0.11+2.g1076c97.dirty" indicates that the -tree is like the "1076c97" commit but has uncommitted changes (".dirty"), and -that this commit is two revisions ("+2") beyond the "0.11" tag. For released -software (exactly equal to a known tag), the identifier will only contain the -stripped tag, e.g. "0.11". - -Other styles are available. See [details.md](details.md) in the Versioneer -source tree for descriptions. - -## Debugging - -Versioneer tries to avoid fatal errors: if something goes wrong, it will tend -to return a version of "0+unknown". To investigate the problem, run `setup.py -version`, which will run the version-lookup code in a verbose mode, and will -display the full contents of `get_versions()` (including the `error` string, -which may help identify what went wrong). - -## Known Limitations - -Some situations are known to cause problems for Versioneer. This details the -most significant ones. More can be found on Github -[issues page](https://github.com/warner/python-versioneer/issues). - -### Subprojects - -Versioneer has limited support for source trees in which `setup.py` is not in -the root directory (e.g. `setup.py` and `.git/` are *not* siblings). The are -two common reasons why `setup.py` might not be in the root: - -* Source trees which contain multiple subprojects, such as - [Buildbot](https://github.com/buildbot/buildbot), which contains both - "master" and "slave" subprojects, each with their own `setup.py`, - `setup.cfg`, and `tox.ini`. Projects like these produce multiple PyPI - distributions (and upload multiple independently-installable tarballs). -* Source trees whose main purpose is to contain a C library, but which also - provide bindings to Python (and perhaps other langauges) in subdirectories. - -Versioneer will look for `.git` in parent directories, and most operations -should get the right version string. However `pip` and `setuptools` have bugs -and implementation details which frequently cause `pip install .` from a -subproject directory to fail to find a correct version string (so it usually -defaults to `0+unknown`). - -`pip install --editable .` should work correctly. `setup.py install` might -work too. - -Pip-8.1.1 is known to have this problem, but hopefully it will get fixed in -some later version. - -[Bug #38](https://github.com/warner/python-versioneer/issues/38) is tracking -this issue. The discussion in -[PR #61](https://github.com/warner/python-versioneer/pull/61) describes the -issue from the Versioneer side in more detail. -[pip PR#3176](https://github.com/pypa/pip/pull/3176) and -[pip PR#3615](https://github.com/pypa/pip/pull/3615) contain work to improve -pip to let Versioneer work correctly. - -Versioneer-0.16 and earlier only looked for a `.git` directory next to the -`setup.cfg`, so subprojects were completely unsupported with those releases. - -### Editable installs with setuptools <= 18.5 - -`setup.py develop` and `pip install --editable .` allow you to install a -project into a virtualenv once, then continue editing the source code (and -test) without re-installing after every change. - -"Entry-point scripts" (`setup(entry_points={"console_scripts": ..})`) are a -convenient way to specify executable scripts that should be installed along -with the python package. - -These both work as expected when using modern setuptools. When using -setuptools-18.5 or earlier, however, certain operations will cause -`pkg_resources.DistributionNotFound` errors when running the entrypoint -script, which must be resolved by re-installing the package. This happens -when the install happens with one version, then the egg_info data is -regenerated while a different version is checked out. Many setup.py commands -cause egg_info to be rebuilt (including `sdist`, `wheel`, and installing into -a different virtualenv), so this can be surprising. - -[Bug #83](https://github.com/warner/python-versioneer/issues/83) describes -this one, but upgrading to a newer version of setuptools should probably -resolve it. - -### Unicode version strings - -While Versioneer works (and is continually tested) with both Python 2 and -Python 3, it is not entirely consistent with bytes-vs-unicode distinctions. -Newer releases probably generate unicode version strings on py2. It's not -clear that this is wrong, but it may be surprising for applications when then -write these strings to a network connection or include them in bytes-oriented -APIs like cryptographic checksums. - -[Bug #71](https://github.com/warner/python-versioneer/issues/71) investigates -this question. - - -## Updating Versioneer - -To upgrade your project to a new release of Versioneer, do the following: - -* install the new Versioneer (`pip install -U versioneer` or equivalent) -* edit `setup.cfg`, if necessary, to include any new configuration settings - indicated by the release notes. See [UPGRADING](./UPGRADING.md) for details. -* re-run `versioneer install` in your source tree, to replace - `SRC/_version.py` -* commit any changed files - -## Future Directions - -This tool is designed to make it easily extended to other version-control -systems: all VCS-specific components are in separate directories like -src/git/ . The top-level `versioneer.py` script is assembled from these -components by running make-versioneer.py . In the future, make-versioneer.py -will take a VCS name as an argument, and will construct a version of -`versioneer.py` that is specific to the given VCS. It might also take the -configuration arguments that are currently provided manually during -installation by editing setup.py . Alternatively, it might go the other -direction and include code from all supported VCS systems, reducing the -number of intermediate scripts. - - -## License - -To make Versioneer easier to embed, all its code is dedicated to the public -domain. The `_version.py` that it creates is also in the public domain. -Specifically, both are released under the Creative Commons "Public Domain -Dedication" license (CC0-1.0), as described in -https://creativecommons.org/publicdomain/zero/1.0/ . - -""" - -from __future__ import print_function - -import errno -import json -import os -import re -import subprocess -import sys - -try: - import configparser -except ImportError: - import ConfigParser as configparser - - -class VersioneerConfig: - """Container for Versioneer configuration parameters.""" - - -def get_root(): - """Get the project root directory. - - We require that all commands are run from the project root, i.e. the - directory that contains setup.py, setup.cfg, and versioneer.py . - """ - root = os.path.realpath(os.path.abspath(os.getcwd())) - setup_py = os.path.join(root, "setup.py") - versioneer_py = os.path.join(root, "versioneer.py") - if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): - # allow 'python path/to/setup.py COMMAND' - root = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0]))) - setup_py = os.path.join(root, "setup.py") - versioneer_py = os.path.join(root, "versioneer.py") - if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): - err = ( - "Versioneer was unable to run the project root directory. " - "Versioneer requires setup.py to be executed from " - "its immediate directory (like 'python setup.py COMMAND'), " - "or in a way that lets it use sys.argv[0] to find the root " - "(like 'python path/to/setup.py COMMAND')." - ) - raise VersioneerBadRootError(err) - try: - # Certain runtime workflows (setup.py install/develop in a setuptools - # tree) execute all dependencies in a single python process, so - # "versioneer" may be imported multiple times, and python's shared - # module-import table will cache the first one. So we can't use - # os.path.dirname(__file__), as that will find whichever - # versioneer.py was first imported, even in later projects. - me = os.path.realpath(os.path.abspath(__file__)) - me_dir = os.path.normcase(os.path.splitext(me)[0]) - vsr_dir = os.path.normcase(os.path.splitext(versioneer_py)[0]) - if me_dir != vsr_dir: - print( - "Warning: build in %s is using versioneer.py from %s" - % (os.path.dirname(me), versioneer_py) - ) - except NameError: - pass - return root - - -def get_config_from_root(root): - """Read the project setup.cfg file to determine Versioneer config.""" - # This might raise EnvironmentError (if setup.cfg is missing), or - # configparser.NoSectionError (if it lacks a [versioneer] section), or - # configparser.NoOptionError (if it lacks "VCS="). See the docstring at - # the top of versioneer.py for instructions on writing your setup.cfg . - setup_cfg = os.path.join(root, "setup.cfg") - parser = configparser.SafeConfigParser() - with open(setup_cfg, "r") as f: - parser.readfp(f) - VCS = parser.get("versioneer", "VCS") # mandatory - - def get(parser, name): - if parser.has_option("versioneer", name): - return parser.get("versioneer", name) - return None - - cfg = VersioneerConfig() - cfg.VCS = VCS - cfg.style = get(parser, "style") or "" - cfg.versionfile_source = get(parser, "versionfile_source") - cfg.versionfile_build = get(parser, "versionfile_build") - cfg.tag_prefix = get(parser, "tag_prefix") - if cfg.tag_prefix in ("''", '""'): - cfg.tag_prefix = "" - cfg.parentdir_prefix = get(parser, "parentdir_prefix") - cfg.verbose = get(parser, "verbose") - return cfg - - -class NotThisMethod(Exception): - """Exception raised if a method is not valid for the current scenario.""" - - -# these dictionaries contain VCS-specific tools -LONG_VERSION_PY = {} -HANDLERS = {} - - -def register_vcs_handler(vcs, method): # decorator - """Decorator to mark a method as the handler for a particular VCS.""" - - def decorate(f): - """Store f in HANDLERS[vcs][method].""" - if vcs not in HANDLERS: - HANDLERS[vcs] = {} - HANDLERS[vcs][method] = f - return f - - return decorate - - -def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=None): - """Call the given command(s).""" - assert isinstance(commands, list) - p = None - for c in commands: - try: - dispcmd = str([c] + args) - # remember shell=False, so use git.cmd on windows, not just git - p = subprocess.Popen( - [c] + args, - cwd=cwd, - env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr else None), - ) - break - except EnvironmentError: - e = sys.exc_info()[1] - if e.errno == errno.ENOENT: - continue - if verbose: - print("unable to run %s" % dispcmd) - print(e) - return None, None - else: - if verbose: - print("unable to find command, tried %s" % (commands,)) - return None, None - stdout = p.communicate()[0].strip() - if sys.version_info[0] >= 3: - stdout = stdout.decode() - if p.returncode != 0: - if verbose: - print("unable to run %s (error)" % dispcmd) - print("stdout was %s" % stdout) - return None, p.returncode - return stdout, p.returncode - - -LONG_VERSION_PY[ - "git" -] = ''' -# This file helps to compute a version number in source trees obtained from -# git-archive tarball (such as those provided by githubs download-from-tag -# feature). Distribution tarballs (built by setup.py sdist) and build -# directories (produced by setup.py build) will contain a much shorter file -# that just contains the computed version number. - -# This file is released into the public domain. Generated by -# versioneer-0.18 (https://github.com/warner/python-versioneer) - -"""Git implementation of _version.py.""" - -import errno -import os -import re -import subprocess -import sys - - -def get_keywords(): - """Get the keywords needed to look up the version information.""" - # these strings will be replaced by git during git-archive. - # setup.py/versioneer.py will grep for the variable names, so they must - # each be defined on a line of their own. _version.py will just call - # get_keywords(). - git_refnames = "%(DOLLAR)sFormat:%%d%(DOLLAR)s" - git_full = "%(DOLLAR)sFormat:%%H%(DOLLAR)s" - git_date = "%(DOLLAR)sFormat:%%ci%(DOLLAR)s" - keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} - return keywords - - -class VersioneerConfig: - """Container for Versioneer configuration parameters.""" - - -def get_config(): - """Create, populate and return the VersioneerConfig() object.""" - # these strings are filled in when 'setup.py versioneer' creates - # _version.py - cfg = VersioneerConfig() - cfg.VCS = "git" - cfg.style = "%(STYLE)s" - cfg.tag_prefix = "%(TAG_PREFIX)s" - cfg.parentdir_prefix = "%(PARENTDIR_PREFIX)s" - cfg.versionfile_source = "%(VERSIONFILE_SOURCE)s" - cfg.verbose = False - return cfg - - -class NotThisMethod(Exception): - """Exception raised if a method is not valid for the current scenario.""" - - -LONG_VERSION_PY = {} -HANDLERS = {} - - -def register_vcs_handler(vcs, method): # decorator - """Decorator to mark a method as the handler for a particular VCS.""" - def decorate(f): - """Store f in HANDLERS[vcs][method].""" - if vcs not in HANDLERS: - HANDLERS[vcs] = {} - HANDLERS[vcs][method] = f - return f - return decorate - - -def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, - env=None): - """Call the given command(s).""" - assert isinstance(commands, list) - p = None - for c in commands: - try: - dispcmd = str([c] + args) - # remember shell=False, so use git.cmd on windows, not just git - p = subprocess.Popen([c] + args, cwd=cwd, env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr - else None)) - break - except EnvironmentError: - e = sys.exc_info()[1] - if e.errno == errno.ENOENT: - continue - if verbose: - print("unable to run %%s" %% dispcmd) - print(e) - return None, None - else: - if verbose: - print("unable to find command, tried %%s" %% (commands,)) - return None, None - stdout = p.communicate()[0].strip() - if sys.version_info[0] >= 3: - stdout = stdout.decode() - if p.returncode != 0: - if verbose: - print("unable to run %%s (error)" %% dispcmd) - print("stdout was %%s" %% stdout) - return None, p.returncode - return stdout, p.returncode - - -def versions_from_parentdir(parentdir_prefix, root, verbose): - """Try to determine the version from the parent directory name. - - Source tarballs conventionally unpack into a directory that includes both - the project name and a version string. We will also support searching up - two directory levels for an appropriately named parent directory - """ - rootdirs = [] - - for i in range(3): - dirname = os.path.basename(root) - if dirname.startswith(parentdir_prefix): - return {"version": dirname[len(parentdir_prefix):], - "full-revisionid": None, - "dirty": False, "error": None, "date": None} - else: - rootdirs.append(root) - root = os.path.dirname(root) # up a level - - if verbose: - print("Tried directories %%s but none started with prefix %%s" %% - (str(rootdirs), parentdir_prefix)) - raise NotThisMethod("rootdir doesn't start with parentdir_prefix") - - -@register_vcs_handler("git", "get_keywords") -def git_get_keywords(versionfile_abs): - """Extract version information from the given file.""" - # the code embedded in _version.py can just fetch the value of these - # keywords. When used from setup.py, we don't want to import _version.py, - # so we do it with a regexp instead. This function is not used from - # _version.py. - keywords = {} - try: - f = open(versionfile_abs, "r") - for line in f.readlines(): - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - f.close() - except EnvironmentError: - pass - return keywords - - -@register_vcs_handler("git", "keywords") -def git_versions_from_keywords(keywords, tag_prefix, verbose): - """Get version information from git keywords.""" - if not keywords: - raise NotThisMethod("no keywords at all, weird") - date = keywords.get("date") - if date is not None: - # git-2.2.0 added "%%cI", which expands to an ISO-8601 -compliant - # datestamp. However we prefer "%%ci" (which expands to an "ISO-8601 - # -like" string, which we must then edit to make compliant), because - # it's been around since git-1.5.3, and it's too difficult to - # discover which version we're using, or to work around using an - # older one. - date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - refnames = keywords["refnames"].strip() - if refnames.startswith("$Format"): - if verbose: - print("keywords are unexpanded, not using") - raise NotThisMethod("unexpanded keywords, not a git-archive tarball") - refs = set([r.strip() for r in refnames.strip("()").split(",")]) - # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of - # just "foo-1.0". If we see a "tag: " prefix, prefer those. - TAG = "tag: " - tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) - if not tags: - # Either we're using git < 1.8.3, or there really are no tags. We use - # a heuristic: assume all version tags have a digit. The old git %%d - # expansion behaves like git log --decorate=short and strips out the - # refs/heads/ and refs/tags/ prefixes that would let us distinguish - # between branches and tags. By ignoring refnames without digits, we - # filter out many common branch names like "release" and - # "stabilization", as well as "HEAD" and "master". - tags = set([r for r in refs if re.search(r'\d', r)]) - if verbose: - print("discarding '%%s', no digits" %% ",".join(refs - tags)) - if verbose: - print("likely tags: %%s" %% ",".join(sorted(tags))) - for ref in sorted(tags): - # sorting will prefer e.g. "2.0" over "2.0rc1" - if ref.startswith(tag_prefix): - r = ref[len(tag_prefix):] - if verbose: - print("picking %%s" %% r) - return {"version": r, - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": None, - "date": date} - # no suitable tags, so version is "0+unknown", but full hex is still there - if verbose: - print("no suitable tags, using unknown + full revision id") - return {"version": "0+unknown", - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": "no suitable tags", "date": None} - - -@register_vcs_handler("git", "pieces_from_vcs") -def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): - """Get version from 'git describe' in the root of the source tree. - - This only gets called if the git-archive 'subst' keywords were *not* - expanded, and _version.py hasn't already been rewritten with a short - version string, meaning we're inside a checked out source tree. - """ - GITS = ["git"] - if sys.platform == "win32": - GITS = ["git.cmd", "git.exe"] - - out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, - hide_stderr=True) - if rc != 0: - if verbose: - print("Directory %%s not under git control" %% root) - raise NotThisMethod("'git rev-parse --git-dir' returned error") - - # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] - # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", - "--always", "--long", - "--match", "%%s*" %% tag_prefix], - cwd=root) - # --long was added in git-1.5.5 - if describe_out is None: - raise NotThisMethod("'git describe' failed") - describe_out = describe_out.strip() - full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) - if full_out is None: - raise NotThisMethod("'git rev-parse' failed") - full_out = full_out.strip() - - pieces = {} - pieces["long"] = full_out - pieces["short"] = full_out[:7] # maybe improved later - pieces["error"] = None - - # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] - # TAG might have hyphens. - git_describe = describe_out - - # look for -dirty suffix - dirty = git_describe.endswith("-dirty") - pieces["dirty"] = dirty - if dirty: - git_describe = git_describe[:git_describe.rindex("-dirty")] - - # now we have TAG-NUM-gHEX or HEX - - if "-" in git_describe: - # TAG-NUM-gHEX - mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) - if not mo: - # unparseable. Maybe git-describe is misbehaving? - pieces["error"] = ("unable to parse git-describe output: '%%s'" - %% describe_out) - return pieces - - # tag - full_tag = mo.group(1) - if not full_tag.startswith(tag_prefix): - if verbose: - fmt = "tag '%%s' doesn't start with prefix '%%s'" - print(fmt %% (full_tag, tag_prefix)) - pieces["error"] = ("tag '%%s' doesn't start with prefix '%%s'" - %% (full_tag, tag_prefix)) - return pieces - pieces["closest-tag"] = full_tag[len(tag_prefix):] - - # distance: number of commits since tag - pieces["distance"] = int(mo.group(2)) - - # commit: short hex revision ID - pieces["short"] = mo.group(3) - - else: - # HEX: no tags - pieces["closest-tag"] = None - count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], - cwd=root) - pieces["distance"] = int(count_out) # total number of commits - - # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = run_command(GITS, ["show", "-s", "--format=%%ci", "HEAD"], - cwd=root)[0].strip() - pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - - return pieces - - -def plus_or_dot(pieces): - """Return a + if we don't already have one, else return a .""" - if "+" in pieces.get("closest-tag", ""): - return "." - return "+" - - -def render_pep440(pieces): - """Build up version string, with post-release "local version identifier". - - Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you - get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty - - Exceptions: - 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += plus_or_dot(pieces) - rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0+untagged.%%d.g%%s" %% (pieces["distance"], - pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def render_pep440_pre(pieces): - """TAG[.post.devDISTANCE] -- No -dirty. - - Exceptions: - 1: no tags. 0.post.devDISTANCE - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += ".post.dev%%d" %% pieces["distance"] - else: - # exception #1 - rendered = "0.post.dev%%d" %% pieces["distance"] - return rendered - - -def render_pep440_post(pieces): - """TAG[.postDISTANCE[.dev0]+gHEX] . - - The ".dev0" means dirty. Note that .dev0 sorts backwards - (a dirty tree will appear "older" than the corresponding clean one), - but you shouldn't be releasing software with -dirty anyways. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%%d" %% pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "g%%s" %% pieces["short"] - else: - # exception #1 - rendered = "0.post%%d" %% pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += "+g%%s" %% pieces["short"] - return rendered - - -def render_pep440_old(pieces): - """TAG[.postDISTANCE[.dev0]] . - - The ".dev0" means dirty. - - Eexceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%%d" %% pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - else: - # exception #1 - rendered = "0.post%%d" %% pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - return rendered - - -def render_git_describe(pieces): - """TAG[-DISTANCE-gHEX][-dirty]. - - Like 'git describe --tags --dirty --always'. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render_git_describe_long(pieces): - """TAG-DISTANCE-gHEX[-dirty]. - - Like 'git describe --tags --dirty --always -long'. - The distance/hash is unconditional. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render(pieces, style): - """Render the given version pieces into the requested style.""" - if pieces["error"]: - return {"version": "unknown", - "full-revisionid": pieces.get("long"), - "dirty": None, - "error": pieces["error"], - "date": None} - - if not style or style == "default": - style = "pep440" # the default - - if style == "pep440": - rendered = render_pep440(pieces) - elif style == "pep440-pre": - rendered = render_pep440_pre(pieces) - elif style == "pep440-post": - rendered = render_pep440_post(pieces) - elif style == "pep440-old": - rendered = render_pep440_old(pieces) - elif style == "git-describe": - rendered = render_git_describe(pieces) - elif style == "git-describe-long": - rendered = render_git_describe_long(pieces) - else: - raise ValueError("unknown style '%%s'" %% style) - - return {"version": rendered, "full-revisionid": pieces["long"], - "dirty": pieces["dirty"], "error": None, - "date": pieces.get("date")} - - -def get_versions(): - """Get version information or return default if unable to do so.""" - # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have - # __file__, we can work backwards from there to the root. Some - # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which - # case we can only use expanded keywords. - - cfg = get_config() - verbose = cfg.verbose - - try: - return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, - verbose) - except NotThisMethod: - pass - - try: - root = os.path.realpath(__file__) - # versionfile_source is the relative path from the top of the source - # tree (where the .git directory might live) to this file. Invert - # this to find the root from __file__. - for i in cfg.versionfile_source.split('/'): - root = os.path.dirname(root) - except NameError: - return {"version": "0+unknown", "full-revisionid": None, - "dirty": None, - "error": "unable to find root of source tree", - "date": None} - - try: - pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) - return render(pieces, cfg.style) - except NotThisMethod: - pass - - try: - if cfg.parentdir_prefix: - return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) - except NotThisMethod: - pass - - return {"version": "0+unknown", "full-revisionid": None, - "dirty": None, - "error": "unable to compute version", "date": None} -''' - - -@register_vcs_handler("git", "get_keywords") -def git_get_keywords(versionfile_abs): - """Extract version information from the given file.""" - # the code embedded in _version.py can just fetch the value of these - # keywords. When used from setup.py, we don't want to import _version.py, - # so we do it with a regexp instead. This function is not used from - # _version.py. - keywords = {} - try: - f = open(versionfile_abs, "r") - for line in f.readlines(): - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - f.close() - except EnvironmentError: - pass - return keywords - - -@register_vcs_handler("git", "keywords") -def git_versions_from_keywords(keywords, tag_prefix, verbose): - """Get version information from git keywords.""" - if not keywords: - raise NotThisMethod("no keywords at all, weird") - date = keywords.get("date") - if date is not None: - # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant - # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 - # -like" string, which we must then edit to make compliant), because - # it's been around since git-1.5.3, and it's too difficult to - # discover which version we're using, or to work around using an - # older one. - date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - refnames = keywords["refnames"].strip() - if refnames.startswith("$Format"): - if verbose: - print("keywords are unexpanded, not using") - raise NotThisMethod("unexpanded keywords, not a git-archive tarball") - refs = set([r.strip() for r in refnames.strip("()").split(",")]) - # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of - # just "foo-1.0". If we see a "tag: " prefix, prefer those. - TAG = "tag: " - tags = set([r[len(TAG) :] for r in refs if r.startswith(TAG)]) - if not tags: - # Either we're using git < 1.8.3, or there really are no tags. We use - # a heuristic: assume all version tags have a digit. The old git %d - # expansion behaves like git log --decorate=short and strips out the - # refs/heads/ and refs/tags/ prefixes that would let us distinguish - # between branches and tags. By ignoring refnames without digits, we - # filter out many common branch names like "release" and - # "stabilization", as well as "HEAD" and "master". - tags = set([r for r in refs if re.search(r"\d", r)]) - if verbose: - print("discarding '%s', no digits" % ",".join(refs - tags)) - if verbose: - print("likely tags: %s" % ",".join(sorted(tags))) - for ref in sorted(tags): - # sorting will prefer e.g. "2.0" over "2.0rc1" - if ref.startswith(tag_prefix): - r = ref[len(tag_prefix) :] - if verbose: - print("picking %s" % r) - return { - "version": r, - "full-revisionid": keywords["full"].strip(), - "dirty": False, - "error": None, - "date": date, - } - # no suitable tags, so version is "0+unknown", but full hex is still there - if verbose: - print("no suitable tags, using unknown + full revision id") - return { - "version": "0+unknown", - "full-revisionid": keywords["full"].strip(), - "dirty": False, - "error": "no suitable tags", - "date": None, - } - - -@register_vcs_handler("git", "pieces_from_vcs") -def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): - """Get version from 'git describe' in the root of the source tree. - - This only gets called if the git-archive 'subst' keywords were *not* - expanded, and _version.py hasn't already been rewritten with a short - version string, meaning we're inside a checked out source tree. - """ - GITS = ["git"] - if sys.platform == "win32": - GITS = ["git.cmd", "git.exe"] - - out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True) - if rc != 0: - if verbose: - print("Directory %s not under git control" % root) - raise NotThisMethod("'git rev-parse --git-dir' returned error") - - # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] - # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = run_command( - GITS, - [ - "describe", - "--tags", - "--dirty", - "--always", - "--long", - "--match", - "%s*" % tag_prefix, - ], - cwd=root, - ) - # --long was added in git-1.5.5 - if describe_out is None: - raise NotThisMethod("'git describe' failed") - describe_out = describe_out.strip() - full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) - if full_out is None: - raise NotThisMethod("'git rev-parse' failed") - full_out = full_out.strip() - - pieces = {} - pieces["long"] = full_out - pieces["short"] = full_out[:7] # maybe improved later - pieces["error"] = None - - # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] - # TAG might have hyphens. - git_describe = describe_out - - # look for -dirty suffix - dirty = git_describe.endswith("-dirty") - pieces["dirty"] = dirty - if dirty: - git_describe = git_describe[: git_describe.rindex("-dirty")] - - # now we have TAG-NUM-gHEX or HEX - - if "-" in git_describe: - # TAG-NUM-gHEX - mo = re.search(r"^(.+)-(\d+)-g([0-9a-f]+)$", git_describe) - if not mo: - # unparseable. Maybe git-describe is misbehaving? - pieces["error"] = "unable to parse git-describe output: '%s'" % describe_out - return pieces - - # tag - full_tag = mo.group(1) - if not full_tag.startswith(tag_prefix): - if verbose: - fmt = "tag '%s' doesn't start with prefix '%s'" - print(fmt % (full_tag, tag_prefix)) - pieces["error"] = "tag '%s' doesn't start with prefix '%s'" % ( - full_tag, - tag_prefix, - ) - return pieces - pieces["closest-tag"] = full_tag[len(tag_prefix) :] - - # distance: number of commits since tag - pieces["distance"] = int(mo.group(2)) - - # commit: short hex revision ID - pieces["short"] = mo.group(3) - - else: - # HEX: no tags - pieces["closest-tag"] = None - count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], cwd=root) - pieces["distance"] = int(count_out) # total number of commits - - # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[ - 0 - ].strip() - pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - - return pieces - - -def do_vcs_install(manifest_in, versionfile_source, ipy): - """Git-specific installation logic for Versioneer. - - For Git, this means creating/changing .gitattributes to mark _version.py - for export-subst keyword substitution. - """ - GITS = ["git"] - if sys.platform == "win32": - GITS = ["git.cmd", "git.exe"] - files = [manifest_in, versionfile_source] - if ipy: - files.append(ipy) - try: - me = __file__ - if me.endswith(".pyc") or me.endswith(".pyo"): - me = os.path.splitext(me)[0] + ".py" - versioneer_file = os.path.relpath(me) - except NameError: - versioneer_file = "versioneer.py" - files.append(versioneer_file) - present = False - try: - f = open(".gitattributes", "r") - for line in f.readlines(): - if line.strip().startswith(versionfile_source): - if "export-subst" in line.strip().split()[1:]: - present = True - f.close() - except EnvironmentError: - pass - if not present: - f = open(".gitattributes", "a+") - f.write("%s export-subst\n" % versionfile_source) - f.close() - files.append(".gitattributes") - run_command(GITS, ["add", "--"] + files) - - -def versions_from_parentdir(parentdir_prefix, root, verbose): - """Try to determine the version from the parent directory name. - - Source tarballs conventionally unpack into a directory that includes both - the project name and a version string. We will also support searching up - two directory levels for an appropriately named parent directory - """ - rootdirs = [] - - for i in range(3): - dirname = os.path.basename(root) - if dirname.startswith(parentdir_prefix): - return { - "version": dirname[len(parentdir_prefix) :], - "full-revisionid": None, - "dirty": False, - "error": None, - "date": None, - } - else: - rootdirs.append(root) - root = os.path.dirname(root) # up a level - - if verbose: - print( - "Tried directories %s but none started with prefix %s" - % (str(rootdirs), parentdir_prefix) - ) - raise NotThisMethod("rootdir doesn't start with parentdir_prefix") - - -SHORT_VERSION_PY = """ -# This file was generated by 'versioneer.py' (0.18) from -# revision-control system data, or from the parent directory name of an -# unpacked source archive. Distribution tarballs contain a pre-generated copy -# of this file. - -import json - -version_json = ''' -%s -''' # END VERSION_JSON - - -def get_versions(): - return json.loads(version_json) -""" - - -def versions_from_file(filename): - """Try to determine the version from _version.py if present.""" - try: - with open(filename) as f: - contents = f.read() - except EnvironmentError: - raise NotThisMethod("unable to read _version.py") - mo = re.search( - r"version_json = '''\n(.*)''' # END VERSION_JSON", contents, re.M | re.S - ) - if not mo: - mo = re.search( - r"version_json = '''\r\n(.*)''' # END VERSION_JSON", contents, re.M | re.S - ) - if not mo: - raise NotThisMethod("no version_json in _version.py") - return json.loads(mo.group(1)) - - -def write_to_version_file(filename, versions): - """Write the given version number to the given _version.py file.""" - os.unlink(filename) - contents = json.dumps(versions, sort_keys=True, indent=1, separators=(",", ": ")) - with open(filename, "w") as f: - f.write(SHORT_VERSION_PY % contents) - - print("set %s to '%s'" % (filename, versions["version"])) - - -def plus_or_dot(pieces): - """Return a + if we don't already have one, else return a .""" - if "+" in pieces.get("closest-tag", ""): - return "." - return "+" - - -def render_pep440(pieces): - """Build up version string, with post-release "local version identifier". - - Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you - get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty - - Exceptions: - 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += plus_or_dot(pieces) - rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0+untagged.%d.g%s" % (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def render_pep440_pre(pieces): - """TAG[.post.devDISTANCE] -- No -dirty. - - Exceptions: - 1: no tags. 0.post.devDISTANCE - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += ".post.dev%d" % pieces["distance"] - else: - # exception #1 - rendered = "0.post.dev%d" % pieces["distance"] - return rendered - - -def render_pep440_post(pieces): - """TAG[.postDISTANCE[.dev0]+gHEX] . - - The ".dev0" means dirty. Note that .dev0 sorts backwards - (a dirty tree will appear "older" than the corresponding clean one), - but you shouldn't be releasing software with -dirty anyways. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "g%s" % pieces["short"] - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += "+g%s" % pieces["short"] - return rendered - - -def render_pep440_old(pieces): - """TAG[.postDISTANCE[.dev0]] . - - The ".dev0" means dirty. - - Eexceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - return rendered - - -def render_git_describe(pieces): - """TAG[-DISTANCE-gHEX][-dirty]. - - Like 'git describe --tags --dirty --always'. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render_git_describe_long(pieces): - """TAG-DISTANCE-gHEX[-dirty]. - - Like 'git describe --tags --dirty --always -long'. - The distance/hash is unconditional. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render(pieces, style): - """Render the given version pieces into the requested style.""" - if pieces["error"]: - return { - "version": "unknown", - "full-revisionid": pieces.get("long"), - "dirty": None, - "error": pieces["error"], - "date": None, - } - - if not style or style == "default": - style = "pep440" # the default - - if style == "pep440": - rendered = render_pep440(pieces) - elif style == "pep440-pre": - rendered = render_pep440_pre(pieces) - elif style == "pep440-post": - rendered = render_pep440_post(pieces) - elif style == "pep440-old": - rendered = render_pep440_old(pieces) - elif style == "git-describe": - rendered = render_git_describe(pieces) - elif style == "git-describe-long": - rendered = render_git_describe_long(pieces) - else: - raise ValueError("unknown style '%s'" % style) - - return { - "version": rendered, - "full-revisionid": pieces["long"], - "dirty": pieces["dirty"], - "error": None, - "date": pieces.get("date"), - } - - -class VersioneerBadRootError(Exception): - """The project root directory is unknown or missing key files.""" - - -def get_versions(verbose=False): - """Get the project version from whatever source is available. - - Returns dict with two keys: 'version' and 'full'. - """ - if "versioneer" in sys.modules: - # see the discussion in cmdclass.py:get_cmdclass() - del sys.modules["versioneer"] - - root = get_root() - cfg = get_config_from_root(root) - - assert cfg.VCS is not None, "please set [versioneer]VCS= in setup.cfg" - handlers = HANDLERS.get(cfg.VCS) - assert handlers, "unrecognized VCS '%s'" % cfg.VCS - verbose = verbose or cfg.verbose - assert ( - cfg.versionfile_source is not None - ), "please set versioneer.versionfile_source" - assert cfg.tag_prefix is not None, "please set versioneer.tag_prefix" - - versionfile_abs = os.path.join(root, cfg.versionfile_source) - - # extract version from first of: _version.py, VCS command (e.g. 'git - # describe'), parentdir. This is meant to work for developers using a - # source checkout, for users of a tarball created by 'setup.py sdist', - # and for users of a tarball/zipball created by 'git archive' or github's - # download-from-tag feature or the equivalent in other VCSes. - - get_keywords_f = handlers.get("get_keywords") - from_keywords_f = handlers.get("keywords") - if get_keywords_f and from_keywords_f: - try: - keywords = get_keywords_f(versionfile_abs) - ver = from_keywords_f(keywords, cfg.tag_prefix, verbose) - if verbose: - print("got version from expanded keyword %s" % ver) - return ver - except NotThisMethod: - pass - - try: - ver = versions_from_file(versionfile_abs) - if verbose: - print("got version from file %s %s" % (versionfile_abs, ver)) - return ver - except NotThisMethod: - pass - - from_vcs_f = handlers.get("pieces_from_vcs") - if from_vcs_f: - try: - pieces = from_vcs_f(cfg.tag_prefix, root, verbose) - ver = render(pieces, cfg.style) - if verbose: - print("got version from VCS %s" % ver) - return ver - except NotThisMethod: - pass - - try: - if cfg.parentdir_prefix: - ver = versions_from_parentdir(cfg.parentdir_prefix, root, verbose) - if verbose: - print("got version from parentdir %s" % ver) - return ver - except NotThisMethod: - pass - - if verbose: - print("unable to compute version") - - return { - "version": "0+unknown", - "full-revisionid": None, - "dirty": None, - "error": "unable to compute version", - "date": None, - } - - -def get_version(): - """Get the short version string for this project.""" - return get_versions()["version"] - - -def get_cmdclass(): - """Get the custom setuptools/distutils subclasses used by Versioneer.""" - if "versioneer" in sys.modules: - del sys.modules["versioneer"] - # this fixes the "python setup.py develop" case (also 'install' and - # 'easy_install .'), in which subdependencies of the main project are - # built (using setup.py bdist_egg) in the same python process. Assume - # a main project A and a dependency B, which use different versions - # of Versioneer. A's setup.py imports A's Versioneer, leaving it in - # sys.modules by the time B's setup.py is executed, causing B to run - # with the wrong versioneer. Setuptools wraps the sub-dep builds in a - # sandbox that restores sys.modules to it's pre-build state, so the - # parent is protected against the child's "import versioneer". By - # removing ourselves from sys.modules here, before the child build - # happens, we protect the child from the parent's versioneer too. - # Also see https://github.com/warner/python-versioneer/issues/52 - - cmds = {} - - # we add "version" to both distutils and setuptools - from distutils.core import Command - - class cmd_version(Command): - description = "report generated version string" - user_options = [] - boolean_options = [] - - def initialize_options(self): - pass - - def finalize_options(self): - pass - - def run(self): - vers = get_versions(verbose=True) - print("Version: %s" % vers["version"]) - print(" full-revisionid: %s" % vers.get("full-revisionid")) - print(" dirty: %s" % vers.get("dirty")) - print(" date: %s" % vers.get("date")) - if vers["error"]: - print(" error: %s" % vers["error"]) - - cmds["version"] = cmd_version - - # we override "build_py" in both distutils and setuptools - # - # most invocation pathways end up running build_py: - # distutils/build -> build_py - # distutils/install -> distutils/build ->.. - # setuptools/bdist_wheel -> distutils/install ->.. - # setuptools/bdist_egg -> distutils/install_lib -> build_py - # setuptools/install -> bdist_egg ->.. - # setuptools/develop -> ? - # pip install: - # copies source tree to a tempdir before running egg_info/etc - # if .git isn't copied too, 'git describe' will fail - # then does setup.py bdist_wheel, or sometimes setup.py install - # setup.py egg_info -> ? - - # we override different "build_py" commands for both environments - if "setuptools" in sys.modules: - from setuptools.command.build_py import build_py as _build_py - else: - from distutils.command.build_py import build_py as _build_py - - class cmd_build_py(_build_py): - def run(self): - root = get_root() - cfg = get_config_from_root(root) - versions = get_versions() - _build_py.run(self) - # now locate _version.py in the new build/ directory and replace - # it with an updated value - if cfg.versionfile_build: - target_versionfile = os.path.join(self.build_lib, cfg.versionfile_build) - print("UPDATING %s" % target_versionfile) - write_to_version_file(target_versionfile, versions) - - cmds["build_py"] = cmd_build_py - - if "cx_Freeze" in sys.modules: # cx_freeze enabled? - from cx_Freeze.dist import build_exe as _build_exe - - # nczeczulin reports that py2exe won't like the pep440-style string - # as FILEVERSION, but it can be used for PRODUCTVERSION, e.g. - # setup(console=[{ - # "version": versioneer.get_version().split("+", 1)[0], # FILEVERSION - # "product_version": versioneer.get_version(), - # ... - - class cmd_build_exe(_build_exe): - def run(self): - root = get_root() - cfg = get_config_from_root(root) - versions = get_versions() - target_versionfile = cfg.versionfile_source - print("UPDATING %s" % target_versionfile) - write_to_version_file(target_versionfile, versions) - - _build_exe.run(self) - os.unlink(target_versionfile) - with open(cfg.versionfile_source, "w") as f: - LONG = LONG_VERSION_PY[cfg.VCS] - f.write( - LONG - % { - "DOLLAR": "$", - "STYLE": cfg.style, - "TAG_PREFIX": cfg.tag_prefix, - "PARENTDIR_PREFIX": cfg.parentdir_prefix, - "VERSIONFILE_SOURCE": cfg.versionfile_source, - } - ) - - cmds["build_exe"] = cmd_build_exe - del cmds["build_py"] - - if "py2exe" in sys.modules: # py2exe enabled? - try: - from py2exe.distutils_buildexe import py2exe as _py2exe # py3 - except ImportError: - from py2exe.build_exe import py2exe as _py2exe # py2 - - class cmd_py2exe(_py2exe): - def run(self): - root = get_root() - cfg = get_config_from_root(root) - versions = get_versions() - target_versionfile = cfg.versionfile_source - print("UPDATING %s" % target_versionfile) - write_to_version_file(target_versionfile, versions) - - _py2exe.run(self) - os.unlink(target_versionfile) - with open(cfg.versionfile_source, "w") as f: - LONG = LONG_VERSION_PY[cfg.VCS] - f.write( - LONG - % { - "DOLLAR": "$", - "STYLE": cfg.style, - "TAG_PREFIX": cfg.tag_prefix, - "PARENTDIR_PREFIX": cfg.parentdir_prefix, - "VERSIONFILE_SOURCE": cfg.versionfile_source, - } - ) - - cmds["py2exe"] = cmd_py2exe - - # we override different "sdist" commands for both environments - if "setuptools" in sys.modules: - from setuptools.command.sdist import sdist as _sdist - else: - from distutils.command.sdist import sdist as _sdist - - class cmd_sdist(_sdist): - def run(self): - versions = get_versions() - self._versioneer_generated_versions = versions - # unless we update this, the command will keep using the old - # version - self.distribution.metadata.version = versions["version"] - return _sdist.run(self) - - def make_release_tree(self, base_dir, files): - root = get_root() - cfg = get_config_from_root(root) - _sdist.make_release_tree(self, base_dir, files) - # now locate _version.py in the new base_dir directory - # (remembering that it may be a hardlink) and replace it with an - # updated value - target_versionfile = os.path.join(base_dir, cfg.versionfile_source) - print("UPDATING %s" % target_versionfile) - write_to_version_file( - target_versionfile, self._versioneer_generated_versions - ) - - cmds["sdist"] = cmd_sdist - - return cmds - - -CONFIG_ERROR = """ -setup.cfg is missing the necessary Versioneer configuration. You need -a section like: - - [versioneer] - VCS = git - style = pep440 - versionfile_source = src/myproject/_version.py - versionfile_build = myproject/_version.py - tag_prefix = - parentdir_prefix = myproject- - -You will also need to edit your setup.py to use the results: - - import versioneer - setup(version=versioneer.get_version(), - cmdclass=versioneer.get_cmdclass(), ...) - -Please read the docstring in ./versioneer.py for configuration instructions, -edit setup.cfg, and re-run the installer or 'python versioneer.py setup'. -""" - -SAMPLE_CONFIG = """ -# See the docstring in versioneer.py for instructions. Note that you must -# re-run 'versioneer.py setup' after changing this section, and commit the -# resulting files. - -[versioneer] -#VCS = git -#style = pep440 -#versionfile_source = -#versionfile_build = -#tag_prefix = -#parentdir_prefix = - -""" - -INIT_PY_SNIPPET = """ -from ._version import get_versions -__version__ = get_versions()['version'] -del get_versions -""" - - -def do_setup(): - """Main VCS-independent setup function for installing Versioneer.""" - root = get_root() - try: - cfg = get_config_from_root(root) - except ( - EnvironmentError, - configparser.NoSectionError, - configparser.NoOptionError, - ) as e: - if isinstance(e, (EnvironmentError, configparser.NoSectionError)): - print("Adding sample versioneer config to setup.cfg", file=sys.stderr) - with open(os.path.join(root, "setup.cfg"), "a") as f: - f.write(SAMPLE_CONFIG) - print(CONFIG_ERROR, file=sys.stderr) - return 1 - - print(" creating %s" % cfg.versionfile_source) - with open(cfg.versionfile_source, "w") as f: - LONG = LONG_VERSION_PY[cfg.VCS] - f.write( - LONG - % { - "DOLLAR": "$", - "STYLE": cfg.style, - "TAG_PREFIX": cfg.tag_prefix, - "PARENTDIR_PREFIX": cfg.parentdir_prefix, - "VERSIONFILE_SOURCE": cfg.versionfile_source, - } - ) - - ipy = os.path.join(os.path.dirname(cfg.versionfile_source), "__init__.py") - if os.path.exists(ipy): - try: - with open(ipy, "r") as f: - old = f.read() - except EnvironmentError: - old = "" - if INIT_PY_SNIPPET not in old: - print(" appending to %s" % ipy) - with open(ipy, "a") as f: - f.write(INIT_PY_SNIPPET) - else: - print(" %s unmodified" % ipy) - else: - print(" %s doesn't exist, ok" % ipy) - ipy = None - - # Make sure both the top-level "versioneer.py" and versionfile_source - # (PKG/_version.py, used by runtime code) are in MANIFEST.in, so - # they'll be copied into source distributions. Pip won't be able to - # install the package without this. - manifest_in = os.path.join(root, "MANIFEST.in") - simple_includes = set() - try: - with open(manifest_in, "r") as f: - for line in f: - if line.startswith("include "): - for include in line.split()[1:]: - simple_includes.add(include) - except EnvironmentError: - pass - # That doesn't cover everything MANIFEST.in can do - # (http://docs.python.org/2/distutils/sourcedist.html#commands), so - # it might give some false negatives. Appending redundant 'include' - # lines is safe, though. - if "versioneer.py" not in simple_includes: - print(" appending 'versioneer.py' to MANIFEST.in") - with open(manifest_in, "a") as f: - f.write("include versioneer.py\n") - else: - print(" 'versioneer.py' already in MANIFEST.in") - if cfg.versionfile_source not in simple_includes: - print( - " appending versionfile_source ('%s') to MANIFEST.in" - % cfg.versionfile_source - ) - with open(manifest_in, "a") as f: - f.write("include %s\n" % cfg.versionfile_source) - else: - print(" versionfile_source already in MANIFEST.in") - - # Make VCS-specific changes. For git, this means creating/changing - # .gitattributes to mark _version.py for export-subst keyword - # substitution. - do_vcs_install(manifest_in, cfg.versionfile_source, ipy) - return 0 - - -def scan_setup_py(): - """Validate the contents of setup.py against Versioneer's expectations.""" - found = set() - setters = False - errors = 0 - with open("setup.py", "r") as f: - for line in f.readlines(): - if "import versioneer" in line: - found.add("import") - if "versioneer.get_cmdclass()" in line: - found.add("cmdclass") - if "versioneer.get_version()" in line: - found.add("get_version") - if "versioneer.VCS" in line: - setters = True - if "versioneer.versionfile_source" in line: - setters = True - if len(found) != 3: - print("") - print("Your setup.py appears to be missing some important items") - print("(but I might be wrong). Please make sure it has something") - print("roughly like the following:") - print("") - print(" import versioneer") - print(" setup( version=versioneer.get_version(),") - print(" cmdclass=versioneer.get_cmdclass(), ...)") - print("") - errors += 1 - if setters: - print("You should remove lines like 'versioneer.VCS = ' and") - print("'versioneer.versionfile_source = ' . This configuration") - print("now lives in setup.cfg, and should be removed from setup.py") - print("") - errors += 1 - return errors - - -if __name__ == "__main__": - cmd = sys.argv[1] - if cmd == "setup": - errors = do_setup() - errors += scan_setup_py() - if errors: - sys.exit(1) From 6c8dc95cc07e8d4eeeb63dab09030c9059e403c6 Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 12:24:58 -0400 Subject: [PATCH 05/16] back compat --- src/mygrad/operation_base.py | 11 +++++++++-- src/mygrad/tensor_base.py | 33 +++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/mygrad/operation_base.py b/src/mygrad/operation_base.py index 3be681d1..655d3bc2 100644 --- a/src/mygrad/operation_base.py +++ b/src/mygrad/operation_base.py @@ -8,6 +8,7 @@ import numpy as np +from mygrad._numpy_version import NP_IS_V2 from mygrad._utils import SkipGradient, reduce_broadcast from mygrad.errors import InvalidBackprop, InvalidGradient from mygrad.typing import DTypeLike, Mask @@ -97,7 +98,10 @@ def grad_post_process_fn( if out.ndim == 0: # sum-reduction to a scalar produces a float - out = np.asarray(out) + if NP_IS_V2: + out = np.asarray(out) + else: # pragma: no cover + np.array(out, copy=False) return out @abstractmethod @@ -198,7 +202,10 @@ def backward( f"numpy arrays, got a gradient of type: {type(backed_grad)}" ) - backed_grad = np.asarray(backed_grad) + if NP_IS_V2: + backed_grad = np.asarray(backed_grad) + else: # pragma: no cover + np.array(backed_grad, copy=False) if self.where is not True: backed_grad = backed_grad * self.where diff --git a/src/mygrad/tensor_base.py b/src/mygrad/tensor_base.py index e06095f3..d3d8cbe7 100644 --- a/src/mygrad/tensor_base.py +++ b/src/mygrad/tensor_base.py @@ -29,6 +29,7 @@ import mygrad._utils.duplicating_graph as _dup import mygrad._utils.graph_tracking as _track import mygrad._utils.lock_management as _mem +from mygrad._numpy_version import NP_IS_V2 from mygrad._tensor_core_ops.indexing import GetItem, SetItem from mygrad._utils import WeakRef, WeakRefIterable, collect_all_tensors_and_clear_grads from mygrad.errors import DisconnectedView @@ -738,7 +739,12 @@ def __array_function__( def __array__( self, dtype: DTypeLike = None, copy: Optional[bool] = None ) -> np.ndarray: - return np.asarray(self.data, dtype=dtype, copy=copy) + if NP_IS_V2: + return np.asarray(self.data, dtype=dtype, copy=copy) + else: # pragma: no cover + if copy is None: + copy = False + return np.array(self.data, dtype=dtype, copy=copy) def __init__( self, @@ -798,18 +804,21 @@ def __init__( self._creator: Optional[Operation] = _creator - if copy is False: - self.data = np.asarray(x, dtype=dtype) # type: np.ndarray - if not isinstance(ndmin, Integral): - raise TypeError( - f"'{type(ndmin)}' object cannot be interpreted as an integer" - ) - if ndmin and self.data.ndim < ndmin: - self.data = self.data[(*(None for _ in range(ndmin - self.data.ndim)),)] + if not NP_IS_V2: + self.data = np.array(x, dtype=dtype, copy=copy, ndmin=ndmin) else: - self.data = np.array( - x, dtype=dtype, copy=copy, ndmin=ndmin - ) # type: np.ndarray + if copy is False: + self.data = np.asarray(x, dtype=dtype) + if not isinstance(ndmin, Integral): + raise TypeError( + f"'{type(ndmin)}' object cannot be interpreted as an integer" + ) + if ndmin and self.data.ndim < ndmin: + self.data = self.data[ + (*(None for _ in range(ndmin - self.data.ndim)),) + ] + else: + self.data = np.array(x, dtype=dtype, copy=copy, ndmin=ndmin) dtype = self.data.dtype.type is_float = issubclass(dtype, np.floating) # faster than `numpy.issubdtype` From 70950e0118fee0c48749ab79c856e005d55dcca0 Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 12:31:06 -0400 Subject: [PATCH 06/16] fix imports --- src/mygrad/_utils/__init__.py | 2 +- src/mygrad/_utils/duplicating_graph.py | 2 +- src/mygrad/_utils/lock_management.py | 14 ++++---------- src/mygrad/math/arithmetic/ops.py | 2 +- src/mygrad/nnet/activations/elu.py | 2 +- src/mygrad/nnet/activations/glu.py | 3 ++- src/mygrad/nnet/activations/selu.py | 2 +- src/mygrad/nnet/initializers/dirac.py | 2 +- src/mygrad/nnet/initializers/glorot_normal.py | 2 +- src/mygrad/nnet/initializers/normal.py | 2 +- src/mygrad/nnet/initializers/uniform.py | 2 +- src/mygrad/nnet/layers/batchnorm.py | 4 ++-- src/mygrad/nnet/losses/negative_log_likelihood.py | 3 ++- src/mygrad/operation_base.py | 2 +- src/mygrad/random/funcs.py | 2 +- 15 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/mygrad/_utils/__init__.py b/src/mygrad/_utils/__init__.py index 8f7d86a7..b3e76049 100644 --- a/src/mygrad/_utils/__init__.py +++ b/src/mygrad/_utils/__init__.py @@ -17,8 +17,8 @@ from weakref import ReferenceType if TYPE_CHECKING: # pragma: no cover - from mygrad import Tensor from mygrad.operation_base import Operation # noqa: F401 + from mygrad.tensor_base import Tensor __all__ = [ "collect_all_operations_and_clear_grads", diff --git a/src/mygrad/_utils/duplicating_graph.py b/src/mygrad/_utils/duplicating_graph.py index 08fa1a6a..c69a87fa 100644 --- a/src/mygrad/_utils/duplicating_graph.py +++ b/src/mygrad/_utils/duplicating_graph.py @@ -18,7 +18,7 @@ from mygrad.operation_base import Operation if TYPE_CHECKING: # pragma: no cover - from mygrad import Tensor + from mygrad.tensor_base import Tensor T = TypeVar("T") diff --git a/src/mygrad/_utils/lock_management.py b/src/mygrad/_utils/lock_management.py index 821a71f8..d9169a84 100644 --- a/src/mygrad/_utils/lock_management.py +++ b/src/mygrad/_utils/lock_management.py @@ -4,15 +4,9 @@ import os from collections import Counter, defaultdict -from typing import ( - TYPE_CHECKING, - Counter as CounterType, - DefaultDict, - Dict, - Generator, - Iterable, - Set, -) +from typing import TYPE_CHECKING +from typing import Counter as CounterType +from typing import DefaultDict, Dict, Generator, Iterable, Set from weakref import finalize, ref import numpy as np @@ -20,7 +14,7 @@ from mygrad._utils import ContextTracker, WeakRef, WeakRefIterable if TYPE_CHECKING: # pragma: no cover - from mygrad import Tensor as TensorType + from mygrad.tensor_base import Tensor as TensorType # arr-id -> num active ops involving arr diff --git a/src/mygrad/math/arithmetic/ops.py b/src/mygrad/math/arithmetic/ops.py index ae7420d2..04caf56a 100644 --- a/src/mygrad/math/arithmetic/ops.py +++ b/src/mygrad/math/arithmetic/ops.py @@ -6,7 +6,7 @@ from mygrad.operation_base import BinaryUfunc, Operation, UnaryUfunc if TYPE_CHECKING: # pragma: no cover - from mygrad import Tensor + from mygrad.tensor_base import Tensor __all__ = [ "Add", diff --git a/src/mygrad/nnet/activations/elu.py b/src/mygrad/nnet/activations/elu.py index 91ac6239..e53b2bb3 100644 --- a/src/mygrad/nnet/activations/elu.py +++ b/src/mygrad/nnet/activations/elu.py @@ -3,8 +3,8 @@ import numpy as np -from mygrad import Tensor from mygrad.operation_base import Operation +from mygrad.tensor_base import Tensor from mygrad.typing import ArrayLike __all__ = ["elu"] diff --git a/src/mygrad/nnet/activations/glu.py b/src/mygrad/nnet/activations/glu.py index ebe352ad..055a84cd 100644 --- a/src/mygrad/nnet/activations/glu.py +++ b/src/mygrad/nnet/activations/glu.py @@ -2,7 +2,8 @@ from numpy import ndarray -from mygrad import Tensor, multiply +from mygrad.math.arithmetic.funcs import multiply +from mygrad.tensor_base import Tensor from mygrad.typing import ArrayLike from .sigmoid import sigmoid diff --git a/src/mygrad/nnet/activations/selu.py b/src/mygrad/nnet/activations/selu.py index a1be3eba..5083ee6e 100644 --- a/src/mygrad/nnet/activations/selu.py +++ b/src/mygrad/nnet/activations/selu.py @@ -2,8 +2,8 @@ import numpy as np -from mygrad import Tensor from mygrad.operation_base import Operation +from mygrad.tensor_base import Tensor from mygrad.typing import ArrayLike __all__ = ["selu"] diff --git a/src/mygrad/nnet/initializers/dirac.py b/src/mygrad/nnet/initializers/dirac.py index ba9478cb..5cdff242 100644 --- a/src/mygrad/nnet/initializers/dirac.py +++ b/src/mygrad/nnet/initializers/dirac.py @@ -2,7 +2,7 @@ import numpy as np -from mygrad import Tensor +from mygrad.tensor_base import Tensor def dirac(*shape: int, dtype=np.float32, constant: Optional[bool] = None) -> Tensor: diff --git a/src/mygrad/nnet/initializers/glorot_normal.py b/src/mygrad/nnet/initializers/glorot_normal.py index 68c04972..fa8282d1 100644 --- a/src/mygrad/nnet/initializers/glorot_normal.py +++ b/src/mygrad/nnet/initializers/glorot_normal.py @@ -1,6 +1,6 @@ import numpy as np -from mygrad import Tensor +from mygrad.tensor_base import Tensor def glorot_normal(*shape, gain=1, dtype=np.float32, constant=None): diff --git a/src/mygrad/nnet/initializers/normal.py b/src/mygrad/nnet/initializers/normal.py index 2b2151ca..fc310f20 100644 --- a/src/mygrad/nnet/initializers/normal.py +++ b/src/mygrad/nnet/initializers/normal.py @@ -1,6 +1,6 @@ import numpy as np -from mygrad import Tensor +from mygrad.tensor_base import Tensor def normal(*shape, mean=0, std=1, dtype=np.float32, constant=None): diff --git a/src/mygrad/nnet/initializers/uniform.py b/src/mygrad/nnet/initializers/uniform.py index 6a9c2b81..e5e5e09a 100644 --- a/src/mygrad/nnet/initializers/uniform.py +++ b/src/mygrad/nnet/initializers/uniform.py @@ -1,6 +1,6 @@ import numpy as np -from mygrad import Tensor +from mygrad.tensor_base import Tensor def uniform(*shape, lower_bound=0, upper_bound=1, dtype=np.float32, constant=None): diff --git a/src/mygrad/nnet/layers/batchnorm.py b/src/mygrad/nnet/layers/batchnorm.py index 3e887b2b..6fae426b 100644 --- a/src/mygrad/nnet/layers/batchnorm.py +++ b/src/mygrad/nnet/layers/batchnorm.py @@ -2,8 +2,8 @@ import numpy as np -from mygrad import Tensor from mygrad.operation_base import Operation +from mygrad.tensor_base import Tensor from mygrad.typing import ArrayLike __all__ = ["batchnorm"] @@ -114,7 +114,7 @@ def batchnorm( gamma: Optional[ArrayLike] = None, beta: Optional[ArrayLike] = None, eps: float, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: """ Performs batch normalization on ``x``:: diff --git a/src/mygrad/nnet/losses/negative_log_likelihood.py b/src/mygrad/nnet/losses/negative_log_likelihood.py index 45df0474..5c892836 100644 --- a/src/mygrad/nnet/losses/negative_log_likelihood.py +++ b/src/mygrad/nnet/losses/negative_log_likelihood.py @@ -2,7 +2,8 @@ import numpy as np -from mygrad import Tensor, asarray, mean +from mygrad.math.sequential.funcs import mean +from mygrad.tensor_base import Tensor, asarray from mygrad.typing import ArrayLike from ._utils import check_loss_inputs diff --git a/src/mygrad/operation_base.py b/src/mygrad/operation_base.py index 655d3bc2..19eb350b 100644 --- a/src/mygrad/operation_base.py +++ b/src/mygrad/operation_base.py @@ -14,7 +14,7 @@ from mygrad.typing import DTypeLike, Mask if TYPE_CHECKING: # pragma: no cover - from mygrad import Tensor + from mygrad.tensor_base import Tensor __all__ = [ diff --git a/src/mygrad/random/funcs.py b/src/mygrad/random/funcs.py index e6d80a6e..9dbbe3d3 100644 --- a/src/mygrad/random/funcs.py +++ b/src/mygrad/random/funcs.py @@ -2,7 +2,7 @@ import numpy as np -from mygrad import Tensor +from mygrad.tensor_base import Tensor from mygrad.typing import Shape From 7ca263b34bed42b48f07aac4e632f1faa28462d2 Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 12:35:43 -0400 Subject: [PATCH 07/16] format --- .flake8 | 3 + setup.cfg | 130 ------------------ src/mygrad/indexing_routines/funcs.py | 2 +- src/mygrad/linalg/ops.py | 2 +- src/mygrad/math/sequential/funcs.py | 14 +- src/mygrad/nnet/activations/softmax.py | 4 +- src/mygrad/nnet/losses/margin_ranking_loss.py | 2 +- src/mygrad/nnet/losses/multiclass_hinge.py | 2 +- src/mygrad/no_grad_funcs/__init__.py | 125 +++++++---------- src/mygrad/tensor_manip/array_shape/funcs.py | 2 +- src/mygrad/typing/_array_like.py | 9 +- src/mygrad/typing/_dtype_like.py | 3 +- src/mygrad/typing/_shape.py | 3 +- tests/math/binary/test_binary_funcs.py | 2 +- tests/test_tensor_manip.py | 2 +- tests/utils/numerical_gradient.py | 2 +- 16 files changed, 75 insertions(+), 232 deletions(-) create mode 100644 .flake8 delete mode 100644 setup.cfg diff --git a/.flake8 b/.flake8 new file mode 100644 index 00000000..2081204d --- /dev/null +++ b/.flake8 @@ -0,0 +1,3 @@ +[flake8] +extend-ignore = F811,D1,D205,D209,D213,D400,D401,D999,D202,E203,E501,W503,E721,F403,F405,E701 +exclude = .git,__pycache__,docs,old,build,dis,tests/annotations/*, tests/test_py310.py,docs/*, src/mygrad/__init__.py, src/mygrad/numpy_compat/__init__.py,src/mygrad/nnet/__init__.py \ No newline at end of file diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index e3ef3b8c..00000000 --- a/setup.cfg +++ /dev/null @@ -1,130 +0,0 @@ -[isort] -known_third_party = graphviz, hypothesis, numpy, numba, pytest, scipy -known_first_party = mygrad, tests -multi_line_output = 3 -include_trailing_comma = True -force_grid_wrap = 0 -combine_as_imports = True -line_length = 88 -src_paths=src/mygrad,tests - -[flake8] -extend-ignore = F811,D1,D205,D209,D213,D400,D401,D999,D202,E203,E501,W503,E721,F403,F405 -exclude = .git,__pycache__,docs,old,build,dis,tests/annotations/*, tests/test_py310.py,versioneer.py,docs/*, src/mygrad/__init__.py, src/mygrad/numpy_compat/__init__.py,src/mygrad/nnet/__init__.py - -[versioneer] -VCS = git -style = pep440 -versionfile_source = src/mygrad/_version.py -versionfile_build = mygrad/_version.py -tag_prefix = v -parentdir_prefix = mygrad- - - -[coverage:run] -omit = src/mygrad/_version.py - src/mygrad/computational_graph.py - - -[codespell] -skip = *.po,*.ts,**/_version.py,docs/source/generated/* -ignore-words-list = ND,nd,nin,dout - -[tox:tox] -envlist = py39,py310,py311,py312,format,min_numpy - -[gh-actions] -python = - 3.9: py39 - 3.10: py310 - 3.11: py311 - 3.12: py312 - -[testenv] -deps = - pytest - hypothesis - scipy - pytest-xdist -commands = pytest -n auto --hypothesis-profile ci \ - {posargs} -extras = rnn - - -[testenv:min_numpy] -deps = numpy==1.24 - {[testenv]deps} -basepython = python3.9 -commands = pytest -n auto --hypothesis-profile ci \ - {posargs} -extras = - -[testenv:py310] -deps = {[testenv]deps} -commands = pytest -n auto --hypothesis-profile ci \ - {posargs} -extras = - -[testenv:py311] -deps = {[testenv]deps} -commands = pytest -n auto --hypothesis-profile ci \ - {posargs} -extras = - -[testenv:py312] # exclude numba dependency for now -deps = pytest - pytest-xdist - hypothesis - scipy -commands = pytest -n auto --hypothesis-profile ci \ - {posargs} -extras = - - -[testenv:coverage] -setenv = NUMBA_DISABLE_JIT=1 - MYGRAD_COVERAGE_MODE=1 -usedevelop = true -basepython = python3.10 -deps = {[testenv]deps} - coverage - pytest-cov -commands = pytest -n auto --cov-report term-missing --cov-config=setup.cfg --cov-fail-under=100 --cov=mygrad tests - - -[testenv:format] -deps = - autoflake - black - isort -commands = - autoflake --recursive --in-place --remove-duplicate-keys --remove-unused-variables . - isort . - black . - - -[testenv:pre-release] # test against pre-releases of dependencies -pip_pre = true -deps = pytest - hypothesis - scipy - pytest-xdist -basepython = python3.11 -commands = pytest -n auto --hypothesis-profile ci \ - {posargs} -extras = - - -[testenv:enforce-format] -skip_install=true -basepython=python3.11 -deps=black - isort - flake8 - pytest - codespell -commands= - black src/ tests/ --diff --check - isort src/ tests/ --diff --check - flake8 src/ tests/ - codespell src/ docs/ \ No newline at end of file diff --git a/src/mygrad/indexing_routines/funcs.py b/src/mygrad/indexing_routines/funcs.py index 78d98fd6..8947c39b 100644 --- a/src/mygrad/indexing_routines/funcs.py +++ b/src/mygrad/indexing_routines/funcs.py @@ -17,7 +17,7 @@ def where( x: ArrayLike = _NoValue, y: ArrayLike = _NoValue, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: """ where(condition, [x, y]) diff --git a/src/mygrad/linalg/ops.py b/src/mygrad/linalg/ops.py index 9deac1bb..3992e6dc 100644 --- a/src/mygrad/linalg/ops.py +++ b/src/mygrad/linalg/ops.py @@ -264,7 +264,7 @@ def __call__( axis=None, keepdims: bool = False, *, - nan_to_num: bool = True + nan_to_num: bool = True, ): self.variables = (tensor,) self._nan_to_num = nan_to_num diff --git a/src/mygrad/math/sequential/funcs.py b/src/mygrad/math/sequential/funcs.py index d8c5d5c5..92270cec 100644 --- a/src/mygrad/math/sequential/funcs.py +++ b/src/mygrad/math/sequential/funcs.py @@ -36,7 +36,7 @@ def sum( axis: Axis = None, keepdims: bool = False, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: """ Sum of tensor elements over a given axis. @@ -124,7 +124,7 @@ def mean( axis: Axis = None, keepdims: bool = False, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: """ Mean of tensor elements over a given axis. @@ -202,7 +202,7 @@ def var( ddof: int = 0, keepdims: bool = False, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: """ Compute the variance along the specified axis. @@ -298,7 +298,7 @@ def std( ddof: int = 0, keepdims: bool = False, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: """ Compute the standard deviation along the specified axis. @@ -392,7 +392,7 @@ def max( axis: Axis = None, keepdims: bool = False, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: """ Return the maximum of a tensor or maximum along its axes. @@ -457,7 +457,7 @@ def min( axis: Axis = None, keepdims: bool = False, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: """ Return the minimum of a tensor or minimum along its axes. @@ -527,7 +527,7 @@ def prod( axis: Axis = None, keepdims: bool = False, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: """ Return the product of array elements over given axes. diff --git a/src/mygrad/nnet/activations/softmax.py b/src/mygrad/nnet/activations/softmax.py index d5f86c26..24ab16d3 100644 --- a/src/mygrad/nnet/activations/softmax.py +++ b/src/mygrad/nnet/activations/softmax.py @@ -41,7 +41,7 @@ def softmax( x: ArrayLike, axis: Union[None, int, Tuple[int, ...]] = -1, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: r""" Applies the softmax activation function:: @@ -115,7 +115,7 @@ def logsoftmax( x: ArrayLike, axis: Union[None, int, Tuple[int, ...]] = -1, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: r""" Applies the log-softmax activation function:: diff --git a/src/mygrad/nnet/losses/margin_ranking_loss.py b/src/mygrad/nnet/losses/margin_ranking_loss.py index 3f099253..daf7e2a4 100644 --- a/src/mygrad/nnet/losses/margin_ranking_loss.py +++ b/src/mygrad/nnet/losses/margin_ranking_loss.py @@ -55,7 +55,7 @@ def margin_ranking_loss( y: ArrayLike, margin: float, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: r"""Computes the margin average margin ranking loss. Equivalent to:: diff --git a/src/mygrad/nnet/losses/multiclass_hinge.py b/src/mygrad/nnet/losses/multiclass_hinge.py index 228e81e7..f107a3bc 100644 --- a/src/mygrad/nnet/losses/multiclass_hinge.py +++ b/src/mygrad/nnet/losses/multiclass_hinge.py @@ -64,7 +64,7 @@ def multiclass_hinge( y_true: ArrayLike, hinge: float = 1.0, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: """Computes the average multiclass hinge loss. diff --git a/src/mygrad/no_grad_funcs/__init__.py b/src/mygrad/no_grad_funcs/__init__.py index b362c241..58d9e2a4 100644 --- a/src/mygrad/no_grad_funcs/__init__.py +++ b/src/mygrad/no_grad_funcs/__init__.py @@ -123,9 +123,8 @@ def isnan( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def isfinite( x: ArrayLike, @@ -135,9 +134,8 @@ def isfinite( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def isinf( x: ArrayLike, @@ -147,9 +145,8 @@ def isinf( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def isnat( x: ArrayLike, @@ -159,9 +156,8 @@ def isnat( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def signbit( x: ArrayLike, @@ -171,9 +167,8 @@ def signbit( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def logical_not( x1: ArrayLike, @@ -184,9 +179,8 @@ def logical_not( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def logical_and( x1: ArrayLike, @@ -197,9 +191,8 @@ def logical_and( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def logical_or( x1: ArrayLike, @@ -210,9 +203,8 @@ def logical_or( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def logical_xor( x1: ArrayLike, @@ -223,9 +215,8 @@ def logical_xor( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def greater( x1: ArrayLike, @@ -236,9 +227,8 @@ def greater( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def greater_equal( x1: ArrayLike, @@ -249,9 +239,8 @@ def greater_equal( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def less( x1: ArrayLike, @@ -262,9 +251,8 @@ def less( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def less_equal( x1: ArrayLike, @@ -275,9 +263,8 @@ def less_equal( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def equal( x1: ArrayLike, @@ -288,9 +275,8 @@ def equal( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def not_equal( x1: ArrayLike, @@ -301,9 +287,8 @@ def not_equal( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Union[numpy.ndarray, bool]: - ... + subok: bool = True, + ) -> Union[numpy.ndarray, bool]: ... def floor_divide( x1: ArrayLike, @@ -314,9 +299,8 @@ def floor_divide( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> numpy.ndarray: - ... + subok: bool = True, + ) -> numpy.ndarray: ... def remainder( x1: ArrayLike, @@ -327,9 +311,8 @@ def remainder( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> numpy.ndarray: - ... + subok: bool = True, + ) -> numpy.ndarray: ... def divmod( x1: ArrayLike, @@ -340,9 +323,8 @@ def divmod( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Tuple[numpy.ndarray, numpy.ndarray]: - ... + subok: bool = True, + ) -> Tuple[numpy.ndarray, numpy.ndarray]: ... def mod( x1: ArrayLike, @@ -353,9 +335,8 @@ def mod( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Tuple[numpy.ndarray, numpy.ndarray]: - ... + subok: bool = True, + ) -> Tuple[numpy.ndarray, numpy.ndarray]: ... def fmod( x1: ArrayLike, @@ -366,9 +347,8 @@ def fmod( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> Tuple[numpy.ndarray, numpy.ndarray]: - ... + subok: bool = True, + ) -> Tuple[numpy.ndarray, numpy.ndarray]: ... def rint( x: ArrayLike, @@ -378,9 +358,8 @@ def rint( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> numpy.ndarray: - ... + subok: bool = True, + ) -> numpy.ndarray: ... def sign( x: ArrayLike, @@ -390,9 +369,8 @@ def sign( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> numpy.ndarray: - ... + subok: bool = True, + ) -> numpy.ndarray: ... def floor( x: ArrayLike, @@ -402,9 +380,8 @@ def floor( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> numpy.ndarray: - ... + subok: bool = True, + ) -> numpy.ndarray: ... def ceil( x: ArrayLike, @@ -414,9 +391,8 @@ def ceil( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> numpy.ndarray: - ... + subok: bool = True, + ) -> numpy.ndarray: ... def trunc( x: ArrayLike, @@ -426,6 +402,5 @@ def trunc( where: Mask = True, order: str = "K", dtype: DTypeLike = None, - subok: bool = True - ) -> numpy.ndarray: - ... + subok: bool = True, + ) -> numpy.ndarray: ... diff --git a/src/mygrad/tensor_manip/array_shape/funcs.py b/src/mygrad/tensor_manip/array_shape/funcs.py index 3b6027a2..7dde6bf6 100644 --- a/src/mygrad/tensor_manip/array_shape/funcs.py +++ b/src/mygrad/tensor_manip/array_shape/funcs.py @@ -72,7 +72,7 @@ def squeeze( a: ArrayLike, axis: Optional[Union[int, Tuple[int, ...]]] = None, *, - constant: Optional[bool] = None + constant: Optional[bool] = None, ) -> Tensor: """ Remove single-dimensional entries from the shape of a tensor. diff --git a/src/mygrad/typing/_array_like.py b/src/mygrad/typing/_array_like.py index dc656293..1de45df5 100644 --- a/src/mygrad/typing/_array_like.py +++ b/src/mygrad/typing/_array_like.py @@ -32,8 +32,7 @@ if not TYPE_CHECKING and not HAS_PROTOCOL: # pragma: no cover class ImplementsArray: - def __array__(self, dtype: None = ...) -> np.ndarray: - ... + def __array__(self, dtype: None = ...) -> np.ndarray: ... else: # pragma: no cover @@ -41,8 +40,7 @@ def __array__(self, dtype: None = ...) -> np.ndarray: class ImplementsArray(Protocol): def __array__( self, dtype: None = ..., copy: Optional[bool] = ... - ) -> np.ndarray: - ... + ) -> np.ndarray: ... Real = Union[int, float] @@ -63,8 +61,7 @@ def __array__( ArrayLike = Union[Real, "Tensor", np.ndarray, ImplementsArray, SequenceNDReals] else: # pragma: no cover - class ArrayLike: - ... + class ArrayLike: ... sb1 = Sequence[bool] diff --git a/src/mygrad/typing/_dtype_like.py b/src/mygrad/typing/_dtype_like.py index 412fafeb..31298a4b 100644 --- a/src/mygrad/typing/_dtype_like.py +++ b/src/mygrad/typing/_dtype_like.py @@ -15,5 +15,4 @@ else: # pragma: no cover - class DTypeLikeReals: - ... + class DTypeLikeReals: ... diff --git a/src/mygrad/typing/_shape.py b/src/mygrad/typing/_shape.py index eb33278e..ecd82f3f 100644 --- a/src/mygrad/typing/_shape.py +++ b/src/mygrad/typing/_shape.py @@ -6,5 +6,4 @@ Shape: TypeAlias = Tuple[int, ...] else: - class Shape: - ... + class Shape: ... diff --git a/tests/math/binary/test_binary_funcs.py b/tests/math/binary/test_binary_funcs.py index 345637a4..7416e38a 100644 --- a/tests/math/binary/test_binary_funcs.py +++ b/tests/math/binary/test_binary_funcs.py @@ -65,7 +65,7 @@ def test_inplace_arithmetic_fwd(op_name: str, kwargs: Dict[str, Any]): true_func=iop, num_arrays=2, permit_0d_array_as_float=False, - **kwargs + **kwargs, ) def iop_fwd(): pass diff --git a/tests/test_tensor_manip.py b/tests/test_tensor_manip.py index 58286af6..c1e08035 100644 --- a/tests/test_tensor_manip.py +++ b/tests/test_tensor_manip.py @@ -392,7 +392,7 @@ def gen_roll_args(draw, arr): dict(min_size=len(shift), max_size=len(shift)) if isinstance(shift, tuple) else {} - ) + ), ) axis = draw(st.none() | st.integers(-arr.ndim, arr.ndim - 1) | ax_strat) else: diff --git a/tests/utils/numerical_gradient.py b/tests/utils/numerical_gradient.py index 20080143..f7a356c7 100644 --- a/tests/utils/numerical_gradient.py +++ b/tests/utils/numerical_gradient.py @@ -13,7 +13,7 @@ def finite_difference( vary_ind=None, h=Decimal(1) / Decimal(int(1e8)), as_decimal=False, - kwargs=None + kwargs=None, ): """Computes numerical partial derivatives of f(x0, x1, ...) in each of its variables, using the central difference method. From b07cc39a0b05ea33967543ae9912ff2c81d9d348 Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 12:37:26 -0400 Subject: [PATCH 08/16] bugfix --- src/mygrad/operation_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mygrad/operation_base.py b/src/mygrad/operation_base.py index 19eb350b..84a8fa4d 100644 --- a/src/mygrad/operation_base.py +++ b/src/mygrad/operation_base.py @@ -205,7 +205,7 @@ def backward( if NP_IS_V2: backed_grad = np.asarray(backed_grad) else: # pragma: no cover - np.array(backed_grad, copy=False) + backed_grad = np.array(backed_grad, copy=False) if self.where is not True: backed_grad = backed_grad * self.where From a5767849a888a107f2bf9b4e004f996146848279 Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 12:40:59 -0400 Subject: [PATCH 09/16] fix formatting and coverage --- pyproject.toml | 2 +- src/mygrad/linalg/funcs.py | 3 ++- tests/math/sequence/test_sequential_funcs.py | 6 ++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f149af2b..7f6abcd7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -141,7 +141,7 @@ basepython = python3.10 deps = {[testenv]deps} coverage pytest-cov -commands = pytest -n auto --cov-report term-missing --cov-config=setup.cfg --cov-fail-under=100 --cov=mygrad tests +commands = pytest -n auto --cov-report term-missing --cov-config=pyproject.toml --cov-fail-under=100 --cov=mygrad tests [testenv:format] diff --git a/src/mygrad/linalg/funcs.py b/src/mygrad/linalg/funcs.py index 030b7904..acbdc930 100644 --- a/src/mygrad/linalg/funcs.py +++ b/src/mygrad/linalg/funcs.py @@ -9,7 +9,8 @@ from numpy.core.einsumfunc import _parse_einsum_input from mygrad.math.misc.funcs import absolute -from mygrad.math.sequential.funcs import max as mg_max, min as mg_min +from mygrad.math.sequential.funcs import max as mg_max +from mygrad.math.sequential.funcs import min as mg_min from mygrad.tensor_base import Tensor, implements_numpy_override from mygrad.typing import ArrayLike diff --git a/tests/math/sequence/test_sequential_funcs.py b/tests/math/sequence/test_sequential_funcs.py index d3be0aa2..cacd27c8 100644 --- a/tests/math/sequence/test_sequential_funcs.py +++ b/tests/math/sequence/test_sequential_funcs.py @@ -14,10 +14,8 @@ from tests.utils.numerical_gradient import numerical_gradient_full from ...custom_strategies import tensors, valid_axes -from ...wrappers.uber import ( - backprop_test_factory as backprop_test_factory, - fwdprop_test_factory as fwdprop_test_factory, -) +from ...wrappers.uber import backprop_test_factory as backprop_test_factory +from ...wrappers.uber import fwdprop_test_factory as fwdprop_test_factory def axis_arg(*arrs, min_dim=0): From 78b7321f463a8769d8e2ae308e7d78cc6a58f2af Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 12:43:20 -0400 Subject: [PATCH 10/16] another bugfix --- src/mygrad/operation_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mygrad/operation_base.py b/src/mygrad/operation_base.py index 84a8fa4d..30ed4f5c 100644 --- a/src/mygrad/operation_base.py +++ b/src/mygrad/operation_base.py @@ -101,7 +101,7 @@ def grad_post_process_fn( if NP_IS_V2: out = np.asarray(out) else: # pragma: no cover - np.array(out, copy=False) + out = np.array(out, copy=False) return out @abstractmethod From 2005668d120f8b7ae30f195f0592919da824052e Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 14:39:12 -0400 Subject: [PATCH 11/16] coverage --- docs/source/changes.rst | 12 ++++++++ pyproject.toml | 2 +- src/mygrad/__init__.py | 2 +- src/mygrad/linalg/funcs.py | 4 +-- src/mygrad/tensor_base.py | 2 +- src/mygrad/typing/_array_like.py | 43 +++++++++++----------------- src/mygrad/typing/_shape.py | 4 +-- src/mygrad/ufuncs/_ufunc_creators.py | 14 ++------- 8 files changed, 37 insertions(+), 46 deletions(-) diff --git a/docs/source/changes.rst b/docs/source/changes.rst index 0f665d73..d66c51cb 100644 --- a/docs/source/changes.rst +++ b/docs/source/changes.rst @@ -6,6 +6,18 @@ This is a record of all past mygrad releases and what went into them, in reverse chronological order. All previous releases should still be available on pip. +.. _v2.3.0: + +------------------ +2.3.0 - 2024-09-07 +------------------ + +- Adds support for NumPy 2.0 +- Minimum supported NumPy version is now 1.24 +- Minimum supported Python version is now 3.9 +- Adds testing for Python 3.12 +- Drops typing-extensions dependency + .. _v2.2.0: ------------------ diff --git a/pyproject.toml b/pyproject.toml index 7f6abcd7..e473477f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ dynamic = ["version"] description = "Drop-in automatic differentiation to NumPy" readme = "README.md" requires-python = ">=3.9" -dependencies = ["numpy >= 1.24, !=1.25.0", "typing-extensions >= 4.1.0, !=4.6.0"] +dependencies = ["numpy >= 1.24, !=1.25.0"] license = { text = "MIT" } authors = [ diff --git a/src/mygrad/__init__.py b/src/mygrad/__init__.py index 371b4335..48b6321a 100644 --- a/src/mygrad/__init__.py +++ b/src/mygrad/__init__.py @@ -40,7 +40,7 @@ ) -if not TYPE_CHECKING: +if not TYPE_CHECKING: # pragma: no cover try: from ._version import version as __version__ except ImportError: diff --git a/src/mygrad/linalg/funcs.py b/src/mygrad/linalg/funcs.py index acbdc930..322591d1 100644 --- a/src/mygrad/linalg/funcs.py +++ b/src/mygrad/linalg/funcs.py @@ -3,9 +3,9 @@ import numpy as np -try: +try: # pragma: no cover from numpy._core.einsumfunc import _parse_einsum_input -except ImportError: +except ImportError: # pragma: no cover from numpy.core.einsumfunc import _parse_einsum_input from mygrad.math.misc.funcs import absolute diff --git a/src/mygrad/tensor_base.py b/src/mygrad/tensor_base.py index d3d8cbe7..baec9773 100644 --- a/src/mygrad/tensor_base.py +++ b/src/mygrad/tensor_base.py @@ -804,7 +804,7 @@ def __init__( self._creator: Optional[Operation] = _creator - if not NP_IS_V2: + if not NP_IS_V2: # pragma: no cover self.data = np.array(x, dtype=dtype, copy=copy, ndmin=ndmin) else: if copy is False: diff --git a/src/mygrad/typing/_array_like.py b/src/mygrad/typing/_array_like.py index 1de45df5..0d63036f 100644 --- a/src/mygrad/typing/_array_like.py +++ b/src/mygrad/typing/_array_like.py @@ -1,25 +1,21 @@ import sys -from typing import TYPE_CHECKING, List, Optional, Sequence, Tuple, TypeVar, Union +from typing import ( + TYPE_CHECKING, + List, + Optional, + Sequence, + Tuple, + TypeVar, + Union, + runtime_checkable, +) import numpy as np -from typing_extensions import runtime_checkable if TYPE_CHECKING: # pragma: no cover from mygrad import Tensor -if sys.version_info >= (3, 8): # pragma: no cover - from typing import Protocol - - HAS_PROTOCOL = True -else: # pragma: no cover - try: - from typing_extensions import Protocol - except ImportError: - HAS_PROTOCOL = False - Protocol = object - else: - HAS_PROTOCOL = True - +from typing import Protocol if sys.version_info >= (3, 10): # pragma: no cover from types import EllipsisType @@ -29,18 +25,11 @@ EllipsisType = type(Ellipsis) -if not TYPE_CHECKING and not HAS_PROTOCOL: # pragma: no cover - - class ImplementsArray: - def __array__(self, dtype: None = ...) -> np.ndarray: ... - -else: # pragma: no cover - - @runtime_checkable - class ImplementsArray(Protocol): - def __array__( - self, dtype: None = ..., copy: Optional[bool] = ... - ) -> np.ndarray: ... +@runtime_checkable +class ImplementsArray(Protocol): + def __array__( + self, dtype: None = ..., copy: Optional[bool] = ... + ) -> np.ndarray: ... Real = Union[int, float] diff --git a/src/mygrad/typing/_shape.py b/src/mygrad/typing/_shape.py index ecd82f3f..2f2eb6b6 100644 --- a/src/mygrad/typing/_shape.py +++ b/src/mygrad/typing/_shape.py @@ -1,6 +1,4 @@ -from typing import TYPE_CHECKING, Tuple - -from typing_extensions import TypeAlias +from typing import TYPE_CHECKING, Tuple, TypeAlias if TYPE_CHECKING: # pragma: no cover Shape: TypeAlias = Tuple[int, ...] diff --git a/src/mygrad/ufuncs/_ufunc_creators.py b/src/mygrad/ufuncs/_ufunc_creators.py index 9ac312be..6a8b032a 100644 --- a/src/mygrad/ufuncs/_ufunc_creators.py +++ b/src/mygrad/ufuncs/_ufunc_creators.py @@ -1,4 +1,3 @@ -import sys from abc import ABC, ABCMeta, abstractmethod from inspect import signature from typing import ( @@ -23,17 +22,10 @@ __all__ = ["ufunc_creator"] -if sys.version_info >= (3, 8): # pragma: no cover - from typing import Literal +from typing import Literal + +HAS_LITERAL = True - HAS_LITERAL = True -else: # pragma: no cover - try: - from typing_extensions import Literal - except ImportError: - HAS_LITERAL = False - else: - HAS_LITERAL = True if TYPE_CHECKING and HAS_LITERAL: # pragma: no cover One = Literal[1] From e66c84e9239ad0205ec17220e34b1c968300d119 Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 14:40:52 -0400 Subject: [PATCH 12/16] lol --- docs/source/changes.rst | 1 - pyproject.toml | 2 +- src/mygrad/typing/_shape.py | 2 ++ 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/source/changes.rst b/docs/source/changes.rst index d66c51cb..8d047821 100644 --- a/docs/source/changes.rst +++ b/docs/source/changes.rst @@ -16,7 +16,6 @@ on pip. - Minimum supported NumPy version is now 1.24 - Minimum supported Python version is now 3.9 - Adds testing for Python 3.12 -- Drops typing-extensions dependency .. _v2.2.0: diff --git a/pyproject.toml b/pyproject.toml index e473477f..7f6abcd7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ dynamic = ["version"] description = "Drop-in automatic differentiation to NumPy" readme = "README.md" requires-python = ">=3.9" -dependencies = ["numpy >= 1.24, !=1.25.0"] +dependencies = ["numpy >= 1.24, !=1.25.0", "typing-extensions >= 4.1.0, !=4.6.0"] license = { text = "MIT" } authors = [ diff --git a/src/mygrad/typing/_shape.py b/src/mygrad/typing/_shape.py index 2f2eb6b6..7796916a 100644 --- a/src/mygrad/typing/_shape.py +++ b/src/mygrad/typing/_shape.py @@ -1,5 +1,7 @@ from typing import TYPE_CHECKING, Tuple, TypeAlias +from typing_extensions import TypeAlias + if TYPE_CHECKING: # pragma: no cover Shape: TypeAlias = Tuple[int, ...] else: From e24e3cac9e0379cbc345cf5e5cb4795a7eb44e3c Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 14:47:51 -0400 Subject: [PATCH 13/16] a --- src/mygrad/typing/_shape.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mygrad/typing/_shape.py b/src/mygrad/typing/_shape.py index 7796916a..ecd82f3f 100644 --- a/src/mygrad/typing/_shape.py +++ b/src/mygrad/typing/_shape.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Tuple, TypeAlias +from typing import TYPE_CHECKING, Tuple from typing_extensions import TypeAlias From c612ba026d37817dba968562734597884b0e0f71 Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sat, 7 Sep 2024 14:48:25 -0400 Subject: [PATCH 14/16] b --- src/mygrad/typing/_array_like.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mygrad/typing/_array_like.py b/src/mygrad/typing/_array_like.py index 0d63036f..f731351b 100644 --- a/src/mygrad/typing/_array_like.py +++ b/src/mygrad/typing/_array_like.py @@ -21,7 +21,7 @@ from types import EllipsisType elif TYPE_CHECKING: # pragma: no cover from builtins import ellipsis as EllipsisType -else: +else: # pragma: no cover EllipsisType = type(Ellipsis) From da9cbd7ecc2c1c517e9959096f3e3c90adea1b57 Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sun, 8 Sep 2024 09:33:32 -0400 Subject: [PATCH 15/16] healthcheck --- tests/test_io.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_io.py b/tests/test_io.py index c5f54852..82cc1573 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -4,7 +4,7 @@ import hypothesis.strategies as st import pytest -from hypothesis import given, settings +from hypothesis import HealthCheck, given, settings from numpy.testing import assert_array_equal from mygrad import Tensor, load, save @@ -35,7 +35,7 @@ def test_save_load_roundtrip(fname: str, as_path: bool, tensor: Tensor): assert_array_equal(tensor.grad, loaded.grad) -@settings(max_examples=10) +@settings(max_examples=10, suppress_health_check=(HealthCheck.too_slow,)) @given( fname=filenames, as_path=st.booleans(), From d3b71f96e6db092cda2966c29ee844a5c2eada74 Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sun, 8 Sep 2024 10:09:59 -0400 Subject: [PATCH 16/16] more health checks --- tests/test_tensor_manip.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_tensor_manip.py b/tests/test_tensor_manip.py index c1e08035..beed4b59 100644 --- a/tests/test_tensor_manip.py +++ b/tests/test_tensor_manip.py @@ -5,7 +5,7 @@ import hypothesis.strategies as st import numpy as np import pytest -from hypothesis import given, infer, settings +from hypothesis import HealthCheck, given, infer, settings from numpy.testing import assert_allclose from pytest import raises @@ -262,6 +262,7 @@ def test_expand_dims_bkwd(): pass +@settings(deadline=None, suppress_health_check=(HealthCheck.filter_too_much,)) @fwdprop_test_factory( mygrad_func=moveaxis, true_func=np.moveaxis, @@ -277,7 +278,7 @@ def test_moveaxis_fwd(): pass -@settings(deadline=None) +@settings(deadline=None, suppress_health_check=(HealthCheck.filter_too_much,)) @backprop_test_factory( mygrad_func=add_constant_passthrough(np.moveaxis), # exercises __array_function__ true_func=np.moveaxis,