Skip to content

Commit c424adc

Browse files
committed
Reformat all files with Python Black
1 parent 2eae434 commit c424adc

File tree

158 files changed

+11075
-6894
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

+11075
-6894
lines changed

examples/3zone_toy_stochastic_PySP/PySPInputGenerator.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
4949
"""
5050
from __future__ import print_function
51+
5152
# Inputs directory relative to the location of this script.
5253
inputs_dir = "inputs"
5354
# ScenarioStructure.dat and RootNode.dat will be saved to a
@@ -58,11 +59,10 @@
5859
stage_list = ["Investment", "Operation"]
5960
stage_vars = {
6061
"Investment": ["BuildGen", "BuildLocalTD", "BuildTx"],
61-
"Operation": ["DispatchGen", "GenFuelUseRate"]
62+
"Operation": ["DispatchGen", "GenFuelUseRate"],
6263
}
6364
# List of scenario names
64-
scenario_list = [
65-
"LowFuelCosts", "MediumFuelCosts", "HighFuelCosts"]
65+
scenario_list = ["LowFuelCosts", "MediumFuelCosts", "HighFuelCosts"]
6666

6767
###########################################################
6868

@@ -82,6 +82,7 @@
8282
instance = model.load_inputs(inputs_dir=inputs_dir)
8383
print("inputs successfully loaded...")
8484

85+
8586
def save_dat_files():
8687

8788
if not os.path.exists(os.path.join(inputs_dir, pysp_subdir)):
@@ -92,8 +93,9 @@ def save_dat_files():
9293

9394
dat_file = os.path.join(inputs_dir, pysp_subdir, "RootNode.dat")
9495
print("creating and saving {}...".format(dat_file))
95-
utilities.save_inputs_as_dat(model, instance, save_path=dat_file,
96-
sorted_output=model.options.sorted_output)
96+
utilities.save_inputs_as_dat(
97+
model, instance, save_path=dat_file, sorted_output=model.options.sorted_output
98+
)
9799

98100
#######################
99101
# ScenarioStructure.dat
@@ -117,7 +119,7 @@ def save_dat_files():
117119

118120
f.write("param NodeStage := RootNode {}\n".format(stage_list[0]))
119121
for s in scenario_list:
120-
f.write(" {scen} {st}\n".format(scen=s,st=stage_list[1]))
122+
f.write(" {scen} {st}\n".format(scen=s, st=stage_list[1]))
121123
f.write(";\n\n")
122124

123125
f.write("set Children[RootNode] := ")
@@ -127,7 +129,7 @@ def save_dat_files():
127129

128130
f.write("param ConditionalProbability := RootNode 1.0")
129131
# All scenarios have the same probability in this example
130-
probs = [1.0/len(scenario_list)] * (len(scenario_list) - 1)
132+
probs = [1.0 / len(scenario_list)] * (len(scenario_list) - 1)
131133
# The remaining probability is lumped in the last scenario to avoid rounding issues
132134
probs.append(1.0 - sum(probs))
133135
for (s, p) in zip(scenario_list, probs):
@@ -150,14 +152,16 @@ def write_var_name(f, cname):
150152
if hasattr(instance, cname):
151153
dimen = getattr(instance, cname).index_set().dimen
152154
if dimen == 0:
153-
f.write(" {cn}\n".format(cn=cname))
155+
f.write(" {cn}\n".format(cn=cname))
154156
else:
155-
indexing = (",".join(["*"]*dimen))
157+
indexing = ",".join(["*"] * dimen)
156158
f.write(" {cn}[{dim}]\n".format(cn=cname, dim=indexing))
157159
else:
158160
raise ValueError(
159-
"Variable '{}' is not a component of the model. Did you make a typo?".
160-
format(cname))
161+
"Variable '{}' is not a component of the model. Did you make a typo?".format(
162+
cname
163+
)
164+
)
161165

162166
for st in stage_list:
163167
f.write("set StageVariables[{}] := \n".format(st))
@@ -171,8 +175,9 @@ def write_var_name(f, cname):
171175
f.write(" Operation OperationCost\n")
172176
f.write(";")
173177

178+
174179
####################
175180

176-
if __name__ == '__main__':
181+
if __name__ == "__main__":
177182
# If the script is executed on the command line, then the .dat files are created.
178183
save_dat_files()

examples/3zone_toy_stochastic_PySP/ReferenceModel.py

+26-16
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
# Ideally, we would use the main codebase to generate the model, but the
3939
# mandatory switch argument parser is interferring with pysp's command line tools
40-
#model = switch_model.solve.main(return_model=True)
40+
# model = switch_model.solve.main(return_model=True)
4141

4242
module_list = utilities.get_module_list(args=None)
4343
model = utilities.create_model(module_list, args=[])
@@ -53,14 +53,19 @@
5353
# are nested inside another function in the financials module, they can't
5454
# be called from this script.
5555

56+
5657
def calc_tp_costs_in_period(m, t):
57-
return sum(
58-
getattr(m, tp_cost)[t] * m.tp_weight_in_year[t]
59-
for tp_cost in m.Cost_Components_Per_TP)
58+
return sum(
59+
getattr(m, tp_cost)[t] * m.tp_weight_in_year[t]
60+
for tp_cost in m.Cost_Components_Per_TP
61+
)
62+
63+
6064
def calc_annual_costs_in_period(m, p):
61-
return sum(
62-
getattr(m, annual_cost)[p]
63-
for annual_cost in m.Cost_Components_Per_Period)
65+
return sum(
66+
getattr(m, annual_cost)[p] for annual_cost in m.Cost_Components_Per_Period
67+
)
68+
6469

6570
# In the current version of Switch, all annual costs are defined
6671
# by First Stage decision variables, such as fixed O&M and capital
@@ -73,14 +78,19 @@ def calc_annual_costs_in_period(m, p):
7378
# decisions in this example.
7479
# Further comments on this are written in the Readme file.
7580

76-
model.InvestmentCost = Expression(rule=lambda m: sum(
77-
calc_annual_costs_in_period(m, p) * m.bring_annual_costs_to_base_year[p]
78-
for p in m.PERIODS))
79-
80-
model.OperationCost = Expression(rule=lambda m:
81-
sum(
82-
sum(calc_tp_costs_in_period(m, t) for t in m.TPS_IN_PERIOD[p]
83-
) * m.bring_annual_costs_to_base_year[p]
84-
for p in m.PERIODS))
81+
model.InvestmentCost = Expression(
82+
rule=lambda m: sum(
83+
calc_annual_costs_in_period(m, p) * m.bring_annual_costs_to_base_year[p]
84+
for p in m.PERIODS
85+
)
86+
)
87+
88+
model.OperationCost = Expression(
89+
rule=lambda m: sum(
90+
sum(calc_tp_costs_in_period(m, t) for t in m.TPS_IN_PERIOD[p])
91+
* m.bring_annual_costs_to_base_year[p]
92+
for p in m.PERIODS
93+
)
94+
)
8595

8696
print("model successfully loaded...")

examples/3zone_toy_stochastic_PySP/pha_bounds_cfg.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44
# Use this by adding terms like the following to the runph command:
55
# --linearize-nonbinary-penalty-terms=5 --bounds-cfgfile=pha_bounds_cfg.py
66

7+
78
def pysp_boundsetter_callback(self, scenario_tree, scenario):
8-
m = scenario._instance # see pyomo/pysp/scenariotree/tree_structure.py
9+
m = scenario._instance # see pyomo/pysp/scenariotree/tree_structure.py
910

1011
# BuildLocalTD
1112
for p in m.PERIODS:
1213
for lz in m.LOAD_ZONES:
13-
m.BuildLocalTD[lz, p].setub(2 * m.zone_expected_coincident_peak_demand[lz, p])
14+
m.BuildLocalTD[lz, p].setub(
15+
2 * m.zone_expected_coincident_peak_demand[lz, p]
16+
)
1417

1518
# Estimate an upper bound of system peak demand for limiting generation unit
1619
# & transmission line builds
1720
system_wide_peak = {}
1821
for p in m.PERIODS:
1922
system_wide_peak[p] = sum(
20-
m.zone_expected_coincident_peak_demand[lz, p] for lz in m.LOAD_ZONES)
23+
m.zone_expected_coincident_peak_demand[lz, p] for lz in m.LOAD_ZONES
24+
)
2125

2226
# BuildGen
2327
for g, bld_yr in m.NEW_GEN_BLD_YRS:
@@ -27,6 +31,7 @@ def pysp_boundsetter_callback(self, scenario_tree, scenario):
2731
for tx, bld_yr in m.TRANS_BLD_YRS:
2832
m.BuildTx[tx, bld_yr].setub(5 * system_wide_peak[bld_yr])
2933

34+
3035
# For some reason runph looks for pysp_boundsetter_callback when run in
3136
# single-thread mode and ph_boundsetter_callback when called from mpirun with
3237
# remote execution via pyro. so we map both names to the same function.

examples/3zone_toy_stochastic_PySP/rhosetter.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@
1717
from pyomo.environ import Objective
1818

1919
try:
20-
from pyomo.repn import generate_standard_repn # Pyomo >=5.6
20+
from pyomo.repn import generate_standard_repn # Pyomo >=5.6
21+
2122
newPyomo = True
2223
except ImportError:
23-
from pyomo.repn import generate_canonical_repn # Pyomo <=5.6
24+
from pyomo.repn import generate_canonical_repn # Pyomo <=5.6
25+
2426
newPyomo = False
2527

28+
2629
def ph_rhosetter_callback(ph, scenario_tree, scenario):
27-
# Derive coefficients from active objective
28-
cost_expr = next(scenario._instance.component_data_objects(
29-
Objective, active=True, descend_into=True
30-
))
30+
# Derive coefficients from active objective
31+
cost_expr = next(
32+
scenario._instance.component_data_objects(
33+
Objective, active=True, descend_into=True
34+
)
35+
)
3136
set_rho_values(ph, scenario_tree, scenario, cost_expr)
3237

3338

@@ -56,8 +61,7 @@ def set_rho_values(ph, scenario_tree, scenario, cost_expr):
5661

5762
cost_coefficients = {}
5863
var_names = {}
59-
for (variable, coef) in \
60-
zip(standard_repn.linear_vars, standard_repn.linear_coefs):
64+
for (variable, coef) in zip(standard_repn.linear_vars, standard_repn.linear_coefs):
6165
variable_id = symbol_map.getSymbol(variable)
6266
cost_coefficients[variable_id] = coef
6367
var_names[variable_id] = variable.name
@@ -71,11 +75,13 @@ def set_rho_values(ph, scenario_tree, scenario, cost_expr):
7175
tree_node,
7276
scenario,
7377
variable_id,
74-
cost_coefficients[variable_id] * rho_coefficient)
78+
cost_coefficients[variable_id] * rho_coefficient,
79+
)
7580
set_rho = True
7681
break
7782
if not set_rho:
7883
print(
79-
"Warning! Could not find tree node for variable {}; rho not set."
80-
.format(var_names[variable_id])
84+
"Warning! Could not find tree node for variable {}; rho not set.".format(
85+
var_names[variable_id]
86+
)
8187
)

examples/3zone_toy_stochastic_PySP/rhosetter_FS_only.py

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# The rhosetter module should be in the same directory as this file.
2626
from rhosetter import set_rho_values
2727

28+
2829
def ph_rhosetter_callback(ph, scenario_tree, scenario):
2930
# This component name must match the expression used for first stage
3031
# costs defined in the ReferenceModel.

examples/custom_extension/sunk_costs.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,5 @@
2727

2828

2929
def define_components(mod):
30-
mod.administration_fees = Param(
31-
mod.PERIODS,
32-
initialize=lambda m, p: 1000000)
33-
mod.Cost_Components_Per_Period.append('administration_fees')
30+
mod.administration_fees = Param(mod.PERIODS, initialize=lambda m, p: 1000000)
31+
mod.Cost_Components_Per_Period.append("administration_fees")

run_tests.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ class TestLoader(unittest.TestLoader):
2020
# effects when imported.
2121
def discover(self, start_dir, pattern, top_level_dir):
2222
test_suite = unittest.TestSuite()
23-
for subdir in ('switch_model', 'tests'):
23+
for subdir in ("switch_model", "tests"):
2424
test_suite.addTests(
2525
super(TestLoader, self).discover(
26-
os.path.join(top_level_dir, subdir),
27-
pattern, top_level_dir))
26+
os.path.join(top_level_dir, subdir), pattern, top_level_dir
27+
)
28+
)
2829
return test_suite
2930

3031
# The unittest module does not have built-in support for finding
@@ -37,7 +38,7 @@ def loadTestsFromModule(self, module, **kwargs):
3738
if not docstring:
3839
# Work around a misfeature whereby doctest complains if a
3940
# module contains no docstrings.
40-
module.__doc__ = 'Placeholder docstring'
41+
module.__doc__ = "Placeholder docstring"
4142
test_suite.addTests(doctest.DocTestSuite(module))
4243
if not docstring:
4344
# Restore the original, in case this matters.
@@ -48,12 +49,16 @@ def loadTestsFromModule(self, module, **kwargs):
4849
def main():
4950
script_dir = os.path.join(os.getcwd(), os.path.dirname(__file__))
5051
# print('old argv: {}'.format(sys.argv))
51-
argv = [sys.argv[0],
52-
'discover',
53-
'--top-level-directory', script_dir,
54-
'--pattern', '*.py'] + sys.argv[1:]
52+
argv = [
53+
sys.argv[0],
54+
"discover",
55+
"--top-level-directory",
56+
script_dir,
57+
"--pattern",
58+
"*.py",
59+
] + sys.argv[1:]
5560
unittest.TestProgram(testLoader=TestLoader(), argv=argv, module=None)
5661

5762

58-
if __name__ == '__main__':
63+
if __name__ == "__main__":
5964
main()

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def read(*rnames):
6767
"planning",
6868
"optimization",
6969
],
70-
python_requires='>=3.7',
70+
python_requires=">=3.7",
7171
install_requires=[
7272
"Pyomo>=6.1,<6.4.1", # 6.1 Has all the bug fixes we need
7373
"pint", # needed by Pyomo when we run our tests, but not included
@@ -96,7 +96,7 @@ def read(*rnames):
9696
"dev": ["ipdb", "black", "psycopg2-binary"],
9797
# On Windows at least, installing these will only work via conda.
9898
# Run conda install -c conda-forge geopandas shapely [... all the other packages]
99-
"maps_INSTALL_WITH_CONDA": ["geopandas", "shapely", "cartopy", "plotnine"]
99+
"maps_INSTALL_WITH_CONDA": ["geopandas", "shapely", "cartopy", "plotnine"],
100100
},
101101
entry_points={
102102
"console_scripts": [

switch_model/__init__.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
transmission, local_td, reserves, etc.
1919
"""
2020
from .version import __version__
21+
2122
core_modules = [
22-
'switch_model.timescales',
23-
'switch_model.financials',
24-
'switch_model.balancing.load_zones',
25-
'switch_model.energy_sources.properties',
26-
'switch_model.generators.core',
27-
'switch_model.reporting']
23+
"switch_model.timescales",
24+
"switch_model.financials",
25+
"switch_model.balancing.load_zones",
26+
"switch_model.energy_sources.properties",
27+
"switch_model.generators.core",
28+
"switch_model.reporting",
29+
]

0 commit comments

Comments
 (0)