|
| 1 | +# ***************************************************************************** |
| 2 | +# Copyright (c) 2019, Intel Corporation All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# |
| 7 | +# Redistributions of source code must retain the above copyright notice, |
| 8 | +# this list of conditions and the following disclaimer. |
| 9 | +# |
| 10 | +# Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | +# |
| 14 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 | +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 18 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 21 | +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 22 | +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 | +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 24 | +# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | +# ***************************************************************************** |
| 26 | + |
| 27 | + |
| 28 | +import numba |
| 29 | +from numba import types, cgutils |
| 30 | +from numba.extending import (models, register_model, make_attribute_wrapper) |
| 31 | + |
| 32 | +from sdc.str_ext import string_type |
| 33 | + |
| 34 | + |
| 35 | +class DataFrameType(types.Type): # TODO: IterableType over column names |
| 36 | + """Temporary type class for DataFrame objects. |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self, data=None, index=None, columns=None, has_parent=False): |
| 40 | + self.data = data |
| 41 | + if index is None: |
| 42 | + index = types.none |
| 43 | + self.index = index |
| 44 | + self.columns = columns |
| 45 | + # keeping whether it is unboxed from Python to enable reflection of new |
| 46 | + # columns |
| 47 | + self.has_parent = has_parent |
| 48 | + super(DataFrameType, self).__init__( |
| 49 | + name="dataframe({}, {}, {}, {})".format(data, index, columns, has_parent)) |
| 50 | + |
| 51 | + def copy(self, index=None, has_parent=None): |
| 52 | + # XXX is copy necessary? |
| 53 | + if index is None: |
| 54 | + index = types.none if self.index == types.none else self.index.copy() |
| 55 | + data = tuple(a.copy() for a in self.data) |
| 56 | + if has_parent is None: |
| 57 | + has_parent = self.has_parent |
| 58 | + return DataFrameType(data, index, self.columns, has_parent) |
| 59 | + |
| 60 | + @property |
| 61 | + def key(self): |
| 62 | + # needed? |
| 63 | + return self.data, self.index, self.columns, self.has_parent |
| 64 | + |
| 65 | + def unify(self, typingctx, other): |
| 66 | + if (isinstance(other, DataFrameType) |
| 67 | + and len(other.data) == len(self.data) |
| 68 | + and other.columns == self.columns |
| 69 | + and other.has_parent == self.has_parent): |
| 70 | + new_index = types.none |
| 71 | + if self.index != types.none and other.index != types.none: |
| 72 | + new_index = self.index.unify(typingctx, other.index) |
| 73 | + elif other.index != types.none: |
| 74 | + new_index = other.index |
| 75 | + elif self.index != types.none: |
| 76 | + new_index = self.index |
| 77 | + |
| 78 | + data = tuple(a.unify(typingctx, b) for a, b in zip(self.data, other.data)) |
| 79 | + return DataFrameType(data, new_index, self.columns, self.has_parent) |
| 80 | + |
| 81 | + def is_precise(self): |
| 82 | + return all(a.is_precise() for a in self.data) and self.index.is_precise() |
| 83 | + |
| 84 | + |
| 85 | +@register_model(DataFrameType) |
| 86 | +class DataFrameModel(models.StructModel): |
| 87 | + def __init__(self, dmm, fe_type): |
| 88 | + n_cols = len(fe_type.columns) |
| 89 | + members = [ |
| 90 | + ('data', types.Tuple(fe_type.data)), |
| 91 | + ('index', fe_type.index), |
| 92 | + ('columns', types.UniTuple(string_type, n_cols)), |
| 93 | + # for lazy unboxing of df coming from Python (usually argument) |
| 94 | + # list of flags noting which columns and index are unboxed |
| 95 | + # index flag is last |
| 96 | + ('unboxed', types.UniTuple(types.int8, n_cols + 1)), |
| 97 | + ('parent', types.pyobject), |
| 98 | + ] |
| 99 | + super(DataFrameModel, self).__init__(dmm, fe_type, members) |
| 100 | + |
| 101 | + |
| 102 | +make_attribute_wrapper(DataFrameType, 'data', '_data') |
| 103 | +make_attribute_wrapper(DataFrameType, 'index', '_index') |
| 104 | +make_attribute_wrapper(DataFrameType, 'columns', '_columns') |
| 105 | +make_attribute_wrapper(DataFrameType, 'unboxed', '_unboxed') |
| 106 | +make_attribute_wrapper(DataFrameType, 'parent', '_parent') |
0 commit comments