Skip to content

Commit

Permalink
unit tests for 2d objs
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoarbitrio committed Jul 3, 2024
1 parent b475ccd commit 2a63c29
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 0 deletions.
193 changes: 193 additions & 0 deletions tests/unit/matrix/test_cubemeasure.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
_CatXArrCubeCounts,
_CatXCatCubeCounts,
_CatXCatCubeMeans,
_CatXCatCubeMedians,
_CatXCatCubeStdDev,
_CatXCatCubeSums,
_CatXCatUnconditionalCubeCounts,
_CatXMrCubeCounts,
_CatXMrCubeMeans,
_CatXMrCubeMedians,
_CatXMrCubeStdDev,
_CatXMrCubeSums,
_CatXMrUnconditionalCubeCounts,
_MrXCatCubeMedians,
_MrXMrCubeMedians,
CubeMeasures,
_MrXArrCubeCounts,
_MrXCatCubeCounts,
Expand Down Expand Up @@ -1238,6 +1242,195 @@ def raw_means(self):
)


# === MEDIANS ===


class Describe_BaseCubeMedians:
"""Unit test suite for `cr.cube.matrix.cubemeasure._BaseCubeMedians`."""

@pytest.mark.parametrize(
"dimension_types, CubeMediansCls",
(
((DT.MR, DT.MR), _MrXMrCubeMedians),
((DT.MR, DT.CAT), _MrXCatCubeMedians),
((DT.CAT, DT.MR), _CatXMrCubeMedians),
((DT.CAT, DT.CAT), _CatXCatCubeMedians),
),
)
def it_provides_a_factory_for_constructing_cube_medians_objects(
self, request, dimension_types, CubeMediansCls
):
cube_ = instance_mock(request, Cube)
dimensions_ = (
instance_mock(request, Dimension),
instance_mock(request, Dimension),
)
cube_medians_ = instance_mock(request, CubeMediansCls)
CubeMediansCls_ = class_mock(
request,
"cr.cube.matrix.cubemeasure.%s" % CubeMediansCls.__name__,
return_value=cube_medians_,
)
_slice_idx_expr_ = method_mock(
request,
_BaseCubeMedians,
"_slice_idx_expr",
return_value=1,
autospec=False,
)
cube_.dimension_types = dimension_types
cube_.medians = [[1, 2], [3, 4]]

cube_medians = _BaseCubeMedians.factory(cube_, dimensions_, slice_idx=7)

_slice_idx_expr_.assert_called_once_with(cube_, 7)
CubeMediansCls_.assert_called_once_with(dimensions_, [3, 4])
assert cube_medians is cube_medians_

def but_it_raises_a_value_error_when_cube_result_does_not_contain_mean_measure(
self, cube_
):
cube_.means = None

with pytest.raises(ValueError) as e:
_BaseCubeMeans.factory(cube_, None, None)

assert str(e.value) == "cube-result does not contain cube-means measure"

# fixture components ---------------------------------------------

@pytest.fixture
def cube_(self, request):
return instance_mock(request, Cube)


class Describe_CatXCatCubeMedians:
"""Unit test suite for `cr.cube.matrix.cubemeasure._CatXCatCubeMedians`."""

def it_knows_its_medians(self):
raw_medians = np.array(
[
[1.1, 2.3, 3.3],
[3.4, 1.5, 1.6],
]
)
cube_medians = _CatXCatCubeMedians(None, raw_medians)

assert cube_medians.medians.tolist() == [
[1.1, 2.3, 3.3],
[3.4, 1.5, 1.6],
]


class Describe_CatXMrCubeMedians:
"""Unit test suite for `cr.cube.matrix.cubemeasure._CatXMrCubeMedians`."""

def it_knows_its_means(self, raw_medians):
cube_medians = _CatXMrCubeMedians(None, raw_medians)

assert cube_medians.medians.tolist() == [
[1.1, 2.2, 3.2],
[4.3, 5.1, 6.1],
]

# fixtures -------------------------------------------------------

@pytest.fixture
def raw_medians(self):
"""(2, 3, 2) np.float64 ndarray of medians as received from Cube."""
return np.array(
[ # -- axes are (rows, cols, sel/not) --
# --sel/not--
[ # -- row 0 ------------
[1.1, 6.1], # -- col 0 --
[2.2, 5.2], # -- col 1 --
[3.2, 4.2], # -- col 2 --
],
[ # -- row 1 ------------
[4.3, 3.1], # -- col 0 --
[5.1, 2.1], # -- col 1 --
[6.1, 1.1], # -- col 2 --
# --------------------
],
]
)


class Describe_MrXCatCubeMedians:
"""Unit test suite for `cr.cube.matrix.cubemeasure._MrXCatCubeMedians`."""

def it_knows_its_means(self, raw_medians):
cube_medians = _MrXCatCubeMedians(None, raw_medians)

assert cube_medians.medians.tolist() == [
[1.1, 6.1],
[4.3, 3.1],
]

# fixtures -------------------------------------------------------

@pytest.fixture
def raw_medians(self):
"""(2, 3, 2) np.int float64 of medians as received from Cube."""
return np.array(
[ # -- axes are (rows, cols, sel/not) --
# --sel/not--
[ # -- row 0 ------------
[1.1, 6.1], # -- col 0 --
[2.2, 5.2], # -- col 1 --
[3.2, 4.2], # -- col 2 --
],
[ # -- row 1 ------------
[4.3, 3.1], # -- col 0 --
[5.1, 2.1], # -- col 1 --
[6.1, 1.1], # -- col 2 --
# --------------------
],
]
)


class Describe_MrXMrCubeMedians:
"""Unit test suite for `cr.cube.matrix.cubemeasure._MrXMrCubeMedians`."""

def it_knows_its_means(self, raw_medians):
cube_medians = _MrXMrCubeMedians(None, raw_medians)

assert cube_medians.medians.tolist() == [[0.1, 0.1], [0.4, 0.5]]

# fixtures -------------------------------------------------------

@pytest.fixture
def raw_medians(self):
"""(2, 2, 2, 2) np.float64 ndarray of means as from Cube."""
return np.array(
# -- axes are (rows, sel/not, cols, sel/not) --
[
[ # -- row 0 -------------
# --sel/not--
[ # -- selected ------
[0.1, 0.8], # -- col 0
[0.1, 0.7], # -- col 1
],
[ # -- not selected --
[0.2, 0.6], # -- col 0
[0.3, 0.5], # -- col 1
],
],
[ # -- row 1 -------------
[ # -- selected ------
[0.4, 0.4], # -- col 0
[0.5, 0.3], # -- col 1
],
[ # -- not selected --
[0.6, 0.2], # -- col 0
[0.7, 0.1], # -- col 1
],
],
]
)


# === STD DEV ===


Expand Down
2 changes: 2 additions & 0 deletions tests/unit/matrix/test_measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
_ColumnStandardError,
_ColumnUnweightedBases,
_ColumnWeightedBases,
_Medians,
_PairwiseSigPvals,
_PairwiseSigTstats,
_PairwiseMeansSigPVals,
Expand Down Expand Up @@ -81,6 +82,7 @@ class DescribeSecondOrderMeasures:
("column_unweighted_bases", _ColumnUnweightedBases),
("column_weighted_bases", _ColumnWeightedBases),
("means", _Means),
("medians", _Medians),
("population_proportions", _PopulationProportions),
("population_std_err", _PopulationStandardError),
("pvalues", _Pvalues),
Expand Down

0 comments on commit 2a63c29

Please sign in to comment.