File size: 2,475 Bytes
3bb804c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.

import numpy as np

from ..._fiff.utils import read_str


def _unpack_matrix(fid, rows, cols, dtype, out_dtype):
    """Unpack matrix."""
    dtype = np.dtype(dtype)

    string = fid.read(int(dtype.itemsize * rows * cols))
    out = np.frombuffer(string, dtype=dtype).reshape(rows, cols).astype(out_dtype)
    return out


def _unpack_simple(fid, dtype, out_dtype):
    """Unpack a NumPy type."""
    dtype = np.dtype(dtype)
    string = fid.read(dtype.itemsize)
    out = np.frombuffer(string, dtype=dtype).astype(out_dtype)

    if len(out) > 0:
        out = out[0]
    return out


def read_char(fid, count=1):
    """Read character from bti file."""
    return _unpack_simple(fid, f">S{count}", "S")


def read_uint16(fid):
    """Read unsigned 16bit integer from bti file."""
    return _unpack_simple(fid, ">u2", np.uint32)


def read_int16(fid):
    """Read 16bit integer from bti file."""
    return _unpack_simple(fid, ">i2", np.int32)


def read_uint32(fid):
    """Read unsigned 32bit integer from bti file."""
    return _unpack_simple(fid, ">u4", np.uint32)


def read_int32(fid):
    """Read 32bit integer from bti file."""
    return _unpack_simple(fid, ">i4", np.int32)


def read_int64(fid):
    """Read 64bit integer from bti file."""
    return _unpack_simple(fid, ">u8", np.int64)


def read_float(fid):
    """Read 32bit float from bti file."""
    return _unpack_simple(fid, ">f4", np.float32)


def read_double(fid):
    """Read 64bit float from bti file."""
    return _unpack_simple(fid, ">f8", np.float64)


def read_int16_matrix(fid, rows, cols):
    """Read 16bit integer matrix from bti file."""
    return _unpack_matrix(
        fid,
        rows,
        cols,
        dtype=">i2",
        out_dtype=np.int32,
    )


def read_float_matrix(fid, rows, cols):
    """Read 32bit float matrix from bti file."""
    return _unpack_matrix(fid, rows, cols, dtype=">f4", out_dtype=np.float32)


def read_double_matrix(fid, rows, cols):
    """Read 64bit float matrix from bti file."""
    return _unpack_matrix(fid, rows, cols, dtype=">f8", out_dtype=np.float64)


def read_transform(fid):
    """Read 64bit float matrix transform from bti file."""
    return read_double_matrix(fid, rows=4, cols=4)


def read_dev_header(x):
    """Create a dev header."""
    return dict(size=read_int32(x), checksum=read_int32(x), reserved=read_str(x, 32))