Skip to content

Commit

Permalink
Merge pull request #401 from Crunch-io/enum-refactoring-187998654
Browse files Browse the repository at this point in the history
Enum refactoring 187998654
  • Loading branch information
ernestoarbitrio authored Aug 1, 2024
2 parents 56d03c5 + 2599849 commit 12c68b2
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 66 deletions.
26 changes: 26 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Read the Docs configuration file for Sphinx projects
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the OS, Python version and other tools you might need
build:
os: ubuntu-22.04
tools:
python: "3.11"

# Build documentation in the "docs/" directory with Sphinx
sphinx:
configuration: docs/source/conf.py

# Optionally build your docs in additional formats such as PDF and ePub
formats:
- pdf

# Optional but recommended, declare the Python requirements required
# to build your documentation
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
python:
install:
- requirements: requirements-rtd.txt
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ The detailed description can be found
[![Build Status](https://travis-ci.org/Crunch-io/crunch-cube.png?branch=master)](https://travis-ci.org/Crunch-io/crunch-cube)
[![Coverage Status](https://codecov.io/gh/Crunch-io/crunch-cube/branch/master/graph/badge.svg?token=C6auKOj8tZ)](https://codecov.io/gh/Crunch-io/crunch-cube)
[![Documentation Status](https://readthedocs.org/projects/crunch-cube/badge/?version=latest)](http://crunch-cube.readthedocs.io/en/latest/?badge=latest)
[![CodeFactor](https://www.codefactor.io/repository/github/crunch-io/crunch-cube/badge)](https://www.codefactor.io/repository/github/crunch-io/crunch-cube)
---

## Changes
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def ascii_bytes_from(path, *paths):
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
# html_static_path = ["_static"]

# -- Options for HTMLHelp output ------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions requirements-rtd.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
scipy
tabulate
sphinx-rtd-theme
17 changes: 15 additions & 2 deletions src/cr/cube/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from cr.cube.cubepart import CubePartition
from cr.cube.dimension import Dimensions
from cr.cube.enums import CUBE_MEASURE, DIMENSION_TYPE as DT, NUMERIC_CUBE_MEASURES
from cr.cube.enums import CUBE_MEASURE, DIMENSION_TYPE as DT
from cr.cube.util import lazyproperty


Expand Down Expand Up @@ -68,6 +68,17 @@ def description(self) -> str:
"""str description of first cube in this set."""
return self._cubes[0].description

@lazyproperty
def has_numeric_measures(self) -> bool:
"""True if cube response contains numeric measures like mean, sum, stddev.
Returns true if any of the numeric cube measure is in the cube response false
otherwise.
"""
if self.available_measures.intersection(CUBE_MEASURE.NUMERIC_CUBE_MEASURES()):
return True
return False

@lazyproperty
def has_weighted_counts(self) -> bool:
"""True if cube-responses include a weighted-count measure."""
Expand Down Expand Up @@ -636,7 +647,9 @@ def _available_numeric_measures(self) -> Tuple[CUBE_MEASURE, ...]:
Basically the numeric measures are the intersection between all the measures
within the cube response and the defined NUMERIC_CUBE_MEASURES.
"""
return tuple(self.available_measures.intersection(NUMERIC_CUBE_MEASURES))
return tuple(
self.available_measures.intersection(CUBE_MEASURE.NUMERIC_CUBE_MEASURES())
)

@lazyproperty
def _ca_as_0th(self) -> bool:
Expand Down
36 changes: 10 additions & 26 deletions src/cr/cube/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,13 @@ class CUBE_MEASURE(enum.Enum):
WEIGHTED_VALID_COUNT = "valid_count_weighted"
WEIGHTED_SQUARED_COUNT = "weighted_squared_count"


NUMERIC_CUBE_MEASURES = frozenset(
(
CUBE_MEASURE.MEAN,
CUBE_MEASURE.MEDIAN,
CUBE_MEASURE.SUM,
CUBE_MEASURE.STDDEV,
CUBE_MEASURE.UNWEIGHTED_VALID_COUNT,
CUBE_MEASURE.WEIGHTED_VALID_COUNT,
)
)

NUMERIC_MEASURES = frozenset(
(
MEASURE.MEAN,
MEASURE.MEDIAN,
MEASURE.SUM,
MEASURE.STDDEV,
MEASURE.WEIGHTED_VALID_COUNT,
MEASURE.UNWEIGHTED_VALID_COUNT,
MEASURE.SMOOTHED_MEAN,
MEASURE.COLUMN_SHARE_SUM,
MEASURE.ROW_SHARE_SUM,
MEASURE.TOTAL_SHARE_SUM,
)
)
@classmethod
def NUMERIC_CUBE_MEASURES(cls):
return {
cls.MEAN,
cls.MEDIAN,
cls.SUM,
cls.STDDEV,
cls.UNWEIGHTED_VALID_COUNT,
cls.WEIGHTED_VALID_COUNT,
}
15 changes: 14 additions & 1 deletion tests/unit/test_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
_UnweightedValidCountsMeasure,
)
from cr.cube.cubepart import _Slice, _Strand, _Nub
from cr.cube.enums import DIMENSION_TYPE as DT
from cr.cube.enums import DIMENSION_TYPE as DT, CUBE_MEASURE as M

from ..fixtures import CR # ---mnemonic: CR = 'cube-response'---
from ..unitutil import call, class_mock, instance_mock, property_mock
Expand All @@ -29,6 +29,19 @@ def it_knows_its_availabe_measures(self, cube_, _cubes_prop_):

assert cube_set.available_measures == {"mean", "sum"}

@pytest.mark.parametrize(
"available_measures, expected_value",
(({M.MEAN, M.COUNT}, True), ({M.OVERLAP, M.COUNT}, False)),
)
def it_knows_if_it_has_numeric_measures(
self, request, available_measures, expected_value
):
property_mock(
request, CubeSet, "available_measures", return_value=available_measures
)
cube_set = CubeSet(None, None, None, None)
assert cube_set.has_numeric_measures is expected_value

def but_it_includes_availabe_measures_from_all_cubes_in_cube_set(
self, request, _cubes_prop_
):
Expand Down
36 changes: 1 addition & 35 deletions tests/unit/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

"""Unit test suite for cr.cube.enums module."""

from cr.cube.enums import (
CUBE_MEASURE,
MEASURE,
NUMERIC_CUBE_MEASURES,
NUMERIC_MEASURES,
_DimensionType,
)
from cr.cube.enums import _DimensionType


class Describe_DimensionType:
Expand All @@ -26,31 +20,3 @@ def it_knows_its_name(self):
dimension_type = _DimensionType("WORM_HOLE")
name = dimension_type.name
assert name == "WORM_HOLE"


class TestCubeMeasures:
def test_numeric_cube_measures_intersection(self):
intersection = NUMERIC_CUBE_MEASURES & {m for m in CUBE_MEASURE}
expected_intersection = {
MEASURE.MEAN,
MEASURE.MEDIAN,
MEASURE.SUM,
MEASURE.STDDEV,
MEASURE.UNWEIGHTED_VALID_COUNT,
MEASURE.WEIGHTED_VALID_COUNT,
}
assert sorted(list([m.value for m in intersection])) == sorted(
list([m.value for m in expected_intersection])
)

def test_numeric_cube_measures_difference(self):
difference = {m.value for m in NUMERIC_MEASURES} - {
m.value for m in NUMERIC_CUBE_MEASURES
}
expected_difference = {
"smoothed_mean",
"total_share_sum",
"row_share_sum",
"col_share_sum",
}
assert list(sorted(difference)) == list(sorted(expected_difference))

0 comments on commit 12c68b2

Please sign in to comment.