diff --git a/src/biocutils/__init__.py b/src/biocutils/__init__.py index 9801f3d..b12c56a 100644 --- a/src/biocutils/__init__.py +++ b/src/biocutils/__init__.py @@ -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 @@ -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 \ No newline at end of file diff --git a/src/biocutils/relaxed_combine_columns.py b/src/biocutils/relaxed_combine_columns.py new file mode 100644 index 0000000..a4e2747 --- /dev/null +++ b/src/biocutils/relaxed_combine_columns.py @@ -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) diff --git a/src/biocutils/relaxed_combine_rows.py b/src/biocutils/relaxed_combine_rows.py new file mode 100644 index 0000000..2dc634b --- /dev/null +++ b/src/biocutils/relaxed_combine_rows.py @@ -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)