Commit 160a9caea8af7c382641b04a8c9d19fcffac64d3

Authored by Brice COLOMBIER
1 parent dcf51f9030
Exists in master

Rename and add downsample

Showing 3 changed files with 187 additions and 160 deletions

downsample.py View file @ 160a9ca
  1 +import numpy as np
  2 +import argparse
  3 +
  4 +def downsample(traces, factor, offset=0):
  5 + return traces[:,offset::factor]
  6 +
  7 +if __name__ == "__main__":
  8 +
  9 + # Parsing arguments
  10 + parser = argparse.ArgumentParser(description='Preprocess traces')
  11 + parser.add_argument("traces_name", type=str)
  12 + parser.add_argument("factor", type=int)
  13 + parser.add_argument("offset", type=int, nargs='?', default=0)
  14 + args = parser.parse_args()
  15 +
  16 + fake_nb_samples = 10
  17 + fake_nb_traces = 2
  18 +
  19 + test_array = np.random.random_integers(10, size=(fake_nb_traces, fake_nb_samples))
  20 + traces = test_array
  21 + # Load traces from file
  22 + # traces = np.load(args.traces_name)
  23 +
  24 + print traces
  25 + downsampled_traces = downsample(traces, args.factor, args.offset)
  26 + print downsampled_traces
  27 + np.save("downsampled_"+str(args.factor)+"_"+args.traces_name, downsampled_traces)
pairwise_operation.py View file @ 160a9ca
  1 +# Imports for processing
  2 +import numpy as np
  3 +from skimage.util.shape import view_as_windows
  4 +
  5 +# Imports for parallel processing
  6 +from multiprocessing import Pool, current_process
  7 +import math
  8 +import copy
  9 +
  10 +# Imports for script utilitaries
  11 +import logging as log
  12 +import argparse
  13 +
  14 +# Imports for dev
  15 +import cProfile
  16 +import time
  17 +
  18 +def multi_run_wrapper(args):
  19 + return pairwise_operation(*args)
  20 +
  21 +def pairwise_operation(traces,
  22 + window_size,
  23 + minimum_distance,
  24 + operation,
  25 + dtype,
  26 + start_index=0,
  27 + verbose=False,
  28 + first_chunk=True):
  29 +
  30 + """Operates in a sliding window_size over the trace
  31 + Computes f(x_i, x_j) for all possible pairs of samples (x_i, x_j)
  32 + in the window_size with distance(x_i, x_j) > minimum_distance.
  33 +
  34 + Keyword arguments:
  35 + traces_name: name of the file storing the traces
  36 + window_size: size of the window in which pairwise operation is done
  37 + minimum_distance: minimum distance between two samples processed
  38 + operation: processing operation to apply on the pair of samples
  39 + start_index:
  40 + verbose: display INFO
  41 + first_chunk: indicates first chunk of data for parallel processing
  42 +
  43 + Returns:
  44 + preprocessed_trace: numpy array containing the preprocessed trace
  45 + indexes: the indexes of the processed pairs
  46 + """
  47 +
  48 + if verbose:
  49 + log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG)
  50 + if not current_process()._identity or current_process()._identity[0] == 1:
  51 + log.info("Verbose output enabled")
  52 + else:
  53 + log.basicConfig(format="%(levelname)s: %(message)s")
  54 +
  55 + nb_traces, nb_samples = np.shape(traces)
  56 + if not current_process()._identity:
  57 + log.info("Processing {0} traces of {1} samples".format(nb_traces, nb_samples))
  58 +
  59 + preprocessed_trace_length = 0
  60 + for current_distance in xrange(minimum_distance, window_size):
  61 + for current_start_index in xrange(nb_samples - current_distance):
  62 + if first_chunk or (not first_chunk and current_start_index+current_distance>=window_size-1):
  63 + preprocessed_trace_length+=1
  64 + # Allocate memory for the preprocessed trace
  65 + preprocessed_trace = np.zeros((preprocessed_trace_length, nb_traces), dtype=dtype)
  66 + current_index = 0
  67 + indexes = np.zeros((preprocessed_trace_length),dtype='i,i')
  68 + # For all possible start indices in the window
  69 + for current_distance in xrange(minimum_distance, window_size):
  70 + for current_start_index in xrange(nb_samples - current_distance):
  71 + if first_chunk or (not first_chunk and current_start_index+current_distance>=window_size-1):
  72 + value = np.array(operation(traces[:,current_start_index], traces[:,current_start_index+current_distance]), ndmin=2)
  73 + # Store the resulting vector
  74 + preprocessed_trace[current_index,:] = np.transpose(value)[:,0]
  75 + indexes[current_index] = (start_index+current_start_index, start_index+current_start_index+current_distance)
  76 + # Increase the running index
  77 + current_index+=1
  78 + preprocessed_trace = np.transpose(preprocessed_trace)
  79 + return preprocessed_trace, indexes
  80 +
  81 +# Operations to perform on the pairs of samples
  82 +def multiplication(*args):
  83 + return args[0] * args[1]
  84 +def addition(*args):
  85 + return args[0] + args[1]
  86 +def squared_addition(*args):
  87 + return (args[0] + args[1])*(args[0] + args[1])
  88 +def absolute_difference(*args):
  89 + return abs(args[0] - args[1])
  90 +
  91 +if __name__ == "__main__":
  92 +
  93 + # Parsing arguments
  94 + parser = argparse.ArgumentParser(description='Preprocess traces')
  95 + parser.add_argument("traces_name", type=str)
  96 + parser.add_argument("--op", type=str, choices=['addition', 'multiplication', 'squared_addition', 'absolute_difference'])
  97 + parser.add_argument("--window_size", type=int)
  98 + parser.add_argument("--min_dist", type=int)
  99 + parser.add_argument("--dtype", type=str)
  100 + parser.add_argument("--ncores", type=int)
  101 + parser.add_argument('-v', '--verbose', action='store_true')
  102 + args = parser.parse_args()
  103 +
  104 + if args.op == 'multiplication': operation = multiplication
  105 + elif args.op == 'addition': operation = addition
  106 + elif args.op == 'squared_addition': operation = squared_addition
  107 + elif args.op == 'absolute_difference': operation = absolute_difference
  108 + dtype = np.dtype(args.dtype).type
  109 + if args.verbose:
  110 + log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG)
  111 + if not current_process()._identity or current_process()._identity[0] == 1:
  112 + log.info("Verbose output enabled")
  113 + else:
  114 + log.basicConfig(format="%(levelname)s: %(message)s")
  115 +
  116 +
  117 + # Generate fake data for testing purposes
  118 + fake_nb_samples = 1000
  119 + fake_nb_traces = 1000
  120 + # test_array = np.array([xrange(i, i+data_set_width-data_set_height) for i in xrange(data_set_height)])
  121 + test_array = np.random.rand(fake_nb_traces, fake_nb_samples)
  122 + traces = test_array
  123 + # Load traces from file
  124 + # traces = np.load(args.traces_name)
  125 + # Shorten the traces to split them into equally-sized chunks
  126 + shortened = 0
  127 + while int(np.shape(traces)[1] + (args.ncores - 1)*(args.window_size - 1))%args.ncores != 0:
  128 + traces = traces[:,:-1].copy()
  129 + shortened+=1
  130 + if shortened:
  131 + log.warning("Traces shortened by {0} samples to split them into equally-sized chunks".format(shortened))
  132 + nb_samples = np.shape(traces)[1]
  133 +
  134 + # Perform non-parallel preprocessing
  135 + preprocessed_traces = []
  136 + # preprocessed_traces, indexes = pairwise_operation(traces, args.window_size, args.min_dist, operation, dtype, args.verbose)
  137 +
  138 + # Init pool of workers for parallel preprocessing
  139 + pool = Pool(args.ncores)
  140 + # Compute the size of each chunk of traces to be preprocessed
  141 + chunk_size = int(np.shape(traces)[1]+(args.ncores-1)*(args.window_size-1))/args.ncores
  142 + log.info("Traces split into {0} chunks of {1} samples".format(args.ncores, chunk_size))
  143 + # Split the traces, with overlapping
  144 + traces = view_as_windows(traces, (np.shape(traces)[0],chunk_size), step=chunk_size-args.window_size+1)[0]
  145 + # Create the list of arguments
  146 + start_indexes = xrange(0, nb_samples, chunk_size-args.window_size+1)
  147 + arguments = [(trace_set, args.window_size, args.min_dist, operation, dtype, start_index, args.verbose, first_chunk) for (trace_set, start_index, first_chunk) in zip(traces, start_indexes, [True]+(args.ncores-1)*[False])]
  148 + # Run the parallel computation
  149 + parallel_processing_results = np.concatenate(pool.map(multi_run_wrapper, arguments), axis=1)
  150 + # print parallel_processing_results
  151 + preprocessed_traces_parallel, indexes_parallel = parallel_processing_results[::2], parallel_processing_results[1::2]
  152 + preprocessed_traces_parallel = np.concatenate(preprocessed_traces_parallel, axis=1)
  153 + indexes_parallel = np.concatenate(indexes_parallel, axis=1)
  154 +
  155 + # Compare normal and parallel processing
  156 + if preprocessed_traces:
  157 + if np.all(preprocessed_traces.sort()==preprocessed_traces_parallel.sort()):
  158 + if np.all(indexes.sort()==indexes_parallel.sort()):
  159 + print "###\nGreat, sequential and\nparallel processing\nreturned the same result\n###"
  160 + np.save("preprocessed_"+args.traces_name, preprocessed_traces_parallel)
preprocessing.py View file @ 160a9ca
1   -# Imports for processing
2   -import numpy as np
3   -from skimage.util.shape import view_as_windows
4   -
5   -# Imports for parallel processing
6   -from multiprocessing import Pool, current_process
7   -import math
8   -import copy
9   -
10   -# Imports for script utilitaries
11   -import logging as log
12   -import argparse
13   -
14   -# Imports for dev
15   -import cProfile
16   -import time
17   -
18   -def multi_run_wrapper(args):
19   - return pairwise_operation(*args)
20   -
21   -def pairwise_operation(traces,
22   - window_size,
23   - minimum_distance,
24   - operation,
25   - dtype,
26   - start_index=0,
27   - verbose=False,
28   - first_chunk=True):
29   -
30   - """Operates in a sliding window_size over the trace
31   - Computes f(x_i, x_j) for all possible pairs of samples (x_i, x_j)
32   - in the window_size with distance(x_i, x_j) > minimum_distance.
33   -
34   - Keyword arguments:
35   - traces_name: name of the file storing the traces
36   - window_size: size of the window in which pairwise operation is done
37   - minimum_distance: minimum distance between two samples processed
38   - operation: processing operation to apply on the pair of samples
39   - start_index:
40   - verbose: display INFO
41   - first_chunk: indicates first chunk of data for parallel processing
42   -
43   - Returns:
44   - preprocessed_trace: numpy array containing the preprocessed trace
45   - indexes: the indexes of the processed pairs
46   - """
47   -
48   - if verbose:
49   - log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG)
50   - if not current_process()._identity or current_process()._identity[0] == 1:
51   - log.info("Verbose output enabled")
52   - else:
53   - log.basicConfig(format="%(levelname)s: %(message)s")
54   -
55   - nb_traces, nb_samples = np.shape(traces)
56   - if not current_process()._identity:
57   - log.info("Processing {0} traces of {1} samples".format(nb_traces, nb_samples))
58   -
59   - preprocessed_trace_length = 0
60   - for current_distance in xrange(minimum_distance, window_size):
61   - for current_start_index in xrange(nb_samples - current_distance):
62   - if first_chunk or (not first_chunk and current_start_index+current_distance>=window_size-1):
63   - preprocessed_trace_length+=1
64   - # Allocate memory for the preprocessed trace
65   - preprocessed_trace = np.zeros((preprocessed_trace_length, nb_traces), dtype=dtype)
66   - current_index = 0
67   - indexes = np.zeros((preprocessed_trace_length),dtype='i,i')
68   - # For all possible start indices in the window
69   - for current_distance in xrange(minimum_distance, window_size):
70   - for current_start_index in xrange(nb_samples - current_distance):
71   - if first_chunk or (not first_chunk and current_start_index+current_distance>=window_size-1):
72   - value = np.array(operation(traces[:,current_start_index], traces[:,current_start_index+current_distance]), ndmin=2)
73   - # Store the resulting vector
74   - preprocessed_trace[current_index,:] = np.transpose(value)[:,0]
75   - indexes[current_index] = (start_index+current_start_index, start_index+current_start_index+current_distance)
76   - # Increase the running index
77   - current_index+=1
78   - preprocessed_trace = np.transpose(preprocessed_trace)
79   - return preprocessed_trace, indexes
80   -
81   -# Operations to perform on the pairs of samples
82   -def multiplication(*args):
83   - return args[0] * args[1]
84   -def addition(*args):
85   - return args[0] + args[1]
86   -def squared_addition(*args):
87   - return (args[0] + args[1])*(args[0] + args[1])
88   -def absolute_difference(*args):
89   - return abs(args[0] - args[1])
90   -
91   -if __name__ == "__main__":
92   -
93   - # Parsing arguments
94   - parser = argparse.ArgumentParser(description='Preprocess traces')
95   - parser.add_argument("traces_name", type=str)
96   - parser.add_argument("--op", type=str, choices=['addition', 'multiplication', 'squared_addition', 'absolute_difference'])
97   - parser.add_argument("--window_size", type=int)
98   - parser.add_argument("--min_dist", type=int)
99   - parser.add_argument("--dtype", type=str)
100   - parser.add_argument("--ncores", type=int)
101   - parser.add_argument('-v', '--verbose', action='store_true')
102   - args = parser.parse_args()
103   -
104   - if args.op == 'multiplication': operation = multiplication
105   - elif args.op == 'addition': operation = addition
106   - elif args.op == 'squared_addition': operation = squared_addition
107   - elif args.op == 'absolute_difference': operation = absolute_difference
108   - dtype = np.dtype(args.dtype).type
109   - if args.verbose:
110   - log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG)
111   - if not current_process()._identity or current_process()._identity[0] == 1:
112   - log.info("Verbose output enabled")
113   - else:
114   - log.basicConfig(format="%(levelname)s: %(message)s")
115   -
116   -
117   - # Generate fake data for testing purposes
118   - fake_nb_samples = 1000
119   - fake_nb_traces = 1000
120   - # test_array = np.array([xrange(i, i+data_set_width-data_set_height) for i in xrange(data_set_height)])
121   - test_array = np.random.rand(fake_nb_traces, fake_nb_samples)
122   - traces = test_array
123   - # Load traces from file
124   - # traces = np.load(args.traces_name)
125   - # Shorten the traces to split them into equally-sized chunks
126   - shortened = 0
127   - while int(np.shape(traces)[1] + (args.ncores - 1)*(args.window_size - 1))%args.ncores != 0:
128   - traces = traces[:,:-1].copy()
129   - shortened+=1
130   - if shortened:
131   - log.warning("Traces shortened by {0} samples to split them into equally-sized chunks".format(shortened))
132   - nb_samples = np.shape(traces)[1]
133   -
134   - # Perform non-parallel preprocessing
135   - preprocessed_traces = []
136   - # preprocessed_traces, indexes = pairwise_operation(traces, args.window_size, args.min_dist, operation, dtype, args.verbose)
137   -
138   - # Init pool of workers for parallel preprocessing
139   - pool = Pool(args.ncores)
140   - # Compute the size of each chunk of traces to be preprocessed
141   - chunk_size = int(np.shape(traces)[1]+(args.ncores-1)*(args.window_size-1))/args.ncores
142   - log.info("Traces split into {0} chunks of {1} samples".format(args.ncores, chunk_size))
143   - # Split the traces, with overlapping
144   - traces = view_as_windows(traces, (np.shape(traces)[0],chunk_size), step=chunk_size-args.window_size+1)[0]
145   - # Create the list of arguments
146   - start_indexes = xrange(0, nb_samples, chunk_size-args.window_size+1)
147   - arguments = [(trace_set, args.window_size, args.min_dist, operation, dtype, start_index, args.verbose, first_chunk) for (trace_set, start_index, first_chunk) in zip(traces, start_indexes, [True]+(args.ncores-1)*[False])]
148   - # Run the parallel computation
149   - parallel_processing_results = np.concatenate(pool.map(multi_run_wrapper, arguments), axis=1)
150   - # print parallel_processing_results
151   - preprocessed_traces_parallel, indexes_parallel = parallel_processing_results[::2], parallel_processing_results[1::2]
152   - preprocessed_traces_parallel = np.concatenate(preprocessed_traces_parallel, axis=1)
153   - indexes_parallel = np.concatenate(indexes_parallel, axis=1)
154   -
155   - # Compare normal and parallel processing
156   - if preprocessed_traces:
157   - if np.all(preprocessed_traces.sort()==preprocessed_traces_parallel.sort()):
158   - if np.all(indexes.sort()==indexes_parallel.sort()):
159   - print "###\nGreat, sequential and\nparallel processing\nreturned the same result\n###"
160   - np.save("preprocessed_masked_traces.npy", preprocessed_traces_parallel)