-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorrelation_functions.py
103 lines (87 loc) · 3.38 KB
/
correlation_functions.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
import math
import numpy as np
import collections
from preprocessing import Pixel, Grid
FILENAME = 'correlation_functions'
"""
Compares the grid-objects from input with their output.
Returns a list of correlation-objects.
"""
def correlate(preprocessed_task):
correlations = []
# Create List of correlations between each input and output
for i in range(len(preprocessed_task)):
corr = Correlation(preprocessed_task[i][0], preprocessed_task[i][1])
correlations.append(corr)
return correlations
class Correlation:
def __init__(self, grid1: Grid, grid2: Grid):
self.grid1 = grid1
self.grid2 = grid2
self.sameShape = grid1.shape == grid2.shape
self.sameColorCount = self.color_count(grid1) == self.color_count(grid2)
self.sameSize = grid1.size == grid2.size
self.sameColors = np.all(grid1.colors == grid2.colors)
color_diff_prep = self.color_count(grid1)
color_diff_prep.subtract(self.color_count(grid2))
self.colorDiff = color_diff_prep
self.sameObjectsIdpPosFixCol = self._get_same_objects_idp_pos_fix_col()
self.sameObjectsFixPosFixCol = self._get_same_objects_fix_pos_fix_col()
self.sameObjectsFixPosIdpCol = self._get_same_objects_fix_pos_idp_col()
self.sameObjectsIdpPosIdpCol = self._get_same_objects_idp_pos_idp_col()
self.diff = grid1.raw - grid2.raw
"""Counter of different colors"""
def color_count(self, grid: Grid):
pixels = grid.getPixels()
counter = collections.Counter()
for pixel in pixels:
counter[pixel.color] += 1
return counter
def _get_same_objects_idp_pos_fix_col(self):
objects1 = self.grid1.objects
objects2 = self.grid2.objects
same = []
for o1 in objects1:
for o2 in objects2:
if np.array_equal(o1.raw, o2.raw):
same.append(o1)
break
return same
def _get_same_objects_fix_pos_fix_col(self):
objects1 = self.grid1.objects
objects2 = self.grid2.objects
same = []
for o1 in objects1:
for o2 in objects2:
if np.array_equal(o1.raw, o2.raw):
is_same = True
for i, pixel in enumerate(o1.pixels):
if not np.array_equal(pixel.coord, o2.pixels[i].coord):
is_same = False
if is_same:
same.append(o1)
return same
def _get_same_objects_fix_pos_idp_col(self):
objects1 = self.grid1.objects
objects2 = self.grid2.objects
same = []
for o1 in objects1:
for o2 in objects2:
if o1.raw.shape == o2.raw.shape:
same_pos = True
for i, pixel in enumerate(o1.pixels):
if not np.array_equal(pixel.coord, o2.pixels[i].coord):
same_pos = False
if same_pos:
same.append(o1)
return same
def _get_same_objects_idp_pos_idp_col(self):
objects1 = self.grid1.objects
objects2 = self.grid2.objects
same = []
for o1 in objects1:
for o2 in objects2:
if o1.raw.shape == o2.raw.shape:
same.append(o1)
break
return same