Commit a46f48b03d6045128c528b89f39330026bc72ead

Authored by Brice COLOMBIER
1 parent d547646229
Exists in master

Add functions

Showing 11 changed files with 198 additions and 28 deletions

  1 +*.*~
1 2 *.npy
2 3 *.bin
3 4 *.txt
4   -*.*~
5 5 *.org
  6 +*.pyc
6 7 flymd.*
7   -plot.py
  8 +test*.*
downsample.py View file @ a46f48b
... ... @@ -26,5 +26,7 @@
26 26 downsampled_traces = downsample(traces, args.factor, args.offset)
27 27  
28 28 dirname, filename = os.path.split(args.traces_name)
29   - np.save(os.path.join(dirname, "downsampled_"+str(args.factor)+"_"+filename), downsampled_traces)
  29 + filename, extension = os.path.splitext(filename)
  30 +
  31 + np.save(os.path.join(dirname, filename+"_downsampled_"+str(args.factor)+extension), downsampled_traces)
filter_highest_variance.py View file @ a46f48b
1 1 import numpy as np
  2 +
2 3 import argparse
  4 +import os
3 5  
4 6 def filter_highest_variance(traces, ratio=0, nsamples=0):
5 7  
6 8  
7 9  
8 10  
... ... @@ -33,18 +35,14 @@
33 35 group.add_argument("--nsamples", type=int)
34 36 args = parser.parse_args()
35 37  
36   - fake_nb_samples = 10
37   - fake_nb_traces = 2
38   - test_array = np.random.random_integers(100, size=(fake_nb_traces, fake_nb_samples))
39   - traces = test_array
40   - # Load traces from file
41   - # traces = np.load(args.traces_name)
  38 + traces = np.load(args.traces_name)
42 39  
43 40 dirname, filename = os.path.split(args.traces_name)
  41 + filename, extension = os.path.splitext(filename)
44 42 if args.ratio:
45 43 filtered_variance_traces = filter_highest_variance(traces, ratio=args.ratio)
46   - np.save(dirname+"filtered_variance_"+str(args.ratio)+"_"+filename, filtered_variance_traces)
  44 + np.save(os.path.join(dirname, filename+"_filtered_highest_variance_ratio_"+str(args.ratio)+extension), filtered_variance_traces)
47 45 elif args.nsamples:
48 46 filtered_variance_traces = filter_highest_variance(traces, nsamples=args.nsamples)
49   - np.save(os.path.join(dirname, "filtered_variance_"+str(args.nsamples)+"_"+filename), filtered_variance_traces)
  47 + np.save(os.path.join(dirname, filename+"_filtered_highest_variance_nsamples_"+str(args.nsamples)+extension), filtered_variance_traces)
group_process.py View file @ a46f48b
... ... @@ -5,23 +5,43 @@
5 5 import os
6 6  
7 7 # Possible operations
8   -from pairwise_operation import pairwise_operation
  8 +from pairwise_operation import *
9 9 from downsample import downsample
10 10 from filter_highest_variance import filter_highest_variance
11 11 from shorten import shorten
12 12  
13   -def group_process(traces_name, prefix, nb_shares, function):
  13 +def group_process(dirname, filename, extension, prefix, nb_shares, function, *args):
14 14 for share in [str(i) for i in range(nb_shares)]:
15   - traces = np.load(prefix+"_"+share+"_"+traces_name)
16   - yield function(traces, 4)
  15 + traces = np.load(os.path.join(dirname, prefix+"_"+share+"_"+filename+extension))
  16 + yield function(traces, *args)
17 17  
18 18 if __name__ == "__main__":
19 19  
20   - filename = "masked_traces.npy"
21   - prefix = "split"
22   - for counter, processed_trace in enumerate(group_process(filename, prefix, 4, downsample)):
23   - print "BLA"
24   - np.save("processed_"+prefix+"_"+str(counter)+"_"+filename, processed_trace)
  20 + parser = argparse.ArgumentParser(description='Preprocess traces')
  21 + parser.add_argument("traces_name", type=str)
  22 + parser.add_argument("--prefix", type=str)
  23 + parser.add_argument("--nb_shares", type=int)
  24 + parser.add_argument("--function", type=str)
25 25  
26   -
  26 + group_process_args = parser.parse_known_args()[0]
  27 + function_args = parser.parse_known_args()[1]
  28 + functions={'pairwise_operation' : pairwise_operation,
  29 + 'downsample' : downsample,
  30 + 'filter_highest_variance' : filter_highest_variance,
  31 + 'shorten' : shorten,
  32 + }
  33 + try:
  34 + function=functions[group_process_args.function]
  35 + except KeyError:
  36 + raise ValueError('Invalid function')
  37 +
  38 + print group_process_args
  39 + print function_args
  40 +
  41 + dirname, filename = os.path.split(group_process_args.traces_name)
  42 + filename, extension = os.path.splitext(filename)
  43 +
  44 + for counter, processed_trace in enumerate(group_process(dirname, filename, extension, group_process_args.prefix, group_process_args.nb_shares, function, *function_args)):
  45 + print "Processing share {0}".format(counter)
  46 + np.save(os.path.join(dirname, "processed_"+args.prefix+"_"+str(counter)+"_"+filename), processed_trace)
  1 +import numpy as np
  2 +import math
  3 +
  4 +import argparse
  5 +import os
  6 +
  7 +def merge(*arg):
  8 + arg = arg[0] # Get first tuple element
  9 + final_trace = np.concatenate([np.load(i) for i in arg], axis=0)
  10 + print np.shape(final_trace)
  11 + return final_trace
  12 +
  13 +if __name__ == "__main__":
  14 +
  15 + # Parsing arguments
  16 + parser = argparse.ArgumentParser(description='Preprocess traces')
  17 + parser.add_argument("traces", nargs='*', type=str)
  18 + args = parser.parse_args()
  19 +
  20 + dirname, filename = os.path.split(args.traces[0])
  21 + filename, extension = os.path.splitext(filename)
  22 +
  23 + merged_traces = merge(args.traces)
  24 +
  25 + np.save(os.path.join(dirname, "merged_"+filename), merged_traces)
npy_to_bin.py View file @ a46f48b
  1 +import numpy as np
  2 +
  3 +import argparse
  4 +import os
  5 +
  6 +def npy_to_bin(traces_name, output_format):
  7 + traces = np.load(traces_name)
  8 +
  9 + dirname, filename = os.path.split(traces_name)
  10 + filename, extension = os.path.splitext(filename)
  11 +
  12 + traces.astype(output_format).tofile(os.path.join(dirname, filename)+'.bin')
  13 +
  14 +if __name__ == "__main__":
  15 +
  16 + # Parsing arguments
  17 + parser = argparse.ArgumentParser(description='Preprocess traces')
  18 + parser.add_argument("traces_name", type=str)
  19 + parser.add_argument("-o", "--output_format", type=str)
  20 + args = parser.parse_args()
  21 +
  22 + npy_to_bin(args.traces_name, args.output_format)
pairwise_operation.py View file @ a46f48b
... ... @@ -65,7 +65,7 @@
65 65 preprocessed_trace_length+=1
66 66 # Allocate memory for the preprocessed trace
67 67 if not check_ram.check_ram(preprocessed_trace_length, 64):
68   - raise MemoryError("The preprocessed trace cannot be stored in RAM")
  68 + raise MemoryError("The preprocessed trace, consisting in {0} samples of {1} bits, cannot be stored in RAM".format(preprocessed_trace_length, 64))
69 69 else:
70 70 log.info('This will occupy {0} Mo in RAM'.format(round(preprocessed_trace_length/(1024*1024))))
71 71 preprocessed_trace = np.zeros((preprocessed_trace_length, nb_traces), dtype=dtype)
... ... @@ -100,11 +100,11 @@
100 100 # Parsing arguments
101 101 parser = argparse.ArgumentParser(description='Preprocess traces')
102 102 parser.add_argument("traces_name", type=str)
103   - parser.add_argument("--op", type=str, choices=['addition', 'multiplication', 'squared_addition', 'absolute_difference'])
104   - parser.add_argument("--window_size", type=int)
105   - parser.add_argument("--min_dist", type=int)
106   - parser.add_argument("--dtype", type=str, nargs='?', default='float64')
107   - parser.add_argument("--ncores", type=int)
  103 + parser.add_argument("-o", "--op", type=str, choices=['addition', 'multiplication', 'squared_addition', 'absolute_difference'])
  104 + parser.add_argument("-w", "--window_size", type=int)
  105 + parser.add_argument("-d", "--min_dist", type=int)
  106 + parser.add_argument("-t", "--dtype", type=str, nargs='?', default='float64')
  107 + parser.add_argument("-n", "--ncores", type=int)
108 108 parser.add_argument('-v', '--verbose', action='store_true')
109 109 args = parser.parse_args()
110 110  
... ... @@ -128,6 +128,7 @@
128 128 # traces = test_array
129 129 # Load traces from file
130 130 traces = np.load(args.traces_name)
  131 + print "Input:", np.shape(traces)
131 132 # Shorten the traces to split them into equally-sized chunks
132 133 shortened = 0
133 134 while int(np.shape(traces)[1] + (args.ncores - 1)*(args.window_size - 1))%args.ncores != 0:
... ... @@ -166,5 +167,6 @@
166 167 # print "###\nGreat, sequential and\nparallel processing\nreturned the same result\n###"
167 168  
168 169 dirname, filename = os.path.split(args.traces_name)
  170 + print "Final trace of dimensions:", np.shape(preprocessed_traces_parallel)
169 171 np.save(os.path.join(dirname, "pairwise_"+args.op+"_"+filename), preprocessed_traces_parallel)
  1 +import numpy as np
  2 +import matplotlib.pyplot as plt
  3 +import argparse
  4 +
  5 +def plot(traces_file):
  6 + traces = np.load(traces_file)
  7 + plt.plot(traces[1,:])
  8 + plt.show()
  9 +
  10 +if __name__ == "__main__":
  11 +
  12 + # Parsing arguments
  13 + parser = argparse.ArgumentParser(description='Preprocess traces')
  14 + parser.add_argument("traces_file", type=str)
  15 + args = parser.parse_args()
  16 + plot(args.traces_file)
remove_window.py View file @ a46f48b
  1 +import numpy as np
  2 +
  3 +import argparse
  4 +import os
  5 +
  6 +import matplotlib.pyplot as plt
  7 +
  8 +def remove_window(traces, start_index, stop_index):
  9 + return np.delete(traces, range(start_index, stop_index), axis=1)
  10 +
  11 +if __name__ == "__main__":
  12 +
  13 + # Parsing arguments
  14 + parser = argparse.ArgumentParser(description='Preprocess traces')
  15 + parser.add_argument("traces_name", type=str)
  16 + parser.add_argument("--start_index", type=int)
  17 + parser.add_argument("--stop_index", type=int)
  18 + args = parser.parse_args()
  19 +
  20 + fake_nb_samples = 10
  21 + fake_nb_traces = 2
  22 +
  23 + test_array = np.random.random_integers(10, size=(fake_nb_traces, fake_nb_samples))
  24 + traces = test_array
  25 + # Load traces from file
  26 + traces = np.load(args.traces_name)
  27 +
  28 + plt.plot(traces[1,:], color='red')
  29 + remove_windowed_traces = remove_window(traces, args.start_index, args.stop_index)
  30 + plt.figure()
  31 + plt.plot(remove_windowed_traces[1,:], color='green')
  32 + plt.show()
  33 +
  34 + dirname, filename = os.path.split(args.traces_name)
  35 + filename, extension = os.path.splitext(filename)
  36 +
  37 + # np.save(os.path.join(dirname, "remove_windowed_"+str(args.start_index)+"_"+str(args.stop_index)+"_"+filename+extension), remove_windowed_traces)
... ... @@ -26,5 +26,7 @@
26 26 shortened_traces = shorten(traces, args.start_index, args.stop_index)
27 27  
28 28 dirname, filename = os.path.split(args.traces_name)
29   - np.save(os.path.join(dirname, filename+"_shortened"), shortened_traces)
  29 + filename, extension = os.path.splitext(filename)
  30 +
  31 + np.save(os.path.join(dirname, "shortened_"+str(args.start_index)+"_"+str(args.stop_index)+"_"+filename+extension), shortened_traces)
step_average.py View file @ a46f48b
  1 +import numpy as np
  2 +
  3 +import argparse
  4 +import os
  5 +import logging as log
  6 +
  7 +def step_average(traces, step_size, offset=0):
  8 +
  9 + shortened = 0
  10 + traces = traces[:,offset:]
  11 + while np.shape(traces)[1]%step_size!=0:
  12 + traces = traces[:,:-1].copy()
  13 + shortened+=1
  14 + if shortened:
  15 + log.warning("Traces shortened by {0} samples to split them into equally-sized chunks".format(shortened))
  16 +
  17 + step_traces = np.split(traces, np.shape(traces)[1]/step_size, axis=1)
  18 + step_averaged_traces = np.transpose(np.zeros(np.shape(step_traces)[0:2]))
  19 + for index, i in enumerate(step_traces):
  20 + mean = np.mean(step_traces[index], axis=1)
  21 + step_averaged_traces[:,index] = mean
  22 + return step_averaged_traces
  23 +
  24 +if __name__ == "__main__":
  25 +
  26 + # Parsing arguments
  27 + parser = argparse.ArgumentParser(description='Preprocess traces')
  28 + parser.add_argument("traces_name", type=str)
  29 + parser.add_argument("--step_size", type=int)
  30 + parser.add_argument("--offset", type=int, nargs='?', default=0)
  31 + args = parser.parse_args()
  32 +
  33 + fake_nb_samples = 8
  34 + fake_nb_traces = 2
  35 +
  36 + test_array = np.random.random_integers(2, size=(fake_nb_traces, fake_nb_samples))
  37 + traces = test_array
  38 + # Load traces from file
  39 + traces = np.load(args.traces_name)
  40 +
  41 + step_averaged_traces = step_average(traces, args.step_size, args.offset)
  42 + dirname, filename = os.path.split(args.traces_name)
  43 + filename, extension = os.path.splitext(filename)
  44 +
  45 + np.save(os.path.join(dirname, filename+"_step_averaged"+extension), step_averaged_traces)