diff options
| author | J08nY | 2020-03-04 00:04:53 +0100 |
|---|---|---|
| committer | J08nY | 2020-03-04 00:04:53 +0100 |
| commit | a97f49ebe3c8e28d2a9ba76711555a3378b62341 (patch) | |
| tree | d6064aec39573ad9e83607dbed5873d7872aed21 /pyecsca/sca | |
| parent | deca0e3d89ff4483dd6b6b4ad99b3400145bee5b (diff) | |
| download | pyecsca-a97f49ebe3c8e28d2a9ba76711555a3378b62341.tar.gz pyecsca-a97f49ebe3c8e28d2a9ba76711555a3378b62341.tar.zst pyecsca-a97f49ebe3c8e28d2a9ba76711555a3378b62341.zip | |
Fix some type issues.
Diffstat (limited to 'pyecsca/sca')
| -rw-r--r-- | pyecsca/sca/scope/picoscope_sdk.py | 3 | ||||
| -rw-r--r-- | pyecsca/sca/target/binary.py | 4 | ||||
| -rw-r--r-- | pyecsca/sca/target/chipwhisperer.py | 2 | ||||
| -rw-r--r-- | pyecsca/sca/target/flash.py | 2 | ||||
| -rw-r--r-- | pyecsca/sca/target/serial.py | 2 | ||||
| -rw-r--r-- | pyecsca/sca/target/simpleserial.py | 2 | ||||
| -rw-r--r-- | pyecsca/sca/trace/align.py | 6 | ||||
| -rw-r--r-- | pyecsca/sca/trace/combine.py | 6 | ||||
| -rw-r--r-- | pyecsca/sca/trace/edit.py | 8 | ||||
| -rw-r--r-- | pyecsca/sca/trace/filter.py | 2 | ||||
| -rw-r--r-- | pyecsca/sca/trace/process.py | 22 | ||||
| -rw-r--r-- | pyecsca/sca/trace/sampling.py | 6 | ||||
| -rw-r--r-- | pyecsca/sca/trace/test.py | 4 | ||||
| -rw-r--r-- | pyecsca/sca/trace/trace.py | 10 | ||||
| -rw-r--r-- | pyecsca/sca/trace_set/chipwhisperer.py | 2 | ||||
| -rw-r--r-- | pyecsca/sca/trace_set/inspector.py | 7 |
16 files changed, 44 insertions, 44 deletions
diff --git a/pyecsca/sca/scope/picoscope_sdk.py b/pyecsca/sca/scope/picoscope_sdk.py index b21df09..a198367 100644 --- a/pyecsca/sca/scope/picoscope_sdk.py +++ b/pyecsca/sca/scope/picoscope_sdk.py @@ -159,6 +159,8 @@ class PicoScopeSdk(Scope): # pragma: no cover return True def retrieve(self, channel: str) -> Optional[np.ndarray]: + if self.samples is None: + raise ValueError actual_samples = ctypes.c_int32(self.samples) overflow = ctypes.c_int16() assert_pico_ok( @@ -179,6 +181,7 @@ class PicoScopeSdk(Scope): # pragma: no cover raise ValueError return method(*args, **kwargs) + @public class PS5000Scope(PicoScopeSdk): # pragma: no cover MODULE = ps5000 diff --git a/pyecsca/sca/target/binary.py b/pyecsca/sca/target/binary.py index 9a469b6..cb4c918 100644 --- a/pyecsca/sca/target/binary.py +++ b/pyecsca/sca/target/binary.py @@ -14,7 +14,7 @@ class BinaryTarget(SerialTarget): debug_output: bool def __init__(self, binary: Union[str, List[str]], debug_output: bool = False, **kwargs): - super().__init__(**kwargs) + super().__init__() if not isinstance(binary, (str, list)): raise TypeError if isinstance(binary, str): @@ -34,7 +34,7 @@ class BinaryTarget(SerialTarget): self.process.stdin.write(data.decode()) self.process.stdin.flush() - def read(self, num: Optional[int] = 0, timeout: Optional[int] = 0) -> bytes: + def read(self, num: int = 0, timeout: int = 0) -> bytes: if self.process is None: raise ValueError if num != 0: diff --git a/pyecsca/sca/target/chipwhisperer.py b/pyecsca/sca/target/chipwhisperer.py index 3710195..3dd5686 100644 --- a/pyecsca/sca/target/chipwhisperer.py +++ b/pyecsca/sca/target/chipwhisperer.py @@ -36,7 +36,7 @@ class ChipWhispererTarget(Flashable, SimpleSerialTarget): # pragma: no cover self.target.flush() self.target.write(data.decode()) - def read(self, num: Optional[int] = 0, timeout: Optional[int] = 0) -> bytes: + def read(self, num: int = 0, timeout: int = 0) -> bytes: return self.target.read(num, timeout).encode() def reset(self): diff --git a/pyecsca/sca/target/flash.py b/pyecsca/sca/target/flash.py index be3cfb0..bfdb79f 100644 --- a/pyecsca/sca/target/flash.py +++ b/pyecsca/sca/target/flash.py @@ -8,4 +8,4 @@ class Flashable(ABC): @abstractmethod def flash(self, fw_path: str) -> bool: - ...
\ No newline at end of file + ... diff --git a/pyecsca/sca/target/serial.py b/pyecsca/sca/target/serial.py index 3c0b5c4..ccde326 100644 --- a/pyecsca/sca/target/serial.py +++ b/pyecsca/sca/target/serial.py @@ -14,5 +14,5 @@ class SerialTarget(Target): ... @abstractmethod - def read(self, num: Optional[int] = 0, timeout: Optional[int] = 0) -> bytes: + def read(self, num: int = 0, timeout: int = 0) -> bytes: ... diff --git a/pyecsca/sca/target/simpleserial.py b/pyecsca/sca/target/simpleserial.py index 9e26b79..1ad0b68 100644 --- a/pyecsca/sca/target/simpleserial.py +++ b/pyecsca/sca/target/simpleserial.py @@ -63,7 +63,7 @@ class SimpleSerialTarget(SerialTarget): """ data = bytes(cmd) for i in range(0, len(data), 64): - chunk = data[i:i+64] + chunk = data[i:i + 64] sleep(0.010) self.write(chunk) self.write(b"\n") diff --git a/pyecsca/sca/trace/align.py b/pyecsca/sca/trace/align.py index 643efcf..1c9c216 100644 --- a/pyecsca/sca/trace/align.py +++ b/pyecsca/sca/trace/align.py @@ -27,7 +27,7 @@ def align_reference(reference: Trace, *traces: Trace, result_samples[:length - offset] = trace.samples[offset:] else: result_samples[-offset:] = trace.samples[:length + offset] - result.append(Trace(copy(trace.title), copy(trace.data), result_samples)) + result.append(Trace(result_samples, copy(trace.title), copy(trace.data))) return result @@ -198,7 +198,7 @@ def align_dtw_scale(reference: Trace, *traces: Trace, radius: int = 1, scale[x] += 1 result_samples //= scale del scale - result.append(Trace(copy(trace.title), copy(trace.data), result_samples)) + result.append(Trace(result_samples, copy(trace.title), copy(trace.data))) return result @@ -233,5 +233,5 @@ def align_dtw(reference: Trace, *traces: Trace, radius: int = 1, fast: bool = Tr # or manually: # for x, y in path: # result_samples[x] = trace.samples[y] - result.append(Trace(copy(trace.title), copy(trace.data), result_samples)) + result.append(Trace(result_samples, copy(trace.title), copy(trace.data))) return result diff --git a/pyecsca/sca/trace/combine.py b/pyecsca/sca/trace/combine.py index c484748..2ab552a 100644 --- a/pyecsca/sca/trace/combine.py +++ b/pyecsca/sca/trace/combine.py @@ -16,10 +16,10 @@ def average(*traces: Trace) -> Optional[CombinedTrace]: if not traces: return None if len(traces) == 1: - return CombinedTrace(None, None, traces[0].samples.copy(), parents=traces) + return CombinedTrace(traces[0].samples.copy(), None, None) dtype = traces[0].samples.dtype result_samples = np.mean(np.array([trace.samples for trace in traces]), axis=0).astype(dtype) - return CombinedTrace(None, None, result_samples, parents=traces) + return CombinedTrace(result_samples, None, None) @public @@ -46,4 +46,4 @@ def standard_deviation(*traces: Trace) -> Optional[CombinedTrace]: return None dtype = traces[0].samples.dtype result_samples = np.std(np.array([trace.samples for trace in traces]), axis=0).astype(dtype) - return CombinedTrace(None, None, result_samples, parents=traces) + return CombinedTrace(result_samples, None, None) diff --git a/pyecsca/sca/trace/edit.py b/pyecsca/sca/trace/edit.py index f01a0dc..f58ad57 100644 --- a/pyecsca/sca/trace/edit.py +++ b/pyecsca/sca/trace/edit.py @@ -22,7 +22,7 @@ def trim(trace: Trace, start: int = None, end: int = None) -> Trace: end = len(trace.samples) if start > end: raise ValueError("Invalid trim arguments.") - return Trace(copy(trace.title), copy(trace.data), trace.samples[start:end].copy()) + return Trace(trace.samples[start:end].copy(), copy(trace.title), copy(trace.data)) @public @@ -33,7 +33,7 @@ def reverse(trace: Trace) -> Trace: :param trace: :return: """ - return Trace(copy(trace.title), copy(trace.data), np.flipud(trace.samples)) + return Trace(np.flipud(trace.samples), copy(trace.title), copy(trace.data)) @public @@ -51,5 +51,5 @@ def pad(trace: Trace, lengths: Union[Tuple[int, int], int], lengths = (lengths, lengths) if not isinstance(values, tuple): values = (values, values) - return Trace(copy(trace.title), copy(trace.data), - np.pad(trace.samples, lengths, "constant", constant_values=values)) + return Trace(np.pad(trace.samples, lengths, "constant", constant_values=values), + copy(trace.title), copy(trace.data)) diff --git a/pyecsca/sca/trace/filter.py b/pyecsca/sca/trace/filter.py index 11cc89c..6bb17b9 100644 --- a/pyecsca/sca/trace/filter.py +++ b/pyecsca/sca/trace/filter.py @@ -14,7 +14,7 @@ def filter_any(trace: Trace, sampling_frequency: int, else: b, a = butter(6, cutoff / nyq, btype=band_type, analog=False, output='ba') result_samples = lfilter(b, a, trace.samples) - return Trace(copy(trace.title), copy(trace.data), result_samples) + return Trace(result_samples, copy(trace.title), copy(trace.data)) @public diff --git a/pyecsca/sca/trace/process.py b/pyecsca/sca/trace/process.py index 8e983b2..4cdbeff 100644 --- a/pyecsca/sca/trace/process.py +++ b/pyecsca/sca/trace/process.py @@ -13,7 +13,7 @@ def absolute(trace: Trace) -> Trace: :param trace: :return: """ - return Trace(copy(trace.title), copy(trace.data), np.absolute(trace.samples)) + return Trace(np.absolute(trace.samples), copy(trace.title), copy(trace.data)) @public @@ -24,7 +24,7 @@ def invert(trace: Trace) -> Trace: :param trace: :return: """ - return Trace(copy(trace.title), copy(trace.data), np.negative(trace.samples)) + return Trace(np.negative(trace.samples), copy(trace.title), copy(trace.data)) @public @@ -39,7 +39,7 @@ def threshold(trace: Trace, value) -> Trace: result_samples = trace.samples.copy() result_samples[result_samples <= value] = 0 result_samples[np.nonzero(result_samples)] = 1 - return Trace(copy(trace.title), copy(trace.data), result_samples) + return Trace(result_samples, copy(trace.title), copy(trace.data)) def rolling_window(samples: np.ndarray, window: int) -> np.ndarray: @@ -57,9 +57,8 @@ def rolling_mean(trace: Trace, window: int) -> Trace: :param window: :return: """ - return Trace(copy(trace.title), copy(trace.data), - np.mean(rolling_window(trace.samples, window), -1).astype( - dtype=trace.samples.dtype)) + return Trace(np.mean(rolling_window(trace.samples, window), -1).astype( + dtype=trace.samples.dtype), copy(trace.title), copy(trace.data)) @public @@ -71,7 +70,7 @@ def offset(trace: Trace, offset) -> Trace: :param offset: :return: """ - return Trace(copy(trace.title), copy(trace.data), trace.samples + offset) + return Trace(trace.samples + offset, copy(trace.title), copy(trace.data)) def root_mean_square(trace: Trace): @@ -92,12 +91,11 @@ def recenter(trace: Trace) -> Trace: @public def normalize(trace: Trace) -> Trace: - return Trace(copy(trace.title), copy(trace.data), - (trace.samples - np.mean(trace.samples)) / np.std(trace.samples)) + return Trace((trace.samples - np.mean(trace.samples)) / np.std(trace.samples), + copy(trace.title), copy(trace.data)) @public def normalize_wl(trace: Trace) -> Trace: - return Trace(copy(trace.title), copy(trace.data), - (trace.samples - np.mean(trace.samples)) / ( - np.std(trace.samples) * len(trace.samples))) + return Trace((trace.samples - np.mean(trace.samples)) / ( + np.std(trace.samples) * len(trace.samples)), copy(trace.title), copy(trace.data)) diff --git a/pyecsca/sca/trace/sampling.py b/pyecsca/sca/trace/sampling.py index 29dc251..fdd5e5b 100644 --- a/pyecsca/sca/trace/sampling.py +++ b/pyecsca/sca/trace/sampling.py @@ -18,7 +18,7 @@ def downsample_average(trace: Trace, factor: int = 2) -> Trace: """ resized = np.resize(trace.samples, len(trace.samples) - (len(trace.samples) % factor)) result_samples = resized.reshape(-1, factor).mean(axis=1).astype(trace.samples.dtype) - return Trace(copy(trace.title), copy(trace.data), result_samples) + return Trace(result_samples, copy(trace.title), copy(trace.data)) @public @@ -32,7 +32,7 @@ def downsample_pick(trace: Trace, factor: int = 2, offset: int = 0) -> Trace: :return: """ result_samples = trace.samples[offset::factor].copy() - return Trace(copy(trace.title), copy(trace.data), result_samples) + return Trace(result_samples, copy(trace.title), copy(trace.data)) @public @@ -45,4 +45,4 @@ def downsample_decimate(trace: Trace, factor: int = 2) -> Trace: :return: """ result_samples = decimate(trace.samples, factor) - return Trace(copy(trace.title), copy(trace.data), result_samples) + return Trace(result_samples, copy(trace.title), copy(trace.data)) diff --git a/pyecsca/sca/trace/test.py b/pyecsca/sca/trace/test.py index f2cfb33..4fc0236 100644 --- a/pyecsca/sca/trace/test.py +++ b/pyecsca/sca/trace/test.py @@ -14,7 +14,7 @@ def ttest_func(first_set: Sequence[Trace], second_set: Sequence[Trace], first_stack = np.stack([first.samples for first in first_set]) second_stack = np.stack([second.samples for second in second_set]) result = ttest_ind(first_stack, second_stack, axis=0, equal_var=equal_var) - return CombinedTrace(None, None, result[0], parents=[*first_set, *second_set]) + return CombinedTrace(result[0], None, None) @public @@ -61,4 +61,4 @@ def ks_test(first_set: Sequence[Trace], second_set: Sequence[Trace]) -> Optional results = np.empty(len(first_set[0].samples), dtype=first_set[0].samples.dtype) for i in range(len(first_set[0].samples)): results[i] = ks_2samp(first_stack[..., i], second_stack[..., i])[0] - return CombinedTrace(None, None, results, parents=[*first_set, *second_set]) + return CombinedTrace(results, None, None) diff --git a/pyecsca/sca/trace/trace.py b/pyecsca/sca/trace/trace.py index 5324a37..18df978 100644 --- a/pyecsca/sca/trace/trace.py +++ b/pyecsca/sca/trace/trace.py @@ -11,8 +11,8 @@ class Trace(object): data: Optional[bytes] samples: ndarray - def __init__(self, title: Optional[str], data: Optional[bytes], - samples: ndarray, trace_set: Any = None): + def __init__(self, samples: ndarray, title: Optional[str], data: Optional[bytes], + trace_set: Any = None): self.title = title self.data = data self.samples = samples @@ -39,9 +39,9 @@ class Trace(object): class CombinedTrace(Trace): """A power trace that was combined from other traces, `parents`.""" - def __init__(self, title: Optional[str], data: Optional[bytes], - samples: ndarray, trace_set=None, parents: Sequence[Trace] = None): - super().__init__(title, data, samples, trace_set=trace_set) + def __init__(self, samples: ndarray, title: Optional[str], data: Optional[bytes], + trace_set=None, parents: Sequence[Trace] = None): + super().__init__(samples, title, data, trace_set=trace_set) self.parents = None if parents is not None: self.parents = weakref.WeakSet(parents) diff --git a/pyecsca/sca/trace_set/chipwhisperer.py b/pyecsca/sca/trace_set/chipwhisperer.py index d9dcf3f..2b37c6f 100644 --- a/pyecsca/sca/trace_set/chipwhisperer.py +++ b/pyecsca/sca/trace_set/chipwhisperer.py @@ -17,7 +17,7 @@ class ChipWhispererTraceSet(TraceSet): else: data = self.__read_data(path, name) trace_data = data["traces"] - traces = [Trace(None, None, trace_samples, trace_set=self) for trace_samples in trace_data] + traces = [Trace(trace_samples, None, None, trace_set=self) for trace_samples in trace_data] del data["traces"] config = self.__read_config(path, name) super().__init__(*traces, **data, **config) diff --git a/pyecsca/sca/trace_set/inspector.py b/pyecsca/sca/trace_set/inspector.py index f9475c0..b07313d 100644 --- a/pyecsca/sca/trace_set/inspector.py +++ b/pyecsca/sca/trace_set/inspector.py @@ -197,7 +197,7 @@ class InspectorTraceSet(TraceSet): samples = np.frombuffer( file.read(dtype.itemsize * self.num_samples), dtype, self.num_samples) - result.append(Trace(title, data, samples, trace_set=self)) + result.append(Trace(samples, title, data, trace_set=self)) return result def __write(self, file): @@ -229,9 +229,8 @@ class InspectorTraceSet(TraceSet): file.write(trace.samples.tobytes()) def __scale(self, traces): - return list(map(lambda trace: Trace(trace.title, trace.data, - trace.samples.astype("f4") * self.y_scale, - trace_set=self), + return list(map(lambda trace: Trace(trace.samples.astype("f4") * self.y_scale, trace.title, + trace.data, trace_set=self), traces)) def save(self, output: Union[Path, str, BinaryIO]): |
