forked from rapidsai/libgdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
61 lines (44 loc) · 1.58 KB
/
utils.py
File metadata and controls
61 lines (44 loc) · 1.58 KB
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 libgdf_cffi import ffi, libgdf
def new_column():
return ffi.new('gdf_column*')
def unwrap_devary(devary):
return ffi.cast('void*', devary.device_ctypes_pointer.value)
def get_dtype(dtype):
return {
np.float64: libgdf.GDF_FLOAT64,
np.float32: libgdf.GDF_FLOAT32,
np.int64: libgdf.GDF_INT64,
np.int32: libgdf.GDF_INT32,
np.int8: libgdf.GDF_INT8,
np.bool_: libgdf.GDF_INT8,
}[np.dtype(dtype).type]
def seed_rand():
# A constant seed for deterministic testing
np.random.seed(0xabcdef)
def gen_rand(dtype, size, **kwargs):
dtype = np.dtype(dtype)
if dtype.kind == 'f':
res = np.random.random(size=size).astype(dtype)
if kwargs.get('positive_only', False):
return res
else:
return (res * 2 - 1)
elif dtype.kind == 'i':
low = kwargs.get('low', -10000)
high = kwargs.get('high', 10000)
return np.random.random_integers(low=low, high=high, size=size).astype(dtype)
elif dtype.kind == 'b':
low = kwargs.get('low', 0)
high = kwargs.get('high', 1)
return np.random.random_integers(low=low, high=high, size=size).astype(np.bool)
raise NotImplementedError('dtype.kind={}'.format(dtype.kind))
def fix_zeros(arr, val=1):
arr[arr == 0] = val
def buffer_as_bits(data):
def fix_binary(x):
x = x[2:]
diff = 8 - len(x)
return ('0' * diff + x)[::-1]
binaries = ''.join(fix_binary(bin(x)) for x in bytearray(data))
return list(map(lambda x: x == '1', binaries))