Exemple #1
0
    def all_subaccounts(self):
        """
        Returns an iterator of all subaccounts that have a recorded transaction
        with the account.

        """
        return tz.unique(t.subaccount for t in self.transactions)
 def choosers_columns_used(self):
     """
     Columns from the choosers table that are used for filtering.
     """
     return list(tz.unique(tz.concatv(
         util.columns_in_filters(self.choosers_predict_filters),
         util.columns_in_filters(self.choosers_fit_filters))))
def columns_in_filters(filters):
    """
    Returns a list of the columns used in a set of query filters.

    Parameters
    ----------
    filters : list of str or str
        List of the filters as passed passed to ``apply_filter_query``.

    Returns
    -------
    columns : list of str
        List of all the strings mentioned in the filters.

    """
    if not filters:
        return []

    if not isinstance(filters, str):
        filters = ' '.join(filters)

    columns = []
    reserved = {'and', 'or', 'in', 'not'}

    for toknum, tokval, _, _, _ in generate_tokens(StringIO(filters).readline):
        if toknum == NAME and tokval not in reserved:
            columns.append(tokval)

    return list(tz.unique(columns))
Exemple #4
0
def columns_in_filters(filters):
    """
    Returns a list of the columns used in a set of query filters.

    Parameters
    ----------
    filters : list of str or str
        List of the filters as passed passed to ``apply_filter_query``.

    Returns
    -------
    columns : list of str
        List of all the strings mentioned in the filters.

    """
    if not filters:
        return []

    if not isinstance(filters, str):
        filters = ' '.join(filters)

    columns = []
    reserved = {'and', 'or', 'in', 'not'}

    for toknum, tokval, _, _, _ in generate_tokens(StringIO(filters).readline):
        if toknum == NAME and tokval not in reserved:
            columns.append(tokval)

    return list(tz.unique(columns))
    def all_subaccounts(self):
        """
        Returns an iterator of all subaccounts that have a recorded transaction
        with the account.

        """
        return tz.unique(t.subaccount for t in self.transactions)
Exemple #6
0
    def columns_used(self):
        """
        Returns all the columns used across all models in the group
        for filtering and in the model expression.

        """
        return list(tz.unique(tz.concat(
            m.columns_used() for m in self.models.values())))
 def columns_used(self):
     """
     Columns from any table used in the model. May come from either
     the choosers or alternatives tables.
     """
     return list(tz.unique(tz.concatv(
         self.choosers_columns_used(),
         #self.alts_columns_used(),
         self.interaction_columns_used())))
 def interaction_columns_used(self):
     """
     Columns from the interaction dataset used for filtering and in
     the model. These may come originally from either the choosers or
     alternatives tables.
     """
     return list(tz.unique(tz.concatv(
         util.columns_in_filters(self.interaction_predict_filters),
         util.columns_in_formula(self.model_expression))))
Exemple #9
0
    def columns_used(self):
        """
        Returns all the columns used across all models in the group
        for filtering and in the model expression.

        """
        return list(
            tz.unique(tz.concat(m.columns_used()
                                for m in self.models.values())))
Exemple #10
0
    def columns_used(self):
        """
        Returns all the columns used in this model for filtering
        and in the model expression.

        """
        return list(tz.unique(tz.concatv(
            util.columns_in_filters(self.fit_filters),
            util.columns_in_filters(self.predict_filters),
            util.columns_in_formula(self.model_expression))))
Exemple #11
0
    def columns_used(self):
        """
        Returns all the columns used in this model for filtering
        and in the model expression.

        """
        return list(
            tz.unique(
                tz.concatv(util.columns_in_filters(self.fit_filters),
                           util.columns_in_filters(self.predict_filters),
                           util.columns_in_formula(self.model_expression))))
Exemple #12
0
    def columns_used(self):
        """
        Returns all the columns used across all models in the group
        for filtering and in the model expression.

        """
        return list(tz.unique(tz.concatv(
            util.columns_in_filters(self.fit_filters),
            util.columns_in_filters(self.predict_filters),
            util.columns_in_formula(self.default_model_expr),
            self._group.columns_used(),
            [self.segmentation_col])))
Exemple #13
0
    def columns_used(self):
        """
        Returns all the columns used across all models in the group
        for filtering and in the model expression.

        """
        return list(
            tz.unique(
                tz.concatv(util.columns_in_filters(self.fit_filters),
                           util.columns_in_filters(self.predict_filters),
                           util.columns_in_formula(self.default_model_expr),
                           self._group.columns_used(),
                           [self.segmentation_col])))
Exemple #14
0
def _column_names_from_metadata(dicts):
    """
    Get the unique set of keys from a list of dictionaries.

    Parameters
    ----------
    dicts : iterable
        Sequence of dictionaries.

    Returns
    -------
    keys : list
        Unique set of keys.

    """
    return list(tz.unique(tz.concat(dicts)))
def _column_names_from_metadata(dicts):
    """
    Get the unique set of keys from a list of dictionaries.

    Parameters
    ----------
    dicts : iterable
        Sequence of dictionaries.

    Returns
    -------
    keys : list
        Unique set of keys.

    """
    return list(tz.unique(tz.concat(dicts)))
Exemple #16
0
def columns_in_formula(formula):
    """
    Returns the names of all the columns used in a patsy formula.

    Parameters
    ----------
    formula : str, iterable, or dict
        Any formula construction supported by ``str_model_expression``.

    Returns
    -------
    columns : list of str

    """
    if formula is None:
        return []

    formula = str_model_expression(formula, add_constant=False)
    columns = []

    tokens = map(
        lambda x: x.extra,
        tz.remove(
            lambda x: x.extra is None,
            _tokens_from_patsy(patsy.parse_formula.parse_formula(formula))))

    for tok in tokens:
        # if there are parentheses in the expression we
        # want to drop them and everything outside
        # and start again from the top
        if '(' in tok:
            start = tok.find('(') + 1
            fin = tok.rfind(')')
            columns.extend(columns_in_formula(tok[start:fin]))
        else:
            for toknum, tokval, _, _, _ in generate_tokens(
                    StringIO(tok).readline):
                if toknum == NAME:
                    columns.append(tokval)

    return list(tz.unique(columns))
def columns_in_formula(formula):
    """
    Returns the names of all the columns used in a patsy formula.

    Parameters
    ----------
    formula : str, iterable, or dict
        Any formula construction supported by ``str_model_expression``.

    Returns
    -------
    columns : list of str

    """
    if formula is None:
        return []

    formula = str_model_expression(formula, add_constant=False)
    columns = []

    tokens = map(
        lambda x: x.extra,
        tz.remove(
            lambda x: x.extra is None,
            _tokens_from_patsy(patsy.parse_formula.parse_formula(formula))))

    for tok in tokens:
        # if there are parentheses in the expression we
        # want to drop them and everything outside
        # and start again from the top
        if '(' in tok:
            start = tok.find('(') + 1
            fin = tok.rfind(')')
            columns.extend(columns_in_formula(tok[start:fin]))
        else:
            for toknum, tokval, _, _, _ in generate_tokens(
                    StringIO(tok).readline):
                if toknum == NAME:
                    columns.append(tokval)

    return list(tz.unique(columns))
Exemple #18
0
def make_household_choices(hh_util):
    """
    Decide on the activity pattern for each household.

    Parameters
    ----------
    hh_util : dict of pandas.Series
        Keys will be household IDs and values will be Series
        mapping alternative choices to their utility.

    Returns
    -------
    choices : pandas.Series
        Maps household ID to chosen alternative, where the alternative
        is a tuple of individual utilities.

    """
    # convert hh_util dict to a few DFs with alternatives in the columns
    # and household IDs in the index
    df_func = tz.compose(
        pd.DataFrame.transpose,
        pd.DataFrame.from_dict)
    grouped_by_size = (
        tz.valfilter(lambda x: len(x) == l, hh_util)
        for l in tz.unique(tz.map(len, hh_util.values())))
    dfs = tz.map(df_func, grouped_by_size)

    # go over all the DFs and do utils_to_probs and make_choices
    choices = (
        pd.Series(
            df.columns[mnl.make_choices(mnl.utils_to_probs(df))].values,
            index=df.index)
        for df in dfs)

    # concat all the resulting Series
    return pd.concat(choices)