-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_distribution.py
71 lines (63 loc) · 3.59 KB
/
image_distribution.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
from utility import Move, in_bound, add_to_dict
class ImageDistribution:
MOVESET = Move.CCW
def __init__(self):
self.unit_frequency = {}
self.pair_frequency = [{} for _ in ImageDistribution.MOVESET]
self.pair_dir_frequency = {}
self.context_frequency = {}
self.unit_frequency_sorted = []
self.pair_frequency_sorted = [[] for _ in ImageDistribution.MOVESET]
self.pair_dir_frequency_sorted = []
self.context_frequency_sorted = []
self.exists = set()
self.unit_numbers = set()
def train(self, tiled_image):
self._train_unit_frequency(tiled_image)
self._train_pair_frequency(tiled_image)
self._train_context_frequency(tiled_image)
def get_unit_frequency(self, unit):
return self.unit_frequency.get(unit, 0)
def get_context_frequency(self, context):
return self.context_frequency.get(context, 0)
def _train_unit_frequency(self, tiled_image):
units = tiled_image.unit_numbers
for i in range(units.shape[0]):
for j in range(units.shape[1]):
add_to_dict(self.unit_frequency, units[i, j])
self.unit_numbers.add(units[i, j])
self.unit_frequency_sorted = sorted(list(self.unit_frequency.items()), key=lambda keyvalue: -keyvalue[1])
def _train_pair_frequency(self, tiled_image):
units = tiled_image.unit_numbers
for i in range(units.shape[0]):
for j in range(units.shape[1]):
for k in range(len(ImageDistribution.MOVESET)):
dx, dy = ImageDistribution.MOVESET[k]
if in_bound(i+dx, j+dy, units):
add_to_dict(self.pair_frequency[k], (units[i, j], units[i+dx, j+dy]))
add_to_dict(self.pair_dir_frequency, (units[i, j], units[i+dx, j+dy], k))
self.exists.add((units[i, j], units[i+dx, j+dy], k))
for k in range(len(ImageDistribution.MOVESET)):
self.pair_frequency_sorted[k] = sorted(list(self.pair_frequency[k].items()), key=lambda keyvalue: -keyvalue[1])
self.pair_dir_frequency_sorted = sorted(list(self.pair_dir_frequency.items()), key=lambda keyvalue: -keyvalue[1])
def _train_context_frequency(self, tiled_image):
units = tiled_image.unit_numbers
for i in range(units.shape[0]):
for j in range(units.shape[1]):
context = [units[i, j], None, None, None, None]
for k in range(len(ImageDistribution.MOVESET)):
dx, dy = ImageDistribution.MOVESET[k]
if in_bound(i+dx, j+dy, units):
context[k+1] = units[i+dx, j+dy]
# TODO None senario probably won't happen at all
for e1 in range(1 if context[1] is None else 2):
for e2 in range(1 if context[2] is None else 2):
for e3 in range(1 if context[3] is None else 2):
for e4 in range(1 if context[4] is None else 2):
context_key = (units[i, j],
None if e1 == 1 else context[1],
None if e2 == 1 else context[2],
None if e3 == 1 else context[3],
None if e4 == 1 else context[4])
add_to_dict(self.context_frequency, context_key)
self.context_frequency_sorted = sorted(list(self.context_frequency.items()), key=lambda keyvalue: -keyvalue[1])