text_plain_output_1.png
from matplotlib import colors

import json

import matplotlib.pyplot as plt

import re # Regular expressions

testDirectory = '/kaggle/input/abstraction-and-reasoning-challenge/test/'
trainingDirectory = '/kaggle/input/abstraction-and-reasoning-challenge/training/'
evaluationDirectory = '/kaggle/input/abstraction-and-reasoning-challenge/training/'

def readTaskFile(filename):
    f = open(filename, 'r')
    data = json.loads(f.read())
    data['id'] = re.sub('(.*/)|(\\.json)', '', filename)
    f.close()
    return data

filename = testDirectory + '19bb5feb.json'
readTaskFile(filename)

def getGridSizeComparison(filename):
    data = readTaskFile(filename)
    trainSection = data['train']
    ident = data['id']
    numTrain = len(trainSection)
    result = {}
    for i in range(numTrain):
        trainCase = trainSection[i]
        trainCaseInput = trainCase['input']
        trainCaseOutput = trainCase['output']
        sameY = len(trainCaseInput) == len(trainCaseOutput)
        sameX = len(trainCaseInput[0]) == len(trainCaseOutput[0])
        result[ident + '_train_' + str(i)] = sameX and sameY
    return result

filename = testDirectory + '19bb5feb.json'
getGridSizeComparison(filename)

# Visualise the training cases for a task
# Code inspiration from https://www.kaggle.com/inversion/abstraction-and-reasoning-starter-notebook
def plotTaskTraining(task):
    """
    Plots the training pairs of a specified task,
    using same color scheme as the ARC app
    """
    cmap = colors.ListedColormap(
        ['#000000', '#0074D9','#FF4136','#2ECC40','#FFDC00',
         '#AAAAAA', '#F012BE', '#FF851B', '#7FDBFF', '#870C25'])
    norm = colors.Normalize(vmin=0, vmax=9)
    
    # Plot all the training cases
    nTrainingCases = len(task["train"])
    fig, axs = plt.subplots(nTrainingCases, 2, figsize=(15,15))
    for i in range(nTrainingCases):
        axs[i][0].imshow(task['train'][i]['input'], cmap=cmap, norm=norm)
        axs[i][0].axis('off')
        axs[i][0].set_title('Train Input')
        axs[i][1].imshow(task['train'][i]['output'], cmap=cmap, norm=norm)
        axs[i][1].axis('off')
        axs[i][1].set_title('Train Output')
    plt.tight_layout()
    plt.show()

filename = testDirectory + '19bb5feb.json'
task = readTaskFile(filename)

def getGridSizeComparison2(filename):
    data = readTaskFile(filename)
    trainSection = data['train']
    ident = data['id']
    numTrain = len(trainSection)
    result = {'allCorrespond': True, 'outputsSame': True}
    for i in range(numTrain):
        trainCase = trainSection[i]
        trainCaseInput = trainCase['input']
        trainCaseOutput = trainCase['output']
        sameY = len(trainCaseInput) == len(trainCaseOutput)
        sameX = len(trainCaseInput[0]) == len(trainCaseOutput[0])
        if not (sameX and sameY):
            result['allCorrespond'] = False
            break
    outputX = None
    outputY = None
    for i in range(numTrain):
        trainCase = trainSection[i]
        trainCaseOutput = trainCase['output']
        same = True
        if outputY == None:
            outputY = len(trainCaseOutput)
        elif not outputY == len(trainCaseOutput):
            same = False
        if outputX == None:
            outputX = len(trainCaseOutput[0])
        elif not outputX == len(trainCaseOutput[0]):
            same = False
        if not same:
            result['outputsSame'] = False
            break
    return {ident: result}

filename = testDirectory + '19bb5feb.json'
print(getGridSizeComparison2(filename))