From bb5b4a9fce3e789b015db40972b77ef811f5f08a Mon Sep 17 00:00:00 2001 From: LTLA Date: Tue, 24 Oct 2023 12:21:43 -0700 Subject: [PATCH] Added all the tests and stuff. --- src/biocframe/BiocFrame.py | 10 +++------- src/biocframe/_validators.py | 5 ----- tests/test_combine.py | 27 +++++++++++++++++++++++++++ tests/test_initialize.py | 25 +++++++++++++++++++++++++ tests/test_methods.py | 25 +++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 12 deletions(-) diff --git a/src/biocframe/BiocFrame.py b/src/biocframe/BiocFrame.py index d91e1e1..077c873 100644 --- a/src/biocframe/BiocFrame.py +++ b/src/biocframe/BiocFrame.py @@ -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: @@ -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 @@ -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( diff --git a/src/biocframe/_validators.py b/src/biocframe/_validators.py index 8708aaa..e43753a 100644 --- a/src/biocframe/_validators.py +++ b/src/biocframe/_validators.py @@ -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 diff --git a/tests/test_combine.py b/tests/test_combine.py index 1691c8e..518c0e7 100644 --- a/tests/test_combine.py +++ b/tests/test_combine.py @@ -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 diff --git a/tests/test_initialize.py b/tests/test_initialize.py index c4a30bc..af83537 100644 --- a/tests/test_initialize.py +++ b/tests/test_initialize.py @@ -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" diff --git a/tests/test_methods.py b/tests/test_methods.py index f1df1e7..bcf90f5 100644 --- a/tests/test_methods.py +++ b/tests/test_methods.py @@ -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],