-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimulator_dna.py
440 lines (412 loc) · 16.1 KB
/
simulator_dna.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/python
# -*- coding: latin-1 -*-
import argparse
import os
import bcolors
import colorama
import time
import math
from random import random
from norec4dna.Encoder import Encoder
from norec4dna.rules.DNARules import DNARules
from norec4dna.RU10Decoder import RU10Decoder
from norec4dna.OnlineEncoder import OnlineEncoder
from norec4dna.OnlineBPDecoder import OnlineBPDecoder
from norec4dna.LTEncoder import LTEncoder
from norec4dna.LTBPDecoder import LTBPDecoder
from norec4dna.LTDecoder import LTDecoder
from norec4dna.distributions.IdealSolitonDistribution import IdealSolitonDistribution
from norec4dna.RU10Encoder import RU10Encoder
from norec4dna.distributions.RaptorDistribution import RaptorDistribution
from norec4dna.distributions.RobustSolitonDistribution import RobustSolitonDistribution
from norec4dna.distributions.OnlineDistribution import OnlineDistribution
from norec4dna.OnlineDecoder import OnlineDecoder
if os.name == "nt" and "PYCHARM_HOSTED" not in os.environ:
colorama.init()
lines = [
"Algorithm,ID,A_Permutation,T_Permutation,C_Permutation,G_Permutation,dinucleotid_Runs,Homopolymers,GC_Content,"
"Trinucleotid_Runs,Random_Permutation,Overall_Dropchance,Random_Number,Did_Drop"
]
useDNARules = True
algo_type = []
packetToDropChance = {}
def blackbox(encoder, decoder, scale_first=1.0, scale_second=1.0):
encoder.encode_to_packets()
encoded_packets = encoder.get_encoded_packets()
dec_input = 0
invalid_drop = 0
print(
bcolors.BOLD
+ "[+] Created "
+ str(len(encoded_packets))
+ " Packets"
+ bcolors.ENDC
)
for packet in encoded_packets:
if isinstance(decoder, LTBPDecoder) or isinstance(
decoder, OnlineBPDecoder
):
if not should_drop_packet(packet, scale=scale_first):
# first stage: we detect error and generate new packet (simulates encoding)
if not should_drop_packet(packet, False, scale=scale_second):
# second stage: simulate error for real DNA-Storage (simulates decoding)
if decoder.input_new_packet(packet):
dec_input += 1
decoder.solve()
print(
"[!] DNA-Simulator dropped "
+ str(invalid_drop)
+ " Packets and finished successful"
)
return decoder.is_decoded(), dec_input, invalid_drop
else:
invalid_drop += 1
else:
# we are in the FAST_decoder part...
if not should_drop_packet(packet, scale=scale_first):
# first stage: we detect error and generate new packet (simulates encoding)
if not should_drop_packet(packet, False, scale=scale_second):
# second stage: simulate error for real DNA-Storage (simulates decoding)
dec_input += 1
decoder.input_new_packet(packet)
else:
invalid_drop += 1
if (
packet.total_number_of_chunks <= dec_input - invalid_drop
) and decoder.solve():
print(
"DNA-Simulator dropped "
+ str(invalid_drop)
+ " Packets and finished successful"
)
return decoder.is_decoded(), dec_input, invalid_drop
j = 0
while j < invalid_drop:
packet = encoder.create_and_add_new_packet()
if not should_drop_packet(packet, scale=scale_first):
# first stage: we detect error and generate new packet (simulates encoding)
# this might happen transitive
if not should_drop_packet(packet, False, scale=scale_second):
# second stage: simulate error for real DNA-Storage (simulates decoding)
decoder.input_new_packet(packet)
dec_input += 1
# only increase j if the new packet has made it trough first stage
j += 1
decoder.solve()
print("[!] DNA-Simulator dropped " + str(invalid_drop) + " Packets.")
return decoder.is_decoded(), dec_input, invalid_drop
def should_drop_packet(packet, add_line=True, scale=1.0, rules=DNARules):
if not useDNARules:
return False
rand = random() # create number from [0,1)
if add_line:
drop_chance, data, annotated_packet = rules.apply_all_rules_with_data(packet)
drop_chance = drop_chance * scale # scale according to parameter
line = (
algo_type[0]
+ ","
+ str(hex(packet.id))
+ ","
+ ",".join([str(round(x, 4)) for x in data])
+ ","
+ str(drop_chance)
+ ","
+ str(rand)
+ ","
+ str(drop_chance > rand)
)
lines.append(line)
packetToDropChance[packet] = drop_chance
elif packet in packetToDropChance:
drop_chance = packetToDropChance[packet]
packetToDropChance.clear()
else:
drop_chance, data, annotated_packet = rules.apply_all_rules_with_data(packet)
# drop packet if rand bigger than the drop_chance for this Packet.
return drop_chance > rand
def blackboxOnlineTest(file, number_of_chunks=800, seed=2, overhead=0.20, scale_first=1.0, scale_second=1.0):
start = time.time()
epsilon = (
0.024343
) # pruefOrdnung: 0.007084 # according to filesize this should make a length of 200...
quality = 5
dist = OnlineDistribution(epsilon, seed)
number_of_chunks = dist.get_size()
algo_type.clear()
algo_type.append("Online_" + str(number_of_chunks) + "_" + str(dist.get_config_string()))
print(bcolors.OK + "Starting Blackbox Test with " + str(number_of_chunks) + " Chunks" + bcolors.ENDC)
encoder = OnlineEncoder(file, number_of_chunks, dist, epsilon, quality)
encoder.set_overhead_limit(overhead)
decoder = OnlineDecoder.pseudo_decoder(number_of_chunks)
decoder.set_read_all_before_decode(True)
result, dec_input, invalid_drop = blackbox(encoder, decoder, scale_first=scale_first, scale_second=scale_second)
end = time.time() - start
print(bcolors.BLUE + "Blackbox-Decode " + (
bcolors.OK + "successful" if result else bcolors.ERR + "NOT successful") + bcolors.END + bcolors.BLUE
+ " after " + str(round(end, 4)) + " sec." + bcolors.ENDC)
return ["Online_eps=" + str(epsilon) + "_quality=" + str(quality), result, number_of_chunks, dec_input,
invalid_drop, round(end, 4), ]
def blackboxLTTest(file, number_of_chunks=800, seed=2, chunk_size=0, overhead=0.20, scale_first=1.0, scale_second=1.0):
print(
bcolors.OK
+ "Starting Blackbox Test with "
+ str(number_of_chunks)
+ " Chunks"
+ bcolors.ENDC
)
start = time.time()
dist = RobustSolitonDistribution(S=number_of_chunks, delta=overhead, seed=seed)
algo_type.clear()
algo_type.append("LT_" + str(number_of_chunks) + "_" + str(dist.get_config_string()))
encoder = LTEncoder(file, number_of_chunks, dist, chunk_size=chunk_size)
encoder.set_overhead_limit(overhead)
decoder = LTDecoder.pseudo_decoder(number_of_chunks)
decoder.set_read_all_before_decode(True)
result, dec_input, invalid_drop = blackbox(
encoder, decoder, scale_first=scale_first, scale_second=scale_second
)
end = time.time() - start
print(
bcolors.BLUE
+ "Blackbox-Decode "
+ (bcolors.OK + "successful" if result else bcolors.ERR + "NOT successful")
+ bcolors.END
+ bcolors.BLUE
+ " after "
+ str(round(end, 4))
+ " sec."
+ bcolors.ENDC
)
return [dist.get_config_string(), result, number_of_chunks, dec_input, invalid_drop, round(end, 4), ]
def blackboxLTIdealTest(
file,
number_of_chunks=800,
seed=2,
chunk_size=0,
overhead=0.20,
scale_first=1.0,
scale_second=1.0,
):
print(bcolors.OK + "Starting Blackbox Test with " + str(number_of_chunks) + " Chunks" + bcolors.ENDC)
start = time.time()
dist = IdealSolitonDistribution(S=number_of_chunks, seed=seed)
algo_type.clear()
algo_type.append("LT_" + str(number_of_chunks) + "_" + str(dist.get_config_string()))
encoder = LTEncoder(file, number_of_chunks, dist, chunk_size=chunk_size)
encoder.set_overhead_limit(overhead)
decoder = LTDecoder.pseudo_decoder(number_of_chunks)
decoder.set_read_all_before_decode(True)
result, dec_input, invalid_drop = blackbox(encoder, decoder, scale_first=scale_first, scale_second=scale_second)
end = time.time() - start
print(
bcolors.BLUE
+ "Blackbox-Decode "
+ (bcolors.OK + "successful" if result else bcolors.ERR + "NOT successful")
+ bcolors.END
+ bcolors.BLUE
+ " after "
+ str(round(end, 4))
+ " sec."
+ bcolors.ENDC
)
return [
dist.get_config_string(),
result,
number_of_chunks,
dec_input,
invalid_drop,
round(end, 4),
]
def blackboxRU10Test(
file,
number_of_chunks=800,
seed=2,
chunk_size=200,
overhead=0.20,
scale_first=1.0,
scale_second=1.0,
):
print(
bcolors.OK
+ "Starting Blackbox Test with "
+ str(number_of_chunks)
+ " Chunks"
+ bcolors.ENDC
)
start = time.time()
dist = RaptorDistribution(number_of_chunks)
algo_type.clear()
algo_type.append("RU10_" + str(number_of_chunks) + "_" + str(dist.get_config_string()))
encoder = RU10Encoder(file, number_of_chunks, dist, chunk_size=chunk_size)
encoder.set_overhead_limit(overhead)
decoder = RU10Decoder.pseudo_decoder(number_of_chunks)
decoder.set_read_all_before_decode(True)
result, dec_input, invalid_drop = blackbox(
encoder, decoder, scale_first=scale_first, scale_second=scale_second
)
end = time.time() - start
print(
bcolors.BLUE
+ "Blackbox-Decode "
+ (bcolors.OK + "successful" if result else bcolors.ERR + "NOT successful")
+ bcolors.END
+ bcolors.BLUE
+ " after "
+ str(round(end, 4))
+ " sec."
+ bcolors.ENDC
)
return [
dist.get_config_string(),
result,
number_of_chunks,
dec_input,
invalid_drop,
round(end, 4),
]
def get_random_int(max_int):
return int(random() * max_int)
def main(file="logo.jpg", repeats=5):
csv = [
"filename, overhead, codecName, number_of_chunks, dec_input, invalid_drop, seed, result, time_needed"
]
name = "ERROR"
scale_first = 1.0
scale_second = 0.5
for mode in ["RU10", "LT", "LTIdeal", "Online"]:
for overhead in [0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, ]:
for _ in range(repeats):
rnd = get_random_int(math.pow(2, 31) - 1)
chunk_size = 100
insert_header = True
if chunk_size != 0:
number_of_chunks = Encoder.get_number_of_chunks_for_file_with_chunk_size(
file, chunk_size=chunk_size, insert_header=insert_header
)
else:
number_of_chunks = 800
try:
if mode == "Online":
res = blackboxOnlineTest(
file,
number_of_chunks=number_of_chunks,
seed=rnd,
overhead=overhead,
scale_first=scale_first,
scale_second=scale_second,
)
# chunk_size=chunk_size)
elif mode == "LT":
res = blackboxLTTest(
file,
number_of_chunks=number_of_chunks,
seed=rnd,
chunk_size=chunk_size,
overhead=overhead,
scale_first=scale_first,
scale_second=scale_second,
)
elif mode == "LTIdeal":
res = blackboxLTIdealTest(
file,
number_of_chunks=number_of_chunks,
seed=rnd,
chunk_size=chunk_size,
overhead=overhead,
scale_first=scale_first,
scale_second=scale_second,
)
else:
res = blackboxRU10Test(
file,
number_of_chunks=number_of_chunks,
seed=rnd,
chunk_size=chunk_size,
overhead=overhead,
scale_first=scale_first,
scale_second=scale_second,
)
name, result, number_of_chunks, dec_input, invalid_drop, time_needed = (
res
)
line = (
str(file)
+ ","
+ str(overhead)
+ ","
+ str(name)
+ ","
+ str(number_of_chunks)
+ ","
+ str(dec_input)
+ ","
+ str(invalid_drop)
+ ","
+ str(rnd)
+ ","
+ str(result)
+ ","
+ str(time_needed)
)
except Exception as ex:
raise ex
print(line)
csv.append(line)
dtimeno = (mode + "_" + str(overhead) + "_sim" + str(
time.strftime("%Y-%m-%d_%H-%M", time.localtime())) + ".csv")
with open("DNA_" + dtimeno, "w") as f:
for line in lines:
f.write(line + "\n")
lines.clear()
lines.append(
"Algorithm,ID,A_Permutation,T_Permutation,C_Permutation,G_Permutation,dinucleotid_Runs,Homopolymers,"
"GC_Content,Trinucleotid_Runs,Random_Permutation,Overall_Dropchance,Random_Number,Did_Drop"
)
with open(dtimeno, "w") as f:
for line in csv:
f.write(line + "\n")
csv = [
"filename, codecName, number_of_chunks, dec_input, invalid_drop, seed, result, time_needed"
]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Analyze network traffic")
parser.add_argument(
"-f", "--file", help="File to use for Simulation", required=True
)
parser.add_argument(
"-p",
"--profile",
help='If set, a profiling Graph "pycallgraph.png" will be created.',
action="store_true",
required=False,
)
parser.add_argument(
"-r",
"--repeats",
type=int,
help="Number of repeats the Simulator should run (default=5)",
default=5,
)
args = parser.parse_args()
profile = bool(args.profile)
filename = str(args.file)
repeats = int(args.repeats)
if profile:
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
print(
bcolors.WARN
+ "[!] running with Profiler - this might decrease performance"
+ bcolors.ENDC
)
with PyCallGraph(output=GraphvizOutput()):
main(filename, repeats)
print(
bcolors.BLUE
+ '[*] profiling Graph saved as "pycallgraph.png"'
+ bcolors.ENDC
)
else:
main(filename, repeats)
else:
print(bcolors.WARN + "[!] WARNING: RUN THIS SCRIPT DIRECTLY!" + bcolors.ENDC)