Skip to content

Commit c5a50b7

Browse files
authored
Merge pull request #3563 from jsiirola/imports-and-tests
Defer `pathlib` import, remove test output file
2 parents 5ae07c8 + aca625e commit c5a50b7

File tree

6 files changed

+19
-22
lines changed

6 files changed

+19
-22
lines changed

Diff for: pyomo/common/dependencies.py

+1
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ def _pyutilib_importer():
11031103
networkx, networkx_available = attempt_import('networkx')
11041104
numpy, numpy_available = attempt_import('numpy', callback=_finalize_numpy)
11051105
pandas, pandas_available = attempt_import('pandas')
1106+
pathlib, pathlib_available = attempt_import('pathlib')
11061107
pint, pint_available = attempt_import(
11071108
'pint',
11081109
# TypeError for pint<=0.24.3 and python>=3.13

Diff for: pyomo/contrib/doe/doe.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@
3232
import logging
3333
import math
3434

35-
from pathlib import Path
36-
3735
from pyomo.common.dependencies import (
3836
numpy as np,
3937
numpy_available,
4038
pandas as pd,
39+
pathlib,
4140
matplotlib as plt,
4241
)
4342
from pyomo.common.modeling import unique_component_name
@@ -243,7 +242,7 @@ def run_doe(self, model=None, results_file=None):
243242
"""
244243
# Check results file name
245244
if results_file is not None:
246-
if type(results_file) not in [Path, str]:
245+
if type(results_file) not in [pathlib.Path, str]:
247246
raise ValueError(
248247
"``results_file`` must be either a Path object or a string."
249248
)
@@ -1827,7 +1826,7 @@ def _curve1D(
18271826
plt.pyplot.show()
18281827
else:
18291828
plt.pyplot.savefig(
1830-
Path(figure_file_name + "_A_opt.png"), format="png", dpi=450
1829+
pathlib.Path(figure_file_name + "_A_opt.png"), format="png", dpi=450
18311830
)
18321831

18331832
# Draw D-optimality
@@ -1848,7 +1847,7 @@ def _curve1D(
18481847
plt.pyplot.show()
18491848
else:
18501849
plt.pyplot.savefig(
1851-
Path(figure_file_name + "_D_opt.png"), format="png", dpi=450
1850+
pathlib.Path(figure_file_name + "_D_opt.png"), format="png", dpi=450
18521851
)
18531852

18541853
# Draw E-optimality
@@ -1869,7 +1868,7 @@ def _curve1D(
18691868
plt.pyplot.show()
18701869
else:
18711870
plt.pyplot.savefig(
1872-
Path(figure_file_name + "_E_opt.png"), format="png", dpi=450
1871+
pathlib.Path(figure_file_name + "_E_opt.png"), format="png", dpi=450
18731872
)
18741873

18751874
# Draw Modified E-optimality
@@ -1890,7 +1889,7 @@ def _curve1D(
18901889
plt.pyplot.show()
18911890
else:
18921891
plt.pyplot.savefig(
1893-
Path(figure_file_name + "_ME_opt.png"), format="png", dpi=450
1892+
pathlib.Path(figure_file_name + "_ME_opt.png"), format="png", dpi=450
18941893
)
18951894

18961895
def _heatmap(
@@ -1991,7 +1990,7 @@ def _heatmap(
19911990
plt.pyplot.show()
19921991
else:
19931992
plt.pyplot.savefig(
1994-
Path(figure_file_name + "_A_opt.png"), format="png", dpi=450
1993+
pathlib.Path(figure_file_name + "_A_opt.png"), format="png", dpi=450
19951994
)
19961995

19971996
# D-optimality
@@ -2017,7 +2016,7 @@ def _heatmap(
20172016
plt.pyplot.show()
20182017
else:
20192018
plt.pyplot.savefig(
2020-
Path(figure_file_name + "_D_opt.png"), format="png", dpi=450
2019+
pathlib.Path(figure_file_name + "_D_opt.png"), format="png", dpi=450
20212020
)
20222021

20232022
# E-optimality
@@ -2043,7 +2042,7 @@ def _heatmap(
20432042
plt.pyplot.show()
20442043
else:
20452044
plt.pyplot.savefig(
2046-
Path(figure_file_name + "_E_opt.png"), format="png", dpi=450
2045+
pathlib.Path(figure_file_name + "_E_opt.png"), format="png", dpi=450
20472046
)
20482047

20492048
# Modified E-optimality
@@ -2069,7 +2068,7 @@ def _heatmap(
20692068
plt.pyplot.show()
20702069
else:
20712070
plt.pyplot.savefig(
2072-
Path(figure_file_name + "_ME_opt.png"), format="png", dpi=450
2071+
pathlib.Path(figure_file_name + "_ME_opt.png"), format="png", dpi=450
20732072
)
20742073

20752074
# Gets the FIM from an existing model

Diff for: pyomo/contrib/doe/examples/reactor_compute_factorial_FIM.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@
88
# rights in this software.
99
# This software is distributed under the 3-clause BSD License.
1010
# ___________________________________________________________________________
11-
from pyomo.common.dependencies import numpy as np
11+
from pyomo.common.dependencies import numpy as np, pathlib
1212

1313
from pyomo.contrib.doe.examples.reactor_experiment import ReactorExperiment
1414
from pyomo.contrib.doe import DesignOfExperiments
1515

1616
import pyomo.environ as pyo
1717

1818
import json
19-
from pathlib import Path
2019

2120

2221
# Example to run a DoE on the reactor
2322
def run_reactor_doe():
2423
# Read in file
25-
DATA_DIR = Path(__file__).parent
24+
DATA_DIR = pathlib.Path(__file__).parent
2625
file_path = DATA_DIR / "result.json"
2726

2827
with open(file_path) as f:

Diff for: pyomo/contrib/doe/examples/reactor_example.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@
88
# rights in this software.
99
# This software is distributed under the 3-clause BSD License.
1010
# ___________________________________________________________________________
11-
from pyomo.common.dependencies import numpy as np
11+
from pyomo.common.dependencies import numpy as np, pathlib
1212

1313
from pyomo.contrib.doe.examples.reactor_experiment import ReactorExperiment
1414
from pyomo.contrib.doe import DesignOfExperiments
1515

1616
import pyomo.environ as pyo
1717

1818
import json
19-
from pathlib import Path
2019

2120

2221
# Example for sensitivity analysis on the reactor experiment
2322
# After sensitivity analysis is done, we perform optimal DoE
2423
def run_reactor_doe():
2524
# Read in file
26-
DATA_DIR = Path(__file__).parent
25+
DATA_DIR = pathlib.Path(__file__).parent
2726
file_path = DATA_DIR / "result.json"
2827

2928
with open(file_path) as f:

Diff for: pyomo/solvers/plugins/solvers/KNITROAMPL.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
# This software is distributed under the 3-clause BSD License.
1010
# ___________________________________________________________________________
1111
import logging
12-
from pathlib import Path
1312

1413
from pyomo.common import Executable
1514
from pyomo.common.collections import Bunch
15+
from pyomo.common.dependencies import pathlib
1616
from pyomo.opt.base.formats import ProblemFormat, ResultsFormat
1717
from pyomo.opt.base.solvers import SolverFactory, OptSolver
1818
from pyomo.solvers.plugins.solvers.ASL import ASL
@@ -71,7 +71,9 @@ def _default_executable(self):
7171
import knitro
7272

7373
package_knitroampl_path = (
74-
Path(knitro.__file__).resolve().parent / 'knitroampl' / 'knitroampl'
74+
pathlib.Path(knitro.__file__).resolve().parent
75+
/ 'knitroampl'
76+
/ 'knitroampl'
7577
)
7678
executable = Executable(str(package_knitroampl_path))
7779
except ModuleNotFoundError:

Diff for: pyomo/solvers/tests/mip/test4.soln

-3
This file was deleted.

0 commit comments

Comments
 (0)