Skip to content

Commit d836482

Browse files
authored
Merge pull request #425 from bknueven/ruff
Add `ruff` linter fixes and checks
2 parents 944ffd1 + fb28cd5 commit d836482

File tree

158 files changed

+339
-698
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+339
-698
lines changed

Diff for: .github/workflows/test_pr_and_main.yml

+22-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ defaults:
1717
shell: bash -l {0}
1818

1919
jobs:
20+
ruff:
21+
name: Ruff Linting
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@v4
25+
- uses: chartboost/ruff-action@v1
26+
2027
nompi4py:
2128
name: no mpi4py
2229
runs-on: ubuntu-latest
30+
needs: [ruff]
2331
steps:
2432
- uses: actions/checkout@v3
2533
- uses: conda-incubator/setup-miniconda@v2
@@ -45,6 +53,7 @@ jobs:
4553
regression:
4654
name: Basic regression tests
4755
runs-on: ubuntu-latest
56+
needs: [ruff]
4857

4958
steps:
5059
- uses: actions/checkout@v3
@@ -86,6 +95,9 @@ jobs:
8695
name: run_all.py
8796
runs-on: ubuntu-latest
8897
timeout-minutes: 15
98+
# this takes the most time, so we'll start it
99+
# without waiting on the linting and other checks
100+
# needs: [ruff]
89101

90102
steps:
91103
- uses: actions/checkout@v3
@@ -117,6 +129,7 @@ jobs:
117129
schur-complement:
118130
name: schur-complement
119131
runs-on: ubuntu-latest
132+
needs: [ruff]
120133
strategy:
121134
matrix:
122135
python-version: [3.8, 3.9]
@@ -151,6 +164,7 @@ jobs:
151164
straight-tests:
152165
name: straight_tests.py
153166
runs-on: ubuntu-latest
167+
needs: [ruff]
154168

155169
steps:
156170
- uses: actions/checkout@v3
@@ -176,6 +190,7 @@ jobs:
176190
admm-wrapper:
177191
name: admm wrapper tests
178192
runs-on: ubuntu-latest
193+
needs: [ruff]
179194

180195
steps:
181196
- uses: actions/checkout@v3
@@ -205,6 +220,7 @@ jobs:
205220
aph:
206221
name: aph tests
207222
runs-on: ubuntu-latest
223+
needs: [ruff]
208224

209225
steps:
210226
- uses: actions/checkout@v3
@@ -232,6 +248,7 @@ jobs:
232248
pickled-bundles:
233249
name: pickled bundles tests
234250
runs-on: ubuntu-latest
251+
needs: [ruff]
235252

236253
steps:
237254
- uses: actions/checkout@v3
@@ -259,6 +276,7 @@ jobs:
259276
confidence-intervals:
260277
name: confidence intervals tests
261278
runs-on: ubuntu-latest
279+
needs: [ruff]
262280

263281
steps:
264282
- uses: actions/checkout@v3
@@ -291,6 +309,7 @@ jobs:
291309
test-generic-pyomo-released:
292310
name: generic_tester.py
293311
runs-on: ubuntu-latest
312+
needs: [ruff]
294313
timeout-minutes: 15
295314

296315
steps:
@@ -323,6 +342,7 @@ jobs:
323342
test-gradient-rho:
324343
name: gradient and rho tests
325344
runs-on: ubuntu-latest
345+
needs: [ruff]
326346

327347
steps:
328348
- uses: actions/checkout@v3
@@ -354,7 +374,6 @@ jobs:
354374
355375
test-headers:
356376
name: header test
357-
358377
runs-on: ubuntu-latest
359378

360379
steps:
@@ -381,8 +400,8 @@ jobs:
381400
382401
test-pysp:
383402
name: pysp tests
384-
385403
runs-on: ubuntu-latest
404+
needs: [ruff]
386405

387406
steps:
388407
- uses: actions/checkout@v3
@@ -415,8 +434,8 @@ jobs:
415434
416435
test-cylinders:
417436
name: tests on some cylinders
418-
419437
runs-on: ubuntu-latest
438+
needs: [ruff]
420439

421440
steps:
422441
- uses: actions/checkout@v3

Diff for: .ruff.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ignore old Pyomo models
2+
extend-exclude = [
3+
"ReferenceModel*.py",
4+
"./mpisppy/utils/callbacks/termination/tests/markshare2.py",
5+
"./mpisppy/tests/examples/hydro/hydro.py",
6+
"./examples/hydro/hydro.py",
7+
]

Diff for: examples/acopf3/ACtree.py

-2
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,9 @@ def __init__(self, Parent, TreeInfo, ScenarioList, Name, CondProb, acstream):
149149
self.kids = []
150150
if self.stage < TreeInfo.NumStages:
151151
bf = TreeInfo.BFs[self.stage-1]
152-
snstr = "_sn"+str(self.sn)
153152
self.sn += 1 # serial number for non-leaf, non-ROOT nodes
154153
else:
155154
bf = 1 # leaf node
156-
snstr = ""
157155
for b in range(bf):
158156
# divide up the scenario list
159157
plist = self.ScenarioList # typing aid

Diff for: examples/acopf3/ccopf2wood.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
# the problem
2424
import ACtree as etree
2525
from ccopf_multistage import pysp2_callback,\
26-
scenario_denouement, _md_dict, FixFast, FixNever, FixGaussian
27-
import rho_setter
26+
scenario_denouement, _md_dict, FixFast
2827

2928
import pyomo.environ as pyo
3029
import socket

Diff for: examples/acopf3/ccopf_multistage.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import egret
1212
import egret.models.acopf as eac
1313
import egret.models.ac_relaxations as eac_relax
14-
from egret.data.model_data import ModelData
1514
from egret.parsers.matpower_parser import create_ModelData
1615
import mpisppy.scenario_tree as scenario_tree
1716
import mpisppy.utils.sputils as sputils
@@ -22,9 +21,7 @@
2221
import sys
2322
import copy
2423
import scipy
25-
import socket
2624
import numpy as np
27-
import datetime as dt
2825
import mpisppy.MPI as mpi
2926

3027
import pyomo.environ as pyo
@@ -57,10 +54,10 @@ def FixGaussian(minutes, acstream, mu, sigma):
5754
#======= end repair functions =====
5855

5956
def _md_dict(epath):
60-
p = str(egret.__path__)
61-
l = p.find("'")
62-
r = p.find("'", l+1)
63-
egretrootpath = p[l+1:r]
57+
path = str(egret.__path__)
58+
left = path.find("'")
59+
right = path.find("'", left+1)
60+
egretrootpath = path[left+1:right]
6461
if epath[0] != os.sep:
6562
test_case = os.path.join(egretrootpath, epath)
6663
else:
@@ -262,9 +259,10 @@ def scenario_denouement(rank, scenario_name, scenario):
262259

263260
print("GEN: %4s PG:" % gen, end="")
264261

262+
previous_val = None
265263
for stage in stages:
266264
current_val = pyo.value(getattr(scenario, "stage_models_"+str(stage)).pg[gen])
267-
if stage == stages[0]:
265+
if previous_val is None:
268266
print("%6.2f -->> " % current_val, end=" ")
269267
else:
270268
print("%6.2f" % (current_val-previous_val), end=" ")
@@ -273,9 +271,10 @@ def scenario_denouement(rank, scenario_name, scenario):
273271

274272
print("GEN: %4s QG:" % gen, end="")
275273

274+
previous_val = None
276275
for stage in stages:
277276
current_val = pyo.value(getattr(scenario, "stage_models_"+str(stage)).qg[gen])
278-
if stage == stages[0]:
277+
if previous_val is None:
279278
print("%6.2f -->> " % current_val, end=" ")
280279
else:
281280
print("%6.2f" % (current_val-previous_val), end=" ")

Diff for: examples/acopf3/fourstage.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# the problem
2424
import ACtree as etree
2525
from ccopf_multistage import pysp2_callback,\
26-
scenario_denouement, _md_dict, FixFast, FixNever, FixGaussian
26+
scenario_denouement, _md_dict, FixFast
2727
import rho_setter
2828

2929
import pyomo.environ as pyo

Diff for: examples/aircond/aircond_cylinders.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
# Use bundle_pickler.py to create bundle pickles
1111
# NOTE: As of 3 March 2022, you can't compare pickle bundle problems with non-pickled. See _demands_creator in aircondB.py for more discusion.
1212

13-
import sys
14-
import os
15-
import copy
1613
import numpy as np
1714
import itertools
1815
from mpisppy import global_toc
@@ -101,7 +98,7 @@ def make_nodenames_balanced(BFs, leaf_nodes=False, root = True):
10198
0, 1, 2, ..., BFs[0], 0_0, 0_1, ..., 0_BFs[1], 1_0, 1_1, ... ,
10299
1_BFs[1], ... , BFs[0]_BFs[1], ... , BFs[0]...BFs[-2]
103100
"""
104-
if leaf_nodes == False:
101+
if not leaf_nodes:
105102
BFs = BFs[:-1] # exclude leaf nodes
106103

107104
# Constructs all nodenames
@@ -156,7 +153,7 @@ def _parse_args():
156153
domain=bool,
157154
default=False)
158155
# special "proper" bundle arguments
159-
parser = pickle_bundle.pickle_bundle_parser(cfg)
156+
pickle_bundle.pickle_bundle_config(cfg)
160157

161158
cfg.add_to_config("EF_directly",
162159
description="Solve the EF directly instead of using cylinders (default False)",
@@ -211,15 +208,13 @@ def main():
211208
all_scenario_names = [f"Bundle_{bn*bsize}_{(bn+1)*bsize-1}" for bn in range(numbuns)]
212209
refmodule = aircondB
213210
primal_rho_setter = None
214-
dual_rho_setter = None
215211
global_toc("WARNING: not using rho setters with proper bundles")
216212

217213
else:
218214
ScenCount = np.prod(BFs)
219215
all_scenario_names = [f"scen{i}" for i in range(ScenCount)] #Scens are 0-based
220216
refmodule = aircond
221217
primal_rho_setter = refmodule.primal_rho_setter
222-
dual_rho_setter = refmodule.dual_rho_setter
223218

224219
xhat_scenario_dict = make_node_scenario_dict_balanced(BFs)
225220
all_nodenames = list(xhat_scenario_dict.keys())
@@ -243,8 +238,8 @@ def main():
243238
ama = amalgamator.from_module(refmodule,
244239
cfg, use_command_line=False)
245240
ama.run()
246-
print(f"EF inner bound=", ama.best_inner_bound)
247-
print(f"EF outer bound=", ama.best_outer_bound)
241+
print("EF inner bound=", ama.best_inner_bound)
242+
print("EF outer bound=", ama.best_outer_bound)
248243
quit()
249244

250245
# if we are still here, we are running cylinders

Diff for: examples/aircond/aircond_ef.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
# full copyright and license information.
88
###############################################################################
99
# multistage (4-stage) example using aircond model. Can be any number of stages, does not support unbalanced trees
10-
import pyomo.environ as pyo
1110
import numpy as np
1211
import mpisppy.utils.sputils as sputils
1312
import mpisppy.utils.amalgamator as amalgamator
14-
from mpisppy import global_toc
1513

1614
# Use this random stream:
1715
aircondstream = np.random.RandomState()
@@ -42,8 +40,8 @@
4240
ama = amalgamator.from_module(refmodel,
4341
ama_options,use_command_line=False)
4442
ama.run()
45-
print(f"inner bound=", ama.best_inner_bound)
46-
print(f"outer bound=", ama.best_outer_bound)
43+
print("inner bound=", ama.best_inner_bound)
44+
print("outer bound=", ama.best_outer_bound)
4745
print ("quitting early")
4846
quit()
4947
from mpisppy.confidence_intervals.mmw_ci import MMWConfidenceIntervals

Diff for: examples/aircond/aircond_seqsampling.py

+2-17
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,8 @@
99
# Use the aircond model to illustrate how to use sequential sampling.
1010
#
1111

12-
import sys
1312
import numpy as np
14-
import argparse
1513
import mpisppy.tests.examples.aircond as aircond
16-
import pyomo.environ as pyo
17-
import pyomo.common.config as pyofig
18-
import mpisppy.utils.sputils as sputils
19-
import mpisppy.utils.amalgamator as amalgamator
2014
import mpisppy.confidence_intervals.multi_seqsampling as multi_seqsampling
2115
import mpisppy.confidence_intervals.confidence_config as conf_config
2216
from mpisppy.utils import config
@@ -29,19 +23,10 @@ def main(cfg):
2923
results (dict): the solution, gap confidence interval and T
3024
"""
3125
refmodelname = "mpisppy.tests.examples.aircond"
32-
scenario_creator = aircond.scenario_creator
3326

3427
BFs = cfg.branching_factors
3528
num_scens = np.prod(BFs)
3629

37-
scenario_creator_kwargs = {"num_scens" : num_scens,
38-
"branching_factors": BFs,
39-
"mu_dev": cfg.mu_dev,
40-
"sigma_dev": cfg.sigma_dev,
41-
"start_ups": cfg.start_ups,
42-
"start_seed": cfg.seed,
43-
}
44-
4530
scenario_names = ['Scenario' + str(i) for i in range(num_scens)]
4631

4732
xhat_gen_kwargs = {"scenario_names": scenario_names,
@@ -114,7 +99,7 @@ def _parse_args():
11499
if cfg.BM_vs_BPL is None:
115100
raise RuntimeError("--BM-vs-BPL must be given.")
116101
if cfg.BM_vs_BPL != "BM" and cfg.BM_vs_BPL != "BPL":
117-
raise RuntimeError(f"--BM-vs-BPL must be BM or BPL (you gave {args.BM_vs_BMPL})")
102+
raise RuntimeError(f"--BM-vs-BPL must be BM or BPL (you gave {cfg.BM_vs_BPL})")
118103

119104
return cfg
120105

@@ -124,7 +109,7 @@ def _parse_args():
124109
cfg.quick_assign("EF_mstage", bool, True)
125110

126111
results = main(cfg)
127-
print(f"Final gap confidence interval results:", results)
112+
print("Final gap confidence interval results:", results)
128113

129114
if cfg.xhat1_file is not None:
130115
print(f"Writing xhat1 to {cfg.xhat1_file}.npy")

Diff for: examples/aircond/bundle_pickler.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
# see try_pickles.bash
1212
# parallel version
1313

14-
import sys
15-
import os
16-
import copy
1714
import numpy as np
18-
import itertools
1915
import mpisppy.tests.examples.aircondB as aircondB
2016
from mpisppy.utils import config
2117
from mpisppy.utils import pickle_bundle
@@ -31,7 +27,7 @@
3127
def _parse_args():
3228
cfg = config.Config()
3329
cfg.multistage()
34-
pickle_bundle.pickle_bundle_parser(cfg)
30+
pickle_bundle.pickle_bundle_config(cfg)
3531
aircondB.inparser_adder(cfg)
3632
cfg.parse_command_line("bundle_pickler for aircond")
3733

@@ -55,8 +51,6 @@ def main():
5551

5652
bsize = int(cfg.scenarios_per_bundle)
5753
numbuns = ScenCount // bsize
58-
# we won't actually use all names
59-
all_bundle_names = [f"Bundle_{bn*bsize}_{(bn+1)*bsize-1}" for bn in range(numbuns)]
6054

6155
if numbuns < n_proc:
6256
raise RuntimeError(

0 commit comments

Comments
 (0)