1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
import bz2
import struct
import warnings
import zlib
import numpy as np
from .config import get_config
from .exceptions import AsdfWarning
def validate(compression):
"""
Validate the compression string.
Parameters
----------
compression : str, bytes or None
Returns
-------
compression : str or None
In canonical form.
Raises
------
ValueError
"""
if not compression or compression == b"\0\0\0\0":
return None
if isinstance(compression, bytes):
compression = compression.decode("ascii")
compression = compression.strip("\0")
builtin_labels = ["zlib", "bzp2", "lz4", "input"]
ext_labels = _get_all_compression_extension_labels()
all_labels = ext_labels + builtin_labels
# An extension is allowed to override a builtin compression or another extension,
# but let's warn the user of this.
# TODO: is this the desired behavior?
for i, label in enumerate(all_labels):
if label in all_labels[i + 1 :]:
warnings.warn(f'Found more than one compressor for "{label}"', AsdfWarning)
if compression not in all_labels:
raise ValueError(f"Supported compression types are: {all_labels}, not '{compression}'")
return compression
class Lz4Compressor:
def __init__(self):
try:
import lz4.block
except ImportError:
raise ImportError(
"lz4 library in not installed in your Python environment, "
"therefore the compressed block in this ASDF file "
"can not be decompressed."
)
self._api = lz4.block
def compress(self, data, **kwargs):
kwargs["mode"] = kwargs.get("mode", "default")
compression_block_size = kwargs.pop("compression_block_size", 1 << 22)
nelem = compression_block_size // data.itemsize
for i in range(0, len(data), nelem):
_output = self._api.compress(data[i : i + nelem], **kwargs)
header = struct.pack("!I", len(_output))
yield header + _output
def decompress(self, blocks, out, **kwargs):
_size = 0
_pos = 0
_partial_len = b""
_buffer = None
bytesout = 0
for block in blocks:
cast = "c"
block = memoryview(block).cast(cast) # don't copy on slice
while len(block):
if not _size:
# Don't know the (compressed) length of this block yet
if len(_partial_len) + len(block) < 4:
_partial_len += block
break # we've exhausted the block
if _partial_len:
# If we started to fill a len key, finish filling it
remaining = 4 - len(_partial_len)
if remaining:
_partial_len += block[:remaining]
block = block[remaining:]
_size = struct.unpack("!I", _partial_len)[0]
_partial_len = b""
else:
# Otherwise just read the len key directly
_size = struct.unpack("!I", block[:4])[0]
block = block[4:]
if len(block) < _size or _buffer is not None:
# If we have a partial block, or we're already filling a buffer, use the buffer
if _buffer is None:
_buffer = np.empty(
_size, dtype=np.byte
) # use numpy instead of bytearray so we can avoid zero initialization
_pos = 0
newbytes = min(_size - _pos, len(block)) # don't fill past the buffer len!
_buffer[_pos : _pos + newbytes] = np.frombuffer(block[:newbytes], dtype=np.byte)
_pos += newbytes
block = block[newbytes:]
if _pos == _size:
_out = self._api.decompress(_buffer, return_bytearray=True, **kwargs)
out[bytesout : bytesout + len(_out)] = _out
bytesout += len(_out)
_buffer = None
_size = 0
else:
# We have at least one full block
_out = self._api.decompress(memoryview(block[:_size]), return_bytearray=True, **kwargs)
out[bytesout : bytesout + len(_out)] = _out
bytesout += len(_out)
block = block[_size:]
_size = 0
return bytesout
class ZlibCompressor:
def compress(self, data, **kwargs):
comp = zlib.compress(data, **kwargs)
yield comp
def decompress(self, blocks, out, **kwargs):
decompressor = zlib.decompressobj(**kwargs)
i = 0
for block in blocks:
decomp = decompressor.decompress(block)
out[i : i + len(decomp)] = decomp
i += len(decomp)
return i
class Bzp2Compressor:
def compress(self, data, **kwargs):
comp = bz2.compress(data, **kwargs)
yield comp
def decompress(self, blocks, out, **kwargs):
decompressor = bz2.BZ2Decompressor(**kwargs)
i = 0
for block in blocks:
decomp = decompressor.decompress(block)
out[i : i + len(decomp)] = decomp
i += len(decomp)
return i
def _get_compressor_from_extensions(compression, return_extension=False):
"""
Look at the loaded ASDF extensions and return the first one (if any)
that can handle this type of compression.
`return_extension` can be used to return corresponding extension for bookkeeping purposes.
Returns None if no match found.
"""
# TODO: in ASDF 3, this will be done by the ExtensionManager
extensions = get_config().extensions
for ext in extensions:
for comp in ext.compressors:
if compression == comp.label.decode("ascii"):
if return_extension:
return comp, ext
else:
return comp
return None
def _get_all_compression_extension_labels():
"""
Get the list of compression labels supported via extensions
"""
# TODO: in ASDF 3, this will be done by the ExtensionManager
labels = []
extensions = get_config().extensions
for ext in extensions:
for comp in ext.compressors:
labels += [comp.label.decode("ascii")]
return labels
def _get_compressor(label):
ext_comp = _get_compressor_from_extensions(label)
if ext_comp is not None:
# Use an extension before builtins
comp = ext_comp
elif label == "zlib":
comp = ZlibCompressor()
elif label == "bzp2":
comp = Bzp2Compressor()
elif label == "lz4":
comp = Lz4Compressor()
else:
raise ValueError(f"Unknown compression type: '{label}'")
return comp
def to_compression_header(compression):
"""
Converts a compression string to the four byte field in a block
header.
"""
if not compression:
return b""
if isinstance(compression, str):
return compression.encode("ascii")
return compression
def decompress(fd, used_size, data_size, compression, config=None):
"""
Decompress binary data in a file
Parameters
----------
fd : generic_io.GenericIO object
The file to read the compressed data from.
used_size : int
The size of the compressed data
data_size : int
The size of the uncompressed data
compression : str
The compression type used.
config : dict or None, optional
Any kwarg parameters to pass to the underlying decompression
function
Returns
-------
array : numpy.array
A flat uint8 containing the decompressed data.
"""
buffer = np.empty((data_size,), np.uint8)
compression = validate(compression)
decoder = _get_compressor(compression)
if config is None:
config = {}
blocks = fd.read_blocks(used_size) # data is a generator
len_decoded = decoder.decompress(blocks, out=buffer.data, **config)
if len_decoded != data_size:
raise ValueError("Decompressed data wrong size")
return buffer
def compress(fd, data, compression, config=None):
"""
Compress array data and write to a file.
Parameters
----------
fd : generic_io.GenericIO object
The file to write to.
data : buffer
The buffer of uncompressed data.
compression : str
The type of compression to use.
config : dict or None, optional
Any kwarg parameters to pass to the underlying compression
function
"""
compression = validate(compression)
encoder = _get_compressor(compression)
if config is None:
config = {}
# Get a contiguous, 1D memoryview of the underlying data, preserving data.itemsize
# - contiguous: because we may not want to assume that all compressors can handle arbitrary strides
# - 1D: so that len(data) works, not just data.nbytes
# - itemsize: should preserve data.itemsize for compressors that want to use the record size
# - memoryview: don't incur the expense of a memcpy, such as with tobytes()
data = memoryview(data)
if not data.contiguous:
data = memoryview(data.tobytes()) # make a contiguous copy
data = memoryview(np.frombuffer(data, dtype=data.format)) # get a 1D array that preserves byteorder
if not data.contiguous:
# the data will be contiguous by construction, but better safe than sorry!
raise ValueError(data.contiguous)
compressed = encoder.compress(data, **config)
# Write block by block
for comp in compressed:
fd.write(comp)
def get_compressed_size(data, compression, config=None):
"""
Returns the number of bytes required when the given data is
compressed.
Parameters
----------
See `compress()`.
Returns
-------
nbytes : int
The size of the compressed data
"""
class _ByteCountingFile:
def __init__(self):
self.count = 0
def write(self, data):
self.count += len(data)
bcf = _ByteCountingFile()
compress(bcf, data, compression, config=config)
return bcf.count
|