-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigit_mutator.py
51 lines (40 loc) · 1.59 KB
/
digit_mutator.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
import random
import mutation_manager
import rasterization_tools
import vectorization_tools
from digit_input import Digit
from properties import MUTOPPROB
from utils import get_distance
class DigitMutator:
def __init__(self, digit):
self.digit = digit
def mutate(self, reference=None):
# Select mutation operator.
rand_mutation_probability = random.uniform(0, 1)
if rand_mutation_probability >= MUTOPPROB:
mutation = 1
else:
mutation = 2
condition = True
counter_mutations = 0
while condition:
counter_mutations += 1
mutant_vector = mutation_manager.mutate(self.digit.xml_desc, mutation, counter_mutations/20)
mutant_xml_desc = vectorization_tools.create_svg_xml(mutant_vector)
rasterized_digit = rasterization_tools.rasterize_in_memory(mutant_xml_desc)
distance_inputs = get_distance(self.digit.purified, rasterized_digit)
if distance_inputs != 0:
if reference is not None:
distance_inputs = get_distance(reference.purified, rasterized_digit)
if distance_inputs != 0:
condition = False
else:
condition = False
self.digit.xml_desc = mutant_xml_desc
self.digit.purified = rasterized_digit
# TODO
self.digit.confidence = list()
self.digit.predicted_label = list()
self.digit.confidence_original = list()
self.digit.predicted_label_original = list()
return distance_inputs