-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.py
109 lines (96 loc) · 3.67 KB
/
preprocessing.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
import os
import json
import numpy as np
from skimage.measure import label, regionprops
BORDER_VALUE = 0
TRAINING_DATA_FOLDER_NAME = 'arcdata/evaluation';
fileNames = os.listdir(TRAINING_DATA_FOLDER_NAME);
fileNames = list(filter(lambda x: 'label' not in x, fileNames))
class Pixel:
def getX(self):
return self.coord[0]
def getY(self):
return self.coord[1]
def __init__(self, color, coord):
self.color = color
self.coord = coord
class Grid:
def getPixels(self):
return self.pixels
def getRawWithoutColor(self):
no_color_raw = np.array(self.raw)
no_color_raw[np.where(no_color_raw != 0)] = 1
return no_color_raw
def __init__(self, raw_grid, pixels=[], child=False):
self.raw = raw_grid
self.shape = raw_grid.shape
self.sum = np.sum(raw_grid)
self.size = np.count_nonzero(raw_grid)
if(pixels):
self.pixels = pixels
else:
self.pixels = [Pixel(color, [i[0], i[1]]) for i, color in np.ndenumerate(raw_grid)]
self.colors = np.unique(raw_grid)
self.objects = find_objects(raw_grid, child)
def evaluate_effectiveness_of_function(function):
"""
The idea of this function is that a newly created library function can be tested directly on the evaluation data
(how many problems could be solved right away with this functionality).
"""
solved_files = []
for fileName in fileNames:
with open(f'{TRAINING_DATA_FOLDER_NAME}/{fileName}', 'r') as file:
data = json.loads(file.read())
result = []
#print(data)
for i in data['train']:
#print(i)
result.append(np.array_equal(function(i['input']), np.array(i['output'])))
if np.all(result):
solved_files.append(fileName)
return solved_files
def add_border(matrix):
''''Outline a grid so out of bounds exceptions can be minimized (nxm => n+2xm+2)'''
return np.pad(matrix, 1, mode='constant', constant_values=BORDER_VALUE)
def matrix_per_color(matrix):
"""
Extract all equal color pixels from a matrix and return one matrix per color
:param matrix:
:return: result_matrices
"""
grids_by_color = []
for i in np.nditer(np.unique(matrix)):
if i == 0:
continue
grids_by_color.append(np.where(matrix==i, True, False) * i)
grids_by_color.append(matrix)
return grids_by_color
#1. Suche erste Zahl, welche nicht 0 oder 10 ist
#2. Get Neighbours rekursiv bis alle neighbours schon im objekt oder 0 oder 10 sind
#3. suche nächste Zahl, welche in keinem objekt vorkommt und nicht 0 ist
def find_objects(raw_grid, child):
if child == True:
return
objects = []
for i in regionprops(label(raw_grid, connectivity=2)):
grid = []
for j in i.coords:
grid.append(Pixel(raw_grid[j[0]][j[1]], j))
objects.append(Grid(normalize(grid), grid, True))
return objects
def normalize(pixels):
x = max(list(map(Pixel.getX, pixels))) - min(list(map(Pixel.getX, pixels)))
y = max(list(map(Pixel.getY, pixels))) - min(list(map(Pixel.getY, pixels)))
raw = np.zeros((x + 1,y + 1))
for p in pixels:
raw[(p.getX() - min(list(map(Pixel.getX, pixels))), p.getY() - min(list(map(Pixel.getY, pixels))))] = p.color
return raw
"""
In dieser funktion wird preprocessing für alle matrizen durchgeführt.
Return value ist eine 2D array von input output Grid Objects
[[Grid_Input_1, Grid_Output_1],
[Grid_Input_2, Grid_Output_2],
[Grid_Input_3, Grid_Output_3]]
"""
def preprocess(task):
return [[Grid(io_tuple[0]), Grid(io_tuple[1])] for io_tuple in task]