Example #1
0
    def evaluate(self, context):
        entity = context['__entity__']

        baseperiod = entity.base_period
        period = context['period'] - 1
        expr = self.expr

        typemap = {bool: int, int: int, float: float}
        res_type = typemap[dtype(expr, context)]
        res_size = len(entity.array)

        sum_values = np.zeros(res_size, dtype=res_type)
        id_to_rownum = context.id_to_rownum
        while period >= baseperiod:
            ids, values = entity.value_for_period(expr, period, context,
                                                  fill=None)

            # filter out lines which are present because there was a value for
            # that individual at that period but not for that column
            acceptable_rows = hasvalue(values)
            acceptable_ids = ids[acceptable_rows]
            if len(acceptable_ids):
                acceptable_values = values[acceptable_rows]

                value_rows = id_to_rownum[acceptable_ids]

                period_value = np.zeros(res_size, dtype=np.float)
                safe_put(period_value, value_rows, acceptable_values)

                sum_values += period_value
            period -= 1
        return sum_values
Example #2
0
 def evaluate(self, context):
     if self.filter is None:
         return context_length(context)
     else:
         # TODO: check this at "compile" time (in __init__), though for
         # that we need to know the type of all temporary variables
         # first
         if dtype(self.filter, context) is not bool:
             raise Exception("grpcount filter must be a boolean expression")
         return np.sum(expr_eval(self.filter, context))
Example #3
0
 def _getfilter(self, context):
     ctx_filter = context.get('__filter__')
     if self.filter is not None and ctx_filter is not None:
         filter_expr = ctx_filter & self.filter
     elif self.filter is not None:
         filter_expr = self.filter
     elif ctx_filter is not None:
         filter_expr = ctx_filter
     else:
         filter_expr = None
     if filter_expr is not None and dtype(filter_expr, context) is not bool:
         raise Exception("filter must be a boolean expression")
     return filter_expr
Example #4
0
    def evaluate(self, context):
        expr = self.expr

        # FIXME: either take "contextual filter" into account here (by using
        # self._getfilter), or don't do it in grpsum & grpgini
        if self.filter is not None:
            filter_values = expr_eval(self.filter, context)
            tmp_varname = get_tmp_varname()
            context = context.copy()
            context[tmp_varname] = filter_values
            if dtype(expr, context) is bool:
                # convert expr to int because mul_bbb is not implemented in
                # numexpr
                expr *= 1
            expr *= Variable(tmp_varname)
        else:
            filter_values = True

        values = expr_eval(expr, context)
        values = np.asarray(values)

        if self.skip_na:
            # we should *not* use an inplace operation because filter_values
            # can be a simple variable
            filter_values = filter_values & ispresent(values)

        if filter_values is True:
            numrows = len(values)
        else:
            numrows = np.sum(filter_values)

        if numrows:
            if self.skip_na:
                return na_sum(values) / float(numrows)
            else:
                return np.sum(values) / float(numrows)
        else:
            return float("nan")
Example #5
0
 def evaluate(self, context):
     expr = self.expr
     #FIXME: either take "contextual filter" into account here (by using
     # self._getfilter), or don't do it in grpsum (& grpgini?)
     if self.filter is not None:
         filter_values = expr_eval(self.filter, context)
         tmp_varname = get_tmp_varname()
         context = context.copy()
         context[tmp_varname] = filter_values
         if dtype(expr, context) is bool:
             # convert expr to int because mul_bbb is not implemented in
             # numexpr
             expr *= 1
         expr *= Variable(tmp_varname)
     else:
         filter_values = True
     values = expr_eval(expr, context)
     filter_values &= np.isfinite(values)
     numrows = np.sum(filter_values)
     if numrows:
         return np.nansum(values) / float(numrows)
     else:
         return float('nan')
Example #6
0
 def dtype(self, context):
     assert dtype(self.cond, context) == bool
     return coerce_types(context, self.iftrue, self.iffalse)
Example #7
0
 def dtype(self, context):
     assert dtype(self.expr, context) == float
     return int
Example #8
0
 def dtype(self, context):
     # result dtype is the same as the input dtype
     res = dtype(self.args[0], context)
     assert res == float
     return res
Example #9
0
 def dtype(self, context):
     return dtype(self.expr1, context)
Example #10
0
 def dtype(self, context):
     assert dtype(self.expr, context) == bool
     return int
Example #11
0
 def dtype(self, context):
     target_context = self.target_context(context)
     return dtype(self.target_expr, target_context)
Example #12
0
 def dtype(self, context):
     target_context = self.target_context(context)
     expr_dype = dtype(self.target_expr, target_context)
     #TODO: merge this typemap with tsum's
     typemap = {bool: int, int: int, float: float}
     return typemap[expr_dype]
Example #13
0
 def add_filter(self, expr, context):
     if self.filter is not None:
         missing_value = missing_values[dtype(expr, context)]
         return Where(self.filter, expr, missing_value)
     else:
         return expr
Example #14
0
 def dtype(self, context):
     return dtype(self.args[0], context)
Example #15
0
 def dtype(self, context):
     # TODO: merge this typemap with tsum's
     typemap = {bool: int, int: int, float: float}
     return typemap[dtype(self.args[0], context)]