# file: datastructures_soa.py
#===============================================================================
# Copyright 2014-2019 Intel Corporation.
#
# This software and the related documents are Intel copyrighted  materials,  and
# your use of  them is  governed by the  express license  under which  they were
# provided to you (License).  Unless the License provides otherwise, you may not
# use, modify, copy, publish, distribute,  disclose or transmit this software or
# the related documents without Intel's prior written permission.
#
# This software and the related documents  are provided as  is,  with no express
# or implied  warranties,  other  than those  that are  expressly stated  in the
# License.
#===============================================================================

## <a name="DAAL-EXAMPLE-PY-DATASTRUCTURES_SOA"></a>
## \example datastructures_soa.py

import os
import sys

import numpy as np

from daal.data_management import BlockDescriptor, SOANumericTable, features, readOnly

utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
if utils_folder not in sys.path:
    sys.path.insert(0, utils_folder)
from utils import printArray


def toString(v):
    if v == features.DAAL_CATEGORICAL:
        return "DAAL_CATEGORICAL"
    elif v == features.DAAL_ORDINAL:
        return "DAAL_ORDINAL"
    elif v == features.DAAL_CONTINUOUS:
        return "DAAL_CONTINUOUS"
    else:
        return "[Unknown FeatureType]"


if __name__ == "__main__":
    print("Structure of array (SOA) numeric table example\n")

    firstReadRow = 0
    nRead = 3
    readFeatureIdx = None
    nObservations = 10
    nFeatures = 4
    dDataSOA = np.array([1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6, 2.8], dtype=np.float64)
    fDataSOA = np.array([3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0], dtype=np.float32)
    iDataSOA = np.array([-10, -20, -30, -40, -50, -60, -70, -80, -90, -100], dtype=np.int32)
    cDataSOA = np.array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5], dtype=np.uint8)

    dataTable = SOANumericTable(nFeatures, nObservations)
    dataTable.setArray(cDataSOA, 0)
    dataTable.setArray(fDataSOA, 1)
    dataTable.setArray(dDataSOA, 2)
    dataTable.setArray(iDataSOA, 3)

    doubleBlock = BlockDescriptor()
    dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, doubleBlock)
    printArray(
        doubleBlock.getArray(), nFeatures, doubleBlock.getNumberOfRows(), doubleBlock.getNumberOfColumns(),
        "Print SOA data structures as double:"
    )
    dataTable.releaseBlockOfRows(doubleBlock)

    readFeatureIdx = 0
    intBlock = BlockDescriptor(ntype=np.intc)
    dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nObservations, readOnly, intBlock)
    printArray(
        intBlock.getArray(), 1, intBlock.getNumberOfRows(), intBlock.getNumberOfColumns(),
        "Print the first feature of SOA:", flt64=False
    )
    dataTable.releaseBlockOfColumnValues(intBlock)

    pDictionary = dataTable.getDictionary()
    print("Number of features in table: " + str(pDictionary.getNumberOfFeatures()))
    print("")

    print("Default type in autogenerated dictionary:")
    for i in range(0, nFeatures):
        featureType = pDictionary[i].featureType
        print("Type of " + str(i) + " feature: " + toString(featureType))
    print("")

    categoricalFeature = pDictionary[0]
    categoricalFeature.featureType = features.DAAL_CATEGORICAL

    print("Modified type in the dictionary:")
    for i in range(0, nFeatures):
        featureType = pDictionary[i].featureType
        print("Type of " + str(i) + " feature: " + toString(featureType))
    print("")
