#===============================================================================
# Copyright 2014-2017 Intel Corporation
# All Rights Reserved.
#
# If this  software was obtained  under the  Intel Simplified  Software License,
# the following terms apply:
#
# The source code,  information  and material  ("Material") contained  herein is
# owned by Intel Corporation or its  suppliers or licensors,  and  title to such
# Material remains with Intel  Corporation or its  suppliers or  licensors.  The
# Material  contains  proprietary  information  of  Intel or  its suppliers  and
# licensors.  The Material is protected by  worldwide copyright  laws and treaty
# provisions.  No part  of  the  Material   may  be  used,  copied,  reproduced,
# modified, published,  uploaded, posted, transmitted,  distributed or disclosed
# in any way without Intel's prior express written permission.  No license under
# any patent,  copyright or other  intellectual property rights  in the Material
# is granted to  or  conferred  upon  you,  either   expressly,  by implication,
# inducement,  estoppel  or  otherwise.  Any  license   under such  intellectual
# property rights must be express and approved by Intel in writing.
#
# Unless otherwise agreed by Intel in writing,  you may not remove or alter this
# notice or  any  other  notice   embedded  in  Materials  by  Intel  or Intel's
# suppliers or licensors in any way.
#
#
# If this  software  was obtained  under the  Apache License,  Version  2.0 (the
# "License"), the following terms apply:
#
# You may  not use this  file except  in compliance  with  the License.  You may
# obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless  required  by   applicable  law  or  agreed  to  in  writing,  software
# distributed under the License  is distributed  on an  "AS IS"  BASIS,  WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
# See the   License  for the   specific  language   governing   permissions  and
# limitations under 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, data_feature_utils, 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 == data_feature_utils.DAAL_CATEGORICAL:
        return "DAAL_CATEGORICAL"
    elif v == data_feature_utils.DAAL_ORDINAL:
        return "DAAL_ORDINAL"
    elif v == data_feature_utils.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 = data_feature_utils.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("")
