# --- # jupyter: # jupytext: # cell_metadata_filter: -all # custom_cell_magics: kql # text_representation: # extension: .py # format_name: percent # format_version: '1.3' # jupytext_version: 1.11.2 # kernelspec: # display_name: arxiv-classifier-next # language: python # name: python3 # --- # %% # # parse arguments # from argparse import ArgumentParser parser = ArgumentParser() parser.add_argument("--dataset", "-d", type=str, default="minor") parser.add_argument("--split", "-s", type=str, default="train") parser.add_argument("--output_path", "-op", type=str, default="./") args, opt = parser.parse_known_args() dataset = args.dataset split = args.split output_path = args.output_path # %% # # Remaining imports # import pandas as pd import numpy as np import os from tqdm import tqdm # %% IGNORED_CATEGOREIES = set([ "atom-ph", "bayes-an", "chao-dyn", "chem-ph", "cmp-lg", "comp-gas", "cond-mat", "dg-ga", "funct-an", "mtrl-th", "patt-sol", "plasm-ph", "q-alg", "q-bio", "solv-int", "supr-con", "test", "test.dis-nn", "test.mes-hall", "test.mtrl-sci", "test.soft", "test.stat-mech", "test.str-el", "test.supr-con" ]) print("number of ignored categories: ", len(IGNORED_CATEGOREIES)) CATEGORY_ALIASES = { 'math.MP': 'math-ph', 'stat.TH': 'math.ST', 'math.IT': 'cs.IT', 'q-fin.EC': 'econ.GN', 'cs.SY': 'eess.SY', 'cs.NA': 'math.NA' } print("number of category aliases: ", len(CATEGORY_ALIASES)) # %% [markdown] # Rename columns to reflect standarized terminology: # - Field: Bio/cs/physics # - Subfield: Subcategories within each # - Primary subfield (bio., cs.LG): Given primary subfield, you can infer the field # - Secondary subfields: Includes primary subfield, but also includes any subfields that were tagged in the paper (1-5) # # Old terminology to standardized terminology translation: # - Prime category = Primary subfield # - Abstract category = secondary subfield # - Major category = field # %% file_path = os.path.join('data', 'raw', dataset, f"{split}_{dataset}_cats_full.json") save_dir = os.path.join(output_path, dataset) os.makedirs(save_dir, exist_ok=True) save_path = os.path.join(save_dir, f"{split}.json") print("Reading from file: ", file_path) # # delete file if it exists # if os.path.exists(save_path): # print("Overwriting existing file: ", save_path) # os.remove(save_path) # with open(save_path, 'a') as f: for i, original_df in tqdm(enumerate(pd.read_json(file_path, lines=True, chunksize=10**4))): # print(f"Chunk {i+1}") # print("original columns: ", original_df.columns) # Rename columns original_df.rename(columns={ 'major_category': 'field', 'prime_category': 'primary_subfield', 'abs_categories': 'secondary_subfield' }, inplace=True) # ignore entries from ignored categories (i.e., primary subfield) df = original_df[~original_df['field'].isin(IGNORED_CATEGOREIES)] df = df[(df['version'] == 1)] if df['primary_subfield'].isin(CATEGORY_ALIASES.keys()).sum(): ValueError("IMPLEMENT CODE TO CHANGE CATEGORY_ALIASES ") # # preprocess the chunk here # df_chunk = df.astype(str) # # convert the DataFrame to a JSON string # json_str = df_chunk.to_json(orient='records', lines=True) # # write the JSON string to file # f.write(json_str) # # add a newline character after each chunk except the last one # # if i != num_chunks - 1: # f.write('\n') if i == 0: df.astype(str).to_json(save_path, lines=True, orient="records") else: df.astype(str).to_json(save_path, lines=True, orient="records", mode='a') # %%