Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
Adapt package for Python 3.8 (#10)
Browse files Browse the repository at this point in the history
- In addition updates github action to test the package from python 3.8 to 3.12. 
- Most changes deal with registering dispatchers for scipy's sparray.
  • Loading branch information
jkanche authored Oct 25, 2023
1 parent 770b87a commit c39f54a
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 103 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/pypi-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ '3.8', '3.9', '3.10', '3.11', '3.12' ]

name: Python ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.9
python-version: ${{ matrix.python-version }}
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ package_dir =
=src

# Require a min/specific Python version (comma-separated conditions)
# python_requires = >=3.8
python_requires = >=3.8

# Add here dependencies of your project (line-separated), e.g. requests>=2.2,<3.0.
# Version specifiers like >=2.2,<3.0 avoid problems due to API changes in
Expand All @@ -50,7 +50,7 @@ package_dir =
install_requires =
importlib-metadata; python_version<"3.8"
numpy
biocutils>=0.0.2
biocutils>=0.0.4

[options.packages.find]
where = src
Expand Down
6 changes: 3 additions & 3 deletions src/biocgenerics/colnames.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from functools import singledispatch
from typing import List

from .utils import _is_package_installed
from biocutils.package_utils import is_package_installed

__author__ = "jkanche"
__copyright__ = "jkanche"
Expand All @@ -27,7 +27,7 @@ def colnames(x) -> list:
raise NotImplementedError(f"`colnames` is not supported for class: '{type(x)}'.")


if _is_package_installed("pandas") is True:
if is_package_installed("pandas") is True:
from pandas import DataFrame

@colnames.register(DataFrame)
Expand All @@ -54,7 +54,7 @@ def set_colnames(x, names: List[str]):
)


if _is_package_installed("pandas") is True:
if is_package_installed("pandas") is True:
from pandas import DataFrame

@set_colnames.register(DataFrame)
Expand Down
17 changes: 12 additions & 5 deletions src/biocgenerics/combine.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from functools import singledispatch
from typing import Any

from biocutils.package_utils import is_package_installed
from numpy import ndarray

from .combine_rows import combine_rows
Expand All @@ -9,7 +10,6 @@
_is_1d_dense_arrays,
_is_1d_sparse_arrays,
_is_any_element_list,
_is_package_installed,
)

__author__ = "jkanche"
Expand Down Expand Up @@ -48,7 +48,7 @@ def _combine_dense_arrays(*x: ndarray):
return combine_rows(*x)


if _is_package_installed("scipy") is True:
if is_package_installed("scipy") is True:
import scipy.sparse as sp

def _combine_sparse(*x):
Expand All @@ -60,11 +60,18 @@ def _combine_sparse(*x):

return combine_rows(*x)

combine.register(sp.sparray, _combine_sparse)
combine.register(sp.spmatrix, _combine_sparse)
try:
combine.register(sp.sparray, _combine_sparse)
except Exception:
pass

try:
combine.register(sp.spmatrix, _combine_sparse)
except Exception:
pass

if _is_package_installed("pandas") is True:

if is_package_installed("pandas") is True:
from pandas import DataFrame, Series

@combine.register(Series)
Expand Down
75 changes: 56 additions & 19 deletions src/biocgenerics/combine_cols.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
from warnings import warn

from biocutils import is_list_of_type
from biocutils.package_utils import is_package_installed
from numpy import hstack, ndarray

from .utils import (
_convert_sparse_to_dense,
_do_arrays_match,
_is_package_installed,
)
from .utils import _convert_sparse_to_dense, _do_arrays_match

__author__ = "jkanche"
__copyright__ = "jkanche"
Expand Down Expand Up @@ -76,44 +73,84 @@ def _combine_cols_dense_arrays(*x: ndarray):
raise ValueError("All elements must be 2-dimensional matrices.")


if _is_package_installed("scipy") is True:
if is_package_installed("scipy") is True:
import scipy.sparse as sp

def _combine_cols_sparse_arrays(*x):
if is_list_of_type(x, (sp.sparray, sp.spmatrix)):
def _combine_cols_sparse_matrices(*x):
if is_list_of_type(x, sp.spmatrix):
sp_conc = sp.hstack(x)

if _do_arrays_match(x, 0) is not True:
raise ValueError("1st dimension does not match across all elements.")

first = x[0]
if isinstance(first, (sp.csr_matrix, sp.csr_array)):
if isinstance(first, sp.csr_matrix):
return sp_conc.tocsr()
elif isinstance(first, (sp.csc_matrix, sp.csc_array)):
elif isinstance(first, sp.csc_matrix):
return sp_conc.tocsc()
elif isinstance(first, (sp.bsr_matrix, sp.bsr_array)):
elif isinstance(first, sp.bsr_matrix):
return sp_conc.tobsr()
elif isinstance(first, (sp.coo_matrix, sp.coo_array)):
elif isinstance(first, sp.coo_matrix):
return sp_conc.tocoo()
elif isinstance(first, (sp.dia_matrix, sp.dia_array)):
elif isinstance(first, sp.dia_matrix):
return sp_conc.todia()
elif isinstance(first, (sp.lil_matrix, sp.lil_array)):
elif isinstance(first, sp.lil_matrix):
return sp_conc.tolil()
else:
return sp_conc

warn("Not all elements are scipy sparse arrays.")
warn("Not all elements are scipy sparse matrices.")

if is_list_of_type(x, (ndarray, sp.sparray, sp.spmatrix)):
if is_list_of_type(x, (ndarray, sp.spmatrix)):
return _generic_combine_cols_dense_sparse(x)

raise ValueError("All elements must be 2-dimensional matrices.")

combine_cols.register(sp.sparray, _combine_cols_sparse_arrays)
combine_cols.register(sp.spmatrix, _combine_cols_sparse_arrays)
try:

def _combine_cols_sparse_arrays(*x):
if is_list_of_type(x, sp.sparray):
sp_conc = sp.hstack(x)

if _is_package_installed("pandas") is True:
if _do_arrays_match(x, 0) is not True:
raise ValueError(
"1st dimension does not match across all elements."
)

first = x[0]
if isinstance(first, sp.csr_array):
return sp_conc.tocsr()
elif isinstance(first, sp.csc_array):
return sp_conc.tocsc()
elif isinstance(first, sp.bsr_array):
return sp_conc.tobsr()
elif isinstance(first, sp.coo_array):
return sp_conc.tocoo()
elif isinstance(first, sp.dia_array):
return sp_conc.todia()
elif isinstance(first, sp.lil_array):
return sp_conc.tolil()
else:
return sp_conc

warn("Not all elements are scipy sparse arrays.")

if is_list_of_type(x, (ndarray, sp.sparray, sp.spmatrix)):
return _generic_combine_cols_dense_sparse(x)

raise ValueError("All elements must be 2-dimensional arrays.")

combine_cols.register(sp.sparray, _combine_cols_sparse_arrays)
except Exception:
pass

try:
combine_cols.register(sp.spmatrix, _combine_cols_sparse_matrices)
except Exception:
pass


if is_package_installed("pandas") is True:
from pandas import DataFrame, concat

@combine_cols.register(DataFrame)
Expand Down
70 changes: 55 additions & 15 deletions src/biocgenerics/combine_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from warnings import warn

from biocutils import is_list_of_type
from biocutils.package_utils import is_package_installed
from numpy import concatenate, ndarray

from .utils import (
_convert_sparse_to_dense,
_do_arrays_match,
_is_package_installed,
)

__author__ = "jkanche"
Expand Down Expand Up @@ -76,44 +76,84 @@ def _combine_rows_dense_arrays(*x: ndarray):
raise ValueError("All elements must be 2-dimensional matrices.")


if _is_package_installed("scipy") is True:
if is_package_installed("scipy") is True:
import scipy.sparse as sp

def _combine_rows_sparse_arrays(*x):
if is_list_of_type(x, (sp.sparray, sp.spmatrix)):
def _combine_rows_sparse_matrices(*x):
if is_list_of_type(x, sp.spmatrix):
sp_conc = sp.vstack(x)

if _do_arrays_match(x, 1) is not True:
raise ValueError("2nd dimension does not match across all elements.")

first = x[0]
if isinstance(first, (sp.csr_matrix, sp.csr_array)):
if isinstance(first, sp.csr_matrix):
return sp_conc.tocsr()
elif isinstance(first, (sp.csc_matrix, sp.csc_array)):
elif isinstance(first, sp.csc_matrix):
return sp_conc.tocsc()
elif isinstance(first, (sp.bsr_matrix, sp.bsr_array)):
elif isinstance(first, sp.bsr_matrix):
return sp_conc.tobsr()
elif isinstance(first, (sp.coo_matrix, sp.coo_array)):
elif isinstance(first, sp.coo_matrix):
return sp_conc.tocoo()
elif isinstance(first, (sp.dia_matrix, sp.dia_array)):
elif isinstance(first, sp.dia_matrix):
return sp_conc.todia()
elif isinstance(first, (sp.lil_matrix, sp.lil_array)):
elif isinstance(first, sp.lil_matrix):
return sp_conc.tolil()
else:
return sp_conc

warn("Not all elements are scipy sparse arrays.")
warn("Not all elements are scipy sparse matrices.")

if is_list_of_type(x, (ndarray, sp.sparray, sp.spmatrix)):
if is_list_of_type(x, (ndarray, sp.spmatrix)):
return _generic_combine_rows_dense_sparse(x)

raise ValueError("All elements must be 2-dimensional matrices.")

combine_rows.register(sp.sparray, _combine_rows_sparse_arrays)
combine_rows.register(sp.spmatrix, _combine_rows_sparse_arrays)
try:

def _combine_rows_sparse_arrays(*x):
if is_list_of_type(x, sp.sparray):
sp_conc = sp.vstack(x)

if _is_package_installed("pandas") is True:
if _do_arrays_match(x, 1) is not True:
raise ValueError(
"2nd dimension does not match across all elements."
)

first = x[0]
if isinstance(first, sp.csr_array):
return sp_conc.tocsr()
elif isinstance(first, sp.csc_array):
return sp_conc.tocsc()
elif isinstance(first, sp.bsr_array):
return sp_conc.tobsr()
elif isinstance(first, sp.coo_array):
return sp_conc.tocoo()
elif isinstance(first, sp.dia_array):
return sp_conc.todia()
elif isinstance(first, sp.lil_array):
return sp_conc.tolil()
else:
return sp_conc

warn("Not all elements are scipy sparse arrays.")

if is_list_of_type(x, (ndarray, sp.sparray, sp.spmatrix)):
return _generic_combine_rows_dense_sparse(x)

raise ValueError("All elements must be 2-dimensional arrays.")

combine_rows.register(sp.sparray, _combine_rows_sparse_arrays)
except Exception:
pass

try:
combine_rows.register(sp.spmatrix, _combine_rows_sparse_matrices)
except Exception:
pass


if is_package_installed("pandas") is True:
from pandas import DataFrame, concat

@combine_rows.register(DataFrame)
Expand Down
Loading

0 comments on commit c39f54a

Please sign in to comment.