def create_categorical(dataset, categories, rules, name, alias, description=""):
    """
    method for creating a categorical variable
    """

    if not ((len(categories) - 1) <= len(rules) <= len(categories)):
        raise ValueError("Amount of rules should match categories (or categories -1")

    if not hasattr(dataset, "variables"):
        dataset.refresh()

    args = [
        {"column": [c["id"] for c in categories], "type": {"value": {"class": "categorical", "categories": categories}}}
    ]

    more_args = []
    for rule in rules:
        more_args.append(parse_expr(rule))

    more_args = process_expr(more_args, dataset)

    expr = dict(function="case", args=args + more_args)

    payload = dict(element="shoji:entity", body=dict(alias=alias, name=name, expr=expr, description=description))

    return dataset.variables.create(payload)
Example #2
0
    def create_categorical(self, categories, rules, name, alias, description="", missing=True):
        """
        creates a categorical variable deriving from other variables
        """
        if not ((len(categories) - 1) <= len(rules) <= len(categories)):
            raise ValueError("Amount of rules should match categories (or categories -1)")

        if not hasattr(self, "variables"):
            self.refresh()

        args = [
            {
                "column": [c["id"] for c in categories],
                "type": {"value": {"class": "categorical", "categories": categories}},
            }
        ]

        for cat in args[0]["type"]["value"]["categories"]:
            cat.setdefault("missing", False)

        if missing:
            args[0]["column"].append(-1)
            args[0]["type"]["value"]["categories"].append(dict(id=-1, name="No Data", numeric_value=None, missing=True))

        more_args = []
        for rule in rules:
            more_args.append(parse_expr(rule))

        more_args = process_expr(more_args, self)

        expr = dict(function="case", args=args + more_args)

        payload = dict(element="shoji:entity", body=dict(alias=alias, name=name, expr=expr, description=description))

        return self.variables.create(payload)
Example #3
0
    def create_categorical(self,
                           categories,
                           rules,
                           name,
                           alias,
                           description='',
                           missing=True):
        """
        creates a categorical variable deriving from other variables
        """
        if not ((len(categories) - 1) <= len(rules) <= len(categories)):
            raise ValueError(
                'Amount of rules should match categories (or categories -1)')

        if not hasattr(self, 'variables'):
            self.refresh()

        args = [{
            'column': [c['id'] for c in categories],
            'type': {
                'value': {
                    'class': 'categorical',
                    'categories': categories
                }
            }
        }]

        for cat in args[0]['type']['value']['categories']:
            cat.setdefault('missing', False)

        if missing:
            args[0]['column'].append(-1)
            args[0]['type']['value']['categories'].append(
                dict(id=-1, name='No Data', numeric_value=None, missing=True))

        more_args = []
        for rule in rules:
            more_args.append(parse_expr(rule))

        more_args = process_expr(more_args, self)

        expr = dict(function='case', args=args + more_args)

        payload = dict(element='shoji:entity',
                       body=dict(alias=alias,
                                 name=name,
                                 expr=expr,
                                 description=description))

        return self.variables.create(payload)
Example #4
0
    def exclude(self, expr=None):
        """
        Given a dataset object, apply an exclusion filter to it (defined as an
        expression string).

        If the `expr` parameter is None, an empty expression object is sent
        as part of the PATCH request, which effectively removes the exclusion
        filter (if any).
        """
        if isinstance(expr, six.string_types):
            expr_obj = parse_expr(expr)
            expr_obj = process_expr(expr_obj, self)  # cause we need URLs
        elif expr is None:
            expr_obj = {}
        else:
            expr_obj = expr
        return self.session.patch(self.fragments.exclusion, data=json.dumps(dict(expression=expr_obj)))
Example #5
0
    def exclude(self, expr=None):
        """
        Given a dataset object, apply an exclusion filter to it (defined as an
        expression string).

        If the `expr` parameter is None, an empty expression object is sent
        as part of the PATCH request, which effectively removes the exclusion
        filter (if any).
        """
        if isinstance(expr, six.string_types):
            expr_obj = parse_expr(expr)
            expr_obj = process_expr(expr_obj, self)  # cause we need URLs
        elif expr is None:
            expr_obj = {}
        else:
            expr_obj = expr
        return self.session.patch(self.fragments.exclusion,
                                  data=json.dumps(dict(expression=expr_obj)))