From e4f2643fa6f688566dab09f32b9ba6f5600f6eea Mon Sep 17 00:00:00 2001 From: Michael-T-McCann Date: Thu, 5 May 2022 15:06:03 -0600 Subject: [PATCH 1/5] Start work --- scico/numpy/util.py | 8 +++++++- setup.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/scico/numpy/util.py b/scico/numpy/util.py index 73f626ef4..e3560c234 100644 --- a/scico/numpy/util.py +++ b/scico/numpy/util.py @@ -2,8 +2,9 @@ from __future__ import annotations +import operator import warnings -from math import prod +from functools import reduce from typing import Any, List, Optional, Tuple, Union import numpy as np @@ -18,6 +19,11 @@ from ._blockarray import BlockArray +def prod(iterable): + """Replacement for math.prod for Python 3.7.""" + return reduce(operator.mul, iterable, 1) + + def ensure_on_device( *arrays: Union[np.ndarray, JaxArray, BlockArray] ) -> Union[JaxArray, BlockArray]: diff --git a/setup.py b/setup.py index 22f872e5b..c11d43dc5 100644 --- a/setup.py +++ b/setup.py @@ -60,7 +60,7 @@ f"requirements.txt ({req_jax_ver}) do not match" ) -python_requires = ">=3.8" +python_requires = ">=3.7" tests_require = ["pytest", "pytest-runner"] extra_require_files = [ From 255de4ab142281cc51f797db3cbd33ada8d16d60 Mon Sep 17 00:00:00 2001 From: Michael-T-McCann Date: Fri, 20 May 2022 16:32:50 -0600 Subject: [PATCH 2/5] Add typing.Literal and math.prod for Python 3.7 --- scico/__init__.py | 21 +++++++++++++++++++++ scico/_version.py | 6 +++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/scico/__init__.py b/scico/__init__.py index 96ae86b69..a90974437 100644 --- a/scico/__init__.py +++ b/scico/__init__.py @@ -12,6 +12,27 @@ import sys +# Python 3.7 compatibility +try: + from math import prod + from typing import Literal + + del Literal, prod +except ImportError: + import typing + + typing.Literal = typing.Any + + import math + from functools import reduce + from operator import mul + + # use a double lambda to bind reduce and mul right now + math.prod = (lambda fun, op: lambda iterable: fun(op, iterable, 1))(reduce, mul) + + del typing, math, mul, reduce + + # isort: off from ._autograd import grad, jacrev, linear_adjoint, value_and_grad diff --git a/scico/_version.py b/scico/_version.py index 08de1705b..c6eb1c9db 100644 --- a/scico/_version.py +++ b/scico/_version.py @@ -41,7 +41,11 @@ def variable_assign_value(path: str, var: str) -> Any: with open(path) as f: try: # See http://stackoverflow.com/questions/2058802 - value = parse(next(filter(lambda line: line.startswith(var), f))).body[0].value.s # type: ignore + value_obj = parse(next(filter(lambda line: line.startswith(var), f))).body[0].value + try: + value = value_obj.s # type: ignore + except AttributeError: # Python 3.7 s -> n + value = value_obj.n except StopIteration: raise RuntimeError(f"Could not find initialization of variable {var}") return value From 9a5c5aea4a5b39cb751df50cb8a787a5c5f153d0 Mon Sep 17 00:00:00 2001 From: Michael-T-McCann Date: Fri, 20 May 2022 16:40:04 -0600 Subject: [PATCH 3/5] Ignore types --- scico/__init__.py | 4 ++-- scico/_version.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scico/__init__.py b/scico/__init__.py index a90974437..3b2f1adde 100644 --- a/scico/__init__.py +++ b/scico/__init__.py @@ -21,14 +21,14 @@ except ImportError: import typing - typing.Literal = typing.Any + typing.Literal = typing.Any # type: ignore import math from functools import reduce from operator import mul # use a double lambda to bind reduce and mul right now - math.prod = (lambda fun, op: lambda iterable: fun(op, iterable, 1))(reduce, mul) + math.prod = (lambda fun, op: lambda iterable: fun(op, iterable, 1))(reduce, mul) # type: ignore del typing, math, mul, reduce diff --git a/scico/_version.py b/scico/_version.py index c6eb1c9db..2627c2713 100644 --- a/scico/_version.py +++ b/scico/_version.py @@ -41,7 +41,7 @@ def variable_assign_value(path: str, var: str) -> Any: with open(path) as f: try: # See http://stackoverflow.com/questions/2058802 - value_obj = parse(next(filter(lambda line: line.startswith(var), f))).body[0].value + value_obj = parse(next(filter(lambda line: line.startswith(var), f))).body[0].value # type: ignore try: value = value_obj.s # type: ignore except AttributeError: # Python 3.7 s -> n From d6061fb19c1d41202386b8e8f367af7dc3644722 Mon Sep 17 00:00:00 2001 From: Michael-T-McCann Date: Mon, 23 May 2022 10:38:29 -0600 Subject: [PATCH 4/5] Replace try with version check, move to separate file --- scico/__init__.py | 21 +-------------------- scico/_python37.py | 24 ++++++++++++++++++++++++ scico/_version.py | 9 +++++---- scico/numpy/util.py | 8 +------- 4 files changed, 31 insertions(+), 31 deletions(-) create mode 100644 scico/_python37.py diff --git a/scico/__init__.py b/scico/__init__.py index 3b2f1adde..9bdf5949a 100644 --- a/scico/__init__.py +++ b/scico/__init__.py @@ -12,26 +12,7 @@ import sys -# Python 3.7 compatibility -try: - from math import prod - from typing import Literal - - del Literal, prod -except ImportError: - import typing - - typing.Literal = typing.Any # type: ignore - - import math - from functools import reduce - from operator import mul - - # use a double lambda to bind reduce and mul right now - math.prod = (lambda fun, op: lambda iterable: fun(op, iterable, 1))(reduce, mul) # type: ignore - - del typing, math, mul, reduce - +from . import _python37 # python 3.7 compatibility # isort: off from ._autograd import grad, jacrev, linear_adjoint, value_and_grad diff --git a/scico/_python37.py b/scico/_python37.py new file mode 100644 index 000000000..23b31484f --- /dev/null +++ b/scico/_python37.py @@ -0,0 +1,24 @@ +# Copyright (C) 2021-2022 by SCICO Developers +# All rights reserved. BSD 3-clause License. +# This file is part of the SCICO package. Details of the copyright and +# user license can be found in the 'LICENSE' file distributed with the +# package. + +""" Alter the behavior of (monkey patch) several modules so that SCICO +is compatible with Python 3.7, allowing it to run on Google Colab. +This file should be removed when/if Colab upgrades to Python 3.8. +""" + +import sys + +if sys.version_info.minor == 7: + import typing + + typing.Literal = typing.Any # type: ignore + + import math + from functools import reduce + from operator import mul + + # math.prod = (lambda fun, op: lambda iterable: fun(op, iterable, 1))(reduce, mul) # type: ignore + math.prod = lambda iterable: reduce(mul, iterable, 1) # type: ignore diff --git a/scico/_version.py b/scico/_version.py index 2627c2713..333b6d92e 100644 --- a/scico/_version.py +++ b/scico/_version.py @@ -7,9 +7,9 @@ """Support functions for determining the package version.""" - import os import re +import sys from ast import parse from subprocess import PIPE, Popen from typing import Any, Optional, Tuple, Union @@ -42,10 +42,11 @@ def variable_assign_value(path: str, var: str) -> Any: try: # See http://stackoverflow.com/questions/2058802 value_obj = parse(next(filter(lambda line: line.startswith(var), f))).body[0].value # type: ignore - try: + if sys.version_info.minor == 7: + value = value_obj.n # type: ignore + else: value = value_obj.s # type: ignore - except AttributeError: # Python 3.7 s -> n - value = value_obj.n + except StopIteration: raise RuntimeError(f"Could not find initialization of variable {var}") return value diff --git a/scico/numpy/util.py b/scico/numpy/util.py index e3560c234..73f626ef4 100644 --- a/scico/numpy/util.py +++ b/scico/numpy/util.py @@ -2,9 +2,8 @@ from __future__ import annotations -import operator import warnings -from functools import reduce +from math import prod from typing import Any, List, Optional, Tuple, Union import numpy as np @@ -19,11 +18,6 @@ from ._blockarray import BlockArray -def prod(iterable): - """Replacement for math.prod for Python 3.7.""" - return reduce(operator.mul, iterable, 1) - - def ensure_on_device( *arrays: Union[np.ndarray, JaxArray, BlockArray] ) -> Union[JaxArray, BlockArray]: From 66f3b55853d0fb44dcd175838af3ffba8d025df8 Mon Sep 17 00:00:00 2001 From: Michael-T-McCann Date: Mon, 23 May 2022 11:50:33 -0600 Subject: [PATCH 5/5] Check version 3.7 instead of X.7 --- scico/_python37.py | 2 +- scico/_version.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scico/_python37.py b/scico/_python37.py index 23b31484f..d4f567c5a 100644 --- a/scico/_python37.py +++ b/scico/_python37.py @@ -11,7 +11,7 @@ import sys -if sys.version_info.minor == 7: +if sys.version_info.major == 3 and sys.version_info.minor == 7: import typing typing.Literal = typing.Any # type: ignore diff --git a/scico/_version.py b/scico/_version.py index 333b6d92e..82f2a5860 100644 --- a/scico/_version.py +++ b/scico/_version.py @@ -42,7 +42,7 @@ def variable_assign_value(path: str, var: str) -> Any: try: # See http://stackoverflow.com/questions/2058802 value_obj = parse(next(filter(lambda line: line.startswith(var), f))).body[0].value # type: ignore - if sys.version_info.minor == 7: + if sys.version_info.major == 3 and sys.version_info.minor == 7: value = value_obj.n # type: ignore else: value = value_obj.s # type: ignore