File size: 1,604 Bytes
f7685b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# script to test for any paper id overlap between the training and test set
# to run:
#   python test_paper_id.py

import pandas as pd
import re
from tqdm import tqdm

def test(dataset):
    print(f"Checking dataset: {dataset}")
    # load the training and test data
    train_file = f"data/raw/{dataset}/train_{dataset}_cats_full.json"
    # read json in chunks of 1000 rows, get the paper_id column and convert to list
    train_ids = []
    for df in tqdm(pd.read_json(train_file, lines=True, chunksize=1000, dtype=str)):
        train_ids.extend(df['paper_id'].tolist())
    test_file = f"data/raw/{dataset}/test_{dataset}_cats_full.json"
    test_ids = []
    for df in tqdm(pd.read_json(test_file, lines=True, chunksize=1000, dtype=str)):
        test_ids.extend(df['paper_id'].tolist())

    # check paper id formats
    ARXIV_IDENTIFIER_PATTERN = r"^\d{2}(0[1-9]|1[0-2])\.\d{4,5}$"
    OLD_ARXIV_IDENTIFIER_PATTERN = r"^[\w\-]+/\d{7}$"
    assert not any([re.match(ARXIV_IDENTIFIER_PATTERN, pid) is None for pid in train_ids if '.' in pid])
    assert not any([re.match(ARXIV_IDENTIFIER_PATTERN, pid) is None for pid in test_ids if '.' in pid])
    assert not any([re.match(OLD_ARXIV_IDENTIFIER_PATTERN, pid) is None for pid in train_ids if '/' in pid])
    assert not any([re.match(OLD_ARXIV_IDENTIFIER_PATTERN, pid) is None for pid in test_ids if '/' in pid])

    # check for overlap
    overlap = set(train_ids).intersection(set(test_ids))
    assert len(overlap) == 0, f"Overlap found: {overlap}"

if __name__ == "__main__":
    test("major")
    test("minor")
    print("All tests passed!")