Skip to content

ARC AGI

ARCAGIInput

Bases: DataModel

Input data model

Source code in synalinks/src/datasets/arcagi/arcagi.py
class ARCAGIInput(DataModel):
    """Input data model"""

    examples: List[TaskExample] = []
    grid: Grid

ARCAGIOutput

Bases: DataModel

Find the common rule that maps an input grid to an output grid.

Source code in synalinks/src/datasets/arcagi/arcagi.py
class ARCAGIOutput(DataModel):
    """Find the common rule that maps an input grid to an output grid."""

    grid: Grid

TaskExample

Bases: DataModel

An example of task

Source code in synalinks/src/datasets/arcagi/arcagi.py
class TaskExample(DataModel):
    """An example of task"""

    inputs: Grid
    outputs: Grid

fetch_and_format(task_name)

Fetch and format one task by name

Example:

x, y = synalinks.datasets.arcagi.fetch_and_format("62c24649")
Source code in synalinks/src/datasets/arcagi/arcagi.py
@synalinks_export("synalinks.datasets.arcagi.fetch_and_format")
def fetch_and_format(task_name):
    """
    Fetch and format one task by name

    Example:

    ```python
    x, y = synalinks.datasets.arcagi.fetch_and_format("62c24649")
    ```
    """
    if task_name in TRAINING_TASK_NAMES:
        url = f"{BASE_URL}/training/{task_name}.json"
    elif task_name in EVALUATION_TASK_NAMES:
        url = f"{BASE_URL}/evaluation/{task_name}.json"
    else:
        raise ValueError(
            f"Task '{task_name}' not recognized, make sure that the task name is valid."
        )

    x = None
    y = None

    file_path = file_utils.get_file(origin=url, progbar=False)
    with open(file_path, "r") as f:
        json_data = json.loads(f.read())
        trainset = json_data.get("train")
        testset = json_data.get("test")
        x = ARCAGIInput(grid=testset[0].get("input"))
        x.examples.append(
            TaskExample(
                inputs=tuple(map(tuple, trainset[0].get("input"))),
                outputs=tuple(map(tuple, trainset[0].get("output"))),
            )
        )
        x.examples.append(
            TaskExample(
                inputs=tuple(map(tuple, trainset[1].get("input"))),
                outputs=tuple(map(tuple, trainset[1].get("output"))),
            )
        )
        if len(trainset) > 2:
            x.examples.append(
                TaskExample(
                    inputs=tuple(map(tuple, trainset[2].get("input"))),
                    outputs=tuple(map(tuple, trainset[2].get("output"))),
                )
            )
        y = ARCAGIOutput(grid=tuple(map(tuple, testset[0].get("output"))))
    return x, y

get_input_data_model()

Returns ARC-AGI input data_model for pipeline configurations.

Returns:

Type Description
DataModel

The ARC-AGI input data_model

Source code in synalinks/src/datasets/arcagi/arcagi.py
@synalinks_export("synalinks.datasets.arcagi.get_input_data_model")
def get_input_data_model():
    """
    Returns ARC-AGI input data_model for pipeline configurations.

    Returns:
        (DataModel): The ARC-AGI input data_model
    """
    return ARCAGIInput

get_output_data_model()

Returns ARC-AGI output data_model for pipeline configurations.

Returns:

Type Description
DataModel

The ARC-AGI output data_model

Source code in synalinks/src/datasets/arcagi/arcagi.py
@synalinks_export("synalinks.datasets.arcagi.get_output_data_model")
def get_output_data_model():
    """
    Returns ARC-AGI output data_model for pipeline configurations.

    Returns:
        (DataModel): The ARC-AGI output data_model
    """
    return ARCAGIOutput

load_data(task_names=None)

Load and format data from github

Example:

(x_train, y_train), (x_test, y_test) = synalinks.datasets.arcagi.load_data()

Parameters:

Name Type Description Default
task_names list

Optional. The list of tasks to fetch.

None

Returns:

Type Description
tuple

The train and test data ready for training

Source code in synalinks/src/datasets/arcagi/arcagi.py
@synalinks_export("synalinks.datasets.arcagi.load_data")
def load_data(task_names=None):
    """
    Load and format data from github

    Example:

    ```python
    (x_train, y_train), (x_test, y_test) = synalinks.datasets.arcagi.load_data()
    ```

    Args:
        task_names (list): Optional. The list of tasks to fetch.

    Returns:
        (tuple): The train and test data ready for training
    """
    x_train = []
    y_train = []
    x_test = []
    y_test = []

    if not task_names:
        for task_name in TRAINING_TASK_NAMES:
            (x, y) = fetch_and_format(task_name)
            x_train.append(x)
            y_train.append(y)

        for task_name in EVALUATION_TASK_NAMES:
            (x, y) = fetch_and_format(task_name)
            x_test.append(x)
            y_test.append(y)

    else:
        for task_name in task_names:
            if task_name in TRAINING_TASK_NAMES:
                (x, y) = fetch_and_format(task_name)
                x_train.append(x)
                y_train.append(y)

            if task_name in EVALUATION_TASK_NAMES:
                (x, y) = fetch_and_format(task_name)
                x_test.append(x)
                x_test.append(y)

    x_train = np.array(x_train, dtype="object")
    y_train = np.array(y_train, dtype="object")
    x_test = np.array(x_test, dtype="object")
    y_test = np.array(y_test, dtype="object")

    return (x_train, y_train), (x_test, y_test)

plot_task(x=None, y_true=None, y_pred=None, task_name=None, to_file=None)

Plot a task for debugging purposes

Code Example:

synlinks.datasets.arcagi.plot_task(task_name="025d127b")

Example:

025d127b

Parameters:

Name Type Description Default
x DataModel

Optional. The task inputs.

None
y_true DataModel

Optional. The task target data.

None
y_pred DataModel

Optional. The prediction.

None
task_name str

The task name to fetch the data if not provided.

None
to_file str

The filepath where to save the figure.

None
Source code in synalinks/src/datasets/arcagi/arcagi.py
@synalinks_export("synalinks.datasets.arcagi.plot_task")
def plot_task(
    x=None,
    y_true=None,
    y_pred=None,
    task_name=None,
    to_file=None,
):
    """
    Plot a task for debugging purposes

    Code Example:

    ```python
    synlinks.datasets.arcagi.plot_task(task_name="025d127b")
    ```

    Example:

    ![025d127b](../../assets/025d127b.png)

    Args:
        x (DataModel): Optional. The task inputs.
        y_true (DataModel): Optional. The task target data.
        y_pred (DataModel): Optional. The prediction.
        task_name (str): The task name to fetch the data if not provided.
        to_file (str): The filepath where to save the figure.
    """
    if not x and not y_true and task_name:
        x, y_true = fetch_and_format(task_name)
        if not to_file:
            to_file = f"{task_name}.png"

    if not to_file:
        to_file = "arc_agi_task.png"

    cmap = colors.ListedColormap(
        [
            "#000000",
            "#0074D9",
            "#FF4136",
            "#2ECC40",
            "#FFDC00",
            "#AAAAAA",
            "#F012BE",
            "#FF851B",
            "#7FDBFF",
            "#870C25",
        ]
    )
    norm = colors.Normalize(vmin=0, vmax=9)

    nb_examples = len(x.examples)
    n = nb_examples + 1
    fig, axis = plt.subplots(2, n)

    for i, example in enumerate(x.examples):
        inputs, outputs = x.examples[i].inputs, x.examples[i].outputs
        inputs, outputs = np.array(inputs), np.array(outputs)

        axis[0, i].imshow(inputs, cmap=cmap, norm=norm)
        axis[0, i].set_title(f"x_train {i}")
        axis[0, i].grid(axis="both", color="white", linewidth=1)
        axis[0, i].set_yticks(np.arange(-0.5, inputs.shape[0], 1))
        axis[0, i].set_xticks(np.arange(-0.5, inputs.shape[1], 1))
        axis[0, i].set_yticklabels([])
        axis[0, i].set_xticklabels([])

        axis[1, i].imshow(outputs, cmap=cmap, norm=norm)
        axis[1, i].set_title(f"y_train {i}")
        axis[1, i].grid(axis="both", color="white", linewidth=1)
        axis[1, i].set_yticks(np.arange(-0.5, outputs.shape[0], 1))
        axis[1, i].set_xticks(np.arange(-0.5, outputs.shape[1], 1))
        axis[1, i].set_yticklabels([])
        axis[1, i].set_xticklabels([])

    inputs = np.array(x.grid)
    axis[0, nb_examples].imshow(inputs, cmap=cmap, norm=norm)
    axis[0, nb_examples].set_title("x_test")
    axis[0, nb_examples].grid(axis="both", color="white", linewidth=1)
    axis[0, nb_examples].set_yticks(np.arange(-0.5, inputs.shape[0], 1))
    axis[0, nb_examples].set_xticks(np.arange(-0.5, inputs.shape[1], 1))
    axis[0, nb_examples].set_yticklabels([])
    axis[0, nb_examples].set_xticklabels([])

    if y_true:
        outputs = np.array(y_true.grid)
        title = "y_true"
    if y_pred:
        outputs = np.array(y_pred.grid)
        title = "y_pred"
    axis[1, nb_examples].imshow(outputs, cmap=cmap, norm=norm)
    axis[1, nb_examples].set_title(title)
    axis[1, nb_examples].grid(axis="both", color="white", linewidth=1)
    axis[1, nb_examples].set_yticks(np.arange(-0.5, outputs.shape[0], 1))
    axis[1, nb_examples].set_xticks(np.arange(-0.5, outputs.shape[1], 1))
    axis[1, nb_examples].set_yticklabels([])
    axis[1, nb_examples].set_xticklabels([])

    plt.tight_layout()
    plt.savefig(to_file)
    plt.close()
    try:
        from IPython import display

        return display.Image(filename=to_file)
    except ImportError:
        pass