application_vnd.jupyter.stderr_output_2.png text_plain_output_1.png
from tqdm import tqdm

import json

import numpy as np # linear algebra

import os

train_path = '/kaggle/input/abstraction-and-reasoning-challenge/training/'
evaluation_path = '/kaggle/input/abstraction-and-reasoning-challenge/evaluation/'
test_path = '/kaggle/input/abstraction-and-reasoning-challenge/test/'

same_shape = []
for ex in tqdm(os.listdir(evaluation_path)):
    with open(evaluation_path + ex, 'r') as train_file:
        all_im = json.load(train_file)
        im_in = np.array(all_im['train'][0]['input'])
        im_out = np.array(all_im['train'][0]['output'])
        if im_in.shape == im_out.shape:
            same_shape.append(ex)

def get_im_with_same_ioshape(file_path, name, show=False, mode='train'):
    train = []
    test = []
    with open(file_path + name, 'r') as train_file:
        all_im = json.load(train_file)
        im_in = np.array(all_im['train'][0]['input'])
        im_out = np.array(all_im['train'][0]['output'])
        if im_in.shape != im_out.shape:
            return None
        for im in all_im['train']:
            im_in = np.array(im['input'])
            im_out = np.array(im['output'])
            mask = np.asarray(np.nan_to_num((im_in - im_out) / (im_in - im_out), 0), 'int8')
            train.append((im_in, im_out, mask))
        if mode == 'train':
            for im in all_im['test']:
                im_in = np.array(im['input'])
                im_out = np.array(im['output'])
                test.append((im_in, im_out))
        if mode == 'test':
            for im in all_im['test']:
                im_in = np.array(im['input'])
                test.append(im_in)
    return (train, test)
train, test = get_im_with_same_ioshape(evaluation_path, same_shape[1], False)

def get_features(input_):
    im_in, im_out, mask = input_
    features = np.zeros((sum(sum(mask)), 8))
    colors = np.zeros(sum(sum(mask)))
    f = 0
    for y in range(mask.shape[0]):
        for x in range(mask.shape[1]):
            if mask[y, x] == 1:
                pix_exp = np.zeros(8)
                n_p = 0
                for dy in range(-1, 2):
                    for dx in range(-1, 2):
                        if dy != 0 or dx != 0:
                            if dx + x >= 0 and dy + y >= 0 and (dx + x < mask.shape[1]) and (dy + y < mask.shape[0]):
                                pix_exp[n_p] = im_in[y + dy, x + dx]
                            else:
                                pix_exp[n_p] = -1
                            n_p += 1
                features[f] = pix_exp
                colors[f] = im_out[y, x]
                f += 1
    return (features, colors)

def get_cf(train):
    features_set = []
    colors_set = []
    for in_out_mask in train:
        features, colors = get_features(in_out_mask)
        features_set += list(features)
        colors_set += list(colors)
    features_set_min = np.unique(np.array(features_set), axis=0)
    colors_min = np.zeros(len(features_set_min))
    for n, feature in enumerate(features_set):
        if feature in features_set_min:
            for i, feature_uniq in enumerate(features_set_min):
                if str(feature_uniq) == str(feature):
                    break
            colors_min[i] = colors_set[n]
    return (colors_min, features_set_min)
colors_min, features_set_min = get_cf(train)

print(colors_min, '\n')
print(features_set_min)