From cf8ac853e0d4cff39863f5f1548aa7427c1a7883 Mon Sep 17 00:00:00 2001 From: John Siirola Date: Thu, 16 May 2024 11:30:15 -0600 Subject: [PATCH 1/5] Reorder definitions to avoid NameError in some situations --- pyomo/core/expr/sympy_tools.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/pyomo/core/expr/sympy_tools.py b/pyomo/core/expr/sympy_tools.py index 6c184f0e4c4..b9381148544 100644 --- a/pyomo/core/expr/sympy_tools.py +++ b/pyomo/core/expr/sympy_tools.py @@ -10,12 +10,13 @@ # ___________________________________________________________________________ import operator import sys +from math import prod as _prod +import pyomo.core.expr as EXPR from pyomo.common import DeveloperError from pyomo.common.collections import ComponentMap from pyomo.common.dependencies import attempt_import from pyomo.common.errors import NondifferentiableError -import pyomo.core.expr as EXPR from pyomo.core.expr.numvalue import value, native_types # @@ -113,18 +114,6 @@ def _configure_sympy(sympy, available): sympy, sympy_available = attempt_import('sympy', callback=_configure_sympy) -if sys.version_info[:2] < (3, 8): - - def _prod(args): - ans = 1 - for arg in args: - ans *= arg - return ans - -else: - from math import prod as _prod - - def _nondifferentiable(x): if type(x[1]) is tuple: # sympy >= 1.3 returns tuples (var, order) From fef9947f428125fa8d02887d8f596a87bd61c87c Mon Sep 17 00:00:00 2001 From: John Siirola Date: Thu, 16 May 2024 11:33:18 -0600 Subject: [PATCH 2/5] Remove unused import --- pyomo/core/expr/sympy_tools.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyomo/core/expr/sympy_tools.py b/pyomo/core/expr/sympy_tools.py index b9381148544..b1fd9f8245c 100644 --- a/pyomo/core/expr/sympy_tools.py +++ b/pyomo/core/expr/sympy_tools.py @@ -9,7 +9,6 @@ # This software is distributed under the 3-clause BSD License. # ___________________________________________________________________________ import operator -import sys from math import prod as _prod import pyomo.core.expr as EXPR From 02034790d49a49019bb5e04dedb60de1b8693b7f Mon Sep 17 00:00:00 2001 From: John Siirola Date: Thu, 16 May 2024 12:36:55 -0600 Subject: [PATCH 3/5] Add additional matplotlib import to fix error in community_detection plotting --- pyomo/common/dependencies.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyomo/common/dependencies.py b/pyomo/common/dependencies.py index 4c9e43002ef..d30885e4860 100644 --- a/pyomo/common/dependencies.py +++ b/pyomo/common/dependencies.py @@ -965,6 +965,7 @@ def _finalize_matplotlib(module, available): import matplotlib.pyplot import matplotlib.pylab import matplotlib.backends + import matplotlib.cm # explicit import required for matplotlib>=3.9.0 def _finalize_numpy(np, available): From 8ae8348a3df281b032101255536f19fab8c049e3 Mon Sep 17 00:00:00 2001 From: John Siirola Date: Thu, 16 May 2024 13:30:39 -0600 Subject: [PATCH 4/5] Change source of get_cmap; set minimum matplotlib version --- pyomo/common/dependencies.py | 1 - pyomo/contrib/community_detection/detection.py | 2 +- setup.py | 4 +++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pyomo/common/dependencies.py b/pyomo/common/dependencies.py index d30885e4860..4c9e43002ef 100644 --- a/pyomo/common/dependencies.py +++ b/pyomo/common/dependencies.py @@ -965,7 +965,6 @@ def _finalize_matplotlib(module, available): import matplotlib.pyplot import matplotlib.pylab import matplotlib.backends - import matplotlib.cm # explicit import required for matplotlib>=3.9.0 def _finalize_numpy(np, available): diff --git a/pyomo/contrib/community_detection/detection.py b/pyomo/contrib/community_detection/detection.py index 0e2c3912e06..db3bb8f5a20 100644 --- a/pyomo/contrib/community_detection/detection.py +++ b/pyomo/contrib/community_detection/detection.py @@ -580,7 +580,7 @@ def visualize_model_graph( pos = nx.spring_layout(model_graph) # Define color_map - color_map = plt.cm.get_cmap('viridis', len(numbered_community_map)) + color_map = plt.get_cmap('viridis', len(numbered_community_map)) # Create the figure and draw the graph fig = plt.figure() diff --git a/setup.py b/setup.py index a125b02b2fe..6d28e4d184b 100644 --- a/setup.py +++ b/setup.py @@ -264,7 +264,9 @@ def __ne__(self, other): 'ipython', # contrib.viewer # Note: matplotlib 3.6.1 has bug #24127, which breaks # seaborn's histplot (triggering parmest failures) - 'matplotlib!=3.6.1', + # Note: minimum version from community_detection use of + # matplotlib.pyplot.get_cmap() + 'matplotlib>=3.6.0,!=3.6.1', # network, incidence_analysis, community_detection # Note: networkx 3.2 is Python>-3.9, but there is a broken # 3.2 package on conda-forge that will get implicitly From 8f1d61884edcff657b7f5da64cb72caba0a1880a Mon Sep 17 00:00:00 2001 From: John Siirola Date: Thu, 16 May 2024 15:45:25 -0600 Subject: [PATCH 5/5] Resolve definition ordering --- pyomo/core/expr/sympy_tools.py | 38 +++++++++++++++++----------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/pyomo/core/expr/sympy_tools.py b/pyomo/core/expr/sympy_tools.py index b1fd9f8245c..d751ca35e5f 100644 --- a/pyomo/core/expr/sympy_tools.py +++ b/pyomo/core/expr/sympy_tools.py @@ -28,6 +28,25 @@ _functionMap = {} +def _nondifferentiable(x): + if type(x[1]) is tuple: + # sympy >= 1.3 returns tuples (var, order) + wrt = x[1][0] + else: + # early versions of sympy returned the bare var + wrt = x[1] + raise NondifferentiableError( + "The sub-expression '%s' is not differentiable with respect to %s" % (x[0], wrt) + ) + + +def _external_fcn(*x): + raise TypeError( + "Expressions containing external functions are not convertible to " + f"sympy expressions (found 'f{x}')" + ) + + def _configure_sympy(sympy, available): if not available: return @@ -113,25 +132,6 @@ def _configure_sympy(sympy, available): sympy, sympy_available = attempt_import('sympy', callback=_configure_sympy) -def _nondifferentiable(x): - if type(x[1]) is tuple: - # sympy >= 1.3 returns tuples (var, order) - wrt = x[1][0] - else: - # early versions of sympy returned the bare var - wrt = x[1] - raise NondifferentiableError( - "The sub-expression '%s' is not differentiable with respect to %s" % (x[0], wrt) - ) - - -def _external_fcn(*x): - raise TypeError( - "Expressions containing external functions are not convertible to " - f"sympy expressions (found 'f{x}')" - ) - - class PyomoSympyBimap(object): def __init__(self): self.pyomo2sympy = ComponentMap()