-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated the remaining generics from biocgenerics.
- Loading branch information
Showing
7 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |