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

type sparse.py, enable mypy in CI #773

Merged
merged 26 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 25 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
6 changes: 5 additions & 1 deletion .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Update pip
- name: Update pip, install dev deps
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
- name: Code Quality
run: |
python -m pip install black
black pulp/ --check
- name: Type Check
run: |
mypy ./
- name: Install dependencies
run: |
python -m pip install .
Expand Down
42 changes: 0 additions & 42 deletions .travis.yml

This file was deleted.

46 changes: 32 additions & 14 deletions pulp/sparse.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Dict, Generic, List, Tuple, TypeVar, cast

# Sparse : Python basic dictionary sparse matrix

# Copyright (c) 2007, Stuart Mitchell (s.mitchell@auckland.ac.nz)
Expand Down Expand Up @@ -27,22 +29,31 @@
notably this allows the sparse matrix to be output in various formats
"""

T = TypeVar("T")


class Matrix(dict):
class Matrix(Generic[T], Dict[Tuple[int, int], T]):
"""This is a dictionary based sparse matrix class"""

def __init__(self, rows, cols):
def __init__(self, rows: List[int], cols: List[int]):
"""initialises the class by creating a matrix that will have the given
rows and columns
"""
self.rows = rows
self.cols = cols
self.rowdict = {row: {} for row in rows}
self.coldict = {col: {} for col in cols}
self.rows: List[int] = rows
self.cols: List[int] = cols
self.rowdict: Dict[int, Dict[int, T]] = {row: {} for row in rows}
self.coldict: Dict[int, Dict[int, T]] = {col: {} for col in cols}

def add(self, row, col, item, colcheck=False, rowcheck=False):
if not (rowcheck and row not in self.rows):
if not (colcheck and col not in self.cols):
def add(
self,
row: int,
col: int,
item: T,
colcheck: bool = False,
rowcheck: bool = False,
) -> None:
if not rowcheck or row in self.rows:
if not colcheck or col in self.cols:
dict.__setitem__(self, (row, col), item)
self.rowdict[row][col] = item
self.coldict[col][row] = item
Expand All @@ -52,20 +63,26 @@ def add(self, row, col, item, colcheck=False, rowcheck=False):
else:
raise RuntimeError(f"row {row} is not in the matrix rows")

def addcol(self, col, rowitems):
def addcol(self, col: int, rowitems: Dict[int, T]) -> None:
"""adds a column"""
if col in self.cols:
for row, item in rowitems.items():
self.add(row, col, item, colcheck=False)
else:
raise RuntimeError("col is not in the matrix columns")

def get(self, k, d=0):
return dict.get(self, k, d)
def get( # type: ignore[override]
self,
coords: Tuple[int, int],
default: T = 0, # type: ignore[assignment]
) -> T:
return dict.get(self, coords, default)

def col_based_arrays(self):
def col_based_arrays(
self,
) -> Tuple[int, List[int], List[int], List[int], List[T]]:
numEls = len(self)
elemBase = []
elemBase: List[T] = []
startsBase = []
indBase = []
lenBase = []
Expand All @@ -74,5 +91,6 @@ def col_based_arrays(self):
elemBase.extend(list(self.coldict[col].values()))
indBase.extend(list(self.coldict[col].keys()))
lenBase.append(len(elemBase) - startsBase[-1])

startsBase.append(len(elemBase))
return numEls, startsBase, lenBase, indBase, elemBase
2 changes: 0 additions & 2 deletions pulp/tests/test_pulp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,6 @@ def add_const(prob):

@gurobi_test
def test_measuring_solving_time(self):

time_limit = 10
solver_settings = dict(
PULP_CBC_CMD=30,
Expand Down Expand Up @@ -1256,7 +1255,6 @@ def test_measuring_solving_time(self):

@gurobi_test
def test_time_limit_no_solution(self):

time_limit = 1
solver_settings = dict(HiGHS_CMD=60, HiGHS=60, PULP_CBC_CMD=60, COIN_CMD=60)
bins = solver_settings.get(self.solver.name)
Expand Down
40 changes: 38 additions & 2 deletions pulp/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@


class SparseTest(unittest.TestCase):
def test_sparse(self):
def test_sparse(self) -> None:
rows = list(range(10))
cols = list(range(50, 60))
mat = Matrix(rows, cols)
mat = Matrix[str](rows, cols)
mat.add(1, 52, "item")
mat.add(2, 54, "stuff")
assert mat.col_based_arrays() == (
Expand All @@ -17,3 +17,39 @@ def test_sparse(self):
[1, 2],
["item", "stuff"],
)

assert mat.get((1, 52)) == "item"

mat.addcol(51, {2: "hello"})
assert mat.col_based_arrays() == (
3,
[0, 0, 1, 2, 2, 3, 3, 3, 3, 3, 3],
[0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[2, 1, 2],
["hello", "item", "stuff"],
)

def test_sparse_floats(self) -> None:
rows = list(range(10))
cols = list(range(50, 60))
mat = Matrix[float](rows, cols)
mat.add(1, 52, 1.234)
mat.add(2, 54, 5.678)
assert mat.col_based_arrays() == (
2,
[0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 2],
[1.234, 5.678],
)

assert mat.get((1, 52)) == 1.234

mat.addcol(51, {2: 9.876})
assert mat.col_based_arrays() == (
3,
[0, 0, 1, 2, 2, 3, 3, 3, 3, 3, 3],
[0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[2, 1, 2],
[9.876, 1.234, 5.678],
)
70 changes: 70 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
[tool.mypy]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just add a directory instead of each file? something like seems to work according to https://stackoverflow.com/a/78480102/6508131:

exclude = [
"examples/",
"doc",
"pulp/apis",
"pulp/solverdir",
]

exclude = [
"doc/KPyCon2009/code/beerdistribution.py",
"doc/KPyCon2009/code/wedding.py",
"doc/KPyCon2009/code/whiskas.py",
"doc/source/_static/plotter.py",
"doc/source/conf.py",
"examples/AmericanSteelProblem.py",
"examples/BeerDistributionProblem.py",
"examples/BeerDistributionProblemCompetitorExtension.py",
"examples/BeerDistributionProblemWarehouseExtension.py",
"examples/BeerDistributionProblem_resolve.py",
"examples/CG.py",
"examples/CGcolumnwise.py",
"examples/ComputerPlantProblem.py",
"examples/SpongeRollProblem1.py",
"examples/SpongeRollProblem2.py",
"examples/SpongeRollProblem3.py",
"examples/SpongeRollProblem4.py",
"examples/SpongeRollProblem5.py",
"examples/SpongeRollProblem6.py",
"examples/Sudoku1.py",
"examples/Sudoku2.py",
"examples/Two_stage_Stochastic_GemstoneTools.py",
"examples/WhiskasModel1.py",
"examples/WhiskasModel2.py",
"examples/furniture.py",
"examples/test1.py",
"examples/test2.py",
"examples/test3.py",
"examples/test4.py",
"examples/test5.py",
"examples/test6.py",
"examples/test7.py",
"examples/wedding.py",
"examples/wedding_initial.py",
"pulp/__init__.py",
"pulp/apis/__init__.py",
"pulp/apis/choco_api.py",
"pulp/apis/coin_api.py",
"pulp/apis/copt_api.py",
"pulp/apis/core.py",
"pulp/apis/cplex_api.py",
"pulp/apis/glpk_api.py",
"pulp/apis/gurobi_api.py",
"pulp/apis/highs_api.py",
"pulp/apis/mipcl_api.py",
"pulp/apis/mosek_api.py",
"pulp/apis/scip_api.py",
"pulp/apis/xpress_api.py",
"pulp/mps_lp.py",
"pulp/pulp.py",
"pulp/solverdir/cbc/linux/32",
"pulp/solverdir/cbc/linux/64",
"pulp/solverdir/cbc/osx/64",
"pulp/solverdir/cbc/win/32",
"pulp/solverdir/cbc/win/64",
"pulp/tests/bin_packing_problem.py",
"pulp/tests/run_tests.py",
"pulp/tests/test_examples.py",
"pulp/tests/test_gurobipy_env.py",
"pulp/tests/test_lpdot.py",
"pulp/tests/test_pulp.py",
"pulp/utilities.py",
"setup.py",
"venv/.*"
]
follow_imports = "silent"
strict = true
check_untyped_defs = true
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
black==23.3.0
mypy==1.4.1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please leave the versions open. That is:

black
mypy

pre-commit==2.12.0
sphinx
sphinx_rtd_theme
black
Loading