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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 3,
|
6 |
+
"id": "bfa8dc49-e3e5-4c07-9ce8-576617bba299",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"# Importing relevant libraries\n",
|
11 |
+
"import pandas as pd\n",
|
12 |
+
"import numpy as np\n",
|
13 |
+
"import json"
|
14 |
+
]
|
15 |
+
},
|
16 |
+
{
|
17 |
+
"cell_type": "code",
|
18 |
+
"execution_count": 20,
|
19 |
+
"id": "faeb31dd-97d3-46e8-b956-2fac4ee6d463",
|
20 |
+
"metadata": {},
|
21 |
+
"outputs": [],
|
22 |
+
"source": [
|
23 |
+
"# Fixing and cleaning up the new categories mapping\n",
|
24 |
+
"with open(r\"./dataset/categories.txt\")as f:\n",
|
25 |
+
" CATEGORIES = json.load(f)\n",
|
26 |
+
"\n",
|
27 |
+
"CATEGORIES = set(CATEGORIES.keys())\n",
|
28 |
+
"\n",
|
29 |
+
"IGNORED_CATEGOREIES = set([\n",
|
30 |
+
" \"atom-ph\", \"bayes-an\", \"chao-dyn\", \"chem-ph\", \"cmp-lg\", \"comp-gas\", \"cond-mat\", \"dg-ga\", \"funct-an\",\n",
|
31 |
+
" \"mtrl-th\", \"patt-sol\", \"plasm-ph\", \"q-alg\", \"q-bio\", \"solv-int\", \"supr-con\", \"test\", \"test.dis-nn\", \n",
|
32 |
+
" \"test.mes-hall\", \"test.mtrl-sci\", \"test.soft\", \"test.stat-mech\", \"test.str-el\", \"test.supr-con\"\n",
|
33 |
+
"])\n",
|
34 |
+
"\n",
|
35 |
+
"CATEGORY_ALIASES = {\n",
|
36 |
+
" 'math.MP': 'math-ph',\n",
|
37 |
+
" 'stat.TH': 'math.ST',\n",
|
38 |
+
" 'math.IT': 'cs.IT',\n",
|
39 |
+
" 'q-fin.EC': 'econ.GN',\n",
|
40 |
+
" 'cs.SY': 'eess.SY',\n",
|
41 |
+
" 'cs.NA': 'math.NA'\n",
|
42 |
+
"}"
|
43 |
+
]
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"cell_type": "code",
|
47 |
+
"execution_count": null,
|
48 |
+
"id": "cd452c2f-6b3b-4ea0-812b-2886b33bf09b",
|
49 |
+
"metadata": {},
|
50 |
+
"outputs": [],
|
51 |
+
"source": [
|
52 |
+
"for I_CAT in IGNORED_CATEGOREIES:\n",
|
53 |
+
" if I_CAT in CATEGORIES:\n",
|
54 |
+
" CATEGORIES.remove(I_CAT)\n",
|
55 |
+
"\n",
|
56 |
+
"for C_ALIAS in CATEGORY_ALIASES:\n",
|
57 |
+
" if C_ALIAS in CATEGORIES:\n",
|
58 |
+
" CATEGORIES.remove(C_ALIAS)\n",
|
59 |
+
" CATEGORIES.add(CATEGORY_ALIASES[C_ALIAS])\n",
|
60 |
+
"\n",
|
61 |
+
"CATEGORIES = sorted(list(CATEGORIES))\n",
|
62 |
+
"CATEGORIES = {CATEGORIES[i]:i for i in range(len(CATEGORIES))}\n",
|
63 |
+
"with open(r\"./dataset/categories.txt\", \"w\")as f:\n",
|
64 |
+
" json.dump(CATEGORIES, f)"
|
65 |
+
]
|
66 |
+
},
|
67 |
+
{
|
68 |
+
"cell_type": "markdown",
|
69 |
+
"id": "eff91d4f-a44c-4d2b-a96f-0849354c8273",
|
70 |
+
"metadata": {},
|
71 |
+
"source": [
|
72 |
+
"#### For now only keep version 1"
|
73 |
+
]
|
74 |
+
},
|
75 |
+
{
|
76 |
+
"cell_type": "code",
|
77 |
+
"execution_count": 5,
|
78 |
+
"id": "db881114-a624-456b-a4ab-d4ec8a53ad60",
|
79 |
+
"metadata": {
|
80 |
+
"scrolled": true
|
81 |
+
},
|
82 |
+
"outputs": [],
|
83 |
+
"source": [
|
84 |
+
"# Cleaning the dataset, ie. removing duplicates, verifying none of the ignored tags are there and replace category aliases\n",
|
85 |
+
"test_minor_path = r\"dataset/full_text_sample/test_minor_cats_full.json\"\n",
|
86 |
+
"train_minor_path = r\"dataset/full_text_sample/train_minor_cats_full.json\""
|
87 |
+
]
|
88 |
+
},
|
89 |
+
{
|
90 |
+
"cell_type": "code",
|
91 |
+
"execution_count": 41,
|
92 |
+
"id": "e3bbe301-4e28-4a03-bd3c-1bbc99b43a0e",
|
93 |
+
"metadata": {
|
94 |
+
"scrolled": true
|
95 |
+
},
|
96 |
+
"outputs": [],
|
97 |
+
"source": [
|
98 |
+
"# Fixing test\n",
|
99 |
+
"pd_test_minor = pd.read_json(test_minor_path, lines=True)\n",
|
100 |
+
"\n",
|
101 |
+
"pd_test_minor.rename(columns={'major_category':'field','prime_category':'primary_subfield','abs_categories':'secondary_subfield'}, inplace=True)\n",
|
102 |
+
"pd_test_minor = pd_test_minor[~pd_test_minor['field'].isin(IGNORED_CATEGOREIES)]\n",
|
103 |
+
"pd_test_minor = pd_test_minor[(pd_test_minor['version'] == 1)]\n",
|
104 |
+
"if pd_test_minor['primary_subfield'].isin(CATEGORY_ALIASES.keys()).sum():\n",
|
105 |
+
" ValueError(\"IMPLEMENT CODE TO CHANGE CATEGORY_ALIASES\")\n",
|
106 |
+
" \n",
|
107 |
+
"pd_test_minor[['primary_subfield', \"secondary_subfield\"]].to_csv(\"dataset/baseline_datasets/test_field.csv\")\n",
|
108 |
+
"test_fields[\"Usage\"] = \"Public\"\n",
|
109 |
+
"pd_test_minor.drop(columns=['field', 'primary_subfield', \"secondary_subfield\"]).to_csv(r\"dataset/baseline_datasets/test_papers.csv\")"
|
110 |
+
]
|
111 |
+
},
|
112 |
+
{
|
113 |
+
"cell_type": "code",
|
114 |
+
"execution_count": 43,
|
115 |
+
"id": "5e0c00ef-29c5-44e3-a73e-e3417e07d53c",
|
116 |
+
"metadata": {
|
117 |
+
"scrolled": true
|
118 |
+
},
|
119 |
+
"outputs": [
|
120 |
+
{
|
121 |
+
"name": "stdout",
|
122 |
+
"output_type": "stream",
|
123 |
+
"text": [
|
124 |
+
"Chunk 1\n",
|
125 |
+
"Chunk 2\n",
|
126 |
+
"Chunk 3\n",
|
127 |
+
"Chunk 4\n",
|
128 |
+
"Chunk 5\n",
|
129 |
+
"Chunk 6\n",
|
130 |
+
"Chunk 7\n",
|
131 |
+
"Chunk 8\n",
|
132 |
+
"Chunk 9\n",
|
133 |
+
"Chunk 10\n",
|
134 |
+
"Chunk 11\n",
|
135 |
+
"Chunk 12\n"
|
136 |
+
]
|
137 |
+
}
|
138 |
+
],
|
139 |
+
"source": [
|
140 |
+
"# Fixing train\n",
|
141 |
+
"chunksize = 10**4\n",
|
142 |
+
"for i, pd_train_minor in enumerate(pd.read_json(train_minor_path, lines=True, chunksize=chunksize)):\n",
|
143 |
+
" print(f\"Chunk {i+1}\")\n",
|
144 |
+
" pd_train_minor.rename(columns={'major_category':'field','prime_category':'primary_subfield','abs_categories':'secondary_subfield'}, inplace=True)\n",
|
145 |
+
" pd_train_minor = pd_train_minor[~pd_train_minor['field'].isin(IGNORED_CATEGOREIES)]\n",
|
146 |
+
" pd_train_minor = pd_train_minor[(pd_train_minor['version'] == 1)]\n",
|
147 |
+
" if pd_train_minor['primary_subfield'].isin(CATEGORY_ALIASES.keys()).sum():\n",
|
148 |
+
" ValueError(\"IMPLEMENT CODE TO CHANGE CATEGORY_ALIASES \")\n",
|
149 |
+
"\n",
|
150 |
+
" if i==0:\n",
|
151 |
+
" pd_train_minor.to_csv(r\"dataset/baseline_datasets/train.csv\")\n",
|
152 |
+
" else:\n",
|
153 |
+
" pd_train_minor.to_csv(r\"dataset/baseline_datasets/train.csv\", header=False, mode='a')"
|
154 |
+
]
|
155 |
+
},
|
156 |
+
{
|
157 |
+
"cell_type": "markdown",
|
158 |
+
"id": "e7e5c112-eb78-4fb9-ad12-c3c4a90339a5",
|
159 |
+
"metadata": {},
|
160 |
+
"source": [
|
161 |
+
"#### Removing using titles"
|
162 |
+
]
|
163 |
+
},
|
164 |
+
{
|
165 |
+
"cell_type": "code",
|
166 |
+
"execution_count": 123,
|
167 |
+
"id": "e968294d-a933-4dba-8142-cc593f9cfbf9",
|
168 |
+
"metadata": {},
|
169 |
+
"outputs": [
|
170 |
+
{
|
171 |
+
"data": {
|
172 |
+
"text/html": [
|
173 |
+
"<div>\n",
|
174 |
+
"<style scoped>\n",
|
175 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
176 |
+
" vertical-align: middle;\n",
|
177 |
+
" }\n",
|
178 |
+
"\n",
|
179 |
+
" .dataframe tbody tr th {\n",
|
180 |
+
" vertical-align: top;\n",
|
181 |
+
" }\n",
|
182 |
+
"\n",
|
183 |
+
" .dataframe thead th {\n",
|
184 |
+
" text-align: right;\n",
|
185 |
+
" }\n",
|
186 |
+
"</style>\n",
|
187 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
188 |
+
" <thead>\n",
|
189 |
+
" <tr style=\"text-align: right;\">\n",
|
190 |
+
" <th></th>\n",
|
191 |
+
" <th>paper_id</th>\n",
|
192 |
+
" <th>version</th>\n",
|
193 |
+
" <th>yymm</th>\n",
|
194 |
+
" <th>created</th>\n",
|
195 |
+
" <th>title</th>\n",
|
196 |
+
" <th>secondary_subfield</th>\n",
|
197 |
+
" <th>abstract</th>\n",
|
198 |
+
" <th>primary_subfield</th>\n",
|
199 |
+
" <th>field</th>\n",
|
200 |
+
" <th>fulltext</th>\n",
|
201 |
+
" </tr>\n",
|
202 |
+
" </thead>\n",
|
203 |
+
" <tbody>\n",
|
204 |
+
" <tr>\n",
|
205 |
+
" <th>3888</th>\n",
|
206 |
+
" <td>1006.1164</td>\n",
|
207 |
+
" <td>2</td>\n",
|
208 |
+
" <td>1006</td>\n",
|
209 |
+
" <td>2010-06-10T05:56:49</td>\n",
|
210 |
+
" <td>Non-perturbative renormalization of quark mass...</td>\n",
|
211 |
+
" <td>hep-lat</td>\n",
|
212 |
+
" <td>We present an evaluation of the quark mass ren...</td>\n",
|
213 |
+
" <td>hep-lat</td>\n",
|
214 |
+
" <td>hep-lat</td>\n",
|
215 |
+
" <td>UTHEP-609\\nUTCCS-P-59\\n\\nNon-perturbative reno...</td>\n",
|
216 |
+
" </tr>\n",
|
217 |
+
" <tr>\n",
|
218 |
+
" <th>3957</th>\n",
|
219 |
+
" <td>1011.0157</td>\n",
|
220 |
+
" <td>1</td>\n",
|
221 |
+
" <td>1011</td>\n",
|
222 |
+
" <td>2010-10-31T14:09:45</td>\n",
|
223 |
+
" <td>Non-perturbative renormalization of quark mass...</td>\n",
|
224 |
+
" <td>hep-lat</td>\n",
|
225 |
+
" <td>We present an evaluation of the quark mass ren...</td>\n",
|
226 |
+
" <td>hep-lat</td>\n",
|
227 |
+
" <td>hep-lat</td>\n",
|
228 |
+
" <td>Non-perturbative renormalization of quark mass...</td>\n",
|
229 |
+
" </tr>\n",
|
230 |
+
" </tbody>\n",
|
231 |
+
"</table>\n",
|
232 |
+
"</div>"
|
233 |
+
],
|
234 |
+
"text/plain": [
|
235 |
+
" paper_id version yymm created \\\n",
|
236 |
+
"3888 1006.1164 2 1006 2010-06-10T05:56:49 \n",
|
237 |
+
"3957 1011.0157 1 1011 2010-10-31T14:09:45 \n",
|
238 |
+
"\n",
|
239 |
+
" title secondary_subfield \\\n",
|
240 |
+
"3888 Non-perturbative renormalization of quark mass... hep-lat \n",
|
241 |
+
"3957 Non-perturbative renormalization of quark mass... hep-lat \n",
|
242 |
+
"\n",
|
243 |
+
" abstract primary_subfield \\\n",
|
244 |
+
"3888 We present an evaluation of the quark mass ren... hep-lat \n",
|
245 |
+
"3957 We present an evaluation of the quark mass ren... hep-lat \n",
|
246 |
+
"\n",
|
247 |
+
" field fulltext \n",
|
248 |
+
"3888 hep-lat UTHEP-609\\nUTCCS-P-59\\n\\nNon-perturbative reno... \n",
|
249 |
+
"3957 hep-lat Non-perturbative renormalization of quark mass... "
|
250 |
+
]
|
251 |
+
},
|
252 |
+
"execution_count": 123,
|
253 |
+
"metadata": {},
|
254 |
+
"output_type": "execute_result"
|
255 |
+
}
|
256 |
+
],
|
257 |
+
"source": [
|
258 |
+
"# TBD whether we do this or not\n",
|
259 |
+
"duplicates = pd_test_minor[pd_test_minor.duplicated(subset=['title'], keep=False)]\n",
|
260 |
+
"duplicates"
|
261 |
+
]
|
262 |
+
},
|
263 |
+
{
|
264 |
+
"cell_type": "code",
|
265 |
+
"execution_count": null,
|
266 |
+
"id": "2a5ea0fe-cec7-4f76-891c-fb80852c75a3",
|
267 |
+
"metadata": {},
|
268 |
+
"outputs": [],
|
269 |
+
"source": []
|
270 |
+
},
|
271 |
+
{
|
272 |
+
"cell_type": "markdown",
|
273 |
+
"id": "cbc215aa-3b3d-4fbe-ae85-30ca8b9568e2",
|
274 |
+
"metadata": {},
|
275 |
+
"source": [
|
276 |
+
"#### Score metrics used on Kaggle"
|
277 |
+
]
|
278 |
+
},
|
279 |
+
{
|
280 |
+
"cell_type": "code",
|
281 |
+
"execution_count": 66,
|
282 |
+
"id": "88a5a364-f8f6-4817-aaf7-4e6ce43f5090",
|
283 |
+
"metadata": {},
|
284 |
+
"outputs": [],
|
285 |
+
"source": [
|
286 |
+
"def score(solution: pd.DataFrame, submission: pd.DataFrame, row_id_column_name: str) -> float:\n",
|
287 |
+
"\n",
|
288 |
+
" try:\n",
|
289 |
+
" # First calculate the primary subfield accuracy which is relatively simple\n",
|
290 |
+
" res = solution[\"primary_subfield\"]==submission[\"primary_subfield\"]\n",
|
291 |
+
" primary_field_acc = res.sum()/len(res)\n",
|
292 |
+
"\n",
|
293 |
+
" # We then calculate the secondary field accuracy\n",
|
294 |
+
" sol_sec_list = solution[\"secondary_subfield\"].tolist()\n",
|
295 |
+
" sub_sec_list = solution[\"secondary_subfield\"].tolist()\n",
|
296 |
+
"\n",
|
297 |
+
" secondary_field_acc = 0\n",
|
298 |
+
" for i in range(len(sol_sec_list)):\n",
|
299 |
+
" paper_secondary_field_pred = sub_sec_list[i].split(\" \")\n",
|
300 |
+
" paper_secondary_field_sol = sol_sec_list[i].split(\" \")\n",
|
301 |
+
"\n",
|
302 |
+
" diff = len(paper_secondary_field_pred)-len(paper_secondary_field_sol)\n",
|
303 |
+
"\n",
|
304 |
+
" if diff<0:\n",
|
305 |
+
" tot = 0\n",
|
306 |
+
" for sf_sol in paper_secondary_field_sol:\n",
|
307 |
+
" if sf_sol in paper_secondary_field_pred:\n",
|
308 |
+
" tot+=1\n",
|
309 |
+
" tot /= len(paper_secondary_field_sol)\n",
|
310 |
+
" else:\n",
|
311 |
+
" tot = 0\n",
|
312 |
+
" for sf_pred in paper_secondary_field_pred:\n",
|
313 |
+
" if sf_pred in paper_secondary_field_sol:\n",
|
314 |
+
" tot+=1\n",
|
315 |
+
"\n",
|
316 |
+
" tot /= len(paper_secondary_field_pred)\n",
|
317 |
+
"\n",
|
318 |
+
" secondary_field_acc += tot\n",
|
319 |
+
"\n",
|
320 |
+
" loss = secondary_field_acc/len(sol_sec_list) + primary_field_acc\n",
|
321 |
+
" \n",
|
322 |
+
" return loss\n",
|
323 |
+
"\n",
|
324 |
+
" except Exception as e:\n",
|
325 |
+
" \n",
|
326 |
+
" raise ParticipantVisibleError(f\"Error occurred: {str(e)}\")"
|
327 |
+
]
|
328 |
+
},
|
329 |
+
{
|
330 |
+
"cell_type": "code",
|
331 |
+
"execution_count": 67,
|
332 |
+
"id": "0497a438-13b7-47d9-a493-bd7c9fc1892d",
|
333 |
+
"metadata": {},
|
334 |
+
"outputs": [
|
335 |
+
{
|
336 |
+
"data": {
|
337 |
+
"text/plain": [
|
338 |
+
"2.0"
|
339 |
+
]
|
340 |
+
},
|
341 |
+
"execution_count": 67,
|
342 |
+
"metadata": {},
|
343 |
+
"output_type": "execute_result"
|
344 |
+
}
|
345 |
+
],
|
346 |
+
"source": [
|
347 |
+
"score(pd_sol, pd_sol, \"id\")"
|
348 |
+
]
|
349 |
+
},
|
350 |
+
{
|
351 |
+
"cell_type": "code",
|
352 |
+
"execution_count": null,
|
353 |
+
"id": "38a4812b-26ce-4063-b6a7-245544e8e02a",
|
354 |
+
"metadata": {},
|
355 |
+
"outputs": [],
|
356 |
+
"source": []
|
357 |
+
}
|
358 |
+
],
|
359 |
+
"metadata": {
|
360 |
+
"kernelspec": {
|
361 |
+
"display_name": "Python 3 (ipykernel)",
|
362 |
+
"language": "python",
|
363 |
+
"name": "python3"
|
364 |
+
},
|
365 |
+
"language_info": {
|
366 |
+
"codemirror_mode": {
|
367 |
+
"name": "ipython",
|
368 |
+
"version": 3
|
369 |
+
},
|
370 |
+
"file_extension": ".py",
|
371 |
+
"mimetype": "text/x-python",
|
372 |
+
"name": "python",
|
373 |
+
"nbconvert_exporter": "python",
|
374 |
+
"pygments_lexer": "ipython3",
|
375 |
+
"version": "3.9.19"
|
376 |
+
}
|
377 |
+
},
|
378 |
+
"nbformat": 4,
|
379 |
+
"nbformat_minor": 5
|
380 |
+
}
|