def create_from_list_or_dict(cls, dataset, calculations): calculations = to_list(calculations) if not len(calculations) or not isinstance(calculations, list) or\ any([not isinstance(e, dict) for e in calculations]): raise ArgumentError('Improper format for JSON calculations.') parsed_calculations = [] # Pull out args to check JSON format try: for c in calculations: groups = c.get("groups") if not isinstance(groups, list): groups = [groups] for group in groups: parsed_calculations.append( [c[cls.FORMULA], c[cls.NAME], group]) except KeyError as e: raise ArgumentError('Required key %s not found in JSON' % e) # Save instead of create so that we calculate on all at once. calculations = [ cls().save(dataset, formula, name, group) for formula, name, group in parsed_calculations ] call_async(calculate_task, calculations, dataset.clear_cache())
def __parse_select(self, select, required=False): if required and select is None: raise ArgumentError('no select') if select == self.SELECT_ALL_FOR_SUMMARY: select = None elif select is not None: select = safe_json_loads(select, error_title='select') if not isinstance(select, dict): msg = 'select argument must be a JSON dictionary, found: %s.' raise ArgumentError(msg % select) return select
def delete(self, dataset): """Delete this calculation. First ensure that there are no other calculations which depend on this one. If not, start a background task to delete the calculation. :param dataset: Dataset for this calculation. :raises: `DependencyError` if dependent calculations exist. :raises: `ArgumentError` if group is not in DataSet or calculation does not exist for DataSet. """ if len(self.dependent_calculations): msg = 'Cannot delete, calculations %s depend on this calculation.' raise DependencyError(msg % self.dependent_calculations) if not self.group is None: # it is an aggregate calculation dataset = dataset.aggregated_dataset(self.group) if not dataset: msg = 'Aggregation with group "%s" does not exist for dataset.' raise ArgumentError(msg % self.group) call_async(delete_task, self, dataset)
def action(dataset): if json_file: calculations = safe_json_loads(json_file.file.read()) Calculation.create_from_list_or_dict(dataset, calculations) success_message = 'created calculations from JSON' elif formula is None or name is None: raise ArgumentError('Must provide both formula and name argume' 'nts, or json_file argument') else: Calculation.create(dataset, formula, name, group) return self._success('created calculation: %s' % name, dataset_id)
def delete_columns(self, columns): """Delete column `column` from this dataset. :param column: The column to delete. """ columns = set(self.schema.keys()).intersection(set(to_list(columns))) if not len(columns): raise ArgumentError("Columns: %s not in dataset.") Observation.delete_columns(self, columns) new_schema = self.schema [new_schema.pop(c) for c in columns] self.set_schema(new_schema, set_num_columns=True) return columns
def action(dataset, query=query, select=select, limit=limit): if not dataset.is_ready: raise ArgumentError('dataset is not finished importing') limit = parse_int(limit, 0) query = self.__parse_query(query) select = self.__parse_select(select, required=True) groups = dataset.split_groups(group) [valid_column(dataset, c) for c in groups] # if select append groups to select if select: select.update(dict(zip(groups, [1] * len(groups)))) query_args = QueryArgs(query=query, select=select, limit=limit, order_by=order_by) dframe = dataset.dframe(query_args) return dataset.summarize(dframe, groups=groups, no_cache=query or select, flat=flat)
def action(dataset, select=select): query_args = self.__parse_query_args(limit, order_by, query, select, dataset=dataset) numerics_select = dataset.schema.numerics_select query_select = query_args.select if query_select is None: query_select = numerics_select else: query_select = {k: v for k, v in query_select.items() if k in numerics_select.keys()} if not query_select: raise ArgumentError( 'No numeric columns for dataset, or no select columns are ' 'numeric. Select: %s.' % select) if index: query_select[index] = 1 valid_column(dataset, index) if group: query_select[group] = 1 valid_column(dataset, group) query_args.select = query_select dframe = dataset.dframe(query_args=query_args).dropna() axis = None if group or index: agg = self.__parse_aggregation(aggregation) if index: if group: groupby = dframe.groupby(group) dframes = [] for g in groupby.groups.keys(): renamed = {c: '%s %s' % (c, g) for c in dframe.columns} data = groupby.get_group(g).groupby(index).agg(agg) dframes.append(data.rename(columns=renamed)) dframe = concat(dframes).fillna(0).reset_index().groupby( index).agg(agg).sort_index() else: dframe = dframe.groupby(index).agg(agg) elif group: dframe = dframe.groupby(group).agg(agg).reset_index() axis = dframe[group].tolist() dframe = dframe.drop(group, axis=1) if vega: dframe.index = axis or dframe.index.map(float) vis = vincent.Bar() vis.tabular_data(dframe, columns=dframe.columns.tolist()[0:1]) return vis.vega else: vis = bearcart.Chart(dframe, plt_type=plot_type, width=width, height=height, palette=palette, x_axis=axis or True, x_time=index is not None) return vis.build_html()
def valid_column(dataset, c): if c not in dataset.columns: raise ArgumentError("'%s' is not a column for this dataset." % c)