File size: 2,837 Bytes
2c3c408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import tiledb.cc as lt


class Aggregation:
    """
    Proxy object returned by Query.agg to calculate aggregations.
    """

    def __init__(self, query=None, attr_to_aggs={}):
        if query is None:
            raise ValueError("must pass in a query object")

        self.query = query
        self.attr_to_aggs = attr_to_aggs

    def __getitem__(self, selection):
        from .main import PyAgg
        from .subarray import Subarray

        array = self.query.array
        order = self.query.order

        layout = (
            lt.LayoutType.UNORDERED if array.schema.sparse else lt.LayoutType.ROW_MAJOR
        )
        if order is None or order == "C":
            layout = lt.LayoutType.ROW_MAJOR
        elif order == "F":
            layout = lt.LayoutType.COL_MAJOR
        elif order == "G":
            layout = lt.LayoutType.GLOBAL_ORDER
        elif order == "U":
            layout = lt.LayoutType.UNORDERED
        else:
            raise ValueError(
                "order must be 'C' (TILEDB_ROW_MAJOR), "
                "'F' (TILEDB_COL_MAJOR), "
                "'G' (TILEDB_GLOBAL_ORDER), "
                "or 'U' (TILEDB_UNORDERED)"
            )

        q = PyAgg(array._ctx_(), array, layout, self.attr_to_aggs)

        from .libtiledb import (
            index_as_tuple,
            index_domain_subarray,
            replace_ellipsis,
            replace_scalars_slice,
        )

        selection = index_as_tuple(selection)
        dom = array.schema.domain
        idx = replace_ellipsis(dom.ndim, selection)
        idx, drop_axes = replace_scalars_slice(dom, idx)
        dim_ranges = index_domain_subarray(array, dom, idx)

        subarray = Subarray(array, array._ctx_())
        subarray.add_ranges([list([x]) for x in dim_ranges])
        q.set_subarray(subarray)

        cond = self.query.cond
        if cond is not None and cond != "":
            from .query_condition import QueryCondition

            if isinstance(cond, str):
                q.set_cond(QueryCondition(cond))
            else:
                raise TypeError("`cond` expects type str.")

        result = q.get_aggregate()

        # If there was only one attribute, just show the aggregate results
        if len(result) == 1:
            result = result[list(result.keys())[0]]

            # If there was only one aggregate, just show the value
            if len(result) == 1:
                result = result[list(result.keys())[0]]

        return result

    @property
    def multi_index(self):
        """Apply Array.multi_index with query parameters."""
        from .multirange_indexing import MultiRangeAggregation

        return MultiRangeAggregation(self.query.array, query=self)

    @property
    def df(self):
        raise NotImplementedError(".df indexer not supported for Aggregations")