Skip to content

Commit

Permalink
Added all the tests and stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Oct 24, 2023
1 parent 1519a1c commit bb5b4a9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 12 deletions.
10 changes: 3 additions & 7 deletions src/biocframe/BiocFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def _validate(self):
self._data, self._number_of_rows, self._row_names
)
self._column_names, self._data, self._mcols = validate_cols(
self._column_names, self._data
self._column_names, self._data, self._mcols
)

if self._number_of_rows is None:
Expand Down Expand Up @@ -370,17 +370,13 @@ def mcols(self) -> Union[None, "BiocFrame"]:
# TODO: need to attach row names.
return self._mcols

@mcols.setter
def mcols(self, mcols: Union[None, "BiocFrame"]):
if mcols is not None:
if mcols.shape[0] != self.shape[1]:
raise ValueError(
"Number of rows in `mcols` should be equal to the number of columns."
)
if mcols.row_names is not None:
if mcols.row_names != column_names:
raise ValueError(
"Row names of `mcols` should be equal to the column names."
)
self._mcols = mcols

@property
Expand Down Expand Up @@ -544,7 +540,7 @@ def _slice(
mcols = self._mcols
if mcols is not None:
if column_indices_or_names is not None:
mcols = mcols._slice(new_column_indices)
mcols = mcols._slice(new_column_indices, None)

current_class_const = type(self)
return current_class_const(
Expand Down
5 changes: 0 additions & 5 deletions src/biocframe/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ def validate_cols(
raise ValueError(
"Number of rows in `mcols` should be equal to the number of columns."
)
if mcols.row_names is not None:
if mcols.row_names != column_names:
raise ValueError(
"Row names of `mcols` should be equal to the column names."
)

return column_names, data, mcols

Expand Down
27 changes: 27 additions & 0 deletions tests/test_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,30 @@ def test_combine_generic_preserve_types():
assert isinstance(merged, BiocFrame)
assert isinstance(merged.column("odd"), np.ndarray)
assert isinstance(merged.column("even"), list)


def test_combine_with_extras():
obj1 = BiocFrame(
{
"column1": [1, 2, 3],
"column2": [4, 5, 6],
},
mcols = BiocFrame(
{
"foo": [ -1, -2 ],
"bar": [ "A", "B" ]
}
),
metadata = { "YAY": 2 }
)

obj2 = BiocFrame(
{
"column1": [1, 2, 3],
"column2": [4, 5, 6],
},
)

merged = combine(obj1, obj2)
assert merged.metadata == obj1.metadata
assert merged.mcols.shape == obj1.mcols.shape
25 changes: 25 additions & 0 deletions tests/test_initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,28 @@ def test_nested_biocFrame():
assert isinstance(nested_col, BiocFrame)
assert nested_col.row_names is None
assert len(nested_col.column_names) == 3


def test_extra_bits():
bframe = BiocFrame(
{
"column1": [1, 2, 3],
},
mcols = BiocFrame(
{
"foo": [ 1 ],
"bar": [ "A" ]
}
),
metadata = { "YAY": 2 }
)

assert isinstance(bframe.mcols, BiocFrame)
assert bframe.metadata["YAY"] == 2

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

bframe.metadata = { "FOO": "A" }
assert bframe.metadata["FOO"] == "A"
25 changes: 25 additions & 0 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,31 @@ def test_bframe_slice():
assert sliced_list.dims == (2, 2)


def test_bframe_slice_with_extras():
bframe = BiocFrame(
{
"column1": [1, 2, 3],
"column2": [4, 5, 6],
},
mcols = BiocFrame(
{
"foo": [ -1, -2 ],
"bar": [ "A", "B" ]
}
),
metadata = { "YAY": 2 }
)

subframe = bframe[0:2,:]
assert subframe.mcols.shape[0] == bframe.mcols.shape[1]
assert subframe.metadata == bframe.metadata

subframe = bframe[:,[1]]
assert subframe.mcols.shape[0] == 1
assert subframe.mcols.column("foo") == [-2]
assert subframe.metadata == bframe.metadata


def test_bframe_unary_slice():
obj = {
"column1": [1, 2, 3],
Expand Down

0 comments on commit bb5b4a9

Please sign in to comment.