aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca
diff options
context:
space:
mode:
authorJ08nY2024-05-30 12:41:15 +0200
committerJ08nY2024-05-30 12:41:15 +0200
commit452a942af492b66da2f60bd30d7dcd9cd699fa50 (patch)
tree82ce9c1fbbbb8cbc057c8a20a23fec350831eea3 /pyecsca
parent6175ab546d4cb52966473ff9dbf6efee6f4fe083 (diff)
downloadpyecsca-452a942af492b66da2f60bd30d7dcd9cd699fa50.tar.gz
pyecsca-452a942af492b66da2f60bd30d7dcd9cd699fa50.tar.zst
pyecsca-452a942af492b66da2f60bd30d7dcd9cd699fa50.zip
Allow to skip scaling in inspector trace load.
Diffstat (limited to 'pyecsca')
-rw-r--r--pyecsca/sca/trace_set/base.py4
-rw-r--r--pyecsca/sca/trace_set/chipwhisperer.py8
-rw-r--r--pyecsca/sca/trace_set/hdf5.py12
-rw-r--r--pyecsca/sca/trace_set/inspector.py27
-rw-r--r--pyecsca/sca/trace_set/pickle.py4
5 files changed, 32 insertions, 23 deletions
diff --git a/pyecsca/sca/trace_set/base.py b/pyecsca/sca/trace_set/base.py
index 4de7a8c..99094bc 100644
--- a/pyecsca/sca/trace_set/base.py
+++ b/pyecsca/sca/trace_set/base.py
@@ -34,11 +34,11 @@ class TraceSet:
yield from self._traces
@classmethod
- def read(cls, input: Union[str, Path, bytes, BinaryIO]) -> "TraceSet":
+ def read(cls, input: Union[str, Path, bytes, BinaryIO], **kwargs) -> "TraceSet":
raise NotImplementedError
@classmethod
- def inplace(cls, input: Union[str, Path, bytes, BinaryIO]) -> "TraceSet":
+ def inplace(cls, input: Union[str, Path, bytes, BinaryIO], **kwargs) -> "TraceSet":
raise NotImplementedError
def write(self, output: Union[str, Path, BinaryIO]):
diff --git a/pyecsca/sca/trace_set/chipwhisperer.py b/pyecsca/sca/trace_set/chipwhisperer.py
index 17eb375..8ff20ea 100644
--- a/pyecsca/sca/trace_set/chipwhisperer.py
+++ b/pyecsca/sca/trace_set/chipwhisperer.py
@@ -16,16 +16,16 @@ class ChipWhispererTraceSet(TraceSet):
"""ChipWhisperer trace set (native) format."""
@classmethod
- def read(cls, input: Union[str, Path, bytes, BinaryIO]) -> "ChipWhispererTraceSet":
+ def read(cls, input: Union[str, Path, bytes, BinaryIO], **kwargs) -> "ChipWhispererTraceSet":
if isinstance(input, (str, Path)):
- traces, kwargs = ChipWhispererTraceSet.__read(input)
- return ChipWhispererTraceSet(*traces, **kwargs)
+ traces, kws = ChipWhispererTraceSet.__read(input)
+ return ChipWhispererTraceSet(*traces, **kws)
else:
raise ValueError
@classmethod
def inplace(
- cls, input: Union[str, Path, bytes, BinaryIO]
+ cls, input: Union[str, Path, bytes, BinaryIO], **kwargs
) -> "ChipWhispererTraceSet":
raise NotImplementedError
diff --git a/pyecsca/sca/trace_set/hdf5.py b/pyecsca/sca/trace_set/hdf5.py
index 90169f2..88c10a0 100644
--- a/pyecsca/sca/trace_set/hdf5.py
+++ b/pyecsca/sca/trace_set/hdf5.py
@@ -77,7 +77,7 @@ class HDF5TraceSet(TraceSet):
super().__init__(*traces, **kwargs)
@classmethod
- def read(cls, input: Union[str, Path, bytes, BinaryIO]) -> "HDF5TraceSet":
+ 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)):
@@ -87,7 +87,7 @@ class HDF5TraceSet(TraceSet):
if "traces" not in hdf5:
hdf5.create_group("traces", track_order=True)
group = hdf5["traces"]
- kwargs = dict(group.attrs)
+ kws = dict(group.attrs)
ordering = []
traces = []
for k, samples in group.items():
@@ -95,10 +95,10 @@ class HDF5TraceSet(TraceSet):
traces.append(Trace(np.array(samples, dtype=samples.dtype), meta))
ordering.append(k)
hdf5.close()
- return HDF5TraceSet(*traces, **kwargs, _ordering=ordering)
+ return HDF5TraceSet(*traces, **kws, _ordering=ordering)
@classmethod
- def inplace(cls, input: Union[str, Path, bytes, BinaryIO]) -> "HDF5TraceSet":
+ 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)):
@@ -108,14 +108,14 @@ class HDF5TraceSet(TraceSet):
if "traces" not in hdf5:
hdf5.create_group("traces", track_order=True)
group = hdf5["traces"]
- kwargs = dict(group.attrs)
+ 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, **kwargs, _file=hdf5, _ordering=ordering) # type: ignore[misc]
+ return HDF5TraceSet(*traces, **kws, _file=hdf5, _ordering=ordering) # type: ignore[misc]
def append(self, value: Trace) -> Trace:
key = str(uuid.uuid4())
diff --git a/pyecsca/sca/trace_set/inspector.py b/pyecsca/sca/trace_set/inspector.py
index 15fc512..434826a 100644
--- a/pyecsca/sca/trace_set/inspector.py
+++ b/pyecsca/sca/trace_set/inspector.py
@@ -145,8 +145,10 @@ class InspectorTraceSet(TraceSet):
0x67: ("external_clock_time_base", 4, Parsers.read_int, Parsers.write_int),
}
+ _scaled: bool
+
@classmethod
- def read(cls, input: Union[str, Path, bytes, BinaryIO]) -> "InspectorTraceSet":
+ def read(cls, input: Union[str, Path, bytes, BinaryIO], **kwargs) -> "InspectorTraceSet":
"""
Read Inspector trace set from file path, bytes or file-like object.
@@ -163,10 +165,14 @@ class InspectorTraceSet(TraceSet):
traces, tags = InspectorTraceSet.__read(input)
else:
raise TypeError
- for trace in traces:
- new = InspectorTraceSet.__scale(trace.samples, tags["y_scale"])
- del trace.samples
- trace.samples = new
+ if kwargs.get("scale"):
+ tags["_scaled"] = True
+ for trace in traces:
+ new = InspectorTraceSet.__scale(trace.samples, tags["y_scale"])
+ del trace.samples
+ trace.samples = new
+ else:
+ tags["_scaled"] = False
return InspectorTraceSet(*traces, **tags)
@classmethod
@@ -207,7 +213,7 @@ class InspectorTraceSet(TraceSet):
return result, tags
@classmethod
- def inplace(cls, input: Union[str, Path, bytes, BinaryIO]) -> "InspectorTraceSet":
+ def inplace(cls, input: Union[str, Path, bytes, BinaryIO], **kwargs) -> "InspectorTraceSet":
raise NotImplementedError
def write(self, output: Union[str, Path, BinaryIO]):
@@ -249,9 +255,12 @@ class InspectorTraceSet(TraceSet):
file.write(Parsers.write_str(trace.meta["title"]))
if self.data_space != 0 and trace.meta["data"] is not None:
file.write(trace.meta["data"])
- unscaled = InspectorTraceSet.__unscale(
- trace.samples, self.y_scale, self.sample_coding
- )
+ if self._scaled:
+ unscaled = InspectorTraceSet.__unscale(
+ trace.samples, self.y_scale, self.sample_coding
+ )
+ else:
+ unscaled = trace.samples
try:
unscaled.tofile(file)
except UnsupportedOperation:
diff --git a/pyecsca/sca/trace_set/pickle.py b/pyecsca/sca/trace_set/pickle.py
index b56c985..537f76f 100644
--- a/pyecsca/sca/trace_set/pickle.py
+++ b/pyecsca/sca/trace_set/pickle.py
@@ -18,7 +18,7 @@ class PickleTraceSet(TraceSet):
"""Pickle-based traceset format."""
@classmethod
- def read(cls, input: Union[str, Path, bytes, BinaryIO]) -> "PickleTraceSet":
+ def read(cls, input: Union[str, Path, bytes, BinaryIO], **kwargs) -> "PickleTraceSet":
if isinstance(input, bytes):
return pickle.loads(input) # pickle is OK here, skipcq: BAN-B301
elif isinstance(input, (str, Path)):
@@ -29,7 +29,7 @@ class PickleTraceSet(TraceSet):
raise TypeError
@classmethod
- def inplace(cls, input: Union[str, Path, bytes, BinaryIO]) -> "PickleTraceSet":
+ def inplace(cls, input: Union[str, Path, bytes, BinaryIO], **kwargs) -> "PickleTraceSet":
raise NotImplementedError
def write(self, output: Union[str, Path, BinaryIO]):