-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathshuffle.py
61 lines (40 loc) · 1.54 KB
/
shuffle.py
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
import numpy as np
from ._shuffle import _doShuffle, _doUnshuffle
from .abc import Codec
from .compat import ensure_contiguous_ndarray
class Shuffle(Codec):
"""Codec providing shuffle
Parameters
----------
elementsize : int
Size in bytes of the array elements. Default = 4
"""
codec_id = 'shuffle'
def __init__(self, elementsize=4):
self.elementsize = elementsize
def _prepare_arrays(self, buf, out):
buf = ensure_contiguous_ndarray(buf)
if out is None:
out = np.zeros(buf.nbytes, dtype='uint8')
else:
out = ensure_contiguous_ndarray(out)
if self.elementsize <= 1:
out.view(buf.dtype)[: len(buf)] = buf[:] # no shuffling needed
return buf, out
if buf.nbytes % self.elementsize != 0:
raise ValueError("Shuffle buffer is not an integer multiple of elementsize")
return buf, out
def encode(self, buf, out=None):
buf, out = self._prepare_arrays(buf, out)
if self.elementsize <= 1:
return out # no shuffling needed
_doShuffle(buf.view("uint8"), out.view("uint8"), self.elementsize)
return out
def decode(self, buf, out=None):
buf, out = self._prepare_arrays(buf, out)
if self.elementsize <= 1:
return out # no shuffling needed
_doUnshuffle(buf.view("uint8"), out.view("uint8"), self.elementsize)
return out
def __repr__(self):
return f'{type(self).__name__}(elementsize={self.elementsize})'