Skip to content

Commit

Permalink
Use Names class for row and column names, bump biocutils to 0.1.2 (#84
Browse files Browse the repository at this point in the history
).
  • Loading branch information
jkanche authored Nov 17, 2023
1 parent a2d5701 commit f0f6d4b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 67 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ python_requires = >=3.8
# For more information, check out https://semver.org/.
install_requires =
importlib-metadata; python_version<"3.8"
biocutils>=0.1.1
biocutils>=0.1.2
numpy

[options.packages.find]
Expand Down
57 changes: 29 additions & 28 deletions src/biocframe/BiocFrame.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from collections import OrderedDict
from typing import Any, Dict, List, Optional, Sequence, Tuple, Union, Literal
from warnings import warn
from copy import copy
from typing import Any, Dict, List, Literal, Optional, Sequence, Tuple, Union
from warnings import warn

import biocutils as ut
import numpy

__author__ = "jkanche"
__author__ = "Jayaram Kancherla, Aaron Lun"
__copyright__ = "jkanche"
__license__ = "MIT"

Expand Down Expand Up @@ -168,8 +169,8 @@ def __init__(
Internal use only.
"""
self._data = {} if data is None else data
if row_names is not None and not isinstance(row_names, ut.StringList):
row_names = ut.StringList(row_names)
if row_names is not None and not isinstance(row_names, ut.Names):
row_names = ut.Names(row_names)
self._row_names = row_names
self._number_of_rows = _guess_number_of_rows(
number_of_rows,
Expand All @@ -178,9 +179,9 @@ def __init__(
)

if column_names is None:
column_names = ut.StringList(self._data.keys())
elif not isinstance(column_names, ut.StringList):
column_names = ut.StringList(column_names)
column_names = ut.Names(self._data.keys())
elif not isinstance(column_names, ut.Names):
column_names = ut.Names(column_names)
self._column_names = column_names

self._metadata = {} if metadata is None else metadata
Expand Down Expand Up @@ -267,7 +268,7 @@ def __str__(self) -> str:
indices = [0, 1, 2, nr - 3, nr - 2, nr - 1]
insert_ellipsis = True

raw_floating = ut.create_floating_names(self._row_names, indices)
raw_floating = ut.create_floating_names(self._row_names.as_list(), indices)
if insert_ellipsis:
raw_floating = raw_floating[:3] + [""] + raw_floating[3:]
floating = ["", ""] + raw_floating
Expand Down Expand Up @@ -323,7 +324,7 @@ def __str__(self) -> str:
######>> Row names <<######
###########################

def get_row_names(self) -> Optional[ut.StringList]:
def get_row_names(self) -> Optional[ut.Names]:
"""
Returns:
List of row names, or None if no row names are available.
Expand Down Expand Up @@ -354,15 +355,15 @@ def set_row_names(
)
if any(x is None for x in names):
raise ValueError("`row_names` cannot contain None values.")
if not isinstance(names, ut.StringList):
names = ut.StringList(names)
if not isinstance(names, ut.Names):
names = ut.Names(names)

output = self._define_output(in_place)
output._row_names = names
return output

@property
def row_names(self) -> Optional[ut.StringList]:
def row_names(self) -> Optional[ut.Names]:
"""Alias for :py:attr:`~get_row_names`."""
return self.get_row_names()

Expand All @@ -379,7 +380,7 @@ def row_names(self, names: Optional[List]):
self.set_row_names(names, in_place=True)

@property
def rownames(self) -> Optional[ut.StringList]:
def rownames(self) -> Optional[ut.Names]:
"""Alias for :py:attr:`~get_row_names`, provided for back-compatibility."""
return self.get_row_names()

Expand Down Expand Up @@ -411,7 +412,7 @@ def data(self) -> Dict[str, Any]:
######>> Column names <<######
##############################

def get_column_names(self) -> ut.StringList:
def get_column_names(self) -> ut.Names:
"""
Returns:
A list of column names.
Expand All @@ -435,7 +436,7 @@ def set_column_names(self, names: List[str], in_place: bool = False) -> "BiocFra
if len(names) != len(self._column_names):
raise ValueError("Provided `names` does not match number of columns.")

new_names = ut.StringList()
new_names = ut.Names()
new_data = {}
for i, x in enumerate(names):
if x is None:
Expand All @@ -452,7 +453,7 @@ def set_column_names(self, names: List[str], in_place: bool = False) -> "BiocFra
return output

@property
def column_names(self) -> ut.StringList:
def column_names(self) -> ut.Names:
"""Alias for :py:attr:`~get_column_names`."""
return self.get_column_names()

Expand All @@ -469,12 +470,12 @@ def column_names(self, names: List[str]):
self.set_column_names(names, in_place=True)

@property
def colnames(self) -> ut.StringList:
def colnames(self) -> ut.Names:
"""Alias for :py:attr:`~get_column_names`, provided for back-compatibility only."""
return self.get_column_names()

@colnames.setter
def colnames(self, names: ut.StringList):
def colnames(self, names: ut.Names):
"""Alias for :py:attr:`~set_column_names` with ``in_place = True``, provided for back-compatibility only.
As this mutates the original object, a warning is raised.
Expand Down Expand Up @@ -699,7 +700,7 @@ def get_slice(
"""
new_column_names = self._column_names
if columns != slice(None):
new_column_indices, is_col_scalar = ut.normalize_subscript(
new_column_indices, _ = ut.normalize_subscript(
columns, len(new_column_names), new_column_names
)
new_column_names = ut.subset_sequence(new_column_names, new_column_indices)
Expand All @@ -712,7 +713,7 @@ def get_slice(
new_number_of_rows = self.shape[0]
if rows != slice(None):
new_row_names = self.row_names
new_row_indices, is_row_scalar = ut.normalize_subscript(
new_row_indices, _ = ut.normalize_subscript(
rows, self.shape[0], new_row_names
)

Expand Down Expand Up @@ -864,11 +865,11 @@ def set_slice(
if not in_place:
output._data = copy(output._data)

row_idx, scalar = ut.normalize_subscript(
row_idx, _ = ut.normalize_subscript(
rows, output.shape[0], names=output._row_names
)

col_idx, scalar = ut.normalize_subscript(
col_idx, _ = ut.normalize_subscript(
columns, output.shape[1], names=output._column_names
)

Expand Down Expand Up @@ -1092,12 +1093,12 @@ def copy(self):
################################

@property
def index(self) -> Optional[ut.StringList]:
def index(self) -> Optional[ut.Names]:
"""Alias to :py:attr:`~get_row_names`, provided for compatibility with **pandas**."""
return self.get_row_names()

@property
def columns(self) -> ut.StringList:
def columns(self) -> ut.Names:
"""Alias for :py:attr:`~get_column_names`, provided for compatibility with **pandas**."""
return self.get_column_names()

Expand Down Expand Up @@ -1245,7 +1246,7 @@ def _combine_cols_bframes(*x: BiocFrame):

first = x[0]
first_nr = first.shape[0]
all_column_names = ut.StringList()
all_column_names = ut.Names()
all_data = {}
all_column_data = []
for df in x:
Expand Down Expand Up @@ -1412,9 +1413,9 @@ def _normalize_merge_key_to_index(x, i, by):
else:
return by
elif isinstance(by, str):
ib = x[i]._column_names.index(by)
ib = x[i]._column_names.map(by)
if ib < 0:
raise ValueError("No key column '" + b + "' in object " + str(i) + ".")
raise ValueError("No key column '" + by + "' in object " + str(i) + ".")
return ib
else:
raise TypeError(
Expand Down
10 changes: 5 additions & 5 deletions tests/test_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest

from biocframe import BiocFrame, relaxed_combine_rows
from biocutils import combine, combine_columns, StringList
from biocutils import combine, combine_columns, Names

__author__ = "jkanche"
__copyright__ = "jkanche"
Expand Down Expand Up @@ -56,7 +56,7 @@ def test_with_rownames():
merged = combine(obj1, obj2)

assert isinstance(merged, BiocFrame)
assert isinstance(merged.get_row_names(), StringList)
assert isinstance(merged.get_row_names(), Names)
assert len(merged.row_names) == 10
assert merged.shape[0] == 10
assert merged.shape[1] == 2
Expand All @@ -67,7 +67,7 @@ def test_with_rownames():
assert isinstance(merged, BiocFrame)
assert merged.row_names is not None
assert len(merged.row_names) == 10
assert merged.row_names == [""] * 5 + obj2.row_names
assert merged.row_names.as_list() == [""] * 5 + obj2.row_names.as_list()
assert merged.shape[0] == 10
assert merged.shape[1] == 2

Expand Down Expand Up @@ -134,7 +134,7 @@ def test_relaxed_combine_rows():
)

merged = relaxed_combine_rows(obj1, obj2, obj3)
assert merged.get_column_names() == ["column1", "column2", "column3"]
assert merged.get_column_names().as_list() == ["column1", "column2", "column3"]
assert merged.column("column1") == [1, 2, 3, -1, -2, -3, None, None, None]
assert (
merged.column("column2").mask
Expand Down Expand Up @@ -173,7 +173,7 @@ def test_combine_columns_basic():

merged = combine_columns(obj1, obj2)
assert isinstance(merged, BiocFrame)
assert merged.get_column_names() == ["odd", "even", "foo", "bar"]
assert merged.get_column_names().as_list() == ["odd", "even", "foo", "bar"]
assert merged.get_column("odd") == [1, 3, 5, 7, 9]
assert merged.get_column("bar") == [0, 22, 44, 66, 88]

Expand Down
7 changes: 3 additions & 4 deletions tests/test_initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import biocframe
from biocframe import BiocFrame
from biocutils import StringList
from biocutils import Names

__author__ = "jkanche"
__copyright__ = "jkanche"
Expand Down Expand Up @@ -33,7 +33,7 @@ def test_initialize_obj():

bframe = BiocFrame(obj)
assert bframe is not None
assert isinstance(bframe.get_column_names(), StringList)
assert isinstance(bframe.get_column_names(), Names)


def test_initialize_pandas():
Expand Down Expand Up @@ -133,7 +133,7 @@ def test_extra_bits():

# Setters work correctly.
bframe.column_data = BiocFrame({"STUFF": [2.5]})
assert bframe.column_data.column_names == ["STUFF"]
assert list(bframe.column_data.column_names) == ["STUFF"]

bframe.metadata = {"FOO": "A"}
assert bframe.metadata["FOO"] == "A"
Expand Down Expand Up @@ -163,5 +163,4 @@ def test_with_add_deletions():
# lets delete
del obj1["new_column"]
assert obj1.shape == (3, 2)
print(obj1.column_data)
assert len(obj1.column_data) == 2
Loading

0 comments on commit f0f6d4b

Please sign in to comment.