File size: 12,517 Bytes
52c4764
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import json
import os
from contextlib import contextmanager

import psycopg2
from psycopg2.extras import RealDictCursor

DATABASE_URL = os.environ['DATABASE_URL']

TABLE_ANNOTATORS = 'annotators_pp2'
TABLE_COMBINATIONS = 'combinations_pp2'
TABLE_ASSIGNMENTS = 'combo_assignments_pp2'
TABLE_ANNOTATIONS = 'annotations_pp2'


@contextmanager
def get_conn():
    conn = psycopg2.connect(DATABASE_URL)
    try:
        yield conn
        conn.commit()
    except Exception:
        conn.rollback()
        raise
    finally:
        conn.close()


def create_tables() -> None:
    with get_conn() as conn:
        with conn.cursor() as cur:
            cur.execute(
                f'''
                CREATE EXTENSION IF NOT EXISTS pgcrypto;

                CREATE TABLE IF NOT EXISTS {TABLE_ANNOTATORS} (
                    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                    code INT UNIQUE NOT NULL CHECK (code BETWEEN 1 AND 8),
                    created_at TIMESTAMPTZ DEFAULT now()
                );

                CREATE TABLE IF NOT EXISTS {TABLE_COMBINATIONS} (
                    id TEXT PRIMARY KEY,
                    base_id TEXT NOT NULL,
                    seed_id TEXT,
                    level INT,
                    class_tag TEXT,
                    ratio_type TEXT,
                    matchup_key TEXT NOT NULL,
                    model_left TEXT NOT NULL,
                    model_right TEXT NOT NULL,
                    n_total INT,
                    n_humans INT,
                    n_objects INT,
                    prompt TEXT,
                    prompt_zh TEXT,
                    humans JSONB,
                    objects JSONB
                );

                CREATE TABLE IF NOT EXISTS {TABLE_ASSIGNMENTS} (
                    combo_id TEXT PRIMARY KEY REFERENCES {TABLE_COMBINATIONS}(id) ON DELETE CASCADE,
                    group_key TEXT,
                    annotator_a_code INT NOT NULL CHECK (annotator_a_code BETWEEN 1 AND 8),
                    annotator_b_code INT NOT NULL CHECK (annotator_b_code BETWEEN 1 AND 8),
                    created_at TIMESTAMPTZ DEFAULT now()
                );

                CREATE TABLE IF NOT EXISTS {TABLE_ANNOTATIONS} (
                    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                    annotator_id UUID NOT NULL REFERENCES {TABLE_ANNOTATORS}(id),
                    combo_id TEXT NOT NULL REFERENCES {TABLE_COMBINATIONS}(id),
                    model_a TEXT NOT NULL,
                    model_b TEXT NOT NULL,
                    a_existence INT CHECK (a_existence IN (0, 1)),
                    a_appearance INT CHECK (a_appearance IN (0, 1)),
                    a_interaction INT CHECK (a_interaction IN (0, 1)),
                    b_existence INT CHECK (b_existence IN (0, 1)),
                    b_appearance INT CHECK (b_appearance IN (0, 1)),
                    b_interaction INT CHECK (b_interaction IN (0, 1)),
                    preference TEXT CHECK (preference IN ('A', 'B')),
                    prompt_ilogical BOOLEAN NOT NULL DEFAULT FALSE,
                    created_at TIMESTAMPTZ DEFAULT now(),
                    UNIQUE (annotator_id, combo_id)
                );
                '''
            )
            for code in range(1, 9):
                cur.execute(
                    f"INSERT INTO {TABLE_ANNOTATORS} (code) VALUES (%s) ON CONFLICT (code) DO NOTHING",
                    (code,),
                )


def clear_round_tables() -> None:
    with get_conn() as conn:
        with conn.cursor() as cur:
            cur.execute(f'TRUNCATE TABLE {TABLE_ANNOTATIONS} CASCADE')
            cur.execute(f'TRUNCATE TABLE {TABLE_ASSIGNMENTS} CASCADE')
            cur.execute(f'TRUNCATE TABLE {TABLE_COMBINATIONS} CASCADE')


def get_annotator_id_by_code(code: int) -> str:
    with get_conn() as conn:
        with conn.cursor() as cur:
            cur.execute(f'SELECT id FROM {TABLE_ANNOTATORS} WHERE code = %s', (code,))
            row = cur.fetchone()
            if not row:
                raise ValueError(f'annotator code not found: {code}')
            return str(row[0])


def get_candidate_combos(annotator_code: int, limit: int = 500) -> list[dict]:
    with get_conn() as conn:
        with conn.cursor(cursor_factory=RealDictCursor) as cur:
            cur.execute(
                f'''
                SELECT c.*
                FROM {TABLE_COMBINATIONS} c
                JOIN {TABLE_ASSIGNMENTS} a ON a.combo_id = c.id
                JOIN {TABLE_ANNOTATORS} an ON an.code = %s
                WHERE (a.annotator_a_code = %s OR a.annotator_b_code = %s)
                  AND NOT EXISTS (
                    SELECT 1 FROM {TABLE_ANNOTATIONS} ann
                    WHERE ann.combo_id = c.id AND ann.annotator_id = an.id
                  )
                ORDER BY c.seed_id, c.matchup_key, c.level DESC, c.id
                LIMIT %s
                ''',
                (annotator_code, annotator_code, annotator_code, limit),
            )
            return [dict(r) for r in cur.fetchall()]


def count_remaining(annotator_code: int) -> int:
    with get_conn() as conn:
        with conn.cursor() as cur:
            cur.execute(
                f'''
                SELECT COUNT(*)
                FROM {TABLE_COMBINATIONS} c
                JOIN {TABLE_ASSIGNMENTS} a ON a.combo_id = c.id
                JOIN {TABLE_ANNOTATORS} an ON an.code = %s
                WHERE (a.annotator_a_code = %s OR a.annotator_b_code = %s)
                  AND NOT EXISTS (
                    SELECT 1 FROM {TABLE_ANNOTATIONS} ann
                    WHERE ann.combo_id = c.id AND ann.annotator_id = an.id
                  )
                ''',
                (annotator_code, annotator_code, annotator_code),
            )
            return int(cur.fetchone()[0])


def save_annotation_by_code(data: dict) -> None:
    annotator_id = get_annotator_id_by_code(int(data['annotator_code']))
    payload = {
        'annotator_id': annotator_id,
        'combo_id': data['combo_id'],
        'model_a': data['model_a'],
        'model_b': data['model_b'],
        'a_existence': data['a_existence'],
        'a_appearance': data['a_appearance'],
        'a_interaction': data['a_interaction'],
        'b_existence': data['b_existence'],
        'b_appearance': data['b_appearance'],
        'b_interaction': data['b_interaction'],
        'preference': data['preference'],
        'prompt_ilogical': bool(data.get('prompt_ilogical', False)),
    }
    with get_conn() as conn:
        with conn.cursor() as cur:
            cur.execute(
                f'''
                INSERT INTO {TABLE_ANNOTATIONS}
                (annotator_id, combo_id, model_a, model_b,
                 a_existence, a_appearance, a_interaction,
                 b_existence, b_appearance, b_interaction,
                 preference, prompt_ilogical)
                VALUES
                (%(annotator_id)s, %(combo_id)s, %(model_a)s, %(model_b)s,
                 %(a_existence)s, %(a_appearance)s, %(a_interaction)s,
                 %(b_existence)s, %(b_appearance)s, %(b_interaction)s,
                 %(preference)s, %(prompt_ilogical)s)
                ON CONFLICT (annotator_id, combo_id) DO UPDATE SET
                    model_a = EXCLUDED.model_a,
                    model_b = EXCLUDED.model_b,
                    a_existence = EXCLUDED.a_existence,
                    a_appearance = EXCLUDED.a_appearance,
                    a_interaction = EXCLUDED.a_interaction,
                    b_existence = EXCLUDED.b_existence,
                    b_appearance = EXCLUDED.b_appearance,
                    b_interaction = EXCLUDED.b_interaction,
                    preference = EXCLUDED.preference,
                    prompt_ilogical = EXCLUDED.prompt_ilogical
                ''',
                payload,
            )


def get_progress(annotator_code: int) -> dict:
    annotator_id = get_annotator_id_by_code(annotator_code)
    with get_conn() as conn:
        with conn.cursor() as cur:
            cur.execute(f'SELECT COUNT(*) FROM {TABLE_ANNOTATIONS} WHERE annotator_id = %s', (annotator_id,))
            completed = int(cur.fetchone()[0])
            cur.execute(
                f'''
                SELECT COUNT(*)
                FROM {TABLE_COMBINATIONS} c
                JOIN {TABLE_ASSIGNMENTS} a ON a.combo_id = c.id
                WHERE (a.annotator_a_code = %s OR a.annotator_b_code = %s)
                ''',
                (annotator_code, annotator_code),
            )
            quota = int(cur.fetchone()[0])
            cur.execute(
                f'''
                SELECT COUNT(*) FROM (
                    SELECT combo_id
                    FROM {TABLE_ANNOTATIONS}
                    GROUP BY combo_id
                    HAVING COUNT(DISTINCT annotator_id) >= 2
                ) s
                '''
            )
            overall_completed = int(cur.fetchone()[0])
            cur.execute(f'SELECT COUNT(*) FROM {TABLE_COMBINATIONS}')
            overall_total = int(cur.fetchone()[0])
            return {
                'completed': completed,
                'quota': quota,
                'overall_completed': overall_completed,
                'overall_total': overall_total,
            }


def get_progress_board() -> dict:
    board = []
    overall_completed = 0
    overall_total = 0
    for code in range(1, 9):
        p = get_progress(code)
        board.append({'code': code, 'completed': p['completed'], 'quota': p['quota']})
        overall_completed = p['overall_completed']
        overall_total = p['overall_total']
    return {'annotators': board, 'overall_completed': overall_completed, 'overall_total': overall_total}


def upsert_combinations(combos: list[dict]) -> None:
    with get_conn() as conn:
        with conn.cursor() as cur:
            for c in combos:
                cur.execute(
                    f'''
                    INSERT INTO {TABLE_COMBINATIONS}
                    (id, base_id, seed_id, level, class_tag, ratio_type, matchup_key, model_left, model_right,
                     n_total, n_humans, n_objects, prompt, prompt_zh, humans, objects)
                    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
                    ON CONFLICT (id) DO UPDATE SET
                        base_id = EXCLUDED.base_id,
                        seed_id = EXCLUDED.seed_id,
                        level = EXCLUDED.level,
                        class_tag = EXCLUDED.class_tag,
                        ratio_type = EXCLUDED.ratio_type,
                        matchup_key = EXCLUDED.matchup_key,
                        model_left = EXCLUDED.model_left,
                        model_right = EXCLUDED.model_right,
                        n_total = EXCLUDED.n_total,
                        n_humans = EXCLUDED.n_humans,
                        n_objects = EXCLUDED.n_objects,
                        prompt = EXCLUDED.prompt,
                        prompt_zh = EXCLUDED.prompt_zh,
                        humans = EXCLUDED.humans,
                        objects = EXCLUDED.objects
                    ''',
                    (
                        c['id'], c['base_id'], c.get('seed_id', ''), int(c.get('level', 0)), c.get('class_tag', ''),
                        c.get('ratio_type', ''), c['matchup_key'], c['model_left'], c['model_right'],
                        int(c.get('n_total', 0)), int(c.get('n_humans', 0)), int(c.get('n_objects', 0)),
                        c.get('prompt', ''), c.get('prompt_zh', ''), json.dumps(c.get('humans', [])), json.dumps(c.get('objects', []))
                    ),
                )


def upsert_assignments(assignments: list[dict]) -> None:
    with get_conn() as conn:
        with conn.cursor() as cur:
            for a in assignments:
                cur.execute(
                    f'''
                    INSERT INTO {TABLE_ASSIGNMENTS}
                    (combo_id, group_key, annotator_a_code, annotator_b_code)
                    VALUES (%s, %s, %s, %s)
                    ON CONFLICT (combo_id) DO UPDATE SET
                        group_key = EXCLUDED.group_key,
                        annotator_a_code = EXCLUDED.annotator_a_code,
                        annotator_b_code = EXCLUDED.annotator_b_code
                    ''',
                    (a['combo_id'], a['group_key'], int(a['annotator_a_code']), int(a['annotator_b_code'])),
                )