Beispiel #1
0
 def test_array_slice(self):
     """Test slices applied to a grid."""
     dataset = apply_projection(parse_projection("SimpleGrid[1]"),
                                self.dataset)
     np.testing.assert_array_equal(dataset.SimpleGrid.x.data,
                                   self.dataset.SimpleGrid[1].x.data)
     np.testing.assert_array_equal(dataset.SimpleGrid.y.data,
                                   self.dataset.SimpleGrid[1].y.data)
     np.testing.assert_array_equal(
         dataset.SimpleGrid.SimpleGrid.data,
         self.dataset.SimpleGrid[1:2].SimpleGrid.data)
Beispiel #2
0
 def test_array_slice(self):
     """Test slices applied to a grid."""
     dataset = apply_projection(
         parse_projection("SimpleGrid[1]"), self.dataset)
     np.testing.assert_array_equal(
         dataset.SimpleGrid.x.data, self.dataset.SimpleGrid[1].x.data)
     np.testing.assert_array_equal(
         dataset.SimpleGrid.y.data, self.dataset.SimpleGrid[1].y.data)
     np.testing.assert_array_equal(
         dataset.SimpleGrid.SimpleGrid.data,
         self.dataset.SimpleGrid[1:2].SimpleGrid.data)
Beispiel #3
0
 def test_sequence_projection(self):
     """Test projection slicing on sequences."""
     dataset = apply_projection(parse_projection("sequence[2]"),
                                self.dataset)
     np.testing.assert_array_equal(dataset.sequence.data,
                                   VerySimpleSequence.sequence.data[2])
Beispiel #4
0
 def test_array(self):
     """Test that the grid degenerates into a structure."""
     dataset = apply_projection(parse_projection("SimpleGrid.SimpleGrid"),
                                self.dataset)
     self.assertIsInstance(dataset.SimpleGrid, StructureType)
Beispiel #5
0
 def test_simple_projection_with_index(self):
     """Test simple projections."""
     dataset = apply_projection(parse_projection("x[1]"), self.dataset)
     np.testing.assert_array_equal(dataset.x.data, [1])
Beispiel #6
0
 def test_simple_projection(self):
     """Test simple projections."""
     dataset = apply_projection(parse_projection("x"), self.dataset)
     self.assertEqual(list(dataset.keys()), ["x"])
Beispiel #7
0
 def test_no_projection(self):
     """Test no projections."""
     dataset = apply_projection("", self.dataset)
     self.assertEqual(list(dataset.children()), [])
Beispiel #8
0
 def test_no_projection(self):
     """Test no projections."""
     dataset = apply_projection("", self.dataset)
     self.assertEqual(list(dataset.children()), [])
Beispiel #9
0
 def test_structure_projection(self):
     """Test projection slicing on a structure."""
     with self.assertRaises(ConstraintExpressionError):
         apply_projection(parse_projection("types[0]"), SimpleStructure)
Beispiel #10
0
 def test_sequence_projection(self):
     """Test projection slicing on sequences."""
     dataset = apply_projection(
         parse_projection("sequence[2]"), self.dataset)
     np.testing.assert_array_equal(
         dataset.sequence.data, VerySimpleSequence.sequence.data[2])
Beispiel #11
0
 def test_array(self):
     """Test that the grid degenerates into a structure."""
     dataset = apply_projection(
         parse_projection("SimpleGrid.SimpleGrid"), self.dataset)
     self.assertIsInstance(dataset.SimpleGrid, StructureType)
Beispiel #12
0
 def test_simple_projection_with_index(self):
     """Test simple projections."""
     dataset = apply_projection(parse_projection("x[1]"), self.dataset)
     np.testing.assert_array_equal(
         dataset.x.data, [1])
Beispiel #13
0
 def test_simple_projection(self):
     """Test simple projections."""
     dataset = apply_projection(parse_projection("x"), self.dataset)
     self.assertEqual(list(dataset.keys()), ["x"])
Beispiel #14
0
 def test_structure_projection(self):
     """Test projection slicing on a structure."""
     with self.assertRaises(ConstraintExpressionError):
         apply_projection(parse_projection("types[0]"), SimpleStructure)
Beispiel #15
0
    def __call__(self, environ, start_response):
        # specify that we want the parsed dataset
        environ['x-wsgiorg.want_parsed_response'] = True
        req = Request(environ)
        projection, selection = parse_ce(req.query_string)

        # check if there are any functions calls in the request
        called = (
            any(s for s in selection if FUNCTION.match(s)) or
            any(p for p in projection if isinstance(p, string_types)))

        # ignore DAS requests and requests without functions
        path, response = req.path.rsplit('.', 1)
        if response == 'das' or not called:
            return self.app(environ, start_response)

        # apply selection without any function calls
        req.query_string = '&'.join(
            s for s in selection if not FUNCTION.match(s))
        res = req.get_response(self.app)

        # get the dataset
        method = getattr(res.app_iter, 'x_wsgiorg_parsed_response', False)
        if not method:
            raise ServerError("Unable to call server-side function!")
        dataset = method(DatasetType)

        # apply selection containing server-side functions
        selection = (s for s in selection if FUNCTION.match(s))
        for expr in selection:
            if RELOP.search(expr):
                call, op, other = RELOP.split(expr)
                op = {
                    '<':  operator.lt,
                    '>':  operator.gt,
                    '!=': operator.ne,
                    '=':  operator.eq,
                    '>=': operator.ge,
                    '<=': operator.le,
                    '=~': lambda a, b: re.match(b, a),
                }[op]
                other = ast.literal_eval(other)
            else:
                call, op, other = expr, operator.eq, 1

            # evaluate the function call
            sequence = eval_function(dataset, call, self.functions)

            # is this an inplace call?
            for var in walk(dataset, SequenceType):
                if sequence is var:
                    break
            else:
                # get the data from the resulting variable, and use it to
                # constrain the original dataset
                child = list(sequence.children())[0]
                data = np.fromiter(child.data, child.dtype)
                if data.dtype.char == "S":
                    valid = np.array(
                        list(map(lambda v: op(str(v), str(other)), data)),
                        bool)
                else:
                    valid = op(data, other)

                for sequence in walk(dataset, SequenceType):
                    sequence.data = np.rec.fromrecords(
                        [tuple(row) for row in sequence],
                        names=sequence.keys())[valid]

        # now apply projection
        if projection:
            projection = fix_shorthand(projection, dataset)
            base = [p for p in projection if not isinstance(p, string_types)]
            func = [p for p in projection if isinstance(p, string_types)]

            # apply non-function projection
            out = apply_projection(base, dataset)

            # apply function projection
            for call in func:
                var = eval_function(dataset, call, self.functions)
                for child in walk(var):
                    parent = reduce(
                        operator.getitem, [out] + child.id.split('.')[:-1])
                    if child.name not in parent.keys():
                        parent[child.name] = child
                        break
            dataset = out

        # Return the original response (DDS, DAS, etc.)
        path, response = req.path.rsplit('.', 1)
        res = BaseHandler.responses[response](dataset)

        return res(environ, start_response)
Beispiel #16
0
    def __call__(self, environ, start_response):
        # specify that we want the parsed dataset
        environ['x-wsgiorg.want_parsed_response'] = True
        req = Request(environ)
        original_query = req.query_string
        projection, selection = parse_ce(req.query_string)

        # apply selection without any function calls
        req.query_string = '&'.join(expr for expr in selection if not FUNCTION.match(expr))
        res = req.get_response(self.app)

        # ignore DAS requests
        path, response = req.path.rsplit('.', 1)
        if response == 'das':
            return self.app(environ, start_response)

        # get the dataset
        method = getattr(res.app_iter, 'x_wsgiorg_parsed_response', False)
        if not method:
            environ['QUERY_STRING'] = original_query
            return self.app(environ, start_response)
        dataset = method(DatasetType)

        # apply selection containing server-side functions
        selection = (expr for expr in selection if FUNCTION.match(expr))
        for expr in selection:
            if RELOP.search(expr):
                call, op, other = RELOP.split(expr)
                op = {
                        '<' : operator.lt,
                        '>' : operator.gt,
                        '!=': operator.ne,
                        '=' : operator.eq,
                        '>=': operator.ge,
                        '<=': operator.le,
                        '=~': lambda a, b: re.match(b, a),
                }[op]
                other = ast.literal_eval(other)
            else:
                call, op, other = expr, operator.eq, 1

            # evaluate the function call
            sequence = eval_function(dataset, call, self.functions)

            # is this an inplace call?
            for var in walk(dataset, SequenceType):
                if sequence is var:
                    break
            else:
                # get the data from the resulting variable, and use it to 
                # constrain the original dataset
                data = np.fromiter(sequence)
                print data.shape
                valid = op(data, other)

                for sequence in walk(dataset, SequenceType):
                    data = np.asarray(list(sequence), 'O')[valid]
                    sequence.data = np.asarray(list(sequence), 'O')[valid]
                dataset = out

        # now apply projection
        if projection:
            projection = fix_shorthand(projection, dataset)
            base = [p for p in projection if not isinstance(p, basestring)] 
            func = [p for p in projection if isinstance(p, basestring)] 

            # apply non-function projection
            out = apply_projection(base, dataset)

            # apply function projection
            for call in func:
                var = eval_function(dataset, call, self.functions)
                for child in walk(var):
                    parent = reduce(operator.getitem, [out] + child.id.split('.')[:-1])
                    if child.name not in parent.keys():
                        parent[child.name] = child
                        break
            dataset = out

        # Return the original response (DDS, DAS, etc.)
        path, response = req.path.rsplit('.', 1)
        res = BaseHandler.responses[response](dataset)

        return res(environ, start_response)
Beispiel #17
0
    def __call__(self, environ, start_response):
        # specify that we want the parsed dataset
        environ['x-wsgiorg.want_parsed_response'] = True
        req = Request(environ)
        projection, selection = parse_ce(req.query_string)

        # check if there are any functions calls in the request
        called = (any(s for s in selection if FUNCTION.match(s))
                  or any(p for p in projection if isinstance(p, string_types)))

        # ignore DAS requests and requests without functions
        path, response = req.path.rsplit('.', 1)
        if response == 'das' or not called:
            return self.app(environ, start_response)

        # apply selection without any function calls
        req.query_string = '&'.join(s for s in selection
                                    if not FUNCTION.match(s))
        res = req.get_response(self.app)

        # get the dataset
        method = getattr(res.app_iter, 'x_wsgiorg_parsed_response', False)
        if not method:
            raise ServerError("Unable to call server-side function!")
        dataset = method(DatasetType)

        # apply selection containing server-side functions
        selection = (s for s in selection if FUNCTION.match(s))
        for expr in selection:
            if RELOP.search(expr):
                call, op, other = RELOP.split(expr)
                op = {
                    '<': operator.lt,
                    '>': operator.gt,
                    '!=': operator.ne,
                    '=': operator.eq,
                    '>=': operator.ge,
                    '<=': operator.le,
                    '=~': lambda a, b: re.match(b, a),
                }[op]
                other = ast.literal_eval(other)
            else:
                call, op, other = expr, operator.eq, 1

            # evaluate the function call
            sequence = eval_function(dataset, call, self.functions)

            # is this an inplace call?
            for var in walk(dataset, SequenceType):
                if sequence is var:
                    break
            else:
                # get the data from the resulting variable, and use it to
                # constrain the original dataset
                child = list(sequence.children())[0]
                data = np.fromiter(child.data, child.dtype)
                if data.dtype.char == "S":
                    valid = np.array(
                        list(map(lambda v: op(str(v), str(other)), data)),
                        bool)
                else:
                    valid = op(data, other)

                for sequence in walk(dataset, SequenceType):
                    sequence.data = np.rec.fromrecords(
                        [tuple(row) for row in sequence],
                        names=sequence.keys())[valid]

        # now apply projection
        if projection:
            projection = fix_shorthand(projection, dataset)
            base = [p for p in projection if not isinstance(p, string_types)]
            func = [p for p in projection if isinstance(p, string_types)]

            # apply non-function projection
            out = apply_projection(base, dataset)

            # apply function projection
            for call in func:
                var = eval_function(dataset, call, self.functions)
                for child in walk(var):
                    parent = reduce(operator.getitem,
                                    [out] + child.id.split('.')[:-1])
                    if child.name not in parent.keys():
                        parent[child.name] = child
                        break
            dataset = out

        # Return the original response (DDS, DAS, etc.)
        path, response = req.path.rsplit('.', 1)
        res = BaseHandler.responses[response](dataset)

        return res(environ, start_response)