Python* API Reference for Intel® Data Analytics Acceleration Library 2018 Update 1

serialization.py

1 #===============================================================================
2 # Copyright 2014-2017 Intel Corporation
3 # All Rights Reserved.
4 #
5 # If this software was obtained under the Intel Simplified Software License,
6 # the following terms apply:
7 #
8 # The source code, information and material ("Material") contained herein is
9 # owned by Intel Corporation or its suppliers or licensors, and title to such
10 # Material remains with Intel Corporation or its suppliers or licensors. The
11 # Material contains proprietary information of Intel or its suppliers and
12 # licensors. The Material is protected by worldwide copyright laws and treaty
13 # provisions. No part of the Material may be used, copied, reproduced,
14 # modified, published, uploaded, posted, transmitted, distributed or disclosed
15 # in any way without Intel's prior express written permission. No license under
16 # any patent, copyright or other intellectual property rights in the Material
17 # is granted to or conferred upon you, either expressly, by implication,
18 # inducement, estoppel or otherwise. Any license under such intellectual
19 # property rights must be express and approved by Intel in writing.
20 #
21 # Unless otherwise agreed by Intel in writing, you may not remove or alter this
22 # notice or any other notice embedded in Materials by Intel or Intel's
23 # suppliers or licensors in any way.
24 #
25 #
26 # If this software was obtained under the Apache License, Version 2.0 (the
27 # "License"), the following terms apply:
28 #
29 # You may not use this file except in compliance with the License. You may
30 # obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
31 #
32 #
33 # Unless required by applicable law or agreed to in writing, software
34 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 #
37 # See the License for the specific language governing permissions and
38 # limitations under the License.
39 #===============================================================================
40 
41 
42 
43 
44 import os
45 import sys
46 
47 import numpy as np
48 
49 from daal.data_management import HomogenNumericTable, FileDataSource, DataSource, InputDataArchive, OutputDataArchive
50 
51 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
52 if utils_folder not in sys.path:
53  sys.path.insert(0, utils_folder)
54 from utils import printNumericTable
55 
56 # Input data set parameters
57 datasetFileName = os.path.join('..', 'data', 'batch', 'serialization.csv')
58 
59 
60 def serializeNumericTable(dataTable):
61 
62  # Create a data archive to serialize the numeric table
63  dataArch = InputDataArchive()
64 
65  # Serialize the numeric table into the data archive
66  dataTable.serialize(dataArch)
67 
68  # Get the length of the serialized data in bytes
69  length = dataArch.getSizeOfArchive()
70 
71  # Store the serialized data in an array
72  buffer = np.zeros(length, dtype=np.ubyte)
73  dataArch.copyArchiveToArray(buffer)
74 
75  return buffer
76 
77 
78 def deserializeNumericTable(buffer):
79 
80  # Create a data archive to deserialize the numeric table
81  dataArch = OutputDataArchive(buffer)
82 
83  # Create a numeric table object
84  dataTable = HomogenNumericTable()
85 
86  # Deserialize the numeric table from the data archive
87  dataTable.deserialize(dataArch)
88 
89  return dataTable
90 
91 
92 if __name__ == "__main__":
93 
94  # Initialize FileDataSource_CSVFeatureManager to retrieve the input data from a .csv file
95  dataSource = FileDataSource(
96  datasetFileName, DataSource.doAllocateNumericTable, DataSource.doDictionaryFromContext
97  )
98 
99  # Retrieve the data from the input file
100  dataSource.loadDataBlock()
101 
102  # Retrieve a numeric table
103  dataTable = dataSource.getNumericTable()
104 
105  # Print the original data
106  printNumericTable(dataTable, "Data before serialization:")
107 
108  # Serialize the numeric table into the memory buffer
109  buffer = serializeNumericTable(dataTable)
110 
111  # Deserialize the numeric table from the memory buffer
112  restoredDataTable = deserializeNumericTable(buffer)
113 
114  # Print the restored data
115  printNumericTable(restoredDataTable, "Data after deserialization:")

For more complete information about compiler optimizations, see our Optimization Notice.