Skip to content

Commit

Permalink
smoothing: handle mean
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoarbitrio committed Aug 31, 2020
1 parent 91fd3fd commit d51ee3a
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions src/cr/cube/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ def has_means(self):
"""True if cube includes a means measure."""
return self._measures.means is not None

@lazyproperty
def has_smoothing(self):
return self._measures.has_smoothing

def inflate(self):
"""Return new Cube object with rows-dimension added.
Expand Down Expand Up @@ -436,7 +440,7 @@ def _measure(self, weighted):
def _measures(self):
"""_Measures object for this cube.
Provides access to unweighted counts, and weighted counts and/or means
Provides access to unweighted counts, weighted, smoothed counts and/or means
when available.
"""
return _Measures(self._cube_dict, self._all_dimensions, self._transforms_dict)
Expand Down Expand Up @@ -510,6 +514,14 @@ def means(self):
)
if mean_measure_dict is None:
return None
if self._has_smoothing:
return _SmoothedCountMeasure.factory(
self._cube_dict,
self._all_dimensions,
self.is_weighted,
True,
self._transforms_dict,
)
return _MeanMeasure(self._cube_dict, self._all_dimensions)

@lazyproperty
Expand Down Expand Up @@ -568,7 +580,11 @@ def unweighted_counts(self):
"""
if self._has_smoothing:
return _SmoothedCountMeasure.factory(
self._cube_dict, self._all_dimensions, False, self._transforms_dict
self._cube_dict,
self._all_dimensions,
False,
False,
self._transforms_dict,
)
return _UnweightedCountMeasure(self._cube_dict, self._all_dimensions)

Expand All @@ -583,12 +599,15 @@ def weighted_counts(self):
if self.is_weighted:
if self._has_smoothing:
return _SmoothedCountMeasure.factory(
self._cube_dict, self._all_dimensions, True, self._transforms_dict
self._cube_dict,
self._all_dimensions,
True,
False,
self._transforms_dict,
)
else:
return _WeightedCountMeasure(self._cube_dict, self._all_dimensions)
else:
return self.unweighted_counts
return self.unweighted_counts


class _BaseMeasure(object):
Expand Down Expand Up @@ -645,13 +664,16 @@ def _flat_values(self):
class _SmoothedCountMeasure(_BaseMeasure):
"""Base class for smoothing measure objects"""

def __init__(self, cube_dict, all_dimensions, is_weighted, transforms_dict):
def __init__(
self, cube_dict, all_dimensions, is_weighted, has_means, transforms_dict
):
super(_SmoothedCountMeasure, self).__init__(cube_dict, all_dimensions)
self._is_weighted = is_weighted
self._has_means = has_means
self._transforms_dict = transforms_dict

@classmethod
def factory(cls, cube_dict, all_dimensions, is_weighted, transforms_dict):
def factory(cls, cube_dict, dimensions, is_weighted, has_means, transforms_dict):
"""Return appropriate Smoothing Method according to transforms_dict"""
method = transforms_dict.get("smoothing").get("method", None)
# ATM it returns always _OneSideMovingAvg, but in this way it's ready when
Expand All @@ -660,7 +682,7 @@ def factory(cls, cube_dict, all_dimensions, is_weighted, transforms_dict):
method, _OneSideMovingAvg
)
return SmoothingMethodCls(
cube_dict, all_dimensions, is_weighted, transforms_dict
cube_dict, dimensions, is_weighted, has_means, transforms_dict
)

@lazyproperty
Expand All @@ -675,7 +697,12 @@ def _flat_values(self): # pragma: no cover
def _raw_counts(self):
""" -> tuple, numeric counts (weighted or not) according to is_weighted prop."""
return (
tuple(self._cube_dict["result"]["measures"]["count"]["data"])
tuple(
np.nan if type(x) is dict else x
for x in self._cube_dict["result"]["measures"]["mean"]["data"]
)
if self._has_means
else tuple(self._cube_dict["result"]["measures"]["count"]["data"])
if self._is_weighted
else tuple(self._cube_dict["result"]["counts"])
)
Expand All @@ -686,7 +713,7 @@ class _OneSideMovingAvg(_SmoothedCountMeasure):

@lazyproperty
def _difference(self):
""" -> int, last dimension size difference between smoothed and raw counts
""" -> int, how many dropped elements after smoothing
np.convolve with `valid` mode selected returns output of length
max(M, N) - min(M, N) + 1.
Expand All @@ -699,6 +726,9 @@ def _difference(self):

@lazyproperty
def _flat_values(self):
import ipdb

ipdb.set_trace()
""" -> tuple, counts after smoothing.
Considering that One Side Moving Avg will produce an output of length equal to
Expand Down

0 comments on commit d51ee3a

Please sign in to comment.