# Path: bamboo/lib/jsontools.py
# def series_to_jsondict(series):
#     """Convert a Series to a dictionary encodable as JSON."""
#     return series if series is None else {
#         unicode(key): get_json_value(value)
#         for key, value in series.iteritems()
#     }
# 
# Path: bamboo/lib/mongo.py
# def dict_from_mongo(_dict):
#     for key, value in _dict.items():
#         if isinstance(value, list):
#             value = [dict_from_mongo(obj)
#                      if isinstance(obj, dict) else obj for obj in value]
#         elif isinstance(value, dict):
#             value = dict_from_mongo(value)
# 
#         if _was_encoded_for_mongo(key):
#             del _dict[key]
#             _dict[_decode_from_mongo(key)] = value
# 
#     return _dict
# 
# def dict_for_mongo(_dict):
#     """Encode all keys in `_dict` for MongoDB."""
#     for key, value in _dict.items():
#         if _is_invalid_for_mongo(key):
#             del _dict[key]
#             key = key_for_mongo(key)
# 
#         if isinstance(value, list):
#             _dict[key] = [dict_for_mongo(obj) if isinstance(obj, dict) else obj
#                           for obj in value]
#         elif isinstance(value, dict):
#             _dict[key] = dict_for_mongo(value)
#         else:
#             _dict[key] = value_for_mongo(value)
# 
#     return _dict
# 
# Path: bamboo/lib/utils.py
# def combine_dicts(*dicts):
#     """Combine dicts with keys in later dicts taking precedence."""
#     return dict(chain(*[_dict.iteritems() for _dict in dicts]))


from bamboo.lib.jsontools import series_to_jsondict
from bamboo.lib.mongo import dict_from_mongo, dict_for_mongo
from bamboo.lib.utils import combine_dicts
    """Calculate summary statistics for group."""
    return series_to_jsondict(
        dframe.groupby(groups).apply(summarize_df, dataset, groups))


def summarize(dataset, dframe, groups, no_cache, update=False):
    """Raises a ColumnTypeError if grouping on a non-dimensional column."""
    # do not allow group by numeric types
    for group in groups:
        if not dataset.is_factor(group):
            raise ColumnTypeError("group: '%s' is not a dimension." % group)

    group_str = dataset.join_groups(groups) or dataset.ALL

    # check cached stats for group and update as necessary
    stats = dataset.stats
    group_stats = stats.get(group_str)

    if no_cache or not group_stats or update:
        group_stats = summarize_with_groups(dframe, groups, dataset) if\
            groups else summarize_df(dframe, dataset)

        if not no_cache:
            if update:
                original_group_stats = stats.get(group_str, {})
                group_stats = combine_dicts(original_group_stats, group_stats)

            stats.update({group_str: group_stats})
            dataset.update({dataset.STATS: dict_for_mongo(stats)})


# Path: bamboo/lib/jsontools.py
# def series_to_jsondict(series):
#     """Convert a Series to a dictionary encodable as JSON."""
#     return series if series is None else {
#         unicode(key): get_json_value(value)
#         for key, value in series.iteritems()
#     }
# 
# Path: bamboo/lib/mongo.py
# def dict_from_mongo(_dict):
#     for key, value in _dict.items():
#         if isinstance(value, list):
#             value = [dict_from_mongo(obj)
#                      if isinstance(obj, dict) else obj for obj in value]
#         elif isinstance(value, dict):
#             value = dict_from_mongo(value)
# 
#         if _was_encoded_for_mongo(key):
#             del _dict[key]
#             _dict[_decode_from_mongo(key)] = value
# 
#     return _dict
# 
# def dict_for_mongo(_dict):
#     """Encode all keys in `_dict` for MongoDB."""
#     for key, value in _dict.items():
#         if _is_invalid_for_mongo(key):
#             del _dict[key]
#             key = key_for_mongo(key)
# 
#         if isinstance(value, list):
#             _dict[key] = [dict_for_mongo(obj) if isinstance(obj, dict) else obj
#                           for obj in value]
#         elif isinstance(value, dict):
#             _dict[key] = dict_for_mongo(value)
#         else:
#             _dict[key] = value_for_mongo(value)
# 
#     return _dict
# 
# Path: bamboo/lib/utils.py
# def combine_dicts(*dicts):
#     """Combine dicts with keys in later dicts taking precedence."""
#     return dict(chain(*[_dict.iteritems() for _dict in dicts]))


# Path: bamboo/core/summary.py
from bamboo.lib.jsontools import series_to_jsondict
from bamboo.lib.mongo import dict_from_mongo, dict_for_mongo
from bamboo.lib.utils import combine_dicts

    """Calculate summary statistics for group."""
    return series_to_jsondict(
        dframe.groupby(groups).apply(summarize_df, dataset, groups))


def summarize(dataset, dframe, groups, no_cache, update=False):
    """Raises a ColumnTypeError if grouping on a non-dimensional column."""
    # do not allow group by numeric types
    for group in groups:
        if not dataset.is_factor(group):
            raise ColumnTypeError("group: '%s' is not a dimension." % group)

    group_str = dataset.join_groups(groups) or dataset.ALL

    # check cached stats for group and update as necessary
    stats = dataset.stats
    group_stats = stats.get(group_str)

    if no_cache or not group_stats or update:
        group_stats = summarize_with_groups(dframe, groups, dataset) if\
            groups else summarize_df(dframe, dataset)

        if not no_cache:
            if update:
                original_group_stats = stats.get(group_str, {})
                group_stats = combine_dicts(original_group_stats, group_stats)

            stats.update({group_str: group_stats})
            dataset.update({dataset.STATS: dict_for_mongo(stats)})


    stats_dict = dict_from_mongo(group_stats)