Skip to content

Commit

Permalink
Implement generics for rownames and colnames (#46)
Browse files Browse the repository at this point in the history
Add tests as well
  • Loading branch information
jkanche authored Oct 17, 2023
1 parent 1f2d4c1 commit 10c81ef
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/biocframe/BiocFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
from typing import Any, Dict, List, Optional, Tuple, Union
from warnings import warn

from biocgenerics.colnames import colnames as colnames_generic
from biocgenerics.colnames import set_colnames
from biocgenerics.combine import combine
from biocgenerics.combine_cols import combine_cols
from biocgenerics.combine_rows import combine_rows
from biocgenerics.rownames import rownames as rownames_generic
from biocgenerics.rownames import set_rownames
from biocutils import is_list_of_type

from ._validators import validate_cols, validate_rows, validate_unique_list
Expand Down Expand Up @@ -908,3 +912,23 @@ def _combine_cols_bframes(*x: BiocFrame):
raise NotImplementedError(
"`combine_cols` is not implemented for `BiocFrame` objects."
)


@rownames_generic.register(BiocFrame)
def _rownames_bframe(x: BiocFrame):
return x.row_names


@set_rownames.register(BiocFrame)
def _set_rownames_bframe(x: BiocFrame, names: List[str]):
x.row_names = names


@colnames_generic.register(BiocFrame)
def _colnames_bframe(x: BiocFrame):
return x.column_names


@set_colnames.register(BiocFrame)
def _set_colnames_bframe(x: BiocFrame, names: List[str]):
x.column_names = names
63 changes: 63 additions & 0 deletions tests/test_names_generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from biocgenerics import rownames, colnames, set_colnames, set_rownames
from biocframe.BiocFrame import BiocFrame

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


def test_names():
obj = BiocFrame(
{
"column1": [1, 2, 3],
"nested": [
{
"ncol1": [4, 5, 6],
"ncol2": ["a", "b", "c"],
"deep": {"dcol1": ["j", "k", "l"], "dcol2": ["a", "s", "l"]},
},
{
"ncol2": ["a"],
"deep": {"dcol1": ["j"], "dcol2": ["a"]},
},
{
"ncol1": [5, 6],
"ncol2": ["b", "c"],
},
],
"column2": ["b", "n", "m"],
}
)

assert rownames(obj) == None
assert colnames(obj) == ["column1", "nested", "column2"]


def test_set_names():
obj = BiocFrame(
{
"column1": [1, 2, 3],
"nested": [
{
"ncol1": [4, 5, 6],
"ncol2": ["a", "b", "c"],
"deep": {"dcol1": ["j", "k", "l"], "dcol2": ["a", "s", "l"]},
},
{
"ncol2": ["a"],
"deep": {"dcol1": ["j"], "dcol2": ["a"]},
},
{
"ncol1": [5, 6],
"ncol2": ["b", "c"],
},
],
"column2": ["b", "n", "m"],
}
)

set_rownames(obj, names=["row1", "row2", "row3"])
set_colnames(obj, names=["col1", "nn", "col2"])

assert rownames(obj) == ["row1", "row2", "row3"]
assert colnames(obj) == ["col1", "nn", "col2"]

0 comments on commit 10c81ef

Please sign in to comment.