aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/sca/trace_set/hdf5.py
blob: 2c2594cade1a10d625bbbb7b6926e5dcb32cc042 (plain) (blame)
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
"""
Provides a traceset implemented on top of the Hierarchical Data Format (HDF5).

This traceset can be loaded "inplace" which means that it is not fully loaded into memory, and only parts of traces that
are operated on are in memory. This is very useful for working with huge sets of traces that do not fit in memory.
"""

import pickle
import uuid
from collections.abc import MutableMapping
from io import RawIOBase, BufferedIOBase
from pathlib import Path
from typing import Union, Optional, List, BinaryIO

import h5py
import numpy as np
from public import public
from copy import deepcopy

from pyecsca.sca.trace_set.base import TraceSet
from pyecsca.sca import Trace


@public
class HDF5Meta(MutableMapping):
    """Metadata mapping that is HDF5-compatible (items are picklable)."""

    _dataset: h5py.AttributeManager

    def __init__(self, attrs: h5py.AttributeManager):
        self._attrs = attrs
        super().__init__()

    def __getitem__(self, item):
        if item not in self._attrs:
            raise KeyError
        return pickle.loads(self._attrs[item])  # pickle is OK here, skipcq: BAN-B301

    def __setitem__(self, key, value):
        self._attrs[key] = np.void(pickle.dumps(value))

    def __delitem__(self, key):
        del self._attrs[key]

    def __copy__(self):
        return deepcopy(self)

    def __deepcopy__(self, memodict):
        return dict(self)

    def __iter__(self):
        yield from self._attrs

    def __len__(self):
        return len(self._attrs)


@public
class HDF5TraceSet(TraceSet):
    """Traceset based on the HDF5 (Hierarchical Data Format)."""

    _file: Optional[h5py.File]
    _ordering: List[str]
    # _meta: Optional[HDF5Meta]

    def __init__(
        self,
        *traces: Trace,
        _file: Optional[h5py.File] = None,
        _ordering: Optional[List[str]] = None,
        **kwargs,
    ):
        # self._meta = HDF5Meta(_file.attrs) if _file is not None else None
        self._file = _file
        if _ordering is None:
            _ordering = [str(uuid.uuid4()) for _ in traces]
        self._ordering = _ordering
        super().__init__(*traces, **kwargs)

    @classmethod
    def read(cls, input: Union[str, Path, bytes, BinaryIO], **kwargs) -> "HDF5TraceSet":
        if isinstance(input, (str, Path)):
            hdf5 = h5py.File(str(input), mode="r")
        elif isinstance(input, (RawIOBase, BufferedIOBase, BinaryIO)):
            hdf5 = h5py.File(input, mode="r")
        else:
            raise TypeError
        if "traces" not in hdf5:
            hdf5.create_group("traces", track_order=True)
        group = hdf5["traces"]
        kws = dict(group.attrs)
        ordering = []
        traces = []
        for k, samples in group.items():
            meta = dict(HDF5Meta(samples.attrs))
            traces.append(Trace(np.array(samples, dtype=samples.dtype), meta))
            ordering.append(k)
        hdf5.close()
        return HDF5TraceSet(*traces, **kws, _ordering=ordering)

    @classmethod
    def inplace(
        cls, input: Union[str, Path, bytes, BinaryIO], **kwargs
    ) -> "HDF5TraceSet":
        if isinstance(input, (str, Path)):
            hdf5 = h5py.File(str(input), mode="a")
        elif isinstance(input, (RawIOBase, BufferedIOBase, BinaryIO)):
            hdf5 = h5py.File(input, mode="a")
        else:
            raise TypeError
        if "traces" not in hdf5:
            hdf5.create_group("traces", track_order=True)
        group = hdf5["traces"]
        kws = dict(group.attrs)
        ordering = []
        traces = []
        for k, samples in group.items():
            meta = HDF5Meta(samples.attrs)
            traces.append(Trace(samples, meta))
            ordering.append(k)
        return HDF5TraceSet(*traces, **kws, _file=hdf5, _ordering=ordering)  # type: ignore[misc]

    def append(self, value: Trace) -> Trace:
        key = str(uuid.uuid4())
        if self._file is not None:
            group = self._file["traces"]
            new_samples = group.create_dataset(key, data=value.samples)
            new_meta = HDF5Meta(new_samples.attrs)
            if value.meta:
                for k, v in value.meta.items():
                    new_meta[k] = v
            value = Trace(new_samples, new_meta)

        self._ordering.append(key)
        self._traces.append(value)
        return value

    def get(self, index: int) -> Trace:
        return self[index]

    def remove(self, value: Trace):
        if value in self._traces:
            index = self._traces.index(value)
            key = self._ordering[index]
            self._ordering.remove(key)
            self._traces.remove(value)
            if self._file:
                group = self._file["traces"]
                group.pop(key)
        else:
            raise KeyError

    def save(self):
        if self._file is not None:
            self._file.flush()

    def close(self):
        if self._file is not None:
            self._file.close()

    # def __getattribute__(self, item):
    #     if super().__getattribute__("_meta") and item in super().__getattribute__("_meta"):
    #         return super().__getattribute__("_meta")[item]
    #     return super().__getattribute__(item)
    #
    # def __setattr__(self, key, value):
    #     if key in self._keys and self._meta is not None:
    #         self._meta[key] = value
    #     else:
    #         super().__setattr__(key, value)

    def write(self, output: Union[str, Path, BinaryIO]):
        if isinstance(output, (str, Path)):
            hdf5 = h5py.File(str(output), "w")
        elif isinstance(output, BinaryIO):
            hdf5 = h5py.File(output, "w")
        else:
            raise ValueError
        group = hdf5.create_group("traces", track_order=True)
        for k in self._keys:
            group.attrs[k] = getattr(self, k)
        for k, trace in zip(self._ordering, self):
            dset = group.create_dataset(k, data=trace.samples)
            if trace.meta:
                meta = HDF5Meta(dset.attrs)
                for key, val in trace.meta.items():
                    meta[key] = val
        hdf5.close()

    def __repr__(self):
        fname = ""
        status = ""
        if self._file is not None:
            if self._file.id.valid:
                status = " (opened)"
                fname = self._file.filename
            else:
                status = "(closed)"
        args = ", ".join(
            [
                f"{key}={getattr(self, key)!r}"
                for key in self._keys
                if not key.startswith("_")
            ]
        )
        return f"HDF5TraceSet('{fname}'{status}, {args} <{len(self)}>)"