Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ Please visit <https://crunch-cube.readthedocs.io/en/latest> for the API referenc

## Changes

### 3.4.0

- Add rows_margin_percentage and columns_margin_percentage to _Slice

### 3.3.8

- Use native namespace packages for cr instead of deprecated pkg_resources / pkgutil

### 3.3.7

- Rebuild
Expand Down
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import re

from setuptools import find_packages, setup
from setuptools import setup


def ascii_bytes_from(path, *paths):
Expand Down Expand Up @@ -49,12 +49,9 @@ def ascii_bytes_from(path, *paths):
author_email="dev@crunch.io",
license="MIT License",
install_requires=install_requires,
tests_require=test_requires,
extras_require={"testing": test_requires},
packages=find_packages("src", exclude=["tests"]),
package_dir={"": "src"},
include_package_data=True,
entry_points={},
namespace_packages=["cr"],
zip_safe=True,
)
5 changes: 0 additions & 5 deletions src/cr/__init__.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/cr/cube/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

"""Initialization module for crunch-cube package."""

__version__ = "3.3.7"
__version__ = "3.4.0"
29 changes: 29 additions & 0 deletions src/cr/cube/cubepart.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,18 @@ def columns_margin_proportion(self):
# --- otherwise columns-margin-proportion is a maginal ---
return self._assemble_marginal(self._measures.columns_table_proportion)

@lazyproperty
def columns_margin_percentage(self):
"""1D or 2D np.float64 ndarray of weighted-percentage for each column of slice

This array is 2D (a distinct margin value for each cell) when the rows
dimension is MR, because each MR-subvariable has its own weighted N. This is
because not every possible response is necessarily offered to every respondent.

In all other cases, the array is 1D, containing one value for each column.
"""
return self.columns_margin_proportion * 100

@lazyproperty
def columns_scale_mean(self):
"""Optional 1D np.float64 ndarray of scale mean for each column.
Expand Down Expand Up @@ -1431,6 +1443,18 @@ def rows_margin_proportion(self):
# --- otherwise rows-margin is a vector ---
return self._assemble_marginal(self._measures.rows_table_proportion)

@lazyproperty
def rows_margin_percentage(self):
"""1D or 2D np.float64 ndarray of weighted-percentage for each column of slice

This array is 2D (a distinct margin value for each cell) when the columns
dimension is MR, because each MR-subvariable has its own weighted N. This is
because not every possible response is necessarily offered to every respondent.

In all other cases, the array is 1D, containing one value for each column.
"""
return self.rows_margin_proportion * 100

@lazyproperty
def rows_missing(self):
"""1D/2D np.float64 ndarray of unweighted counts of missing for each cell/row
Expand Down Expand Up @@ -1754,6 +1778,11 @@ def table_code(self):

@lazyproperty
def table_percentages(self):
"""2D np.float64 ndarray of table-percentage.

Table-percentage is the fraction of the table weighted-N contributed by each
cell, expressed as a percentage (float between 0.0 and 100.0 inclusive).
"""
return self.table_proportions * 100

@lazyproperty
Expand Down
7 changes: 7 additions & 0 deletions tests/expectations/cat-hs-x-mr-columns-margin-percentages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
17.935203679537608,
33.454988861780095,
50.762387573092646,
80.27297327014429,
79.83476971629214,
]
46 changes: 46 additions & 0 deletions tests/expectations/cat-hs-x-mr-row-margin-percentages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
[
12.354283136548759,
7.444231360707625,
4.93318248692712,
4.584390457713685,
6.55671553927186,
],
[
18.60667239281892,
19.291282000683193,
16.542087034551418,
13.813767505048451,
18.69747651879045,
],
[
30.960955529367677,
26.735513361390815,
21.475269521478538,
18.398157962762134,
25.25419205806231,
],
[
30.960955529367677,
26.735513361390815,
21.475269521478538,
18.398157962762138,
25.25419205806231,
],
[0.0, 0.0, 0.0, 0.0, 0.0],
[
33.37741042168464,
36.41410555192866,
37.11714542482715,
36.248045110476504,
35.13675072713398,
],
[
35.66163404894768,
36.850381086680535,
41.40758505369432,
45.35379692676135,
39.60905721480371,
],
[100.0, 100.0, 100.0, 99.99999999999999, 100.0],
]
52 changes: 52 additions & 0 deletions tests/expectations/mr-x-cat-hs-columns-margin-percentages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
[
12.354283158588755,
18.60667238084944,
30.960955539438196,
30.960955539438196,
0.0,
33.37741042334933,
35.66163403721248,
100.0,
],
[
7.444231365436714,
19.291281991786978,
26.735513357223688,
26.735513357223688,
0.0,
36.41410557725717,
36.85038106551913,
99.99999999999997,
],
[
4.93318248486953,
16.54208703286872,
21.47526951773825,
21.475269517738248,
0.0,
37.11714541914551,
41.40758506311624,
100.0,
],
[
4.5843904451220485,
13.813767515853032,
18.39815796097508,
18.39815796097508,
0.0,
36.248045116670035,
45.35379692235489,
100.0,
],
[
6.556715546598428,
18.697476512833678,
25.254192059432107,
25.2541920594321,
0.0,
35.1367507213182,
39.60905721924969,
100.0,
],
]
7 changes: 7 additions & 0 deletions tests/expectations/mr-x-cat-hs-row-margin-percentages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
17.935203742867287,
33.454988896294154,
50.76238756298953,
80.27297326388751,
79.83476971842138,
]
6 changes: 6 additions & 0 deletions tests/expectations/mr-x-mr-columns-margin-percentages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
[13.835522515396878, 26.54735977789324, 55.81110549484439, 60.67041874287763],
[12.36197264278271, 32.26874248151863, 53.05910795058967, 67.57591837745224],
[15.913981148498676, 35.664811199716155, 48.05298239913407, 65.97161566200931],
[13.835522515396914, 32.26874248151863, 48.05298239913407, 55.24609836123409],
]
6 changes: 6 additions & 0 deletions tests/expectations/mr-x-mr-row-margin-percentages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
[13.835522515396878, 12.36197264278271, 15.913981148498676, 13.835522515396912],
[26.54735977789324, 32.26874248151863, 35.664811199716155, 32.26874248151863],
[55.81110549484439, 53.05910795058967, 48.05298239913407, 48.05298239913407],
[60.670418742877644, 67.57591837745224, 65.97161566200931, 55.24609836123409],
]
34 changes: 34 additions & 0 deletions tests/integration/test_cubepart.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,23 @@ def test_it_knows_the_rows_margin_proportion(self, fixture, expectation):

assert rows_margin_proportions.tolist() == load_python_expression(expectation)

@pytest.mark.parametrize(
"fixture, expectation",
(
(CR.CAT_HS_X_MR, "cat-hs-x-mr-row-margin-percentages"),
(CR.MR_X_CAT_HS_MT, "mr-x-cat-hs-row-margin-percentages"),
(CR.MR_X_MR, "mr-x-mr-row-margin-percentages"),
),
)
def test_it_knows_the_rows_margin_percentage(self, fixture, expectation):
slice_ = _Slice(
Cube(fixture), slice_idx=0, transforms={}, population=None, mask_size=0
)

rows_margin_percentages = slice_.rows_margin_percentage

assert rows_margin_percentages.tolist() == load_python_expression(expectation)

@pytest.mark.parametrize(
"fixture, expectation",
(
Expand Down Expand Up @@ -855,6 +872,23 @@ def test_it_knows_the_column_margin_proportions(self, fixture, expectation):

assert columns_margin_proportion.tolist() == load_python_expression(expectation)

@pytest.mark.parametrize(
"fixture, expectation",
(
(CR.CAT_HS_X_MR, "cat-hs-x-mr-columns-margin-percentages"),
(CR.MR_X_CAT_HS_MT, "mr-x-cat-hs-columns-margin-percentages"),
(CR.MR_X_MR, "mr-x-mr-columns-margin-percentages"),
),
)
def test_it_knows_the_column_margin_percentages(self, fixture, expectation):
slice_ = _Slice(
Cube(fixture), slice_idx=0, transforms={}, population=None, mask_size=0
)

columns_margin_percentage = slice_.columns_margin_percentage

assert columns_margin_percentage.tolist() == load_python_expression(expectation)

def test_it_knows_the_margins_of_mr_x_mr(self):
slice_ = _Slice(
Cube(CR.MR_X_MR), slice_idx=0, transforms={}, population=None, mask_size=0
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/test_headers_and_subtotals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3212,7 +3212,9 @@ def test_it_computes_measures_for_cat_x_cat_with_subdiffs_on_both(self):
assert slice_.columns_margin[0] == pytest.approx(np.nan, nan_ok=True)
assert slice_.rows_margin[0] == pytest.approx(np.nan, nan_ok=True)
assert slice_.columns_margin_proportion[0] == pytest.approx(11 / 266)
assert slice_.columns_margin_percentage[0] == pytest.approx(11 / 266 * 100)
assert slice_.rows_margin_proportion[0] == pytest.approx(-17 / 266)
assert slice_.rows_margin_percentage[0] == pytest.approx(-17 / 266 * 100)
assert slice_.columns_base[0] == pytest.approx(np.nan, nan_ok=True)
assert slice_.rows_base[0] == pytest.approx(np.nan, nan_ok=True)
assert slice_.column_weighted_bases[:, 0] == pytest.approx(
Expand Down
Loading
Loading