File size: 2,655 Bytes
e00f999
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Check the dataset's files against their documented shape. Exit 1 on the first kind of problem found.

    python3 scripts/validate.py
"""
import csv
import sys

from common import ROLES, data, read_jsonl

problems = []


def check(cond, msg):
    if not cond:
        problems.append(msg)


labels = read_jsonl(data("roles", "labels.jsonl"))
ids = set()
projects = {r["project"]: r for r in csv.DictReader(open(data("projects.csv"), encoding="utf-8"))}
for l in labels:
    for k in ("id", "project", "commit", "module", "path", "url", "qualified_name", "name", "kind", "role", "structure"):
        check(k in l, f"{l.get('id')}: missing {k}")
    check(l["id"] not in ids, f"{l['id']}: duplicate id")
    ids.add(l["id"])
    check(l["id"] == f"{l['project']}/{l['qualified_name']}", f"{l['id']}: id is not project/qualified_name")
    check(l["role"] in ROLES, f"{l['id']}: unknown role {l['role']}")
    check(l["project"] in projects, f"{l['id']}: project not in projects.csv")
    check(l["commit"] == projects.get(l["project"], {}).get("commit"), f"{l['id']}: commit differs from projects.csv")
    check(l["path"].endswith(".java") and "src/main/java/" in l["path"], f"{l['id']}: path is not main Java")
    check(l["kind"] in ("class", "abstract class", "interface", "enum", "record", "annotation"), f"{l['id']}: kind {l['kind']}")
    check(set(l["structure"]["derived_marks"]) <= {"api", "ext"}, f"{l['id']}: unknown mark")
for p, r in projects.items():
    n = sum(1 for l in labels if l["project"] == p)
    check(n == int(r["labelled"]), f"{p}: {n} labels, projects.csv says {r['labelled']}")

csv_rows = list(csv.DictReader(open(data("roles", "labels.csv"), encoding="utf-8")))
check(len(csv_rows) == len(labels), "labels.csv and labels.jsonl differ in length")

features = read_jsonl(data("features", "features.jsonl"))
check({f["id"] for f in features} == ids, "features.jsonl does not cover exactly the labelled types")

try:
    commits = read_jsonl(data("history", "commits.jsonl"))
except FileNotFoundError:
    commits = []
    problems.append("data/history/commits.jsonl missing")
for c in commits:
    modified = {m["qualified_name"] for m in c["modified"]}
    for k, shown in c["shown_before"].items():
        check(set(shown) <= modified, f"{c['project']} {c['commit'][:10]}: {k} shows a type not modified")
    check(c["project"] in projects, f"{c['commit']}: unknown project")

if problems:
    print("\n".join(problems[:50]))
    print(f"{len(problems)} problem(s)")
    sys.exit(1)
print(f"ok: {len(labels)} labels, {len(features)} feature rows, {len(commits)} commits, {len(projects)} projects")