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

compression_batch.py

1 # file: compression_batch.py
2 #===============================================================================
3 # Copyright 2014-2017 Intel Corporation
4 # All Rights Reserved.
5 #
6 # If this software was obtained under the Intel Simplified Software License,
7 # the following terms apply:
8 #
9 # The source code, information and material ("Material") contained herein is
10 # owned by Intel Corporation or its suppliers or licensors, and title to such
11 # Material remains with Intel Corporation or its suppliers or licensors. The
12 # Material contains proprietary information of Intel or its suppliers and
13 # licensors. The Material is protected by worldwide copyright laws and treaty
14 # provisions. No part of the Material may be used, copied, reproduced,
15 # modified, published, uploaded, posted, transmitted, distributed or disclosed
16 # in any way without Intel's prior express written permission. No license under
17 # any patent, copyright or other intellectual property rights in the Material
18 # is granted to or conferred upon you, either expressly, by implication,
19 # inducement, estoppel or otherwise. Any license under such intellectual
20 # property rights must be express and approved by Intel in writing.
21 #
22 # Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 # notice or any other notice embedded in Materials by Intel or Intel's
24 # suppliers or licensors in any way.
25 #
26 #
27 # If this software was obtained under the Apache License, Version 2.0 (the
28 # "License"), the following terms apply:
29 #
30 # You may not use this file except in compliance with the License. You may
31 # obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 #
33 #
34 # Unless required by applicable law or agreed to in writing, software
35 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 #
38 # See the License for the specific language governing permissions and
39 # limitations under the License.
40 #===============================================================================
41 
42 #
43 # ! Content:
44 # ! Python example of compression in the batch processing mode
45 # !
46 # !*****************************************************************************
47 
48 #
49 ## <a name="DAAL-EXAMPLE-PY-COMPRESSION_BATCH"></a>
50 ## \example compression_batch.py
51 #
52 
53 import os
54 import sys
55 
56 import numpy as np
57 
58 from daal.data_management import Compressor_Zlib, Decompressor_Zlib, level9, DecompressionStream, CompressionStream
59 
60 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
61 if utils_folder not in sys.path:
62  sys.path.insert(0, utils_folder)
63 from utils import getCRC32, readTextFile
64 
65 DATA_PREFIX = os.path.join('..', 'data', 'batch')
66 datasetFileName = os.path.join(DATA_PREFIX, 'logitboost_train.csv')
67 
68 
69 def printCRC32(rawData, deCompressedData):
70 
71  # Compute checksums for raw data and the decompressed data
72  crcRawData = getCRC32(rawData)
73  crcDecompressedData = getCRC32(deCompressedData)
74 
75  print("\nCompression example program results:\n")
76 
77  print("Raw data checksum: 0x{:02X}".format(crcRawData))
78  print("Decompressed data checksum: 0x{:02X}".format(crcDecompressedData))
79 
80  if rawData.size != deCompressedData.size:
81  print("ERROR: Decompressed data size mismatches with the raw data size")
82 
83  elif crcRawData != crcDecompressedData:
84  print("ERROR: Decompressed data CRC mismatches with the raw data CRC")
85 
86  else:
87  print("OK: Decompressed data CRC matches with the raw data CRC")
88 
89 
90 if __name__ == "__main__":
91  # Read data from a file
92  rawData = readTextFile(datasetFileName)
93 
94  # Create a compressor
95  compressor = Compressor_Zlib()
96  compressor.parameter.gzHeader = True
97  compressor.parameter.level = level9
98 
99  # Create a stream for compression
100  comprStream = CompressionStream(compressor)
101 
102  # Write raw data to the compression stream and compress if needed
103  comprStream.push_back(rawData)
104 
105  # Allocate memory to store the compressed data
106  compressedData = np.empty(comprStream.getCompressedDataSize(), dtype=np.uint8)
107 
108  # Store the compressed data
109  comprStream.copyCompressedArray(compressedData)
110 
111  # Create a decompressor
112  decompressor = Decompressor_Zlib()
113  decompressor.parameter.gzHeader = True
114 
115  # Create a stream for decompression
116  deComprStream = DecompressionStream(decompressor)
117 
118  # Write the compressed data to the decompression stream and decompress it
119  deComprStream.push_back(compressedData)
120 
121  # Allocate memory to store the decompressed data
122  deCompressedData = np.empty(deComprStream.getDecompressedDataSize(), dtype=np.uint8)
123 
124  # Store the decompressed data
125  deComprStream.copyDecompressedArray(deCompressedData)
126 
127  # Compute and print checksums for raw data and the decompressed data
128  printCRC32(rawData, deCompressedData)

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