Skip to content

Commit

Permalink
Merge 0d50fd9 into d7ea279
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoarbitrio committed Jul 27, 2020
2 parents d7ea279 + 0d50fd9 commit 477bad7
Show file tree
Hide file tree
Showing 17 changed files with 5,303 additions and 2,580 deletions.
5 changes: 5 additions & 0 deletions src/cr/cube/cubepart.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ def cube_index(self):
def cube_is_mr_by_itself(self):
return False

@lazyproperty
def cube_row_dimension_type(self):
"""Member of `cr.cube.enum.DIMENSION_TYPE` for cube row dimension"""
return self._cube.dimensions[0].dimension_type

@lazyproperty
def dimension_types(self):
"""Sequence of member of `cr.cube.enum.DIMENSION_TYPE` for each dimension.
Expand Down
39 changes: 19 additions & 20 deletions src/cr/cube/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,12 +876,19 @@ class _MrXMrMatrix(_MatrixWithMR):
"""

def __init__(
self, dimensions, counts, base_counts, counts_with_missings, overlaps=None
self,
dimensions,
counts,
base_counts,
counts_with_missings,
overlaps=None,
overlaps_margin=None,
):
super(_MrXMrMatrix, self).__init__(
dimensions, counts, base_counts, counts_with_missings
)
self._overlaps = overlaps
self._overlaps_margin = overlaps_margin

@lazyproperty
def columns(self):
Expand Down Expand Up @@ -948,24 +955,27 @@ def tstats_overlap(self):
"""

# Subtraction of the proportions foreach observation
diff = np.subtract.outer(
self._mr_shadow_proportions.diagonal(),
self._mr_shadow_proportions.diagonal(),
diff = (
np.subtract.outer(
self._mr_shadow_proportions.diagonal(),
self._mr_shadow_proportions.diagonal(),
)
* -1
)

se_pi_pj = np.add.outer(
self._mr_shadow_proportions.diagonal()
* (1 - self._mr_shadow_proportions.diagonal())
/ self._pairwise_overlap_total.diagonal(),
/ self._overlaps_margin.diagonal(),
self._mr_shadow_proportions.diagonal()
* (1 - self._mr_shadow_proportions.diagonal())
/ self._pairwise_overlap_total.diagonal(),
/ self._overlaps_margin.diagonal(),
)

# Correction factor considering the overlap
correction_factor = (
2
* self._pairwise_overlap_total
* self._overlaps_margin
* (
self._mr_shadow_proportions
- np.multiply.outer(
Expand All @@ -974,8 +984,7 @@ def tstats_overlap(self):
)
)
) / np.multiply.outer(
self._pairwise_overlap_total.diagonal(),
self._pairwise_overlap_total.diagonal(),
self._overlaps_margin.diagonal(), self._overlaps_margin.diagonal()
)
se_diff = np.sqrt(se_pi_pj - correction_factor)
np.fill_diagonal(diff, 0)
Expand Down Expand Up @@ -1018,17 +1027,7 @@ def _mr_shadow_proportions(self):
element of any prepended dimensions:
A 1d interface to a 4d hypercube of underlying counts.
"""
return self._counts[:, 0, :, 0] / self._pairwise_overlap_total

@lazyproperty
def _pairwise_overlap_total(self):
"""Return 2D ndarray symmetric-square matrix of valid observations.
Given a 4d hypercube of multiple response items, return the
symmetric square matrix of valid observations between all pairs.
n1 = 2; n2 = 2; n12 = 1; overlap total = 3
"""
return np.sum(self._counts, axis=(1, 3))
return self._counts[:, 0, :, 0] / self._overlaps_margin

@lazyproperty
def _row_generator(self):
Expand Down
14 changes: 13 additions & 1 deletion src/cr/cube/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,19 @@ def calculate_overlap_tstats(
if offset != 0
else mr_counts_with_missings[slice_index]
)
overlap_slice = cls(mr_dimensions, counts, base_counts, counts_with_missings)
overlap_margins = (
np.sum(mr_counts[slice_index], axis=0)[:, 0, :, 0]
if offset != 0
else np.sum(mr_counts, axis=0)[:, 0, :, 0]
)
overlap_slice = cls(
mr_dimensions,
counts,
base_counts,
counts_with_missings,
None,
overlap_margins,
)
diff, se_diff = overlap_slice.tstats_overlap
numerator[slice_index] = diff
standard_error[slice_index] = se_diff
Expand Down
8 changes: 4 additions & 4 deletions tests/expectations/cat-x-mr-aug-pw-idxs-alt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(
((2, 4), (2, 4), (4,), (2, 4), ()),
((1, 2, 3, 4), (2, 4), (), (2, 4), ()),
((1, 2, 3, 4), (2, 4), (), (2, 4), ()),
((1, 2, 3, 4), (2, 4), (), (2, 4), ()),
((1, 2, 3), (), (), (), (1, 2, 3)),
((), (), (1, 3, 4), (), (3,)),
((), (0, 4), (4,), (0,), ()),
((), (0, 2, 4), (4,), (0, 2, 4), ()),
)
8 changes: 4 additions & 4 deletions tests/expectations/cat-x-mr-aug-pw-idxs-recip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(
((2, 4), (2, 4), (0, 1, 3, 4), (2, 4), (0, 1, 2, 3)),
((1, 2, 4), (0, 2, 4), (0, 1, 3), (2, 4), (0, 1, 3)),
((1, 2, 3, 4), (0, 2, 4), (0, 1, 3), (0, 2, 4), (0, 1, 3)),
((1, 2, 3, 4), (0, 2, 4), (0, 1, 3), (0, 2, 4), (0, 1, 3)),
((1, 2, 3), (0,), (0, 4), (0, 4), (2, 3)),
((), (2,), (1, 3), (2,), ()),
((), (), (), (), ()),
((1, 3), (0, 4), (3,), (0, 2, 4), (1, 3)),
)
8 changes: 4 additions & 4 deletions tests/expectations/cat-x-mr-aug-pw-idxs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(
((2, 4), (2, 4), (4,), (2, 4), ()),
((1, 2, 4), (2, 4), (), (2, 4), ()),
((1, 2, 3, 4), (2, 4), (), (2, 4), ()),
((1, 2, 3, 4), (2, 4), (), (2, 4), ()),
((1, 2, 3), (), (), (), (2, 3)),
((), (), (1, 3), (), ()),
((), (), (), (), ()),
((), (0, 4), (), (0, 2, 4), ()),
)
File renamed without changes.
Loading

0 comments on commit 477bad7

Please sign in to comment.