ag2435 commited on
Commit
18d335b
·
1 Parent(s): f597317

added all2023 v2 (secondary subfields + fulltext)

Browse files
Files changed (3) hide show
  1. all2023/val.json +2 -2
  2. preprocess_all2023_v2.py +121 -0
  3. util_preprocess.py +2 -2
all2023/val.json CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:016f8e4a4fe37bb0f1ff9f71d842e1d500db965ba6428d0a4124adfa47ce0f48
3
- size 409838372
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e38044ff7ccf7b6d4151dc9a7e30d366db5f43434dae6a346e7fd18cb9b3534a
3
+ size 13926552068
preprocess_all2023_v2.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ---
2
+ # jupyter:
3
+ # jupytext:
4
+ # cell_metadata_filter: -all
5
+ # custom_cell_magics: kql
6
+ # text_representation:
7
+ # extension: .py
8
+ # format_name: percent
9
+ # format_version: '1.3'
10
+ # jupytext_version: 1.11.2
11
+ # kernelspec:
12
+ # display_name: arxiv-classifier-next
13
+ # language: python
14
+ # name: python3
15
+ # ---
16
+
17
+ # %%
18
+ #
19
+ # parse arguments
20
+ #
21
+ from argparse import ArgumentParser
22
+ parser = ArgumentParser()
23
+ # parser.add_argument("--dataset", "-d", type=str, default="minor")
24
+ # parser.add_argument("--split", "-s", type=str, default="val")
25
+ parser.add_argument("--output_path", "-op", type=str, default="./")
26
+ parser.add_argument("--input_path", "-ip", type=str, default="./data",
27
+ help="Path to the input data files")
28
+ args, opt = parser.parse_known_args()
29
+
30
+ dataset = 'all2023' # args.dataset
31
+ split = 'val' # args.split
32
+ output_path = args.output_path
33
+ input_path = args.input_path
34
+
35
+ # %%
36
+ #
37
+ # Remaining imports
38
+ #
39
+ import pandas as pd
40
+ import os
41
+ from tqdm import tqdm
42
+ from util_preprocess import preprocess_primary_secondary
43
+
44
+ # %% [markdown]
45
+ # Rename columns to reflect standarized terminology:
46
+ # - Field: Bio/cs/physics
47
+ # - Subfield: Subcategories within each
48
+ # - Primary subfield (bio., cs.LG): Given primary subfield, you can infer the field
49
+ # - Secondary subfields: Includes primary subfield, but also includes any subfields that were tagged in the paper (1-5)
50
+ #
51
+ # Old terminology to standardized terminology translation:
52
+ # - Prime category = Primary subfield
53
+ # - Abstract category = secondary subfield
54
+ # - Major category = field
55
+
56
+ # %%
57
+ # file_path = os.path.join('data', 'raw', dataset, f"{split}_{dataset}_cats_full.json")
58
+ file_path = os.path.join(
59
+ input_path,
60
+ 'raw',
61
+ 'all2023',
62
+ 'all-2023-v2.tsv'
63
+ )
64
+ save_dir = os.path.join(output_path, dataset)
65
+ os.makedirs(save_dir, exist_ok=True)
66
+ save_path = os.path.join(save_dir, f"{split}.json")
67
+ print("Reading from file: ", file_path)
68
+
69
+ FEATURES = [
70
+ 'paper_id',
71
+ 'version',
72
+ 'yymm',
73
+ 'created',
74
+ 'title',
75
+ 'secondary_subfield',
76
+ 'abstract',
77
+ 'primary_subfield',
78
+ 'field',
79
+ 'fulltext'
80
+ ]
81
+
82
+ def get_fulltext(row):
83
+ filename = f"{row['paper_id']}v{row['version']}.txt"
84
+ filepath = os.path.join(
85
+ input_path,
86
+ 'raw',
87
+ 'all2023',
88
+ 'fulltext',
89
+ row['paper_id'].split('.')[0], # get yymm subfolder
90
+ filename,
91
+ )
92
+ try:
93
+ with open(filepath, 'r') as f:
94
+ fulltext = f.read()
95
+ except FileNotFoundError:
96
+ fulltext = None
97
+ return fulltext
98
+
99
+ # Ignore all quotes by setting quoting=3 (corresponds to csv.QUOTE_NONE)
100
+ for i, original_df in tqdm(enumerate(pd.read_csv(file_path, sep='\t', chunksize=10, dtype=str, quoting=3))):
101
+ # set non-existent features to empty string
102
+ for feature in FEATURES:
103
+ if feature not in original_df.columns:
104
+ original_df[feature] = ""
105
+ # convert categories to primary and secondary subfield
106
+ original_df.loc[:, 'primary_subfield'] = original_df['categories'].apply(lambda x : x.split()[0])
107
+ # keep the secondary_subfield a string, will be converted to list in preprocess_primary_secondary()
108
+ original_df.loc[:, 'secondary_subfield'] = original_df['categories']
109
+ # Apply preprocessing rules
110
+ df = preprocess_primary_secondary(original_df)
111
+ # Get fulltext for each paper from .txt files
112
+ df.loc[:, 'fulltext'] = df.apply(get_fulltext, axis=1)
113
+
114
+ # convert all columns to string per huggingface requirements
115
+ if i == 0:
116
+ df.to_json(save_path, lines=True, orient="records")
117
+ else:
118
+ df.to_json(save_path, lines=True, orient="records", mode='a')
119
+
120
+ print("Saved to: ", save_path)
121
+ # %%
util_preprocess.py CHANGED
@@ -1,4 +1,4 @@
1
- from arxiv_classifier.utils import (CATEGORY_ALIASES,
2
  GENERAL_CATEGORIES,
3
  IGNORED_CATEGOREIES,
4
  SUBFIELD_MAP)
@@ -38,7 +38,7 @@ def preprocess_primary_secondary(df):
38
 
39
  # tests
40
  # assert that each primary subfield is in SUBFIELD_MAP
41
- assert all(df['primary_subfield'].isin(SUBFIELD_MAP.keys()))
42
  # assert that each secondary subfield is in SUBFIELD_MAP
43
  # assert all(df['secondary_subfield'].apply(lambda x: all([subfield in SUBFIELD_MAP.keys() for subfield in x])))
44
  # return preprocessed dataframe
 
1
+ from arxiv_classifier.utils.maps import (CATEGORY_ALIASES,
2
  GENERAL_CATEGORIES,
3
  IGNORED_CATEGOREIES,
4
  SUBFIELD_MAP)
 
38
 
39
  # tests
40
  # assert that each primary subfield is in SUBFIELD_MAP
41
+ assert all(df['primary_subfield'].isin(SUBFIELD_MAP.keys())), df[~df['primary_subfield'].isin(SUBFIELD_MAP.keys())]
42
  # assert that each secondary subfield is in SUBFIELD_MAP
43
  # assert all(df['secondary_subfield'].apply(lambda x: all([subfield in SUBFIELD_MAP.keys() for subfield in x])))
44
  # return preprocessed dataframe