def post_init(self): need = self.args[1] if isinstance(need, basestring): fpath = os.path.join(config.input_directory, need) need = load_ndarray(fpath, float) #XXX: store args in a list so that we can modify it? self.args = (self.args[0], need) + self.args[2:] self.past_error = None
def __init__(self, score, need, filter=None, take=None, leave=None, expressions=None, possible_values=None, errors='default', frac_need='uniform', link=None, secondary_axis=None, method='default'): super(AlignmentAbsoluteValues, self).__init__(score, filter) if isinstance(need, basestring): fpath = os.path.join(config.input_directory, need) need = load_ndarray(fpath, float) # need is a single scalar if not isinstance(need, (tuple, list, Expr, np.ndarray)): need = [need] # need is a simple list (no expr inside) if isinstance(need, (tuple, list)) and \ not any(isinstance(p, Expr) for p in need): need = np.array(need) self.need = need if expressions is None: expressions = [] self.expressions = expressions if possible_values is None: possible_values = [] else: possible_values = [np.array(pv) for pv in possible_values] self.possible_values = possible_values self.take_filter = take self.leave_filter = leave self.errors = errors self.past_error = None self.frac_need = frac_need if frac_need not in ('uniform', 'cutoff', 'round'): raise Exception("frac_need should be one of: 'uniform', 'cutoff' " "or 'round'") self.link = link if secondary_axis is not None and link is None: raise Exception("the 'secondary_axis' argument is only valid in " "combination with the 'link' argument") if not isinstance(secondary_axis, (type(None), int, Variable)): raise Exception("'secondary_axis' should be either an integer or " "an axis name") self.secondary_axis = secondary_axis if method in ("default","sidewalk") : self.method = method else: raise Exception("Method for alignment should be either 'default' " "either 'sidewalk'")
def __init__(self, *args, **kwargs): super(AlignmentAbsoluteValues, self).__init__(*args, **kwargs) need = self.args[1] if isinstance(need, basestring): fpath = os.path.join(config.input_directory, need) need = load_ndarray(fpath, float) # XXX: store args in a list so that we can modify it? self.args = (self.args[0], need) + self.args[2:] self.past_error = None
def compute(self, context, fname, type=None, fields=None): # TODO: move those checks to __init__ if type is None and fields is None: raise ValueError("type or fields must be specified") if type is not None and fields is not None: raise ValueError("cannot specify both type and fields") if type is not None: return load_ndarray(os.path.join(config.input_directory, fname), type) elif fields is not None: return load_table(os.path.join(config.input_directory, fname), fields)
def __init__(self, fname): data = load_ndarray(os.path.join(config.input_directory, fname)) # TODO: handle more dimensions. For that we need to evaluate a # different expr depending on the values for the other dimensions # we will need to either # 1) create awful expressions with lots of nested if() (X*Y*Z) # OR # 2) use groupby (or partition_nd) # the problem with groupby is that once we have one vector of values # for each group, we have to recombine them into a single vector # result = np.empty(context_length(context), dtype=expr.dtype) # groups = partition_nd(filtered_columns, True, possible_values) # if not groups: # return # contexts = [filtered_context.subset(indices, expr_vars, not_hashable) # for indices in groups] # data = [expr_eval(expr, c) for c in contexts] # for group_indices, group_values in zip(groups, data): # result[group_indices] = group_values # 3) use a lookup for each individual & coef (we can only envision # this during the evaluation of the larger expression if done via numba, # otherwise it will be too slow # expr = age * AGECOEF[gender, xyz] + eduach * EDUCOEF[gender, xyz] # 4) compute the coefs separately # 4a) via nested if() # AGECOEF = if(gender, if(workstate == 1, a, if(workstate == 2, b, c) # if(workstate == 1, a, if(workstate == 2, b, c)) # EDUCOEF = ... # expr = age * AGECOEF + eduach * EDUCOEF # 4b) via lookup # AGECOEF = AGECOEFS[gender, workstate] # EDUCOEF = EDUCOEFS[gender, workstate] # expr = age * AGECOEF + eduach * EDUCOEF # Note, in general, we could make # EDUCOEFS (sans rien) equivalent to EDUCOEFS[:, :, period] s'il y a # une dimension period en 3eme position # et non à EDUCOEFS[gender, workstate, period] car ca pose probleme # pour l'alignement (on a pas besoin d'une valeur par personne) # in general, we could let user tell explicitly which fields they want # to index by (autoindex: period) for periodic fields_dim = data.dim_names.index('fields') fields_axis = data.axes[fields_dim] self.names = list(fields_axis.labels) self.coefs = list(data) # needed for compatibility with CompoundExpression self.args = [] self.kwargs = []
def post_init(self): need = self.args[1] if isinstance(need, basestring): fpath = os.path.join(config.input_directory, need) need = load_ndarray(fpath, float) #<<<<<<< HEAD # need is a single scalar if not isinstance(need, (tuple, list, Expr, np.ndarray)): need = [need] # need is a simple list (no expr inside) if isinstance(need, (tuple, list)) and \ not any(isinstance(p, Expr) for p in need): need = np.array(need) if need is None and method != 'sidewalk': raise Exception("No default value for need if method is not sidewalk") self.need = need if expressions is None: expressions = [] self.expressions = expressions if possible_values is None: possible_values = [] else: possible_values = [np.array(pv) for pv in possible_values] self.possible_values = possible_values self.take_filter = take self.leave_filter = leave self.errors = errors #======= # #XXX: store args in a #list so that we can modify it? # self.args = (self.args#[0], need) + self.args[2:] #>>>>>>> liam2/master self.past_error = None assert(periodicity_given in time_period) self.periodicity_given = time_period[periodicity_given] self.frac_need = frac_need if frac_need not in ('uniform', 'cutoff', 'round'): raise Exception("frac_need should be one of: 'uniform', 'cutoff' " "or 'round'") self.link = link if secondary_axis is not None and link is None: raise Exception("the 'secondary_axis' argument is only valid in " "combination with the 'link' argument") if not isinstance(secondary_axis, (type(None), int, Variable)): raise Exception("'secondary_axis' should be either an integer or " "an axis name") self.secondary_axis = secondary_axis if method in ("default","sidewalk") : self.method = method else: raise Exception("Method for alignment should be either 'default' " "either 'sidewalk'")