File size: 3,658 Bytes
806c783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import random
import re
from collections import defaultdict
SEED = 42
random.seed(SEED)

def generate_question_and_answer(grid_dict):
    """
    Generate a question + answer based on a grid dictionary.
    
    Returns:
        question (str): e.g.
            "In grid 1, starting from the torus at position (row 0, column 1),
             how many spheres are there to the right of it in the same row?"
        answer (int): the count of target shapes in that direction.
    """
    # 1. Parse all entries
    positions = []  # list of (grid_id, row, col, shape)
    for key, shape in grid_dict.items():
        m = re.match(r"grid_(\d+)_(\d+)_(\d+)", key)
        if not m:
            continue
        gid, r, c = map(int, m.groups())
        positions.append((gid, r, c, shape))

    # 2. Group by grid and compute bounds
    by_grid = defaultdict(list)
    for gid, r, c, shape in positions:
        by_grid[gid].append((r, c, shape))
    gid, cells = next(iter(by_grid.items()))
    rows = [r for r, c, _ in cells]
    cols = [c for r, c, _ in cells]
    max_row, max_col = max(rows), max(cols)

    # 3. Filter to interior cells if possible
    interior = [(r, c, s) for (r, c, s) in cells
                if 0 < r < max_row and 0 < c < max_col]
    if interior:
        ref_r, ref_c, ref_shape = random.choice(interior)
    else:
        # fallback for very thin grids
        ref_r, ref_c, ref_shape = random.choice(cells)

    # 4. Build possible directions (with predicates)
    directions = []
    if ref_c < max_col:
        directions.append((
            "to the right of it in the same row",
            lambda r, c: r == ref_r and c > ref_c
        ))
    if ref_c > 0:
        directions.append((
            "to the left of it in the same row",
            lambda r, c: r == ref_r and c < ref_c
        ))
    if ref_r < max_row:
        directions.append((
            "ahead it in the same column",
            lambda r, c: c == ref_c and r > ref_r
        ))
    if ref_r > 0:
        directions.append((
            "behind it in the same column",
            lambda r, c: c == ref_c and r < ref_r
        ))
    # Should always have at least one direction now
    direction_text, predicate = random.choice(directions)

    # 5. Pick a target shape (different from reference)
    other_shapes = list({s for _, _, s in cells if s != ref_shape})
    target_shape = random.choice(other_shapes)

    # 6. Compute the answer
    count = sum(
        1
        for (r, c, s) in cells
        if predicate(r, c) and s == target_shape
    )

    # 7. Formulate the question
    question = (
        f"In grid {gid}, starting from the {ref_shape} at position "
        f"(row {ref_r}, column {ref_c}), how many {target_shape}s are there "
        f"{direction_text}?"
    )

    return question, count, (max_row, max_col)

import os
import json

base_dir = "3D_DoYouSeeMe/visual_spatial"

os.listdir(base_dir)

data_list = []
for filename in os.listdir(base_dir):
    if filename.endswith(".json"):
        # print(filename)
        with open(os.path.join(base_dir, filename), "r") as f:
            data = f.read()
        data = json.loads(data)
        q, a, (max_row, max_col) = generate_question_and_answer(data)
        data_list.append({"filename": os.path.splitext(filename)[0] + ".png",
                          "question": q,
                          "answer": a,
                          "sweep": [max_row, max_col]})

import pandas as pd
df = pd.DataFrame(data_list)
df.to_csv(os.path.join(base_dir, "dataset_info.csv"), index=False)