Skip to content

Commit

Permalink
Relaxed combine generics (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche authored Dec 27, 2023
1 parent 05bb412 commit 5ae14e6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/biocutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
from .combine_columns import combine_columns
from .combine_sequences import combine_sequences

from .relaxed_combine_columns import relaxed_combine_columns
from .relaxed_combine_rows import relaxed_combine_rows

from .extract_row_names import extract_row_names
from .extract_column_names import extract_column_names

Expand All @@ -51,4 +54,4 @@
from .convert_to_dense import convert_to_dense

from .get_height import get_height
from .is_high_dimensional import is_high_dimensional
from .is_high_dimensional import is_high_dimensional
35 changes: 35 additions & 0 deletions src/biocutils/relaxed_combine_columns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from functools import singledispatch
from typing import Any

from .package_utils import is_package_installed

__author__ = "jkanche"
__copyright__ = "jkanche"
__license__ = "MIT"


@singledispatch
def relaxed_combine_columns(*x: Any):
"""Combine n-dimensional objects along the second dimension.
Args:
x:
n-dimensional objects to combine. All elements of x are expected
to be the same class.
Returns:
Combined object, typically the same type as the first entry of ``x``
"""
raise NotImplementedError(
"no `combine_columns` method implemented for '"
+ type(x[0]).__name__
+ "' objects."
)


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

@relaxed_combine_columns.register(DataFrame)
def _relaxed_combine_columns_pandas_dataframe(*x):
return concat(x, axis=1)
33 changes: 33 additions & 0 deletions src/biocutils/relaxed_combine_rows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from functools import singledispatch
from typing import Any

from .package_utils import is_package_installed

__author__ = "jkanche"
__copyright__ = "jkanche"
__license__ = "MIT"


@singledispatch
def relaxed_combine_rows(*x: Any):
"""Combine n-dimensional objects along their first dimension.
Args:
x:
One or more n-dimensional objects to combine. All elements of x
are expected to be the same class.
Returns:
Combined object, typically the same type as the first entry of ``x``.
"""
raise NotImplementedError(
"no `combine_rows` method implemented for '" + type(x[0]).__name__ + "' objects."
)


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

@relaxed_combine_rows.register(DataFrame)
def _relaxed_combine_rows_pandas_dataframe(*x):
return concat(x, axis=0)

0 comments on commit 5ae14e6

Please sign in to comment.