Skip to content

Commit

Permalink
Migrated the remaining generics from biocgenerics.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Nov 8, 2023
1 parent d828f38 commit 2d484e0
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/biocutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@
from .combine_columns import combine_columns
from .combine_sequences import combine_sequences

from .extract_row_names import extract_row_names
from .extract_column_names import extract_column_names
from .show_as_cell import show_as_cell
from .convert_to_dense import convert_to_dense
30 changes: 30 additions & 0 deletions src/biocutils/extract_column_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from functools import singledispatch
from typing import Any
import numpy

from .package_utils import is_package_installed

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


@singledispatch
def extract_column_names(x: Any) -> numpy.ndarray:
"""Access column names from 2-dimensional representations.
Args:
x: Any object.
Returns:
Array of strings containing column names.
"""
raise NotImplementedError(f"`colnames` is not supported for class: '{type(x)}'.")


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

@extract_column_names.register(DataFrame)
def _colnames_dataframe(x: DataFrame) -> list:
return numpy.array(x.columns, dtype=str)
30 changes: 30 additions & 0 deletions src/biocutils/extract_row_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from functools import singledispatch
from typing import Any
import numpy

from .package_utils import is_package_installed

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


@singledispatch
def extract_row_names(x: Any) -> numpy.ndarray:
"""Access row names from 2-dimensional representations.
Args:
x: Any object.
Returns:
Array of strings containing row names.
"""
raise NotImplementedError(f"`rownames` do not exist for class: '{type(x)}'.")


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

@extract_row_names.register(DataFrame)
def _rownames_dataframe(x: DataFrame) -> list:
return numpy.array(x.index, dtype=str)
26 changes: 26 additions & 0 deletions src/biocutils/show_as_cell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Sequence, List, Any, Optional
from functools import singledispatch


@singledispatch
def show_as_cell(x: Any, indices: Sequence[int]) -> List[str]:
"""
Show the contents of ``x`` as a cell of a table, typically for use in the
``__str__`` method of a class that contains ``x``.
Args:
x:
Any object. By default, we assume that it can be treated as
a sequence, with a valid ``__getitem__`` method for an index.
indices:
List of indices to be extracted.
Returns:
List of strings of length equal to ``indices``, containing a
string summary of each of the specified elements of ``x``.
"""
output = []
for i in indices:
output.append(str(x[i]))
return output
8 changes: 8 additions & 0 deletions tests/test_extract_column_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from biocutils import extract_column_names
import pandas
import numpy


def test_pandas_column_names():
p = pandas.DataFrame({ "A": [1,2,3,4,5], "B": ["a", "b", "c", "d", "e" ]})
assert (extract_column_names(p) == numpy.array(["A", "B"])).all()
10 changes: 10 additions & 0 deletions tests/test_extract_row_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from biocutils import extract_row_names
import pandas
import numpy


def test_pandas_row_names():
p = pandas.DataFrame({ "A": [1,2,3,4,5] })
rn = ["a", "b", "c", "d", "e" ]
p.index = rn
assert (extract_row_names(p) == numpy.array(rn)).all()
6 changes: 6 additions & 0 deletions tests/test_show_as_cell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from biocutils import show_as_cell


def test_show_as_cell():
assert show_as_cell([1, 2, 3, 4], range(4)) == ["1", "2", "3", "4"]
assert show_as_cell([1, 2, 3, 4], [1, 3]) == ["2", "4"]

0 comments on commit 2d484e0

Please sign in to comment.