|
| 1 | +# ============================================================================== |
| 2 | +# Copyright 2023 Intel Corporation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ============================================================================== |
| 16 | + |
| 17 | +import numpy as np |
| 18 | + |
| 19 | +import onedal |
| 20 | +from ..array import to_array |
| 21 | + |
| 22 | +from .dtype_conversion import get_data_type |
| 23 | +from .column_builder import build_from_column |
| 24 | +from .dataframe_protocol import Column, DataFrame |
| 25 | + |
| 26 | +feature_type = onedal._backend.ftype |
| 27 | +type_array = onedal._backend.data_management.array_q |
| 28 | +metadata = onedal._backend.data_management.table_metadata |
| 29 | +heterogen_table = onedal._backend.data_management.heterogen_table |
| 30 | + |
| 31 | +class DataFrameBuilder: |
| 32 | + def __init__(self): |
| 33 | + self.dtypes = [] |
| 34 | + self.columns = [] |
| 35 | + |
| 36 | + @property |
| 37 | + def column_count(self) -> int: |
| 38 | + result = len(self.columns) |
| 39 | + control = len(self.dtypes) |
| 40 | + assert result == control |
| 41 | + return result |
| 42 | + |
| 43 | + def append(self, column: Column): |
| 44 | + self.dtypes.append(column.dtype) |
| 45 | + self.columns.append(column) |
| 46 | + return self |
| 47 | + |
| 48 | + def build_dtype_array(self) -> type_array: |
| 49 | + result = list() |
| 50 | + for index in range(self.column_count): |
| 51 | + dtype = self.dtypes[index] |
| 52 | + dal_dtype = get_data_type(dtype) |
| 53 | + result.append(dal_dtype) |
| 54 | + result = np.asarray(result) |
| 55 | + result = result.astype(np.int32) |
| 56 | + return to_array(result) |
| 57 | + |
| 58 | + # TODO: implement logic supporting |
| 59 | + def build_ftype_array(self) -> type_array: |
| 60 | + ratio = feature_type.ratio |
| 61 | + col_count = self.column_count |
| 62 | + result = np.full(col_count, ratio) |
| 63 | + result = result.astype(np.int32) |
| 64 | + return to_array(result) |
| 65 | + |
| 66 | + def build_metadata(self) -> metadata: |
| 67 | + dtypes = self.build_dtype_array() |
| 68 | + ftypes = self.build_ftype_array() |
| 69 | + return metadata(dtypes, ftypes) |
| 70 | + |
| 71 | + def __validate(self, result: heterogen_table): |
| 72 | + column_count = result.get_column_count() |
| 73 | + assert column_count == self.column_count |
| 74 | + |
| 75 | + def build(self) -> heterogen_table: |
| 76 | + meta = self.build_metadata() |
| 77 | + result = heterogen_table(meta) |
| 78 | + |
| 79 | + |
| 80 | + for index in range(self.column_count): |
| 81 | + column = self.columns[index] |
| 82 | + array = build_from_column(column) |
| 83 | + result.set_column(index, array) |
| 84 | + |
| 85 | + self.__validate(result) |
| 86 | + return result |
| 87 | + |
| 88 | +def build_from_dataframe(df: DataFrame) -> heterogen_table: |
| 89 | + column_count = df.num_columns() |
| 90 | + builder = DataFrameBuilder() |
| 91 | + |
| 92 | + for index in range(column_count): |
| 93 | + column = df.get_column(index) |
| 94 | + builder.append(column) |
| 95 | + |
| 96 | + result = builder.build() |
| 97 | + row_count = result.get_row_count() |
| 98 | + assert row_count == df.num_rows() |
| 99 | + return result |
0 commit comments