def spatial_grouping(f=None, user=False, summary='default', use_records=False): if f is None: return partial(spatial_grouping, user=user, summary=summary, use_records=use_records) if use_records is True: map_records = lambda x: x else: map_records = _binning def wrapper(_user, groupby='week', summary=summary, day_type=None, time_type=None, datatype=None, **kwargs): m = [ f(map_records(g), _user, **kwargs) if user is True else f( map_records(g), **kwargs) for g in group_records(_user, groupby=groupby, interaction=None, day_type=day_type, time_type=time_type) ] if groupby is None: m = m[0] if len(m) != 0 else None return statistics(m, summary=summary, datatype=datatype) return advanced_wrap(f, wrapper)
def recharges_grouping(f=None, summary='default', user_kwd=False): if f is None: return partial(grouping, user_kwd=user_kwd, summary=summary) def wrapper(user, groupby='week', summary=summary, split_week=False, split_day=False, filter_empty=True, datatype=None, **kwargs): parameters = divide_parameters(split_week, split_day, None) operations = { 'grouping': { 'using': 'recharges', 'binning': False, 'groupby': groupby, 'filter_empty': filter_empty, 'divide_by': parameters }, 'apply': { 'user_kwd': user_kwd, 'summary': summary, 'kwargs': kwargs } } return _generic_wrapper(f, user, operations, datatype) return advanced_wrap(f, wrapper)
def grouping(f=None, user_kwd=False, interaction=['call', 'text'], summary='default'): """ ``grouping`` is a decorator for indicator functions, used to simplify the source code. Parameters ---------- f : function The function to decorate user_kwd : boolean If user_kwd is True, the user object will be passed to the decorated function interaction : 'call', 'text', 'location', or a list By default, all indicators use only 'call' and 'text' records, but the interaction keywords filters the records passed to the function. summary: 'default', 'extended', None An indicator returns data statistics, ether *mean* and *std* by default, more with 'extended', or the inner distribution with None. See :meth:`~bandicoot.helper.group.statistics` for more details. See :ref:`new-indicator-label` to learn how to write an indicator with this decorator. """ if f is None: return partial(grouping, user_kwd=user_kwd, interaction=interaction, summary=summary) def wrapper(user, groupby='week', interaction=interaction, summary=summary, day_type=None, time_type=None, datatype=None, **kwargs): if interaction is None: interaction = ['call', 'text'] elif isinstance(interaction, str): interaction = [interaction] else: interaction = interaction[:] # copy the list for more safety for i in interaction: if i not in ['callandtext', 'call', 'text', 'location']: raise ValueError("%s is not a valid interaction value. Only 'call', " "'text', and 'location' are accepted." % i) mapped = ((i, [f(g, user, **kwargs) if user_kwd is True else f(g, **kwargs) for g in group_records(user, groupby=groupby, interaction=i, day_type=day_type, time_type=time_type)]) for i in interaction) returned = {} for (k, m) in mapped: if groupby is None: m = m[0] if len(m) != 0 else None returned[k] = statistics(m, summary=summary, datatype=datatype) return returned return advanced_wrap(f, wrapper)
def spatial_grouping(f=None, user_kwd=False, summary="default", use_records=False): if f is None: return partial(spatial_grouping, user_kwd=user_kwd, summary=summary, use_records=use_records) if use_records is True: map_records = lambda x: x else: map_records = _binning def wrapper(user, groupby="week", summary=summary, split_week=False, split_day=False, datatype=None, **kwargs): part_of_day = ["allday"] if split_day: part_of_day += ["day", "night"] part_of_week = ["allweek"] if split_week: part_of_week += ["weekday", "weekend"] def map_filters_spatial(part_of_week, part_of_day): """ Call the wrapped function for every combinations of interaction, part_of_week, and part_of_day. """ for filter_week in part_of_week: for filter_day in part_of_day: if user_kwd is True: result = [ f(map_records(g), user, **kwargs) for g in group_records(user, None, groupby, filter_week, filter_day) ] else: result = [ f(map_records(g), **kwargs) for g in group_records(user, None, groupby, filter_week, filter_day) ] yield filter_week, filter_day, result returned = AutoVivification() # nested dict structure for (f_w, f_d, m) in map_filters_spatial(part_of_week, part_of_day): if groupby is None: m = m[0] if len(m) != 0 else None returned[f_w][f_d] = statistics(m, summary=summary, datatype=datatype) return returned return advanced_wrap(f, wrapper)
def spatial_grouping(f=None, user=False, summary='default', use_records=False): if f is None: return partial(spatial_grouping, user=user, summary=summary, use_records=use_records) if use_records is True: map_records = lambda x: x else: map_records = _binning def wrapper(_user, groupby='week', summary=summary, day_type=None, time_type=None, datatype=None, **kwargs): m = [f(map_records(g), _user, **kwargs) if user is True else f(map_records(g), **kwargs) for g in group_records(_user, groupby=groupby, interaction=None, day_type=day_type, time_type=time_type)] if groupby is None: m = m[0] if len(m) != 0 else None return statistics(m, summary=summary, datatype=datatype) return advanced_wrap(f, wrapper)
def grouping(f=None, user_kwd=False, interaction=['call', 'text'], summary='default'): """ ``grouping`` is a decorator for indicator functions, used to simplify the source code. Parameters ---------- f : function The function to decorate user_kwd : boolean If user_kwd is True, the user object will be passed to the decorated function interaction : 'call', 'text', 'location', or a list By default, all indicators use only 'call' and 'text' records, but the interaction keywords filters the records passed to the function. summary: 'default', 'extended', None An indicator returns data statistics, ether *mean* and *std* by default, more with 'extended', or the inner distribution with None. See :meth:`~bandicoot.helper.group.statistics` for more details. See :ref:`new-indicator-label` to learn how to write an indicator with this decorator. """ if f is None: return partial(grouping, user_kwd=user_kwd, interaction=interaction, summary=summary) def wrapper(user, groupby='week', interaction=interaction, summary=summary, day_type=None, time_type=None, datatype=None, **kwargs): if interaction is None: interaction = ['call', 'text'] elif isinstance(interaction, str): interaction = [interaction] else: interaction = interaction[:] # copy the list for more safety for i in interaction: if i not in ['callandtext', 'call', 'text', 'location']: raise ValueError( "%s is not a valid interaction value. Only 'call', " "'text', and 'location' are accepted." % i) mapped = ((i, [ f(g, user, **kwargs) if user_kwd is True else f(g, **kwargs) for g in group_records(user, groupby=groupby, interaction=i, day_type=day_type, time_type=time_type) ]) for i in interaction) returned = {} for (k, m) in mapped: if groupby is None: m = m[0] if len(m) != 0 else None returned[k] = statistics(m, summary=summary, datatype=datatype) return returned return advanced_wrap(f, wrapper)
def spatial_grouping(f=None, user_kwd=False, summary='default', use_records=False): if f is None: return partial(spatial_grouping, user_kwd=user_kwd, summary=summary, use_records=use_records) if use_records is True: map_records = lambda x: x else: map_records = _binning def wrapper(user, groupby='week', summary=summary, split_week=False, split_day=False, datatype=None, **kwargs): part_of_day = ['allday'] if split_day: part_of_day += ['day', 'night'] part_of_week = ['allweek'] if split_week: part_of_week += ['weekday', 'weekend'] def map_filters_spatial(part_of_week, part_of_day): """ Call the wrapped function for every combinations of interaction, part_of_week, and part_of_day. """ for filter_week in part_of_week: for filter_day in part_of_day: if user_kwd is True: result = [ f(map_records(g), user, **kwargs) for g in group_records( user, None, groupby, filter_week, filter_day) ] else: result = [ f(map_records(g), **kwargs) for g in group_records( user, None, groupby, filter_week, filter_day) ] yield filter_week, filter_day, result returned = AutoVivification() # nested dict structure for (f_w, f_d, m) in map_filters_spatial(part_of_week, part_of_day): if groupby is None: m = m[0] if len(m) != 0 else None returned[f_w][f_d] = statistics(m, summary=summary, datatype=datatype) return returned return advanced_wrap(f, wrapper)
def grouping(f=None, user_kwd=False, interaction=['call', 'text'], summary='default'): """ ``grouping`` is a decorator for indicator functions, used to simplify the source code. Parameters ---------- f : function The function to decorate user_kwd : boolean If user_kwd is True, the user object will be passed to the decorated function interaction : 'call', 'text', 'location', or a list By default, all indicators use only 'call' and 'text' records, but the interaction keywords filters the records passed to the function. summary: 'default', 'extended', None An indicator returns data statistics, ether *mean* and *std* by default, more with 'extended', or the inner distribution with None. See :meth:`~bandicoot.helper.group.statistics` for more details. See :ref:`new-indicator-label` to learn how to write an indicator with this decorator. """ if f is None: return partial(grouping, user_kwd=user_kwd, interaction=interaction, summary=summary) def wrapper(user, groupby='week', interaction=interaction, summary=summary, split_week=False, split_day=False, datatype=None, **kwargs): if interaction is None: interaction = ['call', 'text'] elif isinstance(interaction, str): interaction = [interaction] else: interaction = interaction[:] # copy the list for more safety part_of_day = ['allday'] if split_day: part_of_day += ['day', 'night'] part_of_week = ['allweek'] if split_week: part_of_week += ['weekday', 'weekend'] for i in interaction: if i not in ['callandtext', 'call', 'text', 'location']: raise ValueError( "%s is not a valid interaction value. Only 'call', " "'text', and 'location' are accepted." % i) def map_filters(interaction, part_of_week, part_of_day): """ Call the wrapped function for every combinations of interaction, part_of_week, and part_of_day. """ for i in interaction: for filter_week in part_of_week: for filter_day in part_of_day: if user_kwd is True: result = [ f(g, user, **kwargs) for g in group_records( user, i, groupby, filter_week, filter_day) ] else: result = [ f(g, **kwargs) for g in group_records( user, i, groupby, filter_week, filter_day) ] yield filter_week, filter_day, i, result returned = AutoVivification() # nested dict structure for (f_w, f_d, i, m) in map_filters(interaction, part_of_week, part_of_day): if groupby is None: m = m[0] if len(m) != 0 else None returned[f_w][f_d][i] = statistics(m, summary=summary, datatype=datatype) return returned return advanced_wrap(f, wrapper)
def grouping(f=None, user_kwd=False, interaction=['call', 'text'], summary='default'): """ ``grouping`` is a decorator for indicator functions, used to simplify the source code. Parameters ---------- f : function The function to decorate user_kwd : boolean If user_kwd is True, the user object will be passed to the decorated function interaction : 'call', 'text', 'location', or a list By default, all indicators use only 'call' and 'text' records, but the interaction keywords filters the records passed to the function. summary: 'default', 'extended', None An indicator returns data statistics, ether *mean* and *std* by default, more with 'extended', or the inner distribution with None. See :meth:`~bandicoot.helper.group.statistics` for more details. See :ref:`new-indicator-label` to learn how to write an indicator with this decorator. """ if f is None: return partial(grouping, user_kwd=user_kwd, interaction=interaction, summary=summary) def wrapper(user, groupby='week', interaction=interaction, summary=summary, split_week=False, split_day=False, datatype=None, **kwargs): if interaction is None: interaction = ['call', 'text'] elif isinstance(interaction, str): interaction = [interaction] else: interaction = interaction[:] # copy the list for more safety part_of_day = ['allday'] if split_day: part_of_day += ['day', 'night'] part_of_week = ['allweek'] if split_week: part_of_week += ['weekday', 'weekend'] for i in interaction: if i not in ['callandtext', 'call', 'text', 'location']: raise ValueError("%s is not a valid interaction value. Only 'call', " "'text', and 'location' are accepted." % i) def map_filters(interaction, part_of_week, part_of_day): """ Call the wrapped function for every combinations of interaction, part_of_week, and part_of_day. """ for i in interaction: for filter_week in part_of_week: for filter_day in part_of_day: if user_kwd is True: result = [f(g, user, **kwargs) for g in group_records(user, i, groupby, filter_week, filter_day)] else: result = [f(g, **kwargs) for g in group_records(user, i, groupby, filter_week, filter_day)] yield filter_week, filter_day, i, result returned = AutoVivification() # nested dict structure for (f_w, f_d, i, m) in map_filters(interaction, part_of_week, part_of_day): if groupby is None: m = m[0] if len(m) != 0 else None returned[f_w][f_d][i] = statistics(m, summary=summary, datatype=datatype) return returned return advanced_wrap(f, wrapper)
def grouping(f=None, interaction=['call', 'text'], summary='default', user_kwd=False): """ ``grouping`` is a decorator for indicator functions, used to simplify the source code. Parameters ---------- f : function The function to decorate user_kwd : boolean If user_kwd is True, the user object will be passed to the decorated function interaction : 'call', 'text', 'location', or a list By default, all indicators use only 'call' and 'text' records, but the interaction keywords filters the records passed to the function. summary: 'default', 'extended', None An indicator returns data statistics, ether *mean* and *std* by default, more with 'extended', or the inner distribution with None. See :meth:`~bandicoot.helper.group.statistics` for more details. See :ref:`new-indicator-label` to learn how to write an indicator with this decorator. """ if f is None: return partial(grouping, user_kwd=user_kwd, interaction=interaction, summary=summary) def wrapper(user, groupby='week', interaction=interaction, summary=summary, split_week=False, split_day=False, filter_empty=True, datatype=None, **kwargs): if interaction is None: interaction = ['call', 'text'] parameters = divide_parameters(split_week, split_day, interaction) operations = { 'grouping': { 'using': 'records', 'binning': False, 'groupby': groupby, 'filter_empty': filter_empty, 'divide_by': parameters }, 'apply': { 'user_kwd': user_kwd, 'summary': summary, 'kwargs': kwargs } } for i in parameters['interaction']: if i not in ['callandtext', 'call', 'text', 'location']: raise ValueError( "%s is not a valid interaction value. Only " "'call', 'text', and 'location' are accepted." % i) return _generic_wrapper(f, user, operations, datatype) return advanced_wrap(f, wrapper)
def grouping(f=None, interaction=['call', 'text'], summary='default', user_kwd=False): """ ``grouping`` is a decorator for indicator functions, used to simplify the source code. Parameters ---------- f : function The function to decorate user_kwd : boolean If user_kwd is True, the user object will be passed to the decorated function interaction : 'call', 'text', 'location', or a list By default, all indicators use only 'call' and 'text' records, but the interaction keywords filters the records passed to the function. summary: 'default', 'extended', None An indicator returns data statistics, ether *mean* and *std* by default, more with 'extended', or the inner distribution with None. See :meth:`~bandicoot.helper.group.statistics` for more details. See :ref:`new-indicator-label` to learn how to write an indicator with this decorator. """ if f is None: return partial(grouping, user_kwd=user_kwd, interaction=interaction, summary=summary) def wrapper(user, groupby='week', interaction=interaction, summary=summary, split_week=False, split_day=False, filter_empty=True, datatype=None, **kwargs): if interaction is None: interaction = ['call', 'text'] parameters = divide_parameters(split_week, split_day, interaction) operations = { 'grouping': { 'using': 'records', 'binning': False, 'groupby': groupby, 'filter_empty': filter_empty, 'divide_by': parameters }, 'apply': { 'user_kwd': user_kwd, 'summary': summary, 'kwargs': kwargs } } for i in parameters['interaction']: if i not in ['callandtext', 'call', 'text', 'location']: raise ValueError("%s is not a valid interaction value. Only " "'call', 'text', and 'location' are accepted." % i) return _generic_wrapper(f, user, operations, datatype) return advanced_wrap(f, wrapper)