From e50b0519ce783aba78a074883c24f2874fe3262b Mon Sep 17 00:00:00 2001 From: J08nY Date: Mon, 16 Oct 2023 14:15:35 +0200 Subject: Add "astype" to trace. --- pyecsca/sca/trace/trace.py | 10 ++++++++++ test/sca/test_trace.py | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/pyecsca/sca/trace/trace.py b/pyecsca/sca/trace/trace.py index 5c0fd2c..dbab02d 100644 --- a/pyecsca/sca/trace/trace.py +++ b/pyecsca/sca/trace/trace.py @@ -5,6 +5,7 @@ from copy import copy, deepcopy from numpy import ndarray import numpy as np +from numpy.typing import DTypeLike from public import public @@ -89,6 +90,15 @@ class Trace: """ return Trace(samples, deepcopy(self.meta)) + def astype(self, dtype: DTypeLike) -> "Trace": + """ + Construct a copy of this trace, with the same samples retyped using `dtype`. + + :param dtype: The numpy dtype. + :return: The new trace + """ + return self.with_samples(np.array(self.samples.astype(dtype))) + def __copy__(self): return Trace(copy(self.samples), copy(self.meta), copy(self.trace_set)) diff --git a/test/sca/test_trace.py b/test/sca/test_trace.py index 98818d3..bea68f2 100644 --- a/test/sca/test_trace.py +++ b/test/sca/test_trace.py @@ -7,3 +7,9 @@ def test_basic(): assert trace is not None assert "Trace" in str(trace) assert trace.trace_set is None + + +def test_astype(): + trace = Trace(np.array([10, 15, 24], dtype=np.dtype("i1"))) + ta = trace.astype(np.float32) + assert ta.samples.dtype == np.float32 -- cgit v1.3.1