-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery refactored master branch #175
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
@author: fabian | ||
""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
|
||
from pathlib import Path | ||
|
||
import pandas as pd | ||
|
@@ -24,7 +25,7 @@ | |
solver_time = df.loc[df.API == "Solving Process", "Time"].values | ||
|
||
# Make a correction of the memory usage, some APIs use external processes for the solving process | ||
api_with_external_process = set(["pyomo"]) | ||
api_with_external_process = {"pyomo"} | ||
api_with_internal_process = set(snakemake.params.apis).difference( | ||
api_with_external_process | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,7 @@ | |
|
||
# The full version, including alpha/beta/rc tags | ||
version = pkg_resources.get_distribution("linopy").version | ||
if "dev" in version: | ||
# remove the dev description and reduce by minor one release | ||
release = "master" | ||
else: | ||
release = version | ||
|
||
|
||
release = "master" if "dev" in version else version | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
This removes the following comments ( why? ):
|
||
# -- General configuration --------------------------------------------------- | ||
|
||
# Add any Sphinx extension module names here, as strings. They can be | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
Linopy module for solving lp files with different solvers. | ||
""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
|
||
import contextlib | ||
import io | ||
import logging | ||
|
@@ -30,41 +31,29 @@ | |
which = "where" if os.name == "nt" else "which" | ||
|
||
# the first available solver will be the default solver | ||
try: | ||
with contextlib.suppress(ImportError): | ||
import gurobipy | ||
|
||
available_solvers.append("gurobi") | ||
except (ModuleNotFoundError, ImportError): | ||
pass | ||
|
||
try: | ||
with contextlib.suppress(ImportError): | ||
import highspy | ||
|
||
available_solvers.append("highs") | ||
except (ModuleNotFoundError, ImportError): | ||
pass | ||
|
||
if sub.run([which, "glpsol"], stdout=sub.DEVNULL, stderr=sub.STDOUT).returncode == 0: | ||
available_solvers.append("glpk") | ||
|
||
|
||
if sub.run([which, "cbc"], stdout=sub.DEVNULL, stderr=sub.STDOUT).returncode == 0: | ||
available_solvers.append("cbc") | ||
|
||
try: | ||
with contextlib.suppress(ImportError): | ||
import cplex | ||
|
||
available_solvers.append("cplex") | ||
except (ModuleNotFoundError, ImportError): | ||
pass | ||
|
||
try: | ||
with contextlib.suppress(ImportError): | ||
import xpress | ||
|
||
available_solvers.append("xpress") | ||
except (ModuleNotFoundError, ImportError): | ||
pass | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
|
@@ -80,11 +69,9 @@ def safe_get_solution(status, func): | |
if status.is_ok: | ||
return func() | ||
elif status.status == SolverStatus.unknown: | ||
try: | ||
with contextlib.suppress(Exception): | ||
logger.warning("Solution status unknown. Trying to parse solution.") | ||
return func() | ||
except Exception: | ||
pass | ||
Comment on lines
-83
to
-87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return Solution() | ||
|
||
|
||
|
@@ -271,7 +258,7 @@ def run_glpk( | |
def read_until_break(f): | ||
while True: | ||
line = f.readline() | ||
if line == "\n" or line == "": | ||
if line in ["\n", ""]: | ||
Comment on lines
-274
to
+261
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
break | ||
yield line | ||
|
||
|
@@ -462,11 +449,8 @@ def run_cplex( | |
|
||
is_lp = m.problem_type[m.get_problem_type()] == "LP" | ||
|
||
try: | ||
with contextlib.suppress(cplex.exceptions.errors.CplexSolverError): | ||
m.solve() | ||
except cplex.exceptions.errors.CplexSolverError as e: | ||
pass | ||
|
||
Comment on lines
-465
to
-469
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
condition = m.solution.get_status_string() | ||
termination_condition = CONDITION_MAP.get(condition, condition) | ||
status = Status.from_termination_condition(termination_condition) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,10 +166,7 @@ def test_anonymous_constraint_sel(x, y): | |
|
||
def test_constraint_from_rule(m, x, y): | ||
def bound(m, i, j): | ||
if i % 2: | ||
return (i - 1) * x[i - 1] + y[j] >= 0 | ||
else: | ||
return i * x[i] >= 0 | ||
return (i - 1) * x[i - 1] + y[j] >= 0 if i % 2 else i * x[i] >= 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
coords = [x.coords["first"], y.coords["second"]] | ||
con = Constraint.from_rule(m, bound, coords) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,10 +207,7 @@ def test_linear_expression_with_errors(m, x): | |
|
||
def test_linear_expression_from_rule(m, x, y): | ||
def bound(m, i): | ||
if i == 1: | ||
return (i - 1) * x[i - 1] + y[i] + 1 * x[i] | ||
else: | ||
return i * x[i] - y[i] | ||
return (i - 1) * x[i - 1] + y[i] + 1 * x[i] if i == 1 else i * x[i] - y[i] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
expr = LinearExpression.from_rule(m, bound, x.coords) | ||
assert isinstance(expr, LinearExpression) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,13 +83,13 @@ def test_variable_repr(): | |
|
||
def test_scalar_variable_repr(): | ||
for var in [u, v, x, y, z, a, b, c, d]: | ||
coord = tuple([var.indexes[c][0] for c in var.dims]) | ||
coord = tuple(var.indexes[c][0] for c in var.dims) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
repr(var[coord]) | ||
|
||
|
||
def test_single_variable_repr(): | ||
for var in [u, v, x, y, z, a, b, c, d]: | ||
coord = tuple([var.indexes[c][0] for c in var.dims]) | ||
coord = tuple(var.indexes[c][0] for c in var.dims) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
repr(var.loc[coord]) | ||
|
||
|
||
|
@@ -104,13 +104,13 @@ def test_linear_expression_long(): | |
|
||
def test_scalar_linear_expression_repr(): | ||
for var in [u, v, x, y, z, a, b, c, d]: | ||
coord = tuple([var.indexes[c][0] for c in var.dims]) | ||
coord = tuple(var.indexes[c][0] for c in var.dims) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
repr(1 * var[coord]) | ||
|
||
|
||
def test_single_linear_repr(): | ||
for var in [u, v, x, y, z, a, b, c, d]: | ||
coord = tuple([var.indexes[c][0] for c in var.dims]) | ||
coord = tuple(var.indexes[c][0] for c in var.dims) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
repr(1 * var.loc[coord]) | ||
|
||
|
||
|
@@ -120,13 +120,13 @@ def test_anonymous_constraint_repr(): | |
|
||
|
||
def test_scalar_constraint_repr(): | ||
repr(1 * u[0, 0] >= 0) | ||
repr(u[0, 0] >= 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def test_single_constraint_repr(): | ||
for var in [u, v, x, y, z, a, b, c, d]: | ||
coord = tuple([var.indexes[c][0] for c in var.dims]) | ||
repr(1 * var.loc[coord] == 0) | ||
coord = tuple(var.indexes[c][0] for c in var.dims) | ||
repr(var.loc[coord] == 0) | ||
Comment on lines
-128
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
repr(1 * var.loc[coord] - var.loc[coord] == 0) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,7 @@ def test_variable_getter(x, z): | |
x[0, 0] | ||
|
||
with pytest.raises(AssertionError): | ||
x[0:5] | ||
x[:5] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
with pytest.raises(AssertionError): | ||
x[[1, 2, 3]] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
knapsack_model
refactored with the following changes:dict-comprehension
)This removes the following comments ( why? ):