Skip to content
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

chore: replace black, isort, absolufy-imports with ruff #328

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
756928f89df774f79e28380640c3cf45865b5282
8 changes: 0 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,6 @@ jobs:
run: |
python -m pip install "$(ls dist/*.whl)[dev,solvers]"

- name: Lint with flake8
run: |
pip install flake8
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude=benchmark/scripts,.git/
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

- name: Test with pytest
env:
MOSEKLM_LICENSE_FILE: ${{ secrets.MSK_LICENSE }}
Expand Down
19 changes: 6 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-merge-conflict
- repo: https://github.com/psf/black
rev: 24.4.2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.2
hooks:
- id: black
- id: ruff
args: [--fix]
- id: ruff-format
# Current docformatter version has a problem with urls (it's annoying)
# - repo: https://github.com/PyCQA/docformatter
# rev: v1.6.0.rc1
Expand All @@ -18,16 +20,7 @@ repos:
- repo: https://github.com/keewis/blackdoc
rev: v0.3.9
hooks:
- id: blackdoc
- repo: https://github.com/MarcoGorelli/absolufy-imports
rev: v0.3.1
hooks:
- id: absolufy-imports
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
args: ["--profile", "black", "--filter-files"]
- id: blackdoc
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
hooks:
Expand Down
8 changes: 4 additions & 4 deletions benchmark/notebooks/plot-benchmarks.py.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"sns.set_theme(\"paper\", \"white\")\n",
"#plt.rc('text', usetex=True)\n",
"#plt.rc('font', family='sans-serif')"
Expand All @@ -37,7 +37,7 @@
"source": [
"if snakemake.wildcards[\"kind\"] == 'overhead':\n",
" labels = ['Overhead time (s)', 'Overhead memory (MB)']\n",
"else: \n",
"else:\n",
" labels = ['Time (s)', 'Memory (MB)']\n",
"\n",
"g = sns.FacetGrid(data=df, row=\"kind\", sharey=False, height=2., aspect=2)\n",
Expand All @@ -61,7 +61,7 @@
"source": [
"if snakemake.wildcards[\"kind\"] == 'overhead':\n",
" label = 'Computational overhead [MBs]'\n",
"else: \n",
"else:\n",
" label = 'Computational resource [MBs]'\n",
"\n",
"df = data.assign(Resource = data[\"Time\"] * data[\"Memory\"])\n",
Expand Down
8 changes: 4 additions & 4 deletions benchmark/scripts/benchmark_cvxpy.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import cvxpy as cp
import numpy as np
from common import profile
from numpy.random import randint, seed
from numpy.random import default_rng

# Random seed for reproducibility
seed(125)
rng = default_rng(125)
afuetterer marked this conversation as resolved.
Show resolved Hide resolved


def basic_model(n, solver):
Expand All @@ -26,8 +26,8 @@ def basic_model(n, solver):

def knapsack_model(n, solver):
# Define the variables
weight = randint(1, 100, size=n)
value = randint(1, 100, size=n)
weight = rng.integers(1, 100, size=n)
value = rng.integers(1, 100, size=n)

x = cp.Variable(n, boolean=True)

Expand Down
8 changes: 4 additions & 4 deletions benchmark/scripts/benchmark_gurobipy.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import gurobipy as gp
import numpy as np
from common import profile
from numpy.random import randint, seed
from numpy.random import default_rng

# Random seed for reproducibility
seed(125)
rng = default_rng(125)


def basic_model(n, solver):
Expand Down Expand Up @@ -32,8 +32,8 @@ def knapsack_model(n, solver):
# Create a new model
m = gp.Model()

weight = randint(1, 100, size=n)
value = randint(1, 100, size=n)
weight = rng.integers(1, 100, size=n)
value = rng.integers(1, 100, size=n)

# Create variables
x = m.addMVar(n, vtype=gp.GRB.BINARY, name="x")
Expand Down
10 changes: 4 additions & 6 deletions benchmark/scripts/benchmark_linopy.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 17:40:33 2021.

@author: fabian
"""


from common import profile
from numpy import arange
from numpy.random import randint, seed
from numpy.random import default_rng

from linopy import Model

# Random seed for reproducibility
seed(125)
rng = default_rng(125)


def basic_model(n, solver):
Expand All @@ -33,8 +31,8 @@ def basic_model(n, solver):
def knapsack_model(n, solver):
m = Model()
packages = m.add_variables(coords=[arange(n)], binary=True)
weight = randint(1, 100, size=n)
value = randint(1, 100, size=n)
weight = rng.integers(1, 100, size=n)
value = rng.integers(1, 100, size=n)
m.add_constraints((weight * packages).sum() <= 200)
m.add_objective(-(value * packages).sum()) # use minus because of minimization
m.solve(solver_name=solver)
Expand Down
8 changes: 4 additions & 4 deletions benchmark/scripts/benchmark_ortools.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from common import profile
from numpy.random import randint, seed
from numpy.random import default_rng
from ortools.linear_solver import pywraplp

# Random seed for reproducibility
seed(125)
rng = default_rng(125)


def model(n, solver):
Expand Down Expand Up @@ -41,8 +41,8 @@ def knapsack_model(n, solver):
# Create a new linear solver
solver = pywraplp.Solver("LinearExample", pywraplp.Solver.GUROBI_LINEAR_PROGRAMMING)

weight = randint(1, 100, size=n)
value = randint(1, 100, size=n)
weight = rng.integers(1, 100, size=n)
value = rng.integers(1, 100, size=n)

x = {i: solver.BoolVar("x_%d" % i) for i in range(n)}
# Create constraints
Expand Down
9 changes: 4 additions & 5 deletions benchmark/scripts/benchmark_pulp.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import pulp
from common import profile
from numpy import arange
from numpy.random import randint, seed
from numpy.random import default_rng

# Random seed for reproducibility
seed(125)
rng = default_rng(125)


def basic_model(n, solver):
Expand Down Expand Up @@ -34,8 +33,8 @@ def knapsack_model(n, solver):
m.i = list(range(n))

# Define the variables
weight = randint(1, 100, size=n)
value = randint(1, 100, size=n)
weight = rng.integers(1, 100, size=n)
value = rng.integers(1, 100, size=n)

x = pulp.LpVariable.dicts("x", (m.i,), lowBound=0, upBound=1, cat=pulp.LpInteger)

Expand Down
11 changes: 4 additions & 7 deletions benchmark/scripts/benchmark_pyomo.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 17:40:33 2021.

@author: fabian
"""


from common import profile
from numpy import arange
from numpy.random import randint, seed
from numpy.random import default_rng
from pyomo.environ import (
Binary,
ConcreteModel,
Expand All @@ -18,12 +16,11 @@
Set,
Var,
maximize,
value,
)
from pyomo.opt import SolverFactory

# Random seed for reproducibility
seed(125)
rng = default_rng(125)


def basic_model(n, solver):
Expand Down Expand Up @@ -57,8 +54,8 @@ def knapsack_model(n, solver):
m.i = Set(initialize=arange(n))

m.x = Var(m.i, domain=Binary)
m.weight = randint(1, 100, size=n)
m.value = randint(1, 100, size=n)
m.weight = rng.integers(1, 100, size=n)
m.value = rng.integers(1, 100, size=n)

def bound1(m):
return sum(m.x[i] * m.weight[i] for i in m.i) <= 200
Expand Down
1 change: 0 additions & 1 deletion benchmark/scripts/benchmarks-pypsa-eur/benchmark-linopy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 15 16:20:51 2022.

Expand Down
3 changes: 1 addition & 2 deletions benchmark/scripts/benchmarks-pypsa-eur/benchmark-pyomo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 15 16:20:51 2022.

Expand All @@ -18,7 +17,7 @@ def solve():
n.generators.p_nom_max.fillna(np.inf, inplace=True)
n.snapshots = n.snapshots[:NSNAPSHOTS]

m = n.lopf(
m = n.lopf( # noqa: F841
solver_options=SOLVER_PARAMS, formulation="kirchhoff", solver_name=SOLVER
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 15 16:20:51 2022.

Expand All @@ -18,7 +17,7 @@ def solve():
n.generators.p_nom_max.fillna(np.inf, inplace=True)
n.snapshots = n.snapshots[:NSNAPSHOTS]

m = n.lopf(solver_options=SOLVER_PARAMS, pyomo=False, solver_name=SOLVER)
m = n.lopf(solver_options=SOLVER_PARAMS, pyomo=False, solver_name=SOLVER) # noqa: F841


solve()
1 change: 0 additions & 1 deletion benchmark/scripts/benchmarks-pypsa-eur/plot-benchmarks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 15 17:11:01 2022.

Expand Down
1 change: 0 additions & 1 deletion benchmark/scripts/concat-benchmarks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 9 09:34:00 2022.

Expand Down
5 changes: 1 addition & 4 deletions benchmark/scripts/leftovers/benchmark-linopy.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 17:40:33 2021.

@author: fabian
"""


import pandas as pd
from common import profile
from numpy import arange, cos, sin
from numpy import arange

from linopy import Model

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 15 16:20:51 2022.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 15 16:20:51 2022.

Expand All @@ -18,7 +17,7 @@ def solve():
n.generators.p_nom_max.fillna(np.inf, inplace=True)
n.snapshots = n.snapshots[:NSNAPSHOTS]

m = n.lopf(
m = n.lopf( # noqa: F841
solver_options=SOLVER_PARAMS, formulation="kirchhoff", solver_name=SOLVER
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 15 16:20:51 2022.

Expand All @@ -18,7 +17,7 @@ def solve():
n.generators.p_nom_max.fillna(np.inf, inplace=True)
n.snapshots = n.snapshots[:NSNAPSHOTS]

m = n.lopf(solver_options=SOLVER_PARAMS, pyomo=False, solver_name=SOLVER)
m = n.lopf(solver_options=SOLVER_PARAMS, pyomo=False, solver_name=SOLVER) # noqa: F841


solve()
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 15 17:11:01 2022.

Expand Down
4 changes: 0 additions & 4 deletions benchmark/scripts/merge-benchmarks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 14 18:00:44 2022.

@author: fabian
"""


from pathlib import Path

import pandas as pd

df = [pd.read_csv(fn, index_col=0) for fn in snakemake.input.benchmarks]
Expand Down
1 change: 0 additions & 1 deletion benchmark/scripts/plot-benchmarks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 26 23:37:38 2022.

Expand Down
1 change: 0 additions & 1 deletion benchmark/scripts/run-linopy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 17:40:33 2021.

Expand Down
2 changes: 0 additions & 2 deletions benchmark/scripts/run-pyomo.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 17:40:33 2021.

@author: fabian
"""


from benchmark_pyomo import basic_model, knapsack_model

if snakemake.config["benchmark"] == "basic":
Expand Down
Loading